OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chromecast/browser/cast_browser_main_parts.h" | 5 #include "chromecast/browser/cast_browser_main_parts.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string.h> | |
8 | 9 |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/command_line.h" | 12 #include "base/command_line.h" |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
16 #include "base/prefs/pref_registry_simple.h" | 17 #include "base/prefs/pref_registry_simple.h" |
17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 #if defined(USE_AURA) | 70 #if defined(USE_AURA) |
70 #include "chromecast/graphics/cast_screen.h" | 71 #include "chromecast/graphics/cast_screen.h" |
71 #include "ui/aura/env.h" | 72 #include "ui/aura/env.h" |
72 #include "ui/gfx/screen.h" | 73 #include "ui/gfx/screen.h" |
73 #endif | 74 #endif |
74 | 75 |
75 namespace { | 76 namespace { |
76 | 77 |
77 #if !defined(OS_ANDROID) | 78 #if !defined(OS_ANDROID) |
78 int kSignalsToRunClosure[] = { SIGTERM, SIGINT, }; | 79 int kSignalsToRunClosure[] = { SIGTERM, SIGINT, }; |
79 | |
80 // Closure to run on SIGTERM and SIGINT. | 80 // Closure to run on SIGTERM and SIGINT. |
81 base::Closure* g_signal_closure = NULL; | 81 base::Closure* g_signal_closure = nullptr; |
82 base::PlatformThreadId g_main_thread_id; | |
82 | 83 |
83 void RunClosureOnSignal(int signum) { | 84 void RunClosureOnSignal(int signum) { |
84 LOG(ERROR) << "Got signal " << signum; | 85 if (base::PlatformThread::CurrentId() != g_main_thread_id) { |
86 RAW_LOG(INFO, "Received signal on non-main thread\n"); | |
87 return; | |
88 } | |
89 | |
90 char message[48] = "Received close signal: "; | |
91 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
| |
92 RAW_LOG(INFO, message); | |
93 | |
85 DCHECK(g_signal_closure); | 94 DCHECK(g_signal_closure); |
86 // Expect main thread got this signal. Otherwise, weak_ptr of run_loop will | |
87 // crash the process. | |
88 g_signal_closure->Run(); | 95 g_signal_closure->Run(); |
89 } | 96 } |
90 | 97 |
91 void RegisterClosureOnSignal(const base::Closure& closure) { | 98 void RegisterClosureOnSignal(const base::Closure& closure) { |
92 DCHECK(!g_signal_closure); | 99 DCHECK(!g_signal_closure); |
93 DCHECK_GT(arraysize(kSignalsToRunClosure), 0U); | 100 DCHECK_GT(arraysize(kSignalsToRunClosure), 0U); |
94 | 101 |
95 // Allow memory leak by intention. | 102 // Memory leak on purpose, since |g_signal_closure| should live until |
103 // process exit. | |
96 g_signal_closure = new base::Closure(closure); | 104 g_signal_closure = new base::Closure(closure); |
105 g_main_thread_id = base::PlatformThread::CurrentId(); | |
97 | 106 |
98 struct sigaction sa_new; | 107 struct sigaction sa_new; |
99 memset(&sa_new, 0, sizeof(sa_new)); | 108 memset(&sa_new, 0, sizeof(sa_new)); |
100 sa_new.sa_handler = RunClosureOnSignal; | 109 sa_new.sa_handler = RunClosureOnSignal; |
101 sigfillset(&sa_new.sa_mask); | 110 sigfillset(&sa_new.sa_mask); |
102 sa_new.sa_flags = SA_RESTART; | 111 sa_new.sa_flags = SA_RESTART; |
103 | 112 |
104 for (size_t i = 0; i < arraysize(kSignalsToRunClosure); i++) { | 113 for (int sig : kSignalsToRunClosure) { |
105 struct sigaction sa_old; | 114 struct sigaction sa_old; |
106 if (sigaction(kSignalsToRunClosure[i], &sa_new, &sa_old) == -1) { | 115 if (sigaction(sig, &sa_new, &sa_old) == -1) { |
107 NOTREACHED(); | 116 NOTREACHED(); |
108 } else { | 117 } else { |
109 DCHECK_EQ(sa_old.sa_handler, SIG_DFL); | 118 DCHECK_EQ(sa_old.sa_handler, SIG_DFL); |
110 } | 119 } |
111 } | 120 } |
112 | 121 |
113 // Get the first signal to exit when the parent process dies. | 122 // Get the first signal to exit when the parent process dies. |
114 prctl(PR_SET_PDEATHSIG, kSignalsToRunClosure[0]); | 123 prctl(PR_SET_PDEATHSIG, kSignalsToRunClosure[0]); |
115 } | 124 } |
116 | 125 |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
426 #if defined(USE_AURA) | 435 #if defined(USE_AURA) |
427 aura::Env::DeleteInstance(); | 436 aura::Env::DeleteInstance(); |
428 #endif | 437 #endif |
429 | 438 |
430 DeregisterKillOnAlarm(); | 439 DeregisterKillOnAlarm(); |
431 #endif | 440 #endif |
432 } | 441 } |
433 | 442 |
434 } // namespace shell | 443 } // namespace shell |
435 } // namespace chromecast | 444 } // namespace chromecast |
OLD | NEW |