From 61665d2c26be32c396e295345e92449ef3b9c001 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 14 Feb 2013 21:51:30 +0900 Subject: [PATCH] Set sa_mask when setting signal handler --- src/MultiUrlRequestInfo.cc | 28 ++++++++++++++++++++++------ src/main.cc | 11 ++++++++--- src/util.cc | 5 +++-- src/util.h | 7 ++++++- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index 48bfec4f..0d8a224a 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -129,6 +129,7 @@ void MultiUrlRequestInfo::printMessageForContinue() error_code::Value MultiUrlRequestInfo::execute() { error_code::Value returnValue = error_code::FINISHED; + sigset_t mask; try { SharedHandle wsSessionMan; if(option_->getAsBool(PREF_ENABLE_RPC)) { @@ -233,11 +234,22 @@ error_code::Value MultiUrlRequestInfo::execute() if(uriListParser_) { e->getRequestGroupMan()->setUriListParser(uriListParser_); } +#ifdef HAVE_SIGACTION + sigemptyset(&mask); + sigaddset(&mask, SIGINT); + sigaddset(&mask, SIGTERM); #ifdef SIGHUP - util::setGlobalSignalHandler(SIGHUP, handler, 0); + sigaddset(&mask, SIGHUP); #endif // SIGHUP - util::setGlobalSignalHandler(SIGINT, handler, 0); - util::setGlobalSignalHandler(SIGTERM, handler, 0); +#else // !HAVE_SIGACTION + mask = 0; +#endif // !HAVE_SIGACTION + +#ifdef SIGHUP + util::setGlobalSignalHandler(SIGHUP, &mask, handler, 0); +#endif // SIGHUP + util::setGlobalSignalHandler(SIGINT, &mask, handler, 0); + util::setGlobalSignalHandler(SIGTERM, &mask, handler, 0); e->getRequestGroupMan()->getNetStat().downloadStart(); e->run(); @@ -284,11 +296,15 @@ error_code::Value MultiUrlRequestInfo::execute() A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e); } SingletonHolder::instance(0); + +#ifdef HAVE_SIGACTION + sigemptyset(&mask); +#endif // HAVE_SIGACTION #ifdef SIGHUP - util::setGlobalSignalHandler(SIGHUP, SIG_DFL, 0); + util::setGlobalSignalHandler(SIGHUP, &mask, SIG_DFL, 0); #endif // SIGHUP - util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0); - util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0); + util::setGlobalSignalHandler(SIGINT, &mask, SIG_DFL, 0); + util::setGlobalSignalHandler(SIGTERM, &mask, SIG_DFL, 0); return returnValue; } diff --git a/src/main.cc b/src/main.cc index 8eb75ed9..0f45f344 100644 --- a/src/main.cc +++ b/src/main.cc @@ -213,14 +213,19 @@ error_code::Value main(int argc, char* argv[]) std::string iface = op->get(PREF_INTERFACE); SocketCore::bindAddress(iface); } - + sigset_t mask; +#ifdef HAVE_SIGACTION + sigemptyset(&mask); +#else // !HAVE_SIGACTION + mask = 0; +#endif // !HAVE_SIGACTION #ifdef SIGPIPE - util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0); + util::setGlobalSignalHandler(SIGPIPE, &mask, SIG_IGN, 0); #endif #ifdef SIGCHLD // Avoid to create zombie process when forked child processes are // died. - util::setGlobalSignalHandler(SIGCHLD, SIG_IGN, 0); + util::setGlobalSignalHandler(SIGCHLD, &mask, SIG_IGN, 0); #endif // SIGCHILD std::vector > requestGroups; SharedHandle uriListParser; diff --git a/src/util.cc b/src/util.cc index a97bc507..06bc14c3 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1221,12 +1221,13 @@ bool isNumericHost(const std::string& name) return true; } -void setGlobalSignalHandler(int sig, void (*handler)(int), int flags) { +void setGlobalSignalHandler(int sig, sigset_t* mask, void (*handler)(int), + int flags) { #ifdef HAVE_SIGACTION struct sigaction sigact; sigact.sa_handler = handler; sigact.sa_flags = flags; - sigemptyset(&sigact.sa_mask); + sigact.sa_mask = *mask; sigaction(sig, &sigact, NULL); #else signal(sig, handler); diff --git a/src/util.h b/src/util.h index 2e1418cc..c31019f2 100644 --- a/src/util.h +++ b/src/util.h @@ -63,6 +63,10 @@ #include "DlAbortEx.h" #include "fmt.h" +#ifndef HAVE_SIGACTION +# define sigset_t int +#endif // HAVE_SIGACTION + namespace aria2 { class Randomizer; @@ -335,7 +339,8 @@ char toLowerChar(char c); bool isNumericHost(const std::string& name); -void setGlobalSignalHandler(int signal, void (*handler)(int), int flags); +void setGlobalSignalHandler(int signal, sigset_t* mask, void (*handler)(int), + int flags); std::string getHomeDir();