Skip to content

Use PDEATHSIG to kill cli-server workers if parent exits #9476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sapi/cli/php_cli_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#include <signal.h>

#ifdef PHP_WIN32
# include <process.h>
Expand Down Expand Up @@ -49,6 +50,10 @@
#include <dlfcn.h>
#endif

#ifdef __linux__
# include <sys/prctl.h>
#endif

#include "SAPI.h"
#include "php.h"
#include "php_ini.h"
Expand Down Expand Up @@ -2432,6 +2437,20 @@ static char *php_cli_server_parse_addr(const char *addr, int *pport) {
return pestrndup(addr, end - addr, 1);
}

#ifdef __linux__
static void php_cli_server_worker_install_pdeathsig(void)
{
if (prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) {
// Ignore failure to register PDEATHSIG, it's not available on all platforms anyway
}

// Check if parent has exited just after the fork
if (getppid() != php_cli_server_master) {
exit(1);
}
}
#endif

static void php_cli_server_startup_workers(void) {
char *workers = getenv("PHP_CLI_SERVER_WORKERS");
if (!workers) {
Expand Down Expand Up @@ -2459,6 +2478,9 @@ static void php_cli_server_startup_workers(void) {
php_cli_server_worker + 1;
return;
} else if (pid == 0) {
#ifdef __linux__
php_cli_server_worker_install_pdeathsig();
#endif
return;
} else {
php_cli_server_workers[php_cli_server_worker] = pid;
Expand Down