1 #include <ccan/closefrom/closefrom.h>
2 /* Include the C files directly. */
3 #include <ccan/closefrom/closefrom.c>
4 #include <ccan/tap/tap.h>
10 /* Open a pipe, do closefrom, check pipe no longer works. */
22 /* Writing to the write end should succeed, the
25 wres = write(fds[1], &buf, 1);
26 } while ((wres < 0) && (errno == EINTR));
30 closefrom(STDERR_FILENO + 1);
32 /* Writing to the write end should fail because
33 * everything should be closed. */
35 wres = write(fds[1], &buf, 1);
36 } while ((wres < 0) && (errno == EINTR));
38 return (wres < 0) && (errno == EBADF);
41 /* Open a pipe, fork, do closefrom in child, read pipe from parent,
42 * parent should see EOF.
62 closefrom(STDERR_FILENO + 1);
67 /* Close write end of pipe. */
71 rres = read(fds[0], &buf, 1);
72 } while ((rres < 0) && (errno == EINTR));
74 /* Should have seen EOF. */
79 waitpid(child, NULL, 0);
80 closefrom(STDERR_FILENO + 1);
85 /* Open a pipe, fork, in child set the write end to fd #3,
86 * in parent set the read end to fd #3, send a byte from
87 * child to parent, check.
90 int fork_communicate(void)
112 /* Move write end to fd #3. */
114 if (dup2(fds[1], 3) < 0)
123 wres = write(fds[1], &wbuf, 1);
124 } while ((wres < 0) && (errno == EINTR));
132 /* Move read end to fd #3. */
134 if (dup2(fds[0], 3) < 0)
142 /* Wait for child to finish. */
143 waitpid(child, &status, 0);
144 if (!WIFEXITED(status))
146 if (WEXITSTATUS(status) != 0)
151 rres = read(fds[0], &rbuf, 1);
152 } while ((rres < 0) && (errno == EINTR));
157 /* Should get same byte as what was sent. */
161 /* Next attempt to read should EOF. */
163 rres = read(fds[0], &rbuf, 1);
164 } while ((rres < 0) && (errno == EINTR));
180 /* Limit closefrom. */
183 /* This is how many tests you plan to run */
188 ok1(fork_communicate());
190 /* This exits depending on whether all tests passed */
191 return exit_status();