summaryrefslogtreecommitdiff
path: root/src/libstddjb/cspawn.c
blob: 461ccd6ec73bbf511e5f07bf0f7915ae63e204bc (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* ISC license. */

#include <skalibs/sysdeps.h>

#ifdef SKALIBS_HASPOSIXSPAWN
#include <skalibs/nonposix.h>
#endif

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <skalibs/allreadwrite.h>
#include <skalibs/sig.h>
#include <skalibs/djbunix.h>
#include <skalibs/selfpipe.h>
#include <skalibs/exec.h>

#include <skalibs/cspawn.h>

static inline void cspawn_child_exec (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
{
  for (size_t i = 0 ; i < n ; i++)
  {
    switch (fa[i].type)
    {
      case CSPAWN_FA_CLOSE : fd_close(fa[i].x.fd) ; break ;
      case CSPAWN_FA_COPY :
        if (fd_copy(fa[i].x.fd2[0], fa[i].x.fd2[1]) == -1) return ;
        break ;
      case CSPAWN_FA_MOVE :
        if (fd_move(fa[i].x.fd2[0], fa[i].x.fd2[1]) == -1) return ;
        break ;
      case CSPAWN_FA_OPEN :
      {
        int fd = open(fa[i].x.openinfo.file, fa[i].x.openinfo.oflag, fa[i].x.openinfo.mode) ;
        if (fd == -1) return ;
        if (fd_move(fa[i].x.openinfo.fd, fd) == -1) return ;
        break ;
      }
      default :
        errno = EINVAL ; return ;
    }
  }

  if (flags & CSPAWN_FLAGS_SELFPIPE_FINISH) selfpipe_finish() ;
  if (flags & CSPAWN_FLAGS_SIGBLOCKNONE) sig_blocknone() ;
  if (flags & CSPAWN_FLAGS_SETSID) setsid() ;

  exec_ae(prog, argv, envp) ;
}

static inline pid_t cspawn_fork (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
{
  pid_t pid ;
  int p[2] ;
  char c ;

  if (pipecoe(p) == -1) return 0 ;
  pid = fork() ;
  if (pid == -1)
  {
    fd_close(p[1]) ;
    fd_close(p[0]) ;
    return 0 ;
  }
  if (!pid)
  {
    cspawn_child_exec(prog, argv, envp, flags, fa, n) ;
    c = errno ;
    fd_write(p[1], &c, 1) ;
    _exit(127) ;
  }

  fd_close(p[1]) ;
  p[1] = fd_read(p[0], &c, 1) ;
  if (p[1] < 0)
  {
    fd_close(p[0]) ;
    return 0 ;
  }
  fd_close(p[0]) ;
  if (p[1])
  {
    wait_pid(pid, &p[0]) ;
    errno = (unsigned char)c ;
    return 0 ;
  }
  return pid ;
}

#ifdef SKALIBS_HASPOSIXSPAWN

#include <signal.h>
#include <stdlib.h>
#include <spawn.h>

#include <skalibs/config.h>
#include <skalibs/djbunix.h>

#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN

#include <sys/wait.h>

static inline pid_t cspawn_workaround (pid_t pid, int const *p)
{
  siginfo_t si ;
  int e ;
  ssize_t r ;
  char c ;

  fd_close(p[1]) ;
  r = fd_read(p[0], &c, 1) ;
  fd_close(p[0]) ;
  if (r == -1) return 0 ;
  if (r) return (errno = EILSEQ, 0) ;  /* child wrote, wtf */

  do e = waitid(P_PID, pid, &si, WEXITED | WNOHANG | WNOWAIT) ;
  while (e == -1 && errno == EINTR) ;
  if (e == -1) return pid ;  /* we're in trouble, but don't leak a child */
  if (!si.si_pid) return pid ;  /* child is running */
  if (si.si_code != CLD_EXITED || si.si_status != 127) return pid ; /* child died after execve(), let caller handle it */
  /*
    child exited 127, so either execve() failed, which is what we want to catch,
    or it raced like a mofo, execve()d and then exited 127 on its own, in which
    case, tough luck, it never existed.
  */
  wait_pid(pid, 0) ;
  return (errno = 0, 0) ;
}

#endif

static inline pid_t cspawn_pspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
{
  pid_t pid ;
  posix_spawnattr_t attr ;
  posix_spawn_file_actions_t actions ;
  int e ;
  int nopath = !getenv("PATH") ;
#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
  int p[2] ;
  if (pipecoe(p) == -1) return 0 ;
#endif

  if (flags)
  {
    short pfff = 0 ;
    e = posix_spawnattr_init(&attr) ;
    if (e) goto err ;
    if (flags & (CSPAWN_FLAGS_SIGBLOCKNONE | CSPAWN_FLAGS_SELFPIPE_FINISH))
    {
      sigset_t set ;
      sigemptyset(&set) ;
      e = posix_spawnattr_setsigmask(&attr, &set) ;
      if (e) goto errattr ;
      pfff |= POSIX_SPAWN_SETSIGMASK ;
    }
#ifdef SKALIBS_HASPOSIXSPAWNSETSID
    if (flags & CSPAWN_FLAGS_SETSID) pfff |= POSIX_SPAWN_SETSID ;
#endif
    e = posix_spawnattr_setflags(&attr, pfff) ;
    if (e) goto errattr ;
  }

  if (n)
  {
    e = posix_spawn_file_actions_init(&actions) ;
    if (e) goto errattr ;
    for (size_t i = 0 ; i < n ; i++)
    {
      switch (fa[i].type)
      {
        case CSPAWN_FA_CLOSE :
          e = posix_spawn_file_actions_addclose(&actions, fa[i].x.fd) ;
          if (e) goto erractions ;
          break ;
        case CSPAWN_FA_COPY :
          e = posix_spawn_file_actions_adddup2(&actions, fa[i].x.fd2[1], fa[i].x.fd2[0]) ;
          if (e) goto erractions ;
          break ;
        case CSPAWN_FA_MOVE :
          e = posix_spawn_file_actions_adddup2(&actions, fa[i].x.fd2[1], fa[i].x.fd2[0]) ;
          if (e) goto erractions ;
          if (fa[i].x.fd2[0] != fa[i].x.fd2[1])
          {
            e = posix_spawn_file_actions_addclose(&actions, fa[i].x.fd2[1]) ;
            if (e) goto erractions ;
          }
          break ;
        case CSPAWN_FA_OPEN :
          e = posix_spawn_file_actions_addopen(&actions, fa[i].x.openinfo.fd, fa[i].x.openinfo.file, fa[i].x.openinfo.oflag, fa[i].x.openinfo.mode) ;
          if (e) goto erractions ;
          break ;
        default :
          e = EINVAL ;
          goto erractions ;
      }
    }
  }

  if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) == -1)) { e = errno ; goto erractions ; }
  e = posix_spawnp(&pid, prog, n ? &actions : 0, flags ? &attr : 0, (char *const *)argv, (char *const *)envp) ;
  if (nopath) unsetenv("PATH") ;
  if (e) goto errattr ;

  if (n) posix_spawn_file_actions_destroy(&actions) ;
  if (flags) posix_spawnattr_destroy(&attr) ;
#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
  return cspawn_workaround(pid, p) ;
#else
  return pid ;
#endif

 erractions:
  if (n) posix_spawn_file_actions_destroy(&actions) ;
 errattr:
  if (flags) posix_spawnattr_destroy(&attr) ;
 err:
#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
  fd_close(p[1]) ;
  fd_close(p[0]) ;
#endif
  errno = e ;
  return 0 ;
}

#ifdef SKALIBS_HASPOSIXSPAWNSETSID

pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
{
  return cspawn_pspawn(prog, argv, envp, flags, fa, n) ;
}

#else

pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
{
  return flags & CSPAWN_FLAGS_SETSID ? cspawn_fork(prog, argv, envp, flags, fa, n) : cspawn_pspawn(prog, argv, envp, flags, fa, n) ;
}

#endif

#else

pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
{
  return cspawn_fork(prog, argv, envp, flags, fa, n) ;
}

#endif