Spawner.xs

filter: Statementstmt 78.9% 221 / 280 Branchbran 48.1% 307 / 638 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.5% 528 / 918 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« t/try.pl ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include <signal.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <string.h>
9 /* av_count() is perl 5.33.1 and later (X36, RT #182809: perl 5.30.3); the same as perl's own there. */
10 #ifndef av_count
11 # define av_count(av) ((Size_t)(AvFILL(av) + 1))
12 #endif
13 #ifdef _WIN32
14 /* Windows (EXPERIMENTAL, post-0.06, not yet built): posix_spawn on CreateProcessW, in win_spawn.h; these names make the
15 XSUB below one source for both. What Windows does natively: doc/pspawn-spawner.md, section 8. */
16 # include "win_spawn.h"
17 # include "win_loop.h"
18 # define posix_spawn_file_actions_t win_file_actions_t
19 # define posix_spawnattr_t win_spawnattr_t
20 # define posix_spawn win_posix_spawn
21 # define posix_spawnp win_posix_spawnp
22 # define posix_spawn_file_actions_init win_fa_init
23 # define posix_spawn_file_actions_destroy win_fa_destroy
24 # define posix_spawn_file_actions_adddup2 win_fa_adddup2
25 # define posix_spawn_file_actions_addclose win_fa_addclose
26 # define posix_spawn_file_actions_addopen win_fa_addopen
27 # define posix_spawn_file_actions_addchdir_np win_fa_addchdir
28 # define posix_spawn_file_actions_addfchdir_np win_fa_addfchdir
29 # define posix_spawn_file_actions_addclosefrom_np win_fa_addclosefrom
30 # define posix_spawnattr_init win_attr_init
31 # define posix_spawnattr_destroy win_attr_destroy
32 # define posix_spawnattr_setflags win_attr_setflags
33 # define posix_spawnattr_setsigmask win_attr_setsigmask
34 # define posix_spawnattr_setsigdefault win_attr_setsigdefault
35 # define posix_spawnattr_setpgroup win_attr_setpgroup
36 # define POSIX_SPAWN_RESETIDS WIN_SPAWN_RESETIDS
37 # define POSIX_SPAWN_SETPGROUP WIN_SPAWN_SETPGROUP
38 # define POSIX_SPAWN_SETSIGDEF WIN_SPAWN_SETSIGDEF
39 # define POSIX_SPAWN_SETSIGMASK WIN_SPAWN_SETSIGMASK
40 # define HAVE_ADDCHDIR_NP 1
41 # define HAVE_ADDFCHDIR_NP 1
42 # define HAVE_ADDCLOSEFROM_NP 1
43 # define HAVE_POSIX_SPAWN 1
44 typedef win_sigset_t ps_sigset_t;
45 # define ps_sigemptyset win_sigemptyset
46 # define ps_sigaddset win_sigaddset
47 #else
48 # ifdef HAVE_POSIX_SPAWN /* Makefile.PL: without it, the primitive is __spawn_nowait (fork and exec; decision M2) */
49 # include <spawn.h>
50 # endif
51 # include <sys/types.h>
52 # include <sys/time.h>
53 # include <sys/wait.h>
54 # include <unistd.h>
55 # include <sched.h>
56 # include <sys/resource.h>
57 # include <sys/syscall.h>
58 typedef sigset_t ps_sigset_t;
59 # define ps_sigemptyset sigemptyset
60 # define ps_sigaddset sigaddset
61 #endif
62 /* The posix_spawn extensions (glibc's, and some other libcs') are functions, so Makefile.PL finds them by
63 compiling a call to each: -DHAVE_ADDCHDIR_NP and so on. An option this build lacks is dropped or fails
64 with ENOTSUP, as the caller's UNSUPPORTED hash or %Proc::Spawner::UNSUPPORTED says. */
65 #ifdef HAVE_PIDFD_SPAWN
66 # include <sys/pidfd.h>
67 #endif
68 #ifndef _WIN32
69 extern char **environ;
70 #endif
71  
72 /* A string for the spawn call. On Windows: UTF-8 for win_spawn.h, from a byte string in the ANSI code page (as Perl's
73 own CreateProcessA takes it) unless Perl has it as UTF-8 already; the copy is mortal. */
74 #ifdef _WIN32
75 static const char *
76 spawn_str(pTHX_ SV *sv)
77 {
78 const char *p = SvPV_nolen(sv); char *u; SV *m;
79 if (SvUTF8(sv) || !(u = win_acp_to_utf8(p))) return p;
80 m = sv_2mortal(newSVpv(u, 0)); win_free(u);
81 return SvPVX(m);
82 }
83 # define SPAWN_STR(sv) spawn_str(aTHX_ (sv))
84 static char **
85 win_childenv(pTHX) /* Perl's environment for a child (PerlEnv_get_childenv, as Perl's own spawn: with pseudo-forks it
86 can differ from the process's), as a mortal UTF-8 vector; NULL: the process's own */
87 {
88 char *b = (char *)PerlEnv_get_childenv(), *p, **v; SSize_t n = 0, i = 0;
89 if (!b) return NULL;
90 for (p = b; *p; p += strlen(p) + 1) n++;
91 Newx(v, n + 1, char *); SAVEFREEPV(v);
92 for (p = b; *p; p += strlen(p) + 1) {
93 char *u = win_acp_to_utf8(p); SV *m = sv_2mortal(newSVpv(u ? u : p, 0)); win_free(u); v[i++] = SvPVX(m);
94 }
95 v[n] = NULL; PerlEnv_free_childenv(b);
96 return v;
97 }
98 #else
99 # define SPAWN_STR(sv) SvPV_nolen(sv)
100 #endif
101  
102 /* A NULL-terminated char* vector from an array ref; freed with the scope. */
103 static char **
104 619 strv(pTHX_ SV *ref, const char *what)
105 {
106 AV *av; SSize_t n, i; char **v;
107 619 ▶ if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) 619 0 619
if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) 0 619 619
108 0 croak("Proc::Spawner::spawn: %s must be an array reference", what);
109 619 av = (AV *)SvRV(ref);
110 619 n = av_count(av);
111 619 ▶ Newx(v, n + 1, char *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(v, n + 1, char *); 0 619 619
112 619 SAVEFREEPV(v);
113 11153 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10534 619 11153
114 10534 SV **e = av_fetch(av, i, 0);
115 21068 ▶ v[i] = e ? (char *)SPAWN_STR(*e) : (char *)"";
Branch: 1 branch
BranchTrueFalseTotal
v[i] = e ? (char *)SPAWN_STR(*e) : (char *)""; 10534 0 10534
116 }
117 619 v[n] = NULL;
118 619 return v;
119 }
120  
121 static int
122 660 sigset_from(pTHX_ SV *ref, ps_sigset_t *set)
123 {
124 AV *av; SSize_t i;
125 660 ▶ if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) croak("Proc::Spawner::spawn: signal list must be an array reference");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) croak("Proc::Spawner::spawn: signal list must be an array reference"); 660 0 660
if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) croak("Proc::Spawner::spawn: signal list must be an array reference"); 0 660 660
126 660 av = (AV *)SvRV(ref);
127 660 ps_sigemptyset(set);
128 1682 ▶ for (i = 0; i < av_count(av); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < av_count(av); i++) { 1022 660 1682
129 1022 SV **e = av_fetch(av, i, 0);
130 1022 ▶ if (e && ps_sigaddset(set, (int)SvIV(*e)) != 0) return errno;
Branch: 2 branches
BranchTrueFalseTotal
if (e && ps_sigaddset(set, (int)SvIV(*e)) != 0) return errno; 1022 0 1022
if (e && ps_sigaddset(set, (int)SvIV(*e)) != 0) return errno; 0 1022 1022
131 }
132 660 return 0;
133 }
134  
135 /* PROTOTYPE for decision D1, option A (page "Child Exit Wake-up"): a C handler for SIGCHLD
136 that only writes one byte to a file descriptor; no Perl code runs inside the signal.
137 The fd should be non-blocking, so that a full pipe cannot hang the handler. */
138 #ifndef _WIN32 /* no SIGCHLD on Windows: Proc::Spawner falls back (chld_to_fd is not defined there) */
139 static volatile sig_atomic_t chld_fd = -1;
140 static struct sigaction chld_old;
141 static int chld_installed = 0;
142 static void
143 276 chld_handler(int sig)
144 {
145 276 int saved = errno;
146 (void)sig;
147 276 ▶ if (chld_fd >= 0) { ssize_t r = write(chld_fd, "x", 1); (void)r; }
Branch: 1 branch
BranchTrueFalseTotal
if (chld_fd >= 0) { ssize_t r = write(chld_fd, "x", 1); (void)r; } 276 0 276
148 276 errno = saved;
149 276 }
150 #endif
151  
152 static int
153 2 missing(pTHX_ SV *uns, const char *name) /* 0 if the option is to be dropped, else ENOTSUP */
154 {
155 2 SV **v = NULL; HV *g;
156 2 ▶ if (SvOK(uns)) v = hv_fetch((HV *)SvRV(uns), name, strlen(name), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(uns)) v = hv_fetch((HV *)SvRV(uns), name, strlen(name), 0); 0 2 2
157 2 ▶ if (!v && (g = get_hv("Proc::Spawner::UNSUPPORTED", 0)) && !(v = hv_fetch(g, name, strlen(name), 0)))
Branch: 3 branches
BranchTrueFalseTotal
if (!v && (g = get_hv("Proc::Spawner::UNSUPPORTED", 0)) && !(v = hv_fetch(g, name, strlen(name), 0))) 2 0 2
if (!v && (g = get_hv("Proc::Spawner::UNSUPPORTED", 0)) && !(v = hv_fetch(g, name, strlen(name), 0))) 2 0 2
if (!v && (g = get_hv("Proc::Spawner::UNSUPPORTED", 0)) && !(v = hv_fetch(g, name, strlen(name), 0))) 2 0 2
158 2 v = hv_fetchs(g, "*", 0);
159 4 ▶ return v && SvOK(*v) && strEQ(SvPV_nolen(*v), "drop") ? 0 : ENOTSUP;
Branch: 3 branches
BranchTrueFalseTotal
return v && SvOK(*v) && strEQ(SvPV_nolen(*v), "drop") ? 0 : ENOTSUP; 2 0 2
return v && SvOK(*v) && strEQ(SvPV_nolen(*v), "drop") ? 0 : ENOTSUP; 2 0 2
return v && SvOK(*v) && strEQ(SvPV_nolen(*v), "drop") ? 0 : ENOTSUP; 0 2 2
160 }
161  
162 /* Settings POSIX's posix_spawn has no option for: rlimit, sigignore, nice (the user, 2026-11-06, decision M1). They are
163 set in this process right around the call, in C (nothing is allocated and no Perl code runs in between), and put
164 back right after; the child inherits them. One that could not be put back is unsupported (dropped or failed, as
165 UNSUPPORTED says): a lower hard limit, or a higher nice value, without the privilege to undo it; a CPU limit this
166 process has used up already (it would get SIGXCPU); ignoring SIGCHLD (this process's other children ending
167 meanwhile would be reaped by the kernel, their statuses lost). Every signal is blocked meanwhile (Linux keeps a
168 blocked signal pending even while it is ignored); the child gets the mask from before. On Windows: unsupported. */
169 #define AR_MAX 32
170 #ifndef _WIN32
171 # ifdef __linux__
172 # include <linux/capability.h>
173 # endif
174 # ifndef CAP_SYS_NICE
175 # define CAP_SYS_NICE 23
176 # endif
177 # ifndef CAP_SYS_RESOURCE
178 # define CAP_SYS_RESOURCE 24
179 # endif
180 typedef struct {
181 int on; /* anything to do */
182 int nrl, rl_res[AR_MAX], rl_done; struct rlimit rl_new[AR_MAX], rl_old[AR_MAX];
183 int nsig, sig[AR_MAX], sig_done; struct sigaction sig_old[AR_MAX];
184 int nice_on, nice_new, nice_old, nice_done;
185 sigset_t mask_old; int blocked;
186 } around_t;
187  
188 static int
189 1 has_cap(int cap) /* may this process do what needs the capability (CAP_SYS_NICE, CAP_SYS_RESOURCE)? */
190 {
191 # ifdef __linux__
192 struct __user_cap_header_struct h; struct __user_cap_data_struct d[2];
193 1 memset(&h, 0, sizeof h); memset(d, 0, sizeof d);
194 1 h.version = _LINUX_CAPABILITY_VERSION_3; h.pid = 0;
195 1 ▶ if (syscall(SYS_capget, &h, d) == 0) return (d[cap / 32].effective >> (cap % 32)) & 1;
Branch: 1 branch
BranchTrueFalseTotal
if (syscall(SYS_capget, &h, d) == 0) return (d[cap / 32].effective >> (cap % 32)) & 1; 1 0 1
196 # endif
197 0 return geteuid() == 0;
198 }
199  
200 static int
201 1 cpu_used_up(int res, rlim_t lim) /* a CPU limit this process has used up already (it would get SIGXCPU meanwhile) */
202 {
203 # ifdef RLIMIT_CPU
204 struct rusage u;
205 1 ▶ if (res != RLIMIT_CPU || lim == RLIM_INFINITY || getrusage(RUSAGE_SELF, &u) != 0) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (res != RLIMIT_CPU || lim == RLIM_INFINITY || getrusage(RUSAGE_SELF, &u) != 0) return 0; 0 1 1
if (res != RLIMIT_CPU || lim == RLIM_INFINITY || getrusage(RUSAGE_SELF, &u) != 0) return 0; 0 0 0
if (res != RLIMIT_CPU || lim == RLIM_INFINITY || getrusage(RUSAGE_SELF, &u) != 0) return 0; 0 0 0
206 0 return lim <= (rlim_t)(u.ru_utime.tv_sec + u.ru_stime.tv_sec) + 1;
207 # else
208 return 0;
209 # endif
210 }
211  
212 static rlim_t
213 2 rl_value(pTHX_ SV **sv, rlim_t keep) /* undef: keep ours; 'unlimited' or 'infinity': RLIM_INFINITY */
214 {
215 2 ▶ if (!sv || !SvOK(*sv)) return keep;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(*sv)) return keep; 2 0 2
if (!sv || !SvOK(*sv)) return keep; 1 1 2
216 1 ▶ if (looks_like_number(*sv)) return (rlim_t)SvUV(*sv);
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_number(*sv)) return (rlim_t)SvUV(*sv); 1 0 1
217 0 ▶ if (strEQ(SvPV_nolen(*sv), "unlimited") || strEQ(SvPV_nolen(*sv), "infinity")) return RLIM_INFINITY;
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(SvPV_nolen(*sv), "unlimited") || strEQ(SvPV_nolen(*sv), "infinity")) return RLIM_INFINITY; 0 0 0
if (strEQ(SvPV_nolen(*sv), "unlimited") || strEQ(SvPV_nolen(*sv), "infinity")) return RLIM_INFINITY; 0 0 0
218 0 croak("Proc::Spawner::spawn: rlimit value '%s' is not a number", SvPV_nolen(*sv));
219 }
220  
221 static int
222 345 around_parse(pTHX_ HV *at, SV *uns, around_t *ar) /* 0, or an errno; croaks on malformed input (before anything is allocated) */
223 {
224 SV **v; SSize_t i; int rc;
225 345 memset(ar, 0, sizeof *ar);
226 345 ▶ if (!at) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!at) return 0; 0 345 345
227 345 ▶ if ((v = hv_fetchs(at, "rlimit", 0)) && SvOK(*v)) { /* [[RESOURCE, SOFT, HARD], ...] */
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "rlimit", 0)) && SvOK(*v)) { /* [[RESOURCE, SOFT, HARD], ...] */ 2 343 345
if ((v = hv_fetchs(at, "rlimit", 0)) && SvOK(*v)) { /* [[RESOURCE, SOFT, HARD], ...] */ 2 0 2
228 AV *l;
229 2 ▶ if (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV) croak("Proc::Spawner::spawn: rlimit must be an array reference");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV) croak("Proc::Spawner::spawn: rlimit must be an array reference"); 2 0 2
if (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV) croak("Proc::Spawner::spawn: rlimit must be an array reference"); 0 2 2
230 2 l = (AV *)SvRV(*v);
231 3 ▶ for (i = 0; i < av_count(l); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < av_count(l); i++) { 1 2 3
232 1 SV **e = av_fetch(l, i, 0); AV *r; struct rlimit o, n; int res;
233 1 ▶ if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV || av_count((AV *)SvRV(*e)) < 2 || av_count((AV *)SvRV(*e)) > 3)
Branch: 5 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV || av_count((AV *)SvRV(*e)) < 2 || av_count((AV *)SvRV(*e)) > 3) 1 0 1
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV || av_count((AV *)SvRV(*e)) < 2 || av_count((AV *)SvRV(*e)) > 3) 1 0 1
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV || av_count((AV *)SvRV(*e)) < 2 || av_count((AV *)SvRV(*e)) > 3) 1 0 1
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV || av_count((AV *)SvRV(*e)) < 2 || av_count((AV *)SvRV(*e)) > 3) 1 0 1
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV || av_count((AV *)SvRV(*e)) < 2 || av_count((AV *)SvRV(*e)) > 3) 0 1 1
234 0 croak("Proc::Spawner::spawn: rlimit entry %ld must be [RESOURCE, SOFT, HARD]", (long)i);
235 1 r = (AV *)SvRV(*e); res = (int)SvIV(*av_fetch(r, 0, 0));
236 1 ▶ if (getrlimit(res, &o) != 0) return errno;
Branch: 1 branch
BranchTrueFalseTotal
if (getrlimit(res, &o) != 0) return errno; 0 1 1
237 1 n.rlim_cur = rl_value(aTHX_ av_fetch(r, 1, 0), o.rlim_cur);
238 1 n.rlim_max = rl_value(aTHX_ av_fetch(r, 2, 0), o.rlim_max);
239 1 ▶ if (n.rlim_cur == o.rlim_cur && n.rlim_max == o.rlim_max) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (n.rlim_cur == o.rlim_cur && n.rlim_max == o.rlim_max) continue; 0 1 1
if (n.rlim_cur == o.rlim_cur && n.rlim_max == o.rlim_max) continue; 0 0 0
240 1 ▶ if (n.rlim_cur > n.rlim_max) return EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (n.rlim_cur > n.rlim_max) return EINVAL; 0 1 1
241 1 ▶ if ((n.rlim_max < o.rlim_max && !has_cap(CAP_SYS_RESOURCE)) /* a lower hard limit: no way back */
Branch: 2 branches
BranchTrueFalseTotal
if ((n.rlim_max < o.rlim_max && !has_cap(CAP_SYS_RESOURCE)) /* a lower hard limit: no way back */ 0 1 1
if ((n.rlim_max < o.rlim_max && !has_cap(CAP_SYS_RESOURCE)) /* a lower hard limit: no way back */ 0 0 0
242 1 ▶ || cpu_used_up(res, n.rlim_cur)) {
Branch: 1 branch
BranchTrueFalseTotal
|| cpu_used_up(res, n.rlim_cur)) { 0 1 1
243 0 ▶ if ((rc = missing(aTHX_ uns, "rlimit"))) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if ((rc = missing(aTHX_ uns, "rlimit"))) return rc; 0 0 0
244 0 continue;
245 }
246 1 ▶ if (ar->nrl == AR_MAX) return EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (ar->nrl == AR_MAX) return EINVAL; 0 1 1
247 1 ar->rl_res[ar->nrl] = res; ar->rl_new[ar->nrl] = n; ar->rl_old[ar->nrl] = o; ar->nrl++;
248 }
249 }
250 345 ▶ if ((v = hv_fetchs(at, "sigignore", 0)) && SvOK(*v)) { /* [SIGNUMS] */
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "sigignore", 0)) && SvOK(*v)) { /* [SIGNUMS] */ 4 341 345
if ((v = hv_fetchs(at, "sigignore", 0)) && SvOK(*v)) { /* [SIGNUMS] */ 4 0 4
251 4 AV *l; SV **d; ps_sigset_t def; int have_def = 0;
252 4 ▶ if (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV) croak("Proc::Spawner::spawn: sigignore must be an array reference");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV) croak("Proc::Spawner::spawn: sigignore must be an array reference"); 4 0 4
if (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV) croak("Proc::Spawner::spawn: sigignore must be an array reference"); 0 4 4
253 6 ▶ if ((d = hv_fetchs(at, "sigdefault", 0)) && SvOK(*d)) { if ((rc = sigset_from(aTHX_ *d, &def))) return rc; have_def = 1; }
Branch: 3 branches
BranchTrueFalseTotal
if ((d = hv_fetchs(at, "sigdefault", 0)) && SvOK(*d)) { if ((rc = sigset_from(aTHX_ *d, &def))) return rc; have_def = 1; } 2 2 4
if ((d = hv_fetchs(at, "sigdefault", 0)) && SvOK(*d)) { if ((rc = sigset_from(aTHX_ *d, &def))) return rc; have_def = 1; } 2 0 2
if ((d = hv_fetchs(at, "sigdefault", 0)) && SvOK(*d)) { if ((rc = sigset_from(aTHX_ *d, &def))) return rc; have_def = 1; } 0 2 2
254 4 l = (AV *)SvRV(*v);
255 6 ▶ for (i = 0; i < av_count(l); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < av_count(l); i++) { 4 2 6
256 4 ▶ SV **e = av_fetch(l, i, 0); int s = e ? (int)SvIV(*e) : 0;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = av_fetch(l, i, 0); int s = e ? (int)SvIV(*e) : 0; 4 0 4
257 4 ▶ if (s <= 0 || s >= NSIG || s == SIGKILL || s == SIGSTOP || (have_def && sigismember(&def, s) == 1)) return EINVAL;
Branch: 6 branches
BranchTrueFalseTotal
if (s <= 0 || s >= NSIG || s == SIGKILL || s == SIGSTOP || (have_def && sigismember(&def, s) == 1)) return EINVAL; 4 0 4
if (s <= 0 || s >= NSIG || s == SIGKILL || s == SIGSTOP || (have_def && sigismember(&def, s) == 1)) return EINVAL; 4 0 4
if (s <= 0 || s >= NSIG || s == SIGKILL || s == SIGSTOP || (have_def && sigismember(&def, s) == 1)) return EINVAL; 4 0 4
if (s <= 0 || s >= NSIG || s == SIGKILL || s == SIGSTOP || (have_def && sigismember(&def, s) == 1)) return EINVAL; 4 0 4
if (s <= 0 || s >= NSIG || s == SIGKILL || s == SIGSTOP || (have_def && sigismember(&def, s) == 1)) return EINVAL; 2 2 4
if (s <= 0 || s >= NSIG || s == SIGKILL || s == SIGSTOP || (have_def && sigismember(&def, s) == 1)) return EINVAL; 1 1 2
258 3 ▶ if (s == SIGCHLD) { if ((rc = missing(aTHX_ uns, "sigignore"))) return rc; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (s == SIGCHLD) { if ((rc = missing(aTHX_ uns, "sigignore"))) return rc; continue; } 1 2 3
if (s == SIGCHLD) { if ((rc = missing(aTHX_ uns, "sigignore"))) return rc; continue; } 1 0 1
259 2 ▶ if (ar->nsig == AR_MAX) return EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (ar->nsig == AR_MAX) return EINVAL; 0 2 2
260 2 ar->sig[ar->nsig++] = s;
261 }
262 }
263 343 ▶ if ((v = hv_fetchs(at, "nice", 0)) && SvOK(*v) && SvIV(*v)) { /* an increment, as nice(1) */
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "nice", 0)) && SvOK(*v) && SvIV(*v)) { /* an increment, as nice(1) */ 1 342 343
if ((v = hv_fetchs(at, "nice", 0)) && SvOK(*v) && SvIV(*v)) { /* an increment, as nice(1) */ 1 0 1
if ((v = hv_fetchs(at, "nice", 0)) && SvOK(*v) && SvIV(*v)) { /* an increment, as nice(1) */ 1 0 1
264 1 int inc = (int)SvIV(*v), old, nw;
265 1 errno = 0; old = getpriority(PRIO_PROCESS, 0);
266 1 ▶ if (old == -1 && errno) return errno;
Branch: 2 branches
BranchTrueFalseTotal
if (old == -1 && errno) return errno; 0 1 1
if (old == -1 && errno) return errno; 0 0 0
267 1 ▶ nw = inc > 39 ? 19 : inc < -39 ? -20 : old + inc; if (nw > 19) nw = 19; if (nw < -20) nw = -20;
Branch: 4 branches
BranchTrueFalseTotal
nw = inc > 39 ? 19 : inc < -39 ? -20 : old + inc; if (nw > 19) nw = 19; if (nw < -20) nw = -20; 1 0 1
nw = inc > 39 ? 19 : inc < -39 ? -20 : old + inc; if (nw > 19) nw = 19; if (nw < -20) nw = -20; 1 0 1
nw = inc > 39 ? 19 : inc < -39 ? -20 : old + inc; if (nw > 19) nw = 19; if (nw < -20) nw = -20; 0 1 1
nw = inc > 39 ? 19 : inc < -39 ? -20 : old + inc; if (nw > 19) nw = 19; if (nw < -20) nw = -20; 0 1 1
268 1 ▶ if (nw > old) { /* putting it back lowers it: CAP_SYS_NICE, or RLIMIT_NICE allows it */
Branch: 1 branch
BranchTrueFalseTotal
if (nw > old) { /* putting it back lowers it: CAP_SYS_NICE, or RLIMIT_NICE allows it */ 1 0 1
269 1 int ok = has_cap(CAP_SYS_NICE);
270 # ifdef RLIMIT_NICE
271 struct rlimit r;
272 1 ▶ if (!ok && getrlimit(RLIMIT_NICE, &r) == 0 && (r.rlim_cur == RLIM_INFINITY || 20 - (long)r.rlim_cur <= old)) ok = 1;
Branch: 4 branches
BranchTrueFalseTotal
if (!ok && getrlimit(RLIMIT_NICE, &r) == 0 && (r.rlim_cur == RLIM_INFINITY || 20 - (long)r.rlim_cur <= old)) ok = 1; 1 0 1
if (!ok && getrlimit(RLIMIT_NICE, &r) == 0 && (r.rlim_cur == RLIM_INFINITY || 20 - (long)r.rlim_cur <= old)) ok = 1; 1 0 1
if (!ok && getrlimit(RLIMIT_NICE, &r) == 0 && (r.rlim_cur == RLIM_INFINITY || 20 - (long)r.rlim_cur <= old)) ok = 1; 1 0 1
if (!ok && getrlimit(RLIMIT_NICE, &r) == 0 && (r.rlim_cur == RLIM_INFINITY || 20 - (long)r.rlim_cur <= old)) ok = 1; 0 1 1
273 # endif
274 1 ▶ if (!ok) { if ((rc = missing(aTHX_ uns, "nice"))) return rc; nw = old; }
Branch: 2 branches
BranchTrueFalseTotal
if (!ok) { if ((rc = missing(aTHX_ uns, "nice"))) return rc; nw = old; } 1 0 1
if (!ok) { if ((rc = missing(aTHX_ uns, "nice"))) return rc; nw = old; } 1 0 1
275 }
276 0 ▶ if (nw != old) { ar->nice_on = 1; ar->nice_new = nw; ar->nice_old = old; }
Branch: 1 branch
BranchTrueFalseTotal
if (nw != old) { ar->nice_on = 1; ar->nice_new = nw; ar->nice_old = old; } 0 0 0
277 }
278 342 ▶ ar->on = ar->nrl || ar->nsig || ar->nice_on;
Branch: 3 branches
BranchTrueFalseTotal
ar->on = ar->nrl || ar->nsig || ar->nice_on; 341 1 342
ar->on = ar->nrl || ar->nsig || ar->nice_on; 339 2 341
ar->on = ar->nrl || ar->nsig || ar->nice_on; 0 339 339
279 342 return 0;
280 }
281  
282 static int
283 342 around_begin(around_t *ar) /* 0, or an errno (around_end puts back what was done) */
284 {
285 sigset_t all; int k;
286 342 ▶ if (!ar->on) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ar->on) return 0; 339 3 342
287 3 sigfillset(&all);
288 3 ▶ if (sigprocmask(SIG_BLOCK, &all, &ar->mask_old) != 0) return errno;
Branch: 1 branch
BranchTrueFalseTotal
if (sigprocmask(SIG_BLOCK, &all, &ar->mask_old) != 0) return errno; 0 3 3
289 3 ar->blocked = 1;
290 5 ▶ for (k = 0; k < ar->nsig; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < ar->nsig; k++) { 2 3 5
291 struct sigaction ign;
292 2 memset(&ign, 0, sizeof ign); ign.sa_handler = SIG_IGN; sigemptyset(&ign.sa_mask);
293 2 ▶ if (sigaction(ar->sig[k], &ign, &ar->sig_old[k]) != 0) return errno;
Branch: 1 branch
BranchTrueFalseTotal
if (sigaction(ar->sig[k], &ign, &ar->sig_old[k]) != 0) return errno; 0 2 2
294 2 ar->sig_done = k + 1;
295 }
296 3 ▶ if (ar->nice_on) { if (setpriority(PRIO_PROCESS, 0, ar->nice_new) != 0) return errno; ar->nice_done = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (ar->nice_on) { if (setpriority(PRIO_PROCESS, 0, ar->nice_new) != 0) return errno; ar->nice_done = 1; } 0 3 3
if (ar->nice_on) { if (setpriority(PRIO_PROCESS, 0, ar->nice_new) != 0) return errno; ar->nice_done = 1; } 0 0 0
297 4 ▶ for (k = 0; k < ar->nrl; k++) { if (setrlimit(ar->rl_res[k], &ar->rl_new[k]) != 0) return errno; ar->rl_done = k + 1; }
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < ar->nrl; k++) { if (setrlimit(ar->rl_res[k], &ar->rl_new[k]) != 0) return errno; ar->rl_done = k + 1; } 0 1 1
for (k = 0; k < ar->nrl; k++) { if (setrlimit(ar->rl_res[k], &ar->rl_new[k]) != 0) return errno; ar->rl_done = k + 1; } 1 3 4
298 3 return 0;
299 }
300  
301 static void
302 345 around_end(pTHX_ around_t *ar) /* errno is kept */
303 {
304 345 int e = errno, bad = 0;
305 346 ▶ while (ar->rl_done > 0) { int k = --ar->rl_done; if (setrlimit(ar->rl_res[k], &ar->rl_old[k]) != 0) bad = 1; }
Branch: 2 branches
BranchTrueFalseTotal
while (ar->rl_done > 0) { int k = --ar->rl_done; if (setrlimit(ar->rl_res[k], &ar->rl_old[k]) != 0) bad = 1; } 0 1 1
while (ar->rl_done > 0) { int k = --ar->rl_done; if (setrlimit(ar->rl_res[k], &ar->rl_old[k]) != 0) bad = 1; } 1 345 346
306 345 ▶ if (ar->nice_done) { if (setpriority(PRIO_PROCESS, 0, ar->nice_old) != 0) bad = 1; ar->nice_done = 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (ar->nice_done) { if (setpriority(PRIO_PROCESS, 0, ar->nice_old) != 0) bad = 1; ar->nice_done = 0; } 0 345 345
if (ar->nice_done) { if (setpriority(PRIO_PROCESS, 0, ar->nice_old) != 0) bad = 1; ar->nice_done = 0; } 0 0 0
307 347 ▶ while (ar->sig_done > 0) { int k = --ar->sig_done; if (sigaction(ar->sig[k], &ar->sig_old[k], NULL) != 0) bad = 1; }
Branch: 2 branches
BranchTrueFalseTotal
while (ar->sig_done > 0) { int k = --ar->sig_done; if (sigaction(ar->sig[k], &ar->sig_old[k], NULL) != 0) bad = 1; } 0 2 2
while (ar->sig_done > 0) { int k = --ar->sig_done; if (sigaction(ar->sig[k], &ar->sig_old[k], NULL) != 0) bad = 1; } 2 345 347
308 345 ▶ if (ar->blocked) { sigprocmask(SIG_SETMASK, &ar->mask_old, NULL); ar->blocked = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (ar->blocked) { sigprocmask(SIG_SETMASK, &ar->mask_old, NULL); ar->blocked = 0; } 3 342 345
309 345 ▶ if (bad) warn("Proc::Spawner: a setting changed around the call could not be put back");
Branch: 1 branch
BranchTrueFalseTotal
if (bad) warn("Proc::Spawner: a setting changed around the call could not be put back"); 0 345 345
310 345 errno = e;
311 345 }
312 #else /* Windows: none of them */
313 typedef struct { int on; } around_t;
314 static int
315 around_parse(pTHX_ HV *at, SV *uns, around_t *ar)
316 {
317 SV **v; int rc;
318 ar->on = 0;
319 if (!at) return 0;
320 if ((v = hv_fetchs(at, "rlimit", 0)) && SvOK(*v) && (rc = missing(aTHX_ uns, "rlimit"))) return rc;
321 if ((v = hv_fetchs(at, "sigignore", 0)) && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV && av_count((AV *)SvRV(*v))
322 && (rc = missing(aTHX_ uns, "sigignore"))) return rc;
323 if ((v = hv_fetchs(at, "nice", 0)) && SvOK(*v) && SvIV(*v) && (rc = missing(aTHX_ uns, "nice"))) return rc;
324 return 0;
325 }
326 static int around_begin(around_t *ar) { PERL_UNUSED_ARG(ar); return 0; }
327 static void around_end(pTHX_ around_t *ar) { PERL_UNUSED_ARG(ar); }
328 #endif
329  
330 MODULE = Proc::Spawner PACKAGE = Proc::Spawner
331  
332 PROTOTYPES: DISABLE
333  
334 #ifdef HAVE_POSIX_SPAWN
335  
336 void
337 __posix_spawn(file_sv, argv, envp, actions, attr, unsupported = &PL_sv_undef)
338 SV *file_sv
339 SV *argv
340 SV *envp
341 SV *actions
342 SV *attr
343 SV *unsupported
344 PREINIT:
345 posix_spawn_file_actions_t fa;
346 posix_spawnattr_t sa;
347 char **av, **ev;
348 346 int rc = 0, search = 0, want_pidfd = 0, pidfd = -1;
349 346 short flags = 0;
350 346 pid_t pid = 0;
351 AV *acts;
352 SSize_t i;
353 346 ▶ HV *at = NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *at = NULL; 13 333 346
354 SV **v;
355 ps_sigset_t set;
356 const char *file;
357 around_t ar;
358 PPCODE:
359 346 ENTER; SAVETMPS;
360 346 file = SPAWN_STR(file_sv);
361 346 av = strv(aTHX_ argv, "argv");
362 #ifdef _WIN32
363 ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : win_childenv(aTHX); /* NULL: CreateProcessW gives the child ours */
364 #else
365 346 ▶ ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : environ;
Branch: 1 branch
BranchTrueFalseTotal
ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : environ; 273 73 346
366 #endif
367 /* unsupported: { OPTION => 'drop' | 'fail' }, for the options this build lacks (see missing()) */
368 346 ▶ if (SvOK(unsupported) && (!SvROK(unsupported) || SvTYPE(SvRV(unsupported)) != SVt_PVHV)) croak("Proc::Spawner::spawn: unsupported must be a hash reference");
Branch: 3 branches
BranchTrueFalseTotal
if (SvOK(unsupported) && (!SvROK(unsupported) || SvTYPE(SvRV(unsupported)) != SVt_PVHV)) croak("Proc::Spawner::spawn: unsupported must be a hash reference"); 4 342 346
if (SvOK(unsupported) && (!SvROK(unsupported) || SvTYPE(SvRV(unsupported)) != SVt_PVHV)) croak("Proc::Spawner::spawn: unsupported must be a hash reference"); 3 1 4
if (SvOK(unsupported) && (!SvROK(unsupported) || SvTYPE(SvRV(unsupported)) != SVt_PVHV)) croak("Proc::Spawner::spawn: unsupported must be a hash reference"); 0 3 3
369 345 ▶ if (!SvROK(actions) || SvTYPE(SvRV(actions)) != SVt_PVAV) croak("Proc::Spawner::spawn: actions must be an array reference");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(actions) || SvTYPE(SvRV(actions)) != SVt_PVAV) croak("Proc::Spawner::spawn: actions must be an array reference"); 345 0 345
if (!SvROK(actions) || SvTYPE(SvRV(actions)) != SVt_PVAV) croak("Proc::Spawner::spawn: actions must be an array reference"); 0 345 345
370 345 acts = (AV *)SvRV(actions);
371 345 ▶ if (SvOK(attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(attr)) { 345 0 345
372 345 ▶ if (!SvROK(attr) || SvTYPE(SvRV(attr)) != SVt_PVHV) croak("Proc::Spawner::spawn: attr must be a hash reference");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(attr) || SvTYPE(SvRV(attr)) != SVt_PVHV) croak("Proc::Spawner::spawn: attr must be a hash reference"); 345 0 345
if (!SvROK(attr) || SvTYPE(SvRV(attr)) != SVt_PVHV) croak("Proc::Spawner::spawn: attr must be a hash reference"); 0 345 345
373 345 at = (HV *)SvRV(attr);
374 }
375 635 ▶ for (i = 0; i < av_count(acts); i++) { /* check everything that may croak before allocating */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < av_count(acts); i++) { /* check everything that may croak before allocating */ 290 345 635
376 290 SV **e = av_fetch(acts, i, 0);
377 290 ▶ if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i);
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 290 0 290
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 290 0 290
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 0 290 290
378 }
379 345 ▶ if (at) {
Branch: 1 branch
BranchTrueFalseTotal
if (at) { 345 0 345
380 345 ▶ if ((v = hv_fetchs(at, "sigdefault", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigdefault must be an array reference");
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "sigdefault", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigdefault must be an array reference"); 330 15 345
if ((v = hv_fetchs(at, "sigdefault", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigdefault must be an array reference"); 330 0 330
if ((v = hv_fetchs(at, "sigdefault", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigdefault must be an array reference"); 0 330 330
381 345 ▶ if ((v = hv_fetchs(at, "sigmask", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigmask must be an array reference");
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "sigmask", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigmask must be an array reference"); 329 16 345
if ((v = hv_fetchs(at, "sigmask", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigmask must be an array reference"); 329 0 329
if ((v = hv_fetchs(at, "sigmask", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigmask must be an array reference"); 0 329 329
382 }
383 345 rc = around_parse(aTHX_ at, unsupported, &ar); /* rlimit, sigignore, nice (M1) */
384 345 posix_spawn_file_actions_init(&fa);
385 345 posix_spawnattr_init(&sa);
386 #ifdef _WIN32
387 { /* the caller's choices: %Proc::Spawner::WINDOWS (win_spawn.h, WIN_OPT_*) */
388 HV *w = get_hv("Proc::Spawner::WINDOWS", 0); SV **o;
389 static const struct { const char *k; I32 l; int bit; } opt[] = {
390 { "new_group", 9, WIN_OPT_NEW_GROUP }, { "kill_with_parent", 16, WIN_OPT_KILL_WITH_PARENT }, { "new_console", 11, WIN_OPT_NEW_CONSOLE } };
391 int k;
392 for (k = 0; w && k < 3; k++)
393 if ((o = hv_fetch(w, opt[k].k, opt[k].l, 0)))
394 win_spawn_options = SvTRUE(*o) ? (win_spawn_options | opt[k].bit) : (win_spawn_options & ~opt[k].bit);
395 }
396 win_spawn_showwindow = w32_use_showwindow ? (int)w32_showwindow : -1; /* Win32::SetChildShowWindow (post-0.09) */
397 { /* Perl's own idea of the directory (PerlEnv_get_childdir, as Perl's spawn; with pseudo-forks it can differ from
398 the process's): first, so that the caller's chdir actions start from it */
399 char *d = PerlEnv_get_childdir();
400 if (d) { char *u = win_acp_to_utf8(d); if (!rc) rc = posix_spawn_file_actions_addchdir_np(&fa, u ? u : d); win_free(u); PerlEnv_free_childdir(d); }
401 }
402 #endif
403 635 ▶ for (i = 0; !rc && i < av_count(acts); i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; !rc && i < av_count(acts); i++) { 632 3 635
for (i = 0; !rc && i < av_count(acts); i++) { 290 342 632
404 290 SV **e = av_fetch(acts, i, 0);
405 AV *a; const char *op; SSize_t n;
406 290 ▶ if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i);
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 290 0 290
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 290 0 290
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 0 290 290
407 290 a = (AV *)SvRV(*e); n = av_count(a);
408 #define ARG(k) (k < n && av_fetch(a, k, 0) ? *av_fetch(a, k, 0) : &PL_sv_undef)
409 290 ▶ op = SvPV_nolen(ARG(0));
Branch: 2 branches
BranchTrueFalseTotal
op = SvPV_nolen(ARG(0)); 290 0 290
op = SvPV_nolen(ARG(0)); 290 0 290
410 290 ▶ if (strEQ(op, "dup2") && n == 3)
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(op, "dup2") && n == 3) 259 31 290
if (strEQ(op, "dup2") && n == 3) 259 0 259
411 259 ▶ rc = posix_spawn_file_actions_adddup2(&fa, SvIV(ARG(1)), SvIV(ARG(2)));
Branch: 4 branches
BranchTrueFalseTotal
rc = posix_spawn_file_actions_adddup2(&fa, SvIV(ARG(1)), SvIV(ARG(2))); 259 0 259
rc = posix_spawn_file_actions_adddup2(&fa, SvIV(ARG(1)), SvIV(ARG(2))); 259 0 259
rc = posix_spawn_file_actions_adddup2(&fa, SvIV(ARG(1)), SvIV(ARG(2))); 259 0 259
rc = posix_spawn_file_actions_adddup2(&fa, SvIV(ARG(1)), SvIV(ARG(2))); 259 0 259
412 31 ▶ else if (strEQ(op, "close") && n == 2)
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(op, "close") && n == 2) 8 23 31
else if (strEQ(op, "close") && n == 2) 8 0 8
413 8 ▶ rc = posix_spawn_file_actions_addclose(&fa, SvIV(ARG(1)));
Branch: 2 branches
BranchTrueFalseTotal
rc = posix_spawn_file_actions_addclose(&fa, SvIV(ARG(1))); 8 0 8
rc = posix_spawn_file_actions_addclose(&fa, SvIV(ARG(1))); 8 0 8
414 23 ▶ else if (strEQ(op, "open") && (n == 4 || n == 5))
Branch: 3 branches
BranchTrueFalseTotal
else if (strEQ(op, "open") && (n == 4 || n == 5)) 1 22 23
else if (strEQ(op, "open") && (n == 4 || n == 5)) 1 0 1
else if (strEQ(op, "open") && (n == 4 || n == 5)) 1 0 1
415 2 ▶ rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666);
Branch: 9 branches
BranchTrueFalseTotal
rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666); 1 0 1
rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666); 1 0 1
rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666); 1 0 1
rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666); 1 0 1
rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666); 1 0 1
rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666); 1 0 1
rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666); 1 0 1
rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666); 1 0 1
rc = posix_spawn_file_actions_addopen(&fa, SvIV(ARG(1)), SPAWN_STR(ARG(2)), SvIV(ARG(3)), n == 5 ? SvIV(ARG(4)) : 0666); 1 0 1
416 22 ▶ else if (strEQ(op, "chdir") && n == 2)
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(op, "chdir") && n == 2) 20 2 22
else if (strEQ(op, "chdir") && n == 2) 20 0 20
417 #ifdef HAVE_ADDCHDIR_NP
418 40 ▶ rc = posix_spawn_file_actions_addchdir_np(&fa, SPAWN_STR(ARG(1)));
Branch: 2 branches
BranchTrueFalseTotal
rc = posix_spawn_file_actions_addchdir_np(&fa, SPAWN_STR(ARG(1))); 20 0 20
rc = posix_spawn_file_actions_addchdir_np(&fa, SPAWN_STR(ARG(1))); 20 0 20
419 #else
420 rc = missing(aTHX_ unsupported, "chdir");
421 #endif
422 2 ▶ else if (strEQ(op, "fchdir") && n == 2)
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(op, "fchdir") && n == 2) 2 0 2
else if (strEQ(op, "fchdir") && n == 2) 2 0 2
423 #ifdef HAVE_ADDFCHDIR_NP
424 2 ▶ rc = posix_spawn_file_actions_addfchdir_np(&fa, SvIV(ARG(1)));
Branch: 2 branches
BranchTrueFalseTotal
rc = posix_spawn_file_actions_addfchdir_np(&fa, SvIV(ARG(1))); 2 0 2
rc = posix_spawn_file_actions_addfchdir_np(&fa, SvIV(ARG(1))); 2 0 2
425 #else
426 rc = missing(aTHX_ unsupported, "fchdir");
427 #endif
428 0 ▶ else if (strEQ(op, "closefrom") && n == 2)
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(op, "closefrom") && n == 2) 0 0 0
else if (strEQ(op, "closefrom") && n == 2) 0 0 0
429 #ifdef HAVE_ADDCLOSEFROM_NP
430 0 ▶ rc = posix_spawn_file_actions_addclosefrom_np(&fa, SvIV(ARG(1)));
Branch: 2 branches
BranchTrueFalseTotal
rc = posix_spawn_file_actions_addclosefrom_np(&fa, SvIV(ARG(1))); 0 0 0
rc = posix_spawn_file_actions_addclosefrom_np(&fa, SvIV(ARG(1))); 0 0 0
431 #else
432 rc = missing(aTHX_ unsupported, "closefrom");
433 #endif
434 0 ▶ else if (strEQ(op, "tcsetpgrp") && n == 2)
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(op, "tcsetpgrp") && n == 2) 0 0 0
else if (strEQ(op, "tcsetpgrp") && n == 2) 0 0 0
435 #ifdef HAVE_ADDTCSETPGRP_NP
436 0 ▶ rc = posix_spawn_file_actions_addtcsetpgrp_np(&fa, SvIV(ARG(1)));
Branch: 2 branches
BranchTrueFalseTotal
rc = posix_spawn_file_actions_addtcsetpgrp_np(&fa, SvIV(ARG(1))); 0 0 0
rc = posix_spawn_file_actions_addtcsetpgrp_np(&fa, SvIV(ARG(1))); 0 0 0
437 #else
438 rc = missing(aTHX_ unsupported, "tcsetpgrp");
439 #endif
440 else
441 0 rc = EINVAL; /* unknown action or wrong count: no croak here, things are allocated */
442 #undef ARG
443 }
444 345 ▶ if (!rc && at) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rc && at) { 342 3 345
if (!rc && at) { 342 0 342
445 342 ▶ if ((v = hv_fetchs(at, "search", 0)) && SvTRUE(*v)) search = 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "search", 0)) && SvTRUE(*v)) search = 1; 1 341 342
if ((v = hv_fetchs(at, "search", 0)) && SvTRUE(*v)) search = 1; 1 0 1
446 342 ▶ if ((v = hv_fetchs(at, "sigdefault", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "sigdefault", 0))) { 329 13 342
447 329 ▶ if (!(rc = sigset_from(aTHX_ *v, &set)) && !(rc = posix_spawnattr_setsigdefault(&sa, &set)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(rc = sigset_from(aTHX_ *v, &set)) && !(rc = posix_spawnattr_setsigdefault(&sa, &set))) 329 0 329
if (!(rc = sigset_from(aTHX_ *v, &set)) && !(rc = posix_spawnattr_setsigdefault(&sa, &set))) 329 0 329
448 329 flags |= POSIX_SPAWN_SETSIGDEF;
449 }
450 342 ▶ if (!rc && (v = hv_fetchs(at, "sigmask", 0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rc && (v = hv_fetchs(at, "sigmask", 0))) { 342 0 342
if (!rc && (v = hv_fetchs(at, "sigmask", 0))) { 329 13 342
451 329 ▶ if (!(rc = sigset_from(aTHX_ *v, &set)) && !(rc = posix_spawnattr_setsigmask(&sa, &set)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(rc = sigset_from(aTHX_ *v, &set)) && !(rc = posix_spawnattr_setsigmask(&sa, &set))) 329 0 329
if (!(rc = sigset_from(aTHX_ *v, &set)) && !(rc = posix_spawnattr_setsigmask(&sa, &set))) 329 0 329
452 329 flags |= POSIX_SPAWN_SETSIGMASK;
453 }
454 342 ▶ if (!rc && (v = hv_fetchs(at, "pgroup", 0)) && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!rc && (v = hv_fetchs(at, "pgroup", 0)) && SvOK(*v)) { 342 0 342
if (!rc && (v = hv_fetchs(at, "pgroup", 0)) && SvOK(*v)) { 3 339 342
if (!rc && (v = hv_fetchs(at, "pgroup", 0)) && SvOK(*v)) { 3 0 3
455 3 ▶ if (!(rc = posix_spawnattr_setpgroup(&sa, (pid_t)SvIV(*v))))
Branch: 1 branch
BranchTrueFalseTotal
if (!(rc = posix_spawnattr_setpgroup(&sa, (pid_t)SvIV(*v)))) 3 0 3
456 3 flags |= POSIX_SPAWN_SETPGROUP;
457 }
458 #ifndef _WIN32
459 342 ▶ if (!rc && ar.on && !(flags & POSIX_SPAWN_SETSIGMASK)) { /* every signal is blocked around the call: the child gets the mask from before */
Branch: 3 branches
BranchTrueFalseTotal
if (!rc && ar.on && !(flags & POSIX_SPAWN_SETSIGMASK)) { /* every signal is blocked around the call: the child gets the mask from before */ 342 0 342
if (!rc && ar.on && !(flags & POSIX_SPAWN_SETSIGMASK)) { /* every signal is blocked around the call: the child gets the mask from before */ 3 339 342
if (!rc && ar.on && !(flags & POSIX_SPAWN_SETSIGMASK)) { /* every signal is blocked around the call: the child gets the mask from before */ 1 2 3
460 sigset_t cur;
461 1 sigprocmask(SIG_BLOCK, NULL, &cur);
462 1 ▶ if (!(rc = posix_spawnattr_setsigmask(&sa, &cur))) flags |= POSIX_SPAWN_SETSIGMASK;
Branch: 1 branch
BranchTrueFalseTotal
if (!(rc = posix_spawnattr_setsigmask(&sa, &cur))) flags |= POSIX_SPAWN_SETSIGMASK; 1 0 1
463 }
464 #endif
465 342 ▶ if ((v = hv_fetchs(at, "resetids", 0)) && SvTRUE(*v)) flags |= POSIX_SPAWN_RESETIDS;
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "resetids", 0)) && SvTRUE(*v)) flags |= POSIX_SPAWN_RESETIDS; 0 342 342
if ((v = hv_fetchs(at, "resetids", 0)) && SvTRUE(*v)) flags |= POSIX_SPAWN_RESETIDS; 0 0 0
466 #ifdef POSIX_SPAWN_SETSID
467 342 ▶ if ((v = hv_fetchs(at, "setsid", 0)) && SvTRUE(*v)) flags |= POSIX_SPAWN_SETSID;
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "setsid", 0)) && SvTRUE(*v)) flags |= POSIX_SPAWN_SETSID; 2 340 342
if ((v = hv_fetchs(at, "setsid", 0)) && SvTRUE(*v)) flags |= POSIX_SPAWN_SETSID; 2 0 2
468 #else
469 if (!rc && (v = hv_fetchs(at, "setsid", 0)) && SvTRUE(*v)) rc = missing(aTHX_ unsupported, "setsid");
470 #endif
471 #ifdef _WIN32
472 if (!rc && (v = hv_fetchs(at, "sched", 0)) && SvOK(*v)) rc = missing(aTHX_ unsupported, "sched");
473 #else
474 342 ▶ if (!rc && (v = hv_fetchs(at, "sched", 0)) && SvOK(*v)) { /* [POLICY, PRIORITY] */
Branch: 3 branches
BranchTrueFalseTotal
if (!rc && (v = hv_fetchs(at, "sched", 0)) && SvOK(*v)) { /* [POLICY, PRIORITY] */ 342 0 342
if (!rc && (v = hv_fetchs(at, "sched", 0)) && SvOK(*v)) { /* [POLICY, PRIORITY] */ 0 342 342
if (!rc && (v = hv_fetchs(at, "sched", 0)) && SvOK(*v)) { /* [POLICY, PRIORITY] */ 0 0 0
475 0 ▶ AV *s = SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV ? (AV *)SvRV(*v) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
AV *s = SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV ? (AV *)SvRV(*v) : NULL; 0 0 0
AV *s = SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV ? (AV *)SvRV(*v) : NULL; 0 0 0
476 struct sched_param sp;
477 0 ▶ if (!s || av_count(s) != 2) rc = EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || av_count(s) != 2) rc = EINVAL; 0 0 0
if (!s || av_count(s) != 2) rc = EINVAL; 0 0 0
478 else {
479 0 sp.sched_priority = (int)SvIV(*av_fetch(s, 1, 0));
480 0 ▶ if (!(rc = posix_spawnattr_setschedpolicy(&sa, (int)SvIV(*av_fetch(s, 0, 0))))
Branch: 1 branch
BranchTrueFalseTotal
if (!(rc = posix_spawnattr_setschedpolicy(&sa, (int)SvIV(*av_fetch(s, 0, 0)))) 0 0 0
481 0 ▶ && !(rc = posix_spawnattr_setschedparam(&sa, &sp)))
Branch: 1 branch
BranchTrueFalseTotal
&& !(rc = posix_spawnattr_setschedparam(&sa, &sp))) 0 0 0
482 0 flags |= POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_SETSCHEDPARAM;
483 }
484 }
485 #endif
486 342 ▶ if (!rc && (v = hv_fetchs(at, "cgroup", 0)) && SvOK(*v)) { /* an fd of a cgroup v2 directory */
Branch: 3 branches
BranchTrueFalseTotal
if (!rc && (v = hv_fetchs(at, "cgroup", 0)) && SvOK(*v)) { /* an fd of a cgroup v2 directory */ 342 0 342
if (!rc && (v = hv_fetchs(at, "cgroup", 0)) && SvOK(*v)) { /* an fd of a cgroup v2 directory */ 0 342 342
if (!rc && (v = hv_fetchs(at, "cgroup", 0)) && SvOK(*v)) { /* an fd of a cgroup v2 directory */ 0 0 0
487 #ifdef HAVE_SETCGROUP_NP
488 0 ▶ if (!(rc = posix_spawnattr_setcgroup_np(&sa, (int)SvIV(*v))))
Branch: 1 branch
BranchTrueFalseTotal
if (!(rc = posix_spawnattr_setcgroup_np(&sa, (int)SvIV(*v)))) 0 0 0
489 0 flags |= POSIX_SPAWN_SETCGROUP;
490 #else
491 rc = missing(aTHX_ unsupported, "cgroup");
492 #endif
493 }
494 342 ▶ if (!rc && (v = hv_fetchs(at, "pidfd", 0)) && SvTRUE(*v))
Branch: 3 branches
BranchTrueFalseTotal
if (!rc && (v = hv_fetchs(at, "pidfd", 0)) && SvTRUE(*v)) 342 0 342
if (!rc && (v = hv_fetchs(at, "pidfd", 0)) && SvTRUE(*v)) 3 339 342
if (!rc && (v = hv_fetchs(at, "pidfd", 0)) && SvTRUE(*v)) 3 0 3
495 #ifdef HAVE_PIDFD_SPAWN
496 3 want_pidfd = 1;
497 #else
498 rc = missing(aTHX_ unsupported, "pidfd"); /* dropped: only the pid is returned */
499 #endif
500 #ifdef _WIN32
501 if (!rc && (v = hv_fetchs(at, "win_priority", 0)) && SvOK(*v)) { /* W4: a priority class, exact */
502 static const struct { const char *n; DWORD f; } pc[] = { { "idle", IDLE_PRIORITY_CLASS },
503 { "below_normal", BELOW_NORMAL_PRIORITY_CLASS }, { "normal", NORMAL_PRIORITY_CLASS },
504 { "above_normal", ABOVE_NORMAL_PRIORITY_CLASS }, { "high", HIGH_PRIORITY_CLASS }, { "realtime", REALTIME_PRIORITY_CLASS } };
505 const char *s = SvPV_nolen(*v); int k;
506 for (rc = EINVAL, k = 0; k < 6; k++) if (strEQ(s, pc[k].n)) { sa.priority_class = pc[k].f; rc = 0; }
507 }
508 if (!rc && (v = hv_fetchs(at, "win_job", 0)) && SvOK(*v)) { /* W4: limits of a job of the child's own, exact */
509 if (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVHV) rc = EINVAL;
510 else {
511 HV *j = (HV *)SvRV(*v); HE *e; win_job_limits_t *l = &sa.job;
512 hv_iterinit(j);
513 while (!rc && (e = hv_iternext(j))) {
514 const char *k = HePV(e, PL_na); SV *x = HeVAL(e);
515 if (!SvOK(x)) continue;
516 if (strEQ(k, "ProcessMemoryLimit")) { l->ProcessMemoryLimit = (SIZE_T)SvNV(x); l->flags |= JOB_OBJECT_LIMIT_PROCESS_MEMORY; }
517 else if (strEQ(k, "JobMemoryLimit")) { l->JobMemoryLimit = (SIZE_T)SvNV(x); l->flags |= JOB_OBJECT_LIMIT_JOB_MEMORY; }
518 else if (strEQ(k, "PerProcessUserTimeLimit")) { l->PerProcessUserTimeLimit = (LONGLONG)SvNV(x); l->flags |= JOB_OBJECT_LIMIT_PROCESS_TIME; }
519 else if (strEQ(k, "PerJobUserTimeLimit")) { l->PerJobUserTimeLimit = (LONGLONG)SvNV(x); l->flags |= JOB_OBJECT_LIMIT_JOB_TIME; }
520 else if (strEQ(k, "ActiveProcessLimit")) { l->ActiveProcessLimit = (DWORD)SvUV(x); l->flags |= JOB_OBJECT_LIMIT_ACTIVE_PROCESS; }
521 else rc = EINVAL;
522 }
523 }
524 }
525 #endif
526 342 ▶ if (!rc) rc = posix_spawnattr_setflags(&sa, flags);
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) rc = posix_spawnattr_setflags(&sa, flags); 342 0 342
527 }
528 345 ▶ if (!rc && !(rc = around_begin(&ar))) { /* nothing is allocated from here to around_end */
Branch: 2 branches
BranchTrueFalseTotal
if (!rc && !(rc = around_begin(&ar))) { /* nothing is allocated from here to around_end */ 342 3 345
if (!rc && !(rc = around_begin(&ar))) { /* nothing is allocated from here to around_end */ 342 0 342
529 #ifdef HAVE_PIDFD_SPAWN
530 342 ▶ if (want_pidfd) { /* glibc 2.39: a pidfd instead of a pid; the pid is asked from it */
Branch: 1 branch
BranchTrueFalseTotal
if (want_pidfd) { /* glibc 2.39: a pidfd instead of a pid; the pid is asked from it */ 3 339 342
531 3 ▶ rc = (search ? pidfd_spawnp : pidfd_spawn)(&pidfd, file, &fa, &sa, av, ev);
Branch: 1 branch
BranchTrueFalseTotal
rc = (search ? pidfd_spawnp : pidfd_spawn)(&pidfd, file, &fa, &sa, av, ev); 0 3 3
532 3 ▶ if (!rc && (pid = pidfd_getpid(pidfd)) < 0) pid = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!rc && (pid = pidfd_getpid(pidfd)) < 0) pid = 0; 3 0 3
if (!rc && (pid = pidfd_getpid(pidfd)) < 0) pid = 0; 0 3 3
533 }
534 else
535 #endif
536 339 ▶ rc = (search ? posix_spawnp : posix_spawn)(&pid, file, &fa, &sa, av, ev);
Branch: 1 branch
BranchTrueFalseTotal
rc = (search ? posix_spawnp : posix_spawn)(&pid, file, &fa, &sa, av, ev); 1 338 339
537 }
538 345 around_end(aTHX_ &ar);
539 345 posix_spawn_file_actions_destroy(&fa);
540 345 posix_spawnattr_destroy(&sa);
541 345 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 345 345
542 345 ▶ if (rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc) { 8 337 345
543 8 SETERRNO(rc, 0);
544 8 XSRETURN_EMPTY;
545 }
546 337 ▶ mXPUSHi(pid);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(pid); 0 337 337
547 337 ▶ if (want_pidfd) mXPUSHi(pidfd);
Branch: 2 branches
BranchTrueFalseTotal
if (want_pidfd) mXPUSHi(pidfd); 3 334 337
if (want_pidfd) mXPUSHi(pidfd); 0 3 3
548  
549 #else
550  
551 void
552 __posix_spawn(...)
553 PPCODE:
554 /* no posix_spawn in this libc (Makefile.PL): Proc::Spawner::spawn uses __spawn_nowait */
555 PERL_UNUSED_VAR(items);
556 SETERRNO(ENOSYS, 0);
557 XSRETURN_EMPTY;
558  
559 #endif
560  
561 #ifndef _WIN32
562  
563 void
564 __spawn_nowait(file_sv, argv, envp, attr = &PL_sv_undef, unsupported = &PL_sv_undef)
565 SV *file_sv
566 SV *argv
567 SV *envp
568 SV *attr
569 SV *unsupported
570 PREINIT:
571 char **av, **ev;
572 const char *file;
573 around_t ar;
574 0 HV *at = NULL;
575 0 int rc, p[2] = { -1, -1 }, e = 0;
576 0 ▶ pid_t pid = -1;
Branch: 1 branch
BranchTrueFalseTotal
pid_t pid = -1; 0 0 0
577 ssize_t got;
578 PPCODE:
579 /* The primitive without posix_spawn (decision M2), as Run 0.06's spawn_nowait: fork and exec with nothing in
580 between but the signal mask put back; the parent has done everything else around the call. Of the
581 attributes only rlimit, sigignore and nice (M1). The pid, or the empty list with $! (exec's errno too,
582 through a close-on-exec pipe). */
583 0 ENTER; SAVETMPS;
584 0 file = SvPV_nolen(file_sv);
585 0 av = strv(aTHX_ argv, "argv");
586 0 ▶ ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : environ;
Branch: 1 branch
BranchTrueFalseTotal
ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : environ; 0 0 0
587 0 ▶ if (SvOK(unsupported) && (!SvROK(unsupported) || SvTYPE(SvRV(unsupported)) != SVt_PVHV)) croak("Proc::Spawner::spawn: unsupported must be a hash reference");
Branch: 3 branches
BranchTrueFalseTotal
if (SvOK(unsupported) && (!SvROK(unsupported) || SvTYPE(SvRV(unsupported)) != SVt_PVHV)) croak("Proc::Spawner::spawn: unsupported must be a hash reference"); 0 0 0
if (SvOK(unsupported) && (!SvROK(unsupported) || SvTYPE(SvRV(unsupported)) != SVt_PVHV)) croak("Proc::Spawner::spawn: unsupported must be a hash reference"); 0 0 0
if (SvOK(unsupported) && (!SvROK(unsupported) || SvTYPE(SvRV(unsupported)) != SVt_PVHV)) croak("Proc::Spawner::spawn: unsupported must be a hash reference"); 0 0 0
588 0 ▶ if (SvOK(attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(attr)) { 0 0 0
589 0 ▶ if (!SvROK(attr) || SvTYPE(SvRV(attr)) != SVt_PVHV) croak("Proc::Spawner::spawn: attr must be a hash reference");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(attr) || SvTYPE(SvRV(attr)) != SVt_PVHV) croak("Proc::Spawner::spawn: attr must be a hash reference"); 0 0 0
if (!SvROK(attr) || SvTYPE(SvRV(attr)) != SVt_PVHV) croak("Proc::Spawner::spawn: attr must be a hash reference"); 0 0 0
590 0 at = (HV *)SvRV(attr);
591 }
592 0 rc = around_parse(aTHX_ at, unsupported, &ar);
593 0 ▶ if (!rc && pipe(p) != 0) rc = errno;
Branch: 2 branches
BranchTrueFalseTotal
if (!rc && pipe(p) != 0) rc = errno; 0 0 0
if (!rc && pipe(p) != 0) rc = errno; 0 0 0
594 0 ▶ if (!rc) { fcntl(p[0], F_SETFD, FD_CLOEXEC); fcntl(p[1], F_SETFD, FD_CLOEXEC); rc = around_begin(&ar); }
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { fcntl(p[0], F_SETFD, FD_CLOEXEC); fcntl(p[1], F_SETFD, FD_CLOEXEC); rc = around_begin(&ar); } 0 0 0
595 0 ▶ if (!rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { 0 0 0
596 0 pid = fork();
597 0 ▶ if (pid == 0) { /* async-signal-safe calls only */
Branch: 1 branch
BranchTrueFalseTotal
if (pid == 0) { /* async-signal-safe calls only */ 0 0 0
598 0 ▶ if (ar.blocked) sigprocmask(SIG_SETMASK, &ar.mask_old, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (ar.blocked) sigprocmask(SIG_SETMASK, &ar.mask_old, NULL); 0 0 0
599 0 execve(file, av, ev);
600 0 e = errno; got = write(p[1], &e, sizeof e); (void)got;
601 0 _exit(127);
602 }
603 0 ▶ if (pid < 0) rc = errno;
Branch: 1 branch
BranchTrueFalseTotal
if (pid < 0) rc = errno; 0 0 0
604 }
605 0 around_end(aTHX_ &ar);
606 0 ▶ if (p[1] >= 0) close(p[1]);
Branch: 1 branch
BranchTrueFalseTotal
if (p[1] >= 0) close(p[1]); 0 0 0
607 0 ▶ if (!rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { 0 0 0
608 0 ▶ do got = read(p[0], &e, sizeof e); while (got < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
do got = read(p[0], &e, sizeof e); while (got < 0 && errno == EINTR); 0 0 0
do got = read(p[0], &e, sizeof e); while (got < 0 && errno == EINTR); 0 0 0
609 0 ▶ if (got == (ssize_t)sizeof e) { int st; while (waitpid(pid, &st, 0) < 0 && errno == EINTR) ; rc = e ? e : EINVAL; }
Branch: 4 branches
BranchTrueFalseTotal
if (got == (ssize_t)sizeof e) { int st; while (waitpid(pid, &st, 0) < 0 && errno == EINTR) ; rc = e ? e : EINVAL; } 0 0 0
if (got == (ssize_t)sizeof e) { int st; while (waitpid(pid, &st, 0) < 0 && errno == EINTR) ; rc = e ? e : EINVAL; } 0 0 0
if (got == (ssize_t)sizeof e) { int st; while (waitpid(pid, &st, 0) < 0 && errno == EINTR) ; rc = e ? e : EINVAL; } 0 0 0
if (got == (ssize_t)sizeof e) { int st; while (waitpid(pid, &st, 0) < 0 && errno == EINTR) ; rc = e ? e : EINVAL; } 0 0 0
610 }
611 0 ▶ if (p[0] >= 0) close(p[0]);
Branch: 1 branch
BranchTrueFalseTotal
if (p[0] >= 0) close(p[0]); 0 0 0
612 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
613 0 ▶ if (rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc) { 0 0 0
614 0 SETERRNO(rc, 0);
615 0 XSRETURN_EMPTY;
616 }
617 0 ▶ mXPUSHi(pid);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(pid); 0 0 0
618  
619 void
620 __rlimits()
621 PPCODE:
622 /* The resources this system has, as NAME => NUMBER pairs (rlimit) */
623 # define RL(n) mXPUSHs(newSVpvs(#n)); mXPUSHi(RLIMIT_##n);
624 # ifdef RLIMIT_CPU
625 510 ▶ RL(CPU)
Branch: 2 branches
BranchTrueFalseTotal
RL(CPU) 0 510 510
RL(CPU) 0 510 510
626 # endif
627 # ifdef RLIMIT_FSIZE
628 510 ▶ RL(FSIZE)
Branch: 2 branches
BranchTrueFalseTotal
RL(FSIZE) 0 510 510
RL(FSIZE) 0 510 510
629 # endif
630 # ifdef RLIMIT_DATA
631 510 ▶ RL(DATA)
Branch: 2 branches
BranchTrueFalseTotal
RL(DATA) 0 510 510
RL(DATA) 0 510 510
632 # endif
633 # ifdef RLIMIT_STACK
634 510 ▶ RL(STACK)
Branch: 2 branches
BranchTrueFalseTotal
RL(STACK) 0 510 510
RL(STACK) 0 510 510
635 # endif
636 # ifdef RLIMIT_CORE
637 510 ▶ RL(CORE)
Branch: 2 branches
BranchTrueFalseTotal
RL(CORE) 0 510 510
RL(CORE) 0 510 510
638 # endif
639 # ifdef RLIMIT_RSS
640 510 ▶ RL(RSS)
Branch: 2 branches
BranchTrueFalseTotal
RL(RSS) 0 510 510
RL(RSS) 0 510 510
641 # endif
642 # ifdef RLIMIT_NPROC
643 510 ▶ RL(NPROC)
Branch: 2 branches
BranchTrueFalseTotal
RL(NPROC) 0 510 510
RL(NPROC) 0 510 510
644 # endif
645 # ifdef RLIMIT_NOFILE
646 510 ▶ RL(NOFILE)
Branch: 2 branches
BranchTrueFalseTotal
RL(NOFILE) 0 510 510
RL(NOFILE) 0 510 510
647 # endif
648 # ifdef RLIMIT_MEMLOCK
649 510 ▶ RL(MEMLOCK)
Branch: 2 branches
BranchTrueFalseTotal
RL(MEMLOCK) 0 510 510
RL(MEMLOCK) 0 510 510
650 # endif
651 # ifdef RLIMIT_AS
652 510 ▶ RL(AS)
Branch: 2 branches
BranchTrueFalseTotal
RL(AS) 0 510 510
RL(AS) 0 510 510
653 # endif
654 # ifdef RLIMIT_LOCKS
655 510 ▶ RL(LOCKS)
Branch: 2 branches
BranchTrueFalseTotal
RL(LOCKS) 0 510 510
RL(LOCKS) 0 510 510
656 # endif
657 # ifdef RLIMIT_SIGPENDING
658 510 ▶ RL(SIGPENDING)
Branch: 2 branches
BranchTrueFalseTotal
RL(SIGPENDING) 0 510 510
RL(SIGPENDING) 0 510 510
659 # endif
660 # ifdef RLIMIT_MSGQUEUE
661 510 ▶ RL(MSGQUEUE)
Branch: 2 branches
BranchTrueFalseTotal
RL(MSGQUEUE) 0 510 510
RL(MSGQUEUE) 0 510 510
662 # endif
663 # ifdef RLIMIT_NICE
664 510 ▶ RL(NICE)
Branch: 2 branches
BranchTrueFalseTotal
RL(NICE) 0 510 510
RL(NICE) 0 510 510
665 # endif
666 # ifdef RLIMIT_RTPRIO
667 510 ▶ RL(RTPRIO)
Branch: 2 branches
BranchTrueFalseTotal
RL(RTPRIO) 0 510 510
RL(RTPRIO) 0 510 510
668 # endif
669 # ifdef RLIMIT_RTTIME
670 510 ▶ RL(RTTIME)
Branch: 2 branches
BranchTrueFalseTotal
RL(RTTIME) 0 510 510
RL(RTTIME) 0 510 510
671 # endif
672 # undef RL
673  
674 void
675 __getrlimit(res)
676 int res
677 PPCODE:
678 /* (SOFT, HARD), 'unlimited' for RLIM_INFINITY; the empty list with $! */
679 struct rlimit r;
680 12 ▶ if (getrlimit(res, &r) != 0) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (getrlimit(res, &r) != 0) XSRETURN_EMPTY; 0 12 12
681 12 ▶ if (r.rlim_cur == RLIM_INFINITY) mXPUSHs(newSVpvs("unlimited")); else mXPUSHu((UV)r.rlim_cur);
Branch: 3 branches
BranchTrueFalseTotal
if (r.rlim_cur == RLIM_INFINITY) mXPUSHs(newSVpvs("unlimited")); else mXPUSHu((UV)r.rlim_cur); 5 7 12
if (r.rlim_cur == RLIM_INFINITY) mXPUSHs(newSVpvs("unlimited")); else mXPUSHu((UV)r.rlim_cur); 0 5 5
if (r.rlim_cur == RLIM_INFINITY) mXPUSHs(newSVpvs("unlimited")); else mXPUSHu((UV)r.rlim_cur); 0 7 7
682 12 ▶ if (r.rlim_max == RLIM_INFINITY) mXPUSHs(newSVpvs("unlimited")); else mXPUSHu((UV)r.rlim_max);
Branch: 3 branches
BranchTrueFalseTotal
if (r.rlim_max == RLIM_INFINITY) mXPUSHs(newSVpvs("unlimited")); else mXPUSHu((UV)r.rlim_max); 6 6 12
if (r.rlim_max == RLIM_INFINITY) mXPUSHs(newSVpvs("unlimited")); else mXPUSHu((UV)r.rlim_max); 0 6 6
if (r.rlim_max == RLIM_INFINITY) mXPUSHs(newSVpvs("unlimited")); else mXPUSHu((UV)r.rlim_max); 0 6 6
683  
684 #endif
685  
686 void
687 __native()
688 PPCODE:
689 /* The settings this build's primitive does natively (the rest Proc::Spawner emulates, or reports); "posix_spawn"
690 if there is one (else the primitive is __spawn_nowait). */
691 #ifndef _WIN32
692 498 ▶ mXPUSHs(newSVpvs("rlimit")); mXPUSHs(newSVpvs("sigignore")); mXPUSHs(newSVpvs("nice")); /* around the call, in C (M1) */
Branch: 3 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("rlimit")); mXPUSHs(newSVpvs("sigignore")); mXPUSHs(newSVpvs("nice")); /* around the call, in C (M1) */ 0 498 498
mXPUSHs(newSVpvs("rlimit")); mXPUSHs(newSVpvs("sigignore")); mXPUSHs(newSVpvs("nice")); /* around the call, in C (M1) */ 0 498 498
mXPUSHs(newSVpvs("rlimit")); mXPUSHs(newSVpvs("sigignore")); mXPUSHs(newSVpvs("nice")); /* around the call, in C (M1) */ 0 498 498
693 #endif
694 #ifdef HAVE_POSIX_SPAWN
695 498 ▶ mXPUSHs(newSVpvs("posix_spawn"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("posix_spawn")); 0 498 498
696 498 ▶ mXPUSHs(newSVpvs("dup2")); mXPUSHs(newSVpvs("close")); mXPUSHs(newSVpvs("open"));
Branch: 3 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("dup2")); mXPUSHs(newSVpvs("close")); mXPUSHs(newSVpvs("open")); 0 498 498
mXPUSHs(newSVpvs("dup2")); mXPUSHs(newSVpvs("close")); mXPUSHs(newSVpvs("open")); 0 498 498
mXPUSHs(newSVpvs("dup2")); mXPUSHs(newSVpvs("close")); mXPUSHs(newSVpvs("open")); 0 498 498
697 498 ▶ mXPUSHs(newSVpvs("sigmask")); mXPUSHs(newSVpvs("sigdefault")); mXPUSHs(newSVpvs("pgroup"));
Branch: 3 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("sigmask")); mXPUSHs(newSVpvs("sigdefault")); mXPUSHs(newSVpvs("pgroup")); 0 498 498
mXPUSHs(newSVpvs("sigmask")); mXPUSHs(newSVpvs("sigdefault")); mXPUSHs(newSVpvs("pgroup")); 0 498 498
mXPUSHs(newSVpvs("sigmask")); mXPUSHs(newSVpvs("sigdefault")); mXPUSHs(newSVpvs("pgroup")); 0 498 498
698 498 ▶ mXPUSHs(newSVpvs("resetids")); mXPUSHs(newSVpvs("search"));
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("resetids")); mXPUSHs(newSVpvs("search")); 0 498 498
mXPUSHs(newSVpvs("resetids")); mXPUSHs(newSVpvs("search")); 0 498 498
699 #ifndef _WIN32
700 498 ▶ mXPUSHs(newSVpvs("sched")); /* Windows: pgroup only as 0, sigmask only empty (win_spawn.h refuses the rest) */
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("sched")); /* Windows: pgroup only as 0, sigmask only empty (win_spawn.h refuses the rest) */ 0 498 498
701 #endif
702 #ifdef HAVE_ADDCHDIR_NP
703 498 ▶ mXPUSHs(newSVpvs("chdir"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("chdir")); 0 498 498
704 #endif
705 #ifdef HAVE_ADDFCHDIR_NP
706 498 ▶ mXPUSHs(newSVpvs("fchdir"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("fchdir")); 0 498 498
707 #endif
708 #ifdef HAVE_ADDCLOSEFROM_NP
709 498 ▶ mXPUSHs(newSVpvs("closefrom"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("closefrom")); 0 498 498
710 #endif
711 #ifdef HAVE_ADDTCSETPGRP_NP
712 498 ▶ mXPUSHs(newSVpvs("tcsetpgrp"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("tcsetpgrp")); 0 498 498
713 #endif
714 #ifdef POSIX_SPAWN_SETSID
715 498 ▶ mXPUSHs(newSVpvs("setsid"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("setsid")); 0 498 498
716 #endif
717 #ifdef HAVE_SETCGROUP_NP
718 498 ▶ mXPUSHs(newSVpvs("cgroup"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("cgroup")); 0 498 498
719 #endif
720 #ifdef HAVE_PIDFD_SPAWN
721 498 ▶ mXPUSHs(newSVpvs("pidfd"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("pidfd")); 0 498 498
722 #endif
723 #endif
724  
725 int
726 __set_cloexec(fd, on)
727 int fd
728 int on
729 CODE:
730 /* Set or clear FD_CLOEXEC; the old setting (1 or 0), or -1 if fd is not open. */
731 {
732 #ifdef _WIN32 /* close-on-exec is a handle that is not inherited */
733 intptr_t h = _get_osfhandle(fd); DWORD f;
734 if (h == -1 || h == -2 || !GetHandleInformation((HANDLE)h, &f)) RETVAL = -1;
735 else { RETVAL = (f & HANDLE_FLAG_INHERIT) ? 0 : 1; SetHandleInformation((HANDLE)h, HANDLE_FLAG_INHERIT, on ? 0 : HANDLE_FLAG_INHERIT); }
736 #else
737 386 int f = fcntl(fd, F_GETFD);
738 386 ▶ if (f < 0) RETVAL = -1;
Branch: 1 branch
BranchTrueFalseTotal
if (f < 0) RETVAL = -1; 0 386 386
739 386 ▶ else { RETVAL = (f & FD_CLOEXEC) ? 1 : 0; fcntl(fd, F_SETFD, on ? (f | FD_CLOEXEC) : (f & ~FD_CLOEXEC)); }
Branch: 1 branch
BranchTrueFalseTotal
else { RETVAL = (f & FD_CLOEXEC) ? 1 : 0; fcntl(fd, F_SETFD, on ? (f | FD_CLOEXEC) : (f & ~FD_CLOEXEC)); } 386 0 386
740 #endif
741 }
742 OUTPUT:
743 RETVAL
744  
745 int
746 __dup_high(fd, min)
747 int fd
748 int min
749 CODE:
750 /* A close-on-exec copy of fd numbered min or above (F_DUPFD_CLOEXEC), or -1 with $!. */
751 #ifdef _WIN32
752 { /* no F_DUPFD: dup until the copy is high enough, then close the ones below; not inherited (post-0.08, for
753 the emulation around Perl's own spawn, M2) */
754 int low[256], n = 0, d = -1, e;
755 while ((d = _dup(fd)) >= 0 && d < min && n < 256) low[n++] = d;
756 e = errno;
757 if (d >= 0 && d < min) { _close(d); d = -1; e = EMFILE; }
758 while (n > 0) _close(low[--n]);
759 if (d >= 0) { intptr_t h = _get_osfhandle(d); if (h != -1) SetHandleInformation((HANDLE)h, HANDLE_FLAG_INHERIT, 0); }
760 else errno = e;
761 RETVAL = d;
762 }
763 #else
764 0 RETVAL = fcntl(fd, F_DUPFD_CLOEXEC, min);
765 #endif
766 OUTPUT:
767 RETVAL
768  
769 int
770 __fchdir(fd)
771 int fd
772 CODE:
773 #ifdef _WIN32
774 PERL_UNUSED_VAR(fd); errno = ENOSYS; RETVAL = 0;
775 #else
776 0 ▶ RETVAL = fchdir(fd) == 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = fchdir(fd) == 0; 0 0 0
777 #endif
778 OUTPUT:
779 RETVAL
780  
781 int
782 __pidfd_open(pid)
783 int pid
784 CODE:
785 /* A pidfd for a child already started (the emulation of pidfd => 1), or -1 with $! (ENOSYS without it). */
786 #if defined(SYS_pidfd_open) && !defined(_WIN32)
787 0 ▶ RETVAL = (int)syscall(SYS_pidfd_open, (pid_t)pid, 0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)syscall(SYS_pidfd_open, (pid_t)pid, 0); 0 0 0
788 #else
789 PERL_UNUSED_VAR(pid); errno = ENOSYS; RETVAL = -1;
790 #endif
791 OUTPUT:
792 RETVAL
793  
794 int
795 __fd_limit()
796 CODE:
797 /* One more than the highest fd number this process may have (the soft RLIMIT_NOFILE, at most 65536). */
798 #ifdef _WIN32
799 RETVAL = _getmaxstdio();
800 #else
801 {
802 struct rlimit r;
803 0 ▶ RETVAL = getrlimit(RLIMIT_NOFILE, &r) == 0 && r.rlim_cur != RLIM_INFINITY && r.rlim_cur < 65536 ? (int)r.rlim_cur : 65536;
Branch: 4 branches
BranchTrueFalseTotal
RETVAL = getrlimit(RLIMIT_NOFILE, &r) == 0 && r.rlim_cur != RLIM_INFINITY && r.rlim_cur < 65536 ? (int)r.rlim_cur : 65536; 0 0 0
RETVAL = getrlimit(RLIMIT_NOFILE, &r) == 0 && r.rlim_cur != RLIM_INFINITY && r.rlim_cur < 65536 ? (int)r.rlim_cur : 65536; 0 0 0
RETVAL = getrlimit(RLIMIT_NOFILE, &r) == 0 && r.rlim_cur != RLIM_INFINITY && r.rlim_cur < 65536 ? (int)r.rlim_cur : 65536; 0 0 0
RETVAL = getrlimit(RLIMIT_NOFILE, &r) == 0 && r.rlim_cur != RLIM_INFINITY && r.rlim_cur < 65536 ? (int)r.rlim_cur : 65536; 0 0 0
804 }
805 #endif
806 OUTPUT:
807 RETVAL
808  
809 int
810 __fd_cloexec(fd)
811 int fd
812 CODE:
813 /* Is fd open, and close-on-exec? 1: close-on-exec, 0: inherited by a child, -1: not open. Portable
814 (fcntl F_GETFD); Perl itself could ask only through a handle, which would close the fd later. */
815 {
816 #ifdef _WIN32
817 intptr_t h = _get_osfhandle(fd); DWORD f;
818 RETVAL = h == -1 || h == -2 || !GetHandleInformation((HANDLE)h, &f) ? -1 : (f & HANDLE_FLAG_INHERIT) ? 0 : 1;
819 #else
820 4 int f = fcntl(fd, F_GETFD);
821 4 ▶ RETVAL = f < 0 ? -1 : (f & FD_CLOEXEC) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = f < 0 ? -1 : (f & FD_CLOEXEC) ? 1 : 0; 1 3 4
RETVAL = f < 0 ? -1 : (f & FD_CLOEXEC) ? 1 : 0; 0 4 4
822 #endif
823 }
824 OUTPUT:
825 RETVAL
826  
827 int
828 fpe_at_exec()
829 CODE:
830 /* What Perl's own exec gives SIGFPE: Perl may ignore SIGFPE for itself at startup and then restores
831 the saved startup disposition around exec (perl.h: PERL_FPU_INIT, PERL_FPU_PRE_EXEC).
832 1: ignored, 0: default, -1: this Perl does not touch SIGFPE (it reads as any other signal). */
833 #if !defined(HAS_FPSETMASK) && defined(SIGFPE) && defined(SIG_IGN)
834 493 ▶ RETVAL = PL_sigfpe_saved == (Sighandler_t)SIG_IGN ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PL_sigfpe_saved == (Sighandler_t)SIG_IGN ? 1 : 0; 0 493 493
835 #else
836 RETVAL = -1;
837 #endif
838 OUTPUT:
839 RETVAL
840  
841 #ifdef _WIN32
842  
843 IV
844 __win_spawn_handle(pid)
845 IV pid
846 CODE:
847 /* The process handle of a child started here (kept until __win_spawn_forget), or 0: for its exit code. */
848 RETVAL = PTR2IV(win_spawn_handle((pid_t)pid));
849 OUTPUT:
850 RETVAL
851  
852 void
853 __win_spawn_forget(pid)
854 IV pid
855 CODE:
856 win_spawn_forget((pid_t)pid);
857  
858 int
859 __wl_io(id, kind, fd, data)
860 IV id
861 char kind
862 int fd
863 SV *data
864 CODE:
865 /* W5 (win_loop.h): start a read ('r', at most SvIV(data) bytes) or a write ('w', the bytes of data) on fd; its
866 event comes from __wl_wait. 1, or 0 with $!. */
867 {
868 STRLEN len = 0; const char *p = NULL;
869 if (kind == 'w') p = SvPVbyte(data, len); else len = (STRLEN)SvIV(data);
870 RETVAL = wl_io(id, kind, fd, p, (DWORD)len);
871 }
872 OUTPUT:
873 RETVAL
874  
875 int
876 __wl_child(id, pid)
877 IV id
878 IV pid
879 CODE:
880 /* W5: watch the process pid for its end (its status then comes from waitpid). 1, or 0 with $!. */
881 RETVAL = wl_child(id, (DWORD)pid);
882 OUTPUT:
883 RETVAL
884  
885 void
886 __wl_cancel(id)
887 IV id
888 CODE:
889 wl_cancel(id);
890  
891 void
892 __wl_wait(ms)
893 int ms
894 PPCODE:
895 /* W5: the events of one wait of at most ms milliseconds (-1: no limit): [ID, KIND, BYTES, ERRNO] each; for a read,
896 BYTES undef at end of file (ERRNO 0) or on an error. While an alarm is set (Perl's alarm is a WM_TIMER message,
897 which only win32_async_check reads), the wait is cut into slices of 50 ms, each followed by that check. After a
898 console control event, it returns once Perl's own handler has marked the signal (200 ms at most): Perl then
899 dispatches it. */
900 {
901 OVERLAPPED_ENTRY e[64]; ULONG n = 0, i; DWORD t = ms < 0 ? INFINITE : (DWORD)ms; BOOL got;
902 int timer = w32_timerid != 0;
903 if (timer && (t == INFINITE || t > 50)) t = 50;
904 got = wl_init() && GetQueuedCompletionStatusEx(wl_port, e, 64, &n, t, FALSE);
905 if (InterlockedExchange(&wl_woken, 0)) { int k; for (k = 0; k < 200 && !PL_sig_pending; k++) Sleep(1); }
906 if (timer) win32_async_check(aTHX);
907 if (got) {
908 for (i = 0; i < n; i++) {
909 wl_op *op = (wl_op *)e[i].lpOverlapped; AV *av; int err;
910 if (e[i].lpCompletionKey == WL_KEY_WAKE) continue;
911 if (!op || !wl_complete(op, e[i].dwNumberOfBytesTransferred, e[i].lpCompletionKey)) continue;
912 err = op->err ? wl_errno(op->err) : 0;
913 if (op->kind == 'r' && (op->err == ERROR_BROKEN_PIPE || op->err == ERROR_HANDLE_EOF
914 || op->err == ERROR_PIPE_NOT_CONNECTED || (!op->err && op->done == 0))) err = 0;
915 av = newAV();
916 av_push(av, newSViv(op->id));
917 av_push(av, newSVpvn(&op->kind, 1));
918 av_push(av, op->kind == 'r' && op->done && !op->err ? newSVpvn(op->buf, op->done) : newSV(0));
919 av_push(av, newSViv(err));
920 mXPUSHs(newRV_noinc((SV *)av));
921 wl_free(op);
922 }
923 }
924 }
925  
926 IV
927 __win_ppid()
928 CODE:
929 /* The parent's process ID (Perl's getppid is unimplemented on Windows): NtQueryInformationProcess's
930 ProcessBasicInformation, InheritedFromUniqueProcessId; 0 if unknown. */
931 {
932 typedef LONG (NTAPI *nqip_t)(HANDLE, int, PVOID, ULONG, PULONG);
933 struct { LONG_PTR status; PVOID peb; ULONG_PTR affinity; LONG_PTR priority; ULONG_PTR pid, ppid; } pbi;
934 nqip_t f = (nqip_t)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
935 RETVAL = f && f(GetCurrentProcess(), 0, &pbi, sizeof pbi, NULL) == 0 ? (IV)pbi.ppid : 0;
936 }
937 OUTPUT:
938 RETVAL
939  
940 int
941 __wl_ctrl(on)
942 int on
943 CODE:
944 /* W5: install (1) or remove (0) the console control handler that wakes the loop; 1 if done */
945 RETVAL = wl_ctrl_watch(on);
946 OUTPUT:
947 RETVAL
948  
949 int
950 __wl_is_async(fd)
951 int fd
952 CODE:
953 /* W5: 1 if fd's handle was opened for overlapped I/O (the loop then needs no helper thread), 0 if not, -1 if no handle */
954 { intptr_t h = _get_osfhandle(fd); RETVAL = h == -1 || h == -2 ? -1 : wl_is_async((HANDLE)h); }
955 OUTPUT:
956 RETVAL
957  
958 void
959 __wl_pipe(async_r, async_w)
960 int async_r
961 int async_w
962 PPCODE:
963 /* W5: a pipe whose ends are opened for overlapped I/O where asked: (READ_FD, WRITE_FD), or nothing with $!. */
964 {
965 int r, w;
966 if (wl_pipe(async_r, async_w, &r, &w) == 0) { mXPUSHi(r); mXPUSHi(w); }
967 }
968  
969 const char *
970 __win_priority_class()
971 CODE:
972 /* this process's priority class, by the names win_priority takes (W4: nice is mapped from it) */
973 switch (GetPriorityClass(GetCurrentProcess())) {
974 case IDLE_PRIORITY_CLASS: RETVAL = "idle"; break;
975 case BELOW_NORMAL_PRIORITY_CLASS: RETVAL = "below_normal"; break;
976 case ABOVE_NORMAL_PRIORITY_CLASS: RETVAL = "above_normal"; break;
977 case HIGH_PRIORITY_CLASS: RETVAL = "high"; break;
978 case REALTIME_PRIORITY_CLASS: RETVAL = "realtime"; break;
979 default: RETVAL = "normal";
980 }
981 OUTPUT:
982 RETVAL
983  
984 #else
985  
986 int
987 chld_to_fd(fd)
988 int fd
989 CODE:
990 /* Install the C handler (SA_RESTART); keep the previous action for chld_restore. 1 or 0 with $!. */
991 struct sigaction sa;
992 537 memset(&sa, 0, sizeof sa);
993 537 sa.sa_handler = chld_handler;
994 537 sigemptyset(&sa.sa_mask);
995 537 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
996 537 chld_fd = fd;
997 537 ▶ RETVAL = sigaction(SIGCHLD, &sa, chld_installed ? NULL : &chld_old) == 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = sigaction(SIGCHLD, &sa, chld_installed ? NULL : &chld_old) == 0; 537 0 537
998 537 ▶ if (RETVAL) chld_installed = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL) chld_installed = 1; 537 0 537
999 OUTPUT:
1000 RETVAL
1001  
1002 int
1003 chld_restore()
1004 CODE:
1005 537 RETVAL = 1;
1006 537 ▶ if (chld_installed) { RETVAL = sigaction(SIGCHLD, &chld_old, NULL) == 0; chld_installed = 0; chld_fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (chld_installed) { RETVAL = sigaction(SIGCHLD, &chld_old, NULL) == 0; chld_installed = 0; chld_fd = -1; } 537 0 537
1007 OUTPUT:
1008 RETVAL
1009  
1010 #endif
« t/try.pl ↑ Summary