Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Unified Diff: chromecast/browser/cast_browser_main_parts.cc

Issue 1542233002: [Chromecast] Make SIGTERM/SIGINT handler safer (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Use signal-safe logging Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromecast/browser/cast_browser_main_parts.cc
diff --git a/chromecast/browser/cast_browser_main_parts.cc b/chromecast/browser/cast_browser_main_parts.cc
index 14f23316fdbcb325950edcc800116b23e304fcae..a7037237f5597bd379f74cb2d154dc846f700525 100644
--- a/chromecast/browser/cast_browser_main_parts.cc
+++ b/chromecast/browser/cast_browser_main_parts.cc
@@ -5,6 +5,7 @@
#include "chromecast/browser/cast_browser_main_parts.h"
#include <stddef.h>
+#include <string.h>
#include <string>
@@ -76,15 +77,21 @@ namespace {
#if !defined(OS_ANDROID)
int kSignalsToRunClosure[] = { SIGTERM, SIGINT, };
-
// Closure to run on SIGTERM and SIGINT.
-base::Closure* g_signal_closure = NULL;
+base::Closure* g_signal_closure = nullptr;
+base::PlatformThreadId g_main_thread_id;
void RunClosureOnSignal(int signum) {
- LOG(ERROR) << "Got signal " << signum;
+ if (base::PlatformThread::CurrentId() != g_main_thread_id) {
+ RAW_LOG(INFO, "Received signal on non-main thread\n");
+ return;
+ }
+
+ char message[48] = "Received close signal: ";
+ strncat(message, sys_siglist[signum], sizeof(message) - strlen(message) - 1);
maclellant 2015/12/29 18:36:14 I was about to reply that string library functions
+ RAW_LOG(INFO, message);
+
DCHECK(g_signal_closure);
- // Expect main thread got this signal. Otherwise, weak_ptr of run_loop will
- // crash the process.
g_signal_closure->Run();
}
@@ -92,8 +99,10 @@ void RegisterClosureOnSignal(const base::Closure& closure) {
DCHECK(!g_signal_closure);
DCHECK_GT(arraysize(kSignalsToRunClosure), 0U);
- // Allow memory leak by intention.
+ // Memory leak on purpose, since |g_signal_closure| should live until
+ // process exit.
g_signal_closure = new base::Closure(closure);
+ g_main_thread_id = base::PlatformThread::CurrentId();
struct sigaction sa_new;
memset(&sa_new, 0, sizeof(sa_new));
@@ -101,9 +110,9 @@ void RegisterClosureOnSignal(const base::Closure& closure) {
sigfillset(&sa_new.sa_mask);
sa_new.sa_flags = SA_RESTART;
- for (size_t i = 0; i < arraysize(kSignalsToRunClosure); i++) {
+ for (int sig : kSignalsToRunClosure) {
struct sigaction sa_old;
- if (sigaction(kSignalsToRunClosure[i], &sa_new, &sa_old) == -1) {
+ if (sigaction(sig, &sa_new, &sa_old) == -1) {
NOTREACHED();
} else {
DCHECK_EQ(sa_old.sa_handler, SIG_DFL);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698