7 /* fifo server test program
11 /* fifo_rdonly_bug creates a FIFO and opens it read only. It
12 /* then opens the FIFO for writing, writes one byte, and closes
13 /* the writing end. On Linux Redhat 4.2 and 5.0, and HP-UX 9.05
14 /* and 10.20, select() will report that the FIFO remains readable
15 /* even after multiple read operations.
17 /* Problems are reported to the standard error stream.
21 /* The Secure Mailer license must be distributed with this software.
24 /* IBM T.J. Watson Research
26 /* Yorktown Heights, NY 10598, USA
38 #define FIFO_PATH "test-fifo"
39 #define TRIGGER_DELAY 5
41 #define perrorexit(s) { perror(s); exit(1); }
43 static void cleanup(void)
45 printf("Removing fifo %s...\n", FIFO_PATH
);
46 if (unlink(FIFO_PATH
))
51 static void perrorcleanup(char *str
)
58 static void readable_event(int fd
)
63 if (read(fd
, &ch
, 1) < 0) {
68 printf("FIFO remains readable after multiple reads.\n");
74 int main(int unused_argc
, char **unused_argv
)
82 (void) unlink(FIFO_PATH
);
84 printf("Create fifo %s...\n", FIFO_PATH
);
85 if (mkfifo(FIFO_PATH
, 0600) < 0)
88 printf("Open fifo %s, read-only mode...\n", FIFO_PATH
);
89 if ((fd
= open(FIFO_PATH
, O_RDONLY
| O_NONBLOCK
, 0)) < 0)
90 perrorcleanup("open");
92 printf("Write one byte to the fifo, then close it...\n");
93 if ((fd2
= open(FIFO_PATH
, O_WRONLY
, 0)) < 0)
94 perrorcleanup("open fifo O_WRONLY");
95 if (write(fd2
, "", 1) < 1)
96 perrorcleanup("write one byte to fifo");
98 perrorcleanup("close fifo");
100 printf("Selecting the fifo for readability...\n");
104 FD_SET(fd
, &read_fds
);
105 FD_ZERO(&except_fds
);
106 FD_SET(fd
, &except_fds
);
110 switch (select(fd
+ 1, &read_fds
, (fd_set
*) 0, &except_fds
, &tv
)) {
112 perrorexit("select");
114 if (FD_ISSET(fd
, &except_fds
)) {
115 printf("Exceptional fifo condition! You are not normal!\n");
117 } else if (FD_ISSET(fd
, &read_fds
)) {
118 printf("Readable fifo condition\n");
123 printf("The fifo is not readable. You're normal.\n");