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