-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTaskit.m
611 lines (465 loc) · 17.6 KB
/
Taskit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Taskit
// Written by Alex Gordon on 09/09/2011.
// Licensed under the WTFPL: http://sam.zoy.org/wtfpl/
#import "Taskit.h"
@interface Taskit ()
@end
@implementation Taskit
@synthesize launchPath;
@synthesize arguments;
@synthesize environment;
@synthesize workingDirectory;
@synthesize input;
@synthesize inputString;
@synthesize inputPath;
//TODO: @synthesize usesAuthorization;
#ifdef TASKIT_BACKGROUNDING
@synthesize receivedOutputData;
@synthesize receivedOutputString;
@synthesize receivedErrorData;
@synthesize receivedErrorString;
#endif
//TODO: @synthesize processExited;
@synthesize timeoutIfNothing;
// The amount of time to wait for stdout if stderr HAS been read
@synthesize timeoutSinceOutput;
// The amount of time to wait for stderr if stdout HAS been read
@synthesize timeoutSinceError;
@synthesize priority;
+ (id)task {
return [[[self class] alloc] init];
}
- (id)init {
self = [super init];
if (!self)
return nil;
arguments = [[NSMutableArray alloc] init];
environment = [[NSMutableDictionary alloc] init];
self.workingDirectory = [[NSFileManager defaultManager] currentDirectoryPath];
inPipe = [[NSPipe alloc] init];
outPipe = [[NSPipe alloc] init];
errPipe = [[NSPipe alloc] init];
priority = NSIntegerMax;
shouldSetUpFileHandlesAutomatically = YES;
return self;
}
static const char* CHAllocateCopyString(NSString *str) {
const char* originalString = [str fileSystemRepresentation];
if (!originalString)
return NULL;
size_t copysize = (strlen(originalString) + 1) * sizeof(char);
char* newString = (char*)calloc(copysize, 1);
if (!newString)
return NULL;
memcpy(newString, originalString, copysize);
return newString;
}
- (void)populateWithCurrentEnvironment {
[environment addEntriesFromDictionary:[[NSProcessInfo processInfo] environment]];
}
- (BOOL)launch {
if (![launchPath length])
return NO;
self.launchPath = [launchPath stringByStandardizingPath];
if (![[NSFileManager defaultManager] isExecutableFileAtPath:launchPath])
return NO;
[arguments insertObject:launchPath atIndex:0];
if ([arguments count] + [environment count] + 2 > ARG_MAX)
return NO;
// Set up
// Set up launch path, arguments, environment and working directory
const char* executablePath = CHAllocateCopyString(launchPath);
if (!executablePath)
return NO;
const char* workingDirectoryPath = CHAllocateCopyString(workingDirectory);
if (!workingDirectoryPath)
return NO;
const char** argumentsArray = (const char**)calloc([arguments count] + 1, sizeof(char*));
NSInteger argCounter = 0;
for (NSString *argument in arguments) {
argumentsArray[argCounter] = CHAllocateCopyString(argument);
if (argumentsArray[argCounter])
argCounter++;
}
const char **environmentArray = (const char**)calloc([environment count] + 1, sizeof(char*));
NSInteger envCounter = 0;
for (NSString *environmentKey in environment) {
NSString *environmentValue = [environment valueForKey:environmentKey];
if (![environmentKey length] || !environmentValue)
continue;
NSString *environmentPair = [NSString stringWithFormat:@"%@=%@", environmentKey, environmentValue];
environmentArray[envCounter] = CHAllocateCopyString(environmentPair);
envCounter++;
}
// Backgrounding
#ifdef TASKIT_BACKGROUNDING
if (receivedOutputData || receivedOutputString) {
CFRetain(self);
hasRetainedForOutput = YES;
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(asyncFileHandleReadCompletion:) name:NSFileHandleReadCompletionNotification object:[outPipe fileHandleForReading]];
// [[outPipe fileHandleForReading] readInBackgroundAndNotifyForModes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, @"taskitwait", nil]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(asyncFileHandleReadCompletion:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[outPipe fileHandleForReading]];
[[outPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotifyForModes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, @"taskitwait", nil]];
}
if (receivedErrorData || receivedErrorString) {
CFRetain(self);
hasRetainedForError = YES;
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(asyncFileHandleReadCompletion:) name:NSFileHandleReadCompletionNotification object:[errPipe fileHandleForReading]];
// [[errPipe fileHandleForReading] readInBackgroundAndNotifyForModes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, @"taskitwait", nil]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(asyncFileHandleReadCompletion:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[errPipe fileHandleForReading]];
[[errPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotifyForModes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, @"taskitwait", nil]];
}
#endif
// sleep(5);
NSFileHandle* inHandle = [inPipe fileHandleForReading];
if ([inputPath length]) {
inHandle = [NSFileHandle fileHandleForReadingAtPath:inputPath];
}
int in_parent = [[inPipe fileHandleForWriting] fileDescriptor];
int in_child = [inHandle fileDescriptor];
int out_parent = [[outPipe fileHandleForReading] fileDescriptor];
int out_child = [[outPipe fileHandleForWriting] fileDescriptor];
int err_parent = [[errPipe fileHandleForReading] fileDescriptor];
int err_child = [[errPipe fileHandleForWriting] fileDescriptor];
// Execution
// return NO;
pid_t p = fork();
if (p == 0) {
// setsid();
// Set up stdin, stdout and stderr
//sigprocmask
close(in_parent);
dup2(in_child, STDIN_FILENO);
close(in_child);
close(out_parent);
dup2(out_child, STDOUT_FILENO);
close(out_child);
close(err_parent);
dup2(err_child, STDERR_FILENO);
close(err_child);
chdir(workingDirectoryPath);
//sleep(1);
int oldpriority = getpriority(PRIO_PROCESS, getpid());
if (priority < 20 && priority > -20 && priority > oldpriority)
setpriority(PRIO_PROCESS, getpid(), (int)priority);
// Close any open file handles that are NOT stderr
for (int i = getdtablesize(); i >= 3; i--) {
close(i);
}
execve(executablePath, (char * const *)argumentsArray, (char * const *)environmentArray);
// execve failed for some reason, try to quit gracefullyish
_exit(0);
// Uh oh, we shouldn't be here
abort();
return NO;
}
else if (p == -1) {
// Error
printf("A forking error occurred\n");
return NO;
}
else {
pid = p;
close(in_child);
close(out_child);
close(err_child);
}
isRunning = YES;
// Clean up
free((void *)executablePath);
free((void *)workingDirectoryPath);
for (size_t i = 0; i < argCounter; i++) free((void *)argumentsArray[i]);
free(argumentsArray);
for (size_t i = 0; i < envCounter; i++) free((void *)environmentArray[i]);
free(environmentArray);
// Writing
// We want to open stdin on p and write our input
NSData *inputData = input ?: [inputString dataUsingEncoding:NSUTF8StringEncoding];
if (inputData)
[[inPipe fileHandleForWriting] writeData:inputData];
[[inPipe fileHandleForWriting] closeFile];
inPipe = nil;
return YES;
}
- (BOOL)isRunning {
if (!isRunning)
return NO;
waitpid_status = 0;
pid_t wp = waitpid(pid, &waitpid_status, WNOHANG);
if (!wp)
return YES;
// if wp == -1, fail safely: act as though the process exited normally
if (wp == -1)
waitpid_status = 0;
isRunning = NO;
return isRunning;
}
- (NSInteger)processIdentifier {
return pid;
}
- (NSInteger)terminationStatus {
if (WIFEXITED(waitpid_status))
return WEXITSTATUS(waitpid_status);
return 1; // lie
}
- (NSTaskTerminationReason)terminationReason {
if (WIFEXITED(waitpid_status))
return NSTaskTerminationReasonExit;
if (WIFSIGNALED(waitpid_status))
return NSTaskTerminationReasonUncaughtSignal;
return 0;
}
- (NSInteger)terminationSignal {
if (WIFSIGNALED(waitpid_status))
return WTERMSIG(waitpid_status);
return 0;
}
- (void)interrupt // Not always possible. Sends SIGINT.
{
if ([self isRunning])
kill(pid, SIGINT);
}
- (void)terminate // Not always possible. Sends SIGTERM.
{
if ([self isRunning]) {
kill(pid, SIGTERM);
[self isRunning];
}
}
- (void)kill
{
[self terminate];
if ([self isRunning]) {
kill(pid, SIGKILL);
[self isRunning];
}
}
- (BOOL)suspend
{
if ([self isRunning])
kill(pid, SIGSTOP);
return [self isRunning];
}
- (BOOL)resume
{
if ([self isRunning])
kill(pid, SIGCONT);
return [self isRunning];
}
#pragma mark Blocking methods
- (void)reapOnExit {
if (pid > 0 && [self isRunning]) {
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, dispatch_get_main_queue());
#if !__has_feature(objc_arc)
CFRetain(self);
#endif
if (source) {
dispatch_source_set_event_handler(source, ^{
#if !__has_feature(objc_arc)
CFRelease(self);
#endif
[self isRunning];
dispatch_source_cancel(source);
dispatch_release(source);
});
dispatch_resume(source);
}
}
}
- (void)waitUntilExit {
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
NSTimeInterval delay = 0.01;
while ([self isRunning]) {
[runloop runMode:@"taskitwait" beforeDate:[NSDate dateWithTimeIntervalSinceNow:delay]];
delay *= 1.5;
if (delay >= 1.0)
delay = 1.0;
}
}
- (BOOL)waitUntilExitWithTimeout:(NSTimeInterval)timeout {
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
NSTimeInterval delay = 0.01;
NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
BOOL hitTimeout = NO;
while ([self isRunning]) {
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
if (timeout > 0 && currentTime - startTime > timeout) {
hitTimeout = YES;
break;
}
[runloop runMode:@"taskitwait" beforeDate:[NSDate dateWithTimeIntervalSinceNow:delay]];
delay *= 1.5;
if (delay >= 1.0)
delay = 1.0;
}
if (hitTimeout)
[self kill];
return hitTimeout;
}
- (NSData *)waitForOutput {
NSData *ret = nil;
[self waitForOutputData:&ret errorData:NULL];
return ret;
}
- (NSString *)waitForOutputString {
NSString *ret = nil;
[self waitForOutputString:&ret errorString:NULL];
return ret;
}
// Want to either wait for it to exit, or for it to EOF
- (NSData *)waitForError {
NSData *ret = nil;
[self waitForOutputData:NULL errorData:&ret];
return ret;
}
- (NSString *)waitForErrorString {
NSString *ret = nil;
[self waitForOutputString:NULL errorString:&ret];
return ret;
}
#ifdef TASKIT_BACKGROUNDING
- (void)asyncFileHandleReadCompletion:(NSNotification *)notif {
NSData *data = [[notif userInfo] valueForKey:NSFileHandleNotificationDataItem];
[[NSNotificationCenter defaultCenter] removeObserver:self name:[notif name] object:[notif object]];
if ([[notif object] isEqual:[outPipe fileHandleForReading]]) {
hasFinishedReadingOutput = YES;
[[outPipe fileHandleForReading] closeFile];
if (receivedOutputData)
receivedOutputData(data);
if (receivedOutputString)
receivedOutputString([[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
if (hasRetainedForOutput) {
CFRelease(self);
hasRetainedForOutput = NO;
}
}
else if ([[notif object] isEqual:[errPipe fileHandleForReading]]) {
hasFinishedReadingError = YES;
[[errPipe fileHandleForReading] closeFile];
if (receivedErrorData)
receivedErrorData(data);
if (receivedErrorString)
receivedErrorString([[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
if (hasRetainedForError) {
CFRelease(self);
hasRetainedForError = NO;
}
}
}
#endif
- (void)syncFileHandleReadCompletion:(NSNotification *)notif {
NSData *data = [[notif userInfo] valueForKey:NSFileHandleNotificationDataItem];
if ([[notif object] isEqual:[outPipe fileHandleForReading]]) {
if (!outputBuffer)
outputBuffer = [data copy];
hasFinishedReadingOutput = YES;
[[outPipe fileHandleForReading] closeFile];
}
else if ([[notif object] isEqual:[errPipe fileHandleForReading]]) {
if (!errorBuffer)
errorBuffer = [data copy];
hasFinishedReadingError = YES;
[[errPipe fileHandleForReading] closeFile];
}
}
- (BOOL)waitForOutputData:(NSData **)output errorData:(NSData **)error {
NSMutableData *outdata = [NSMutableData data];
NSMutableData *errdata = [NSMutableData data];
BOOL hadWhoopsie = [self waitForIntoOutputData:outdata intoErrorData:errdata];
if (output)
*output = outdata;
if (error)
*error = errdata;
return hadWhoopsie;
}
@synthesize shouldSetUpFileHandlesAutomatically;
- (void)setUpFileHandles {
int outfd = [[outPipe fileHandleForReading] fileDescriptor];
int errfd = [[errPipe fileHandleForReading] fileDescriptor];
int outflags = fcntl(outfd, F_GETFL, 0);
fcntl(outfd, F_SETFL, outflags | O_NONBLOCK);
int errflags = fcntl(errfd, F_GETFL, 0);
fcntl(errfd, F_SETFL, errflags | O_NONBLOCK);
}
- (BOOL)waitForIntoOutputData:(NSMutableData *)outdata intoErrorData:(NSMutableData *)errdata {
// if (receivedOutputData || receivedOutputString || receivedErrorData || receivedErrorString)
// @throw [[NSException alloc] initWithName:@"TaskitAsyncSyncCombination" reason:@"-waitForOutputData:errorData: called when async output is in use. These two features are mutually exclusive!" userInfo:[NSDictionary dictionary]];
// return YES;
if (![self isRunning]) {
// CHDebug(@"not running!");
return YES;
}
int outfd = [[outPipe fileHandleForReading] fileDescriptor];
int errfd = [[errPipe fileHandleForReading] fileDescriptor];
if (shouldSetUpFileHandlesAutomatically)
[self setUpFileHandles];
#define TASKIT_BUFLEN 200
char outbuf[TASKIT_BUFLEN];
char errbuf[TASKIT_BUFLEN];
BOOL hasFinishedOutput = NO;
BOOL hasFinishedError = NO;
BOOL outputHadAWhoopsie = NO;
BOOL errorHadAWhoopsie = NO;
while (1) {
if (!hasFinishedOutput) {
ssize_t outread = read(outfd, &outbuf, TASKIT_BUFLEN);
const volatile int outerrno = errno;
if (outread >= 1) {
[outdata appendBytes:outbuf length:outread];
}
else if (outread == 0) {
hasFinishedOutput = YES;
}
else {
// CHDebug(@"out errno = %d", outerrno);
if (outerrno != EAGAIN) {
hasFinishedOutput = YES;
outputHadAWhoopsie = YES;
}
}
}
if (!hasFinishedError) {
ssize_t errread = read(errfd, &errbuf, TASKIT_BUFLEN);
const volatile int errerrno = errno;
if (errread >= 1) {
[errdata appendBytes:errbuf length:errread];
}
else if (errread == 0) {
hasFinishedError = YES;
}
else {
// CHDebug(@"err errno = %d", errerrno);
if (errerrno != EAGAIN) {
hasFinishedError = YES;
errorHadAWhoopsie = YES;
}
}
}
if (hasFinishedOutput && hasFinishedError) {
break;
}
}
// CHDebug(@"output = %ld, error = %ld", (long)output, (long)error);
return !outputHadAWhoopsie && !errorHadAWhoopsie;
}
- (void)waitForOutputString:(NSString **)output errorString:(NSString **)error {
NSData *outputData = nil;
NSData *errorData = nil;
[self waitForOutputData:output ? &outputData : NULL errorData:error ? &errorData : NULL];
if (outputData)
*output = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
if (errorData)
*error = [[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding];
}
#pragma mark Goodbye!
- (void)end {
[[outPipe fileHandleForReading] closeFile];
outPipe = nil;
[[errPipe fileHandleForReading] closeFile];
errPipe = nil;
}
- (void)dealloc {
//NSLog(@"Deallocing %@", launchPath);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end