author | Dan |
Fri, 12 Jun 2009 13:48:22 -0400 | |
changeset 76 | 487a16c7117c |
parent 68 | 32f6e2ee15ab |
permissions | -rw-r--r-- |
48
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
1 |
<?php |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
2 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
3 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
4 |
* Multi-threading (well sort of) tools |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
5 |
* |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
6 |
* Greyhound - real web management for Amarok |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
7 |
* Copyright (C) 2008 Dan Fuhry |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
8 |
* |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
9 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
10 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
11 |
* |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
13 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
14 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
15 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
16 |
require_once('json.php'); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
17 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
18 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
19 |
* Global signal handler for SIGCHLD. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
20 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
21 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
22 |
function Threader_SigChld() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
23 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
24 |
global $threader_instances; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
25 |
foreach ( $threader_instances as &$mt ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
26 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
27 |
if ( is_object($mt) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
28 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
29 |
$mt->event_sigchld(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
30 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
31 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
32 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
33 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
34 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
35 |
* Global signal handler for SIGUSR2. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
36 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
37 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
38 |
function Threader_SigUsr2() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
39 |
{ |
68
32f6e2ee15ab
WebServer: fixed longstanding non-security fwrite buffer overflow bug
Dan
parents:
53
diff
changeset
|
40 |
global $threader_instances, $threader_notick; |
32f6e2ee15ab
WebServer: fixed longstanding non-security fwrite buffer overflow bug
Dan
parents:
53
diff
changeset
|
41 |
if ( @$threader_notick ) |
32f6e2ee15ab
WebServer: fixed longstanding non-security fwrite buffer overflow bug
Dan
parents:
53
diff
changeset
|
42 |
return; |
48
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
43 |
foreach ( $threader_instances as &$mt ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
44 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
45 |
if ( is_object($mt) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
46 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
47 |
$mt->event_sigusr2(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
48 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
49 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
50 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
51 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
52 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
53 |
* List of Threader instances. Needed for global handling of signals. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
54 |
* @var array |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
55 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
56 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
57 |
global $threader_instances; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
58 |
$threader_instances = array(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
59 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
60 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
61 |
* Tools for emulating multi-threaded operation in PHP scripts. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
62 |
* @package Amarok |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
63 |
* @subpackage WebControl |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
64 |
* @author Dan Fuhry |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
65 |
* @license GNU General Public License <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html> |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
66 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
67 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
68 |
class Threader |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
69 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
70 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
71 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
72 |
* Return value of fork() if the process is a child. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
73 |
* @const int |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
74 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
75 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
76 |
const FORK_CHILD = -1; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
77 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
78 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
79 |
* Set to true if this is a child process. No exceptions. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
80 |
* @var bool |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
81 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
82 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
83 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
84 |
private $is_child = false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
85 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
86 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
87 |
* Sockets for inter-process communication. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
88 |
* @var array |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
89 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
90 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
91 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
92 |
protected $ipc_sockets = array(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
93 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
94 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
95 |
* Socket for communication with the parent. Obviously only used after calling fork(). |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
96 |
* @var resource |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
97 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
98 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
99 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
100 |
protected $parent_sock = false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
101 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
102 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
103 |
* Services_JSON instance. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
104 |
* @var object |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
105 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
106 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
107 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
108 |
protected $json = false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
109 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
110 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
111 |
* PID of the parent process. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
112 |
* @var int |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
113 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
114 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
115 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
116 |
protected $parent_pid = 1; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
117 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
118 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
119 |
* List of actions for IPC events. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
120 |
* @var array |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
121 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
122 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
123 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
124 |
protected $ipc_actions = array(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
125 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
126 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
127 |
* Constructor. Sets up signal handlers. Nothing to see here, move along. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
128 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
129 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
130 |
public function __construct() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
131 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
132 |
global $threader_instances; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
133 |
|
53 | 134 |
if ( function_exists('pcntl_signal') ) |
135 |
{ |
|
136 |
declare(ticks=1); |
|
137 |
||
138 |
$threader_instances[] =& $this; |
|
139 |
||
68
32f6e2ee15ab
WebServer: fixed longstanding non-security fwrite buffer overflow bug
Dan
parents:
53
diff
changeset
|
140 |
pcntl_signal(SIGCHLD, 'Threader_SigChld'); |
53 | 141 |
pcntl_signal(SIGUSR2, 'Threader_SigUsr2'); |
142 |
} |
|
48
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
143 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
144 |
$this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
145 |
$this->parent_pid = getmypid(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
146 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
147 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
148 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
149 |
* Forks the current process. See your system's fork(2) man page for details. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
150 |
* @return int FORK_CHILD if child process, PID of child if parent process. Returns false on failure. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
151 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
152 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
153 |
public function fork() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
154 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
155 |
// create our new sockets for IPC |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
156 |
$socket_pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
157 |
// fork (emoticon of the day: --<E) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
158 |
$fork_result = pcntl_fork(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
159 |
if ( $fork_result == -1 ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
160 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
161 |
// fork failed. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
162 |
return false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
163 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
164 |
else if ( $fork_result ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
165 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
166 |
// we are the parent - register the child |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
167 |
fclose($socket_pair[0]); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
168 |
$this->ipc_sockets[$fork_result] = $socket_pair[1]; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
169 |
return $fork_result; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
170 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
171 |
else |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
172 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
173 |
// we are the child. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
174 |
fclose($socket_pair[1]); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
175 |
$this->parent_sock = $socket_pair[0]; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
176 |
$this->is_child = true; |
53 | 177 |
return self::FORK_CHILD; |
48
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
178 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
179 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
180 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
181 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
182 |
* Are we a child? |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
183 |
* @return bool |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
184 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
185 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
186 |
public function is_child() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
187 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
188 |
return $this->is_child; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
189 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
190 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
191 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
192 |
* Register an action so that when it is fired over IPC, a custom function can be called. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
193 |
* @param string Action |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
194 |
* @param callback Function to call |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
195 |
* @return true on success, false on failure |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
196 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
197 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
198 |
function ipc_register($action, $callback) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
199 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
200 |
if ( !is_string($action) || empty($action) || !is_callable($callback) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
201 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
202 |
return false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
203 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
204 |
$this->ipc_actions[$action] = $callback; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
205 |
return true; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
206 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
207 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
208 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
209 |
* Send through an IPC event. If this is a child, it only notifies the parent; if we're the parent, all children are notified. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
210 |
* @param array Data to be sent through. This must be an associative array containing an "action" key at minimum. If this a key "propagate" set to true, a parent that receives this will propagate the message to all children. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
211 |
* @return null |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
212 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
213 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
214 |
function ipc_send($data) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
215 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
216 |
if ( !isset($data['action']) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
217 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
218 |
return false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
219 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
220 |
$data = $this->json->encode($data); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
221 |
if ( $this->is_child() ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
222 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
223 |
fwrite($this->parent_sock, "$data\n"); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
224 |
// signal the parent that we've got an event |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
225 |
posix_kill($this->parent_pid, SIGUSR2); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
226 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
227 |
else |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
228 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
229 |
// signal each child |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
230 |
foreach ( $this->ipc_sockets as $pid => $socket ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
231 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
232 |
fwrite($socket, "$data\n"); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
233 |
posix_kill($pid, SIGUSR2); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
234 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
235 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
236 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
237 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
238 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
239 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
240 |
* Handler for SIGCHLD events. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
241 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
242 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
243 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
244 |
function event_sigchld() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
245 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
246 |
// this should never happen to children. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
247 |
if ( $this->is_child() ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
248 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
249 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
250 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
251 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
252 |
// wait for child to exit. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
253 |
pcntl_wait($status); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
254 |
// for each child PID, kill with signal 0 (effectively, test if process is alive) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
255 |
// if posix_kill fails, it's dead so remove it from the list. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
256 |
foreach ( $this->ipc_sockets as $pid => $socket ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
257 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
258 |
if ( !@posix_kill($pid, 0) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
259 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
260 |
// signal failed. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
261 |
fclose($socket); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
262 |
unset($this->ipc_sockets[$pid]); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
263 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
264 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
265 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
266 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
267 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
268 |
* Handler for IPC events. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
269 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
270 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
271 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
272 |
function event_sigusr2() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
273 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
274 |
if ( $this->is_child() ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
275 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
276 |
// this is easy - the parent sent the signal. |
68
32f6e2ee15ab
WebServer: fixed longstanding non-security fwrite buffer overflow bug
Dan
parents:
53
diff
changeset
|
277 |
@stream_set_blocking($this->parent_sock, 0); |
48
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
278 |
$command = rtrim(fgets($this->parent_sock, 102400), "\n"); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
279 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
280 |
else |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
281 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
282 |
// since we can't find which PID sent the signal, set the timeout to a very small amount |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
283 |
// of time and try to read; if we get something, awesome. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
284 |
foreach ( $this->ipc_sockets as $pid => $socket ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
285 |
{ |
68
32f6e2ee15ab
WebServer: fixed longstanding non-security fwrite buffer overflow bug
Dan
parents:
53
diff
changeset
|
286 |
@stream_set_blocking($socket, 0); |
48
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
287 |
$command = rtrim(@fgets($socket, 102400), "\n"); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
288 |
if ( !empty($command) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
289 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
290 |
break; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
291 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
292 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
293 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
294 |
if ( empty($command) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
295 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
296 |
// hmm, got a sigusr2 without an incoming command. oh well, ignore. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
297 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
298 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
299 |
$command = $this->json->decode($command); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
300 |
if ( !isset($command['action']) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
301 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
302 |
// no action = no way to figure out how to handle this. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
303 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
304 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
305 |
if ( !isset($this->ipc_actions[$command['action']]) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
306 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
307 |
// action not registered |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
308 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
309 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
310 |
// should we propagate this event? |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
311 |
if ( !$this->is_child() && isset($command['propagate']) && $command['propagate'] === true ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
312 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
313 |
$this->ipc_send($command); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
314 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
315 |
// we're good |
76
487a16c7117c
Considerably improved reliability of reboot code. Still producing respawn errors so currently disabled.
Dan
parents:
68
diff
changeset
|
316 |
call_user_func($this->ipc_actions[$command['action']], $command, $this); |
48
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
317 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
318 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
319 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
320 |
* Kills all child processes. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
321 |
* @access public |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
322 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
323 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
324 |
public function kill_all_children() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
325 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
326 |
foreach ( $this->ipc_sockets as $pid => $socket ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
327 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
328 |
$socklen = count($this->ipc_sockets); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
329 |
posix_kill($pid, SIGTERM); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
330 |
// wait until we are conscious of this child's death |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
331 |
while ( count($this->ipc_sockets) >= $socklen ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
332 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
333 |
usleep(20000); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
334 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
335 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
336 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
337 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
338 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
339 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
340 |
?> |