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 /* win_spawn.h's hooks (post-0.12): Perl's own idea of the environment and the directory, which in a pseudo-process (a
99 child of Perl's fork emulation) differ from the process's, as Perl's own spawn takes them (PerlEnv_getenv,
100 PerlEnv_get_childenv, PerlEnv_get_childdir); each a mortal UTF-8 string, alive until the call's FREETMPS */
101 static const char *
102 win_hook_var(void *ctx, const char *name)
103 {
104 dTHXa(ctx); const char *v = PerlEnv_getenv(name); char *u; SV *m;
105 PERL_UNUSED_ARG(ctx);
106 if (!v || !(u = win_acp_to_utf8(v))) return v;
107 m = sv_2mortal(newSVpv(u, 0)); win_free(u);
108 return SvPVX(m);
109 }
110 static char *const *
111 win_hook_env(void *ctx)
112 {
113 dTHXa(ctx); PERL_UNUSED_ARG(ctx);
114 return win_childenv(aTHX);
115 }
116 static const char *
117 win_hook_cwd(void *ctx)
118 {
119 dTHXa(ctx); char *d = PerlEnv_get_childdir(), *u; SV *m;
120 PERL_UNUSED_ARG(ctx);
121 if (!d) return NULL;
122 u = win_acp_to_utf8(d); m = sv_2mortal(newSVpv(u ? u : d, 0)); win_free(u); PerlEnv_free_childdir(d);
123 return SvPVX(m);
124 }
125 #else
126 # define SPAWN_STR(sv) SvPV_nolen(sv)
127 #endif
128  
129 /* A NULL-terminated char* vector from an array ref; freed with the scope. */
130 static char **
131 705 strv(pTHX_ SV *ref, const char *what)
132 {
133 AV *av; SSize_t n, i; char **v;
134 705 ▶ if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) 705 0 705
if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) 0 705 705
135 0 croak("Proc::Spawner::spawn: %s must be an array reference", what);
136 705 av = (AV *)SvRV(ref);
137 705 n = av_count(av);
138 705 ▶ Newx(v, n + 1, char *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(v, n + 1, char *); 0 705 705
139 705 SAVEFREEPV(v);
140 12784 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12079 705 12784
141 12079 SV **e = av_fetch(av, i, 0);
142 24158 ▶ v[i] = e ? (char *)SPAWN_STR(*e) : (char *)"";
Branch: 1 branch
BranchTrueFalseTotal
v[i] = e ? (char *)SPAWN_STR(*e) : (char *)""; 12079 0 12079
143 }
144 705 v[n] = NULL;
145 705 return v;
146 }
147  
148 static int
149 744 sigset_from(pTHX_ SV *ref, ps_sigset_t *set)
150 {
151 AV *av; SSize_t i;
152 744 ▶ 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"); 744 0 744
if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) croak("Proc::Spawner::spawn: signal list must be an array reference"); 0 744 744
153 744 av = (AV *)SvRV(ref);
154 744 ps_sigemptyset(set);
155 1896 ▶ for (i = 0; i < av_count(av); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < av_count(av); i++) { 1152 744 1896
156 1152 SV **e = av_fetch(av, i, 0);
157 1152 ▶ 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; 1152 0 1152
if (e && ps_sigaddset(set, (int)SvIV(*e)) != 0) return errno; 0 1152 1152
158 }
159 744 return 0;
160 }
161  
162 /* PROTOTYPE for decision D1, option A (page "Child Exit Wake-up"): a C handler for SIGCHLD
163 that only writes one byte to a file descriptor; no Perl code runs inside the signal.
164 The fd should be non-blocking, so that a full pipe cannot hang the handler. */
165 #ifndef _WIN32 /* no SIGCHLD on Windows: Proc::Spawner falls back (chld_to_fd is not defined there) */
166 static volatile sig_atomic_t chld_fd = -1;
167 static struct sigaction chld_old;
168 static int chld_installed = 0;
169 static void
170 277 chld_handler(int sig)
171 {
172 277 int saved = errno;
173 (void)sig;
174 277 ▶ 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; } 277 0 277
175 277 errno = saved;
176 277 }
177 #endif
178  
179 static int
180 3 missing(pTHX_ SV *uns, const char *name) /* 0 if the option is to be dropped, else ENOTSUP */
181 {
182 3 SV **v = NULL; HV *g;
183 3 ▶ 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 3 3
184 3 ▶ 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))) 3 0 3
if (!v && (g = get_hv("Proc::Spawner::UNSUPPORTED", 0)) && !(v = hv_fetch(g, name, strlen(name), 0))) 3 0 3
if (!v && (g = get_hv("Proc::Spawner::UNSUPPORTED", 0)) && !(v = hv_fetch(g, name, strlen(name), 0))) 3 0 3
185 3 v = hv_fetchs(g, "*", 0);
186 6 ▶ 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; 3 0 3
return v && SvOK(*v) && strEQ(SvPV_nolen(*v), "drop") ? 0 : ENOTSUP; 3 0 3
return v && SvOK(*v) && strEQ(SvPV_nolen(*v), "drop") ? 0 : ENOTSUP; 0 3 3
187 }
188  
189 /* Settings POSIX's posix_spawn has no option for: rlimit, sigignore, nice (the user, 2026-11-06, decision M1). They are
190 set in this process right around the call, in C (nothing is allocated and no Perl code runs in between), and put
191 back right after; the child inherits them. One that could not be put back is unsupported (dropped or failed, as
192 UNSUPPORTED says): a lower hard limit, or a higher nice value, without the privilege to undo it; a CPU limit this
193 process has used up already (it would get SIGXCPU); ignoring SIGCHLD (this process's other children ending
194 meanwhile would be reaped by the kernel, their statuses lost). Every signal is blocked meanwhile (Linux keeps a
195 blocked signal pending even while it is ignored); the child gets the mask from before. On Windows: unsupported. */
196 #define AR_MAX 32
197 #ifndef _WIN32
198 # ifdef __linux__
199 # include <linux/capability.h>
200 # endif
201 # ifndef CAP_SYS_NICE
202 # define CAP_SYS_NICE 23
203 # endif
204 # ifndef CAP_SYS_RESOURCE
205 # define CAP_SYS_RESOURCE 24
206 # endif
207 typedef struct {
208 int on; /* anything to do */
209 int nrl, rl_res[AR_MAX], rl_done; struct rlimit rl_new[AR_MAX], rl_old[AR_MAX];
210 int nsig, sig[AR_MAX], sig_done; struct sigaction sig_old[AR_MAX];
211 int nice_on, nice_new, nice_old, nice_done;
212 sigset_t mask_old; int blocked;
213 } around_t;
214  
215 static int
216 2 has_cap(int cap) /* may this process do what needs the capability (CAP_SYS_NICE, CAP_SYS_RESOURCE)? */
217 {
218 # ifdef __linux__
219 struct __user_cap_header_struct h; struct __user_cap_data_struct d[2];
220 2 memset(&h, 0, sizeof h); memset(d, 0, sizeof d);
221 2 h.version = _LINUX_CAPABILITY_VERSION_3; h.pid = 0;
222 2 ▶ 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; 2 0 2
223 # endif
224 0 return geteuid() == 0;
225 }
226  
227 static int
228 1 cpu_used_up(int res, rlim_t lim) /* a CPU limit this process has used up already (it would get SIGXCPU meanwhile) */
229 {
230 # ifdef RLIMIT_CPU
231 struct rusage u;
232 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
233 0 return lim <= (rlim_t)(u.ru_utime.tv_sec + u.ru_stime.tv_sec) + 1;
234 # else
235 return 0;
236 # endif
237 }
238  
239 static rlim_t
240 2 rl_value(pTHX_ SV **sv, rlim_t keep) /* undef: keep ours; 'unlimited' or 'infinity': RLIM_INFINITY */
241 {
242 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
243 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
244 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
245 0 croak("Proc::Spawner::spawn: rlimit value '%s' is not a number", SvPV_nolen(*sv));
246 }
247  
248 static int
249 389 around_parse(pTHX_ HV *at, SV *uns, around_t *ar) /* 0, or an errno; croaks on malformed input (before anything is allocated) */
250 {
251 SV **v; SSize_t i; int rc;
252 389 memset(ar, 0, sizeof *ar);
253 389 ▶ if (!at) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!at) return 0; 0 389 389
254 389 ▶ 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 387 389
if ((v = hv_fetchs(at, "rlimit", 0)) && SvOK(*v)) { /* [[RESOURCE, SOFT, HARD], ...] */ 2 0 2
255 AV *l;
256 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
257 2 l = (AV *)SvRV(*v);
258 3 ▶ for (i = 0; i < av_count(l); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < av_count(l); i++) { 1 2 3
259 1 SV **e = av_fetch(l, i, 0); AV *r; struct rlimit o, n; int res;
260 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
261 0 croak("Proc::Spawner::spawn: rlimit entry %ld must be [RESOURCE, SOFT, HARD]", (long)i);
262 1 r = (AV *)SvRV(*e); res = (int)SvIV(*av_fetch(r, 0, 0));
263 1 ▶ if (getrlimit(res, &o) != 0) return errno;
Branch: 1 branch
BranchTrueFalseTotal
if (getrlimit(res, &o) != 0) return errno; 0 1 1
264 1 n.rlim_cur = rl_value(aTHX_ av_fetch(r, 1, 0), o.rlim_cur);
265 1 n.rlim_max = rl_value(aTHX_ av_fetch(r, 2, 0), o.rlim_max);
266 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
267 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
268 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
269 1 ▶ || cpu_used_up(res, n.rlim_cur)) {
Branch: 1 branch
BranchTrueFalseTotal
|| cpu_used_up(res, n.rlim_cur)) { 0 1 1
270 0 ▶ if ((rc = missing(aTHX_ uns, "rlimit"))) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if ((rc = missing(aTHX_ uns, "rlimit"))) return rc; 0 0 0
271 0 continue;
272 }
273 1 ▶ if (ar->nrl == AR_MAX) return EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (ar->nrl == AR_MAX) return EINVAL; 0 1 1
274 1 ar->rl_res[ar->nrl] = res; ar->rl_new[ar->nrl] = n; ar->rl_old[ar->nrl] = o; ar->nrl++;
275 }
276 }
277 389 ▶ 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 385 389
if ((v = hv_fetchs(at, "sigignore", 0)) && SvOK(*v)) { /* [SIGNUMS] */ 4 0 4
278 4 AV *l; SV **d; ps_sigset_t def; int have_def = 0;
279 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
280 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
281 4 l = (AV *)SvRV(*v);
282 6 ▶ for (i = 0; i < av_count(l); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < av_count(l); i++) { 4 2 6
283 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
284 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
285 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
286 2 ▶ if (ar->nsig == AR_MAX) return EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (ar->nsig == AR_MAX) return EINVAL; 0 2 2
287 2 ar->sig[ar->nsig++] = s;
288 }
289 }
290 387 ▶ 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) */ 2 385 387
if ((v = hv_fetchs(at, "nice", 0)) && SvOK(*v) && SvIV(*v)) { /* an increment, as nice(1) */ 2 0 2
if ((v = hv_fetchs(at, "nice", 0)) && SvOK(*v) && SvIV(*v)) { /* an increment, as nice(1) */ 2 0 2
291 2 int inc = (int)SvIV(*v), old, nw;
292 2 errno = 0; old = getpriority(PRIO_PROCESS, 0);
293 2 ▶ if (old == -1 && errno) return errno;
Branch: 2 branches
BranchTrueFalseTotal
if (old == -1 && errno) return errno; 0 2 2
if (old == -1 && errno) return errno; 0 0 0
294 2 ▶ 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; 2 0 2
nw = inc > 39 ? 19 : inc < -39 ? -20 : old + inc; if (nw > 19) nw = 19; if (nw < -20) nw = -20; 2 0 2
nw = inc > 39 ? 19 : inc < -39 ? -20 : old + inc; if (nw > 19) nw = 19; if (nw < -20) nw = -20; 0 2 2
nw = inc > 39 ? 19 : inc < -39 ? -20 : old + inc; if (nw > 19) nw = 19; if (nw < -20) nw = -20; 0 2 2
295 2 ▶ 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 */ 2 0 2
296 2 int ok = has_cap(CAP_SYS_NICE);
297 # ifdef RLIMIT_NICE
298 struct rlimit r;
299 2 ▶ 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; 2 0 2
if (!ok && getrlimit(RLIMIT_NICE, &r) == 0 && (r.rlim_cur == RLIM_INFINITY || 20 - (long)r.rlim_cur <= old)) ok = 1; 2 0 2
if (!ok && getrlimit(RLIMIT_NICE, &r) == 0 && (r.rlim_cur == RLIM_INFINITY || 20 - (long)r.rlim_cur <= old)) ok = 1; 2 0 2
if (!ok && getrlimit(RLIMIT_NICE, &r) == 0 && (r.rlim_cur == RLIM_INFINITY || 20 - (long)r.rlim_cur <= old)) ok = 1; 0 2 2
300 # endif
301 2 ▶ 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; } 2 0 2
if (!ok) { if ((rc = missing(aTHX_ uns, "nice"))) return rc; nw = old; } 2 0 2
302 }
303 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
304 }
305 385 ▶ ar->on = ar->nrl || ar->nsig || ar->nice_on;
Branch: 3 branches
BranchTrueFalseTotal
ar->on = ar->nrl || ar->nsig || ar->nice_on; 384 1 385
ar->on = ar->nrl || ar->nsig || ar->nice_on; 382 2 384
ar->on = ar->nrl || ar->nsig || ar->nice_on; 0 382 382
306 385 return 0;
307 }
308  
309 static int
310 385 around_begin(around_t *ar) /* 0, or an errno (around_end puts back what was done) */
311 {
312 sigset_t all; int k;
313 385 ▶ if (!ar->on) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ar->on) return 0; 382 3 385
314 3 sigfillset(&all);
315 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
316 3 ar->blocked = 1;
317 5 ▶ for (k = 0; k < ar->nsig; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < ar->nsig; k++) { 2 3 5
318 struct sigaction ign;
319 2 memset(&ign, 0, sizeof ign); ign.sa_handler = SIG_IGN; sigemptyset(&ign.sa_mask);
320 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
321 2 ar->sig_done = k + 1;
322 }
323 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
324 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
325 3 return 0;
326 }
327  
328 static void
329 389 around_end(pTHX_ around_t *ar) /* errno is kept */
330 {
331 389 int e = errno, bad = 0;
332 390 ▶ 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 389 390
333 389 ▶ 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 389 389
if (ar->nice_done) { if (setpriority(PRIO_PROCESS, 0, ar->nice_old) != 0) bad = 1; ar->nice_done = 0; } 0 0 0
334 391 ▶ 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 389 391
335 389 ▶ 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 386 389
336 389 ▶ 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 389 389
337 389 errno = e;
338 389 }
339 #else /* Windows: none of them */
340 typedef struct { int on; } around_t;
341 static int
342 around_parse(pTHX_ HV *at, SV *uns, around_t *ar)
343 {
344 SV **v; int rc;
345 ar->on = 0;
346 if (!at) return 0;
347 if ((v = hv_fetchs(at, "rlimit", 0)) && SvOK(*v) && (rc = missing(aTHX_ uns, "rlimit"))) return rc;
348 if ((v = hv_fetchs(at, "sigignore", 0)) && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV && av_count((AV *)SvRV(*v))
349 && (rc = missing(aTHX_ uns, "sigignore"))) return rc;
350 if ((v = hv_fetchs(at, "nice", 0)) && SvOK(*v) && SvIV(*v) && (rc = missing(aTHX_ uns, "nice"))) return rc;
351 return 0;
352 }
353 static int around_begin(around_t *ar) { PERL_UNUSED_ARG(ar); return 0; }
354 static void around_end(pTHX_ around_t *ar) { PERL_UNUSED_ARG(ar); }
355 #endif
356  
357 MODULE = Proc::Spawner PACKAGE = Proc::Spawner
358  
359 PROTOTYPES: DISABLE
360  
361 #ifdef HAVE_POSIX_SPAWN
362  
363 void
364 __posix_spawn(file_sv, argv, envp, actions, attr, unsupported = &PL_sv_undef)
365 SV *file_sv
366 SV *argv
367 SV *envp
368 SV *actions
369 SV *attr
370 SV *unsupported
371 PREINIT:
372 posix_spawn_file_actions_t fa;
373 posix_spawnattr_t sa;
374 char **av, **ev;
375 390 int rc = 0, search = 0, want_pidfd = 0, pidfd = -1;
376 390 short flags = 0;
377 390 pid_t pid = 0;
378 AV *acts;
379 SSize_t i;
380 390 ▶ HV *at = NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *at = NULL; 14 376 390
381 SV **v;
382 ps_sigset_t set;
383 const char *file;
384 around_t ar;
385 #ifdef _WIN32
386 win_spawn_hooks_t hooks;
387 #endif
388 PPCODE:
389 #ifdef _WIN32
390 memset(&hooks, 0, sizeof hooks);
391 #endif
392 390 ENTER; SAVETMPS;
393 390 file = SPAWN_STR(file_sv);
394 390 av = strv(aTHX_ argv, "argv");
395 #ifdef _WIN32
396 ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : NULL; /* NULL: Perl's environment (the hook env) */
397 #else
398 390 ▶ ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : environ;
Branch: 1 branch
BranchTrueFalseTotal
ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : environ; 315 75 390
399 #endif
400 /* unsupported: { OPTION => 'drop' | 'fail' }, for the options this build lacks (see missing()) */
401 390 ▶ 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 386 390
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
402 389 ▶ 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"); 389 0 389
if (!SvROK(actions) || SvTYPE(SvRV(actions)) != SVt_PVAV) croak("Proc::Spawner::spawn: actions must be an array reference"); 0 389 389
403 389 acts = (AV *)SvRV(actions);
404 389 ▶ if (SvOK(attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(attr)) { 389 0 389
405 389 ▶ 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"); 389 0 389
if (!SvROK(attr) || SvTYPE(SvRV(attr)) != SVt_PVHV) croak("Proc::Spawner::spawn: attr must be a hash reference"); 0 389 389
406 389 at = (HV *)SvRV(attr);
407 }
408 703 ▶ 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 */ 314 389 703
409 314 SV **e = av_fetch(acts, i, 0);
410 314 ▶ 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); 314 0 314
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 314 0 314
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 0 314 314
411 }
412 389 ▶ if (at) {
Branch: 1 branch
BranchTrueFalseTotal
if (at) { 389 0 389
413 389 ▶ 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"); 372 17 389
if ((v = hv_fetchs(at, "sigdefault", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigdefault must be an array reference"); 372 0 372
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 372 372
414 389 ▶ 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"); 371 18 389
if ((v = hv_fetchs(at, "sigmask", 0)) && (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVAV)) croak("Proc::Spawner::spawn: sigmask must be an array reference"); 371 0 371
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 371 371
415 }
416 389 rc = around_parse(aTHX_ at, unsupported, &ar); /* rlimit, sigignore, nice (M1) */
417 389 posix_spawn_file_actions_init(&fa);
418 389 posix_spawnattr_init(&sa);
419 #ifdef _WIN32
420 { /* the caller's choices, in this call's attributes (post-0.12; before, win_spawn.h's process-wide variables):
421 %Proc::Spawner::WINDOWS over the defaults (win_spawn.h, WIN_OPT_*) */
422 HV *w = get_hv("Proc::Spawner::WINDOWS", 0); SV **o; int opts = sa.options, k;
423 static const struct { const char *k; I32 l; int bit; } opt[] = {
424 { "new_group", 9, WIN_OPT_NEW_GROUP }, { "kill_with_parent", 16, WIN_OPT_KILL_WITH_PARENT }, { "new_console", 11, WIN_OPT_NEW_CONSOLE } };
425 for (k = 0; w && k < 3; k++)
426 if ((o = hv_fetch(w, opt[k].k, opt[k].l, 0)) && !(SvPOK(*o) && strEQ(SvPVX(*o), "auto"))) /* (new_group 'auto':
427 Proc::Spawner::spawn decides, win_new_group below; called directly, the default: a new group) */
428 opts = SvTRUE(*o) ? (opts | opt[k].bit) : (opts & ~opt[k].bit);
429 win_attr_setoptions(&sa, opts);
430 }
431 win_attr_setshowwindow(&sa, w32_use_showwindow ? (int)w32_showwindow : -1); /* Win32::SetChildShowWindow (post-0.09) */
432 /* Perl's own idea of the environment and the directory (as Perl's spawn; with pseudo-forks they can differ from the
433 process's): the directory is the child's until a chdir action, and the base of relative names; PATH and PATHEXT
434 are Perl's (post-0.12, X57: they were the process's) */
435 # ifdef PERL_IMPLICIT_CONTEXT
436 hooks.ctx = (void *)aTHX;
437 # else
438 hooks.ctx = NULL;
439 # endif
440 hooks.var = win_hook_var; hooks.env = win_hook_env; hooks.cwd = win_hook_cwd;
441 win_attr_sethooks(&sa, &hooks);
442 #endif
443 703 ▶ for (i = 0; !rc && i < av_count(acts); i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; !rc && i < av_count(acts); i++) { 699 4 703
for (i = 0; !rc && i < av_count(acts); i++) { 314 385 699
444 314 SV **e = av_fetch(acts, i, 0);
445 AV *a; const char *op; SSize_t n;
446 314 ▶ 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); 314 0 314
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 314 0 314
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) croak("Proc::Spawner::spawn: action %ld is not an array reference", (long)i); 0 314 314
447 314 a = (AV *)SvRV(*e); n = av_count(a);
448 #define ARG(k) (k < n && av_fetch(a, k, 0) ? *av_fetch(a, k, 0) : &PL_sv_undef)
449 314 ▶ op = SvPV_nolen(ARG(0));
Branch: 2 branches
BranchTrueFalseTotal
op = SvPV_nolen(ARG(0)); 314 0 314
op = SvPV_nolen(ARG(0)); 314 0 314
450 314 ▶ if (strEQ(op, "dup2") && n == 3)
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(op, "dup2") && n == 3) 282 32 314
if (strEQ(op, "dup2") && n == 3) 282 0 282
451 282 ▶ 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))); 282 0 282
rc = posix_spawn_file_actions_adddup2(&fa, SvIV(ARG(1)), SvIV(ARG(2))); 282 0 282
rc = posix_spawn_file_actions_adddup2(&fa, SvIV(ARG(1)), SvIV(ARG(2))); 282 0 282
rc = posix_spawn_file_actions_adddup2(&fa, SvIV(ARG(1)), SvIV(ARG(2))); 282 0 282
452 32 ▶ else if (strEQ(op, "close") && n == 2)
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(op, "close") && n == 2) 8 24 32
else if (strEQ(op, "close") && n == 2) 8 0 8
453 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
454 24 ▶ else if (strEQ(op, "open") && (n == 4 || n == 5))
Branch: 3 branches
BranchTrueFalseTotal
else if (strEQ(op, "open") && (n == 4 || n == 5)) 1 23 24
else if (strEQ(op, "open") && (n == 4 || n == 5)) 1 0 1
else if (strEQ(op, "open") && (n == 4 || n == 5)) 1 0 1
455 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
456 23 ▶ else if (strEQ(op, "chdir") && n == 2)
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(op, "chdir") && n == 2) 21 2 23
else if (strEQ(op, "chdir") && n == 2) 21 0 21
457 #ifdef HAVE_ADDCHDIR_NP
458 42 ▶ 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))); 21 0 21
rc = posix_spawn_file_actions_addchdir_np(&fa, SPAWN_STR(ARG(1))); 21 0 21
459 #else
460 rc = missing(aTHX_ unsupported, "chdir");
461 #endif
462 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
463 #ifdef HAVE_ADDFCHDIR_NP
464 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
465 #else
466 rc = missing(aTHX_ unsupported, "fchdir");
467 #endif
468 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
469 #ifdef HAVE_ADDCLOSEFROM_NP
470 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
471 #else
472 rc = missing(aTHX_ unsupported, "closefrom");
473 #endif
474 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
475 #ifdef HAVE_ADDTCSETPGRP_NP
476 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
477 #else
478 rc = missing(aTHX_ unsupported, "tcsetpgrp");
479 #endif
480 else
481 0 rc = EINVAL; /* unknown action or wrong count: no croak here, things are allocated */
482 #undef ARG
483 }
484 389 ▶ if (!rc && at) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rc && at) { 385 4 389
if (!rc && at) { 385 0 385
485 385 ▶ 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 384 385
if ((v = hv_fetchs(at, "search", 0)) && SvTRUE(*v)) search = 1; 1 0 1
486 385 ▶ if ((v = hv_fetchs(at, "sigdefault", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = hv_fetchs(at, "sigdefault", 0))) { 371 14 385
487 371 ▶ 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))) 371 0 371
if (!(rc = sigset_from(aTHX_ *v, &set)) && !(rc = posix_spawnattr_setsigdefault(&sa, &set))) 371 0 371
488 371 flags |= POSIX_SPAWN_SETSIGDEF;
489 }
490 385 ▶ if (!rc && (v = hv_fetchs(at, "sigmask", 0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rc && (v = hv_fetchs(at, "sigmask", 0))) { 385 0 385
if (!rc && (v = hv_fetchs(at, "sigmask", 0))) { 371 14 385
491 371 ▶ 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))) 371 0 371
if (!(rc = sigset_from(aTHX_ *v, &set)) && !(rc = posix_spawnattr_setsigmask(&sa, &set))) 371 0 371
492 371 flags |= POSIX_SPAWN_SETSIGMASK;
493 }
494 385 ▶ if (!rc && (v = hv_fetchs(at, "pgroup", 0)) && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!rc && (v = hv_fetchs(at, "pgroup", 0)) && SvOK(*v)) { 385 0 385
if (!rc && (v = hv_fetchs(at, "pgroup", 0)) && SvOK(*v)) { 3 382 385
if (!rc && (v = hv_fetchs(at, "pgroup", 0)) && SvOK(*v)) { 3 0 3
495 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
496 3 flags |= POSIX_SPAWN_SETPGROUP;
497 }
498 #ifndef _WIN32
499 385 ▶ 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 */ 385 0 385
if (!rc && ar.on && !(flags & POSIX_SPAWN_SETSIGMASK)) { /* every signal is blocked around the call: the child gets the mask from before */ 3 382 385
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
500 sigset_t cur;
501 1 sigprocmask(SIG_BLOCK, NULL, &cur);
502 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
503 }
504 #endif
505 385 ▶ 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 385 385
if ((v = hv_fetchs(at, "resetids", 0)) && SvTRUE(*v)) flags |= POSIX_SPAWN_RESETIDS; 0 0 0
506 #ifdef POSIX_SPAWN_SETSID
507 385 ▶ 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 383 385
if ((v = hv_fetchs(at, "setsid", 0)) && SvTRUE(*v)) flags |= POSIX_SPAWN_SETSID; 2 0 2
508 #else
509 if (!rc && (v = hv_fetchs(at, "setsid", 0)) && SvTRUE(*v)) rc = missing(aTHX_ unsupported, "setsid");
510 #endif
511 #ifdef _WIN32
512 if (!rc && (v = hv_fetchs(at, "sched", 0)) && SvOK(*v)) rc = missing(aTHX_ unsupported, "sched");
513 #else
514 385 ▶ 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] */ 385 0 385
if (!rc && (v = hv_fetchs(at, "sched", 0)) && SvOK(*v)) { /* [POLICY, PRIORITY] */ 0 385 385
if (!rc && (v = hv_fetchs(at, "sched", 0)) && SvOK(*v)) { /* [POLICY, PRIORITY] */ 0 0 0
515 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
516 struct sched_param sp;
517 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
518 else {
519 0 sp.sched_priority = (int)SvIV(*av_fetch(s, 1, 0));
520 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
521 0 ▶ && !(rc = posix_spawnattr_setschedparam(&sa, &sp)))
Branch: 1 branch
BranchTrueFalseTotal
&& !(rc = posix_spawnattr_setschedparam(&sa, &sp))) 0 0 0
522 0 flags |= POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_SETSCHEDPARAM;
523 }
524 }
525 #endif
526 385 ▶ 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 */ 385 0 385
if (!rc && (v = hv_fetchs(at, "cgroup", 0)) && SvOK(*v)) { /* an fd of a cgroup v2 directory */ 0 385 385
if (!rc && (v = hv_fetchs(at, "cgroup", 0)) && SvOK(*v)) { /* an fd of a cgroup v2 directory */ 0 0 0
527 #ifdef HAVE_SETCGROUP_NP
528 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
529 0 flags |= POSIX_SPAWN_SETCGROUP;
530 #else
531 rc = missing(aTHX_ unsupported, "cgroup");
532 #endif
533 }
534 385 ▶ if (!rc && (v = hv_fetchs(at, "pidfd", 0)) && SvTRUE(*v))
Branch: 3 branches
BranchTrueFalseTotal
if (!rc && (v = hv_fetchs(at, "pidfd", 0)) && SvTRUE(*v)) 385 0 385
if (!rc && (v = hv_fetchs(at, "pidfd", 0)) && SvTRUE(*v)) 3 382 385
if (!rc && (v = hv_fetchs(at, "pidfd", 0)) && SvTRUE(*v)) 3 0 3
535 #ifdef HAVE_PIDFD_SPAWN
536 3 want_pidfd = 1;
537 #else
538 rc = missing(aTHX_ unsupported, "pidfd"); /* dropped: only the pid is returned */
539 #endif
540 #ifdef _WIN32
541 if ((v = hv_fetchs(at, "win_new_group", 0)) && SvOK(*v)) /* W7 (post-0.12): Proc::Spawner::spawn's choice of a
542 process group of the child's own, by its SIGINT (win_new_group) */
543 win_attr_setoptions(&sa, SvTRUE(*v) ? (sa.options | WIN_OPT_NEW_GROUP) : (sa.options & ~WIN_OPT_NEW_GROUP));
544 if (!rc && (v = hv_fetchs(at, "win_priority", 0)) && SvOK(*v)) { /* W4: a priority class, exact */
545 static const struct { const char *n; DWORD f; } pc[] = { { "idle", IDLE_PRIORITY_CLASS },
546 { "below_normal", BELOW_NORMAL_PRIORITY_CLASS }, { "normal", NORMAL_PRIORITY_CLASS },
547 { "above_normal", ABOVE_NORMAL_PRIORITY_CLASS }, { "high", HIGH_PRIORITY_CLASS }, { "realtime", REALTIME_PRIORITY_CLASS } };
548 const char *s = SvPV_nolen(*v); int k;
549 for (rc = EINVAL, k = 0; k < 6; k++) if (strEQ(s, pc[k].n)) { sa.priority_class = pc[k].f; rc = 0; }
550 }
551 if (!rc && (v = hv_fetchs(at, "win_job", 0)) && SvOK(*v)) { /* W4: limits of a job of the child's own, exact */
552 if (!SvROK(*v) || SvTYPE(SvRV(*v)) != SVt_PVHV) rc = EINVAL;
553 else {
554 HV *j = (HV *)SvRV(*v); HE *e; win_job_limits_t *l = &sa.job;
555 hv_iterinit(j);
556 while (!rc && (e = hv_iternext(j))) {
557 const char *k = HePV(e, PL_na); SV *x = HeVAL(e);
558 if (!SvOK(x)) continue;
559 if (strEQ(k, "ProcessMemoryLimit")) { l->ProcessMemoryLimit = (SIZE_T)SvNV(x); l->flags |= JOB_OBJECT_LIMIT_PROCESS_MEMORY; }
560 else if (strEQ(k, "JobMemoryLimit")) { l->JobMemoryLimit = (SIZE_T)SvNV(x); l->flags |= JOB_OBJECT_LIMIT_JOB_MEMORY; }
561 else if (strEQ(k, "PerProcessUserTimeLimit")) { l->PerProcessUserTimeLimit = (LONGLONG)SvNV(x); l->flags |= JOB_OBJECT_LIMIT_PROCESS_TIME; }
562 else if (strEQ(k, "PerJobUserTimeLimit")) { l->PerJobUserTimeLimit = (LONGLONG)SvNV(x); l->flags |= JOB_OBJECT_LIMIT_JOB_TIME; }
563 else if (strEQ(k, "ActiveProcessLimit")) { l->ActiveProcessLimit = (DWORD)SvUV(x); l->flags |= JOB_OBJECT_LIMIT_ACTIVE_PROCESS; }
564 else rc = EINVAL;
565 }
566 }
567 }
568 #endif
569 385 ▶ if (!rc) rc = posix_spawnattr_setflags(&sa, flags);
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) rc = posix_spawnattr_setflags(&sa, flags); 385 0 385
570 }
571 389 ▶ 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 */ 385 4 389
if (!rc && !(rc = around_begin(&ar))) { /* nothing is allocated from here to around_end */ 385 0 385
572 #ifdef HAVE_PIDFD_SPAWN
573 385 ▶ 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 382 385
574 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
575 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
576 }
577 else
578 #endif
579 382 ▶ 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 381 382
580 }
581 389 around_end(aTHX_ &ar);
582 389 posix_spawn_file_actions_destroy(&fa);
583 389 posix_spawnattr_destroy(&sa);
584 389 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 389 389
585 389 ▶ if (rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc) { 9 380 389
586 9 SETERRNO(rc, 0);
587 9 XSRETURN_EMPTY;
588 }
589 380 ▶ mXPUSHi(pid);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(pid); 0 380 380
590 380 ▶ if (want_pidfd) mXPUSHi(pidfd);
Branch: 2 branches
BranchTrueFalseTotal
if (want_pidfd) mXPUSHi(pidfd); 3 377 380
if (want_pidfd) mXPUSHi(pidfd); 0 3 3
591  
592 #else
593  
594 void
595 __posix_spawn(...)
596 PPCODE:
597 /* no posix_spawn in this libc (Makefile.PL): Proc::Spawner::spawn uses __spawn_nowait */
598 PERL_UNUSED_VAR(items);
599 SETERRNO(ENOSYS, 0);
600 XSRETURN_EMPTY;
601  
602 #endif
603  
604 #ifndef _WIN32
605  
606 void
607 __spawn_nowait(file_sv, argv, envp, attr = &PL_sv_undef, unsupported = &PL_sv_undef)
608 SV *file_sv
609 SV *argv
610 SV *envp
611 SV *attr
612 SV *unsupported
613 PREINIT:
614 char **av, **ev;
615 const char *file;
616 around_t ar;
617 0 HV *at = NULL;
618 0 int rc, p[2] = { -1, -1 }, e = 0;
619 0 ▶ pid_t pid = -1;
Branch: 1 branch
BranchTrueFalseTotal
pid_t pid = -1; 0 0 0
620 ssize_t got;
621 PPCODE:
622 /* The primitive without posix_spawn (decision M2), as Run 0.06's spawn_nowait: fork and exec with nothing in
623 between but the signal mask put back; the parent has done everything else around the call. Of the
624 attributes only rlimit, sigignore and nice (M1). The pid, or the empty list with $! (exec's errno too,
625 through a close-on-exec pipe). */
626 0 ENTER; SAVETMPS;
627 0 file = SvPV_nolen(file_sv);
628 0 av = strv(aTHX_ argv, "argv");
629 0 ▶ ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : environ;
Branch: 1 branch
BranchTrueFalseTotal
ev = SvOK(envp) ? strv(aTHX_ envp, "envp") : environ; 0 0 0
630 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
631 0 ▶ if (SvOK(attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(attr)) { 0 0 0
632 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
633 0 at = (HV *)SvRV(attr);
634 }
635 0 rc = around_parse(aTHX_ at, unsupported, &ar);
636 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
637 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
638 0 ▶ if (!rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { 0 0 0
639 0 pid = fork();
640 0 ▶ if (pid == 0) { /* async-signal-safe calls only */
Branch: 1 branch
BranchTrueFalseTotal
if (pid == 0) { /* async-signal-safe calls only */ 0 0 0
641 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
642 0 execve(file, av, ev);
643 0 e = errno; got = write(p[1], &e, sizeof e); (void)got;
644 0 _exit(127);
645 }
646 0 ▶ if (pid < 0) rc = errno;
Branch: 1 branch
BranchTrueFalseTotal
if (pid < 0) rc = errno; 0 0 0
647 }
648 0 around_end(aTHX_ &ar);
649 0 ▶ if (p[1] >= 0) close(p[1]);
Branch: 1 branch
BranchTrueFalseTotal
if (p[1] >= 0) close(p[1]); 0 0 0
650 0 ▶ if (!rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { 0 0 0
651 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
652 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
653 }
654 0 ▶ if (p[0] >= 0) close(p[0]);
Branch: 1 branch
BranchTrueFalseTotal
if (p[0] >= 0) close(p[0]); 0 0 0
655 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
656 0 ▶ if (rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc) { 0 0 0
657 0 SETERRNO(rc, 0);
658 0 XSRETURN_EMPTY;
659 }
660 0 ▶ mXPUSHi(pid);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(pid); 0 0 0
661  
662 void
663 __rlimits()
664 PPCODE:
665 /* The resources this system has, as NAME => NUMBER pairs (rlimit) */
666 # define RL(n) mXPUSHs(newSVpvs(#n)); mXPUSHi(RLIMIT_##n);
667 # ifdef RLIMIT_CPU
668 584 ▶ RL(CPU)
Branch: 2 branches
BranchTrueFalseTotal
RL(CPU) 0 584 584
RL(CPU) 0 584 584
669 # endif
670 # ifdef RLIMIT_FSIZE
671 584 ▶ RL(FSIZE)
Branch: 2 branches
BranchTrueFalseTotal
RL(FSIZE) 0 584 584
RL(FSIZE) 0 584 584
672 # endif
673 # ifdef RLIMIT_DATA
674 584 ▶ RL(DATA)
Branch: 2 branches
BranchTrueFalseTotal
RL(DATA) 0 584 584
RL(DATA) 0 584 584
675 # endif
676 # ifdef RLIMIT_STACK
677 584 ▶ RL(STACK)
Branch: 2 branches
BranchTrueFalseTotal
RL(STACK) 0 584 584
RL(STACK) 0 584 584
678 # endif
679 # ifdef RLIMIT_CORE
680 584 ▶ RL(CORE)
Branch: 2 branches
BranchTrueFalseTotal
RL(CORE) 0 584 584
RL(CORE) 0 584 584
681 # endif
682 # ifdef RLIMIT_RSS
683 584 ▶ RL(RSS)
Branch: 2 branches
BranchTrueFalseTotal
RL(RSS) 0 584 584
RL(RSS) 0 584 584
684 # endif
685 # ifdef RLIMIT_NPROC
686 584 ▶ RL(NPROC)
Branch: 2 branches
BranchTrueFalseTotal
RL(NPROC) 0 584 584
RL(NPROC) 0 584 584
687 # endif
688 # ifdef RLIMIT_NOFILE
689 584 ▶ RL(NOFILE)
Branch: 2 branches
BranchTrueFalseTotal
RL(NOFILE) 0 584 584
RL(NOFILE) 0 584 584
690 # endif
691 # ifdef RLIMIT_MEMLOCK
692 584 ▶ RL(MEMLOCK)
Branch: 2 branches
BranchTrueFalseTotal
RL(MEMLOCK) 0 584 584
RL(MEMLOCK) 0 584 584
693 # endif
694 # ifdef RLIMIT_AS
695 584 ▶ RL(AS)
Branch: 2 branches
BranchTrueFalseTotal
RL(AS) 0 584 584
RL(AS) 0 584 584
696 # endif
697 # ifdef RLIMIT_LOCKS
698 584 ▶ RL(LOCKS)
Branch: 2 branches
BranchTrueFalseTotal
RL(LOCKS) 0 584 584
RL(LOCKS) 0 584 584
699 # endif
700 # ifdef RLIMIT_SIGPENDING
701 584 ▶ RL(SIGPENDING)
Branch: 2 branches
BranchTrueFalseTotal
RL(SIGPENDING) 0 584 584
RL(SIGPENDING) 0 584 584
702 # endif
703 # ifdef RLIMIT_MSGQUEUE
704 584 ▶ RL(MSGQUEUE)
Branch: 2 branches
BranchTrueFalseTotal
RL(MSGQUEUE) 0 584 584
RL(MSGQUEUE) 0 584 584
705 # endif
706 # ifdef RLIMIT_NICE
707 584 ▶ RL(NICE)
Branch: 2 branches
BranchTrueFalseTotal
RL(NICE) 0 584 584
RL(NICE) 0 584 584
708 # endif
709 # ifdef RLIMIT_RTPRIO
710 584 ▶ RL(RTPRIO)
Branch: 2 branches
BranchTrueFalseTotal
RL(RTPRIO) 0 584 584
RL(RTPRIO) 0 584 584
711 # endif
712 # ifdef RLIMIT_RTTIME
713 584 ▶ RL(RTTIME)
Branch: 2 branches
BranchTrueFalseTotal
RL(RTTIME) 0 584 584
RL(RTTIME) 0 584 584
714 # endif
715 # undef RL
716  
717 void
718 __getrlimit(res)
719 int res
720 PPCODE:
721 /* (SOFT, HARD), 'unlimited' for RLIM_INFINITY; the empty list with $! */
722 struct rlimit r;
723 12 ▶ if (getrlimit(res, &r) != 0) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (getrlimit(res, &r) != 0) XSRETURN_EMPTY; 0 12 12
724 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
725 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
726  
727 #endif
728  
729 void
730 __native()
731 PPCODE:
732 /* The settings this build's primitive does natively (the rest Proc::Spawner emulates, or reports); "posix_spawn"
733 if there is one (else the primitive is __spawn_nowait). */
734 #ifndef _WIN32
735 572 ▶ 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 572 572
mXPUSHs(newSVpvs("rlimit")); mXPUSHs(newSVpvs("sigignore")); mXPUSHs(newSVpvs("nice")); /* around the call, in C (M1) */ 0 572 572
mXPUSHs(newSVpvs("rlimit")); mXPUSHs(newSVpvs("sigignore")); mXPUSHs(newSVpvs("nice")); /* around the call, in C (M1) */ 0 572 572
736 #endif
737 #ifdef HAVE_POSIX_SPAWN
738 572 ▶ mXPUSHs(newSVpvs("posix_spawn"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("posix_spawn")); 0 572 572
739 572 ▶ mXPUSHs(newSVpvs("dup2")); mXPUSHs(newSVpvs("close")); mXPUSHs(newSVpvs("open"));
Branch: 3 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("dup2")); mXPUSHs(newSVpvs("close")); mXPUSHs(newSVpvs("open")); 0 572 572
mXPUSHs(newSVpvs("dup2")); mXPUSHs(newSVpvs("close")); mXPUSHs(newSVpvs("open")); 0 572 572
mXPUSHs(newSVpvs("dup2")); mXPUSHs(newSVpvs("close")); mXPUSHs(newSVpvs("open")); 0 572 572
740 572 ▶ mXPUSHs(newSVpvs("sigmask")); mXPUSHs(newSVpvs("sigdefault")); mXPUSHs(newSVpvs("pgroup"));
Branch: 3 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("sigmask")); mXPUSHs(newSVpvs("sigdefault")); mXPUSHs(newSVpvs("pgroup")); 0 572 572
mXPUSHs(newSVpvs("sigmask")); mXPUSHs(newSVpvs("sigdefault")); mXPUSHs(newSVpvs("pgroup")); 0 572 572
mXPUSHs(newSVpvs("sigmask")); mXPUSHs(newSVpvs("sigdefault")); mXPUSHs(newSVpvs("pgroup")); 0 572 572
741 572 ▶ mXPUSHs(newSVpvs("resetids")); mXPUSHs(newSVpvs("search"));
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("resetids")); mXPUSHs(newSVpvs("search")); 0 572 572
mXPUSHs(newSVpvs("resetids")); mXPUSHs(newSVpvs("search")); 0 572 572
742 #ifndef _WIN32
743 572 ▶ 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 572 572
744 #endif
745 #ifdef HAVE_ADDCHDIR_NP
746 572 ▶ mXPUSHs(newSVpvs("chdir"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("chdir")); 0 572 572
747 #endif
748 #ifdef HAVE_ADDFCHDIR_NP
749 572 ▶ mXPUSHs(newSVpvs("fchdir"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("fchdir")); 0 572 572
750 #endif
751 #ifdef HAVE_ADDCLOSEFROM_NP
752 572 ▶ mXPUSHs(newSVpvs("closefrom"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("closefrom")); 0 572 572
753 #endif
754 #ifdef HAVE_ADDTCSETPGRP_NP
755 572 ▶ mXPUSHs(newSVpvs("tcsetpgrp"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("tcsetpgrp")); 0 572 572
756 #endif
757 #ifdef POSIX_SPAWN_SETSID
758 572 ▶ mXPUSHs(newSVpvs("setsid"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("setsid")); 0 572 572
759 #endif
760 #ifdef HAVE_SETCGROUP_NP
761 572 ▶ mXPUSHs(newSVpvs("cgroup"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("cgroup")); 0 572 572
762 #endif
763 #ifdef HAVE_PIDFD_SPAWN
764 572 ▶ mXPUSHs(newSVpvs("pidfd"));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvs("pidfd")); 0 572 572
765 #endif
766 #endif
767  
768 int
769 __set_cloexec(fd, on)
770 int fd
771 int on
772 CODE:
773 /* Set or clear FD_CLOEXEC; the old setting (1 or 0), or -1 if fd is not open. */
774 {
775 #ifdef _WIN32 /* close-on-exec is a handle that is not inherited */
776 intptr_t h = _get_osfhandle(fd); DWORD f;
777 if (h == -1 || h == -2 || !GetHandleInformation((HANDLE)h, &f)) RETVAL = -1;
778 else { RETVAL = (f & HANDLE_FLAG_INHERIT) ? 0 : 1; SetHandleInformation((HANDLE)h, HANDLE_FLAG_INHERIT, on ? 0 : HANDLE_FLAG_INHERIT); }
779 #else
780 440 int f = fcntl(fd, F_GETFD);
781 440 ▶ if (f < 0) RETVAL = -1;
Branch: 1 branch
BranchTrueFalseTotal
if (f < 0) RETVAL = -1; 0 440 440
782 440 ▶ 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)); } 440 0 440
783 #endif
784 }
785 OUTPUT:
786 RETVAL
787  
788 int
789 __dup_high(fd, min)
790 int fd
791 int min
792 CODE:
793 /* A close-on-exec copy of fd numbered min or above (F_DUPFD_CLOEXEC), or -1 with $!. */
794 #ifdef _WIN32
795 { /* no F_DUPFD: dup until the copy is high enough, then close the ones below; not inherited (post-0.08, for
796 the emulation around Perl's own spawn, M2) */
797 int low[256], n = 0, d = -1, e;
798 while ((d = _dup(fd)) >= 0 && d < min && n < 256) low[n++] = d;
799 e = errno;
800 if (d >= 0 && d < min) { _close(d); d = -1; e = EMFILE; }
801 while (n > 0) _close(low[--n]);
802 if (d >= 0) { intptr_t h = _get_osfhandle(d); if (h != -1) SetHandleInformation((HANDLE)h, HANDLE_FLAG_INHERIT, 0); }
803 else errno = e;
804 RETVAL = d;
805 }
806 #else
807 0 RETVAL = fcntl(fd, F_DUPFD_CLOEXEC, min);
808 #endif
809 OUTPUT:
810 RETVAL
811  
812 int
813 __fchdir(fd)
814 int fd
815 CODE:
816 #ifdef _WIN32
817 PERL_UNUSED_VAR(fd); errno = ENOSYS; RETVAL = 0;
818 #else
819 0 ▶ RETVAL = fchdir(fd) == 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = fchdir(fd) == 0; 0 0 0
820 #endif
821 OUTPUT:
822 RETVAL
823  
824 int
825 __pidfd_open(pid)
826 int pid
827 CODE:
828 /* A pidfd for a child already started (the emulation of pidfd => 1), or -1 with $! (ENOSYS without it). */
829 #if defined(SYS_pidfd_open) && !defined(_WIN32)
830 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
831 #else
832 PERL_UNUSED_VAR(pid); errno = ENOSYS; RETVAL = -1;
833 #endif
834 OUTPUT:
835 RETVAL
836  
837 int
838 __fd_limit()
839 CODE:
840 /* One more than the highest fd number this process may have (the soft RLIMIT_NOFILE, at most 65536). */
841 #ifdef _WIN32
842 RETVAL = _getmaxstdio();
843 #else
844 {
845 struct rlimit r;
846 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
847 }
848 #endif
849 OUTPUT:
850 RETVAL
851  
852 int
853 __fd_cloexec(fd)
854 int fd
855 CODE:
856 /* Is fd open, and close-on-exec? 1: close-on-exec, 0: inherited by a child, -1: not open. Portable
857 (fcntl F_GETFD); Perl itself could ask only through a handle, which would close the fd later. */
858 {
859 #ifdef _WIN32
860 intptr_t h = _get_osfhandle(fd); DWORD f;
861 RETVAL = h == -1 || h == -2 || !GetHandleInformation((HANDLE)h, &f) ? -1 : (f & HANDLE_FLAG_INHERIT) ? 0 : 1;
862 #else
863 4 int f = fcntl(fd, F_GETFD);
864 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
865 #endif
866 }
867 OUTPUT:
868 RETVAL
869  
870 int
871 fpe_at_exec()
872 CODE:
873 /* What Perl's own exec gives SIGFPE: Perl may ignore SIGFPE for itself at startup and then restores
874 the saved startup disposition around exec (perl.h: PERL_FPU_INIT, PERL_FPU_PRE_EXEC).
875 1: ignored, 0: default, -1: this Perl does not touch SIGFPE (it reads as any other signal). */
876 #if !defined(HAS_FPSETMASK) && defined(SIGFPE) && defined(SIG_IGN)
877 566 ▶ 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 566 566
878 #else
879 RETVAL = -1;
880 #endif
881 OUTPUT:
882 RETVAL
883  
884 #ifdef _WIN32
885  
886 IV
887 __win_spawn_handle(pid)
888 IV pid
889 CODE:
890 /* The process handle of a child started here (kept until __win_spawn_forget), or 0: for its exit code. */
891 RETVAL = PTR2IV(win_spawn_handle((pid_t)pid));
892 OUTPUT:
893 RETVAL
894  
895 void
896 __win_spawn_forget(pid)
897 IV pid
898 CODE:
899 win_spawn_forget((pid_t)pid);
900  
901 int
902 __wl_io(id, kind, fd, data)
903 IV id
904 char kind
905 int fd
906 SV *data
907 CODE:
908 /* W5 (win_loop.h): start a read ('r', at most SvIV(data) bytes) or a write ('w', the bytes of data) on fd; its
909 event comes from __wl_wait. 1, or 0 with $!. */
910 {
911 STRLEN len = 0; const char *p = NULL;
912 if (kind == 'w') p = SvPVbyte(data, len); else len = (STRLEN)SvIV(data);
913 RETVAL = wl_io(id, kind, fd, p, (DWORD)len);
914 }
915 OUTPUT:
916 RETVAL
917  
918 int
919 __wl_child(id, pid)
920 IV id
921 IV pid
922 CODE:
923 /* W5: watch the process pid for its end (its status then comes from waitpid). 1, or 0 with $!. */
924 RETVAL = wl_child(id, (DWORD)pid);
925 OUTPUT:
926 RETVAL
927  
928 void
929 __wl_cancel(id)
930 IV id
931 CODE:
932 wl_cancel(id);
933  
934 void
935 __wl_wait(ms)
936 int ms
937 PPCODE:
938 /* W5: the events of one wait of at most ms milliseconds (-1: no limit): [ID, KIND, BYTES, ERRNO] each; for a read,
939 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,
940 which only win32_async_check reads), the wait is cut into slices of 50 ms, each followed by that check. After a
941 console control event, it returns once Perl's own handler has marked the signal (200 ms at most): Perl then
942 dispatches it. */
943 {
944 OVERLAPPED_ENTRY e[64]; ULONG n = 0, i; DWORD t = ms < 0 ? INFINITE : (DWORD)ms; BOOL got;
945 int timer = w32_timerid != 0;
946 if (timer && (t == INFINITE || t > 50)) t = 50;
947 got = wl_init() && GetQueuedCompletionStatusEx(wl_port, e, 64, &n, t, FALSE);
948 if (InterlockedExchange(&wl_woken, 0)) { int k; for (k = 0; k < 200 && !PL_sig_pending; k++) Sleep(1); }
949 if (timer) win32_async_check(aTHX);
950 if (got) {
951 for (i = 0; i < n; i++) {
952 wl_op *op = (wl_op *)e[i].lpOverlapped; AV *av; int err;
953 if (e[i].lpCompletionKey == WL_KEY_WAKE) continue;
954 if (!op || !wl_complete(op, e[i].dwNumberOfBytesTransferred, e[i].lpCompletionKey)) continue;
955 err = op->err ? wl_errno(op->err) : 0;
956 if (op->kind == 'r' && (op->err == ERROR_BROKEN_PIPE || op->err == ERROR_HANDLE_EOF
957 || op->err == ERROR_PIPE_NOT_CONNECTED || (!op->err && op->done == 0))) err = 0;
958 av = newAV();
959 av_push(av, newSViv(op->id));
960 av_push(av, newSVpvn(&op->kind, 1));
961 av_push(av, op->kind == 'r' && op->done && !op->err ? newSVpvn(op->buf, op->done) : newSV(0));
962 av_push(av, newSViv(err));
963 mXPUSHs(newRV_noinc((SV *)av));
964 wl_free(op);
965 }
966 }
967 }
968  
969 IV
970 __win_ppid()
971 CODE:
972 /* The parent's process ID (Perl's getppid is unimplemented on Windows): NtQueryInformationProcess's
973 ProcessBasicInformation, InheritedFromUniqueProcessId; 0 if unknown. */
974 {
975 typedef LONG (NTAPI *nqip_t)(HANDLE, int, PVOID, ULONG, PULONG);
976 struct { LONG_PTR status; PVOID peb; ULONG_PTR affinity; LONG_PTR priority; ULONG_PTR pid, ppid; } pbi;
977 nqip_t f = (nqip_t)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
978 RETVAL = f && f(GetCurrentProcess(), 0, &pbi, sizeof pbi, NULL) == 0 ? (IV)pbi.ppid : 0;
979 }
980 OUTPUT:
981 RETVAL
982  
983 int
984 __wl_ctrl(on)
985 int on
986 CODE:
987 /* W5: install (1) or remove (0) the console control handler that wakes the loop; 1 if done */
988 RETVAL = wl_ctrl_watch(on);
989 OUTPUT:
990 RETVAL
991  
992 int
993 __wl_is_async(fd)
994 int fd
995 CODE:
996 /* 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 */
997 { intptr_t h = _get_osfhandle(fd); RETVAL = h == -1 || h == -2 ? -1 : wl_is_async((HANDLE)h); }
998 OUTPUT:
999 RETVAL
1000  
1001 void
1002 __wl_pipe(async_r, async_w)
1003 int async_r
1004 int async_w
1005 PPCODE:
1006 /* W5: a pipe whose ends are opened for overlapped I/O where asked: (READ_FD, WRITE_FD), or nothing with $!. */
1007 {
1008 int r, w;
1009 if (wl_pipe(async_r, async_w, &r, &w) == 0) { mXPUSHi(r); mXPUSHi(w); }
1010 }
1011  
1012 const char *
1013 __win_priority_class()
1014 CODE:
1015 /* this process's priority class, by the names win_priority takes (W4: nice is mapped from it) */
1016 switch (GetPriorityClass(GetCurrentProcess())) {
1017 case IDLE_PRIORITY_CLASS: RETVAL = "idle"; break;
1018 case BELOW_NORMAL_PRIORITY_CLASS: RETVAL = "below_normal"; break;
1019 case ABOVE_NORMAL_PRIORITY_CLASS: RETVAL = "above_normal"; break;
1020 case HIGH_PRIORITY_CLASS: RETVAL = "high"; break;
1021 case REALTIME_PRIORITY_CLASS: RETVAL = "realtime"; break;
1022 default: RETVAL = "normal";
1023 }
1024 OUTPUT:
1025 RETVAL
1026  
1027 SV *
1028 __win_same_file(a, b)
1029 SV *a
1030 SV *b
1031 CODE:
1032 /* files_eq on Windows (post-0.12, the user, 2026-11-16): whether two names are one file, as Microsoft documents it:
1033 each opened (no data access, every sharing, FILE_FLAG_BACKUP_SEMANTICS for directories), then the volume's serial
1034 number and the 128-bit file ID compared (GetFileInformationByHandleEx, FileIdInfo: Windows 8 / Server 2012 and
1035 later, and what ReFS needs, whose 64-bit IDs are not unique); where that class is not there (older Windows, a
1036 file system without it), the serial number and the 64-bit file index of GetFileInformationByHandle (Windows XP
1037 and later). GetFileInformationByHandleEx is looked up at run time (absent before Vista: linking it would keep
1038 the DLL from loading there). An ID or index of all zeros identifies nothing: then that way cannot tell. 1 or
1039 0; undef if either name cannot be opened or no way can tell (the caller falls back; never "different" for "not
1040 supported"). The names in the ANSI code page, as Perl's own file functions take them. */
1041 {
1042 const char *n[2]; HANDLE h[2]; int i, r = -1;
1043 n[0] = SvPV_nolen(a); n[1] = SvPV_nolen(b);
1044 for (i = 0; i < 2; i++) {
1045 wchar_t w[32768];
1046 h[i] = INVALID_HANDLE_VALUE;
1047 if (!MultiByteToWideChar(CP_ACP, 0, n[i], -1, w, 32768)) continue;
1048 h[i] = CreateFileW(w, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
1049 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1050 }
1051 if (h[0] != INVALID_HANDLE_VALUE && h[1] != INVALID_HANDLE_VALUE) {
1052 typedef BOOL (WINAPI *info_ex_t)(HANDLE, int, LPVOID, DWORD);
1053 static info_ex_t info_ex; static int looked;
1054 struct { ULONGLONG vol; BYTE id[16]; } x[2]; /* FILE_ID_INFO, class FileIdInfo (18) */
1055 static const BYTE zero[16];
1056 BY_HANDLE_FILE_INFORMATION y[2];
1057 if (!looked) {
1058 HMODULE k = GetModuleHandleW(L"kernel32.dll");
1059 info_ex = k ? (info_ex_t)(void (*)(void))GetProcAddress(k, "GetFileInformationByHandleEx") : NULL;
1060 looked = 1;
1061 }
1062 if (info_ex && info_ex(h[0], 18, &x[0], sizeof x[0]) && info_ex(h[1], 18, &x[1], sizeof x[1])
1063 && memcmp(x[0].id, zero, 16) && memcmp(x[1].id, zero, 16))
1064 r = x[0].vol == x[1].vol && memcmp(x[0].id, x[1].id, sizeof x[0].id) == 0;
1065 else if (GetFileInformationByHandle(h[0], &y[0]) && GetFileInformationByHandle(h[1], &y[1])
1066 && (y[0].nFileIndexHigh || y[0].nFileIndexLow) && (y[1].nFileIndexHigh || y[1].nFileIndexLow))
1067 r = y[0].dwVolumeSerialNumber == y[1].dwVolumeSerialNumber
1068 && y[0].nFileIndexHigh == y[1].nFileIndexHigh && y[0].nFileIndexLow == y[1].nFileIndexLow;
1069 }
1070 for (i = 0; i < 2; i++) if (h[i] != INVALID_HANDLE_VALUE) CloseHandle(h[i]);
1071 RETVAL = r < 0 ? &PL_sv_undef : newSViv(r);
1072 }
1073 OUTPUT:
1074 RETVAL
1075  
1076 void
1077 __win_env_names()
1078 PPCODE:
1079 /* X65 (post-0.12, the user, 2026-11-15, A22 (a)): the names of this process's environment as its block spells them
1080 (Perl's %ENV gives them in upper case on Windows); names that start with '=' (the drives' directories) and names
1081 that are not ASCII are left out (their %ENV keys stay as they are) */
1082 {
1083 wchar_t *b = GetEnvironmentStringsW(), *p;
1084 if (b) {
1085 for (p = b; *p; p += wcslen(p) + 1) {
1086 wchar_t *eq = wcschr(p + 1, L'=');
1087 size_t n = eq ? (size_t)(eq - p) : 0, i;
1088 char name[256];
1089 if (!n || *p == L'=' || n >= sizeof name) continue;
1090 for (i = 0; i < n && p[i] < 128; i++) name[i] = (char)p[i];
1091 if (i < n) continue;
1092 mXPUSHp(name, n);
1093 }
1094 FreeEnvironmentStringsW(b);
1095 }
1096 }
1097  
1098 #else
1099  
1100 int
1101 chld_to_fd(fd)
1102 int fd
1103 CODE:
1104 /* Install the C handler (SA_RESTART); keep the previous action for chld_restore. 1 or 0 with $!. */
1105 struct sigaction sa;
1106 623 memset(&sa, 0, sizeof sa);
1107 623 sa.sa_handler = chld_handler;
1108 623 sigemptyset(&sa.sa_mask);
1109 623 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
1110 623 chld_fd = fd;
1111 623 ▶ RETVAL = sigaction(SIGCHLD, &sa, chld_installed ? NULL : &chld_old) == 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = sigaction(SIGCHLD, &sa, chld_installed ? NULL : &chld_old) == 0; 623 0 623
1112 623 ▶ if (RETVAL) chld_installed = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL) chld_installed = 1; 623 0 623
1113 OUTPUT:
1114 RETVAL
1115  
1116 int
1117 chld_restore()
1118 CODE:
1119 623 RETVAL = 1;
1120 623 ▶ 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; } 623 0 623
1121 OUTPUT:
1122 RETVAL
1123  
1124 #endif
« t/try.pl ↑ Summary