Postfix3.3.1
fifo_rdwr_bug.c
[詳解]
1 /*++
2 /* NAME
3 /* fifo_rdwr_bug 1
4 /* SUMMARY
5 /* fifo server test program
6 /* SYNOPSIS
7 /* fifo_rdwr_bug
8 /* DESCRIPTION
9 /* fifo_rdwr_bug creates a FIFO and opens it read-write mode.
10 /* On BSD/OS 3.1 select() will report that the FIFO is readable
11 /* even before any data is written to it. Doing an actual read
12 /* causes the read to block; a non-blocking read fails.
13 /* DIAGNOSTICS
14 /* Problems are reported to the standard error stream.
15 /* LICENSE
16 /* .ad
17 /* .fi
18 /* The Secure Mailer license must be distributed with this software.
19 /* AUTHOR(S)
20 /* Wietse Venema
21 /* IBM T.J. Watson Research
22 /* P.O. Box 704
23 /* Yorktown Heights, NY 10598, USA
24 /*--*/
25 
26 #include <sys_defs.h>
27 #include <sys/time.h>
28 #include <sys/param.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #define FIFO_PATH "test-fifo"
38 #define perrorexit(s) { perror(s); exit(1); }
39 
40 static void cleanup(void)
41 {
42  printf("Removing fifo %s...\n", FIFO_PATH);
43  if (unlink(FIFO_PATH))
44  perrorexit("unlink");
45  printf("Done.\n");
46 }
47 
48 int main(int unused_argc, char **unused_argv)
49 {
50  struct timeval tv;
51  fd_set read_fds;
52  fd_set except_fds;
53  int fd;
54 
55  (void) unlink(FIFO_PATH);
56 
57  printf("Creating fifo %s...\n", FIFO_PATH);
58  if (mkfifo(FIFO_PATH, 0600) < 0)
59  perrorexit("mkfifo");
60 
61  printf("Opening fifo %s, read-write mode...\n", FIFO_PATH);
62  if ((fd = open(FIFO_PATH, O_RDWR, 0)) < 0) {
63  perror("open");
64  cleanup();
65  exit(1);
66  }
67  printf("Selecting the fifo for readability...\n");
68  FD_ZERO(&read_fds);
69  FD_SET(fd, &read_fds);
70  FD_ZERO(&except_fds);
71  FD_SET(fd, &except_fds);
72  tv.tv_sec = 1;
73  tv.tv_usec = 0;
74 
75  switch (select(fd + 1, &read_fds, (fd_set *) 0, &except_fds, &tv)) {
76  case -1:
77  perrorexit("select");
78  default:
79  if (FD_ISSET(fd, &read_fds)) {
80  printf("Opening a fifo read-write makes it readable!!\n");
81  break;
82  }
83  case 0:
84  printf("The fifo is not readable, as it should be.\n");
85  break;
86  }
87  cleanup();
88  exit(0);
89 }
#define perrorexit(s)
Definition: fifo_rdwr_bug.c:38
#define FIFO_PATH
Definition: fifo_rdwr_bug.c:37
int main(int unused_argc, char **unused_argv)
Definition: fifo_rdwr_bug.c:48