From 1c6c23c6758fb9ef612e3592f7aa415d9e134091 Mon Sep 17 00:00:00 2001 From: "phajdan.jr@chromium.org" Date: Fri, 16 Nov 2012 17:50:14 +0000 Subject: [PATCH] Extract SIGPIPE ignoring code to a common place. BUG=none Review URL: https://codereview.chromium.org/11280010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@168239 0039d316-1c4b-4281-b951-d872f2087c98 --- base/debug/stack_trace_android.cc | 9 +++------ base/debug/stack_trace_posix.cc | 7 ++----- base/process_util.h | 6 ++++++ base/process_util_posix.cc | 9 +++++++++ chrome/browser/chromeos/web_socket_proxy.cc | 15 ++------------- chrome/test/webdriver/webdriver_server.cc | 8 ++++++-- content/app/content_main_runner.cc | 2 +- net/tools/flip_server/flip_in_mem_edsm_server.cc | 3 ++- 8 files changed, 31 insertions(+), 28 deletions(-) diff --git a/base/debug/stack_trace_android.cc b/base/debug/stack_trace_android.cc index cc03d60ba97f..2d9a422ef3f7 100644 --- a/base/debug/stack_trace_android.cc +++ b/base/debug/stack_trace_android.cc @@ -9,6 +9,7 @@ #include #include "base/logging.h" +#include "base/process_util.h" namespace base { namespace debug { @@ -17,12 +18,8 @@ bool EnableInProcessStackDumping() { // When running in an application, our code typically expects SIGPIPE // to be ignored. Therefore, when testing that same code, it should run // with SIGPIPE ignored as well. - // TODO(phajdan.jr): De-duplicate this SIGPIPE code. - struct sigaction action; - memset(&action, 0, sizeof(action)); - action.sa_handler = SIG_IGN; - sigemptyset(&action.sa_mask); - return (sigaction(SIGPIPE, &action, NULL) == 0); + // TODO(phajdan.jr): Ignoring SIGPIPE has nothing to do with stack dumping. + return base::IgnoreSigPipe(); } StackTrace::StackTrace() { diff --git a/base/debug/stack_trace_posix.cc b/base/debug/stack_trace_posix.cc index 6c90b2ba3f7e..1ccee680ca98 100644 --- a/base/debug/stack_trace_posix.cc +++ b/base/debug/stack_trace_posix.cc @@ -30,6 +30,7 @@ #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/posix/eintr_wrapper.h" +#include "base/process_util.h" #include "base/string_number_conversions.h" #if defined(USE_SYMBOLIZE) @@ -301,11 +302,7 @@ bool EnableInProcessStackDumping() { // When running in an application, our code typically expects SIGPIPE // to be ignored. Therefore, when testing that same code, it should run // with SIGPIPE ignored as well. - struct sigaction action; - memset(&action, 0, sizeof(action)); - action.sa_handler = SIG_IGN; - sigemptyset(&action.sa_mask); - bool success = (sigaction(SIGPIPE, &action, NULL) == 0); + bool success = base::IgnoreSigPipe(); // Avoid hangs during backtrace initialization, see above. WarmUpBacktrace(); diff --git a/base/process_util.h b/base/process_util.h index 2805e421607e..00135c4ef6fd 100644 --- a/base/process_util.h +++ b/base/process_util.h @@ -147,6 +147,12 @@ BASE_EXPORT extern size_t g_oom_size; BASE_EXPORT void RouteStdioToConsole(); #endif +#if defined(OS_POSIX) +// Ignores SIGPIPE signal. We handle pipe errors in a different way. +// Returns true on success. +BASE_EXPORT bool IgnoreSigPipe() WARN_UNUSED_RESULT; +#endif + // Returns the id of the current process. BASE_EXPORT ProcessId GetCurrentProcId(); diff --git a/base/process_util_posix.cc b/base/process_util_posix.cc index a19cc2a999ec..9bc19e3676f6 100644 --- a/base/process_util_posix.cc +++ b/base/process_util_posix.cc @@ -154,6 +154,15 @@ void ResetChildSignalHandlersToDefaults() { } // anonymous namespace +bool IgnoreSigPipe() { + struct sigaction action; + memset(&action, 0, sizeof(action)); + action.sa_handler = SIG_IGN; + if (sigemptyset(&action.sa_mask) != 0) + return false; + return (sigaction(SIGPIPE, &action, NULL) == 0); +} + ProcessId GetCurrentProcId() { return getpid(); } diff --git a/chrome/browser/chromeos/web_socket_proxy.cc b/chrome/browser/chromeos/web_socket_proxy.cc index f1ef630b9df4..4edae06bb42b 100644 --- a/chrome/browser/chromeos/web_socket_proxy.cc +++ b/chrome/browser/chromeos/web_socket_proxy.cc @@ -32,6 +32,7 @@ #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "base/message_loop.h" +#include "base/process_util.h" #include "base/sequenced_task_runner_helpers.h" #include "base/sha1.h" #include "base/stl_util.h" @@ -101,18 +102,6 @@ bool SetNonBlock(int fd) { return flags >= 0 && fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0; } -// Returns true on success. -bool IgnoreSigPipe() { - struct sigaction sa; - sa.sa_handler = SIG_IGN; - sa.sa_flags = 0; - if (sigemptyset(&sa.sa_mask) || sigaction(SIGPIPE, &sa, 0)) { - LOG(ERROR) << "WebSocketProxy: Failed to disable sigpipe"; - return false; - } - return true; -} - uint64 ReadNetworkInteger(uint8* buf, int num_bytes) { uint64 rv = 0; DCHECK_GE(num_bytes, 0); @@ -921,7 +910,7 @@ void Serv::Run() { if (evdns_init()) LOG(WARNING) << "WebSocketProxy: Failed to initialize evDNS"; - if (!IgnoreSigPipe()) { + if (!base::IgnoreSigPipe()) { LOG(ERROR) << "WebSocketProxy: Failed to ignore SIGPIPE"; return; } diff --git a/chrome/test/webdriver/webdriver_server.cc b/chrome/test/webdriver/webdriver_server.cc index ccb8c70a9faa..5781bac40def 100644 --- a/chrome/test/webdriver/webdriver_server.cc +++ b/chrome/test/webdriver/webdriver_server.cc @@ -19,6 +19,7 @@ #include "base/file_util.h" #include "base/memory/scoped_ptr.h" #include "base/path_service.h" +#include "base/process_util.h" #include "base/stringprintf.h" #include "base/string_number_conversions.h" #include "base/string_split.h" @@ -226,8 +227,11 @@ int RunChromeDriver() { CommandLine* cmd_line = CommandLine::ForCurrentProcess(); #if defined(OS_POSIX) - signal(SIGPIPE, SIG_IGN); -#endif + if (!IgnoreSigPipe()) { + LOG(ERROR) << "Failed to ignore SIGPIPE"; + return EXIT_FAILURE; + } +#endif // defined(OS_POSIX) srand((unsigned int)time(NULL)); // Register Chrome's path provider so that the AutomationProxy will find our diff --git a/content/app/content_main_runner.cc b/content/app/content_main_runner.cc index 22115ced37a5..e35c8e485eb2 100644 --- a/content/app/content_main_runner.cc +++ b/content/app/content_main_runner.cc @@ -222,7 +222,7 @@ void SetupSignalHandlers() { } // Always ignore SIGPIPE. We check the return value of write(). - CHECK(signal(SIGPIPE, SIG_IGN) != SIG_ERR); + CHECK(base::IgnoreSigPipe()); } #endif // OS_POSIX && !OS_IOS diff --git a/net/tools/flip_server/flip_in_mem_edsm_server.cc b/net/tools/flip_server/flip_in_mem_edsm_server.cc index 8ba150579eec..0e1a08fbd5f4 100644 --- a/net/tools/flip_server/flip_in_mem_edsm_server.cc +++ b/net/tools/flip_server/flip_in_mem_edsm_server.cc @@ -13,6 +13,7 @@ #include "base/command_line.h" #include "base/logging.h" +#include "base/process_util.h" #include "base/synchronization/lock.h" #include "base/timer.h" #include "net/tools/flip_server/acceptor_thread.h" @@ -163,7 +164,7 @@ int main (int argc, char**argv) bool wait_for_iface = false; int pidfile_fd; - signal(SIGPIPE, SIG_IGN); + CHECK(base::IgnoreSigPipe()); signal(SIGTERM, SignalHandler); signal(SIGINT, SignalHandler); signal(SIGHUP, SignalHandler); -- 2.11.4.GIT