From b7d4861c0c4d62be3b8927ad2cc4e066c9a03674 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 28 Oct 2022 15:58:58 +0800 Subject: [PATCH 1/6] update: sub exit function --- kms.php | 6 ++++-- src/Daemon.php | 24 ++++++++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/kms.php b/kms.php index c307f9c..4fc65e0 100755 --- a/kms.php +++ b/kms.php @@ -120,12 +120,14 @@ pcntl_signal(SIGCHLD, function() { // receive SIGCHLD signal pcntl_signal(SIGTERM, function() { // receive SIGTERM signal global $NGINX, $PHP_FPM, $VLMCSD; logging::info('Get SIGTERM -> exit'); - subExit($NGINX['pidFile'], $PHP_FPM['pidFile'], $VLMCSD['pidFile']); + subExit($NGINX, $PHP_FPM, $VLMCSD); + exit; }); pcntl_signal(SIGINT, function() { // receive SIGINT signal global $NGINX, $PHP_FPM, $VLMCSD; logging::info('Get SIGINT -> exit'); - subExit($NGINX['pidFile'], $PHP_FPM['pidFile'], $VLMCSD['pidFile']); + subExit($NGINX, $PHP_FPM, $VLMCSD); + exit; }); logging::info('Loading kms-server (' . $VERSION . ')'); diff --git a/src/Daemon.php b/src/Daemon.php index f223195..a34c63a 100644 --- a/src/Daemon.php +++ b/src/Daemon.php @@ -1,7 +1,5 @@ Date: Fri, 28 Oct 2022 16:03:54 +0800 Subject: [PATCH 2/6] update: loop for multi-sub exit --- kms.php | 4 +++- src/Daemon.php | 20 ++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/kms.php b/kms.php index 4fc65e0..2ac1327 100755 --- a/kms.php +++ b/kms.php @@ -1,7 +1,7 @@ #!/usr/bin/env php8 exit'); subExit($NGINX, $PHP_FPM, $VLMCSD); exit; }); + pcntl_signal(SIGINT, function() { // receive SIGINT signal global $NGINX, $PHP_FPM, $VLMCSD; logging::info('Get SIGINT -> exit'); diff --git a/src/Daemon.php b/src/Daemon.php index a34c63a..4900fce 100644 --- a/src/Daemon.php +++ b/src/Daemon.php @@ -48,20 +48,12 @@ function daemon(array $info): void { function subExit(array $nginx, array $phpFpm, array $vlmcsd): void { - $nginxName = $nginx['name']; - $nginxPid = getPid($nginx['pidFile']); - logging::info("Sending kill signal to $nginxName (PID = $nginxPid)"); - posix_kill($nginxPid, SIGTERM); - - $phpFpmName = $phpFpm['name']; - $phpFpmPid = getPid($phpFpm['pidFile']); - logging::info("Sending kill signal to $phpFpmName (PID = $phpFpmPid)"); - posix_kill($phpFpmPid, SIGTERM); - - $vlmcsdName = $vlmcsd['name']; - $vlmcsdPid = getPid($vlmcsd['pidFile']); - logging::info("Sending kill signal to $vlmcsdName (PID = $vlmcsdPid)"); - posix_kill($vlmcsdPid, SIGTERM); + foreach (array($nginx, $phpFpm, $vlmcsd) as $subFunc) { + $subName = $subFunc['name']; + $subPid = getPid($subFunc['pidFile']); + logging::info("Sending kill signal to $subName (PID = $subPid)"); + posix_kill($subPid, SIGTERM); + } logging::info('Waiting sub process exit...'); pcntl_wait($status); // wait all process exit From c8f3448c60ac5e8fbc7f6a779bcc0412387c66d1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 28 Oct 2022 16:10:58 +0800 Subject: [PATCH 3/6] update: variable-length argument lists --- src/Daemon.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Daemon.php b/src/Daemon.php index 4900fce..9e54443 100644 --- a/src/Daemon.php +++ b/src/Daemon.php @@ -46,16 +46,14 @@ function daemon(array $info): void { } } -function subExit(array $nginx, array $phpFpm, array $vlmcsd): void { - - foreach (array($nginx, $phpFpm, $vlmcsd) as $subFunc) { - $subName = $subFunc['name']; - $subPid = getPid($subFunc['pidFile']); +function subExit(array ...$subList): void { + foreach ($subList as $sub) { + $subName = $sub['name']; + $subPid = getPid($sub['pidFile']); logging::info("Sending kill signal to $subName (PID = $subPid)"); posix_kill($subPid, SIGTERM); } - - logging::info('Waiting sub process exit...'); + logging::info('Waiting sub-process exit...'); pcntl_wait($status); // wait all process exit logging::info('All process exit, Goodbye!'); } From 74ed708a0645654ca9a302471d3d00b0880cbb59 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 28 Oct 2022 16:18:18 +0800 Subject: [PATCH 4/6] feat: support `SIGQUIT` signal --- kms.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/kms.php b/kms.php index 2ac1327..601849c 100755 --- a/kms.php +++ b/kms.php @@ -103,6 +103,7 @@ function load_params(): void { } } +# TODO: add disable http option function start_process(): void { // start sub processes global $NGINX, $PHP_FPM, $VLMCSD; new Process($NGINX['command']); @@ -113,22 +114,32 @@ function start_process(): void { // start sub processes logging::info('Start vlmcsd server...OK'); } +# TODO: add disable http option +function exit_process(): void { + global $NGINX, $PHP_FPM, $VLMCSD; + subExit($NGINX, $PHP_FPM, $VLMCSD); +} + declare(ticks = 1); pcntl_signal(SIGCHLD, function() { // receive SIGCHLD signal pcntl_wait($status, WNOHANG); // avoid zombie process }); pcntl_signal(SIGTERM, function() { // receive SIGTERM signal - global $NGINX, $PHP_FPM, $VLMCSD; logging::info('Get SIGTERM -> exit'); - subExit($NGINX, $PHP_FPM, $VLMCSD); + exit_process(); exit; }); pcntl_signal(SIGINT, function() { // receive SIGINT signal - global $NGINX, $PHP_FPM, $VLMCSD; logging::info('Get SIGINT -> exit'); - subExit($NGINX, $PHP_FPM, $VLMCSD); + exit_process(); + exit; +}); + +pcntl_signal(SIGQUIT, function() { // receive SIGQUIT signal + logging::info('Get SIGQUIT -> exit'); + exit_process(); exit; }); From e5a52dda19d0489c987df953fd11201a4d726097 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 28 Oct 2022 16:28:49 +0800 Subject: [PATCH 5/6] feat: add http switch option --- kms.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/kms.php b/kms.php index 601849c..96befc1 100755 --- a/kms.php +++ b/kms.php @@ -9,6 +9,7 @@ require_once './src/Process.php'; $KMS_PORT = 1688; // kms expose port $HTTP_PORT = 1689; // http server port +$ENABLE_HTTP = true; // http interface $NGINX = array( // nginx process 'name' => 'nginx', @@ -103,21 +104,27 @@ function load_params(): void { } } -# TODO: add disable http option function start_process(): void { // start sub processes + global $ENABLE_HTTP; global $NGINX, $PHP_FPM, $VLMCSD; - new Process($NGINX['command']); - logging::info('Start nginx server...OK'); - new Process($PHP_FPM['command']); - logging::info('Start php-fpm server...OK'); + if ($ENABLE_HTTP) { // http server process + new Process($NGINX['command']); + logging::info('Start nginx server...OK'); + new Process($PHP_FPM['command']); + logging::info('Start php-fpm server...OK'); + } new Process($VLMCSD['command']); logging::info('Start vlmcsd server...OK'); } -# TODO: add disable http option -function exit_process(): void { +function exit_process(): void { // kill sub processes + global $ENABLE_HTTP; global $NGINX, $PHP_FPM, $VLMCSD; - subExit($NGINX, $PHP_FPM, $VLMCSD); + if ($ENABLE_HTTP) { + subExit($NGINX, $PHP_FPM, $VLMCSD); // with http server + } else { + subExit($VLMCSD); + } } declare(ticks = 1); @@ -154,7 +161,9 @@ while (true) { // start daemon for ($i = 0; $i < 500; $i++) { // sleep 5s msDelay(10); // return main loop every 10ms } - daemon($NGINX); - daemon($PHP_FPM); + if ($ENABLE_HTTP) { // with http server + daemon($NGINX); + daemon($PHP_FPM); + } daemon($VLMCSD); } From aa9f24818bc91a5c17fec7c8c1cecb3c4c64a9f9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 28 Oct 2022 16:34:30 +0800 Subject: [PATCH 6/6] feat: add `--disable-http` option --- kms.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/kms.php b/kms.php index 96befc1..c74befa 100755 --- a/kms.php +++ b/kms.php @@ -79,6 +79,12 @@ function load_params(): void { logging::$logLevel = logging::DEBUG; } + global $ENABLE_HTTP; + if (in_array('--disable-http', $argv)) { // disable http service + logging::warning('Disable http service'); + $ENABLE_HTTP = false; + } + global $KMS_PORT; $KMS_PORT = intval(get_param('--kms-port', strval($KMS_PORT))); if ($KMS_PORT < 1 || $KMS_PORT > 65535) { // 1 ~ 65535 @@ -121,7 +127,7 @@ function exit_process(): void { // kill sub processes global $ENABLE_HTTP; global $NGINX, $PHP_FPM, $VLMCSD; if ($ENABLE_HTTP) { - subExit($NGINX, $PHP_FPM, $VLMCSD); // with http server + subExit($NGINX, $PHP_FPM, $VLMCSD); // with http service } else { subExit($VLMCSD); } @@ -161,7 +167,7 @@ while (true) { // start daemon for ($i = 0; $i < 500; $i++) { // sleep 5s msDelay(10); // return main loop every 10ms } - if ($ENABLE_HTTP) { // with http server + if ($ENABLE_HTTP) { // with http service daemon($NGINX); daemon($PHP_FPM); }