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
19 int r, pid, fd[2], fdout, s;
24 t_error("pipe failed: %s\n", strerror(errno));
29 t_error("dup(1) failed: %s\n", strerror(errno));
34 t_error("fork failed: %s\n", strerror(errno));
39 /* exhausting all fds makes open("/dev/null") fail in daemon */
44 if (dup2(fdout,1) == -1) {
45 write(fdout, "ERROR:\n", 7);
46 t_error("failed to dup pipe fd for communicating results: %s\n", strerror(errno));
49 t_error("daemon should have failed\n");
51 t_error("daemon should have failed with %d [EMFILE] got %d [%s]\n", EMFILE, errno, strerror(errno));
52 if (getpid() != pid || getppid() == 1)
53 t_error("daemon forked despite failure: ppid is %d, pid is %d, old pid is %d\n",
54 getppid(), getpid(), pid);
55 if (write(fd[1], "1" + !t_status, 1) != 1)
56 t_error("write failed: %s\n", strerror(errno));
60 if (waitpid(r, &s, 0) != r)
61 t_error("waitpid failed: %s\n", strerror(errno));
62 else if (!WIFEXITED(s))
63 t_error("child exited abnormally (signal %d)\n", WIFSIGNALED(s) ? WTERMSIG(s) : 0);
64 else if (WEXITSTATUS(s))
65 t_error("child exited with %d\n", WEXITSTATUS(s));
66 r = read(fd[0], &c, 1);
68 t_error("read failed: %s\n", strerror(errno));
70 t_error("read failed: child did not send its exit status\n");
72 t_error("child failed\n");