File inc/util.inc.php changed (mode: 100644) (index 90d0a0b..c0cf795) |
... |
... |
function rg_exec2_helper_init(&$info) |
1047 |
1047 |
{ |
{ |
1048 |
1048 |
$info['last_activity'] = 0; |
$info['last_activity'] = 0; |
1049 |
1049 |
$info['last_errmsg'] = ''; |
$info['last_errmsg'] = ''; |
1050 |
|
$info['closed'] = array(0 => 0, 1 => 0, 2 => 0); |
|
1051 |
1050 |
$info['start_ts'] = time(); |
$info['start_ts'] = time(); |
1052 |
1051 |
$info['last_failure'] = 0; |
$info['last_failure'] = 0; |
1053 |
1052 |
if (!isset($info['out_buf'])) |
if (!isset($info['out_buf'])) |
|
... |
... |
function rg_exec2_helper_init(&$info) |
1055 |
1054 |
$info['in_buf'] = ''; |
$info['in_buf'] = ''; |
1056 |
1055 |
$info['err_buf'] = ''; |
$info['err_buf'] = ''; |
1057 |
1056 |
$info['done'] = FALSE; |
$info['done'] = FALSE; |
|
1057 |
|
$info['started'] = 0; |
1058 |
1058 |
|
|
1059 |
1059 |
if (!isset($info['idle_time'])) |
if (!isset($info['idle_time'])) |
1060 |
1060 |
$info['idle_time'] = 5; |
$info['idle_time'] = 5; |
1061 |
1061 |
|
|
1062 |
|
if (!isset($info['output_done'])) { |
|
1063 |
|
if (isset($info['input_fd'])) |
|
1064 |
|
$info['output_done'] = 0; |
|
1065 |
|
else |
|
1066 |
|
$info['output_done'] = 1; |
|
1067 |
|
} |
|
|
1062 |
|
if (isset($info['input_fd'])) |
|
1063 |
|
$info['input_fd_done'] = 0; |
|
1064 |
|
else |
|
1065 |
|
$info['input_fd_done'] = 1; |
|
1066 |
|
|
|
1067 |
|
if (!isset($info['output_done'])) |
|
1068 |
|
$info['output_done'] = 1; |
1068 |
1069 |
} |
} |
1069 |
1070 |
|
|
1070 |
1071 |
/* |
/* |
1071 |
1072 |
* Helper for rg_exec to close a stream |
* Helper for rg_exec to close a stream |
1072 |
1073 |
*/ |
*/ |
1073 |
|
function rg_exec2_helper_close_stream($fd, $index, &$info, $stream) |
|
|
1074 |
|
function rg_exec2_helper_close_stream($index, $fd, &$info, $stream) |
1074 |
1075 |
{ |
{ |
1075 |
1076 |
global $rg_util_debug; |
global $rg_util_debug; |
1076 |
1077 |
|
|
|
... |
... |
function rg_exec2_helper_close_stream($fd, $index, &$info, $stream) |
1078 |
1079 |
rg_log_enter($index . ' DEBUG: exec2_helper_close_stream: stream ' . $stream); |
rg_log_enter($index . ' DEBUG: exec2_helper_close_stream: stream ' . $stream); |
1079 |
1080 |
|
|
1080 |
1081 |
if ($stream == -1) { |
if ($stream == -1) { |
1081 |
|
$info['output_done'] = 1; |
|
|
1082 |
|
$info['input_fd_done'] = 1; |
1082 |
1083 |
unset($info['input_fd']); |
unset($info['input_fd']); |
1083 |
1084 |
} else { |
} else { |
1084 |
|
$info['closed'][$stream] = 1; |
|
1085 |
1085 |
unset($info['pipes'][$stream]); |
unset($info['pipes'][$stream]); |
1086 |
1086 |
} |
} |
1087 |
1087 |
|
|
|
... |
... |
function rg_exec2_helper_close_stream($fd, $index, &$info, $stream) |
1094 |
1094 |
$rg_util_debug && |
$rg_util_debug && |
1095 |
1095 |
rg_log($index . ' DEBUG: closing stream 0 =>' |
rg_log($index . ' DEBUG: closing stream 0 =>' |
1096 |
1096 |
. ' closing input_fd ' . $info['input_fd']); |
. ' closing input_fd ' . $info['input_fd']); |
1097 |
|
rg_exec2_helper_close_stream($info['input_fd'], $index, $info, -1); |
|
|
1097 |
|
rg_exec2_helper_close_stream($index, $info['input_fd'], $info, -1); |
1098 |
1098 |
} |
} |
1099 |
1099 |
|
|
1100 |
1100 |
$rg_util_debug && |
$rg_util_debug && |
|
... |
... |
function rg_exec2($a) |
1122 |
1122 |
// some status initialization |
// some status initialization |
1123 |
1123 |
$s = array(); |
$s = array(); |
1124 |
1124 |
foreach ($ret['cmds'] as $cmd => &$info) { |
foreach ($ret['cmds'] as $cmd => &$info) { |
1125 |
|
$info['needs_start'] = TRUE; |
|
1126 |
1125 |
$info['stopped'] = TRUE; |
$info['stopped'] = TRUE; |
1127 |
|
$info['needs_stop'] = FALSE; |
|
|
1126 |
|
$info['wait_for_stop'] = FALSE; |
1128 |
1127 |
rg_exec2_helper_init($info); |
rg_exec2_helper_init($info); |
1129 |
1128 |
} |
} |
1130 |
1129 |
|
|
|
... |
... |
function rg_exec2($a) |
1135 |
1134 |
$lut = array(); |
$lut = array(); |
1136 |
1135 |
$done = TRUE; |
$done = TRUE; |
1137 |
1136 |
foreach ($ret['cmds'] as $index => &$info) { |
foreach ($ret['cmds'] as $index => &$info) { |
1138 |
|
if ($info['closed'][0] + $info['closed'][1] + $info['closed'][2] == 3) { |
|
|
1137 |
|
if ($info['done']) |
|
1138 |
|
continue; |
|
1139 |
|
|
|
1140 |
|
if ($info['started'] == 0) { |
|
1141 |
|
// nothing |
|
1142 |
|
} else if (empty($info['pipes'])) { |
1139 |
1143 |
$rg_util_debug && |
$rg_util_debug && |
1140 |
1144 |
rg_log($index . ' DEBUG: All streams are closed.' |
rg_log($index . ' DEBUG: All streams are closed.' |
1141 |
|
. ' Set needs_stop to TRUE'); |
|
1142 |
|
$info['needs_stop'] = TRUE; |
|
|
1145 |
|
. ' Set wait_for_stop to TRUE'); |
|
1146 |
|
$info['wait_for_stop'] = TRUE; |
|
1147 |
|
} else { |
|
1148 |
|
$rg_util_debug && |
|
1149 |
|
rg_log_ml($index . ' DEBUG: Cannot set wait_for_stop to' |
|
1150 |
|
. ' true because something is not closed; pipes:' |
|
1151 |
|
. ' ' . rg_array2string($info['pipes'])); |
1143 |
1152 |
} |
} |
1144 |
1153 |
|
|
1145 |
|
if ($info['needs_stop'] == TRUE) { |
|
|
1154 |
|
if ($info['wait_for_stop'] == TRUE) { |
1146 |
1155 |
$rg_util_debug && |
$rg_util_debug && |
1147 |
1156 |
rg_log($index . ' DEBUG: needs stop is TRUE!'); |
rg_log($index . ' DEBUG: needs stop is TRUE!'); |
1148 |
1157 |
|
|
1149 |
|
$info['ps'] = @proc_get_status($info['a']); |
|
|
1158 |
|
for ($i = 0; $i <= 2; $i++) |
|
1159 |
|
if (isset($info['pipes'][$i])) |
|
1160 |
|
fclose($info['pipes'][$i]); |
|
1161 |
|
|
|
1162 |
|
if (isset($info['input_fd'])) { |
|
1163 |
|
@fclose($info['input_fd']); |
|
1164 |
|
unset($info['input_fd']); |
|
1165 |
|
} |
|
1166 |
|
|
|
1167 |
|
$info['ps'] = proc_get_status($info['a']); |
1150 |
1168 |
$rg_util_debug && |
$rg_util_debug && |
1151 |
1169 |
rg_log_ml($index . ' DEBUG: info[ps]: ' |
rg_log_ml($index . ' DEBUG: info[ps]: ' |
1152 |
1170 |
. print_r($info['ps'], TRUE)); |
. print_r($info['ps'], TRUE)); |
1153 |
1171 |
|
|
1154 |
|
if ($info['ps']['running'] === FALSE) { |
|
1155 |
|
$ec = $info['ps']['exitcode']; |
|
1156 |
|
} else { |
|
1157 |
|
for ($i = 0; $i < 3; $i++) |
|
1158 |
|
if (isset($info['pipes'][$i])) |
|
1159 |
|
fclose($info['pipes'][$i]); |
|
1160 |
|
|
|
1161 |
|
if (isset($info['input_fd'])) { |
|
1162 |
|
@fclose($info['input_fd']); |
|
1163 |
|
unset($info['input_fd']); |
|
1164 |
|
} |
|
1165 |
|
|
|
1166 |
|
$err = @proc_close($info['a']); |
|
1167 |
|
if ($err != 0) { |
|
1168 |
|
$rg_util_debug && |
|
1169 |
|
rg_log($index . ' DEBUG: proc_close returned ' . $err); |
|
1170 |
|
} |
|
1171 |
|
// TODO: where should we use it? |
|
1172 |
|
$ec = 0; /* we are force closing program */ |
|
|
1172 |
|
if ($info['ps']['running'] !== FALSE) { |
|
1173 |
|
$done = FALSE; |
|
1174 |
|
continue; |
1173 |
1175 |
} |
} |
1174 |
1176 |
|
|
1175 |
|
if (($info['ps']['exitcode'] != 0) && empty($info['last_errmsg'])) |
|
1176 |
|
$info['last_errmsg'] = 'child exited with an error code'; |
|
|
1177 |
|
@proc_close($info['a']); |
|
1178 |
|
$info['exitcode'] = $info['ps']['exitcode']; |
|
1179 |
|
|
|
1180 |
|
if (($info['exitcode'] != 0) && empty($info['last_errmsg'])) |
|
1181 |
|
$info['last_errmsg'] = 'child exited with' |
|
1182 |
|
. ' error code ' . $info['exitcode']; |
1177 |
1183 |
|
|
1178 |
1184 |
if (isset($info['cb_finish'])) |
if (isset($info['cb_finish'])) |
1179 |
|
$info['cb_finish']($index, $info, $ec); |
|
|
1185 |
|
$info['cb_finish']($index, $info, $info['exitcode']); |
1180 |
1186 |
|
|
1181 |
1187 |
unset($info['pipes']); |
unset($info['pipes']); |
1182 |
1188 |
unset($info['a']); |
unset($info['a']); |
1183 |
1189 |
|
|
1184 |
|
$info['needs_stop'] = FALSE; |
|
|
1190 |
|
$info['wait_for_stop'] = FALSE; |
1185 |
1191 |
$info['stopped'] = TRUE; |
$info['stopped'] = TRUE; |
1186 |
1192 |
|
|
1187 |
|
if (!isset($info['restart_delay'])) { |
|
|
1193 |
|
if (isset($info['restart_delay'])) { |
|
1194 |
|
rg_exec2_helper_init($info); |
|
1195 |
|
} else { |
1188 |
1196 |
$rg_util_debug && |
$rg_util_debug && |
1189 |
1197 |
rg_log($index . ' DEBUG: no restart flag => done = TRUE'); |
rg_log($index . ' DEBUG: no restart flag => done = TRUE'); |
1190 |
1198 |
$info['done'] = TRUE; |
$info['done'] = TRUE; |
1191 |
|
} else { |
|
1192 |
|
rg_exec2_helper_init($info); |
|
1193 |
1199 |
} |
} |
1194 |
1200 |
} |
} |
1195 |
1201 |
|
|
|
... |
... |
function rg_exec2($a) |
1203 |
1209 |
rg_log($index . ' DEBUG: set done to FALSE'); |
rg_log($index . ' DEBUG: set done to FALSE'); |
1204 |
1210 |
$done = FALSE; |
$done = FALSE; |
1205 |
1211 |
|
|
1206 |
|
if ($info['needs_start'] == TRUE) { |
|
|
1212 |
|
if ($info['started'] == 0) { |
1207 |
1213 |
$restart_delay = isset($info['restart_delay']) ? $info['restart_delay'] : 0; |
$restart_delay = isset($info['restart_delay']) ? $info['restart_delay'] : 0; |
1208 |
1214 |
|
|
1209 |
1215 |
if ($info['last_failure'] + $restart_delay > $now) |
if ($info['last_failure'] + $restart_delay > $now) |
|
... |
... |
function rg_exec2($a) |
1231 |
1237 |
. rg_array2string($info['pipes'])); |
. rg_array2string($info['pipes'])); |
1232 |
1238 |
|
|
1233 |
1239 |
$info['stopped'] = FALSE; |
$info['stopped'] = FALSE; |
1234 |
|
$info['needs_start'] = FALSE; |
|
|
1240 |
|
$info['started'] = time(); |
1235 |
1241 |
|
|
1236 |
1242 |
// cb_output can populate $info['out_buf'] |
// cb_output can populate $info['out_buf'] |
1237 |
1243 |
if (isset($info['cb_output'])) { |
if (isset($info['cb_output'])) { |
|
... |
... |
function rg_exec2($a) |
1261 |
1267 |
|
|
1262 |
1268 |
if (!empty($info['out_buf'])) { |
if (!empty($info['out_buf'])) { |
1263 |
1269 |
$rg_util_debug && |
$rg_util_debug && |
1264 |
|
rg_log($index . ' DEBUG: enable write notification!'); |
|
|
1270 |
|
rg_log($index . ' DEBUG: out_buf not empty =>' |
|
1271 |
|
. ' enable write notification!'); |
1265 |
1272 |
|
|
1266 |
1273 |
$wx[] = $info['pipes'][0]; |
$wx[] = $info['pipes'][0]; |
1267 |
|
} else if (($info['output_done'] == 1) && ($info['closed'][0] == 0)) { |
|
|
1274 |
|
} else if (($info['input_fd_done'] == 1) |
|
1275 |
|
&& ($info['output_done'] == 1) |
|
1276 |
|
&& (isset($info['pipes'][0]))) { |
1268 |
1277 |
$rg_util_debug && |
$rg_util_debug && |
1269 |
|
rg_log($index . ' DEBUG: No output available' |
|
1270 |
|
. ', close output ' . $info['pipes'][0]); |
|
1271 |
|
rg_exec2_helper_close_stream($info['pipes'][0], $index, $info, 0); |
|
|
1278 |
|
rg_log($index . ' DEBUG: empty(out_buf)' |
|
1279 |
|
. ' && input_fd_done == 1' |
|
1280 |
|
. ' && output_done == 1' |
|
1281 |
|
. ': close output ' . $info['pipes'][0]); |
|
1282 |
|
rg_exec2_helper_close_stream($index, |
|
1283 |
|
$info['pipes'][0], $info, 0); |
1272 |
1284 |
} |
} |
1273 |
1285 |
|
|
1274 |
|
if ($info['closed'][1] == 0) $rx[] = $info['pipes'][1]; |
|
1275 |
|
if ($info['closed'][2] == 0) $rx[] = $info['pipes'][2]; |
|
1276 |
|
if (isset($info['input_fd'])) |
|
1277 |
|
$rx[] = $info['input_fd']; |
|
|
1286 |
|
if (isset($info['pipes'][1])) $rx[] = $info['pipes'][1]; |
|
1287 |
|
if (isset($info['pipes'][2])) $rx[] = $info['pipes'][2]; |
|
1288 |
|
if (isset($info['input_fd'])) $rx[] = $info['input_fd']; |
1278 |
1289 |
|
|
1279 |
1290 |
for ($i = 0; $i <= 2; $i++) { |
for ($i = 0; $i <= 2; $i++) { |
1280 |
1291 |
if (!isset($info['pipes'][$i])) |
if (!isset($info['pipes'][$i])) |
|
... |
... |
function rg_exec2($a) |
1311 |
1322 |
$rg_util_debug && rg_log('DEBUG: before stream_select:' |
$rg_util_debug && rg_log('DEBUG: before stream_select:' |
1312 |
1323 |
. (empty($revents) ? '' : ' revents:' . rg_array2string($revents)) |
. (empty($revents) ? '' : ' revents:' . rg_array2string($revents)) |
1313 |
1324 |
. (empty($wevents) ? '' : ' wevents:' . rg_array2string($wevents))); |
. (empty($wevents) ? '' : ' wevents:' . rg_array2string($wevents))); |
1314 |
|
$r = stream_select($revents, $wevents, $ex, 1, 0); |
|
|
1325 |
|
$r = stream_select($revents, $wevents, $ex, 0, 100 * 1000); |
1315 |
1326 |
if ($r === FALSE) { |
if ($r === FALSE) { |
1316 |
1327 |
$ret['errmsg'] = "cannot select"; |
$ret['errmsg'] = "cannot select"; |
1317 |
1328 |
break; |
break; |
|
... |
... |
function rg_exec2($a) |
1360 |
1371 |
$info['cb_error']($index, $info, $info['last_errmsg']); |
$info['cb_error']($index, $info, $info['last_errmsg']); |
1361 |
1372 |
else |
else |
1362 |
1373 |
rg_log($index . ' fwrite returned FALSE'); |
rg_log($index . ' fwrite returned FALSE'); |
1363 |
|
$info['needs_stop'] = TRUE; |
|
|
1374 |
|
$info['wait_for_stop'] = TRUE; |
1364 |
1375 |
continue; |
continue; |
1365 |
1376 |
} |
} |
1366 |
1377 |
$rg_util_debug && |
$rg_util_debug && |
|
... |
... |
function rg_exec2($a) |
1385 |
1396 |
$info['last_errmsg'] = 'cannot read'; |
$info['last_errmsg'] = 'cannot read'; |
1386 |
1397 |
if (isset($info['cb_error'])) |
if (isset($info['cb_error'])) |
1387 |
1398 |
$info['cb_error']($index, $info, $info['last_errmsg']); |
$info['cb_error']($index, $info, $info['last_errmsg']); |
1388 |
|
$info['needs_stop'] = TRUE; |
|
|
1399 |
|
$info['wait_for_stop'] = TRUE; |
1389 |
1400 |
continue; |
continue; |
1390 |
1401 |
} |
} |
1391 |
1402 |
|
|
1392 |
1403 |
if (empty($_d)) { |
if (empty($_d)) { |
1393 |
1404 |
$rg_util_debug && |
$rg_util_debug && |
1394 |
1405 |
rg_log($index . ' DEBUG: ifd ' . $ifd . ' returned no data.'); |
rg_log($index . ' DEBUG: ifd ' . $ifd . ' returned no data.'); |
1395 |
|
rg_exec2_helper_close_stream($fd, $index, $info, $stream); |
|
|
1406 |
|
rg_exec2_helper_close_stream($index, $fd, $info, $stream); |
1396 |
1407 |
continue; |
continue; |
1397 |
1408 |
} |
} |
1398 |
1409 |
|
|
|
... |
... |
function rg_exec2($a) |
1412 |
1423 |
$info['cb_input']($index, $info, $stream); |
$info['cb_input']($index, $info, $stream); |
1413 |
1424 |
} |
} |
1414 |
1425 |
|
|
1415 |
|
foreach ($ret['cmds'] as $index => &$info) |
|
1416 |
|
if (isset($info['cb_tick'])) |
|
1417 |
|
$info['cb_tick']($index, $info); |
|
|
1426 |
|
foreach ($ret['cmds'] as $index => &$info) { |
|
1427 |
|
if (!isset($info['cb_tick'])) |
|
1428 |
|
continue; |
|
1429 |
|
|
|
1430 |
|
if ($info['done']) |
|
1431 |
|
continue; |
|
1432 |
|
|
|
1433 |
|
$info['cb_tick']($index, $info); |
|
1434 |
|
} |
1418 |
1435 |
} |
} |
1419 |
1436 |
|
|
1420 |
1437 |
rg_log_exit(); |
rg_log_exit(); |
|
... |
... |
function rg_exec($cmd, $input, $cb_stdin, $cb_stdout, $cb_stderr) |
1497 |
1514 |
break; |
break; |
1498 |
1515 |
} |
} |
1499 |
1516 |
|
|
1500 |
|
$ret['code'] = $r['cmds']['cmd1']['ps']['exitcode']; |
|
|
1517 |
|
$ret['code'] = $r['cmds']['cmd1']['exitcode']; |
1501 |
1518 |
$ret['data'] = $r['cmds']['cmd1']['in_buf']; |
$ret['data'] = $r['cmds']['cmd1']['in_buf']; |
1502 |
1519 |
$ret['stderr'] = $r['cmds']['cmd1']['err_buf']; |
$ret['stderr'] = $r['cmds']['cmd1']['err_buf']; |
1503 |
1520 |
$ret['errmsg'] = $r['cmds']['cmd1']['last_errmsg']; |
$ret['errmsg'] = $r['cmds']['cmd1']['last_errmsg']; |