db_updater: Put parentheses back
[merlin.git] / io.c
blob4d5baae5247441056385dabc7397a16c347d845e
1 /*
2 * I/O utilities
3 */
5 #include "shared.h"
7 int io_poll(int fd, int events, int msec)
9 struct pollfd pfd;
11 pfd.fd = fd;
12 pfd.events = events;
14 return poll(&pfd, 1, msec);
17 int io_send_all(int fd, const void *buf, size_t len)
19 int poll_ret, sent, loops = 0;
20 size_t total = 0;
22 if (!buf || !len)
23 return 0;
25 poll_ret = io_poll(fd, POLLOUT, 0);
26 if (poll_ret < 0)
27 lerr("io_poll(%d, POLLOUT, 0) returned %d: %s", fd, poll_ret, strerror(errno));
29 do {
30 loops++;
31 sent = send(fd, buf + total, len - total, MSG_DONTWAIT);
32 if (poll_ret > 0 && sent + total == 0) {
33 /* disconnected peer? */
34 return 0;
36 if (sent < 0) {
37 if (errno == EAGAIN || errno == EWOULDBLOCK) {
38 sent = io_write_ok(fd, 100);
39 continue;
42 lerr("send(%d, (buf + total), %zu, MSG_DONTWAIT) returned %d (%s)",
43 fd, len - total, sent, strerror(errno));
44 continue;
47 total += sent;
48 } while (total < len && sent > 0 && loops < 15);
50 if (sent < 0)
51 return sent;
53 return total;