diff options
author | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-30 19:40:03 +0000 |
---|---|---|
committer | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-30 19:40:03 +0000 |
commit | 3f04f2b1152b3c2b1db720792ed43c7600919953 (patch) | |
tree | eb9e8606323e0cf51fdde717f2a351f3685f0c95 /base/process_util_linux.cc | |
parent | e611fd66e64564d17865ed19658dc0b56e1ee746 (diff) | |
download | chromium_src-3f04f2b1152b3c2b1db720792ed43c7600919953.zip chromium_src-3f04f2b1152b3c2b1db720792ed43c7600919953.tar.gz chromium_src-3f04f2b1152b3c2b1db720792ed43c7600919953.tar.bz2 |
POSIX: Add code for shuffling file descriptors.
When forking a child process, one often wants to move existing file
descriptors to well known locations (stdout, stderr etc). However,
this is a process bedeviled with corner cases. Consider the case where
you open two file descriptors, get assigned fds 1 and 0 for them and
wish to shuffle them so that they end up in slots 0 and 1. Our current
code fails in this case.
We also have a problem where we're currently trying to mark file
descriptors as close-on-exec rather than closing them in the child
process. This is inherently broken in a multithreaded process where
another thread can open a file descriptor and race the loop which is
trying to mark them.
Thus, on Linux we switch to close-after-fork where we known that no
other threads exist in the process. On Mac, the code is sufficiently
different that this simple fix isn't applicable and one of the Mac
folks will need to take a look.
http://codereview.chromium.org/100127
BUG=11174
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14978 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/process_util_linux.cc')
-rw-r--r-- | base/process_util_linux.cc | 26 |
1 files changed, 9 insertions, 17 deletions
diff --git a/base/process_util_linux.cc b/base/process_util_linux.cc index 686b04b..f8e8a04 100644 --- a/base/process_util_linux.cc +++ b/base/process_util_linux.cc @@ -31,30 +31,22 @@ namespace base { bool LaunchApp(const std::vector<std::string>& argv, const file_handle_mapping_vector& fds_to_remap, bool wait, ProcessHandle* process_handle) { - // Make sure we don't leak any FDs to the child process by marking all FDs - // as close-on-exec. - SetAllFDsToCloseOnExec(); - pid_t pid = fork(); if (pid < 0) return false; if (pid == 0) { - for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin(); - it != fds_to_remap.end(); - ++it) { - int src_fd = it->first; - int dest_fd = it->second; - if (src_fd == dest_fd) { - int flags = fcntl(src_fd, F_GETFD); - if (flags != -1) { - fcntl(src_fd, F_SETFD, flags & ~FD_CLOEXEC); - } - } else { - dup2(src_fd, dest_fd); - } + InjectiveMultimap fd_shuffle; + for (file_handle_mapping_vector::const_iterator + it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) { + fd_shuffle.push_back(InjectionArc(it->first, it->second, false)); } + if (!ShuffleFileDescriptors(fd_shuffle)) + exit(127); + + CloseSuperfluousFds(fd_shuffle); + scoped_array<char*> argv_cstr(new char*[argv.size() + 1]); for (size_t i = 0; i < argv.size(); i++) argv_cstr[i] = const_cast<char*>(argv[i].c_str()); |