summaryrefslogtreecommitdiff
path: root/src/libunixonacid/unixmessage_sender_flush.c
blob: e7d4299c8d95329629c70ceb14768b2fa42cc32b (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* ISC license. */

#include <skalibs/sysdeps.h>
#include <skalibs/nonposix.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <skalibs/uint16.h>
#include <skalibs/uint32.h>
#include <skalibs/disize.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/genalloc.h>
#include <skalibs/djbunix.h>
#include <skalibs/unixmessage.h>

 /* MacOS X tries hard to be POSIX-compliant... and fails. */
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif

 /*
    XXX: sendmsg/recvmsg is badly, badly specified.
    XXX: We assume ancillary data is attached to the first byte.
 */

int unixmessage_sender_flush (unixmessage_sender_t *b)
{
  disize last = { .left = b->data.len, .right = genalloc_len(int, &b->fds) } ;
  disize *offsets = genalloc_s(disize, &b->offsets) ;
  size_t n = genalloc_len(disize, &b->offsets) ;
  ssize_t r ;

  if (b->shorty) /* we had a short write, gotta send the remainder first */
  {
    disize *next = b->head+1 < n ? offsets + b->head+1 : &last ;
    size_t len = next->left - offsets[b->head].left ;
    if (b->shorty <= len)
      r = fd_write(b->fd, b->data.s + offsets[b->head].left + (len - b->shorty), b->shorty) ;
    else
    {
      size_t nfds = next->right - offsets[b->head].right ;
      char pack[6] ;
      struct iovec v[2] =
      {
        { .iov_base = pack + 6 - (b->shorty - len), .iov_len = b->shorty - len },
        { .iov_base = b->data.s + offsets[b->head].left, .iov_len = len }
      } ;
      uint32_pack_big(pack, (uint32_t)len) ;
      uint16_pack_big(pack + 4, (uint16_t)nfds) ;
      r = fd_writev(b->fd, v, 2) ;
    }
    if (r <= 0) return 0 ;
    b->shorty -= r ;
    if (b->shorty) return (errno = EWOULDBLOCK, 0) ;
  }

  for (; b->head < n ; b->head++)
  {
    disize *next = b->head+1 < n ? offsets + b->head+1 : &last ;
    size_t len = next->left - offsets[b->head].left ;
    size_t nfds = next->right - offsets[b->head].right ;
    char pack[6] ;
    struct iovec v[2] =
    {
      { .iov_base = pack, .iov_len = 6 },
      { .iov_base = b->data.s + offsets[b->head].left, .iov_len = len }
    } ;
    char ancilbuf[CMSG_SPACE(nfds * sizeof(int))] ;
    struct msghdr hdr =
    {
      .msg_name = 0,
      .msg_namelen = 0,
      .msg_iov = v,
      .msg_iovlen = 2,
      .msg_control = nfds ? ancilbuf : 0,
      .msg_controllen = nfds ? sizeof(ancilbuf) : 0
    } ;
    uint32_pack_big(pack, (uint32_t)len) ;
    uint16_pack_big(pack + 4, (uint16_t)nfds) ;
    if (nfds)
    {
      struct cmsghdr *cp = CMSG_FIRSTHDR(&hdr) ;
      size_t i = 0 ;
      memset(ancilbuf, 0, sizeof(ancilbuf)) ;
      cp->cmsg_level = SOL_SOCKET ;
      cp->cmsg_type = SCM_RIGHTS ;
      cp->cmsg_len = CMSG_LEN(nfds * sizeof(int)) ;
      for (; i < nfds ; i++)
      {
        int fd = genalloc_s(int, &b->fds)[offsets[b->head].right + i] ;
        ((int *)CMSG_DATA(cp))[i] = fd < 0 ? -(fd+1) : fd ;
      }
    }
    do r = sendmsg(b->fd, &hdr, MSG_NOSIGNAL) ;
    while (r < 0 && errno == EINTR) ;
    if (r <= 0) return 0 ;
#ifndef SKALIBS_HASANCILAUTOCLOSE
    if (nfds)
    {
      size_t i = 0 ;
      for (; i < nfds ; i++)
      {
        int fd = genalloc_s(int, &b->fds)[offsets[b->head].right + i] ;
        if (fd < 0) (*b->closecb)(-(fd+1), b->closecbdata) ;
      }
    }
#endif
    if ((size_t)r < 6 + len)
    {
      b->shorty = 6 + len - r ;
      return (errno = EWOULDBLOCK, 0) ;
    }
  }
  b->data.len = 0 ;
  genalloc_setlen(int, &b->fds, 0) ;
  genalloc_setlen(disize, &b->offsets, 0) ;
  b->head = 0 ;
  return 1 ;
}