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

Side by Side Diff: base/process_util_linux.cc

Issue 100127: POSIX: Add code for shuffling file descriptors. (Closed)
Patch Set: ... Created 11 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
« no previous file with comments | « base/process_util.h ('k') | base/process_util_posix.cc » ('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) 2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2008 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 "base/process_util.h" 5 #include "base/process_util.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 #include <dirent.h> 8 #include <dirent.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <unistd.h> 10 #include <unistd.h>
(...skipping 13 matching lines...) Expand all
24 KEY_VALUE 24 KEY_VALUE
25 }; 25 };
26 26
27 } // namespace 27 } // namespace
28 28
29 namespace base { 29 namespace base {
30 30
31 bool LaunchApp(const std::vector<std::string>& argv, 31 bool LaunchApp(const std::vector<std::string>& argv,
32 const file_handle_mapping_vector& fds_to_remap, 32 const file_handle_mapping_vector& fds_to_remap,
33 bool wait, ProcessHandle* process_handle) { 33 bool wait, ProcessHandle* process_handle) {
34 // Make sure we don't leak any FDs to the child process by marking all FDs
35 // as close-on-exec.
36 SetAllFDsToCloseOnExec();
37
38 pid_t pid = fork(); 34 pid_t pid = fork();
39 if (pid < 0) 35 if (pid < 0)
40 return false; 36 return false;
41 37
42 if (pid == 0) { 38 if (pid == 0) {
43 for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin(); 39 InjectiveMultimap fd_shuffle;
44 it != fds_to_remap.end(); 40 for (file_handle_mapping_vector::const_iterator
45 ++it) { 41 it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) {
46 int src_fd = it->first; 42 fd_shuffle.push_back(InjectionArc(it->first, it->second, false));
47 int dest_fd = it->second;
48 if (src_fd == dest_fd) {
49 int flags = fcntl(src_fd, F_GETFD);
50 if (flags != -1) {
51 fcntl(src_fd, F_SETFD, flags & ~FD_CLOEXEC);
52 }
53 } else {
54 dup2(src_fd, dest_fd);
55 }
56 } 43 }
57 44
45 if (!ShuffleFileDescriptors(fd_shuffle))
46 exit(127);
47
48 CloseSuperfluousFds(fd_shuffle);
49
58 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]); 50 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]);
59 for (size_t i = 0; i < argv.size(); i++) 51 for (size_t i = 0; i < argv.size(); i++)
60 argv_cstr[i] = const_cast<char*>(argv[i].c_str()); 52 argv_cstr[i] = const_cast<char*>(argv[i].c_str());
61 argv_cstr[argv.size()] = NULL; 53 argv_cstr[argv.size()] = NULL;
62 execvp(argv_cstr[0], argv_cstr.get()); 54 execvp(argv_cstr[0], argv_cstr.get());
63 exit(127); 55 exit(127);
64 } else { 56 } else {
65 if (wait) 57 if (wait)
66 waitpid(pid, 0, 0); 58 waitpid(pid, 0, 0);
67 59
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); 217 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token());
226 } 218 }
227 state = KEY_NAME; 219 state = KEY_NAME;
228 break; 220 break;
229 } 221 }
230 } 222 }
231 return true; 223 return true;
232 } 224 }
233 225
234 } // namespace base 226 } // namespace base
OLDNEW
« no previous file with comments | « base/process_util.h ('k') | base/process_util_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698