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

Side by Side Diff: base/process_util_linux.cc

Issue 14497: POSIX: don't leak FDs when launching child Processes. (Closed)
Patch Set: Fix Windows Compilation Created 12 years 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_mac.mm » ('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>
10 #include <unistd.h>
9 #include <string> 11 #include <string>
10 #include <sys/types.h> 12 #include <sys/types.h>
11 #include <sys/wait.h> 13 #include <sys/wait.h>
12 14
13 #include "base/file_util.h" 15 #include "base/file_util.h"
14 #include "base/logging.h" 16 #include "base/logging.h"
15 #include "base/platform_thread.h" 17 #include "base/platform_thread.h"
16 #include "base/string_tokenizer.h" 18 #include "base/string_tokenizer.h"
17 #include "base/string_util.h" 19 #include "base/string_util.h"
18 #include "base/time.h" 20 #include "base/time.h"
19 21
20 namespace { 22 namespace {
21 23
22 enum ParsingState { 24 enum ParsingState {
23 KEY_NAME, 25 KEY_NAME,
24 KEY_VALUE 26 KEY_VALUE
25 }; 27 };
26 28
27 } // namespace 29 } // namespace
28 30
29 namespace base { 31 namespace base {
30 32
31 bool LaunchApp(const std::vector<std::string>& argv, 33 bool LaunchApp(const std::vector<std::string>& argv,
34 const file_handle_mapping_vector& fds_to_remap,
32 bool wait, ProcessHandle* process_handle) { 35 bool wait, ProcessHandle* process_handle) {
33 bool retval = true; 36 bool retval = true;
34 37
35 char* argv_copy[argv.size() + 1]; 38 char* argv_copy[argv.size() + 1];
36 for (size_t i = 0; i < argv.size(); i++) { 39 for (size_t i = 0; i < argv.size(); i++) {
37 argv_copy[i] = new char[argv[i].size() + 1]; 40 argv_copy[i] = new char[argv[i].size() + 1];
38 strcpy(argv_copy[i], argv[i].c_str()); 41 strcpy(argv_copy[i], argv[i].c_str());
39 } 42 }
40 argv_copy[argv.size()] = NULL; 43 argv_copy[argv.size()] = NULL;
41 44
45 // Make sure we don't leak any FDs to the child process by marking all FDs
46 // as close-on-exec.
47 int max_files = GetMaxFilesOpenInProcess();
48 for (int i = STDERR_FILENO + 1; i < max_files; i++) {
49 int flags = fcntl(i, F_GETFD);
50 if (flags != -1) {
51 fcntl(i, F_SETFD, flags | FD_CLOEXEC);
52 }
53 }
54
42 int pid = fork(); 55 int pid = fork();
43 if (pid == 0) { 56 if (pid == 0) {
57 for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin();
58 it != fds_to_remap.end();
59 ++it) {
60 int src_fd = it->first;
61 int dest_fd = it->second;
62 if (src_fd == dest_fd) {
63 int flags = fcntl(src_fd, F_GETFD);
64 if (flags != -1) {
65 fcntl(src_fd, F_SETFD, flags & ~FD_CLOEXEC);
66 }
67 } else {
68 dup2(src_fd, dest_fd);
69 }
70 }
71
44 execvp(argv_copy[0], argv_copy); 72 execvp(argv_copy[0], argv_copy);
45 } else if (pid < 0) { 73 } else if (pid < 0) {
46 retval = false; 74 retval = false;
47 } else { 75 } else {
48 if (wait) 76 if (wait)
49 waitpid(pid, 0, 0); 77 waitpid(pid, 0, 0);
50 78
51 if(process_handle) 79 if(process_handle)
52 *process_handle = pid; 80 *process_handle = pid;
53 } 81 }
54 82
55 for (size_t i = 0; i < argv.size(); i++) 83 for (size_t i = 0; i < argv.size(); i++)
56 delete[] argv_copy[i]; 84 delete[] argv_copy[i];
57 85
58 return retval; 86 return retval;
59 } 87 }
60 88
61 bool LaunchApp(const CommandLine& cl, 89 bool LaunchApp(const CommandLine& cl,
62 bool wait, bool start_hidden, ProcessHandle* process_handle) { 90 bool wait, bool start_hidden, ProcessHandle* process_handle) {
63 return LaunchApp(cl.argv(), wait, process_handle); 91 file_handle_mapping_vector no_files;
92 return LaunchApp(cl.argv(), no_files, wait, process_handle);
64 } 93 }
65 94
66 // Attempts to kill the process identified by the given process 95 // Attempts to kill the process identified by the given process
67 // entry structure. Ignores specified exit_code; linux can't force that. 96 // entry structure. Ignores specified exit_code; linux can't force that.
68 // Returns true if this is successful, false otherwise. 97 // Returns true if this is successful, false otherwise.
69 bool KillProcess(int process_id, int exit_code, bool wait) { 98 bool KillProcess(int process_id, int exit_code, bool wait) {
70 bool result = false; 99 bool result = false;
71 100
72 int status = kill(process_id, SIGTERM); 101 int status = kill(process_id, SIGTERM);
73 if (!status && wait) { 102 if (!status && wait) {
(...skipping 27 matching lines...) Expand all
101 130
102 if (WIFEXITED(status)) { 131 if (WIFEXITED(status)) {
103 int exitcode = WEXITSTATUS(status); 132 int exitcode = WEXITSTATUS(status);
104 return (exitcode != 0); 133 return (exitcode != 0);
105 } 134 }
106 135
107 return false; 136 return false;
108 } 137 }
109 138
110 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, 139 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name,
111 const ProcessFilter* filter) 140 const ProcessFilter* filter)
112 : 141 :
113 executable_name_(executable_name), 142 executable_name_(executable_name),
114 filter_(filter) { 143 filter_(filter) {
115 procfs_dir_ = opendir("/proc"); 144 procfs_dir_ = opendir("/proc");
116 } 145 }
117 146
118 NamedProcessIterator::~NamedProcessIterator() { 147 NamedProcessIterator::~NamedProcessIterator() {
119 if (procfs_dir_) { 148 if (procfs_dir_) {
120 closedir(procfs_dir_); 149 closedir(procfs_dir_);
121 procfs_dir_ = 0; 150 procfs_dir_ = 0;
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); 339 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token());
311 } 340 }
312 state = KEY_NAME; 341 state = KEY_NAME;
313 break; 342 break;
314 } 343 }
315 } 344 }
316 return true; 345 return true;
317 } 346 }
318 347
319 } // namespace base 348 } // namespace base
OLDNEW
« no previous file with comments | « base/process_util.h ('k') | base/process_util_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698