4 #include <sys/socket.h>
14 #define FIFONAME "fifo.tmp"
18 #define FT_SOCKETPAIR 1
23 decode_events(int events)
35 case POLLIN | POLLHUP:
36 result = "POLLIN | POLLHUP";
39 asprintf(&ncresult, "%#x", events);
47 report_state(const char *state)
50 printf(" %s state %s: ",
51 filetype == FT_PIPE ? "Pipe" :
52 filetype == FT_SOCKETPAIR ? "Sock" : "FIFO",
57 report(int num, const char *state, int expected, int got, int res,
61 if (res != res_expected) {
62 printf("not ok %-2d", num);
64 printf("poll result %d expected %d. ",
68 printf("ok %-2d ", num);
70 printf("not ok %-2d", num);
73 printf("expected %s; got %s\n", decode_events(expected),
80 static volatile sig_atomic_t state;
83 catch(int sig __unused)
90 child(int fd, int num)
96 if (filetype == FT_FIFO) {
97 fd = open(FIFONAME, O_RDONLY | O_NONBLOCK);
99 err(1, "open for read");
104 if (filetype == FT_FIFO) {
105 if ((res = poll(&pfd, 1, 0)) < 0)
107 report(num++, "0", 0, pfd.revents, res, 0);
114 if (filetype != FT_FIFO) {
116 * The connection cannot be reestablished. Use the code that
117 * delays the read until after the writer disconnects since
118 * that case is more interesting.
123 if ((res = poll(&pfd, 1, 0)) < 0)
125 report(num++, "1", 0, pfd.revents, res, 0);
131 if ((res = poll(&pfd, 1, 0)) < 0)
133 report(num++, "2", POLLIN, pfd.revents, res, 1);
134 if (read(fd, buf, sizeof buf) != 1)
136 if ((res = poll(&pfd, 1, 0)) < 0)
138 report(num++, "2a", 0, pfd.revents, res, 0);
144 if ((res = poll(&pfd, 1, 0)) < 0)
146 report(num++, "3", POLLHUP, pfd.revents, res, 1);
150 * Now we expect a new writer, and a new connection too since
151 * we read all the data. The only new point is that we didn't
152 * start quite from scratch since the read fd is not new. Check
153 * startup state as above, but don't do the read as above.
159 if ((res = poll(&pfd, 1, 0)) < 0)
161 report(num++, "4", 0, pfd.revents, res, 0);
167 if ((res = poll(&pfd, 1, 0)) < 0)
169 report(num++, "5", POLLIN, pfd.revents, res, 1);
176 * Now we have no writer, but should still have data from the old
177 * writer. Check that we have both a data-readable condition and a
178 * hangup condition, and that the data can be read in the usual way.
179 * Since Linux does this, programs must not quit reading when they
180 * see POLLHUP; they must see POLLHUP without POLLIN (or another
181 * input condition) before they decide that there is EOF. gdb-6.1.1
182 * is an example of a broken program that quits on POLLHUP only --
183 * see its event-loop.c.
185 if ((res = poll(&pfd, 1, 0)) < 0)
187 report(num++, "6", POLLIN | POLLHUP, pfd.revents, res, 1);
188 if (read(fd, buf, sizeof buf) != 1)
190 if ((res = poll(&pfd, 1, 0)) < 0)
192 report(num++, "6a", POLLHUP, pfd.revents, res, 1);
193 if (filetype == FT_FIFO) {
195 * Check that POLLHUP is sticky for a new reader and for
198 fd2 = open(FIFONAME, O_RDONLY | O_NONBLOCK);
200 err(1, "open for read");
202 if ((res = poll(&pfd, 1, 0)) < 0)
204 report(num++, "6b", POLLHUP, pfd.revents, res, 1);
206 if ((res = poll(&pfd, 1, 0)) < 0)
208 report(num++, "6c", POLLHUP, pfd.revents, res, 1);
210 if ((res = poll(&pfd, 1, 0)) < 0)
212 report(num++, "6d", POLLHUP, pfd.revents, res, 1);
226 if (filetype == FT_FIFO) {
227 fd = open(FIFONAME, O_WRONLY | O_NONBLOCK);
229 err(1, "open for write");
236 if (write(fd, "", 1) != 1)
244 err(1, "close for write");
250 if (filetype != FT_FIFO)
252 fd = open(FIFONAME, O_WRONLY | O_NONBLOCK);
254 err(1, "open for write");
260 if (write(fd, "", 1) != 1)
268 err(1, "close for write");
284 signal(SIGUSR1, catch);
286 for (filetype = 0; filetype < FT_END; filetype++) {
289 if (mkfifo(FIFONAME, 0666) != 0)
295 if (socketpair(AF_UNIX, SOCK_STREAM, AF_UNSPEC,
297 err(1, "socketpair");
305 switch (cpid = fork()) {
317 num += filetype == FT_FIFO ? 12 : 4;
319 (void)unlink(FIFONAME);