| 368 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
|
|
6 |
Copyright (C) 2014~2026 MIN ZHI, CHEN
|
|
|
7 |
|
|
|
8 |
This file is part of QBPWCF.
|
|
|
9 |
|
|
|
10 |
QBPWCF is free software: you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU General Public License as published by
|
|
|
12 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
QBPWCF is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU General Public License
|
|
|
21 |
along with QBPWCF. If not, see <http://www.gnu.org/licenses/>.
|
|
|
22 |
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
#使用命名空間qbpwcf
|
|
|
26 |
namespace qbpwcf;
|
|
|
27 |
|
|
|
28 |
#assets dir
|
|
|
29 |
$assetsDir="assets of resource::server_20260604-1";
|
|
|
30 |
|
|
|
31 |
#server 端的指令
|
|
|
32 |
$server_cmd=array("php -f server.php");
|
|
|
33 |
|
|
|
34 |
#clent 端的指令
|
|
|
35 |
$client_cmd=array("php -f client.php");
|
|
|
36 |
|
|
|
37 |
#working dir
|
|
|
38 |
$workingDir=pathinfo(__FILE__)["dirname"]."/../".$assetsDir;
|
|
|
39 |
|
|
|
40 |
#server端log的開頭識別
|
|
|
41 |
$serverLogHead="server:";
|
|
|
42 |
|
|
|
43 |
#client端log的開頭識別
|
|
|
44 |
$clientLogHead="client:";
|
|
|
45 |
|
|
|
46 |
#先執行 server 端,再執行 client 端.
|
|
|
47 |
|
|
|
48 |
#運行server端程式
|
|
|
49 |
#函式說明:
|
|
|
50 |
#透過proc來多執行序運作.
|
|
|
51 |
#回傳結果:
|
|
|
52 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
53 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
54 |
#$result["function"],當前執行的函式名稱.
|
|
|
55 |
#$result["argu"],使用的參數.
|
|
|
56 |
#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,若為整數0,則代表順利執行結束,可透過proc_update函式進行資訊的更新與取得;key為"content"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
|
|
|
57 |
#必填參數:
|
|
|
58 |
#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
|
|
|
59 |
$conf["cmds"]=$server_cmd;
|
|
|
60 |
#可省略參數:
|
|
|
61 |
#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
|
|
|
62 |
$conf["wait"]="false";
|
|
|
63 |
#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
|
|
|
64 |
#$conf["timeout"]=array("10");
|
|
|
65 |
#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
|
|
|
66 |
$conf["workingDir"]=array($workingDir);
|
|
|
67 |
#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
|
|
|
68 |
#$conf["envs"]=array(array("key"=>"value"));
|
|
|
69 |
#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
|
|
|
70 |
#$conf["executeBy"]=array("bash");
|
|
|
71 |
#參考資料:
|
|
|
72 |
#https://www.php.net/manual/en/function.proc-open.php
|
|
|
73 |
#https://www.php.net/manual/en/function.proc-get-status.php
|
|
|
74 |
#https://www.php.net/manual/en/function.proc-terminate.php
|
|
|
75 |
#備註:
|
|
|
76 |
#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.
|
|
|
77 |
#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
|
|
|
78 |
$proc_s=threads::proc($conf);
|
|
|
79 |
unset($conf);
|
|
|
80 |
|
|
|
81 |
#如果執行異常
|
|
|
82 |
if($proc_s["status"]==="false"){
|
|
|
83 |
|
|
|
84 |
#debug
|
|
|
85 |
var_dump($proc_s);
|
|
|
86 |
|
|
|
87 |
#結束執行,回傳shell 1.
|
|
|
88 |
exit(1);
|
|
|
89 |
|
|
|
90 |
}#if end
|
|
|
91 |
|
|
|
92 |
#如果程序不是持續運行中
|
|
|
93 |
if($proc_s["content"][0]["statusCode"]!=="?"){
|
|
|
94 |
|
|
|
95 |
#debug
|
|
|
96 |
var_dump($proc_s);
|
|
|
97 |
|
|
|
98 |
#結束執行,回傳shell 1.
|
|
|
99 |
exit(1);
|
|
|
100 |
|
|
|
101 |
}#if end
|
|
|
102 |
|
|
|
103 |
#取得 server.php 的標準輸出
|
|
|
104 |
$serverStdOut=&$proc_s["content"][0]["content"];
|
|
|
105 |
|
|
|
106 |
#取得 server.php 的錯誤輸出
|
|
|
107 |
$serverErrOut=&$proc_s["content"][0]["error"];
|
|
|
108 |
|
|
|
109 |
#提示 server 運行中且提供 pid
|
|
|
110 |
echo $serverLogHead."server(pid:".$proc_s["content"][0]["proc_get_status"]["pid"].") is running....".PHP_EOL;
|
|
|
111 |
|
|
|
112 |
#debug
|
|
|
113 |
#var_dump(__LINE__,$serverStdOut,$serverErrOut);exit;
|
|
|
114 |
|
|
|
115 |
#運行client端程式
|
|
|
116 |
#函式說明:
|
|
|
117 |
#透過proc來多執行序運作.
|
|
|
118 |
#回傳結果:
|
|
|
119 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
120 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
121 |
#$result["function"],當前執行的函式名稱.
|
|
|
122 |
#$result["argu"],使用的參數.
|
|
|
123 |
#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,若為整數0,則代表順利執行結束,可透過proc_update函式進行資訊的更新與取得;key為"content"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
|
|
|
124 |
#必填參數:
|
|
|
125 |
#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
|
|
|
126 |
$conf["cmds"]=$client_cmd;
|
|
|
127 |
#可省略參數:
|
|
|
128 |
#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
|
|
|
129 |
$conf["wait"]="false";
|
|
|
130 |
#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
|
|
|
131 |
#$conf["timeout"]=array("10");
|
|
|
132 |
#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
|
|
|
133 |
$conf["workingDir"]=array($workingDir);
|
|
|
134 |
#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
|
|
|
135 |
#$conf["envs"]=array(array("key"=>"value"));
|
|
|
136 |
#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
|
|
|
137 |
#$conf["executeBy"]=array("bash");
|
|
|
138 |
#參考資料:
|
|
|
139 |
#https://www.php.net/manual/en/function.proc-open.php
|
|
|
140 |
#https://www.php.net/manual/en/function.proc-get-status.php
|
|
|
141 |
#https://www.php.net/manual/en/function.proc-terminate.php
|
|
|
142 |
#備註:
|
|
|
143 |
#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.
|
|
|
144 |
#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
|
|
|
145 |
$proc_c=threads::proc($conf);
|
|
|
146 |
unset($conf);
|
|
|
147 |
|
|
|
148 |
#如果執行異常
|
|
|
149 |
if($proc_c["status"]==="false"){
|
|
|
150 |
|
|
|
151 |
#debug
|
|
|
152 |
var_dump($proc_c);
|
|
|
153 |
|
|
|
154 |
#結束執行,回傳shell 1.
|
|
|
155 |
exit(1);
|
|
|
156 |
|
|
|
157 |
}#if end
|
|
|
158 |
|
|
|
159 |
#取得 client.php 的標準輸出
|
|
|
160 |
$clientStdOut=&$proc_c["content"][0]["content"];
|
|
|
161 |
|
|
|
162 |
#取得 client.php 的錯誤輸出
|
|
|
163 |
$clientErrOut=&$proc_c["content"][0]["error"];
|
|
|
164 |
|
|
|
165 |
#提示 client 運行中且提供 pid
|
|
|
166 |
echo $clientLogHead."client(pid:".$proc_c["content"][0]["proc_get_status"]["pid"].") is running....".PHP_EOL;
|
|
|
167 |
|
|
|
168 |
#用 stream_select 來取得兩個程序的輸出....
|
|
|
169 |
|
|
|
170 |
#給 stream select 的 read msg monitor
|
|
|
171 |
$stream_r=array("serverStdOut"=>$serverStdOut,"serverErrOut"=>$serverErrOut,"clientStdOut"=>$clientStdOut,"clientErrOut"=>$clientErrOut);
|
|
|
172 |
|
|
|
173 |
#debug
|
|
|
174 |
#var_dump(__LINE__,$stream_r);exit;
|
|
|
175 |
|
|
|
176 |
#複製一份,避免被 stream_select 異動到原始的 stream
|
|
|
177 |
$stream_r_select=$stream_r;
|
|
|
178 |
|
|
|
179 |
#給 stream select 的 write msg monitor
|
|
|
180 |
$stream_w=null;
|
|
|
181 |
|
|
|
182 |
#給 steam select 的 unexpect msg monitor
|
|
|
183 |
$stream_e=null;
|
|
|
184 |
|
|
|
185 |
#無窮迴圈
|
|
|
186 |
while(true){
|
|
|
187 |
|
|
|
188 |
#取得有異動的 stream
|
|
|
189 |
$EventCount=stream_select($stream_r_select,$stream_w,$stream_e,5);
|
|
|
190 |
|
|
|
191 |
#如果沒有異動的 stream
|
|
|
192 |
if($EventCount===0){
|
|
|
193 |
|
|
|
194 |
#用 "." 代表 idle,沒有異動.
|
|
|
195 |
echo ".";
|
|
|
196 |
|
|
|
197 |
}#if end
|
|
|
198 |
|
|
|
199 |
#如果有異動的 stream
|
|
|
200 |
if(in_array($stream_r_select,$stream_r)){
|
|
|
201 |
|
|
|
202 |
#針對每個 stream
|
|
|
203 |
foreach($stream_r_select as $key => $stream){
|
|
|
204 |
|
|
|
205 |
#debug
|
|
|
206 |
var_dump(__LINE__,$stream);
|
|
|
207 |
|
|
|
208 |
#如果有內容尚未讀取
|
|
|
209 |
while(!eof($stream)){
|
|
|
210 |
|
|
|
211 |
#取得訊息字串
|
|
|
212 |
$string=fread($stream,2048);
|
|
|
213 |
|
|
|
214 |
#$serverLogHead
|
|
|
215 |
#$clientLogHead
|
|
|
216 |
|
|
|
217 |
#判斷來源
|
|
|
218 |
switch($key){
|
|
|
219 |
|
|
|
220 |
#如果是伺服端的標準輸出
|
|
|
221 |
case "serverStdOut":
|
|
|
222 |
|
|
|
223 |
#如果是伺服端的錯誤輸出
|
|
|
224 |
case "serverErrOut":
|
|
|
225 |
|
|
|
226 |
#印出訊息
|
|
|
227 |
echo $serverLogHead.":".$string.PHP_EOL;
|
|
|
228 |
|
|
|
229 |
break;
|
|
|
230 |
|
|
|
231 |
#如果是用戶的標準士輸出
|
|
|
232 |
case "clientStdOut":
|
|
|
233 |
|
|
|
234 |
#如果是用戶的錯誤輸出
|
|
|
235 |
case "clientErrOut":
|
|
|
236 |
|
|
|
237 |
#印出訊息
|
|
|
238 |
echo $clientLogHead.":".$string.PHP_EOL;
|
|
|
239 |
|
|
|
240 |
break;
|
|
|
241 |
|
|
|
242 |
#其他不支援的 key
|
|
|
243 |
default:
|
|
|
244 |
|
|
|
245 |
#debug
|
|
|
246 |
echo "unsupported key:".$key.PHP_EOL;
|
|
|
247 |
|
|
|
248 |
#結束執行,回傳shell 1.
|
|
|
249 |
exit(1);
|
|
|
250 |
|
|
|
251 |
}#switch end
|
|
|
252 |
|
|
|
253 |
}#while end
|
|
|
254 |
|
|
|
255 |
}#foreach end
|
|
|
256 |
|
|
|
257 |
}#if end
|
|
|
258 |
|
|
|
259 |
}#while end
|
|
|
260 |
|
|
|
261 |
/*
|
|
|
262 |
|
|
|
263 |
#若程序運行中,就不斷執行
|
|
|
264 |
while($proc["content"][0]["statusCode"]==="?"){
|
|
|
265 |
|
|
|
266 |
#更新程序狀況
|
|
|
267 |
#函式說明:
|
|
|
268 |
#更新透過proc執行的多程序資訊.
|
|
|
269 |
#回傳結果:
|
|
|
270 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
271 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
272 |
#$result["function"],當前執行的函式名稱.
|
|
|
273 |
#$result["argu"],使用的參數.
|
|
|
274 |
#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,可透過proc_update函式進行資訊的更新與取得;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
|
|
|
275 |
#必填參數:
|
|
|
276 |
#$conf["procs"],陣列,運行self::proc後回傳的content.
|
|
|
277 |
$conf["procs"]=$proc["content"];
|
|
|
278 |
#可省略參數:
|
|
|
279 |
#無.
|
|
|
280 |
#參考資料:
|
|
|
281 |
#無.
|
|
|
282 |
#備註:
|
|
|
283 |
#無.
|
|
|
284 |
$proc_update=threads::proc_update($conf);
|
|
|
285 |
unset($conf);
|
|
|
286 |
|
|
|
287 |
#如果執行異常
|
|
|
288 |
if($proc_update["status"]==="false"){
|
|
|
289 |
|
|
|
290 |
#debug
|
|
|
291 |
var_dump($proc_update);
|
|
|
292 |
|
|
|
293 |
#結束執行,回傳shell 1.
|
|
|
294 |
exit(1);
|
|
|
295 |
|
|
|
296 |
}#if end
|
|
|
297 |
|
|
|
298 |
#更新程序狀態
|
|
|
299 |
$proc["content"]=$proc_update["content"];
|
|
|
300 |
|
|
|
301 |
}#if end
|
|
|
302 |
|
|
|
303 |
#如果client最終運行異常
|
|
|
304 |
if($proc["content"][0]["statusCode"]!==0){
|
|
|
305 |
|
|
|
306 |
#debug
|
|
|
307 |
var_dump($proc);
|
|
|
308 |
|
|
|
309 |
#結束執行,回傳shell 1.
|
|
|
310 |
exit(1);
|
|
|
311 |
|
|
|
312 |
}#if end
|
|
|
313 |
|
|
|
314 |
#debug
|
|
|
315 |
var_dump($proc);
|
|
|
316 |
|
|
|
317 |
*/
|
|
|
318 |
|
|
|
319 |
?>
|