#include #include #include #include #include #include #include #include #include #define CHECK(OP) check(#OP, OP) static int check(const char *what, int n) { if(n < 0) { perror(what); exit(EXIT_FAILURE); } return n; } static void pollit(const char *what, int fd) { struct pollfd ufd; int n; char buf[1]; memset(&ufd, 0, sizeof ufd); ufd.fd = fd; ufd.events = POLLIN; CHECK(poll(&ufd, 1, 1000)); switch(ufd.revents & (POLLIN|POLLHUP)) { case POLLIN: printf("%s: POLLIN\n", what); break; case POLLHUP: printf("%s: POLLHUP\n", what); break; case POLLIN|POLLHUP: printf("%s: POLLIN|POLLHUP\n", what); break; case POLLERR: printf("%s: POLLERR\n", what); break; default: printf("%s: %#x\n", what, (unsigned)ufd.revents); break; } CHECK(fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL))); n = read(fd, buf, 1); if(n != 0) printf("%s: wasn't at EOF after all (%d, %d=\"%s\")\n", what, n, errno, strerror(errno)); } int main(void) { int p[2], fd; system("uname -a"); CHECK(pipe(p)); CHECK(close(p[1])); pollit("pipe", p[0]); CHECK(socketpair(PF_UNIX, SOCK_STREAM, 0, p)); CHECK(close(p[1])); pollit("socketpair", p[0]); CHECK(socketpair(PF_UNIX, SOCK_STREAM, 0, p)); CHECK(shutdown(p[1], SHUT_WR)); pollit("SHUT_WR socketpair", p[0]); CHECK(socketpair(PF_UNIX, SOCK_STREAM, 0, p)); CHECK(shutdown(p[0], SHUT_RD)); pollit("SHUT_RD socketpair", p[0]); unlink("poll-test-tmp"); CHECK((fd = open("poll-test-tmp", O_CREAT|O_EXCL|O_RDWR, 0600))); pollit("regular", fd); return 0; } /* Local Variables: c-basic-offset:2 comment-column:40 End: */