1 // commit: 19e35c500bd2b5e6146e42705ab9b69c155a2006 2011-02-17
2 // commit: 187fe29d5b89644b68cade75a34257a1c32a75f6 2011-02-17
3 // non-standard musl specific behaviour
4 // daemon should not fork in case of failure of chdir or open, but
5 // since setsid and fork may still fail after fork this behaviour
7 #define _DEFAULT_SOURCE 1
20 int r
, pid
, fd
[2], fdout
, s
;
25 t_error("pipe failed: %s\n", strerror(errno
));
30 t_error("dup(1) failed: %s\n", strerror(errno
));
35 t_error("fork failed: %s\n", strerror(errno
));
40 /* exhausting all fds makes open("/dev/null") fail in daemon */
45 if (dup2(fdout
,1) == -1) {
46 write(fdout
, "ERROR:\n", 7);
47 t_error("failed to dup pipe fd for communicating results: %s\n", strerror(errno
));
50 t_error("daemon should have failed\n");
52 t_error("daemon should have failed with %d [EMFILE] got %d [%s]\n", EMFILE
, errno
, strerror(errno
));
53 if (getpid() != pid
|| getppid() == 1)
54 t_error("daemon forked despite failure: ppid is %d, pid is %d, old pid is %d\n",
55 getppid(), getpid(), pid
);
56 if (write(fd
[1], "1" + !t_status
, 1) != 1)
57 t_error("write failed: %s\n", strerror(errno
));
61 if (waitpid(r
, &s
, 0) != r
)
62 t_error("waitpid failed: %s\n", strerror(errno
));
63 else if (!WIFEXITED(s
))
64 t_error("child exited abnormally (signal %d)\n", WIFSIGNALED(s
) ? WTERMSIG(s
) : 0);
65 else if (WEXITSTATUS(s
))
66 t_error("child exited with %d\n", WEXITSTATUS(s
));
67 r
= read(fd
[0], &c
, 1);
69 t_error("read failed: %s\n", strerror(errno
));
71 t_error("read failed: child did not send its exit status\n");
73 t_error("child failed\n");