7 /* wait until descriptor becomes writable
9 /* #include <iostuff.h>
11 /* int write_wait(fd, timeout)
15 /* write_wait() blocks the current process until the specified file
16 /* descriptor becomes writable, 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, write_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 /* write_wait - block with timeout until file descriptor is writable */
64 int write_wait(int fd
, int timeout
)
76 msg_panic("descriptor %d does not fit FD_SETSIZE %d", fd
, FD_SETSIZE
);
79 * Guard the write() with select() so we do not depend on alarm() and on
80 * signal() handlers. Restart the select when interrupted by some signal.
81 * Some select() implementations may reduce the time to wait when
82 * interrupted, which is exactly what we want.
85 FD_SET(fd
, &write_fds
);
87 FD_SET(fd
, &except_fds
);
97 switch (select(fd
+ 1, (fd_set
*) 0, &write_fds
, &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
= POLLOUT
;
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");