FastSpawn.xs

filter: Statementstmt 80.0% 20 / 25 Branchbran 50.0% 9 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.4% 29 / 43 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Proc/FastSpawn.pm »
1 /* GetProcessId is XP and up, which means in all supported versions */
2 /* but older SDK's might need this */
3 #define _WIN32_WINNT NTDDI_WINXP
4  
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8  
9 #include <stdio.h>
10  
11 #ifdef WIN32
12  
13 /* perl probably did this already */
14 #include <windows.h>
15  
16 #else
17  
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21  
22 /* openbsd seems to have a buggy vfork (what would you expect), */
23 /* while others might implement vfork as fork in older versions, which is fine */
24 #if __linux || __FreeBSD__ || __NetBSD__ || __sun
25 #define USE_VFORK 1
26 #endif
27  
28 #if !USE_VFORK
29 #if _POSIX_SPAWN >= 200809L
30 #define USE_SPAWN 1
31 #include <spawn.h>
32 #else
33 #define vfork() fork()
34 #endif
35 #endif
36  
37 #endif
38  
39 static char *const *
40 2 array_to_cvec (SV *sv)
41 {
42 AV *av;
43 int n, i;
44 char **cvec;
45  
46 2 ▶ if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 2 0 2
if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 0 2 2
47 0 croak ("expected a reference to an array of argument/environment strings");
48  
49 2 av = (AV *)SvRV (sv);
50 2 n = av_len (av) + 1;
51 2 cvec = (char **)SvPVX (sv_2mortal (NEWSV (0, sizeof (char *) * (n + 1))));
52  
53 10 ▶ for (i = 0; i < n; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) 8 2 10
54 16 cvec [i] = SvPVbyte_nolen (*av_fetch (av, i, 1));
55  
56 2 cvec [n] = 0;
57  
58 2 return cvec;
59 }
60  
61 MODULE = Proc::FastSpawn PACKAGE = Proc::FastSpawn
62  
63 PROTOTYPES: ENABLE
64  
65 BOOT:
66 #ifndef WIN32
67 3 cv_undef (get_cv ("Proc::FastSpawn::_quote", 0));
68 #endif
69  
70 long
71 spawn (const char *path, SV *argv, SV *envp = &PL_sv_undef)
72 ALIAS:
73 spawnp = 1
74 INIT:
75 {
76 #ifdef WIN32
77 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS)
78 {
79 errno = EAGAIN;
80 XSRETURN_UNDEF;
81 }
82  
83 argv = sv_2mortal (newSVsv (argv));
84 PUSHMARK (SP);
85 XPUSHs (argv);
86 PUTBACK;
87 call_pv ("Proc::FastSpawn::_quote", G_VOID | G_DISCARD);
88 SPAGAIN;
89 #endif
90 }
91 CODE:
92 {
93 extern char **environ;
94 2 char *const *cargv = array_to_cvec (argv);
95 2 ▶ char *const *cenvp = SvOK (envp) ? array_to_cvec (envp) : environ;
Branch: 1 branch
BranchTrueFalseTotal
char *const *cenvp = SvOK (envp) ? array_to_cvec (envp) : environ; 0 2 2
96 intptr_t pid;
97  
98 2 fflush (0);
99 #ifdef WIN32
100 pid = (ix ? _spawnvpe : _spawnve) (_P_NOWAIT, path, cargv, cenvp);
101  
102 if (pid == -1)
103 XSRETURN_UNDEF;
104  
105 /* do it like perl, dadadoop dadadoop */
106 w32_child_handles [w32_num_children] = (HANDLE)pid;
107 pid = GetProcessId ((HANDLE)pid); /* get the real pid, unfortunately, requires wxp or newer */
108 w32_child_pids [w32_num_children] = pid;
109 ++w32_num_children;
110 #elif USE_SPAWN
111 {
112 pid_t xpid;
113  
114 errno = (ix ? posix_spawnp : posix_spawn) (&xpid, path, 0, 0, cargv, cenvp);
115  
116 if (errno)
117 XSRETURN_UNDEF;
118  
119 pid = xpid;
120 }
121 #else
122 2 ▶ pid = (ix ? fork : vfork) ();
Branch: 1 branch
BranchTrueFalseTotal
pid = (ix ? fork : vfork) (); 0 2 2
123  
124 2 ▶ if (pid < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pid < 0) 0 2 2
125 0 XSRETURN_UNDEF;
126  
127 2 ▶ if (pid == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pid == 0) 2 0 2
128 {
129 2 ▶ if (ix)
Branch: 1 branch
BranchTrueFalseTotal
if (ix) 0 2 2
130 {
131 0 environ = (char **)cenvp;
132 0 execvp (path, cargv);
133 }
134 else
135 2 execve (path, cargv, cenvp);
136  
137 2 _exit (127);
138 }
139 #endif
140  
141 0 ▶ RETVAL = pid;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = pid; 0 0 0
142 }
143 OUTPUT: RETVAL
144  
145 void
146 fd_inherit (int fd, int on = 1)
147 CODE:
148 #ifdef WIN32
149 SetHandleInformation ((HANDLE)_get_osfhandle (fd), HANDLE_FLAG_INHERIT, on ? HANDLE_FLAG_INHERIT : 0);
150 #else
151 2 fcntl (fd, F_SETFD, on ? 0 : FD_CLOEXEC);
152 #endif
153  
↑ Summary blib/lib/Proc/FastSpawn.pm »