25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#ifndef _WIN32
# include <unistd.h>
# include <signal.h>
# include <sys/wait.h>
#endif
#ifdef __MACH__
# include <crt_externs.h>
#endif
#import "OFProcess.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFDataArray.h"
|
|
|
|
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#ifndef _WIN32
# include <unistd.h>
# include <signal.h>
# include <sys/wait.h>
#endif
#ifdef HAVE_SPAWN_H
# include <spawn.h>
#endif
#import "OFProcess.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFDataArray.h"
|
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#ifndef __MACH__
extern char **environ;
#endif
@interface OFProcess (OF_PRIVATE_CATEGORY)
#ifndef _WIN32
- (void)OF_getArgC: (int*)argc
andArgV: (char***)argv
forProgramName: (OFString*)programName
andArguments: (OFArray*)arguments;
- (char**)OF_environmentForDictionary: (OFDictionary*)dictionary;
#else
- (of_char16_t*)OF_environmentForDictionary: (OFDictionary*)dictionary;
#endif
@end
|
|
<
|
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#ifndef __MACH__
extern char **environ;
#endif
@interface OFProcess (OF_PRIVATE_CATEGORY)
#ifndef _WIN32
- (void)OF_getArgV: (char***)argv
forProgramName: (OFString*)programName
andArguments: (OFArray*)arguments;
- (char**)OF_environmentForDictionary: (OFDictionary*)dictionary;
#else
- (of_char16_t*)OF_environmentForDictionary: (OFDictionary*)dictionary;
#endif
@end
|
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
{
self = [super init];
@try {
#ifndef _WIN32
void *pool = objc_autoreleasePoolPush();
const char *path;
int argc;
char **argv, **env;
if (pipe(_readPipe) != 0 || pipe(_writePipe) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
path = [program cStringWithEncoding:
[OFSystemInfo native8BitEncoding]];
[self OF_getArgC: &argc
andArgV: &argv
forProgramName: programName
andArguments: arguments];
env = [self OF_environmentForDictionary: environment];
if ((_pid = fork()) == 0) {
#ifdef __MACH__
*_NSGetEnviron() = env;
#else
environ = env;
#endif
close(_readPipe[0]);
close(_writePipe[1]);
dup2(_writePipe[0], 0);
dup2(_readPipe[1], 1);
execvp(path, argv);
_exit(EXIT_FAILURE);
}
close(_readPipe[1]);
close(_writePipe[0]);
[self freeMemory: argv];
if (_pid == -1)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
objc_autoreleasePoolPop(pool);
#else
SECURITY_ATTRIBUTES sa;
PROCESS_INFORMATION pi;
STARTUPINFOW si;
void *pool;
|
<
|
|
<
>
>
>
|
>
>
>
>
>
>
|
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
<
|
|
|
|
|
|
|
>
>
>
>
>
|
|
|
|
<
<
<
|
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
{
self = [super init];
@try {
#ifndef _WIN32
void *pool = objc_autoreleasePoolPush();
const char *path;
char **argv;
if (pipe(_readPipe) != 0 || pipe(_writePipe) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
path = [program cStringWithEncoding:
[OFSystemInfo native8BitEncoding]];
[self OF_getArgV: &argv
forProgramName: programName
andArguments: arguments];
@try {
char **env = [self
OF_environmentForDictionary: environment];
# ifdef HAVE_POSIX_SPAWNP
posix_spawn_file_actions_t actions;
posix_spawnattr_t attr;
if (posix_spawn_file_actions_init(&actions) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
if (posix_spawnattr_init(&attr) != 0) {
posix_spawn_file_actions_destroy(&actions);
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
}
@try {
if (posix_spawn_file_actions_addclose(&actions,
_readPipe[0]) != 0 ||
posix_spawn_file_actions_addclose(&actions,
_writePipe[1]) != 0 ||
posix_spawn_file_actions_adddup2(&actions,
_writePipe[0], 0) != 0 ||
posix_spawn_file_actions_adddup2(&actions,
_readPipe[1], 1) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
# ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
if (posix_spawnattr_setflags(&attr,
POSIX_SPAWN_CLOEXEC_DEFAULT) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
# endif
if (posix_spawnp(&_pid, path, &actions, &attr,
argv, env) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
} @finally {
posix_spawn_file_actions_destroy(&actions);
posix_spawnattr_destroy(&attr);
}
# else
if ((_pid = fork()) == 0) {
environ = env;
close(_readPipe[0]);
close(_writePipe[1]);
dup2(_writePipe[0], 0);
dup2(_readPipe[1], 1);
execvp(path, argv);
_exit(EXIT_FAILURE);
}
if (_pid == -1)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
# endif
} @finally {
close(_readPipe[1]);
close(_writePipe[0]);
[self freeMemory: argv];
}
objc_autoreleasePoolPop(pool);
#else
SECURITY_ATTRIBUTES sa;
PROCESS_INFORMATION pi;
STARTUPINFOW si;
void *pool;
|
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
{
[self close];
[super dealloc];
}
#ifndef _WIN32
- (void)OF_getArgC: (int*)argc
andArgV: (char***)argv
forProgramName: (OFString*)programName
andArguments: (OFArray*)arguments
{
OFString *const *objects = [arguments objects];
size_t i, count = [arguments count];
of_string_encoding_t encoding;
|
|
<
|
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
{
[self close];
[super dealloc];
}
#ifndef _WIN32
- (void)OF_getArgV: (char***)argv
forProgramName: (OFString*)programName
andArguments: (OFArray*)arguments
{
OFString *const *objects = [arguments objects];
size_t i, count = [arguments count];
of_string_encoding_t encoding;
|