atom_browser_main_parts_posix.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. // Most code came from: chrome/browser/chrome_browser_main_posix.cc.
  5. #include "atom/browser/atom_browser_main_parts.h"
  6. #include <errno.h>
  7. #include <limits.h>
  8. #include <pthread.h>
  9. #include <signal.h>
  10. #include <sys/resource.h>
  11. #include <unistd.h>
  12. #include "atom/browser/browser.h"
  13. #include "base/posix/eintr_wrapper.h"
  14. #include "content/public/browser/browser_thread.h"
  15. using content::BrowserThread;
  16. namespace atom {
  17. namespace {
  18. // See comment in |PreEarlyInitialization()|, where sigaction is called.
  19. void SIGCHLDHandler(int signal) {}
  20. // The OSX fork() implementation can crash in the child process before
  21. // fork() returns. In that case, the shutdown pipe will still be
  22. // shared with the parent process. To prevent child crashes from
  23. // causing parent shutdowns, |g_pipe_pid| is the pid for the process
  24. // which registered |g_shutdown_pipe_write_fd|.
  25. // See <http://crbug.com/175341>.
  26. pid_t g_pipe_pid = -1;
  27. int g_shutdown_pipe_write_fd = -1;
  28. int g_shutdown_pipe_read_fd = -1;
  29. // Common code between SIG{HUP, INT, TERM}Handler.
  30. void GracefulShutdownHandler(int signal) {
  31. // Reinstall the default handler. We had one shot at graceful shutdown.
  32. struct sigaction action;
  33. memset(&action, 0, sizeof(action));
  34. action.sa_handler = SIG_DFL;
  35. RAW_CHECK(sigaction(signal, &action, nullptr) == 0);
  36. RAW_CHECK(g_pipe_pid == getpid());
  37. RAW_CHECK(g_shutdown_pipe_write_fd != -1);
  38. RAW_CHECK(g_shutdown_pipe_read_fd != -1);
  39. size_t bytes_written = 0;
  40. do {
  41. int rv = HANDLE_EINTR(
  42. write(g_shutdown_pipe_write_fd,
  43. reinterpret_cast<const char*>(&signal) + bytes_written,
  44. sizeof(signal) - bytes_written));
  45. RAW_CHECK(rv >= 0);
  46. bytes_written += rv;
  47. } while (bytes_written < sizeof(signal));
  48. }
  49. // See comment in |PostMainMessageLoopStart()|, where sigaction is called.
  50. void SIGHUPHandler(int signal) {
  51. RAW_CHECK(signal == SIGHUP);
  52. GracefulShutdownHandler(signal);
  53. }
  54. // See comment in |PostMainMessageLoopStart()|, where sigaction is called.
  55. void SIGINTHandler(int signal) {
  56. RAW_CHECK(signal == SIGINT);
  57. GracefulShutdownHandler(signal);
  58. }
  59. // See comment in |PostMainMessageLoopStart()|, where sigaction is called.
  60. void SIGTERMHandler(int signal) {
  61. RAW_CHECK(signal == SIGTERM);
  62. GracefulShutdownHandler(signal);
  63. }
  64. class ShutdownDetector : public base::PlatformThread::Delegate {
  65. public:
  66. explicit ShutdownDetector(int shutdown_fd);
  67. void ThreadMain() override;
  68. private:
  69. const int shutdown_fd_;
  70. DISALLOW_COPY_AND_ASSIGN(ShutdownDetector);
  71. };
  72. ShutdownDetector::ShutdownDetector(int shutdown_fd)
  73. : shutdown_fd_(shutdown_fd) {
  74. CHECK_NE(shutdown_fd_, -1);
  75. }
  76. // These functions are used to help us diagnose crash dumps that happen
  77. // during the shutdown process.
  78. NOINLINE void ShutdownFDReadError() {
  79. // Ensure function isn't optimized away.
  80. asm("");
  81. sleep(UINT_MAX);
  82. }
  83. NOINLINE void ShutdownFDClosedError() {
  84. // Ensure function isn't optimized away.
  85. asm("");
  86. sleep(UINT_MAX);
  87. }
  88. NOINLINE void ExitPosted() {
  89. // Ensure function isn't optimized away.
  90. asm("");
  91. sleep(UINT_MAX);
  92. }
  93. void ShutdownDetector::ThreadMain() {
  94. base::PlatformThread::SetName("CrShutdownDetector");
  95. int signal;
  96. size_t bytes_read = 0;
  97. do {
  98. ssize_t ret = HANDLE_EINTR(
  99. read(shutdown_fd_, reinterpret_cast<char*>(&signal) + bytes_read,
  100. sizeof(signal) - bytes_read));
  101. if (ret < 0) {
  102. NOTREACHED() << "Unexpected error: " << strerror(errno);
  103. ShutdownFDReadError();
  104. break;
  105. } else if (ret == 0) {
  106. NOTREACHED() << "Unexpected closure of shutdown pipe.";
  107. ShutdownFDClosedError();
  108. break;
  109. }
  110. bytes_read += ret;
  111. } while (bytes_read < sizeof(signal));
  112. VLOG(1) << "Handling shutdown for signal " << signal << ".";
  113. base::Closure task =
  114. base::Bind(&Browser::Quit, base::Unretained(Browser::Get()));
  115. if (!BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, task)) {
  116. // Without a UI thread to post the exit task to, there aren't many
  117. // options. Raise the signal again. The default handler will pick it up
  118. // and cause an ungraceful exit.
  119. RAW_LOG(WARNING, "No UI thread, exiting ungracefully.");
  120. kill(getpid(), signal);
  121. // The signal may be handled on another thread. Give that a chance to
  122. // happen.
  123. sleep(3);
  124. // We really should be dead by now. For whatever reason, we're not. Exit
  125. // immediately, with the exit status set to the signal number with bit 8
  126. // set. On the systems that we care about, this exit status is what is
  127. // normally used to indicate an exit by this signal's default handler.
  128. // This mechanism isn't a de jure standard, but even in the worst case, it
  129. // should at least result in an immediate exit.
  130. RAW_LOG(WARNING, "Still here, exiting really ungracefully.");
  131. _exit(signal | (1 << 7));
  132. }
  133. ExitPosted();
  134. }
  135. } // namespace
  136. void AtomBrowserMainParts::HandleSIGCHLD() {
  137. // We need to accept SIGCHLD, even though our handler is a no-op because
  138. // otherwise we cannot wait on children. (According to POSIX 2001.)
  139. struct sigaction action;
  140. memset(&action, 0, sizeof(action));
  141. action.sa_handler = SIGCHLDHandler;
  142. CHECK_EQ(sigaction(SIGCHLD, &action, nullptr), 0);
  143. }
  144. void AtomBrowserMainParts::HandleShutdownSignals() {
  145. int pipefd[2];
  146. int ret = pipe(pipefd);
  147. if (ret < 0) {
  148. PLOG(DFATAL) << "Failed to create pipe";
  149. } else {
  150. g_pipe_pid = getpid();
  151. g_shutdown_pipe_read_fd = pipefd[0];
  152. g_shutdown_pipe_write_fd = pipefd[1];
  153. #if !defined(ADDRESS_SANITIZER) && !defined(KEEP_SHADOW_STACKS)
  154. const size_t kShutdownDetectorThreadStackSize = PTHREAD_STACK_MIN * 2;
  155. #else
  156. // ASan instrumentation and -finstrument-functions (used for keeping the
  157. // shadow stacks) bloat the stack frames, so we need to increase the stack
  158. // size to avoid hitting the guard page.
  159. const size_t kShutdownDetectorThreadStackSize = PTHREAD_STACK_MIN * 4;
  160. #endif
  161. // TODO(viettrungluu,willchan): crbug.com/29675 - This currently leaks, so
  162. // if you change this, you'll probably need to change the suppression.
  163. if (!base::PlatformThread::CreateNonJoinable(
  164. kShutdownDetectorThreadStackSize,
  165. new ShutdownDetector(g_shutdown_pipe_read_fd))) {
  166. LOG(DFATAL) << "Failed to create shutdown detector task.";
  167. }
  168. }
  169. // Setup signal handlers for shutdown AFTER shutdown pipe is setup because
  170. // it may be called right away after handler is set.
  171. // If adding to this list of signal handlers, note the new signal probably
  172. // needs to be reset in child processes. See
  173. // base/process_util_posix.cc:LaunchProcess.
  174. // We need to handle SIGTERM, because that is how many POSIX-based distros ask
  175. // processes to quit gracefully at shutdown time.
  176. struct sigaction action;
  177. memset(&action, 0, sizeof(action));
  178. action.sa_handler = SIGTERMHandler;
  179. CHECK_EQ(sigaction(SIGTERM, &action, nullptr), 0);
  180. // Also handle SIGINT - when the user terminates the browser via Ctrl+C. If
  181. // the browser process is being debugged, GDB will catch the SIGINT first.
  182. action.sa_handler = SIGINTHandler;
  183. CHECK_EQ(sigaction(SIGINT, &action, nullptr), 0);
  184. // And SIGHUP, for when the terminal disappears. On shutdown, many Linux
  185. // distros send SIGHUP, SIGTERM, and then SIGKILL.
  186. action.sa_handler = SIGHUPHandler;
  187. CHECK_EQ(sigaction(SIGHUP, &action, nullptr), 0);
  188. }
  189. } // namespace atom