com php-src: Block signals during fpm master initialization: sapi/fpm/fpm/fpm_main.c sapi/fpm/fpm/fpm_process_ctl .c sapi/fpm/fpm/fpm_signals.c
sapi/fpm/fpm/fpm_sign als.h
| From: | Jakub Zelenka | Date: | Sun, 20 Oct 2019 15:08:55 +0000 |
| Subject: | com php-src: Block signals during fpm master initialization: sapi/fpm/fpm/fpm_main.c sapi/fpm/fpm/fpm_process_ctl .c sapi/fpm/fpm/fpm_signals.c sapi/fpm/fpm/fpm_sign als.h |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-108969@lists.php.net to get a copy of this message | ||
Commit: ae5154c6c6af7ba7c592f8af006b7cadd0d66d6e
Author: Maksim Nikulin <mnikulin@plesk.com> Wed, 24 Jul 2019 16:50:57 +0700
Committer: Jakub Zelenka <bukka@php.net> Sun, 20 Oct 2019 16:08:55 +0100
Parents: 62b053a3be3e77186451a503f93287a6002b62a0
Branches: PHP-7.4
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=ae5154c6c6af7ba7c592f8af006b7cadd0d66d6e
Log:
Block signals during fpm master initialization
Fix PHP-FPM failure in the case of concurrent reload attempts.
Postpone signal delivery to the fpm master process till proper signal
handlers are set. Prevent the following case:
- Running master process receives
SIGUSR2 and performs execvp().
- Another SIGUSR2 is arrived before signal handlers are set.
- Master process dies.
- Requests to the HTTP server handled by PHP-fpm can not be served
any more.
Block some signals using sigprocmask() before execvp() and early
in the main() function. Unblock signals as soon as proper
handlers are set.
Fixes bug #74083
Bugs:
https://bugs.php.net/74083
Changed paths:
M sapi/fpm/fpm/fpm_main.c
M sapi/fpm/fpm/fpm_process_ctl.c
M sapi/fpm/fpm/fpm_signals.c
M sapi/fpm/fpm/fpm_signals.h
Diff:
diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c
index 9916b8bf7c8..f77eef6194c 100644
--- a/sapi/fpm/fpm/fpm_main.c
+++ b/sapi/fpm/fpm/fpm_main.c
@@ -86,6 +86,7 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
#include "fpm.h"
#include "fpm_request.h"
#include "fpm_status.h"
+#include "fpm_signals.h"
#include "fpm_conf.h"
#include "fpm_php.h"
#include "fpm_log.h"
@@ -1570,6 +1571,15 @@ int main(int argc, char *argv[])
closes it. in apache|apxs mode apache
does that for us! thies@thieso.net
20000419 */
+
+ /* Subset of signals from fpm_signals_init_main() to avoid unexpected death during early init
+ or during reload just after execvp() or fork */
+ int init_signal_array[] = { SIGUSR1, SIGUSR2, SIGCHLD };
+ if (0 > fpm_signals_init_mask(init_signal_array,
sizeof(init_signal_array)/sizeof(init_signal_array[0])) ||
+ 0 > fpm_signals_block()) {
+ zlog(ZLOG_WARNING, "Could die in the case of too early reload signal");
+ }
+ zlog(ZLOG_DEBUG, "Blocked some signals");
#endif
#ifdef ZTS
diff --git a/sapi/fpm/fpm/fpm_process_ctl.c b/sapi/fpm/fpm/fpm_process_ctl.c
index 17fbe8d3e08..2bc00178ea8 100644
--- a/sapi/fpm/fpm/fpm_process_ctl.c
+++ b/sapi/fpm/fpm/fpm_process_ctl.c
@@ -77,6 +77,10 @@ static void fpm_pctl_exit() /* {{{ */
static void fpm_pctl_exec() /* {{{ */
{
+ zlog(ZLOG_DEBUG, "Blocking some signals before reexec");
+ if (0 > fpm_signals_block()) {
+ zlog(ZLOG_WARNING, "concurrent reloads may be unstable");
+ }
zlog(ZLOG_NOTICE, "reloading: execvp(\"%s\", {\"%s\""
"%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
diff --git a/sapi/fpm/fpm/fpm_signals.c b/sapi/fpm/fpm/fpm_signals.c
index caf41e8a3f8..922d4b1a9c1 100644
--- a/sapi/fpm/fpm/fpm_signals.c
+++ b/sapi/fpm/fpm/fpm_signals.c
@@ -19,6 +19,7 @@
#include "zlog.h"
static int sp[2];
+static sigset_t block_sigset;
const char *fpm_signal_names[NSIG + 1] = {
#ifdef SIGHUP
@@ -210,6 +211,11 @@ int fpm_signals_init_main() /* {{{ */
zlog(ZLOG_SYSERROR, "failed to init signals: sigaction()");
return -1;
}
+
+ zlog(ZLOG_DEBUG, "Unblocking all signals");
+ if (0 > fpm_signals_unblock()) {
+ return -1;
+ }
return 0;
}
/* }}} */
@@ -250,3 +256,50 @@ int fpm_signals_get_fd() /* {{{ */
return sp[0];
}
/* }}} */
+
+int fpm_signals_init_mask(int *signum_array, size_t size) /* {{{ */
+{
+ size_t i = 0;
+ if (0 > sigemptyset(&block_sigset)) {
+ zlog(ZLOG_SYSERROR, "failed to prepare signal block mask: sigemptyset()");
+ return -1;
+ }
+ for (i = 0; i < size; ++i) {
+ int sig_i = signum_array[i];
+ if (0 > sigaddset(&block_sigset, sig_i)) {
+ if (sig_i <= NSIG && fpm_signal_names[sig_i] != NULL) {
+ zlog(ZLOG_SYSERROR, "failed to prepare signal block mask: sigaddset(%s)",
+ fpm_signal_names[sig_i]);
+ } else {
+ zlog(ZLOG_SYSERROR, "failed to prepare signal block mask: sigaddset(%d)", sig_i);
+ }
+ return -1;
+ }
+ }
+ return 0;
+}
+/* }}} */
+
+int fpm_signals_block() /* {{{ */
+{
+ if (0 > sigprocmask(SIG_BLOCK, &block_sigset, NULL)) {
+ zlog(ZLOG_SYSERROR, "failed to block signals");
+ return -1;
+ }
+ return 0;
+}
+/* }}} */
+
+int fpm_signals_unblock() /* {{{ */
+{
+ /* Ensure that during reload after upgrade all signals are unblocked.
+ block_sigset could have different value before execve() */
+ sigset_t all_signals;
+ sigfillset(&all_signals);
+ if (0 > sigprocmask(SIG_UNBLOCK, &all_signals, NULL)) {
+ zlog(ZLOG_SYSERROR, "failed to unblock signals");
+ return -1;
+ }
+ return 0;
+}
+/* }}} */
diff --git a/sapi/fpm/fpm/fpm_signals.h b/sapi/fpm/fpm/fpm_signals.h
index 72028162188..6ce7277cb21 100644
--- a/sapi/fpm/fpm/fpm_signals.h
+++ b/sapi/fpm/fpm/fpm_signals.h
@@ -8,6 +8,9 @@
int fpm_signals_init_main();
int fpm_signals_init_child();
int fpm_signals_get_fd();
+int fpm_signals_init_mask(int *signum_array, size_t size);
+int fpm_signals_block();
+int fpm_signals_unblock();
extern const char *fpm_signal_names[NSIG + 1];