forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proc.php
319 lines (284 loc) · 8.32 KB
/
Proc.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
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
<?php
/**
* Proc.php
*
* Executes a process with proc_open() and guarantees it is terminated on exit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2016 Tony Murray
* @author Tony Murray <[email protected]>
*/
namespace LibreNMS;
use Exception;
use Illuminate\Support\Str;
class Proc
{
/**
* @var resource the process this object is responsible for
*/
private $_process;
/**
* @var array array of process pipes [stdin,stdout,stderr]
*/
private $_pipes;
/**
* @var bool if this process is synchronous (waits for output)
*/
private $_synchronous;
/**
* @var int|null hold the exit code, we can only get this on the first process_status after exit
*/
private $_exitcode = null;
/**
* Create and run a new process
* Most arguments match proc_open()
*
* @param string $cmd the command to execute
* @param array $descriptorspec the definition of pipes to initialize
* @param null $cwd working directory to change to
* @param array|null $env array of environment variables to set
* @param bool $blocking set the output pipes to blocking (default: false)
*
* @throws Exception the command was unable to execute
*/
public function __construct(
$cmd,
$descriptorspec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
],
$cwd = null,
$env = null,
$blocking = false
) {
$this->_process = proc_open($cmd, $descriptorspec, $this->_pipes, $cwd, $env);
if (! is_resource($this->_process)) {
throw new Exception("Command failed: $cmd");
}
stream_set_blocking($this->_pipes[1], $blocking);
stream_set_blocking($this->_pipes[2], $blocking);
$this->_synchronous = true;
}
/**
* Called when this object goes out of scope or php exits
* If it is still running, terminate the process
*/
public function __destruct()
{
if ($this->isRunning()) {
$this->terminate();
}
}
/**
* Get one of the pipes
* 0 - stdin
* 1 - stdout
* 2 - stderr
*
* @param int $nr pipe number (0-2)
* @return resource the pipe handle
*/
public function pipe($nr)
{
return $this->_pipes[$nr];
}
/**
* Send a command to this process and return the output
* the output may not correspond to this command if this
* process is not synchronous
* If the command isn't terminated with a newline, add one
*
* @param string $command
* @return array
*/
public function sendCommand($command)
{
$this->sendInput($this->checkAddEOL($command));
return $this->getOutput();
}
/**
* Send data to stdin
*
* @param string $data the string to send
*/
public function sendInput($data)
{
fwrite($this->_pipes[0], $data);
}
/**
* Gets the current output of the process
* If this process is set to synchronous, wait for output
*
* @param int $timeout time to wait for output, only applies if this process is synchronous
* @return array [stdout, stderr]
*/
public function getOutput($timeout = 15)
{
if ($this->_synchronous) {
$pipes = [$this->_pipes[1], $this->_pipes[2]];
$w = null;
$x = null;
stream_select($pipes, $w, $x, $timeout);
}
return [stream_get_contents($this->_pipes[1]), stream_get_contents($this->_pipes[2])];
}
/**
* Close all pipes for this process
*/
private function closePipes()
{
foreach ($this->_pipes as $pipe) {
if (is_resource($pipe)) {
fclose($pipe);
}
}
}
/**
* Attempt to gracefully close this process
* optionally send one last piece of input
* such as a quit command
*
* ** Warning: this will block until the process closes.
* Some processes might not close on their own.
*
* @param string $command the final command to send (appends newline if one is ommited)
* @return int the exit status of this process (-1 means error)
*/
public function close($command = null)
{
if (isset($command)) {
try {
if (is_resource($this->_pipes[0])) {
$this->sendInput($this->checkAddEOL($command));
}
} catch (\ErrorException $e) {
// might have closed already
}
}
$this->closePipes();
return proc_close($this->_process);
}
/**
* Forcibly close this process
* Please attempt to run close() instead of this
* This will be called when this object is destroyed if the process is still running
*
* @param int $timeout how many microseconds to wait before terminating (SIGKILL)
* @param int $signal the signal to send
*
* @throws Exception
*/
public function terminate($timeout = 3000, $signal = 15)
{
$status = $this->getStatus();
$this->closePipes();
$closed = proc_terminate($this->_process, $signal);
$time = 0;
while ($time < $timeout) {
$closed = ! $this->isRunning();
if ($closed) {
break;
}
usleep(100);
$time += 100;
}
if (! $closed) {
// try harder
if (function_exists('posix_kill')) {
$killed = posix_kill($status['pid'], 9); //9 is the SIGKILL signal
} else {
$killed = proc_terminate($this->_process, 9);
}
proc_close($this->_process);
if (! $killed && $this->isRunning()) {
throw new Exception('Terminate failed!');
}
}
}
/**
* Get the status of this process
* see proc_get_status()
*
* @return array status array
*/
public function getStatus()
{
$status = proc_get_status($this->_process);
if ($status['running'] === false && is_null($this->_exitcode)) {
$this->_exitcode = $status['exitcode'];
}
return $status;
}
/**
* Check if this process is running
*
* @return bool
*/
public function isRunning()
{
if (! is_resource($this->_process)) {
return false;
}
$st = $this->getStatus();
return isset($st['running']) && $st['running'];
}
/**
* Returns the exit code from the process.
* Will return null unless isRunning() or getStatus() has been run and returns false.
*
* @return int|null
*/
public function getExitCode()
{
return $this->_exitcode;
}
/**
* If this process waits for output
*
* @return bool
*/
public function isSynchronous()
{
return $this->_synchronous;
}
/**
* Set this process as synchronous, by default processes are synchronous
* It is advisable not to change this mid way as output could get mixed up
* or you could end up blocking until the getOutput timeout expires
*
* @param bool $synchronous
*/
public function setSynchronous($synchronous)
{
$this->_synchronous = $synchronous;
}
/**
* Add and end of line character to a string if
* it doesn't already end with one
*
* @param string $string
* @return string
*/
private function checkAddEOL($string)
{
if (! Str::endsWith($string, PHP_EOL)) {
$string .= PHP_EOL;
}
return $string;
}
}