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();
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

View File

@ -94,7 +94,7 @@ int shrpx_signal_set(sigset_t *set) {
namespace {
template <typename Signals>
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

View File

@ -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