@@ -39,6 +39,7 @@ class Process
39
39
private $ timeout ;
40
40
private $ options ;
41
41
private $ exitcode ;
42
+ private $ fallbackExitcode ;
42
43
private $ processInformation ;
43
44
private $ stdout ;
44
45
private $ stderr ;
@@ -211,7 +212,13 @@ public function start($callback = null)
211
212
);
212
213
$ descriptors = array (array ('pipe ' , 'r ' ), $ this ->fileHandles [self ::STDOUT ], array ('pipe ' , 'w ' ));
213
214
} else {
214
- $ descriptors = array (array ('pipe ' , 'r ' ), array ('pipe ' , 'w ' ), array ('pipe ' , 'w ' ));
215
+ $ descriptors = array (
216
+ array ('pipe ' , 'r ' ), // stdin
217
+ array ('pipe ' , 'w ' ), // stdout
218
+ array ('pipe ' , 'w ' ), // stderr
219
+ array ('pipe ' , 'w ' ) // last exit code is output on the fourth pipe and caught to work around --enable-sigchild
220
+ );
221
+ $ this ->commandline = '( ' .$ this ->commandline .') 3>/dev/null; echo $? >&3 ' ;
215
222
}
216
223
217
224
$ commandline = $ this ->commandline ;
@@ -336,8 +343,14 @@ public function wait($callback = null)
336
343
foreach ($ r as $ pipe ) {
337
344
$ type = array_search ($ pipe , $ this ->pipes );
338
345
$ data = fread ($ pipe , 8192 );
346
+
339
347
if (strlen ($ data ) > 0 ) {
340
- call_user_func ($ callback , $ type == 1 ? self ::OUT : self ::ERR , $ data );
348
+ // last exit code is output and caught to work around --enable-sigchild
349
+ if (3 == $ type ) {
350
+ $ this ->fallbackExitcode = (int ) $ data ;
351
+ } else {
352
+ call_user_func ($ callback , $ type == 1 ? self ::OUT : self ::ERR , $ data );
353
+ }
341
354
}
342
355
if (false === $ data || feof ($ pipe )) {
343
356
fclose ($ pipe );
@@ -363,7 +376,13 @@ public function wait($callback = null)
363
376
throw new \RuntimeException (sprintf ('The process stopped because of a "%s" signal. ' , $ this ->processInformation ['stopsig ' ]));
364
377
}
365
378
366
- return $ this ->exitcode = $ this ->processInformation ['running ' ] ? $ exitcode : $ this ->processInformation ['exitcode ' ];
379
+ $ this ->exitcode = $ this ->processInformation ['running ' ] ? $ exitcode : $ this ->processInformation ['exitcode ' ];
380
+
381
+ if (-1 == $ this ->exitcode && null !== $ this ->fallbackExitcode ) {
382
+ $ this ->exitcode = $ this ->fallbackExitcode ;
383
+ }
384
+
385
+ return $ this ->exitcode ;
367
386
}
368
387
369
388
/**
0 commit comments