5
/* wait until descriptor becomes readable
7
/* #include <iostuff.h>
9
/* int read_wait(fd, timeout)
13
/* read_wait() blocks the current process until the specified file
14
/* descriptor becomes readable, or until the deadline is exceeded.
18
/* File descriptor in the range 0..FD_SETSIZE (on systems that
19
/* need to use select(2)).
21
/* If positive, deadline in seconds. A zero value effects a poll.
22
/* A negative value means wait until something happens.
24
/* Panic: interface violation. All system call errors are fatal.
26
/* A zero result means success. When the specified deadline is
27
/* exceeded, read_wait() returns -1 and sets errno to ETIMEDOUT.
31
/* The Secure Mailer license must be distributed with this software.
34
/* IBM T.J. Watson Research
36
/* Yorktown Heights, NY 10598, USA
52
#ifdef USE_SYS_SELECT_H
53
#include <sys/select.h>
56
/* Utility library. */
61
/* read_wait - block with timeout until file descriptor is readable */
63
int read_wait(int fd, int timeout)
75
msg_panic("descriptor %d does not fit FD_SETSIZE %d", fd, FD_SETSIZE);
78
* Use select() so we do not depend on alarm() and on signal() handlers.
79
* Restart the select when interrupted by some signal. Some select()
80
* implementations reduce the time to wait when interrupted, which is
81
* exactly what we want.
84
FD_SET(fd, &read_fds);
86
FD_SET(fd, &except_fds);
96
switch (select(fd + 1, &read_fds, (fd_set *) 0, &except_fds, tp)) {
99
msg_fatal("select: %m");
111
* System-V poll() is optimal for polling a few descriptors.
113
struct pollfd pollfd;
115
#define WAIT_FOR_EVENT (-1)
118
pollfd.events = POLLIN;
120
switch (poll(&pollfd, 1, timeout < 0 ?
121
WAIT_FOR_EVENT : timeout * 1000)) {
124
msg_fatal("poll: %m");
130
if (pollfd.revents & POLLNVAL)
131
msg_fatal("poll: %m");