-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess.class.php
271 lines (251 loc) · 7.81 KB
/
Process.class.php
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
<?php
/**
* process class
*
* @author Christian Lück <[email protected]>
* @copyright Copyright (c) 2011, Christian Lück
* @license http://www.opensource.org/licenses/mit-license MIT License
* @package Process
* @version v0.0.1
* @link https://github.com/clue/Process
*/
class Process{
/**
* process resource
*
* @var resource
*/
protected $fp;
/**
* process input/output stream resources
*
* @var array[resource]
*/
protected $pipes = array();
/**
* exit code
*
* @var int|NULL
*/
protected $exitcode = NULL;
/**
* instanciate new process
*
* @param string $cmd command to execute
* @param NULL|string $dir directory to execute command in
* @param NULL|array $env environment to use (NULL=use current)
* @throws Process_Exception if process could not be started
* @uses proc_open()
*/
public function __construct($cmd,$dir=NULL,$env=NULL){
static $pipes = array(
0 => array('pipe','r'),
1 => array('pipe','w'),
2 => STDERR
);
$this->fp = proc_open($cmd,$pipes,$this->pipes,$dir,$env);
if($this->fp === false){
throw new Process_Exception('Unable to start process');
}
}
/**
* destruct and end process
*
* @uses Process::kill()
* @uses Process::close()
*/
public function __destruct(){
$this->kill(true)->close(true);
}
/**
* get process status
*
* @param string|NULL $flag
* @return mixed
* @throws Process_Exception on error
* @uses proc_get_status()
*/
public function getStatus($flag=NULL){
$status = proc_get_status($this->fp);
if($status === false){
throw new Process_Exception('Unable to get process status');
}
if($status['exitcode'] == -1 && $this->exitcode !== NULL){ // exit code unknown, eg not first call after being exited
$status['exitcode'] = $this->exitcode; // restore previous code
}else if($status['exitcode'] != -1 && $status['running'] === false){ // exit code present
$this->exitcode = $status['exitcode']; // save for future reference
}
if($flag !== NULL){
if(!isset($status[$flag])){
throw new Process_Exception('Invalid status flag given');
}
return $status[$flag];
}
return $status;
}
/**
* get process exit code
*
* @return int exit code
* @throws Process_Exception if process is still running
* @uses Process::getStatus()
*/
public function getExitcode(){
$code = $this->exitcode;
if($code === NULL){
$code = $this->getStatus('exitcode');
if($code === NULL){
throw new Exception('Exit code not available');
}
}
return $code;
}
/**
* get process ID
*
* Warning: PHP returns the PID of the shell (sh) that runs the actual
* command.
*
* @return int
* @uses Process::getStatus()
* @link http://www.php.net/manual/en/function.proc-get-status.php#93382 PHP manual for more information on the PID returned
*/
public function getPid(){
return $this->getStatus('pid');
}
/**
* checks whether the process is still running
*
* @return boolean
* @uses Process::getStatus()
*/
public function isRunning(){
return $this->getStatus('running');
}
/**
* get the command string that was passed to the process
*
* @return string
* @uses Process::getStatus()
*/
public function getCommand(){
return $this->getStatus('command');
}
/**
* close process handle and input/output pipes
*
* Warning: proc_close() waits for the process to terminate, so consider
* calling kill(true) before trying to close
*
* @param boolean $force whether to ignore any issues and complete operation (should not be used unless your have a very good reason, e.g. destructing)
* @return Process $this (chainable)
* @throws Process_Exception on error
* @see Process::kill()
* @uses fclose() to close every input/ouput stream
* @uses proc_close() to close process handle
*/
public function close($force=false){
//var_dump($this->pipes);
foreach($this->pipes as $n=>$pipe){
if($force){
@fclose($pipe);
}else if(fclose($pipe) === false){
throw new Process_Exception('Unable to close process pipe '.$n);
}
}
$this->pipes = array();
$code = proc_close($this->fp);
if($code === false){ // invalid result like when handle is invalid (already closed?)
throw new Process_Exception('Unable to close process handle');
}
if($code !== -1){ // ignore invalid exit code (likely process already dead)
$this->exitcode = $code;
}
return $this;
}
/**
* kill process (with given signal)
*
* please be aware that this command does not necessarily terminate the process,
* as some signals can be blocked (most notably the default signal).
*
* calling 'kill(true)' is an alias to SIGKILL. use it to make sure the process
* will actually be terminated
*
* @param NULL|int|boolean $signal signal to send (true=SIGKILL,default:NULL=SIGTERM)
* @return Process $this (chainable)
* @uses Process_Helper::getSignal() to resolve signal names to signal numbers
* @uses proc_terminate()
*/
public function kill($signal=NULL){
if($signal === true){
$signal = Process_Helper::getSignal('SIGKILL');
}
if($signal === NULL || $signal === false){
proc_terminate($this->fp);
}else{
proc_terminate($this->fp,$signal);
}
return $this;
}
/**
* read up to $len bytes from process output
*
* @param int $len
* @return string
* @throws Process_Exception on error
* @uses fread()
*/
public function fread($len){
$ret = fread($this->pipes[1],$len);
if($ret === false){
throw new Process_Exception('Unable to read from process output stream');
}
return $ret;
}
/**
* write buffer to process input
*
* @param string $buffer
* @return int number of bytes actually written
* @throws Process_Exception on error
* @uses fwrite()
*/
public function fwrite($buffer){
$ret = fwrite($this->pipes[0],$buffer);
if($ret === false){
throw new Process_Exception('Unable to send to process input stream');
}
return $ret;
}
/**
* get stream to read from
*
* @return resource
*/
public function getStreamRead(){
return $this->pipes[1];
}
/**
* get stream to write to
*
* @return resource
*/
public function getStreamWrite(){
return $this->pipes[0];
}
public function getStream(){
return Process_Stream::factory($this);
}
public function setStreamBlocking($mode,$pipe=NULL){
if($pipe === NULL){
foreach($this->pipes as $pipe){
stream_set_blocking($pipe,$mode);
}
return true;
}else if(!isset($this->pipes[$pipe])){
throw new Process_Exception('Invalid pipe');
}
return stream_set_blocking($this->pipes[$pipe],$mode);
}
}