1 /* ISC license. */
2 
3 /* MT-unsafe */
4 
5 #include <skalibs/sysdeps.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <sys/wait.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <signal.h>
13 
14 #ifdef SKALIBS_HASPOSIXSPAWN
15 
16 #include <stdlib.h>
17 #include <spawn.h>
18 #include <skalibs/config.h>
19 
20 #else
21 
22 #include <skalibs/sig.h>
23 #include <skalibs/strerr2.h>
24 #include <skalibs/exec.h>
25 
26 #endif
27 
28 #include <skalibs/types.h>
29 #include <skalibs/allreadwrite.h>
30 #include <skalibs/env.h>
31 #include <skalibs/djbunix.h>
32 
33  /*
34     If n = 0 : child's stdin and stdout are the same as the parent's
35     If n >= 1 : pipes between parent and child.
36                 Parent reads on even ones, writes on odd ones.
37  */
38 
child_spawn(char const * prog,char const * const * argv,char const * const * envp,int * fds,unsigned int n)39 pid_t child_spawn (char const *prog, char const *const *argv, char const *const *envp, int *fds, unsigned int n)
40 {
41   pid_t pid ;
42 #ifdef SKALIBS_HASPOSIXSPAWN
43   posix_spawn_file_actions_t actions ;
44   posix_spawnattr_t attr ;
45   int e ;
46 #else
47   int syncpipe[2] ;
48 #endif
49   int p[n ? n : 1][2] ;
50   size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ;
51   unsigned int i = 0 ;
52   char modifs[m + 1 + n * UINT_FMT] ;
53   memcpy(modifs, SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=", sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR)) ;
54   for (; i < n ; i++)
55   {
56     if (pipe(p[i]) < 0) goto errpi ;
57     if ((ndelay_on(p[i][i & 1]) < 0) || (coe(p[i][i & 1]) < 0))
58     {
59       i++ ; goto errpi ;
60     }
61   }
62   for (i = 2 ; i < n ; i++)
63   {
64     m += uint_fmt(modifs + m, p[i][!(i & 1)]) ;
65     if (i+1 < n) modifs[m++] = ',' ;
66   }
67   modifs[m++] = 0 ;
68 
69 #ifdef SKALIBS_HASPOSIXSPAWN
70 
71   e = posix_spawnattr_init(&attr) ;
72   if (e) goto erre ;
73   {
74     sigset_t set ;
75     sigemptyset(&set) ;
76     e = posix_spawnattr_setsigmask(&attr, &set) ;
77     if (e) goto errattr ;
78     e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ;
79     if (e) goto errattr ;
80   }
81   e = posix_spawn_file_actions_init(&actions) ;
82   if (e) goto errattr ;
83   if (n >= 2 && p[1][0])
84   {
85     e = posix_spawn_file_actions_adddup2(&actions, p[1][0], 0) ;
86     if (e) goto erractions ;
87     e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ;
88     if (e) goto erractions ;
89   }
90   if (n && p[0][1] != 1)
91   {
92     e = posix_spawn_file_actions_adddup2(&actions, p[0][1], 1) ;
93     if (e) goto erractions ;
94     e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ;
95     if (e) goto erractions ;
96   }
97   {
98     int nopath = !getenv("PATH") ;
99     size_t envlen = env_len(envp) ;
100     char const *newenv[envlen + 2] ;
101     if (!env_mergen(newenv, envlen+2, envp, envlen, modifs, m, 1)) goto erractions ;
102     if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0))
103     {
104       e = errno ; goto erractions ;
105     }
106     e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)newenv) ;
107     if (nopath) unsetenv("PATH") ;
108     if (e) goto erractions ;
109   }
110 
111   posix_spawn_file_actions_destroy(&actions) ;
112   posix_spawnattr_destroy(&attr) ;
113 
114 #else
115   if (pipecoe(syncpipe) < 0) goto errp ;
116 
117   pid = fork() ;
118   if (pid < 0) goto errsp ;
119   else if (!pid)
120   {
121     size_t len = strlen(PROG) ;
122     char name[len + 9] ;
123     memcpy(name, PROG, len) ;
124     memcpy(name + len, " (child)", 9) ;
125     PROG = name ;
126     if (n >= 2)
127     {
128       if (fd_move2(0, p[1][0], 1, p[0][1]) < 0) goto syncdie ;
129     }
130     else if (n)
131     {
132       if (fd_move(1, p[0][1]) < 0) goto syncdie ;
133     }
134     sig_blocknone() ;
135     mexec_aen(prog, argv, envp, modifs, m, 1) ;
136 
137   syncdie:
138     {
139       char c = errno ;
140       fd_write(syncpipe[1], &c, 1) ;
141     }
142     _exit(127) ;
143   }
144 
145   fd_close(syncpipe[1]) ;
146   {
147     char c ;
148     syncpipe[1] = fd_read(syncpipe[0], &c, 1) ;
149     if (syncpipe[1])
150     {
151       int e = c ;
152       if (syncpipe[1] < 0) e = errno ;
153       if (wait_pid(pid, &syncpipe[1]) < 0) e = errno ;
154       errno = e ;
155       goto errsp0 ;
156     }
157   }
158   fd_close(syncpipe[0]) ;
159 #endif
160 
161   for (i = n ; i ; i--)
162   {
163     fd_close(p[i-1][i & 1]) ;
164     fds[i-1] = p[i-1][!(i & 1)] ;
165   }
166   return pid ;
167 
168 #ifdef SKALIBS_HASPOSIXSPAWN
169  erractions:
170   posix_spawn_file_actions_destroy(&actions) ;
171  errattr:
172   posix_spawnattr_destroy(&attr) ;
173  erre:
174   errno = e ;
175 #endif
176 #ifndef SKALIBS_HASPOSIXSPAWN
177  errsp:
178   fd_close(syncpipe[1]) ;
179  errsp0:
180   fd_close(syncpipe[0]) ;
181  errp:
182 #endif
183   i = n ;
184  errpi:
185   while (i--)
186   {
187     fd_close(p[i][1]) ;
188     fd_close(p[i][0]) ;
189   }
190   return 0 ;
191 }
192