blob: ae8c6a2b4cafd9478f91b832abfe595a28c627ad [file] [log] [blame]
Kevin Marshall3caecccb72017-08-17 01:47:371// Copyright 2017 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.
4
5#include "content/browser/child_process_launcher_helper.h"
6
Kevin Marshallfafbbc02017-08-25 18:21:067#include "base/command_line.h"
8#include "base/process/launch.h"
Sergey Ulanov53ab7dd2019-08-27 17:53:189#include "base/strings/stringprintf.h"
Bo Liu168c8642017-08-28 18:26:0210#include "content/browser/child_process_launcher.h"
Xi Han89d93df2018-03-09 20:55:0711#include "content/public/browser/child_process_launcher_utils.h"
Kevin Marshall65c26702017-09-25 18:21:4212#include "content/public/common/sandboxed_process_launcher_delegate.h"
David Dorwined2ac5c22022-03-05 02:04:5813#include "printing/buildflags/buildflags.h"
Alex Gougheb6a38f2021-10-22 01:55:1314#include "sandbox/policy/mojom/sandbox.mojom.h"
Kevin Marshallfafbbc02017-08-25 18:21:0615
Kevin Marshall3caecccb72017-08-17 01:47:3716namespace content {
17namespace internal {
18
Sergey Ulanov53ab7dd2019-08-27 17:53:1819namespace {
20
Alex Gougheb6a38f2021-10-22 01:55:1321const char* ProcessNameFromSandboxType(sandbox::mojom::Sandbox sandbox_type) {
Sergey Ulanov53ab7dd2019-08-27 17:53:1822 switch (sandbox_type) {
Alex Gougheb6a38f2021-10-22 01:55:1323 case sandbox::mojom::Sandbox::kNoSandbox:
Sergey Ulanov53ab7dd2019-08-27 17:53:1824 return nullptr;
Alex Gougheb6a38f2021-10-22 01:55:1325 case sandbox::mojom::Sandbox::kRenderer:
Sergey Ulanov53ab7dd2019-08-27 17:53:1826 return "renderer";
Alex Gougheb6a38f2021-10-22 01:55:1327 case sandbox::mojom::Sandbox::kUtility:
Sergey Ulanov53ab7dd2019-08-27 17:53:1828 return "utility";
Alex Gougheb6a38f2021-10-22 01:55:1329 case sandbox::mojom::Sandbox::kService:
Wez250fd722021-08-23 19:31:1630 return "service";
Alex Gough72421352021-12-21 11:08:3131 case sandbox::mojom::Sandbox::kServiceWithJit:
32 return "service-with-jit";
Alex Gougheb6a38f2021-10-22 01:55:1333 case sandbox::mojom::Sandbox::kGpu:
Sergey Ulanov53ab7dd2019-08-27 17:53:1834 return "gpu";
Alex Gougheb6a38f2021-10-22 01:55:1335 case sandbox::mojom::Sandbox::kNetwork:
Sergey Ulanov53ab7dd2019-08-27 17:53:1836 return "network";
Alex Gougheb6a38f2021-10-22 01:55:1337 case sandbox::mojom::Sandbox::kVideoCapture:
Sharon Yangefd8e882020-04-28 20:56:4338 return "video-capture";
Alex Gougheb6a38f2021-10-22 01:55:1339 case sandbox::mojom::Sandbox::kAudio:
Wez250fd722021-08-23 19:31:1640 return "audio";
Alex Gougheb6a38f2021-10-22 01:55:1341 case sandbox::mojom::Sandbox::kCdm:
Wez250fd722021-08-23 19:31:1642 return "cdm";
Alex Gougheb6a38f2021-10-22 01:55:1343 case sandbox::mojom::Sandbox::kPrintCompositor:
Wez250fd722021-08-23 19:31:1644 return "print-compositor";
Alex Gougheb6a38f2021-10-22 01:55:1345 case sandbox::mojom::Sandbox::kSpeechRecognition:
Wez250fd722021-08-23 19:31:1646 return "speech-recognition";
David Dorwined2ac5c22022-03-05 02:04:5847#if BUILDFLAG(ENABLE_OOP_PRINTING)
48 case sandbox::mojom::Sandbox::kPrintBackend:
49 return "print-backend";
50#endif
Sergey Ulanov53ab7dd2019-08-27 17:53:1851 }
52}
53
54} // namespace
55
Kevin Marshall3caecccb72017-08-17 01:47:3756void ChildProcessLauncherHelper::SetProcessPriorityOnLauncherThread(
57 base::Process process,
Bo Liu168c8642017-08-28 18:26:0258 const ChildProcessLauncherPriority& priority) {
Xi Han89d93df2018-03-09 20:55:0759 DCHECK(CurrentlyOnProcessLauncherTaskRunner());
Wez2b2c80a2019-01-31 23:03:2260 // TODO(https://crbug.com/926583): Fuchsia does not currently support this.
Kevin Marshall3caecccb72017-08-17 01:47:3761}
62
Bo Liu0d2a2322018-04-19 00:18:0963ChildProcessTerminationInfo ChildProcessLauncherHelper::GetTerminationInfo(
Kevin Marshall3caecccb72017-08-17 01:47:3764 const ChildProcessLauncherHelper::Process& process,
Bo Liu0d2a2322018-04-19 00:18:0965 bool known_dead) {
66 ChildProcessTerminationInfo info;
67 info.status =
68 base::GetTerminationStatus(process.process.Handle(), &info.exit_code);
69 return info;
Kevin Marshall3caecccb72017-08-17 01:47:3770}
71
72// static
73bool ChildProcessLauncherHelper::TerminateProcess(const base::Process& process,
Wez0abfbf512018-03-03 01:54:4574 int exit_code) {
75 return process.Terminate(exit_code, false);
Kevin Marshall3caecccb72017-08-17 01:47:3776}
77
Kevin Marshall3caecccb72017-08-17 01:47:3778void ChildProcessLauncherHelper::BeforeLaunchOnClientThread() {
Ken Rockot8e23c3ab2019-08-01 23:39:1079 DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
Sergey Ulanov4eb9ad62018-07-06 00:15:0280
Robert Sesek7d0b49b2020-07-08 18:31:2781 sandbox_policy_ = std::make_unique<sandbox::policy::SandboxPolicyFuchsia>(
Alex Gough31b34ac2020-04-28 14:35:4882 delegate_->GetSandboxType());
Kevin Marshall3caecccb72017-08-17 01:47:3783}
84
Kevin Marshall3caecccb72017-08-17 01:47:3785std::unique_ptr<FileMappedForLaunch>
86ChildProcessLauncherHelper::GetFilesToMap() {
Xi Han89d93df2018-03-09 20:55:0787 DCHECK(CurrentlyOnProcessLauncherTaskRunner());
Lei Zhangdf291f62021-04-14 17:23:4488 return nullptr;
Kevin Marshall3caecccb72017-08-17 01:47:3789}
90
Greg Kerra1bc9d02018-01-04 23:22:3191bool ChildProcessLauncherHelper::BeforeLaunchOnLauncherThread(
Lucas Furukawa Gadani0d5e7142019-04-18 23:00:5092 PosixFileDescriptorInfo& files_to_register,
Kevin Marshall3caecccb72017-08-17 01:47:3793 base::LaunchOptions* options) {
Xi Han89d93df2018-03-09 20:55:0794 DCHECK(CurrentlyOnProcessLauncherTaskRunner());
Kevin Marshallfafbbc02017-08-25 18:21:0695
Ken Rockot026afc32018-06-04 19:19:1896 mojo_channel_->PrepareToPassRemoteEndpoint(&options->handles_to_transfer,
97 command_line());
Alex Gough31b34ac2020-04-28 14:35:4898 sandbox_policy_->UpdateLaunchOptionsForSandbox(options);
Sergey Ulanov4eb9ad62018-07-06 00:15:0299
Sergey Ulanov53ab7dd2019-08-27 17:53:18100 // Set process name suffix to make it easier to identify the process.
101 const char* process_type =
102 ProcessNameFromSandboxType(delegate_->GetSandboxType());
103 if (process_type)
104 options->process_name_suffix = base::StringPrintf(":%s", process_type);
105
Greg Kerra1bc9d02018-01-04 23:22:31106 return true;
Kevin Marshall3caecccb72017-08-17 01:47:37107}
108
109ChildProcessLauncherHelper::Process
110ChildProcessLauncherHelper::LaunchProcessOnLauncherThread(
111 const base::LaunchOptions& options,
112 std::unique_ptr<FileMappedForLaunch> files_to_register,
113 bool* is_synchronous_launch,
114 int* launch_result) {
Xi Han89d93df2018-03-09 20:55:07115 DCHECK(CurrentlyOnProcessLauncherTaskRunner());
Ken Rockot026afc32018-06-04 19:19:18116 DCHECK(mojo_channel_);
117 DCHECK(mojo_channel_->remote_endpoint().is_valid());
Kevin Marshallfafbbc02017-08-25 18:21:06118
Kevin Marshallfafbbc02017-08-25 18:21:06119 Process child_process;
120 child_process.process = base::LaunchProcess(*command_line(), options);
121 return child_process;
Kevin Marshall3caecccb72017-08-17 01:47:37122}
123
124void ChildProcessLauncherHelper::AfterLaunchOnLauncherThread(
125 const ChildProcessLauncherHelper::Process& process,
126 const base::LaunchOptions& options) {
Kevin Marshall3caecccb72017-08-17 01:47:37127}
128
129// static
130void ChildProcessLauncherHelper::ForceNormalProcessTerminationSync(
131 ChildProcessLauncherHelper::Process process) {
Xi Han89d93df2018-03-09 20:55:07132 DCHECK(CurrentlyOnProcessLauncherTaskRunner());
Ken Rockot1d00d6d02020-10-01 15:10:05133 process.process.Terminate(RESULT_CODE_NORMAL_EXIT, true);
Kevin Marshall3caecccb72017-08-17 01:47:37134}
135
136} // namespace internal
137} // namespace content