OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/zygote_host/zygote_host_impl_linux.h" | 5 #include "content/browser/zygote_host/zygote_host_impl_linux.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 | 151 |
152 base::ScopedFD dummy_fd; | 152 base::ScopedFD dummy_fd; |
153 if (using_suid_sandbox_) { | 153 if (using_suid_sandbox_) { |
154 scoped_ptr<sandbox::SetuidSandboxClient> | 154 scoped_ptr<sandbox::SetuidSandboxClient> |
155 sandbox_client(sandbox::SetuidSandboxClient::Create()); | 155 sandbox_client(sandbox::SetuidSandboxClient::Create()); |
156 sandbox_client->PrependWrapper(&cmd_line); | 156 sandbox_client->PrependWrapper(&cmd_line); |
157 sandbox_client->SetupLaunchOptions(&options, &fds_to_map, &dummy_fd); | 157 sandbox_client->SetupLaunchOptions(&options, &fds_to_map, &dummy_fd); |
158 sandbox_client->SetupLaunchEnvironment(); | 158 sandbox_client->SetupLaunchEnvironment(); |
159 } | 159 } |
160 | 160 |
161 base::ProcessHandle process = -1; | |
162 options.fds_to_remap = &fds_to_map; | 161 options.fds_to_remap = &fds_to_map; |
163 base::LaunchProcess(cmd_line.argv(), options, &process); | 162 base::Process process = base::LaunchProcess(cmd_line.argv(), options); |
164 CHECK(process != -1) << "Failed to launch zygote process"; | 163 CHECK(process.IsValid()) << "Failed to launch zygote process"; |
165 dummy_fd.reset(); | 164 dummy_fd.reset(); |
166 | 165 |
167 if (using_suid_sandbox_) { | 166 if (using_suid_sandbox_) { |
168 // The SUID sandbox will execute the zygote in a new PID namespace, and | 167 // The SUID sandbox will execute the zygote in a new PID namespace, and |
169 // the main zygote process will then fork from there. Watch now our | 168 // the main zygote process will then fork from there. Watch now our |
170 // elaborate dance to find and validate the zygote's PID. | 169 // elaborate dance to find and validate the zygote's PID. |
171 | 170 |
172 // First we receive a message from the zygote boot process. | 171 // First we receive a message from the zygote boot process. |
173 base::ProcessId boot_pid; | 172 base::ProcessId boot_pid; |
174 CHECK(ReceiveFixedMessage( | 173 CHECK(ReceiveFixedMessage( |
175 fds[0], kZygoteBootMessage, sizeof(kZygoteBootMessage), &boot_pid)); | 174 fds[0], kZygoteBootMessage, sizeof(kZygoteBootMessage), &boot_pid)); |
176 | 175 |
177 // Within the PID namespace, the zygote boot process thinks it's PID 1, | 176 // Within the PID namespace, the zygote boot process thinks it's PID 1, |
178 // but its real PID can never be 1. This gives us a reliable test that | 177 // but its real PID can never be 1. This gives us a reliable test that |
179 // the kernel is translating the sender's PID to our namespace. | 178 // the kernel is translating the sender's PID to our namespace. |
180 CHECK_GT(boot_pid, 1) | 179 CHECK_GT(boot_pid, 1) |
181 << "Received invalid process ID for zygote; kernel might be too old? " | 180 << "Received invalid process ID for zygote; kernel might be too old? " |
182 "See crbug.com/357670 or try using --" | 181 "See crbug.com/357670 or try using --" |
183 << switches::kDisableSetuidSandbox << " to workaround."; | 182 << switches::kDisableSetuidSandbox << " to workaround."; |
184 | 183 |
185 // Now receive the message that the zygote's ready to go, along with the | 184 // Now receive the message that the zygote's ready to go, along with the |
186 // main zygote process's ID. | 185 // main zygote process's ID. |
187 CHECK(ReceiveFixedMessage( | 186 CHECK(ReceiveFixedMessage( |
188 fds[0], kZygoteHelloMessage, sizeof(kZygoteHelloMessage), &pid_)); | 187 fds[0], kZygoteHelloMessage, sizeof(kZygoteHelloMessage), &pid_)); |
189 CHECK_GT(pid_, 1); | 188 CHECK_GT(pid_, 1); |
190 | 189 |
191 if (process != pid_) { | 190 if (process.pid() != pid_) { |
192 // Reap the sandbox. | 191 // Reap the sandbox. |
193 base::EnsureProcessGetsReaped(process); | 192 base::EnsureProcessGetsReaped(process.pid()); |
194 } | 193 } |
195 } else { | 194 } else { |
196 // Not using the SUID sandbox. | 195 // Not using the SUID sandbox. |
197 pid_ = process; | 196 // Note that ~base::Process() will reset the internal value, but there's no |
jln (very slow on Chromium)
2015/01/13 19:19:23
Feel free to keep, but this confused me a little.
rvargas (doing something else)
2015/01/13 19:37:04
The pattern
{
ScopedFoo foo = Bar();
foo_ =
| |
197 // real "handle" on POSIX so that is safe. | |
198 pid_ = process.pid(); | |
198 } | 199 } |
199 | 200 |
200 close(fds[1]); | 201 close(fds[1]); |
201 control_fd_ = fds[0]; | 202 control_fd_ = fds[0]; |
202 | 203 |
203 Pickle pickle; | 204 Pickle pickle; |
204 pickle.WriteInt(kZygoteCommandGetSandboxStatus); | 205 pickle.WriteInt(kZygoteCommandGetSandboxStatus); |
205 if (!SendMessage(pickle, NULL)) | 206 if (!SendMessage(pickle, NULL)) |
206 LOG(FATAL) << "Cannot communicate with zygote"; | 207 LOG(FATAL) << "Cannot communicate with zygote"; |
207 // We don't wait for the reply. We'll read it in ReadReply. | 208 // We don't wait for the reply. We'll read it in ReadReply. |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
464 // TODO(stevenjb): Investigate further and fix. | 465 // TODO(stevenjb): Investigate further and fix. |
465 if (IsHeapProfilerRunning()) | 466 if (IsHeapProfilerRunning()) |
466 return; | 467 return; |
467 #endif | 468 #endif |
468 std::vector<std::string> adj_oom_score_cmdline; | 469 std::vector<std::string> adj_oom_score_cmdline; |
469 adj_oom_score_cmdline.push_back(sandbox_binary_); | 470 adj_oom_score_cmdline.push_back(sandbox_binary_); |
470 adj_oom_score_cmdline.push_back(sandbox::kAdjustOOMScoreSwitch); | 471 adj_oom_score_cmdline.push_back(sandbox::kAdjustOOMScoreSwitch); |
471 adj_oom_score_cmdline.push_back(base::Int64ToString(pid)); | 472 adj_oom_score_cmdline.push_back(base::Int64ToString(pid)); |
472 adj_oom_score_cmdline.push_back(base::IntToString(score)); | 473 adj_oom_score_cmdline.push_back(base::IntToString(score)); |
473 | 474 |
474 base::ProcessHandle sandbox_helper_process; | 475 base::Process sandbox_helper_process; |
475 base::LaunchOptions options; | 476 base::LaunchOptions options; |
476 | 477 |
477 // sandbox_helper_process is a setuid binary. | 478 // sandbox_helper_process is a setuid binary. |
478 options.allow_new_privs = true; | 479 options.allow_new_privs = true; |
479 | 480 |
480 if (base::LaunchProcess(adj_oom_score_cmdline, options, | 481 sandbox_helper_process = |
481 &sandbox_helper_process)) { | 482 base::LaunchProcess(adj_oom_score_cmdline, options); |
482 base::EnsureProcessGetsReaped(sandbox_helper_process); | 483 if (sandbox_helper_process.IsValid()) |
483 } | 484 base::EnsureProcessGetsReaped(sandbox_helper_process.pid()); |
484 } else if (!using_suid_sandbox_) { | 485 } else if (!using_suid_sandbox_) { |
485 if (!base::AdjustOOMScore(pid, score)) | 486 if (!base::AdjustOOMScore(pid, score)) |
486 PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid; | 487 PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid; |
487 } | 488 } |
488 } | 489 } |
489 #endif | 490 #endif |
490 | 491 |
491 void ZygoteHostImpl::EnsureProcessTerminated(pid_t process) { | 492 void ZygoteHostImpl::EnsureProcessTerminated(pid_t process) { |
492 DCHECK(init_); | 493 DCHECK(init_); |
493 Pickle pickle; | 494 Pickle pickle; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
552 return pid_; | 553 return pid_; |
553 } | 554 } |
554 | 555 |
555 int ZygoteHostImpl::GetSandboxStatus() const { | 556 int ZygoteHostImpl::GetSandboxStatus() const { |
556 if (have_read_sandbox_status_word_) | 557 if (have_read_sandbox_status_word_) |
557 return sandbox_status_; | 558 return sandbox_status_; |
558 return 0; | 559 return 0; |
559 } | 560 } |
560 | 561 |
561 } // namespace content | 562 } // namespace content |
OLD | NEW |