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

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

Issue 585203002: Turn FileDescriptorInfo a collection class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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
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 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 sizeof(sandbox_status_))) != 280 sizeof(sandbox_status_))) !=
281 sizeof(sandbox_status_)) { 281 sizeof(sandbox_status_)) {
282 return -1; 282 return -1;
283 } 283 }
284 have_read_sandbox_status_word_ = true; 284 have_read_sandbox_status_word_ = true;
285 } 285 }
286 286
287 return HANDLE_EINTR(read(control_fd_, buf, buf_len)); 287 return HANDLE_EINTR(read(control_fd_, buf, buf_len));
288 } 288 }
289 289
290 pid_t ZygoteHostImpl::ForkRequest( 290 pid_t ZygoteHostImpl::ForkRequest(const std::vector<std::string>& argv,
291 const std::vector<std::string>& argv, 291 scoped_ptr<FileDescriptorInfo> mapping,
292 const std::vector<FileDescriptorInfo>& mapping, 292 const std::string& process_type) {
293 const std::string& process_type) {
294 DCHECK(init_); 293 DCHECK(init_);
295 Pickle pickle; 294 Pickle pickle;
296 295
297 int raw_socks[2]; 296 int raw_socks[2];
298 PCHECK(0 == socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks)); 297 PCHECK(0 == socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks));
299 base::ScopedFD my_sock(raw_socks[0]); 298 base::ScopedFD my_sock(raw_socks[0]);
300 base::ScopedFD peer_sock(raw_socks[1]); 299 base::ScopedFD peer_sock(raw_socks[1]);
301 CHECK(UnixDomainSocket::EnableReceiveProcessId(my_sock.get())); 300 CHECK(UnixDomainSocket::EnableReceiveProcessId(my_sock.get()));
302 301
303 pickle.WriteInt(kZygoteCommandFork); 302 pickle.WriteInt(kZygoteCommandFork);
304 pickle.WriteString(process_type); 303 pickle.WriteString(process_type);
305 pickle.WriteInt(argv.size()); 304 pickle.WriteInt(argv.size());
306 for (std::vector<std::string>::const_iterator 305 for (std::vector<std::string>::const_iterator
307 i = argv.begin(); i != argv.end(); ++i) 306 i = argv.begin(); i != argv.end(); ++i)
308 pickle.WriteString(*i); 307 pickle.WriteString(*i);
309 308
310 // Fork requests contain one file descriptor for the PID oracle, and one 309 // Fork requests contain one file descriptor for the PID oracle, and one
311 // more for each file descriptor mapping for the child process. 310 // more for each file descriptor mapping for the child process.
312 const size_t num_fds_to_send = 1 + mapping.size(); 311 const size_t num_fds_to_send = 1 + mapping->size();
313 pickle.WriteInt(num_fds_to_send); 312 pickle.WriteInt(num_fds_to_send);
314 313
315 std::vector<int> fds; 314 std::vector<int> fds;
316 ScopedVector<base::ScopedFD> autoclose_fds;
317 315
318 // First FD to send is peer_sock. 316 // First FD to send is peer_sock.
319 fds.push_back(peer_sock.get()); 317 fds.push_back(peer_sock.get());
320 autoclose_fds.push_back(new base::ScopedFD(peer_sock.Pass()));
321
322 // The rest come from mapping. 318 // The rest come from mapping.
323 for (std::vector<FileDescriptorInfo>::const_iterator 319 for (size_t i = 0; i < mapping->size(); ++i) {
324 i = mapping.begin(); i != mapping.end(); ++i) { 320 pickle.WriteUInt32(mapping->GetIDAt(i));
325 pickle.WriteUInt32(i->id); 321 fds.push_back(mapping->GetFDAt(i));
326 fds.push_back(i->fd.fd);
327 if (i->fd.auto_close) {
328 // Auto-close means we need to close the FDs after they have been passed
329 // to the other process.
330 autoclose_fds.push_back(new base::ScopedFD(i->fd.fd));
331 }
332 } 322 }
333 323
334 // Sanity check that we've populated |fds| correctly. 324 // Sanity check that we've populated |fds| correctly.
335 DCHECK_EQ(num_fds_to_send, fds.size()); 325 DCHECK_EQ(num_fds_to_send, fds.size());
336 326
337 pid_t pid; 327 pid_t pid;
338 { 328 {
339 base::AutoLock lock(control_lock_); 329 base::AutoLock lock(control_lock_);
340 if (!SendMessage(pickle, &fds)) 330 if (!SendMessage(pickle, &fds))
341 return base::kNullProcessHandle; 331 return base::kNullProcessHandle;
342 autoclose_fds.clear(); 332 peer_sock.reset();
333 mapping.reset();
jln (very slow on Chromium) 2014/09/22 17:54:15 I find this slightly worse. Having one autoclose_f
mdempsky 2014/09/22 18:30:51 Agreed, though it's overall fewer lines of code, s
Hajime Morrita 2014/09/22 22:59:26 Let me have some addition to FileDescriptorInfo to
343 334
344 { 335 {
345 char buf[sizeof(kZygoteChildPingMessage) + 1]; 336 char buf[sizeof(kZygoteChildPingMessage) + 1];
346 ScopedVector<base::ScopedFD> recv_fds; 337 ScopedVector<base::ScopedFD> recv_fds;
347 base::ProcessId real_pid; 338 base::ProcessId real_pid;
348 339
349 ssize_t n = UnixDomainSocket::RecvMsgWithPid( 340 ssize_t n = UnixDomainSocket::RecvMsgWithPid(
350 my_sock.get(), buf, sizeof(buf), &recv_fds, &real_pid); 341 my_sock.get(), buf, sizeof(buf), &recv_fds, &real_pid);
351 if (n != sizeof(kZygoteChildPingMessage) || 342 if (n != sizeof(kZygoteChildPingMessage) ||
352 0 != memcmp(buf, 343 0 != memcmp(buf,
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 return pid_; 550 return pid_;
560 } 551 }
561 552
562 int ZygoteHostImpl::GetSandboxStatus() const { 553 int ZygoteHostImpl::GetSandboxStatus() const {
563 if (have_read_sandbox_status_word_) 554 if (have_read_sandbox_status_word_)
564 return sandbox_status_; 555 return sandbox_status_;
565 return 0; 556 return 0;
566 } 557 }
567 558
568 } // namespace content 559 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698