OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/public/app/content_main.h" | 5 #include "content/app/content_main.h" |
6 | 6 |
| 7 #include "base/at_exit.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/debug/debugger.h" |
| 10 #include "base/debug/trace_event.h" |
| 11 #include "base/i18n/icu_util.h" |
| 12 #include "base/logging.h" |
7 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
8 #include "content/public/app/content_main_runner.h" | 14 #include "base/metrics/stats_table.h" |
| 15 #include "base/process_util.h" |
| 16 #include "base/stringprintf.h" |
| 17 #include "base/string_number_conversions.h" |
| 18 #include "content/browser/browser_main.h" |
| 19 #include "content/common/set_process_title.h" |
| 20 #include "content/public/app/content_main_delegate.h" |
| 21 #include "content/public/app/startup_helper_win.h" |
| 22 #include "content/public/common/content_client.h" |
| 23 #include "content/public/common/content_constants.h" |
| 24 #include "content/public/common/content_paths.h" |
| 25 #include "content/public/common/content_switches.h" |
| 26 #include "content/public/common/main_function_params.h" |
| 27 #include "content/public/common/sandbox_init.h" |
| 28 #include "crypto/nss_util.h" |
| 29 #include "ipc/ipc_switches.h" |
| 30 #include "ui/base/ui_base_switches.h" |
| 31 #include "ui/base/ui_base_paths.h" |
| 32 #include "webkit/glue/webkit_glue.h" |
| 33 |
| 34 #if defined(OS_WIN) |
| 35 #include <atlbase.h> |
| 36 #include <atlapp.h> |
| 37 #include <malloc.h> |
| 38 #elif defined(OS_MACOSX) |
| 39 #include "base/mac/scoped_nsautorelease_pool.h" |
| 40 #include "base/mach_ipc_mac.h" |
| 41 #include "base/system_monitor/system_monitor.h" |
| 42 #include "content/browser/mach_broker_mac.h" |
| 43 #include "content/common/sandbox_init_mac.h" |
| 44 #endif // OS_WIN |
| 45 |
| 46 #if defined(OS_POSIX) |
| 47 #include <signal.h> |
| 48 |
| 49 #include "base/global_descriptors_posix.h" |
| 50 #include "content/common/chrome_descriptors.h" |
| 51 |
| 52 #if !defined(OS_MACOSX) |
| 53 #include "content/public/common/zygote_fork_delegate_linux.h" |
| 54 #endif |
| 55 |
| 56 #endif // OS_POSIX |
| 57 |
| 58 #if !defined(OS_MACOSX) && defined(USE_TCMALLOC) |
| 59 extern "C" { |
| 60 int tc_set_new_mode(int mode); |
| 61 } |
| 62 #endif |
| 63 |
| 64 extern int GpuMain(const content::MainFunctionParams&); |
| 65 extern int PluginMain(const content::MainFunctionParams&); |
| 66 extern int PpapiPluginMain(const content::MainFunctionParams&); |
| 67 extern int PpapiBrokerMain(const content::MainFunctionParams&); |
| 68 extern int RendererMain(const content::MainFunctionParams&); |
| 69 extern int WorkerMain(const content::MainFunctionParams&); |
| 70 extern int UtilityMain(const content::MainFunctionParams&); |
| 71 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 72 extern int ZygoteMain(const content::MainFunctionParams&, |
| 73 content::ZygoteForkDelegate* forkdelegate); |
| 74 #endif |
| 75 |
| 76 namespace { |
| 77 |
| 78 #if defined(OS_WIN) |
| 79 |
| 80 static CAppModule _Module; |
| 81 |
| 82 #elif defined(OS_MACOSX) |
| 83 |
| 84 // Completes the Mach IPC handshake by sending this process' task port to the |
| 85 // parent process. The parent is listening on the Mach port given by |
| 86 // |GetMachPortName()|. The task port is used by the parent to get CPU/memory |
| 87 // stats to display in the task manager. |
| 88 void SendTaskPortToParentProcess() { |
| 89 const mach_msg_timeout_t kTimeoutMs = 100; |
| 90 const int32_t kMessageId = 0; |
| 91 std::string mach_port_name = MachBroker::GetMachPortName(); |
| 92 |
| 93 base::MachSendMessage child_message(kMessageId); |
| 94 if (!child_message.AddDescriptor(mach_task_self())) { |
| 95 LOG(ERROR) << "child AddDescriptor(mach_task_self()) failed."; |
| 96 return; |
| 97 } |
| 98 |
| 99 base::MachPortSender child_sender(mach_port_name.c_str()); |
| 100 kern_return_t err = child_sender.SendMessage(child_message, kTimeoutMs); |
| 101 if (err != KERN_SUCCESS) { |
| 102 LOG(ERROR) << StringPrintf("child SendMessage() failed: 0x%x %s", err, |
| 103 mach_error_string(err)); |
| 104 } |
| 105 } |
| 106 |
| 107 #endif // defined(OS_WIN) |
| 108 |
| 109 #if defined(OS_POSIX) |
| 110 |
| 111 // Setup signal-handling state: resanitize most signals, ignore SIGPIPE. |
| 112 void SetupSignalHandlers() { |
| 113 // Sanitise our signal handling state. Signals that were ignored by our |
| 114 // parent will also be ignored by us. We also inherit our parent's sigmask. |
| 115 sigset_t empty_signal_set; |
| 116 CHECK(0 == sigemptyset(&empty_signal_set)); |
| 117 CHECK(0 == sigprocmask(SIG_SETMASK, &empty_signal_set, NULL)); |
| 118 |
| 119 struct sigaction sigact; |
| 120 memset(&sigact, 0, sizeof(sigact)); |
| 121 sigact.sa_handler = SIG_DFL; |
| 122 static const int signals_to_reset[] = |
| 123 {SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGSEGV, |
| 124 SIGALRM, SIGTERM, SIGCHLD, SIGBUS, SIGTRAP}; // SIGPIPE is set below. |
| 125 for (unsigned i = 0; i < arraysize(signals_to_reset); i++) { |
| 126 CHECK(0 == sigaction(signals_to_reset[i], &sigact, NULL)); |
| 127 } |
| 128 |
| 129 // Always ignore SIGPIPE. We check the return value of write(). |
| 130 CHECK(signal(SIGPIPE, SIG_IGN) != SIG_ERR); |
| 131 } |
| 132 |
| 133 #endif // OS_POSIX |
| 134 |
| 135 void CommonSubprocessInit(const std::string& process_type) { |
| 136 #if defined(OS_WIN) |
| 137 // HACK: Let Windows know that we have started. This is needed to suppress |
| 138 // the IDC_APPSTARTING cursor from being displayed for a prolonged period |
| 139 // while a subprocess is starting. |
| 140 PostThreadMessage(GetCurrentThreadId(), WM_NULL, 0, 0); |
| 141 MSG msg; |
| 142 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
| 143 #endif |
| 144 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 145 // Various things break when you're using a locale where the decimal |
| 146 // separator isn't a period. See e.g. bugs 22782 and 39964. For |
| 147 // all processes except the browser process (where we call system |
| 148 // APIs that may rely on the correct locale for formatting numbers |
| 149 // when presenting them to the user), reset the locale for numeric |
| 150 // formatting. |
| 151 // Note that this is not correct for plugin processes -- they can |
| 152 // surface UI -- but it's likely they get this wrong too so why not. |
| 153 setlocale(LC_NUMERIC, "C"); |
| 154 #endif |
| 155 } |
| 156 |
| 157 void InitializeStatsTable(base::ProcessId browser_pid, |
| 158 const CommandLine& command_line) { |
| 159 // Initialize the Stats Counters table. With this initialized, |
| 160 // the StatsViewer can be utilized to read counters outside of |
| 161 // Chrome. These lines can be commented out to effectively turn |
| 162 // counters 'off'. The table is created and exists for the life |
| 163 // of the process. It is not cleaned up. |
| 164 if (command_line.HasSwitch(switches::kEnableStatsTable)) { |
| 165 // NOTIMPLEMENTED: we probably need to shut this down correctly to avoid |
| 166 // leaking shared memory regions on posix platforms. |
| 167 std::string statsfile = |
| 168 base::StringPrintf("%s-%u", |
| 169 content::kStatsFilename, |
| 170 static_cast<unsigned int>(browser_pid)); |
| 171 base::StatsTable* stats_table = new base::StatsTable(statsfile, |
| 172 content::kStatsMaxThreads, content::kStatsMaxCounters); |
| 173 base::StatsTable::set_current(stats_table); |
| 174 } |
| 175 } |
| 176 |
| 177 // We dispatch to a process-type-specific FooMain() based on a command-line |
| 178 // flag. This struct is used to build a table of (flag, main function) pairs. |
| 179 struct MainFunction { |
| 180 const char* name; |
| 181 int (*function)(const content::MainFunctionParams&); |
| 182 }; |
| 183 |
| 184 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 185 // On platforms that use the zygote, we have a special subset of |
| 186 // subprocesses that are launched via the zygote. This function |
| 187 // fills in some process-launching bits around ZygoteMain(). |
| 188 // Returns the exit code of the subprocess. |
| 189 int RunZygote(const content::MainFunctionParams& main_function_params, |
| 190 content::ContentMainDelegate* delegate) { |
| 191 static const MainFunction kMainFunctions[] = { |
| 192 { switches::kRendererProcess, RendererMain }, |
| 193 { switches::kWorkerProcess, WorkerMain }, |
| 194 { switches::kPpapiPluginProcess, PpapiPluginMain }, |
| 195 { switches::kUtilityProcess, UtilityMain }, |
| 196 }; |
| 197 |
| 198 scoped_ptr<content::ZygoteForkDelegate> zygote_fork_delegate; |
| 199 if (delegate) zygote_fork_delegate.reset(delegate->ZygoteStarting()); |
| 200 |
| 201 // This function call can return multiple times, once per fork(). |
| 202 if (!ZygoteMain(main_function_params, zygote_fork_delegate.get())) |
| 203 return 1; |
| 204 |
| 205 if (delegate) delegate->ZygoteForked(); |
| 206 |
| 207 // Zygote::HandleForkRequest may have reallocated the command |
| 208 // line so update it here with the new version. |
| 209 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 210 |
| 211 // If a custom user agent was passed on the command line, we need |
| 212 // to (re)set it now, rather than using the default one the zygote |
| 213 // initialized. |
| 214 bool custom = false; |
| 215 std::string ua = content::GetContentClient()->GetUserAgent(&custom); |
| 216 if (custom) webkit_glue::SetUserAgent(ua, custom); |
| 217 |
| 218 // The StatsTable must be initialized in each process; we already |
| 219 // initialized for the browser process, now we need to initialize |
| 220 // within the new processes as well. |
| 221 pid_t browser_pid = base::GetParentProcessId( |
| 222 base::GetParentProcessId(base::GetCurrentProcId())); |
| 223 InitializeStatsTable(browser_pid, command_line); |
| 224 |
| 225 content::MainFunctionParams main_params(command_line); |
| 226 |
| 227 // Get the new process type from the new command line. |
| 228 std::string process_type = |
| 229 command_line.GetSwitchValueASCII(switches::kProcessType); |
| 230 |
| 231 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { |
| 232 if (process_type == kMainFunctions[i].name) |
| 233 return kMainFunctions[i].function(main_params); |
| 234 } |
| 235 |
| 236 if (delegate) |
| 237 return delegate->RunProcess(process_type, main_params); |
| 238 |
| 239 NOTREACHED() << "Unknown zygote process type: " << process_type; |
| 240 return 1; |
| 241 } |
| 242 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) |
| 243 |
| 244 // Run the FooMain() for a given process type. |
| 245 // If |process_type| is empty, runs BrowserMain(). |
| 246 // Returns the exit code for this process. |
| 247 int RunNamedProcessTypeMain( |
| 248 const std::string& process_type, |
| 249 const content::MainFunctionParams& main_function_params, |
| 250 content::ContentMainDelegate* delegate) { |
| 251 static const MainFunction kMainFunctions[] = { |
| 252 { "", BrowserMain }, |
| 253 { switches::kRendererProcess, RendererMain }, |
| 254 { switches::kPluginProcess, PluginMain }, |
| 255 { switches::kWorkerProcess, WorkerMain }, |
| 256 { switches::kPpapiPluginProcess, PpapiPluginMain }, |
| 257 { switches::kPpapiBrokerProcess, PpapiBrokerMain }, |
| 258 { switches::kUtilityProcess, UtilityMain }, |
| 259 { switches::kGpuProcess, GpuMain }, |
| 260 }; |
| 261 |
| 262 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { |
| 263 if (process_type == kMainFunctions[i].name) |
| 264 return kMainFunctions[i].function(main_function_params); |
| 265 } |
| 266 |
| 267 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 268 // Zygote startup is special -- see RunZygote comments above |
| 269 // for why we don't use ZygoteMain directly. |
| 270 if (process_type == switches::kZygoteProcess) |
| 271 return RunZygote(main_function_params, delegate); |
| 272 #endif |
| 273 |
| 274 // If it's a process we don't know about, the embedder should know. |
| 275 if (delegate) |
| 276 return delegate->RunProcess(process_type, main_function_params); |
| 277 |
| 278 NOTREACHED() << "Unknown process type: " << process_type; |
| 279 return 1; |
| 280 } |
| 281 |
| 282 } // namespace |
9 | 283 |
10 namespace content { | 284 namespace content { |
11 | 285 |
12 #if defined(OS_WIN) | 286 #if defined(OS_WIN) |
13 int ContentMain(HINSTANCE instance, | 287 int ContentMain(HINSTANCE instance, |
14 sandbox::SandboxInterfaceInfo* sandbox_info, | 288 sandbox::SandboxInterfaceInfo* sandbox_info, |
15 ContentMainDelegate* delegate) { | 289 ContentMainDelegate* delegate) { |
| 290 // argc/argv are ignored on Windows; see command_line.h for details. |
| 291 int argc = 0; |
| 292 char** argv = NULL; |
| 293 |
| 294 content::RegisterInvalidParamHandler(); |
| 295 _Module.Init(NULL, static_cast<HINSTANCE>(instance)); |
16 #else | 296 #else |
17 int ContentMain(int argc, | 297 int ContentMain(int argc, |
18 const char** argv, | 298 const char** argv, |
19 ContentMainDelegate* delegate) { | 299 ContentMainDelegate* delegate) { |
| 300 // NOTE(willchan): One might ask why this call is done here rather than in |
| 301 // process_util_linux.cc with the definition of |
| 302 // EnableTerminationOnOutOfMemory(). That's because base shouldn't have a |
| 303 // dependency on TCMalloc. Really, we ought to have our allocator shim code |
| 304 // implement this EnableTerminationOnOutOfMemory() function. Whateverz. This |
| 305 // works for now. |
| 306 #if !defined(OS_MACOSX) && defined(USE_TCMALLOC) |
| 307 // For tcmalloc, we need to tell it to behave like new. |
| 308 tc_set_new_mode(1); |
| 309 #endif |
| 310 |
| 311 #if !defined(OS_ANDROID) |
| 312 // Set C library locale to make sure CommandLine can parse argument values |
| 313 // in correct encoding. |
| 314 setlocale(LC_ALL, ""); |
| 315 #endif |
| 316 |
| 317 SetupSignalHandlers(); |
| 318 |
| 319 base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); |
| 320 g_fds->Set(kPrimaryIPCChannel, |
| 321 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); |
| 322 #if defined(OS_LINUX) || defined(OS_OPENBSD) |
| 323 g_fds->Set(kCrashDumpSignal, |
| 324 kCrashDumpSignal + base::GlobalDescriptors::kBaseDescriptor); |
| 325 #endif |
| 326 |
20 #endif // OS_WIN | 327 #endif // OS_WIN |
21 | 328 |
22 scoped_ptr<ContentMainRunner> main_runner(ContentMainRunner::Create()); | 329 base::EnableTerminationOnHeapCorruption(); |
| 330 base::EnableTerminationOnOutOfMemory(); |
| 331 |
| 332 // The exit manager is in charge of calling the dtors of singleton objects. |
| 333 base::AtExitManager exit_manager; |
| 334 |
| 335 #if defined(OS_MACOSX) |
| 336 // We need this pool for all the objects created before we get to the |
| 337 // event loop, but we don't want to leave them hanging around until the |
| 338 // app quits. Each "main" needs to flush this pool right before it goes into |
| 339 // its main event loop to get rid of the cruft. |
| 340 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
| 341 #endif |
| 342 |
| 343 CommandLine::Init(argc, argv); |
23 | 344 |
24 int exit_code; | 345 int exit_code; |
25 | 346 if (delegate && delegate->BasicStartupComplete(&exit_code)) |
26 #if defined(OS_WIN) | 347 return exit_code; |
27 exit_code = main_runner->Initialize(instance, sandbox_info, delegate); | 348 |
28 #else | 349 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
29 exit_code = main_runner->Initialize(argc, argv, delegate); | 350 std::string process_type = |
| 351 command_line.GetSwitchValueASCII(switches::kProcessType); |
| 352 |
| 353 // Enable startup tracing asap to avoid early TRACE_EVENT calls being ignored. |
| 354 if (command_line.HasSwitch(switches::kTraceStartup)) { |
| 355 base::debug::TraceLog::GetInstance()->SetEnabled( |
| 356 command_line.GetSwitchValueASCII(switches::kTraceStartup)); |
| 357 } |
| 358 |
| 359 #if defined(OS_MACOSX) |
| 360 // We need to allocate the IO Ports before the Sandbox is initialized or |
| 361 // the first instance of SystemMonitor is created. |
| 362 // It's important not to allocate the ports for processes which don't register |
| 363 // with the system monitor - see crbug.com/88867. |
| 364 if (process_type.empty() || |
| 365 process_type == switches::kPluginProcess || |
| 366 process_type == switches::kRendererProcess || |
| 367 process_type == switches::kUtilityProcess || |
| 368 process_type == switches::kWorkerProcess || |
| 369 (delegate && delegate->ProcessRegistersWithSystemProcess(process_type))) { |
| 370 base::SystemMonitor::AllocateSystemIOPorts(); |
| 371 } |
| 372 |
| 373 if (!process_type.empty() && |
| 374 (!delegate || delegate->ShouldSendMachPort(process_type))) { |
| 375 SendTaskPortToParentProcess(); |
| 376 } |
| 377 #elif defined(OS_WIN) |
| 378 content::SetupCRT(command_line); |
| 379 #endif |
| 380 |
| 381 #if defined(OS_POSIX) |
| 382 if (!process_type.empty()) { |
| 383 // When you hit Ctrl-C in a terminal running the browser |
| 384 // process, a SIGINT is delivered to the entire process group. |
| 385 // When debugging the browser process via gdb, gdb catches the |
| 386 // SIGINT for the browser process (and dumps you back to the gdb |
| 387 // console) but doesn't for the child processes, killing them. |
| 388 // The fix is to have child processes ignore SIGINT; they'll die |
| 389 // on their own when the browser process goes away. |
| 390 // |
| 391 // Note that we *can't* rely on BeingDebugged to catch this case because we |
| 392 // are the child process, which is not being debugged. |
| 393 // TODO(evanm): move this to some shared subprocess-init function. |
| 394 if (!base::debug::BeingDebugged()) |
| 395 signal(SIGINT, SIG_IGN); |
| 396 } |
| 397 #endif |
| 398 |
| 399 #if defined(USE_NSS) |
| 400 crypto::EarlySetupForNSSInit(); |
| 401 #endif |
| 402 |
| 403 ui::RegisterPathProvider(); |
| 404 content::RegisterPathProvider(); |
| 405 |
| 406 CHECK(icu_util::Initialize()); |
| 407 |
| 408 base::ProcessId browser_pid = base::GetCurrentProcId(); |
| 409 if (command_line.HasSwitch(switches::kProcessChannelID)) { |
| 410 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 411 std::string channel_name = |
| 412 command_line.GetSwitchValueASCII(switches::kProcessChannelID); |
| 413 |
| 414 int browser_pid_int; |
| 415 base::StringToInt(channel_name, &browser_pid_int); |
| 416 browser_pid = static_cast<base::ProcessId>(browser_pid_int); |
| 417 DCHECK_NE(browser_pid_int, 0); |
| 418 #elif defined(OS_POSIX) |
| 419 // On linux, we're in the zygote here; so we need the parent process' id. |
| 420 browser_pid = base::GetParentProcessId(base::GetCurrentProcId()); |
| 421 #endif |
| 422 } |
| 423 |
| 424 InitializeStatsTable(browser_pid, command_line); |
| 425 |
| 426 if (delegate) delegate->PreSandboxStartup(); |
| 427 |
| 428 if (!process_type.empty()) |
| 429 CommonSubprocessInit(process_type); |
| 430 |
| 431 #if defined(OS_WIN) |
| 432 CHECK(content::InitializeSandbox(sandbox_info)); |
| 433 #elif defined(OS_MACOSX) |
| 434 if (process_type == switches::kRendererProcess || |
| 435 process_type == switches::kPpapiPluginProcess || |
| 436 (delegate && delegate->DelaySandboxInitialization(process_type))) { |
| 437 // On OS X the renderer sandbox needs to be initialized later in the startup |
| 438 // sequence in RendererMainPlatformDelegate::EnableSandbox(). |
| 439 } else { |
| 440 CHECK(content::InitializeSandbox()); |
| 441 } |
| 442 #endif |
| 443 |
| 444 if (delegate) delegate->SandboxInitialized(process_type); |
| 445 |
| 446 #if defined(OS_POSIX) |
| 447 SetProcessTitleFromCommandLine(argv); |
| 448 #endif |
| 449 |
| 450 content::MainFunctionParams main_params(command_line); |
| 451 #if defined(OS_WIN) |
| 452 main_params.sandbox_info = sandbox_info; |
| 453 #elif defined(OS_MACOSX) |
| 454 main_params.autorelease_pool = &autorelease_pool; |
| 455 #endif |
| 456 |
| 457 exit_code = RunNamedProcessTypeMain(process_type, main_params, delegate); |
| 458 |
| 459 if (delegate) delegate->ProcessExiting(process_type); |
| 460 |
| 461 #if defined(OS_WIN) |
| 462 #ifdef _CRTDBG_MAP_ALLOC |
| 463 _CrtDumpMemoryLeaks(); |
| 464 #endif // _CRTDBG_MAP_ALLOC |
| 465 |
| 466 _Module.Term(); |
30 #endif // OS_WIN | 467 #endif // OS_WIN |
31 | 468 |
32 if (exit_code >= 0) | |
33 return exit_code; | |
34 | |
35 exit_code = main_runner->Run(); | |
36 | |
37 main_runner->Shutdown(); | |
38 | |
39 return exit_code; | 469 return exit_code; |
40 } | 470 } |
41 | 471 |
42 } // namespace content | 472 } // namespace content |
OLD | NEW |