@@ -76,12 +76,12 @@ class Process
76
76
77
77
private static $ sigchild ;
78
78
private static $ posixSignals = array (
79
- 1 => 1 , // SIGHUP
80
- 2 => 2 , // SIGINT
81
- 3 => 3 , // SIGQUIT
82
- 6 => 6 , // SIGABRT
83
- 14 => 14 , // SIGALRM
84
- 15 => 15 , // SIGTERM
79
+ 1 , // SIGHUP
80
+ 2 , // SIGINT
81
+ 3 , // SIGQUIT
82
+ 6 , // SIGABRT
83
+ 14 , // SIGALRM
84
+ 15 , // SIGTERM
85
85
);
86
86
87
87
/**
@@ -285,27 +285,33 @@ public function start($callback = null)
285
285
if (!isset ($ this ->options ['bypass_shell ' ])) {
286
286
$ this ->options ['bypass_shell ' ] = true ;
287
287
}
288
- }
288
+ } elseif (!$ this ->useFileHandles && $ this ->enhanceSigchildCompatibility && $ this ->isSigchildEnabled ()) {
289
+ // last exit code is output on the fourth pipe and caught to work around --enable-sigchild
290
+ $ descriptors [3 ] = array ('pipe ' , 'w ' );
291
+
292
+ $ commandline = '' ;
293
+ foreach (self ::$ posixSignals as $ s ) {
294
+ $ commandline .= "trap 'echo s $ s >&3' $ s; " ;
295
+ }
289
296
290
- $ ptsWorkaround = null ;
297
+ $ commandline .= '{ ( ' .$ this ->commandline .') <&3 3<&- 3>/dev/null & } 3<&0; ' ;
298
+ $ commandline .= 'pid=$!; echo $pid >&3; wait $pid; code=$?; echo x$code >&3; exit $code ' ;
291
299
292
- if (!$ this ->useFileHandles && $ this ->enhanceSigchildCompatibility && $ this ->isSigchildEnabled ()) {
293
300
// Workaround for the bug, when PTS functionality is enabled.
294
301
// @see : https://bugs.php.net/69442
295
302
$ ptsWorkaround = fopen (__FILE__ , 'r ' );
296
303
}
297
304
298
305
$ this ->process = proc_open ($ commandline , $ descriptors , $ this ->processPipes ->pipes , $ this ->cwd , $ this ->env , $ this ->options );
299
306
300
- if ($ ptsWorkaround ) {
301
- fclose ($ ptsWorkaround );
302
- }
303
-
304
307
if (!is_resource ($ this ->process )) {
305
308
throw new RuntimeException ('Unable to launch a new process. ' );
306
309
}
307
310
$ this ->status = self ::STATUS_STARTED ;
308
311
312
+ if (isset ($ descriptors [3 ])) {
313
+ $ this ->fallbackStatus ['pid ' ] = (int ) fgets ($ this ->processPipes ->pipes [3 ]);
314
+ }
309
315
if ($ this ->tty ) {
310
316
return ;
311
317
}
@@ -596,8 +602,6 @@ public function clearErrorOutput()
596
602
public function getExitCode ()
597
603
{
598
604
if (!$ this ->enhanceSigchildCompatibility && $ this ->isSigchildEnabled ()) {
599
- $ this ->stop (0 );
600
-
601
605
throw new RuntimeException ('This PHP has been compiled with --enable-sigchild. You must use setEnhanceSigchildCompatibility() to use this method. ' );
602
606
}
603
607
@@ -651,8 +655,6 @@ public function hasBeenSignaled()
651
655
$ this ->requireProcessIsTerminated (__FUNCTION__ );
652
656
653
657
if (!$ this ->enhanceSigchildCompatibility && $ this ->isSigchildEnabled ()) {
654
- $ this ->stop (0 );
655
-
656
658
throw new RuntimeException ('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved. ' );
657
659
}
658
660
@@ -674,8 +676,6 @@ public function getTermSignal()
674
676
$ this ->requireProcessIsTerminated (__FUNCTION__ );
675
677
676
678
if ($ this ->isSigchildEnabled () && (!$ this ->enhanceSigchildCompatibility || -1 === $ this ->processInformation ['termsig ' ])) {
677
- $ this ->stop (0 );
678
-
679
679
throw new RuntimeException ('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved. ' );
680
680
}
681
681
@@ -1223,14 +1223,7 @@ public static function isPtySupported()
1223
1223
return $ result = false ;
1224
1224
}
1225
1225
1226
- $ proc = @proc_open ('echo 1 ' , array (array ('pty ' ), array ('pty ' ), array ('pty ' )), $ pipes );
1227
- if (is_resource ($ proc )) {
1228
- proc_close ($ proc );
1229
-
1230
- return $ result = true ;
1231
- }
1232
-
1233
- return $ result = false ;
1226
+ return $ result = (bool ) @proc_open ('echo 1 ' , array (array ('pty ' ), array ('pty ' ), array ('pty ' )), $ pipes );
1234
1227
}
1235
1228
1236
1229
/**
@@ -1245,22 +1238,8 @@ private function getDescriptors()
1245
1238
} else {
1246
1239
$ this ->processPipes = UnixPipes::create ($ this , $ this ->input );
1247
1240
}
1248
- $ descriptors = $ this ->processPipes ->getDescriptors ($ this ->outputDisabled );
1249
-
1250
- if (!$ this ->useFileHandles && $ this ->enhanceSigchildCompatibility && $ this ->isSigchildEnabled ()) {
1251
- // last exit code is output on the fourth pipe and caught to work around --enable-sigchild
1252
- $ descriptors [3 ] = array ('pipe ' , 'w ' );
1253
1241
1254
- $ trap = '' ;
1255
- foreach (self ::$ posixSignals as $ s ) {
1256
- $ trap .= "trap 'echo s $ s >&3' $ s; " ;
1257
- }
1258
-
1259
- $ this ->commandline = $ trap .'{ ( ' .$ this ->commandline .') <&3 3<&- 3>/dev/null & } 3<&0; ' ;
1260
- $ this ->commandline .= 'pid=$!; echo p$pid >&3; wait $pid; code=$?; echo x$code >&3; exit $code ' ;
1261
- }
1262
-
1263
- return $ descriptors ;
1242
+ return $ this ->processPipes ->getDescriptors ($ this ->outputDisabled );
1264
1243
}
1265
1244
1266
1245
/**
@@ -1462,14 +1441,6 @@ private function doSignal($signal, $throwException)
1462
1441
return false ;
1463
1442
}
1464
1443
1465
- if ($ this ->enhanceSigchildCompatibility && $ this ->isSigchildEnabled () && !isset (self ::$ posixSignals [$ signal ]) && !(function_exists ('posix_kill ' ) && @posix_kill ($ this ->getPid (), $ signal ))) {
1466
- if ($ throwException ) {
1467
- throw new RuntimeException ('This PHP has been compiled with --enable-sigchild and posix_kill() is not available. ' );
1468
- }
1469
-
1470
- return false ;
1471
- }
1472
-
1473
1444
if ('\\' === DIRECTORY_SEPARATOR ) {
1474
1445
exec (sprintf ('taskkill /F /T /PID %d 2>&1 ' , $ this ->getPid ()), $ output , $ exitCode );
1475
1446
if ($ exitCode && $ this ->isRunning ()) {
@@ -1479,14 +1450,21 @@ private function doSignal($signal, $throwException)
1479
1450
1480
1451
return false ;
1481
1452
}
1482
- }
1483
-
1484
- if (true !== @proc_terminate ($ this ->process , $ signal ) && '\\' !== DIRECTORY_SEPARATOR ) {
1485
- if ($ throwException ) {
1486
- throw new RuntimeException (sprintf ('Error while sending signal `%s`. ' , $ signal ));
1453
+ } else {
1454
+ if (!$ this ->enhanceSigchildCompatibility || !$ this ->isSigchildEnabled ()) {
1455
+ $ ok = @proc_terminate ($ this ->process , $ signal );
1456
+ } elseif (function_exists ('posix_kill ' )) {
1457
+ $ ok = @posix_kill ($ this ->getPid (), $ signal );
1458
+ } elseif ($ ok = proc_open (sprintf ('kill -%d %d ' , $ signal , $ this ->getPid ()), array (2 => array ('pipe ' , 'w ' )), $ pipes )) {
1459
+ $ ok = false === fgets ($ pipes [2 ]);
1487
1460
}
1461
+ if (!$ ok ) {
1462
+ if ($ throwException ) {
1463
+ throw new RuntimeException (sprintf ('Error while sending signal `%s`. ' , $ signal ));
1464
+ }
1488
1465
1489
- return false ;
1466
+ return false ;
1467
+ }
1490
1468
}
1491
1469
1492
1470
$ this ->latestSignal = (int ) $ signal ;
0 commit comments