// fifo-select - does select(2) work with FIFO? #include #include #include #include #include #include #include #include #include #define FIFO_MSGMAX 127U inline static void set_blocking(int fd) { int flags; flags = fcntl(fd, F_GETFL, 0); if (flags == -1) err(1, "fcntl"); flags &= ~(O_NONBLOCK); if (fcntl(fd, F_SETFL, flags) == -1) err(1, "fcntl"); } int main(int argc, char *argv[]) { char buf[FIFO_MSGMAX + 1], *fname; const int nfd = 1; fd_set fds; int fd, ret; if (argc != 2) { fputs("Usage: fifo-select fifo-name\n", stderr); exit(1); } fname = argv[1]; ret = mkfifo(fname, 0600); if (ret == -1 && errno != EEXIST) err(1, "mkfifo"); if ((fd = open(fname, O_RDONLY | O_NONBLOCK)) == -1) err(1, "open '%s'", fname); FD_ZERO(&fds); FD_SET(fd, &fds); //set_blocking(fd); while (1) { fprintf(stderr, "DBG select...\n"); // Always wedges here on OpenBSD, regardless of how I // fiddle with the blocking. I'm guessing that FIFO // don't work(TM) with select(2). ret = select(nfd, &fds, NULL, NULL, NULL); if (ret == -1) err(1, "select"); if (ret == 0) { warnx("select timeout??"); continue; } if (FD_ISSET(fd, &fds)) { ssize_t amount = read(fd, buf, FIFO_MSGMAX); if (amount == -1) { err(1, "read"); } else if (amount == 0) { warnx("read EOF"); } else if (amount > 0) { fprintf(stderr, "MSG %.*s\n", (int) amount, buf); } else { errx(1, "read negative??"); } } } }