about summary refs log tree commit diff
path: root/src/libunixonacid/skaclient_startf_async.c
blob: 6190913130b803d0efaeee9a761df94f88095c9a (plain) (blame)
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
/* ISC license. */

#include <errno.h>
#include <signal.h>

#include <skalibs/cspawn.h>
#include <skalibs/djbunix.h>
#include <skalibs/kolbak.h>
#include <skalibs/skaclient.h>
#include "skaclient-internal.h"

int skaclient_startf_async (
  skaclient *a,
  char *bufss,
  size_t bufsn,
  char *auxbufss,
  size_t auxbufsn,
  char *bufas,
  size_t bufan,
  char *auxbufas,
  size_t auxbufan,
  kolbak_closure *q,
  size_t qlen,
  char const *prog,
  char const *const *argv,
  char const *const *envp,
  uint32_t options,
  char const *before,
  size_t beforelen,
  char const *after,
  size_t afterlen,
  skaclient_cbdata *blah)
{
  int fd ;
  pid_t pid = child_spawn1_socket(prog, argv, envp, &fd) ;
  if (!pid) return 0 ;
  if (ndelay_on(fd) < 0
   || coe(fd) < 0
   || !skaclient_init(a, fd, bufss, bufsn, auxbufss, auxbufsn, bufas, bufan, auxbufas, auxbufan, q, qlen, before, beforelen))
  {
    fd_close(fd) ;
    if (options & SKACLIENT_OPTION_WAITPID)
    {
      int e = errno ;
      int wstat ;
      waitpid_nointr(a->pid, &wstat, 0) ;
      errno = e ;
    }
    return 0 ;
  }
  a->pid = pid ;
  a->options = options ;
  if (!kolbak_enqueue(&a->kq, (unixmessage_handler_func_ref)&skaclient_start_cb, blah))
  {
    skaclient_end(a) ;
    return 0 ;
  }
  blah->a = a ;
  blah->after = after ;
  blah->afterlen = afterlen ;
  return 1 ;
}