nghttpx: Don't call functions which are not async-signal-safe

.. after fork but before execv in multithreaded process.
This commit is contained in:
Tatsuhiro Tsujikawa 2017-03-01 22:42:30 +09:00
parent d4963bcd32
commit 4989e6e419
3 changed files with 22 additions and 22 deletions

View File

@ -76,14 +76,16 @@ int exec_read_command(Process &proc, char *const argv[]) {
auto pid = fork(); auto pid = fork();
if (pid == 0) { if (pid == 0) {
// This is multithreaded program, and we are allowed to use only
// async-signal-safe functions here.
// child process // child process
shrpx_signal_unset_worker_proc_ign_handler(); shrpx_signal_unset_worker_proc_ign_handler();
rv = shrpx_signal_unblock_all(); rv = shrpx_signal_unblock_all();
if (rv != 0) { if (rv != 0) {
auto error = errno; constexpr char msg[] = "Unblocking all signals failed\n";
LOG(FATAL) << "Unblocking all signals failed: errno=" << error; write(STDERR_FILENO, msg, str_size(msg));
nghttp2_Exit(EXIT_FAILURE); nghttp2_Exit(EXIT_FAILURE);
} }
@ -92,9 +94,8 @@ int exec_read_command(Process &proc, char *const argv[]) {
rv = execv(argv[0], argv); rv = execv(argv[0], argv);
if (rv == -1) { if (rv == -1) {
auto error = errno; constexpr char msg[] = "Could not execute command\n";
LOG(ERROR) << "Could not execute command: " << argv[0] write(STDERR_FILENO, msg, str_size(msg));
<< ", execve() faild, errno=" << error;
nghttp2_Exit(EXIT_FAILURE); nghttp2_Exit(EXIT_FAILURE);
} }
// unreachable // unreachable

View File

@ -94,7 +94,7 @@ int shrpx_signal_set(sigset_t *set) {
namespace { namespace {
template <typename Signals> template <typename Signals>
void signal_set_handler(void (*handler)(int), Signals &&sigs) { int signal_set_handler(void (*handler)(int), Signals &&sigs) {
struct sigaction act {}; struct sigaction act {};
act.sa_handler = handler; act.sa_handler = handler;
sigemptyset(&act.sa_mask); sigemptyset(&act.sa_mask);
@ -102,11 +102,10 @@ void signal_set_handler(void (*handler)(int), Signals &&sigs) {
for (auto sig : sigs) { for (auto sig : sigs) {
rv = sigaction(sig, &act, nullptr); rv = sigaction(sig, &act, nullptr);
if (rv != 0) { if (rv != 0) {
auto error = errno; return -1;
LOG(WARN) << "sigaction() with signal " << sig
<< " failed: errno=" << error;
} }
} }
return 0;
} }
} // namespace } // namespace
@ -120,20 +119,20 @@ constexpr auto worker_proc_ign_signals =
GRACEFUL_SHUTDOWN_SIGNAL, RELOAD_SIGNAL, SIGPIPE}}; GRACEFUL_SHUTDOWN_SIGNAL, RELOAD_SIGNAL, SIGPIPE}};
} // namespace } // namespace
void shrpx_signal_set_master_proc_ign_handler() { int shrpx_signal_set_master_proc_ign_handler() {
signal_set_handler(SIG_IGN, master_proc_ign_signals); return signal_set_handler(SIG_IGN, master_proc_ign_signals);
} }
void shrpx_signal_unset_master_proc_ign_handler() { int shrpx_signal_unset_master_proc_ign_handler() {
signal_set_handler(SIG_DFL, master_proc_ign_signals); return signal_set_handler(SIG_DFL, master_proc_ign_signals);
} }
void shrpx_signal_set_worker_proc_ign_handler() { int shrpx_signal_set_worker_proc_ign_handler() {
signal_set_handler(SIG_IGN, worker_proc_ign_signals); return signal_set_handler(SIG_IGN, worker_proc_ign_signals);
} }
void shrpx_signal_unset_worker_proc_ign_handler() { int shrpx_signal_unset_worker_proc_ign_handler() {
signal_set_handler(SIG_DFL, worker_proc_ign_signals); return signal_set_handler(SIG_DFL, worker_proc_ign_signals);
} }
} // namespace shrpx } // namespace shrpx

View File

@ -49,11 +49,11 @@ int shrpx_signal_unblock_all();
// -1. The errno will indicate the error. // -1. The errno will indicate the error.
int shrpx_signal_set(sigset_t *set); int shrpx_signal_set(sigset_t *set);
void shrpx_signal_set_master_proc_ign_handler(); int shrpx_signal_set_master_proc_ign_handler();
void shrpx_signal_unset_master_proc_ign_handler(); int shrpx_signal_unset_master_proc_ign_handler();
void shrpx_signal_set_worker_proc_ign_handler(); int shrpx_signal_set_worker_proc_ign_handler();
void shrpx_signal_unset_worker_proc_ign_handler(); int shrpx_signal_unset_worker_proc_ign_handler();
} // namespace shrpx } // namespace shrpx