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 if (write(STDERR_FILENO, buffer, 1) != 1) {
47 daemonized.write_to_stderr = errno;
48 if (daemonized.write_to_stderr == 0)
49 daemonized.write_to_stderr = -1;
51 daemonized.write_to_stderr = 0;
53 /* Make sure parent exits. */
54 while (getppid() == pid)
56 daemonized.ppid = getppid();
57 if (write(fds[1], &daemonized, sizeof(daemonized))
58 != sizeof(daemonized))
63 if (read(fds[0], &daemonized, sizeof(daemonized)) != sizeof(daemonized))
64 err(1, "Failed read");
66 ok1(daemonized.pid != pid);
67 ok1(daemonized.ppid == 1);
68 ok1(daemonized.in_root_dir);
69 ok1(daemonized.read_from_stdin == EBADF);
70 ok1(daemonized.write_to_stdout == EBADF);
71 ok1(daemonized.write_to_stderr == 0);