summaryrefslogtreecommitdiff
path: root/src/libunixonacid/textclient_start.c
blob: 8cb7bcd2248b747d9f7e3e78455aa3f5fbb6fc03 (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
/* ISC license. */

#include <skalibs/sysdeps.h>
#include <skalibs/nonposix.h>
#include <sys/uio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <skalibs/error.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/webipc.h>
#include <skalibs/djbunix.h>
#include <skalibs/unix-timed.h>
#include <skalibs/textmessage.h>
#include <skalibs/textclient.h>

union aligner_u
{
  struct cmsghdr cmsghdr ;
  int i ;
} ;

static int getfd (void *p)
{
  return ((int *)p)[0] ;
}

static ssize_t get (void *p)
{
  static int const awesomeflags =
#ifdef SKALIBS_HASMSGDONTWAIT
    MSG_DONTWAIT
#else
    0
#endif
    |
#ifdef SKALIBS_HASCMSGCLOEXEC
    MSG_CMSG_CLOEXEC
#else
    0
#endif
    ;
  struct cmsghdr *c ;
  int *fd = p ;
  ssize_t r ;
  union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ;
  char ch ;
  struct iovec v = { .iov_base = &ch, .iov_len = 1 } ;
  struct msghdr msghdr =
  {
    .msg_name = 0,
    .msg_namelen = 0,
    .msg_iov = &v,
    .msg_iovlen = 1,
    .msg_flags = 0,
    .msg_control = ancilbuf,
    .msg_controllen = CMSG_SPACE(sizeof(int))
  } ;
  do r = recvmsg(fd[0], &msghdr, awesomeflags) ;
  while (r < 0 && errno == EINTR) ;
  if (r <= 0) return sanitize_read(r) ;
  c = CMSG_FIRSTHDR(&msghdr) ;
  if (ch != '|'
   || !c
   || c->cmsg_level != SOL_SOCKET
   || c->cmsg_type != SCM_RIGHTS
   || (size_t)(c->cmsg_len - (CMSG_DATA(c) - (unsigned char *)c)) != sizeof(int)) return (errno = EPROTO, -1) ;
#ifndef SKALIBS_HASCMSGCLOEXEC
  if (coe(*(int *)CMSG_DATA(c)) < 0)
  {
    fd_close(*(int *)CMSG_DATA(c)) ;
    return -1 ;
  }
#endif
  fd[1] = *(int *)CMSG_DATA(c) ;
  return 1 ;
}


int textclient_start (textclient_t *a, char const *path, uint32_t options, char const *before, size_t beforelen, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp)
{
  struct iovec v ;
  int fd[2] ;
  ssize_t r ;
  fd[0] = ipc_stream_nbcoe() ;
  if (fd[0] < 0) return 0 ;
  if (!ipc_timed_connect(fd[0], path, deadline, stamp)) goto err ;
  textmessage_sender_init(&a->syncout, fd[0]) ;
  if (!textmessage_timed_send(&a->syncout, before, beforelen, deadline, stamp)) goto ferr ;
  textmessage_receiver_init(&a->syncin, fd[0], a->syncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ;
  r = timed_get(fd, &getfd, &get, deadline, stamp) ;
  if (!r) errno = EPIPE ;
  if (r <= 0) goto aerr ;
  if (sanitize_read(textmessage_timed_receive(&a->syncin, &v, deadline, stamp)) <= 0) goto perr ;
  if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) { errno = EPROTO ; goto perr ; }
  textmessage_receiver_init(&a->asyncin, fd[1], a->asyncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ;
  if (sanitize_read(textmessage_timed_receive(&a->asyncin, &v, deadline, stamp)) <= 0) goto serr ;
  if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto berr ;
  a->pid = 0 ;
  a->options = options & ~TEXTCLIENT_OPTION_WAITPID ;
  return 1 ;

 berr:
  errno = EPROTO ;
 serr:
  textmessage_receiver_free(&a->asyncin) ;
 perr:
  fd_close(fd[1]) ;
 aerr:
  textmessage_receiver_free(&a->syncin) ;
 ferr:
  textmessage_sender_free(&a->syncout) ;
 err:
  fd_close(fd[0]) ;
  return 0 ;
}