-
Notifications
You must be signed in to change notification settings - Fork 0
/
umd.php
119 lines (103 loc) · 2.92 KB
/
umd.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
<?php
require_once(dirname(__FILE__)."/su/suexec.php");
require_once(dirname(__FILE__)."/sockets/tranceiver.php");
class UMDDaemon extends Tranceiver {
var $timeout = 120; // Daemon idle timeout
var $logprefix = '[daemon] ';
function UMDDaemon($sockaddr) {
$this->sockaddr = $sockaddr;
$this->openlog('/tmp/umd'.basename($sockaddr).'.log');
unlink($this->sockaddr);
$this->file = __FILE__;
$listen = socket_create(AF_UNIX, SOCK_STREAM, 0)
or die('Cannot create socket');
socket_bind($listen, $this->sockaddr)
or die('Cannot bind');
socket_listen($listen)
or die('Cannot listen');
$this->register_listener($listen);
posix_setsid();
}
function run() {
$this->log("Running on ".$this->sockaddr);
while(true) $this->wait();
}
function register_socket(&$s) {
$this->othersockets[(string)$s] &= $s;
}
function terminate() {
exit();
}
function initclient(&$s) {
$this->log("Initializing client on $s");
}
function e_socket(&$s) {
if(!$in = socket_read($s, 1024)) {
if(socket_last_error() == 0) {
unset($this->othersockets[(string)$s]);
return $this->log("Event socket closed");
} else {
return $this->log(socket_strerror(socket_last_error()));
}
} else {
return $this->log("Socket event: $in");
}
}
function error(&$client, $e) {
return $this->log("Error: $e");
}
function signal(&$client, $s) {
return $this->log("Signal: $s");
}
}
class UMDClient extends Tranceiver {
var $serverclass; // Class of server
var $sockaddr; // Address of socket
var $logprefix = '[client] ';
function UMDClient($sockaddr) {
$this->openlog('/tmp/umd'.basename($sockaddr).'.log');
$this->sockaddr = $sockaddr;
if(!$this->file) $this->file = __FILE__;
if(!$this->serverclass) $this->serverclass =
str_replace('client', 'daemon', get_class($this));
if(!$this->serversocket = socket_create(AF_UNIX, SOCK_STREAM, 0))
return FALSE;
$tries = 3;
while($tries--) {
if(!@socket_connect($this->serversocket, $this->sockaddr)) {
print(socket_strerror($e = socket_last_error())."\n");
switch($e) {
case 2:
case 111:
$this->spawn_server();
break;
}
} else {
break;
}
}
$this->register_socket($this->serversocket);
}
function close() {
return socket_close($this->serversocket);
}
function spawn_args() {
return $this->sockaddr;
}
function spawn_server() {
print('Spawning '.$this->serverclass.' on '.$this->sockaddr."\n");
$startupcode = '<?php '.
'require_once("'.$this->file.'");'.
'$server = new '.$this->serverclass.'("'.addslashes($this->spawn_args()).'");'.
'$server->run();'.
'?>';
$cmd = 'echo '. escapeshellarg($startupcode).
' | php -q >>/tmp/umd'.basename($this->sockaddr).'.log 2>&1 &';
print('running '.$cmd."\n");
if(!shell_exec($cmd)) {
// print("exec failed\n");
}
sleep(1);
}
}
?>