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 #if !defined(OS_ANDROID) | |
7 #include <signal.h> | 8 #include <signal.h> |
8 #include <sys/prctl.h> | 9 #include <sys/prctl.h> |
10 #endif | |
9 | 11 |
10 #include "base/command_line.h" | 12 #include "base/command_line.h" |
11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
12 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
13 #include "base/path_service.h" | 15 #include "base/path_service.h" |
14 #include "base/prefs/pref_registry_simple.h" | 16 #include "base/prefs/pref_registry_simple.h" |
15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
16 #include "cc/base/switches.h" | 18 #include "cc/base/switches.h" |
17 #include "chromecast/base/metrics/cast_metrics_helper.h" | 19 #include "chromecast/base/metrics/cast_metrics_helper.h" |
18 #include "chromecast/base/metrics/grouped_histogram.h" | 20 #include "chromecast/base/metrics/grouped_histogram.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 if (sigaction(kSignalsToRunClosure[i], &sa_new, &sa_old) == -1) { | 76 if (sigaction(kSignalsToRunClosure[i], &sa_new, &sa_old) == -1) { |
75 NOTREACHED(); | 77 NOTREACHED(); |
76 } else { | 78 } else { |
77 DCHECK_EQ(sa_old.sa_handler, SIG_DFL); | 79 DCHECK_EQ(sa_old.sa_handler, SIG_DFL); |
78 } | 80 } |
79 } | 81 } |
80 | 82 |
81 // Get the first signal to exit when the parent process dies. | 83 // Get the first signal to exit when the parent process dies. |
82 prctl(PR_SET_PDEATHSIG, kSignalsToRunClosure[0]); | 84 prctl(PR_SET_PDEATHSIG, kSignalsToRunClosure[0]); |
83 } | 85 } |
86 | |
87 const int kKillOnAlarmTimeoutSec = 5; // 5 seconds | |
88 | |
89 void KillOnAlarm(int signum) { | |
90 LOG(ERROR) << "Got alarm signal for termination: " << signum; | |
91 raise(SIGKILL); | |
92 } | |
93 | |
94 void RegisterKillOnAlarm(int timeout_seconds) { | |
95 struct sigaction sa_new; | |
96 memset(&sa_new, 0, sizeof(sa_new)); | |
97 sa_new.sa_handler = KillOnAlarm; | |
98 sigfillset(&sa_new.sa_mask); | |
99 sa_new.sa_flags = SA_RESTART; | |
100 | |
101 struct sigaction sa_old; | |
102 if (sigaction(SIGALRM, &sa_new, &sa_old) == -1) { | |
103 NOTREACHED(); | |
104 } else { | |
105 DCHECK_EQ(sa_old.sa_handler, SIG_DFL); | |
106 } | |
107 | |
108 if (alarm(timeout_seconds) > 0) | |
109 NOTREACHED() << "Previous alarm() was cancelled"; | |
110 } | |
111 | |
112 void DeregisterKillOnAlarm() { | |
113 // Explicitly cancel any outstanding alarm() calls. | |
114 alarm(0); | |
115 | |
116 struct sigaction sa_new; | |
117 memset(&sa_new, 0, sizeof(sa_new)); | |
118 sa_new.sa_handler = SIG_DFL; | |
119 sigfillset(&sa_new.sa_mask); | |
120 sa_new.sa_flags = SA_RESTART; | |
121 | |
122 struct sigaction sa_old; | |
123 if (sigaction(SIGALRM, &sa_new, &sa_old) == -1) { | |
124 NOTREACHED(); | |
125 } else { | |
126 DCHECK_EQ(sa_old.sa_handler, KillOnAlarm); | |
127 } | |
128 } | |
84 #endif // !defined(OS_ANDROID) | 129 #endif // !defined(OS_ANDROID) |
85 | 130 |
86 } // namespace | 131 } // namespace |
87 | 132 |
88 namespace chromecast { | 133 namespace chromecast { |
89 namespace shell { | 134 namespace shell { |
90 | 135 |
91 namespace { | 136 namespace { |
92 | 137 |
93 struct DefaultCommandLineSwitch { | 138 struct DefaultCommandLineSwitch { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
260 | 305 |
261 // If parameters_.ui_task is not NULL, we are running browser tests. | 306 // If parameters_.ui_task is not NULL, we are running browser tests. |
262 if (parameters_.ui_task) { | 307 if (parameters_.ui_task) { |
263 base::MessageLoop* message_loop = base::MessageLoopForUI::current(); | 308 base::MessageLoop* message_loop = base::MessageLoopForUI::current(); |
264 message_loop->PostTask(FROM_HERE, *parameters_.ui_task); | 309 message_loop->PostTask(FROM_HERE, *parameters_.ui_task); |
265 message_loop->PostTask(FROM_HERE, quit_closure); | 310 message_loop->PostTask(FROM_HERE, quit_closure); |
266 } | 311 } |
267 | 312 |
268 run_loop.Run(); | 313 run_loop.Run(); |
269 | 314 |
315 // Once the main loop has stopped running, we give the browser process a few | |
316 // seconds to stop cast service and finalize all resources. If a hang occurs | |
317 // and cast services refuse to terminate successfully, then we SIGKILL the | |
318 // current process to avoid indefinte hangs. | |
319 RegisterKillOnAlarm(kKillOnAlarmTimeoutSec); | |
320 | |
270 cast_browser_process_->cast_service()->Stop(); | 321 cast_browser_process_->cast_service()->Stop(); |
271 return true; | 322 return true; |
272 #endif | 323 #endif |
273 } | 324 } |
274 | 325 |
275 void CastBrowserMainParts::PostMainMessageLoopRun() { | 326 void CastBrowserMainParts::PostMainMessageLoopRun() { |
276 #if defined(OS_ANDROID) | 327 #if defined(OS_ANDROID) |
277 // Android does not use native main MessageLoop. | 328 // Android does not use native main MessageLoop. |
278 NOTREACHED(); | 329 NOTREACHED(); |
279 #else | 330 #else |
280 cast_browser_process_->cast_service()->Finalize(); | 331 cast_browser_process_->cast_service()->Finalize(); |
281 cast_browser_process_->metrics_service_client()->Finalize(); | 332 cast_browser_process_->metrics_service_client()->Finalize(); |
282 cast_browser_process_.reset(); | 333 cast_browser_process_.reset(); |
334 DeregisterKillOnAlarm(); | |
byungchul
2015/03/23 20:51:11
Not sure if it is useful.
maclellant
2015/03/23 21:12:45
It seems cleaner to me to remove any side effects
| |
283 #endif | 335 #endif |
284 } | 336 } |
285 | 337 |
286 } // namespace shell | 338 } // namespace shell |
287 } // namespace chromecast | 339 } // namespace chromecast |
OLD | NEW |