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
3 // found in the LICENSE file.
5 #include "content/browser/utility_process_host_impl.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/synchronization/lock.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "content/browser/browser_child_process_host_impl.h"
18 #include "content/browser/renderer_host/render_process_host_impl.h"
19 #include "content/common/child_process_host_impl.h"
20 #include "content/common/utility_messages.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/content_browser_client.h"
23 #include "content/public/browser/utility_process_host_client.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/common/process_type.h"
26 #include "ipc/ipc_switches.h"
27 #include "ui/base/ui_base_switches.h"
30 #include "content/public/common/sandboxed_process_launcher_delegate.h"
36 // NOTE: changes to this class need to be reviewed by the security team.
37 class UtilitySandboxedProcessLauncherDelegate
38 : public SandboxedProcessLauncherDelegate {
40 explicit UtilitySandboxedProcessLauncherDelegate(
41 const base::FilePath& exposed_dir) : exposed_dir_(exposed_dir) {}
42 virtual ~UtilitySandboxedProcessLauncherDelegate() {}
44 virtual void PreSandbox(bool* disable_default_policy,
45 base::FilePath* exposed_dir) OVERRIDE {
46 *exposed_dir = exposed_dir_;
50 base::FilePath exposed_dir_;
55 UtilityMainThreadFactoryFunction g_utility_main_thread_factory = NULL;
57 UtilityProcessHost* UtilityProcessHost::Create(
58 UtilityProcessHostClient* client,
59 base::SequencedTaskRunner* client_task_runner) {
60 return new UtilityProcessHostImpl(client, client_task_runner);
63 void UtilityProcessHost::RegisterUtilityMainThreadFactory(
64 UtilityMainThreadFactoryFunction create) {
65 g_utility_main_thread_factory = create;
68 UtilityProcessHostImpl::UtilityProcessHostImpl(
69 UtilityProcessHostClient* client,
70 base::SequencedTaskRunner* client_task_runner)
72 client_task_runner_(client_task_runner),
73 is_batch_mode_(false),
74 is_mdns_enabled_(false),
80 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF),
82 child_flags_(ChildProcessHost::CHILD_NORMAL),
87 UtilityProcessHostImpl::~UtilityProcessHostImpl() {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
93 bool UtilityProcessHostImpl::Send(IPC::Message* message) {
97 return process_->Send(message);
100 bool UtilityProcessHostImpl::StartBatchMode() {
101 CHECK(!is_batch_mode_);
102 is_batch_mode_ = StartProcess();
103 Send(new UtilityMsg_BatchMode_Started());
104 return is_batch_mode_;
107 void UtilityProcessHostImpl::EndBatchMode() {
108 CHECK(is_batch_mode_);
109 is_batch_mode_ = false;
110 Send(new UtilityMsg_BatchMode_Finished());
113 void UtilityProcessHostImpl::SetExposedDir(const base::FilePath& dir) {
117 void UtilityProcessHostImpl::EnableMDns() {
118 is_mdns_enabled_ = true;
121 void UtilityProcessHostImpl::DisableSandbox() {
126 void UtilityProcessHostImpl::ElevatePrivileges() {
128 run_elevated_ = true;
132 const ChildProcessData& UtilityProcessHostImpl::GetData() {
133 return process_->GetData();
136 #if defined(OS_POSIX)
138 void UtilityProcessHostImpl::SetEnv(const base::EnvironmentMap& env) {
144 bool UtilityProcessHostImpl::StartProcess() {
152 // Name must be set or metrics_service will crash in any test which
153 // launches a UtilityProcessHost.
154 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this));
155 process_->SetName(base::ASCIIToUTF16("utility process"));
157 std::string channel_id = process_->GetHost()->CreateChannel();
158 if (channel_id.empty())
161 if (RenderProcessHost::run_renderer_in_process()) {
162 DCHECK(g_utility_main_thread_factory);
163 // See comment in RenderProcessHostImpl::Init() for the background on why we
164 // support single process mode this way.
165 in_process_thread_.reset(g_utility_main_thread_factory(channel_id));
166 in_process_thread_->Start();
168 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
169 int child_flags = child_flags_;
171 #if defined(OS_POSIX)
172 bool has_cmd_prefix = browser_command_line.HasSwitch(
173 switches::kUtilityCmdPrefix);
175 // When running under gdb, forking /proc/self/exe ends up forking the gdb
176 // executable instead of Chromium. It is almost safe to assume that no
177 // updates will happen while a developer is running with
178 // |switches::kUtilityCmdPrefix|. See ChildProcessHost::GetChildPath() for
179 // a similar case with Valgrind.
181 child_flags = ChildProcessHost::CHILD_NORMAL;
184 base::FilePath exe_path = ChildProcessHost::GetChildPath(child_flags);
185 if (exe_path.empty()) {
186 NOTREACHED() << "Unable to get utility process binary name.";
190 CommandLine* cmd_line = new CommandLine(exe_path);
191 cmd_line->AppendSwitchASCII(switches::kProcessType,
192 switches::kUtilityProcess);
193 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
194 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
195 cmd_line->AppendSwitchASCII(switches::kLang, locale);
197 if (no_sandbox_ || browser_command_line.HasSwitch(switches::kNoSandbox))
198 cmd_line->AppendSwitch(switches::kNoSandbox);
199 #if defined(OS_MACOSX)
200 if (browser_command_line.HasSwitch(switches::kEnableSandboxLogging))
201 cmd_line->AppendSwitch(switches::kEnableSandboxLogging);
203 if (browser_command_line.HasSwitch(switches::kDebugPluginLoading))
204 cmd_line->AppendSwitch(switches::kDebugPluginLoading);
206 #if defined(OS_POSIX)
207 if (has_cmd_prefix) {
208 // Launch the utility child process with some prefix
209 // (usually "xterm -e gdb --args").
210 cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative(
211 switches::kUtilityCmdPrefix));
214 if (!exposed_dir_.empty()) {
215 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir,
220 if (is_mdns_enabled_)
221 cmd_line->AppendSwitch(switches::kUtilityProcessEnableMDns);
224 // Let the utility process know if it is intended to be elevated.
226 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated);
229 bool use_zygote = false;
231 #if defined(OS_LINUX)
232 // The Linux sandbox does not support granting access to a single directory,
233 // so we need to bypass the zygote in that case.
234 use_zygote = !no_sandbox_ && exposed_dir_.empty();
239 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_),
241 #elif defined(OS_POSIX)
251 bool UtilityProcessHostImpl::OnMessageReceived(const IPC::Message& message) {
252 client_task_runner_->PostTask(
254 base::Bind(base::IgnoreResult(
255 &UtilityProcessHostClient::OnMessageReceived), client_.get(),
260 void UtilityProcessHostImpl::OnProcessLaunchFailed() {
261 client_task_runner_->PostTask(
263 base::Bind(&UtilityProcessHostClient::OnProcessLaunchFailed,
267 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) {
268 client_task_runner_->PostTask(
270 base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(),
274 } // namespace content