1c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant// Use of this source code is governed by a BSD-style license that can be
3c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant// found in the LICENSE file.
4c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
5b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant#include "base/command_line.h"
6b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant#include "base/debug/leak_annotations.h"
7c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#include "base/message_loop/message_loop.h"
8c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#include "base/threading/platform_thread.h"
9c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#include "base/timer/hi_res_timer_manager.h"
10c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#include "content/child/child_process.h"
11c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#include "content/common/sandbox_linux/sandbox_linux.h"
12c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#include "content/public/common/content_switches.h"
13#include "content/public/common/main_function_params.h"
14#include "content/public/common/sandbox_init.h"
15#include "content/utility/utility_thread_impl.h"
16
17#if defined(OS_WIN)
18#include "sandbox/win/src/sandbox.h"
19#endif
20
21namespace content {
22
23// Mainline routine for running as the utility process.
24int UtilityMain(const MainFunctionParams& parameters) {
25  // The main message loop of the utility process.
26  base::MessageLoop main_message_loop;
27  base::PlatformThread::SetName("CrUtilityMain");
28
29#if defined(OS_LINUX)
30  // Initializes the sandbox before any threads are created.
31  // TODO(jorgelo): move this after GTK initialization when we enable a strict
32  // Seccomp-BPF policy.
33  if (parameters.zygote_child)
34    LinuxSandbox::InitializeSandbox();
35#endif
36
37  ChildProcess utility_process;
38  utility_process.set_main_thread(new UtilityThreadImpl());
39
40  base::HighResolutionTimerManager hi_res_timer_manager;
41
42#if defined(OS_WIN)
43  bool no_sandbox = parameters.command_line.HasSwitch(switches::kNoSandbox);
44  if (!no_sandbox) {
45    sandbox::TargetServices* target_services =
46        parameters.sandbox_info->target_services;
47    if (!target_services)
48      return false;
49    target_services->LowerToken();
50  }
51#endif
52
53  base::MessageLoop::current()->Run();
54
55#if defined(LEAK_SANITIZER)
56  // Invoke LeakSanitizer before shutting down the utility thread, to avoid
57  // reporting shutdown-only leaks.
58  __lsan_do_leak_check();
59#endif
60
61  return 0;
62}
63
64}  // namespace content
65