| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/browser_main_posix.h" | 5 #include "chrome/browser/browser_main_posix.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <limits.h> | 8 #include <limits.h> |
| 9 #include <signal.h> | 9 #include <signal.h> |
| 10 #include <sys/resource.h> | 10 #include <sys/resource.h> |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 do { | 48 do { |
| 49 int rv = HANDLE_EINTR( | 49 int rv = HANDLE_EINTR( |
| 50 write(g_shutdown_pipe_write_fd, | 50 write(g_shutdown_pipe_write_fd, |
| 51 reinterpret_cast<const char*>(&signal) + bytes_written, | 51 reinterpret_cast<const char*>(&signal) + bytes_written, |
| 52 sizeof(signal) - bytes_written)); | 52 sizeof(signal) - bytes_written)); |
| 53 RAW_CHECK(rv >= 0); | 53 RAW_CHECK(rv >= 0); |
| 54 bytes_written += rv; | 54 bytes_written += rv; |
| 55 } while (bytes_written < sizeof(signal)); | 55 } while (bytes_written < sizeof(signal)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // See comment in |PreEarlyInitialization()|, where sigaction is called. | 58 // See comment in |PostMainMessageLoopStart()|, where sigaction is called. |
| 59 void SIGHUPHandler(int signal) { | 59 void SIGHUPHandler(int signal) { |
| 60 RAW_CHECK(signal == SIGHUP); | 60 RAW_CHECK(signal == SIGHUP); |
| 61 GracefulShutdownHandler(signal); | 61 GracefulShutdownHandler(signal); |
| 62 } | 62 } |
| 63 | 63 |
| 64 // See comment in |PreEarlyInitialization()|, where sigaction is called. | 64 // See comment in |PostMainMessageLoopStart()|, where sigaction is called. |
| 65 void SIGINTHandler(int signal) { | 65 void SIGINTHandler(int signal) { |
| 66 RAW_CHECK(signal == SIGINT); | 66 RAW_CHECK(signal == SIGINT); |
| 67 GracefulShutdownHandler(signal); | 67 GracefulShutdownHandler(signal); |
| 68 } | 68 } |
| 69 | 69 |
| 70 // See comment in |PreEarlyInitialization()|, where sigaction is called. | 70 // See comment in |PostMainMessageLoopStart()|, where sigaction is called. |
| 71 void SIGTERMHandler(int signal) { | 71 void SIGTERMHandler(int signal) { |
| 72 RAW_CHECK(signal == SIGTERM); | 72 RAW_CHECK(signal == SIGTERM); |
| 73 GracefulShutdownHandler(signal); | 73 GracefulShutdownHandler(signal); |
| 74 } | 74 } |
| 75 | 75 |
| 76 class ShutdownDetector : public base::PlatformThread::Delegate { | 76 class ShutdownDetector : public base::PlatformThread::Delegate { |
| 77 public: | 77 public: |
| 78 explicit ShutdownDetector(int shutdown_fd); | 78 explicit ShutdownDetector(int shutdown_fd); |
| 79 | 79 |
| 80 virtual void ThreadMain(); | 80 virtual void ThreadMain(); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 } | 192 } |
| 193 | 193 |
| 194 void BrowserMainPartsPosix::PreEarlyInitialization() { | 194 void BrowserMainPartsPosix::PreEarlyInitialization() { |
| 195 // We need to accept SIGCHLD, even though our handler is a no-op because | 195 // We need to accept SIGCHLD, even though our handler is a no-op because |
| 196 // otherwise we cannot wait on children. (According to POSIX 2001.) | 196 // otherwise we cannot wait on children. (According to POSIX 2001.) |
| 197 struct sigaction action; | 197 struct sigaction action; |
| 198 memset(&action, 0, sizeof(action)); | 198 memset(&action, 0, sizeof(action)); |
| 199 action.sa_handler = SIGCHLDHandler; | 199 action.sa_handler = SIGCHLDHandler; |
| 200 CHECK(sigaction(SIGCHLD, &action, NULL) == 0); | 200 CHECK(sigaction(SIGCHLD, &action, NULL) == 0); |
| 201 | 201 |
| 202 // If adding to this list of signal handlers, note the new signal probably | |
| 203 // needs to be reset in child processes. See | |
| 204 // base/process_util_posix.cc:LaunchProcess. | |
| 205 | |
| 206 // We need to handle SIGTERM, because that is how many POSIX-based distros ask | |
| 207 // processes to quit gracefully at shutdown time. | |
| 208 memset(&action, 0, sizeof(action)); | |
| 209 action.sa_handler = SIGTERMHandler; | |
| 210 CHECK(sigaction(SIGTERM, &action, NULL) == 0); | |
| 211 // Also handle SIGINT - when the user terminates the browser via Ctrl+C. If | |
| 212 // the browser process is being debugged, GDB will catch the SIGINT first. | |
| 213 action.sa_handler = SIGINTHandler; | |
| 214 CHECK(sigaction(SIGINT, &action, NULL) == 0); | |
| 215 // And SIGHUP, for when the terminal disappears. On shutdown, many Linux | |
| 216 // distros send SIGHUP, SIGTERM, and then SIGKILL. | |
| 217 action.sa_handler = SIGHUPHandler; | |
| 218 CHECK(sigaction(SIGHUP, &action, NULL) == 0); | |
| 219 | |
| 220 const std::string fd_limit_string = | 202 const std::string fd_limit_string = |
| 221 parsed_command_line().GetSwitchValueASCII( | 203 parsed_command_line().GetSwitchValueASCII( |
| 222 switches::kFileDescriptorLimit); | 204 switches::kFileDescriptorLimit); |
| 223 int fd_limit = 0; | 205 int fd_limit = 0; |
| 224 if (!fd_limit_string.empty()) { | 206 if (!fd_limit_string.empty()) { |
| 225 base::StringToInt(fd_limit_string, &fd_limit); | 207 base::StringToInt(fd_limit_string, &fd_limit); |
| 226 } | 208 } |
| 227 #if defined(OS_MACOSX) | 209 #if defined(OS_MACOSX) |
| 228 // We use quite a few file descriptors for our IPC, and the default limit on | 210 // We use quite a few file descriptors for our IPC, and the default limit on |
| 229 // the Mac is low (256), so bump it up if there is no explicit override. | 211 // the Mac is low (256), so bump it up if there is no explicit override. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 255 g_shutdown_pipe_write_fd = pipefd[1]; | 237 g_shutdown_pipe_write_fd = pipefd[1]; |
| 256 const size_t kShutdownDetectorThreadStackSize = 4096; | 238 const size_t kShutdownDetectorThreadStackSize = 4096; |
| 257 // TODO(viettrungluu,willchan): crbug.com/29675 - This currently leaks, so | 239 // TODO(viettrungluu,willchan): crbug.com/29675 - This currently leaks, so |
| 258 // if you change this, you'll probably need to change the suppression. | 240 // if you change this, you'll probably need to change the suppression. |
| 259 if (!base::PlatformThread::CreateNonJoinable( | 241 if (!base::PlatformThread::CreateNonJoinable( |
| 260 kShutdownDetectorThreadStackSize, | 242 kShutdownDetectorThreadStackSize, |
| 261 new ShutdownDetector(g_shutdown_pipe_read_fd))) { | 243 new ShutdownDetector(g_shutdown_pipe_read_fd))) { |
| 262 LOG(DFATAL) << "Failed to create shutdown detector task."; | 244 LOG(DFATAL) << "Failed to create shutdown detector task."; |
| 263 } | 245 } |
| 264 } | 246 } |
| 247 // Setup signal handlers for shutdown AFTER shutdown pipe is setup because |
| 248 // it may be called right away after handler is set. |
| 249 |
| 250 // If adding to this list of signal handlers, note the new signal probably |
| 251 // needs to be reset in child processes. See |
| 252 // base/process_util_posix.cc:LaunchProcess. |
| 253 |
| 254 // We need to handle SIGTERM, because that is how many POSIX-based distros ask |
| 255 // processes to quit gracefully at shutdown time. |
| 256 struct sigaction action; |
| 257 memset(&action, 0, sizeof(action)); |
| 258 action.sa_handler = SIGTERMHandler; |
| 259 CHECK(sigaction(SIGTERM, &action, NULL) == 0); |
| 260 // Also handle SIGINT - when the user terminates the browser via Ctrl+C. If |
| 261 // the browser process is being debugged, GDB will catch the SIGINT first. |
| 262 action.sa_handler = SIGINTHandler; |
| 263 CHECK(sigaction(SIGINT, &action, NULL) == 0); |
| 264 // And SIGHUP, for when the terminal disappears. On shutdown, many Linux |
| 265 // distros send SIGHUP, SIGTERM, and then SIGKILL. |
| 266 action.sa_handler = SIGHUPHandler; |
| 267 CHECK(sigaction(SIGHUP, &action, NULL) == 0); |
| 265 | 268 |
| 266 #if defined(TOOLKIT_USES_GTK) && !defined(OS_CHROMEOS) | 269 #if defined(TOOLKIT_USES_GTK) && !defined(OS_CHROMEOS) |
| 267 printing::PrintingContextCairo::SetCreatePrintDialogFunction( | 270 printing::PrintingContextCairo::SetCreatePrintDialogFunction( |
| 268 &PrintDialogGtk::CreatePrintDialog); | 271 &PrintDialogGtk::CreatePrintDialog); |
| 269 #endif | 272 #endif |
| 270 } | 273 } |
| OLD | NEW |