1 #include <ccan/daemon_with_notify/daemon_with_notify.h>
2 #include <ccan/daemon_with_notify/daemon_with_notify.c>
3 #include <ccan/tap/tap.h>
14 int read_from_stdin, write_to_stdout, write_to_stderr;
17 int main(int argc, char *argv[])
20 struct child_data daemonized;
26 err(1, "Failed pipe");
28 /* Since daemonize forks and parent exits, we need to fork
32 err(1, "Failed fork");
39 /* Keep valgrind happy about uninitialized bytes. */
40 memset(&daemonized, 0, sizeof(daemonized));
41 daemonized.pid = getpid();
42 daemonized.in_root_dir = (getcwd(buffer, 2) != NULL);
43 daemonized.read_from_stdin
44 = read(STDIN_FILENO, buffer, 1) == -1 ? errno : 0;
45 daemonized.write_to_stdout
46 = write(STDOUT_FILENO, buffer, 1) == -1 ? errno : 0;
47 if (write(STDERR_FILENO, buffer, 1) != 1) {
48 daemonized.write_to_stderr = errno;
49 if (daemonized.write_to_stderr == 0)
50 daemonized.write_to_stderr = -1;
52 daemonized.write_to_stderr = 0;
54 /* Make sure parent exits. */
55 while (getppid() == pid)
57 daemonized.ppid = getppid();
58 if (write(fds[1], &daemonized, sizeof(daemonized))
59 != sizeof(daemonized))
64 if (read(fds[0], &daemonized, sizeof(daemonized)) != sizeof(daemonized))
65 err(1, "Failed read");
67 ok1(daemonized.pid != pid);
68 #if 0 /* Believe it or not, this fails under Ubuntu 13.10 (Upstart) */
69 ok1(daemonized.ppid == 1);
71 ok1(daemonized.in_root_dir);
72 ok1(daemonized.read_from_stdin == 0);
73 ok1(daemonized.write_to_stdout == 0);
74 ok1(daemonized.write_to_stderr == 0);