forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchild_process_launcher_helper_linux.cc
166 lines (146 loc) · 6.13 KB
/
child_process_launcher_helper_linux.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/path_service.h"
#include "base/posix/global_descriptors.h"
#include "content/browser/child_process_launcher.h"
#include "content/browser/child_process_launcher_helper.h"
#include "content/browser/child_process_launcher_helper_posix.h"
#include "content/browser/sandbox_host_linux.h"
#include "content/browser/zygote_host/zygote_communication_linux.h"
#include "content/browser/zygote_host/zygote_host_impl_linux.h"
#include "content/common/sandbox_linux/sandbox_linux.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/zygote_handle_linux.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/sandboxed_process_launcher_delegate.h"
#include "gpu/config/gpu_switches.h"
namespace content {
namespace internal {
mojo::edk::ScopedPlatformHandle
ChildProcessLauncherHelper::PrepareMojoPipeHandlesOnClientThread() {
DCHECK_CURRENTLY_ON(client_thread_id_);
return mojo::edk::ScopedPlatformHandle();
}
void ChildProcessLauncherHelper::BeforeLaunchOnClientThread() {
DCHECK_CURRENTLY_ON(client_thread_id_);
}
std::unique_ptr<FileMappedForLaunch>
ChildProcessLauncherHelper::GetFilesToMap() {
DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER);
return CreateDefaultPosixFilesToMap(child_process_id(), mojo_client_handle(),
true /* include_service_required_files */,
GetProcessType(), command_line());
}
void ChildProcessLauncherHelper::BeforeLaunchOnLauncherThread(
const PosixFileDescriptorInfo& files_to_register,
base::LaunchOptions* options) {
// Convert FD mapping to FileHandleMappingVector
options->fds_to_remap = files_to_register.GetMappingWithIDAdjustment(
base::GlobalDescriptors::kBaseDescriptor);
if (GetProcessType() == switches::kRendererProcess ||
(GetProcessType() == switches::kGpuProcess &&
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableOOPRasterization))) {
const int sandbox_fd = SandboxHostLinux::GetInstance()->GetChildSocket();
options->fds_to_remap.push_back(std::make_pair(sandbox_fd, GetSandboxFD()));
}
options->environ = delegate_->GetEnvironment();
}
ChildProcessLauncherHelper::Process
ChildProcessLauncherHelper::LaunchProcessOnLauncherThread(
const base::LaunchOptions& options,
std::unique_ptr<FileMappedForLaunch> files_to_register,
bool* is_synchronous_launch,
int* launch_result) {
*is_synchronous_launch = true;
ZygoteHandle zygote_handle =
base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoZygote)
? nullptr
: delegate_->GetZygote();
if (zygote_handle) {
// TODO(crbug.com/569191): If chrome supported multiple zygotes they could
// be created lazily here, or in the delegate GetZygote() implementations.
// Additionally, the delegate could provide a UseGenericZygote() method.
base::ProcessHandle handle = zygote_handle->ForkRequest(
command_line()->argv(), std::move(files_to_register), GetProcessType());
*launch_result = LAUNCH_RESULT_SUCCESS;
Process process;
process.process = base::Process(handle);
process.zygote = zygote_handle;
return process;
}
Process process;
process.process = base::LaunchProcess(*command_line(), options);
*launch_result = process.process.IsValid() ? LAUNCH_RESULT_SUCCESS
: LAUNCH_RESULT_FAILURE;
return process;
}
void ChildProcessLauncherHelper::AfterLaunchOnLauncherThread(
const ChildProcessLauncherHelper::Process& process,
const base::LaunchOptions& options) {
}
base::TerminationStatus ChildProcessLauncherHelper::GetTerminationStatus(
const ChildProcessLauncherHelper::Process& process,
bool known_dead,
int* exit_code) {
if (process.zygote) {
return process.zygote->GetTerminationStatus(
process.process.Handle(), known_dead, exit_code);
}
if (known_dead) {
return base::GetKnownDeadTerminationStatus(
process.process.Handle(), exit_code);
}
return base::GetTerminationStatus(process.process.Handle(), exit_code);
}
// static
bool ChildProcessLauncherHelper::TerminateProcess(
const base::Process& process, int exit_code, bool wait) {
return process.Terminate(exit_code, wait);
}
// static
void ChildProcessLauncherHelper::ForceNormalProcessTerminationSync(
ChildProcessLauncherHelper::Process process) {
process.process.Terminate(RESULT_CODE_NORMAL_EXIT, false);
// On POSIX, we must additionally reap the child.
if (process.zygote) {
// If the renderer was created via a zygote, we have to proxy the reaping
// through the zygote process.
process.zygote->EnsureProcessTerminated(process.process.Handle());
} else {
base::EnsureProcessTerminated(std::move(process.process));
}
}
void ChildProcessLauncherHelper::SetProcessPriorityOnLauncherThread(
base::Process process,
const ChildProcessLauncherPriority& priority) {
DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER);
if (process.CanBackgroundProcesses())
process.SetProcessBackgrounded(priority.background);
}
// static
void ChildProcessLauncherHelper::SetRegisteredFilesForService(
const std::string& service_name,
catalog::RequiredFileMap required_files) {
SetFilesToShareForServicePosix(service_name, std::move(required_files));
}
// static
void ChildProcessLauncherHelper::ResetRegisteredFilesForTesting() {
ResetFilesToShareForTestingPosix();
}
// static
base::File OpenFileToShare(const base::FilePath& path,
base::MemoryMappedFile::Region* region) {
base::FilePath exe_dir;
bool result = base::PathService::Get(base::BasePathKey::DIR_EXE, &exe_dir);
DCHECK(result);
base::File file(exe_dir.Append(path),
base::File::FLAG_OPEN | base::File::FLAG_READ);
*region = base::MemoryMappedFile::Region::kWholeFile;
return file;
}
} // namespace internal
} // namespace content