pinetd2 /test/netbatch.php

Language PHP Lines 448
MD5 Hash 82c5c284900d57e1a7bb9140a7511f76
Repository https://github.com/blekkzor/pinetd2.git View Raw File View Project SPDX
  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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php

class NetBatch_Process {
	private $pid;
	private $parent;
	private $blocking;
	private $resume;

	public function __construct($parent, $pid, $resume, $persist) {
		$this->parent = $parent;
		$this->pid = $pid;
		$this->blocking = true;
		$this->resume = $resume;
		$this->persist = $persist;
	}

	public function __destruct() {
		if ($this->running())
			$this->kill(9); // SIGKILL
		$this->parent->freePid($this->pid);
	}

	public function setBlocking($blocking) {
		$this->blocking = (bool)$blocking;
	}

	public function dump() {
		return $this->parent->dump($this->pid);
	}

	public function eof($fd) {
		return $this->parent->eof($fd, $this->pid);
	}

	public function read($fd, $size) {
		return $this->parent->read($fd, $size, $this->pid, $this->blocking);
	}

	public function gets($fd, $size = NULL) {
		return $this->parent->gets($fd, $size, $this->pid, $this->blocking);
	}

	public function wait() {
		return $this->parent->wait($this->pid);
	}

	public function write($fd, $buf) {
		return $this->parent->write($fd, $buf, $this->pid);
	}

	public function kill($signal = 15) {
		return $this->parent->kill($signal, $this->pid);
	}

	public function close($fd) {
		return $this->parent->close($fd, $this->pid);
	}

	public function getPid() {
		return $this->pid;
	}

	public function poll() {
		return $this->parent->poll($this->pid);
	}

	public function isResumed() {
		return (bool)$this->resume;
	}

	public function running() {
		return $this->parent->isRunning($this->pid);
	}

	public function exitCode() {
		return $this->parent->getExitCode($this->pid);
	}
}

class NetBatch {
	private $fp;
	private $name;
	private $salt;
	private $type;
	private $pipes = array();
	private $pipes_buf = array();
	private $running = array();
	private $returnCode = array();
	private $persist = array();
	private $last_pid;

	const PKT_STANDARD = 0;
	const PKT_LOGIN = 1;
	const PKT_EXIT = 2;
	const PKT_RUN = 3;
	const PKT_RETURNCODE = 4;
	const PKT_EOF = 5;
	const PKT_CLOSE = 6;
	const PKT_DATA = 7;
	const PKT_NOPIPES = 8;
	const PKT_KILL = 9;
	const PKT_POLL = 10;

	public function __construct($host, $port = 65432) {
		$this->fp = fsockopen($host, $port);
		if (!$this->fp) throw new Exception('Could not connect');

		$this->name = $this->readPacket();
		$this->salt = $this->readPacket();
	}

	public function __destruct() {
		$this->sendPacket('', self::PKT_EXIT);
		fclose($this->fp);
	}

	public function freePid($pid) {
		unset($this->pipes[$pid]);
		unset($this->pipes_buf[$pid]);
		unset($this->returnCode[$pid]);
	}

	public function ident($login, $key) {
		$this->sendPacket(sha1($key.$this->salt, true).$login, self::PKT_LOGIN);
		$result = $this->readPacket();
		return (bool)$result;
	}

	public function run($cmd, $pipes = NULL, $env = NULL, $persist = false) {
		if (is_null($pipes)) 
			$pipes = array(0=>'r', 1=>'w', 2=>'w');

		$packet = array(
			'cmd' => $cmd,
			'pipes' => $pipes,
		);
		if ($persist !== false)
			$packet['persist'] = $persist;

		if (!is_null($env)) $packet['env'] = $env;

		$this->sendPacket(serialize($packet), self::PKT_RUN);

		$pid = $this->readPacket();
		if ($pid === '0') return false;

		list(,$pid, $resume) = unpack('N2', $pid);
		$this->running[$pid] = true;
		$this->last_pid = $pid;
		$this->pipes[$pid] = $pipes;
		$this->pipes_buf[$pid] = array();
		if ($persist !== false) $this->persist[$pid] = true;

		return new NetBatch_Process($this, $pid, $resume, $persist);
	}

	public function dump($pid = NULL) {
		if (is_null($pid)) $pid = $this->last_pid;
		var_dump($this->pipes_buf[$pid]);
	}

	public function eof($fd, $pid = NULL) {
		if (is_null($pid)) $pid = $this->last_pid;
		// still has a buffer => not EOF
		if (isset($this->pipes_buf[$pid][$fd])) return false;

		// if the stream exists it's not EOF yet
		return !isset($this->pipes[$pid][$fd]);
	}

	public function read($fd, $size, $pid = NULL, $blocking) {
		if (is_null($pid)) 
			$pid = $this->last_pid;

		while(1) {
			if (isset($this->pipes_buf[$pid][$fd])) {
				if (strlen($this->pipes_buf[$pid][$fd]) >= $size) {
					$ret = substr($this->pipes_buf[$pid][$fd], 0, $size);
					if ($size == strlen($this->pipes_buf[$pid][$fd])) {
						unset($this->pipes_buf[$pid][$fd]);
					} else {
						$this->pipes_buf[$pid][$fd] = substr($this->pipes_buf[$pid][$fd], $size);
					}

					return $ret;
				}

				if (!isset($this->pipes[$pid][$fd])) {
					// reached EOF, flush buffer first
					$res = $this->pipes_buf[$pid][$fd];
					unset($this->pipes_buf[$pid][$fd]);
					return $res;
				}
			}

			if (!isset($this->pipes[$pid][$fd])) return false;

			if (!$this->getEvent($pid, $blocking)) break;
		}
		return false;
	}

	public function gets($fd, $size = NULL, $pid = NULL, $blocking) {
		if (is_null($pid))
			$pid = $this->last_pid;

		while(1) {
			if (isset($this->pipes_buf[$pid][$fd])) {
				$pos = strpos($this->pipes_buf[$pid][$fd], "\n");

				if ($pos !== false) {
					$pos++;
					$ret = substr($this->pipes_buf[$pid][$fd], 0, $pos);
					if ($pos == strlen($this->pipes_buf[$pid][$fd])) {
						unset($this->pipes_buf[$pid][$fd]);
					} else {
						$this->pipes_buf[$pid][$fd] = substr($this->pipes_buf[$pid][$fd], $pos);
					}
					return $ret;
				}

				if ((!is_null($size)) && (strlen($this->pipes_buf[$pid][$fd]) >= $size)) {
					$ret = substr($this->pipes_buf[$pid][$fd], 0, $size);
					if ($size == strlen($this->pipes_buf[$pid][$fd])) {
						unset($this->pipes_buf[$pid][$fd]);
					} else {
						$this->pipes_buf[$pid][$fd] = substr($this->pipes_buf[$pid][$fd], $size);
					}

					return $ret;
				}

				if (!isset($this->pipes[$pid][$fd])) {
					// reached EOF, flush buffer first
					$res = $this->pipes_buf[$pid][$fd];
					unset($this->pipes_buf[$pid][$fd]);
					return $res;
				}
			}

			if (!isset($this->pipes[$pid][$fd])) return false;

			if (!$this->getEvent($pid, $blocking)) break;
		}
		return false;
	}

	public function wait($pid = NULL) {
		if (is_null($pid)) $pid = $this->last_pid;

		while($this->running[$pid])
			$this->getEvent($pid);

		return $this->returnCode[$pid];
	}

	public function getExitCode($pid) {
		return $this->returnCode[$pid];
	}

	public function isRunning($pid) {
		return isset($this->running[$pid]);
	}

	public function hasProcesses() {
		return (bool)count($this->running);
	}

	public function write($fd, $data, $pid = NULL) {
		if (is_null($pid)) $pid = $this->last_pid;
		$this->sendPacket(pack('NN', $pid, $fd).$data, self::PKT_DATA);
	}

	public function kill($signal = 15, $pid = NULL) {
		if (is_null($pid)) $pid = $this->last_pid;
		$this->sendPacket(pack('NN', $pid, $signal), self::PKT_KILL);
	}

	public function poll($pid) {
		$this->sendPacket(pack('N', $pid), self::PKT_POLL);
	}

	protected function getEvent($pid, $blocking = true) {
		$pkt = $this->readPacket($pid, $blocking);

		if ($pkt === false) return false;

		switch($this->type) {
			case self::PKT_EOF:
				list(,$pid, $fd) = unpack('N2', $pkt);
				unset($this->pipes[$pid][$fd]);
				break;
			case self::PKT_DATA:
				list(,$pid,$fd) = unpack('N2', substr($pkt, 0, 8));
				$pkt = (string)substr($pkt, 8);
				$this->pipes_buf[$pid][$fd] .= $pkt;
				break;
			case self::PKT_NOPIPES:
				// mmh?
				break;
			case self::PKT_RETURNCODE:
				list(,$pid, $rc) = unpack('N2', $pkt);
				unset($this->running[$pid]);
				unset($this->pipes[$pid]);
				$this->returnCode[$pid] = $rc;
				break;
			default:
				var_dump($this->type);
				break;
		}
		return true;
	}

	public function getActive(array $tmp) {
		if (!$tmp)
			throw new Exception('getActive() called without params');

		$this->getEvent(0, false);

		$list = array();
		$keys = array();
		foreach($tmp as $key => $process) {
			$list[$process->getPid()] = $process;
			$keys[$process->getPid()] = $key;
		}

		$final_list = array();
		while(!$final_list) {
			foreach($list as $pid => $process) {
				if ($this->pipes_buf[$pid]) {
					$final_list[$keys[$pid]] = $process;
				}
				
				if (!$process->running())
					$final_list[$keys[$pid]] = $process;
			}

			if (!$final_list)
				$this->getEvent(0);
		}

		return $final_list;
	}

	public function close($fd, $pid = NULL) {
		if (is_null($pid)) $pid = $this->last_pid;
		$this->sendPacket(pack('NN', $pid, $fd), self::PKT_CLOSE);
	}

	protected function sendPacket($data, $type = self::PKT_STANDARD) {
		return fwrite($this->fp, pack('nn', $type, strlen($data)).$data);
	}

	protected function readPacket($pid = 0, $blocking = true) {
		if (feof($this->fp)) throw new Exception('Connection lost');

		if ($this->persist[$pid]) {
			$now = time();
			if ($this->persist[$pid] != $now) {
				$this->poll($pid);
				$this->persist[$pid] = $now;
			}
			if ($blocking)
				while (!stream_select($r = array($this->fp), $w = NULL, $e = NULL, 1)) {
					$this->poll($pid);
				}
		}

		if (!$blocking) {
			if (!stream_select($r = array($this->fp), $w = NULL, $e = NULL, 0))
				return false;
		}

		$len = fread($this->fp, 4);
		if (strlen($len) != 4) throw new Exception('Connection lost');
		list(,$type,$len) = unpack('n2', $len);

		$this->type = $type;

		if ($len == 0) return '';

		return fread($this->fp, $len);
	}
}

$netbatch = new NetBatch('127.0.0.1');
$netbatch->ident('test','test');

$process = $netbatch->run(array('php'), NULL, NULL, 'PhpEval');

if (!$process->isResumed()) {
	$process->write(0, '<?php while(1) { echo "BLAH\n"; sleep(1); }');
	$process->close(0);
}

$c = 10;

while(!$process->eof(1)) {
	var_dump(rtrim($process->gets(1)));
	if ($c-- < 0) {
		$process->kill();
		$c = 99;
	}
}

while(!$process->eof(2))
	var_dump(rtrim($process->gets(2)));

/*
$ping = array();

$ping[] = $netbatch->run(array('ping', '127.0.0.1', '-c', '5'));
$ping[] = $netbatch->run(array('ping', 'google.fr', '-c', '5'));

foreach($ping as $process)
	$process->setBlocking(false);

while($netbatch->hasProcesses()) {
	foreach($netbatch->getActive($ping) as $key => $process) {
		$line = $process->gets(1);
		while($line !== false) {
			echo 'PID#'.$process->getPid().': '.rtrim($line)."\n";
			$line = $process->gets(1);
		}

		if (!$process->running()) {
			echo 'PID#'.$process->getPid().' has exited with exit code '.$process->exitCode()."\n";
			unset($ping[$key]);
		}
	}
}
*/

/*
echo "Running: php\n";

$process = $netbatch->run(array('php'), NULL, array('foo' => 'bougaga!'));
$process->write(0, '<?php print_r($_ENV);');
$process->close(0); // close stdin

while(!$process->eof(1))
	var_dump(rtrim($process->gets(1)));

$process->wait();
$process->dump();
*/
Back to Top