1 #include <ccan/daemonize/daemonize.h>
2 #include <ccan/daemonize/daemonize.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");
38 /* Keep valgrind happy about uninitialized bytes. */
39 memset(&daemonized, 0, sizeof(daemonized));
40 daemonized.pid = getpid();
41 daemonized.in_root_dir = (getcwd(buffer, 2) != NULL);
42 daemonized.read_from_stdin
43 = read(STDIN_FILENO, buffer, 1) == -1 ? errno : 0;
44 daemonized.write_to_stdout
45 = write(STDOUT_FILENO, buffer, 1) == -1 ? errno : 0;
46 daemonized.write_to_stderr
47 = write(STDERR_FILENO, buffer, 1) == -1 ? errno : 0;
49 /* Make sure parent exits. */
50 while (getppid() == pid)
52 daemonized.ppid = getppid();
53 if (write(fds[1], &daemonized, sizeof(daemonized))
54 != sizeof(daemonized))
59 if (read(fds[0], &daemonized, sizeof(daemonized)) != sizeof(daemonized))
60 err(1, "Failed read");
62 ok1(daemonized.pid != pid);
63 ok1(daemonized.ppid == 1);
64 ok1(daemonized.in_root_dir);
65 ok1(daemonized.read_from_stdin == EBADF);
66 ok1(daemonized.write_to_stdout == EBADF);
67 ok1(daemonized.write_to_stderr == EBADF);