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

Side by Side Diff: base/process_util_linux.cc

Issue 18801: Add routine to close file descriptors on exec (Closed)
Patch Set: final cleanups Created 11 years, 10 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_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> 9 #include <fcntl.h>
10 #include <unistd.h> 10 #include <unistd.h>
(...skipping 26 matching lines...) Expand all
37 37
38 char* argv_copy[argv.size() + 1]; 38 char* argv_copy[argv.size() + 1];
39 for (size_t i = 0; i < argv.size(); i++) { 39 for (size_t i = 0; i < argv.size(); i++) {
40 argv_copy[i] = new char[argv[i].size() + 1]; 40 argv_copy[i] = new char[argv[i].size() + 1];
41 strcpy(argv_copy[i], argv[i].c_str()); 41 strcpy(argv_copy[i], argv[i].c_str());
42 } 42 }
43 argv_copy[argv.size()] = NULL; 43 argv_copy[argv.size()] = NULL;
44 44
45 // Make sure we don't leak any FDs to the child process by marking all FDs 45 // Make sure we don't leak any FDs to the child process by marking all FDs
46 // as close-on-exec. 46 // as close-on-exec.
47 int max_files = GetMaxFilesOpenInProcess(); 47 SetAllFDsToCloseOnExec();
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 48
55 int pid = fork(); 49 int pid = fork();
56 if (pid == 0) { 50 if (pid == 0) {
57 for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin(); 51 for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin();
58 it != fds_to_remap.end(); 52 it != fds_to_remap.end();
59 ++it) { 53 ++it) {
60 int src_fd = it->first; 54 int src_fd = it->first;
61 int dest_fd = it->second; 55 int dest_fd = it->second;
62 if (src_fd == dest_fd) { 56 if (src_fd == dest_fd) {
63 int flags = fcntl(src_fd, F_GETFD); 57 int flags = fcntl(src_fd, F_GETFD);
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); 289 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token());
296 } 290 }
297 state = KEY_NAME; 291 state = KEY_NAME;
298 break; 292 break;
299 } 293 }
300 } 294 }
301 return true; 295 return true;
302 } 296 }
303 297
304 } // namespace base 298 } // 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