1 /* ISC license. */
2 
3 #include <skalibs/nonposix.h>
4 
5 #include <sys/socket.h>
6 #include <errno.h>
7 
8 #include <skalibs/error.h>
9 #include <skalibs/iopause.h>
10 #include <skalibs/siovec.h>
11 #include <skalibs/unix-timed.h>
12 #include <skalibs/posixishard.h>
13 
ipc_timed_sendv(int fd,struct iovec const * v,unsigned int n,tain_t const * deadline,tain_t * stamp)14 int ipc_timed_sendv (int fd, struct iovec const *v, unsigned int n, tain_t const *deadline, tain_t *stamp)
15 {
16   struct msghdr hdr =
17   {
18     .msg_name = 0,
19     .msg_namelen = 0,
20     .msg_iov = (struct iovec *)v,
21     .msg_iovlen = n,
22     .msg_control = 0,
23     .msg_controllen = 0,
24     .msg_flags = 0
25   } ;
26   size_t len = siovec_len(v, n) ;
27   iopause_fd x = { .fd = fd, .events = IOPAUSE_WRITE, .revents = 0 } ;
28   for (;;)
29   {
30     int r = iopause_stamp(&x, 1, deadline, stamp) ;
31     if (r < 0) return 0 ;
32     else if (!r) return (errno = ETIMEDOUT, 0) ;
33     else if (x.revents & IOPAUSE_WRITE)
34     {
35       if (sendmsg(fd, &hdr, MSG_NOSIGNAL) == (ssize_t)len) break ;
36       if (!error_isagain(errno)) return 0 ;
37     }
38     else if (x.revents & IOPAUSE_EXCEPT) return (sendmsg(fd, &hdr, MSG_NOSIGNAL) == (ssize_t)len) ;
39   }
40   return 1 ;
41 }
42