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(int num, const char *state, int expected, int got)
50 printf("ok %-2d ", num);
52 printf("not ok %-2d", num);
53 printf(" %s state %s: expected %s; got %s\n",
54 filetype == FT_PIPE ? "Pipe" :
55 filetype == FT_SOCKETPAIR ? "Sock" : "FIFO",
56 state, decode_events(expected), decode_events(got));
62 static volatile sig_atomic_t state;
71 child(int fd, int num)
77 if (filetype == FT_FIFO) {
78 fd = open(FIFONAME, O_RDONLY | O_NONBLOCK);
80 err(1, "open for read");
85 if (filetype == FT_FIFO) {
86 if (poll(&pfd, 1, 0) < 0)
88 report(num++, "0", 0, pfd.revents);
95 if (filetype != FT_FIFO) {
97 * The connection cannot be reestablished. Use the code that
98 * delays the read until after the writer disconnects since
99 * that case is more interesting.
104 if (poll(&pfd, 1, 0) < 0)
106 report(num++, "1", 0, pfd.revents);
112 if (poll(&pfd, 1, 0) < 0)
114 report(num++, "2", POLLIN, pfd.revents);
115 if (read(fd, buf, sizeof buf) != 1)
117 if (poll(&pfd, 1, 0) < 0)
119 report(num++, "2a", 0, pfd.revents);
125 if (poll(&pfd, 1, 0) < 0)
127 report(num++, "3", POLLHUP, pfd.revents);
131 * Now we expect a new writer, and a new connection too since
132 * we read all the data. The only new point is that we didn't
133 * start quite from scratch since the read fd is not new. Check
134 * startup state as above, but don't do the read as above.
140 if (poll(&pfd, 1, 0) < 0)
142 report(num++, "4", 0, pfd.revents);
148 if (poll(&pfd, 1, 0) < 0)
150 report(num++, "5", POLLIN, pfd.revents);
157 * Now we have no writer, but should still have data from the old
158 * writer. Check that we have both a data-readable condition and a
159 * hangup condition, and that the data can be read in the usual way.
160 * Since Linux does this, programs must not quit reading when they
161 * see POLLHUP; they must see POLLHUP without POLLIN (or another
162 * input condition) before they decide that there is EOF. gdb-6.1.1
163 * is an example of a broken program that quits on POLLHUP only --
164 * see its event-loop.c.
166 if (poll(&pfd, 1, 0) < 0)
168 report(num++, "6", POLLIN | POLLHUP, pfd.revents);
169 if (read(fd, buf, sizeof buf) != 1)
171 if (poll(&pfd, 1, 0) < 0)
173 report(num++, "6a", POLLHUP, pfd.revents);
174 if (filetype == FT_FIFO) {
176 * Check that POLLHUP is sticky for a new reader and for
179 fd2 = open(FIFONAME, O_RDONLY | O_NONBLOCK);
181 err(1, "open for read");
183 if (poll(&pfd, 1, 0) < 0)
185 report(num++, "6b", POLLHUP, pfd.revents);
187 if (poll(&pfd, 1, 0) < 0)
189 report(num++, "6c", POLLHUP, pfd.revents);
191 if (poll(&pfd, 1, 0) < 0)
193 report(num++, "6d", POLLHUP, pfd.revents);
207 if (filetype == FT_FIFO) {
208 fd = open(FIFONAME, O_WRONLY | O_NONBLOCK);
210 err(1, "open for write");
217 if (write(fd, "", 1) != 1)
225 err(1, "close for write");
231 if (filetype != FT_FIFO)
233 fd = open(FIFONAME, O_WRONLY | O_NONBLOCK);
235 err(1, "open for write");
241 if (write(fd, "", 1) != 1)
249 err(1, "close for write");
265 signal(SIGUSR1, catch);
267 for (filetype = 0; filetype < FT_END; filetype++) {
270 if (mkfifo(FIFONAME, 0666) != 0)
276 if (socketpair(AF_UNIX, SOCK_STREAM, AF_UNSPEC,
278 err(1, "socketpair");
286 switch (cpid = fork()) {
298 num += filetype == FT_FIFO ? 12 : 4;
300 (void)unlink(FIFONAME);