Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 1 | // 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 Marshall | fafbbc0 | 2017-08-25 18:21:06 | [diff] [blame] | 7 | #include "base/command_line.h" |
| 8 | #include "base/process/launch.h" |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 9 | #include "base/strings/stringprintf.h" |
Bo Liu | 168c864 | 2017-08-28 18:26:02 | [diff] [blame] | 10 | #include "content/browser/child_process_launcher.h" |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 11 | #include "content/public/browser/child_process_launcher_utils.h" |
Kevin Marshall | 65c2670 | 2017-09-25 18:21:42 | [diff] [blame] | 12 | #include "content/public/common/sandboxed_process_launcher_delegate.h" |
David Dorwin | ed2ac5c2 | 2022-03-05 02:04:58 | [diff] [blame] | 13 | #include "printing/buildflags/buildflags.h" |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 14 | #include "sandbox/policy/mojom/sandbox.mojom.h" |
Kevin Marshall | fafbbc0 | 2017-08-25 18:21:06 | [diff] [blame] | 15 | |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 16 | namespace content { |
| 17 | namespace internal { |
| 18 | |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 19 | namespace { |
| 20 | |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 21 | const char* ProcessNameFromSandboxType(sandbox::mojom::Sandbox sandbox_type) { |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 22 | switch (sandbox_type) { |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 23 | case sandbox::mojom::Sandbox::kNoSandbox: |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 24 | return nullptr; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 25 | case sandbox::mojom::Sandbox::kRenderer: |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 26 | return "renderer"; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 27 | case sandbox::mojom::Sandbox::kUtility: |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 28 | return "utility"; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 29 | case sandbox::mojom::Sandbox::kService: |
Wez | 250fd72 | 2021-08-23 19:31:16 | [diff] [blame] | 30 | return "service"; |
Alex Gough | 7242135 | 2021-12-21 11:08:31 | [diff] [blame] | 31 | case sandbox::mojom::Sandbox::kServiceWithJit: |
| 32 | return "service-with-jit"; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 33 | case sandbox::mojom::Sandbox::kGpu: |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 34 | return "gpu"; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 35 | case sandbox::mojom::Sandbox::kNetwork: |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 36 | return "network"; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 37 | case sandbox::mojom::Sandbox::kVideoCapture: |
Sharon Yang | efd8e88 | 2020-04-28 20:56:43 | [diff] [blame] | 38 | return "video-capture"; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 39 | case sandbox::mojom::Sandbox::kAudio: |
Wez | 250fd72 | 2021-08-23 19:31:16 | [diff] [blame] | 40 | return "audio"; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 41 | case sandbox::mojom::Sandbox::kCdm: |
Wez | 250fd72 | 2021-08-23 19:31:16 | [diff] [blame] | 42 | return "cdm"; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 43 | case sandbox::mojom::Sandbox::kPrintCompositor: |
Wez | 250fd72 | 2021-08-23 19:31:16 | [diff] [blame] | 44 | return "print-compositor"; |
Alex Gough | eb6a38f | 2021-10-22 01:55:13 | [diff] [blame] | 45 | case sandbox::mojom::Sandbox::kSpeechRecognition: |
Wez | 250fd72 | 2021-08-23 19:31:16 | [diff] [blame] | 46 | return "speech-recognition"; |
David Dorwin | ed2ac5c2 | 2022-03-05 02:04:58 | [diff] [blame] | 47 | #if BUILDFLAG(ENABLE_OOP_PRINTING) |
| 48 | case sandbox::mojom::Sandbox::kPrintBackend: |
| 49 | return "print-backend"; |
| 50 | #endif |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 56 | void ChildProcessLauncherHelper::SetProcessPriorityOnLauncherThread( |
| 57 | base::Process process, |
Bo Liu | 168c864 | 2017-08-28 18:26:02 | [diff] [blame] | 58 | const ChildProcessLauncherPriority& priority) { |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 59 | DCHECK(CurrentlyOnProcessLauncherTaskRunner()); |
Wez | 2b2c80a | 2019-01-31 23:03:22 | [diff] [blame] | 60 | // TODO(https://crbug.com/926583): Fuchsia does not currently support this. |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 61 | } |
| 62 | |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 63 | ChildProcessTerminationInfo ChildProcessLauncherHelper::GetTerminationInfo( |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 64 | const ChildProcessLauncherHelper::Process& process, |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 65 | bool known_dead) { |
| 66 | ChildProcessTerminationInfo info; |
| 67 | info.status = |
| 68 | base::GetTerminationStatus(process.process.Handle(), &info.exit_code); |
| 69 | return info; |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // static |
| 73 | bool ChildProcessLauncherHelper::TerminateProcess(const base::Process& process, |
Wez | 0abfbf51 | 2018-03-03 01:54:45 | [diff] [blame] | 74 | int exit_code) { |
| 75 | return process.Terminate(exit_code, false); |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 76 | } |
| 77 | |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 78 | void ChildProcessLauncherHelper::BeforeLaunchOnClientThread() { |
Ken Rockot | 8e23c3ab | 2019-08-01 23:39:10 | [diff] [blame] | 79 | DCHECK(client_task_runner_->RunsTasksInCurrentSequence()); |
Sergey Ulanov | 4eb9ad6 | 2018-07-06 00:15:02 | [diff] [blame] | 80 | |
Robert Sesek | 7d0b49b | 2020-07-08 18:31:27 | [diff] [blame] | 81 | sandbox_policy_ = std::make_unique<sandbox::policy::SandboxPolicyFuchsia>( |
Alex Gough | 31b34ac | 2020-04-28 14:35:48 | [diff] [blame] | 82 | delegate_->GetSandboxType()); |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 83 | } |
| 84 | |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 85 | std::unique_ptr<FileMappedForLaunch> |
| 86 | ChildProcessLauncherHelper::GetFilesToMap() { |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 87 | DCHECK(CurrentlyOnProcessLauncherTaskRunner()); |
Lei Zhang | df291f6 | 2021-04-14 17:23:44 | [diff] [blame] | 88 | return nullptr; |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 89 | } |
| 90 | |
Greg Kerr | a1bc9d0 | 2018-01-04 23:22:31 | [diff] [blame] | 91 | bool ChildProcessLauncherHelper::BeforeLaunchOnLauncherThread( |
Lucas Furukawa Gadani | 0d5e714 | 2019-04-18 23:00:50 | [diff] [blame] | 92 | PosixFileDescriptorInfo& files_to_register, |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 93 | base::LaunchOptions* options) { |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 94 | DCHECK(CurrentlyOnProcessLauncherTaskRunner()); |
Kevin Marshall | fafbbc0 | 2017-08-25 18:21:06 | [diff] [blame] | 95 | |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 96 | mojo_channel_->PrepareToPassRemoteEndpoint(&options->handles_to_transfer, |
| 97 | command_line()); |
Alex Gough | 31b34ac | 2020-04-28 14:35:48 | [diff] [blame] | 98 | sandbox_policy_->UpdateLaunchOptionsForSandbox(options); |
Sergey Ulanov | 4eb9ad6 | 2018-07-06 00:15:02 | [diff] [blame] | 99 | |
Sergey Ulanov | 53ab7dd | 2019-08-27 17:53:18 | [diff] [blame] | 100 | // 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 Kerr | a1bc9d0 | 2018-01-04 23:22:31 | [diff] [blame] | 106 | return true; |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | ChildProcessLauncherHelper::Process |
| 110 | ChildProcessLauncherHelper::LaunchProcessOnLauncherThread( |
| 111 | const base::LaunchOptions& options, |
| 112 | std::unique_ptr<FileMappedForLaunch> files_to_register, |
| 113 | bool* is_synchronous_launch, |
| 114 | int* launch_result) { |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 115 | DCHECK(CurrentlyOnProcessLauncherTaskRunner()); |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 116 | DCHECK(mojo_channel_); |
| 117 | DCHECK(mojo_channel_->remote_endpoint().is_valid()); |
Kevin Marshall | fafbbc0 | 2017-08-25 18:21:06 | [diff] [blame] | 118 | |
Kevin Marshall | fafbbc0 | 2017-08-25 18:21:06 | [diff] [blame] | 119 | Process child_process; |
| 120 | child_process.process = base::LaunchProcess(*command_line(), options); |
| 121 | return child_process; |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void ChildProcessLauncherHelper::AfterLaunchOnLauncherThread( |
| 125 | const ChildProcessLauncherHelper::Process& process, |
| 126 | const base::LaunchOptions& options) { |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // static |
| 130 | void ChildProcessLauncherHelper::ForceNormalProcessTerminationSync( |
| 131 | ChildProcessLauncherHelper::Process process) { |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 132 | DCHECK(CurrentlyOnProcessLauncherTaskRunner()); |
Ken Rockot | 1d00d6d0 | 2020-10-01 15:10:05 | [diff] [blame] | 133 | process.process.Terminate(RESULT_CODE_NORMAL_EXIT, true); |
Kevin Marshall | 3caecccb7 | 2017-08-17 01:47:37 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } // namespace internal |
| 137 | } // namespace content |