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

Side by Side Diff: content/browser/zygote_host/zygote_host_impl_linux.cc

Issue 269543014: Use RecvMsgWithPid to find real PID for zygote children (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tweak PID discovery to handle crashing child processes 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
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/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 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 return HANDLE_EINTR(read(control_fd_, buf, buf_len)); 294 return HANDLE_EINTR(read(control_fd_, buf, buf_len));
295 } 295 }
296 296
297 pid_t ZygoteHostImpl::ForkRequest( 297 pid_t ZygoteHostImpl::ForkRequest(
298 const std::vector<std::string>& argv, 298 const std::vector<std::string>& argv,
299 const std::vector<FileDescriptorInfo>& mapping, 299 const std::vector<FileDescriptorInfo>& mapping,
300 const std::string& process_type) { 300 const std::string& process_type) {
301 DCHECK(init_); 301 DCHECK(init_);
302 Pickle pickle; 302 Pickle pickle;
303 303
304 int raw_socks[2];
305 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) != 0) {
jln (very slow on Chromium) 2014/05/02 18:25:00 I think it's fine to PCHECK here, this looks like
mdempsky 2014/05/02 23:36:01 Done.
306 LOG(ERROR) << "Failed to create socketpair";
307 return base::kNullProcessHandle;
308 }
309 base::ScopedFD my_sock(raw_socks[0]);
310 base::ScopedFD peer_sock(raw_socks[1]);
311 CHECK(UnixDomainSocket::EnableReceiveProcessId(my_sock.get()));
312
304 pickle.WriteInt(kZygoteCommandFork); 313 pickle.WriteInt(kZygoteCommandFork);
305 pickle.WriteString(process_type); 314 pickle.WriteString(process_type);
306 pickle.WriteInt(argv.size()); 315 pickle.WriteInt(argv.size());
307 for (std::vector<std::string>::const_iterator 316 for (std::vector<std::string>::const_iterator
308 i = argv.begin(); i != argv.end(); ++i) 317 i = argv.begin(); i != argv.end(); ++i)
309 pickle.WriteString(*i); 318 pickle.WriteString(*i);
310 319
311 pickle.WriteInt(mapping.size()); 320 pickle.WriteInt(mapping.size());
312 321
313 std::vector<int> fds; 322 std::vector<int> fds;
314 // Scoped pointers cannot be stored in containers, so we have to use a 323 ScopedVector<base::ScopedFD> autoclose_fds;
315 // linked_ptr. 324
316 std::vector<linked_ptr<base::ScopedFD> > autodelete_fds; 325 // First FD to send is peer_sock.
326 fds.push_back(peer_sock.get());
327 autoclose_fds.push_back(new base::ScopedFD(peer_sock.Pass()));
328
317 for (std::vector<FileDescriptorInfo>::const_iterator 329 for (std::vector<FileDescriptorInfo>::const_iterator
318 i = mapping.begin(); i != mapping.end(); ++i) { 330 i = mapping.begin(); i != mapping.end(); ++i) {
319 pickle.WriteUInt32(i->id); 331 pickle.WriteUInt32(i->id);
320 fds.push_back(i->fd.fd); 332 fds.push_back(i->fd.fd);
321 if (i->fd.auto_close) { 333 if (i->fd.auto_close) {
322 // Auto-close means we need to close the FDs after they have been passed 334 // Auto-close means we need to close the FDs after they have been passed
323 // to the other process. 335 // to the other process.
324 linked_ptr<base::ScopedFD> ptr(new base::ScopedFD(fds.back())); 336 autoclose_fds.push_back(new base::ScopedFD(i->fd.fd));
325 autodelete_fds.push_back(ptr);
326 } 337 }
327 } 338 }
328 339
329 pid_t pid; 340 pid_t pid;
330 { 341 {
331 base::AutoLock lock(control_lock_); 342 base::AutoLock lock(control_lock_);
332 if (!SendMessage(pickle, &fds)) 343 if (!SendMessage(pickle, &fds))
333 return base::kNullProcessHandle; 344 return base::kNullProcessHandle;
345 autoclose_fds.clear();
jln (very slow on Chromium) 2014/05/02 18:25:00 Hmmm, I sense a new SendMessage that takes ownersh
mdempsky 2014/05/02 23:36:01 Deferred. This is a bit tricky because we're only
346
347 static const unsigned kMaxReplyLength = 2048;
348 char buf[kMaxReplyLength];
349
350 {
351 ScopedVector<base::ScopedFD> recv_fds;
352 base::ProcessId real_pid;
353
354 if (UnixDomainSocket::RecvMsgWithPid(
355 my_sock.get(), buf, sizeof(buf), &recv_fds, &real_pid) <= 0) {
356 LOG(ERROR) << "Failed to receive PID for zygote child";
357 real_pid = -1;
358 }
359
360 // Always write back PID to zygote.
361 const ssize_t n =
362 HANDLE_EINTR(write(control_fd_, &real_pid, sizeof(real_pid)));
363 CHECK_EQ(sizeof(real_pid), static_cast<size_t>(n));
364 }
334 365
335 // Read the reply, which pickles the PID and an optional UMA enumeration. 366 // Read the reply, which pickles the PID and an optional UMA enumeration.
336 static const unsigned kMaxReplyLength = 2048;
337 char buf[kMaxReplyLength];
338 const ssize_t len = ReadReply(buf, sizeof(buf)); 367 const ssize_t len = ReadReply(buf, sizeof(buf));
339 368
340 Pickle reply_pickle(buf, len); 369 Pickle reply_pickle(buf, len);
341 PickleIterator iter(reply_pickle); 370 PickleIterator iter(reply_pickle);
342 if (len <= 0 || !reply_pickle.ReadInt(&iter, &pid)) 371 if (len <= 0 || !reply_pickle.ReadInt(&iter, &pid))
343 return base::kNullProcessHandle; 372 return base::kNullProcessHandle;
344 373
345 // If there is a nonempty UMA name string, then there is a UMA 374 // If there is a nonempty UMA name string, then there is a UMA
346 // enumeration to record. 375 // enumeration to record.
347 std::string uma_name; 376 std::string uma_name;
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 return RenderSandboxHostLinux::GetInstance()->pid(); 556 return RenderSandboxHostLinux::GetInstance()->pid();
528 } 557 }
529 558
530 int ZygoteHostImpl::GetSandboxStatus() const { 559 int ZygoteHostImpl::GetSandboxStatus() const {
531 if (have_read_sandbox_status_word_) 560 if (have_read_sandbox_status_word_)
532 return sandbox_status_; 561 return sandbox_status_;
533 return 0; 562 return 0;
534 } 563 }
535 564
536 } // namespace content 565 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698