7 /* wait until descriptor becomes readable
9 /* #include <iostuff.h>
11 /* int read_wait(fd, timeout)
15 /* read_wait() blocks the current process until the specified file
16 /* descriptor becomes readable, or until the deadline is exceeded.
20 /* File descriptor in the range 0..FD_SETSIZE.
22 /* If positive, deadline in seconds. A zero value effects a poll.
23 /* A negative value means wait until something happens.
25 /* Panic: interface violation. All system call errors are fatal.
27 /* A zero result means success. When the specified deadline is
28 /* exceeded, read_wait() returns -1 and sets errno to ETIMEDOUT.
32 /* The Secure Mailer license must be distributed with this software.
35 /* IBM T.J. Watson Research
37 /* Yorktown Heights, NY 10598, USA
53 #ifdef USE_SYS_SELECT_H
54 #include <sys/select.h>
57 /* Utility library. */
62 /* read_wait - block with timeout until file descriptor is readable */
64 int read_wait(int fd
, int timeout
)
76 msg_panic("descriptor %d does not fit FD_SETSIZE %d", fd
, FD_SETSIZE
);
79 * Use select() so we do not depend on alarm() and on signal() handlers.
80 * Restart the select when interrupted by some signal. Some select()
81 * implementations reduce the time to wait when interrupted, which is
82 * exactly what we want.
85 FD_SET(fd
, &read_fds
);
87 FD_SET(fd
, &except_fds
);
97 switch (select(fd
+ 1, &read_fds
, (fd_set
*) 0, &except_fds
, tp
)) {
100 msg_fatal("select: %m");
112 * System-V poll() is optimal for polling a few descriptors.
114 struct pollfd pollfd
;
116 #define WAIT_FOR_EVENT (-1)
119 pollfd
.events
= POLLIN
;
121 switch (poll(&pollfd
, 1, timeout
< 0 ?
122 WAIT_FOR_EVENT
: timeout
* 1000)) {
125 msg_fatal("poll: %m");
131 if (pollfd
.revents
& POLLNVAL
)
132 msg_fatal("poll: %m");