From 4989e6e419ba5a43f73ba166772a7917978510af Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 1 Mar 2017 22:42:30 +0900 Subject: [PATCH] nghttpx: Don't call functions which are not async-signal-safe .. after fork but before execv in multithreaded process. --- src/shrpx_exec.cc | 13 +++++++------ src/shrpx_signal.cc | 23 +++++++++++------------ src/shrpx_signal.h | 8 ++++---- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/shrpx_exec.cc b/src/shrpx_exec.cc index a93ba3fb..09864dd8 100644 --- a/src/shrpx_exec.cc +++ b/src/shrpx_exec.cc @@ -76,14 +76,16 @@ int exec_read_command(Process &proc, char *const argv[]) { auto pid = fork(); if (pid == 0) { + // This is multithreaded program, and we are allowed to use only + // async-signal-safe functions here. + // child process shrpx_signal_unset_worker_proc_ign_handler(); rv = shrpx_signal_unblock_all(); if (rv != 0) { - auto error = errno; - LOG(FATAL) << "Unblocking all signals failed: errno=" << error; - + constexpr char msg[] = "Unblocking all signals failed\n"; + write(STDERR_FILENO, msg, str_size(msg)); nghttp2_Exit(EXIT_FAILURE); } @@ -92,9 +94,8 @@ int exec_read_command(Process &proc, char *const argv[]) { rv = execv(argv[0], argv); if (rv == -1) { - auto error = errno; - LOG(ERROR) << "Could not execute command: " << argv[0] - << ", execve() faild, errno=" << error; + constexpr char msg[] = "Could not execute command\n"; + write(STDERR_FILENO, msg, str_size(msg)); nghttp2_Exit(EXIT_FAILURE); } // unreachable diff --git a/src/shrpx_signal.cc b/src/shrpx_signal.cc index fcce2dcc..5e16bf3c 100644 --- a/src/shrpx_signal.cc +++ b/src/shrpx_signal.cc @@ -94,7 +94,7 @@ int shrpx_signal_set(sigset_t *set) { namespace { template -void signal_set_handler(void (*handler)(int), Signals &&sigs) { +int signal_set_handler(void (*handler)(int), Signals &&sigs) { struct sigaction act {}; act.sa_handler = handler; sigemptyset(&act.sa_mask); @@ -102,11 +102,10 @@ void signal_set_handler(void (*handler)(int), Signals &&sigs) { for (auto sig : sigs) { rv = sigaction(sig, &act, nullptr); if (rv != 0) { - auto error = errno; - LOG(WARN) << "sigaction() with signal " << sig - << " failed: errno=" << error; + return -1; } } + return 0; } } // namespace @@ -120,20 +119,20 @@ constexpr auto worker_proc_ign_signals = GRACEFUL_SHUTDOWN_SIGNAL, RELOAD_SIGNAL, SIGPIPE}}; } // namespace -void shrpx_signal_set_master_proc_ign_handler() { - signal_set_handler(SIG_IGN, master_proc_ign_signals); +int shrpx_signal_set_master_proc_ign_handler() { + return signal_set_handler(SIG_IGN, master_proc_ign_signals); } -void shrpx_signal_unset_master_proc_ign_handler() { - signal_set_handler(SIG_DFL, master_proc_ign_signals); +int shrpx_signal_unset_master_proc_ign_handler() { + return signal_set_handler(SIG_DFL, master_proc_ign_signals); } -void shrpx_signal_set_worker_proc_ign_handler() { - signal_set_handler(SIG_IGN, worker_proc_ign_signals); +int shrpx_signal_set_worker_proc_ign_handler() { + return signal_set_handler(SIG_IGN, worker_proc_ign_signals); } -void shrpx_signal_unset_worker_proc_ign_handler() { - signal_set_handler(SIG_DFL, worker_proc_ign_signals); +int shrpx_signal_unset_worker_proc_ign_handler() { + return signal_set_handler(SIG_DFL, worker_proc_ign_signals); } } // namespace shrpx diff --git a/src/shrpx_signal.h b/src/shrpx_signal.h index c1c61b3c..8f668aef 100644 --- a/src/shrpx_signal.h +++ b/src/shrpx_signal.h @@ -49,11 +49,11 @@ int shrpx_signal_unblock_all(); // -1. The errno will indicate the error. int shrpx_signal_set(sigset_t *set); -void shrpx_signal_set_master_proc_ign_handler(); -void shrpx_signal_unset_master_proc_ign_handler(); +int shrpx_signal_set_master_proc_ign_handler(); +int shrpx_signal_unset_master_proc_ign_handler(); -void shrpx_signal_set_worker_proc_ign_handler(); -void shrpx_signal_unset_worker_proc_ign_handler(); +int shrpx_signal_set_worker_proc_ign_handler(); +int shrpx_signal_unset_worker_proc_ign_handler(); } // namespace shrpx