ring_tx: handle EINTR from sendto
[netsniff-ng-new.git] / ioexact.c
blob7ba6594ef93ab3a75958390d13f85864190a6a15
1 #include <errno.h>
2 #include <unistd.h>
3 #include <signal.h>
4 #include <stdbool.h>
6 #include "ioexact.h"
8 extern volatile sig_atomic_t sigint;
10 ssize_t read_exact(int fd, void *buf, size_t len, bool mayexit)
12 ssize_t num = 0, written;
14 while (len > 0 && !sigint) {
15 if ((written = read(fd, buf, len)) < 0) {
16 if (errno == EAGAIN && num > 0)
17 continue;
18 if (mayexit)
19 return -1;
20 else
21 continue;
23 if (!written)
24 return 0;
26 len -= written;
27 buf += written;
28 num += written;
31 return num;
34 ssize_t write_exact(int fd, void *buf, size_t len, bool mayexit)
36 ssize_t num = 0, written;
38 while (len > 0 && !sigint) {
39 if ((written = write(fd, buf, len)) < 0) {
40 if (errno == EAGAIN && num > 0)
41 continue;
42 if (mayexit)
43 return -1;
44 else
45 continue;
47 if (!written)
48 return 0;
50 len -= written;
51 buf += written;
52 num += written;
55 return num;