ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Proc-FastSpawn/FastSpawn.xs
(Generate patch)

Comparing Proc-FastSpawn/FastSpawn.xs (file contents):
Revision 1.1 by root, Fri Mar 29 22:15:52 2013 UTC vs.
Revision 1.5 by root, Sun Apr 28 00:49:43 2013 UTC

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
1#include "EXTERN.h" 5#include "EXTERN.h"
2#include "perl.h" 6#include "perl.h"
3#include "XSUB.h" 7#include "XSUB.h"
4 8
5#include <stdio.h> 9#include <stdio.h>
6 10
7#ifdef WIN32 11#ifdef WIN32
8 12
13 /* perl probably did this already */
9 #include <windows.h> 14 #include <windows.h>
10 15
11#else 16#else
12 17
13 #include <errno.h> 18 #include <errno.h>
62 cv_undef (get_cv ("Proc::FastSpawn::_quote", 0)); 67 cv_undef (get_cv ("Proc::FastSpawn::_quote", 0));
63#endif 68#endif
64 69
65long 70long
66spawn (const char *path, SV *argv, SV *envp = &PL_sv_undef) 71spawn (const char *path, SV *argv, SV *envp = &PL_sv_undef)
72 ALIAS:
73 spawnp = 1
67 INIT: 74 INIT:
68{ 75{
69#ifdef WIN32 76#ifdef WIN32
70 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) 77 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS)
71 { 78 {
88 char *const *cenvp = SvOK (envp) ? array_to_cvec (envp) : environ; 95 char *const *cenvp = SvOK (envp) ? array_to_cvec (envp) : environ;
89 intptr_t pid; 96 intptr_t pid;
90 97
91 fflush (0); 98 fflush (0);
92#ifdef WIN32 99#ifdef WIN32
93 pid = _spawnve (_P_NOWAIT, path, cargv, cenvp); 100 pid = (ix ? _spawnvpe : _spawnve) (_P_NOWAIT, path, cargv, cenvp);
94 101
95 if (pid == -1) 102 if (pid == -1)
96 XSRETURN_UNDEF; 103 XSRETURN_UNDEF;
97 104
98 /* do it like perl, dadadoop dadadoop */ 105 /* do it like perl, dadadoop dadadoop */
99 w32_child_handles [w32_num_children] = (HANDLE)pid; 106 w32_child_handles [w32_num_children] = (HANDLE)pid;
100 pid = GetProcessId ((HANDLE)pid); /* get the real pid */ 107 pid = GetProcessId ((HANDLE)pid); /* get the real pid, unfortunately, requires wxp or newer */
101 w32_child_pids [w32_num_children] = pid; 108 w32_child_pids [w32_num_children] = pid;
102 ++w32_num_children; 109 ++w32_num_children;
103#elif USE_SPAWN 110#elif USE_SPAWN
104 { 111 {
105 pid_t xpid; 112 pid_t xpid;
106 113
107 errno = posix_spawn (&xpid, path, 0, 0, cargv, cenvp); 114 errno = (ix ? posix_spawnp : posix_spawn) (&xpid, path, 0, 0, cargv, cenvp);
108 115
109 if (errno) 116 if (errno)
110 XSRETURN_UNDEF; 117 XSRETURN_UNDEF;
111 118
112 pid = xpid; 119 pid = xpid;
113 } 120 }
114#else 121#else
115 pid = vfork (); 122 {
123 char **old_environ = environ;
124 environ = (char **)cenvp;
116 125
117 if (pid < 0) 126 pid = vfork ();
118 XSRETURN_UNDEF;
119 127
128 if (pid)
129 environ = old_environ;
130
131 if (pid < 0)
132 XSRETURN_UNDEF;
133
120 if (pid == 0) 134 if (pid == 0)
121 { 135 {
122 execve (path, cargv, cenvp); 136 (ix ? execvp : execv) (path, cargv);
123 _exit (127); 137 _exit (127);
124 } 138 }
139 }
125#endif 140#endif
126 141
127 RETVAL = pid; 142 RETVAL = pid;
128} 143}
129 OUTPUT: RETVAL 144 OUTPUT: RETVAL
134#ifdef WIN32 149#ifdef WIN32
135 SetHandleInformation ((HANDLE)_get_osfhandle (fd), HANDLE_FLAG_INHERIT, on ? HANDLE_FLAG_INHERIT : 0); 150 SetHandleInformation ((HANDLE)_get_osfhandle (fd), HANDLE_FLAG_INHERIT, on ? HANDLE_FLAG_INHERIT : 0);
136#else 151#else
137 fcntl (fd, F_SETFD, on ? 0 : FD_CLOEXEC); 152 fcntl (fd, F_SETFD, on ? 0 : FD_CLOEXEC);
138#endif 153#endif
154

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines