summaryrefslogtreecommitdiff
path: root/src/libunixonacid/ipc_timed_send.c
blob: 2129ec7ceaa8789593a7e18841cb3e2bba885e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* ISC license. */

 /* For OpenBSD, that still doesn't know what self-contained headers are */
#include <sys/types.h>

#include <sys/socket.h>
#include <errno.h>
#include <skalibs/error.h>
#include <skalibs/tai.h>
#include <skalibs/iopause.h>
#include <skalibs/unix-timed.h>

 /* For MacOS, that still doesn't know what POSIX says */
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif

int ipc_timed_send (int fd, char const *s, unsigned int len, tain_t const *deadline, tain_t *stamp)
{
  iopause_fd x = { .fd = fd, .events = IOPAUSE_WRITE, .revents = 0 } ;
  for (;;)
  {
    register int r = iopause_stamp(&x, 1, deadline, stamp) ;
    if (r < 0) return 0 ;
    else if (!r) return (errno = ETIMEDOUT, 0) ;
    else if (x.revents & IOPAUSE_WRITE)
    {
      if (send(fd, s, len, MSG_NOSIGNAL) == (int)len) break ;
      if (!error_isagain(errno)) return 0 ;
    }
    else if (x.revents & IOPAUSE_EXCEPT) return (send(fd, s, len, MSG_NOSIGNAL) == (int)len) ;
  }
  return 1 ;
}