https://stackoverflow.com/questions/3407939/shell-exec-timeout-management-exec
($cmd, $stdin=null, &$stdout, &$stderr, $timeout=false)
| 18 | |
| 19 | // https://stackoverflow.com/questions/3407939/shell-exec-timeout-management-exec |
| 20 | function execute($cmd, $stdin=null, &$stdout, &$stderr, $timeout=false) |
| 21 | { |
| 22 | $pipes = array(); |
| 23 | $process = proc_open( |
| 24 | $cmd, |
| 25 | array(array('pipe','r'),array('pipe','w'),array('pipe','w')), |
| 26 | $pipes |
| 27 | ); |
| 28 | $start = time(); |
| 29 | $stdout = ''; |
| 30 | $stderr = ''; |
| 31 | |
| 32 | if(is_resource($process)) |
| 33 | { |
| 34 | stream_set_blocking($pipes[0], 0); |
| 35 | stream_set_blocking($pipes[1], 0); |
| 36 | stream_set_blocking($pipes[2], 0); |
| 37 | fwrite($pipes[0], $stdin); |
| 38 | fclose($pipes[0]); |
| 39 | } |
| 40 | |
| 41 | while(is_resource($process)) |
| 42 | { |
| 43 | //echo "."; |
| 44 | $stdout .= stream_get_contents($pipes[1]); |
| 45 | $stderr .= stream_get_contents($pipes[2]); |
| 46 | |
| 47 | if($timeout !== false && time() - $start > $timeout) |
| 48 | { |
| 49 | proc_terminate($process, 9); |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | $status = proc_get_status($process); |
| 54 | if(!$status['running']) |
| 55 | { |
| 56 | fclose($pipes[1]); |
| 57 | fclose($pipes[2]); |
| 58 | proc_close($process); |
| 59 | return $status['exitcode']; |
| 60 | } |
| 61 | |
| 62 | usleep(100000); |
| 63 | } |
| 64 | |
| 65 | return 1; |
| 66 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…