Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: content/zygote/zygote_linux.cc

Issue 269413004: Add support for multiple zygote fork delegates (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: IWYU Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/zygote/zygote_linux.h ('k') | content/zygote/zygote_main.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/zygote/zygote_linux.h" 5 #include "content/zygote/zygote_linux.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <string.h> 8 #include <string.h>
9 #include <sys/socket.h> 9 #include <sys/socket.h>
10 #include <sys/types.h> 10 #include <sys/types.h>
11 #include <sys/wait.h> 11 #include <sys/wait.h>
12 12
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/debug/trace_event.h" 14 #include "base/debug/trace_event.h"
15 #include "base/file_util.h" 15 #include "base/file_util.h"
16 #include "base/linux_util.h" 16 #include "base/linux_util.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/memory/scoped_vector.h"
19 #include "base/pickle.h" 20 #include "base/pickle.h"
20 #include "base/posix/eintr_wrapper.h" 21 #include "base/posix/eintr_wrapper.h"
21 #include "base/posix/global_descriptors.h" 22 #include "base/posix/global_descriptors.h"
22 #include "base/posix/unix_domain_socket_linux.h" 23 #include "base/posix/unix_domain_socket_linux.h"
23 #include "base/process/kill.h" 24 #include "base/process/kill.h"
24 #include "content/common/child_process_sandbox_support_impl_linux.h" 25 #include "content/common/child_process_sandbox_support_impl_linux.h"
25 #include "content/common/sandbox_linux/sandbox_linux.h" 26 #include "content/common/sandbox_linux/sandbox_linux.h"
26 #include "content/common/set_process_title.h" 27 #include "content/common/set_process_title.h"
27 #include "content/common/zygote_commands_linux.h" 28 #include "content/common/zygote_commands_linux.h"
28 #include "content/public/common/content_descriptors.h" 29 #include "content/public/common/content_descriptors.h"
(...skipping 21 matching lines...) Expand all
50 return -1; 51 return -1;
51 } 52 }
52 53
53 void CreatePipe(base::ScopedFD* read_pipe, base::ScopedFD* write_pipe) { 54 void CreatePipe(base::ScopedFD* read_pipe, base::ScopedFD* write_pipe) {
54 int raw_pipe[2]; 55 int raw_pipe[2];
55 PCHECK(0 == pipe(raw_pipe)); 56 PCHECK(0 == pipe(raw_pipe));
56 read_pipe->reset(raw_pipe[0]); 57 read_pipe->reset(raw_pipe[0]);
57 write_pipe->reset(raw_pipe[1]); 58 write_pipe->reset(raw_pipe[1]);
58 } 59 }
59 60
60 void KillAndReap(pid_t pid, bool use_helper) { 61 void KillAndReap(pid_t pid, ZygoteForkDelegate* helper) {
61 if (use_helper) { 62 if (helper) {
62 // Helper children may be forked in another PID namespace, so |pid| might 63 // Helper children may be forked in another PID namespace, so |pid| might
63 // be meaningless to us; or we just might not be able to directly send it 64 // be meaningless to us; or we just might not be able to directly send it
64 // signals. So we can't kill it. 65 // signals. So we can't kill it.
65 // Additionally, we're not its parent, so we can't reap it anyway. 66 // Additionally, we're not its parent, so we can't reap it anyway.
66 // TODO(mdempsky): Extend the ZygoteForkDelegate API to handle this. 67 // TODO(mdempsky): Extend the ZygoteForkDelegate API to handle this.
67 LOG(WARNING) << "Unable to kill or reap helper children"; 68 LOG(WARNING) << "Unable to kill or reap helper children";
68 return; 69 return;
69 } 70 }
70 71
71 // Kill the child process in case it's not already dead, so we can safely 72 // Kill the child process in case it's not already dead, so we can safely
72 // perform a blocking wait. 73 // perform a blocking wait.
73 PCHECK(0 == kill(pid, SIGKILL)); 74 PCHECK(0 == kill(pid, SIGKILL));
74 PCHECK(pid == HANDLE_EINTR(waitpid(pid, NULL, 0))); 75 PCHECK(pid == HANDLE_EINTR(waitpid(pid, NULL, 0)));
75 } 76 }
76 77
77 } // namespace 78 } // namespace
78 79
79 Zygote::Zygote(int sandbox_flags, 80 Zygote::Zygote(int sandbox_flags, ScopedVector<ZygoteForkDelegate> helpers)
80 ZygoteForkDelegate* helper)
81 : sandbox_flags_(sandbox_flags), 81 : sandbox_flags_(sandbox_flags),
82 helper_(helper), 82 helpers_(helpers.Pass()),
83 initial_uma_sample_(0), 83 initial_uma_index_(0) {
84 initial_uma_boundary_value_(0) {
85 if (helper_) {
86 helper_->InitialUMA(&initial_uma_name_,
87 &initial_uma_sample_,
88 &initial_uma_boundary_value_);
89 }
90 } 84 }
91 85
92 Zygote::~Zygote() { 86 Zygote::~Zygote() {
93 } 87 }
94 88
95 bool Zygote::ProcessRequests() { 89 bool Zygote::ProcessRequests() {
96 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the 90 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
97 // browser on it. 91 // browser on it.
98 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel. 92 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
99 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC 93 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 ZygoteProcessInfo child_info; 252 ZygoteProcessInfo child_info;
259 if (!GetProcessInfo(real_pid, &child_info)) { 253 if (!GetProcessInfo(real_pid, &child_info)) {
260 LOG(ERROR) << "Zygote::GetTerminationStatus for unknown PID " 254 LOG(ERROR) << "Zygote::GetTerminationStatus for unknown PID "
261 << real_pid; 255 << real_pid;
262 NOTREACHED(); 256 NOTREACHED();
263 return false; 257 return false;
264 } 258 }
265 // We know about |real_pid|. 259 // We know about |real_pid|.
266 const base::ProcessHandle child = child_info.internal_pid; 260 const base::ProcessHandle child = child_info.internal_pid;
267 if (child_info.started_from_helper) { 261 if (child_info.started_from_helper) {
268 // Let the helper handle the request. 262 if (!child_info.started_from_helper->GetTerminationStatus(
269 DCHECK(helper_); 263 child, known_dead, status, exit_code)) {
270 if (!helper_->GetTerminationStatus(child, known_dead, status, exit_code)) {
271 return false; 264 return false;
272 } 265 }
273 } else { 266 } else {
274 // Handle the request directly. 267 // Handle the request directly.
275 if (known_dead) { 268 if (known_dead) {
276 *status = base::GetKnownDeadTerminationStatus(child, exit_code); 269 *status = base::GetKnownDeadTerminationStatus(child, exit_code);
277 } else { 270 } else {
278 // We don't know if the process is dying, so get its status but don't 271 // We don't know if the process is dying, so get its status but don't
279 // wait. 272 // wait.
280 *status = base::GetTerminationStatus(child, exit_code); 273 *status = base::GetTerminationStatus(child, exit_code);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 PLOG(ERROR) << "write"; 316 PLOG(ERROR) << "write";
324 } 317 }
325 318
326 int Zygote::ForkWithRealPid(const std::string& process_type, 319 int Zygote::ForkWithRealPid(const std::string& process_type,
327 const base::GlobalDescriptors::Mapping& fd_mapping, 320 const base::GlobalDescriptors::Mapping& fd_mapping,
328 const std::string& channel_id, 321 const std::string& channel_id,
329 base::ScopedFD pid_oracle, 322 base::ScopedFD pid_oracle,
330 std::string* uma_name, 323 std::string* uma_name,
331 int* uma_sample, 324 int* uma_sample,
332 int* uma_boundary_value) { 325 int* uma_boundary_value) {
333 const bool use_helper = (helper_ && helper_->CanHelp(process_type, 326 ZygoteForkDelegate* helper = NULL;
334 uma_name, 327 for (ScopedVector<ZygoteForkDelegate>::iterator i = helpers_.begin();
335 uma_sample, 328 i != helpers_.end();
336 uma_boundary_value)); 329 ++i) {
330 if ((*i)->CanHelp(process_type, uma_name, uma_sample, uma_boundary_value)) {
331 helper = *i;
332 break;
333 }
334 }
337 335
338 base::ScopedFD read_pipe, write_pipe; 336 base::ScopedFD read_pipe, write_pipe;
339 base::ProcessId pid = 0; 337 base::ProcessId pid = 0;
340 if (use_helper) { 338 if (helper) {
341 int ipc_channel_fd = LookUpFd(fd_mapping, kPrimaryIPCChannel); 339 int ipc_channel_fd = LookUpFd(fd_mapping, kPrimaryIPCChannel);
342 if (ipc_channel_fd < 0) { 340 if (ipc_channel_fd < 0) {
343 DLOG(ERROR) << "Failed to find kPrimaryIPCChannel in FD mapping"; 341 DLOG(ERROR) << "Failed to find kPrimaryIPCChannel in FD mapping";
344 return -1; 342 return -1;
345 } 343 }
346 std::vector<int> fds; 344 std::vector<int> fds;
347 fds.push_back(ipc_channel_fd); // kBrowserFDIndex 345 fds.push_back(ipc_channel_fd); // kBrowserFDIndex
348 fds.push_back(pid_oracle.get()); // kPIDOracleFDIndex 346 fds.push_back(pid_oracle.get()); // kPIDOracleFDIndex
349 pid = helper_->Fork(process_type, fds, channel_id); 347 pid = helper->Fork(process_type, fds, channel_id);
350 348
351 // Helpers should never return in the child process. 349 // Helpers should never return in the child process.
352 CHECK_NE(pid, 0); 350 CHECK_NE(pid, 0);
353 } else { 351 } else {
354 CreatePipe(&read_pipe, &write_pipe); 352 CreatePipe(&read_pipe, &write_pipe);
355 pid = fork(); 353 pid = fork();
356 } 354 }
357 355
358 if (pid == 0) { 356 if (pid == 0) {
359 // In the child process. 357 // In the child process.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 } 407 }
410 408
411 // Fork failed. 409 // Fork failed.
412 if (pid < 0) { 410 if (pid < 0) {
413 return -1; 411 return -1;
414 } 412 }
415 413
416 // If we successfully forked a child, but it crashed without sending 414 // If we successfully forked a child, but it crashed without sending
417 // a message to the browser, the browser won't have found its PID. 415 // a message to the browser, the browser won't have found its PID.
418 if (real_pid < 0) { 416 if (real_pid < 0) {
419 KillAndReap(pid, use_helper); 417 KillAndReap(pid, helper);
420 return -1; 418 return -1;
421 } 419 }
422 420
423 // If we're not using a helper, send the PID back to the child process. 421 // If we're not using a helper, send the PID back to the child process.
424 if (!use_helper) { 422 if (!helper) {
425 ssize_t written = 423 ssize_t written =
426 HANDLE_EINTR(write(write_pipe.get(), &real_pid, sizeof(real_pid))); 424 HANDLE_EINTR(write(write_pipe.get(), &real_pid, sizeof(real_pid)));
427 if (written != sizeof(real_pid)) { 425 if (written != sizeof(real_pid)) {
428 KillAndReap(pid, use_helper); 426 KillAndReap(pid, helper);
429 return -1; 427 return -1;
430 } 428 }
431 } 429 }
432 430
433 // Now set-up this process to be tracked by the Zygote. 431 // Now set-up this process to be tracked by the Zygote.
434 if (process_info_map_.find(real_pid) != process_info_map_.end()) { 432 if (process_info_map_.find(real_pid) != process_info_map_.end()) {
435 LOG(ERROR) << "Already tracking PID " << real_pid; 433 LOG(ERROR) << "Already tracking PID " << real_pid;
436 NOTREACHED(); 434 NOTREACHED();
437 } 435 }
438 process_info_map_[real_pid].internal_pid = pid; 436 process_info_map_[real_pid].internal_pid = pid;
439 process_info_map_[real_pid].started_from_helper = use_helper; 437 process_info_map_[real_pid].started_from_helper = helper;
440 438
441 return real_pid; 439 return real_pid;
442 } 440 }
443 441
444 base::ProcessId Zygote::ReadArgsAndFork(const Pickle& pickle, 442 base::ProcessId Zygote::ReadArgsAndFork(const Pickle& pickle,
445 PickleIterator iter, 443 PickleIterator iter,
446 ScopedVector<base::ScopedFD> fds, 444 ScopedVector<base::ScopedFD> fds,
447 std::string* uma_name, 445 std::string* uma_name,
448 int* uma_sample, 446 int* uma_sample,
449 int* uma_boundary_value) { 447 int* uma_boundary_value) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 const Pickle& pickle, 529 const Pickle& pickle,
532 PickleIterator iter, 530 PickleIterator iter,
533 ScopedVector<base::ScopedFD> fds) { 531 ScopedVector<base::ScopedFD> fds) {
534 std::string uma_name; 532 std::string uma_name;
535 int uma_sample; 533 int uma_sample;
536 int uma_boundary_value; 534 int uma_boundary_value;
537 base::ProcessId child_pid = ReadArgsAndFork( 535 base::ProcessId child_pid = ReadArgsAndFork(
538 pickle, iter, fds.Pass(), &uma_name, &uma_sample, &uma_boundary_value); 536 pickle, iter, fds.Pass(), &uma_name, &uma_sample, &uma_boundary_value);
539 if (child_pid == 0) 537 if (child_pid == 0)
540 return true; 538 return true;
541 if (uma_name.empty()) { 539 // If there's no UMA report for this particular fork, then check if any
542 // There is no UMA report from this particular fork. 540 // helpers have an initial UMA report for us to send instead.
543 // Use the initial UMA report if any, and clear that record for next time. 541 while (uma_name.empty() && initial_uma_index_ < helpers_.size()) {
544 // Note the swap method here is the efficient way to do this, since 542 helpers_[initial_uma_index_++]->InitialUMA(
545 // we know uma_name is empty. 543 &uma_name, &uma_sample, &uma_boundary_value);
546 uma_name.swap(initial_uma_name_);
547 uma_sample = initial_uma_sample_;
548 uma_boundary_value = initial_uma_boundary_value_;
549 } 544 }
550 // Must always send reply, as ZygoteHost blocks while waiting for it. 545 // Must always send reply, as ZygoteHost blocks while waiting for it.
551 Pickle reply_pickle; 546 Pickle reply_pickle;
552 reply_pickle.WriteInt(child_pid); 547 reply_pickle.WriteInt(child_pid);
553 reply_pickle.WriteString(uma_name); 548 reply_pickle.WriteString(uma_name);
554 if (!uma_name.empty()) { 549 if (!uma_name.empty()) {
555 reply_pickle.WriteInt(uma_sample); 550 reply_pickle.WriteInt(uma_sample);
556 reply_pickle.WriteInt(uma_boundary_value); 551 reply_pickle.WriteInt(uma_boundary_value);
557 } 552 }
558 if (HANDLE_EINTR(write(fd, reply_pickle.data(), reply_pickle.size())) != 553 if (HANDLE_EINTR(write(fd, reply_pickle.data(), reply_pickle.size())) !=
559 static_cast<ssize_t> (reply_pickle.size())) 554 static_cast<ssize_t> (reply_pickle.size()))
560 PLOG(ERROR) << "write"; 555 PLOG(ERROR) << "write";
561 return false; 556 return false;
562 } 557 }
563 558
564 bool Zygote::HandleGetSandboxStatus(int fd, 559 bool Zygote::HandleGetSandboxStatus(int fd,
565 const Pickle& pickle, 560 const Pickle& pickle,
566 PickleIterator iter) { 561 PickleIterator iter) {
567 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_))) != 562 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_))) !=
568 sizeof(sandbox_flags_)) { 563 sizeof(sandbox_flags_)) {
569 PLOG(ERROR) << "write"; 564 PLOG(ERROR) << "write";
570 } 565 }
571 566
572 return false; 567 return false;
573 } 568 }
574 569
575 } // namespace content 570 } // namespace content
OLDNEW
« no previous file with comments | « content/zygote/zygote_linux.h ('k') | content/zygote/zygote_main.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698