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

Side by Side Diff: base/process_util_posix.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_mac.mm ('k') | base/process_util_unittest.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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-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 <dirent.h>
6
7 #include <errno.h> 6 #include <errno.h>
7 #include <fcntl.h>
8 #include <signal.h> 8 #include <signal.h>
9 #include <stdlib.h>
9 #include <sys/resource.h> 10 #include <sys/resource.h>
10 #include <sys/time.h> 11 #include <sys/time.h>
11 #include <sys/types.h> 12 #include <sys/types.h>
12 #include <sys/wait.h> 13 #include <sys/wait.h>
13 #include <unistd.h> 14 #include <unistd.h>
15
14 #include <limits> 16 #include <limits>
15 17
16 #include "base/basictypes.h" 18 #include "base/basictypes.h"
17 #include "base/logging.h" 19 #include "base/logging.h"
20 #include "base/process_util.h"
18 #include "base/sys_info.h" 21 #include "base/sys_info.h"
19 #include "base/time.h" 22 #include "base/time.h"
20 23
21 const int kMicrosecondsPerSecond = 1000000; 24 const int kMicrosecondsPerSecond = 1000000;
22 25
23 namespace base { 26 namespace base {
24 27
25 int GetCurrentProcId() { 28 int GetCurrentProcId() {
26 return getpid(); 29 return getpid();
27 } 30 }
(...skipping 23 matching lines...) Expand all
51 break; 54 break;
52 } 55 }
53 sleep(1); 56 sleep(1);
54 } 57 }
55 } 58 }
56 if (!result) 59 if (!result)
57 DLOG(ERROR) << "Unable to terminate process."; 60 DLOG(ERROR) << "Unable to terminate process.";
58 return result; 61 return result;
59 } 62 }
60 63
61 int GetMaxFilesOpenInProcess() { 64 // A class to handle auto-closing of DIR*'s.
62 struct rlimit rlimit; 65 class ScopedDIRClose {
63 if (getrlimit(RLIMIT_NOFILE, &rlimit) != 0) { 66 public:
64 return 0; 67 inline void operator()(DIR* x) const {
68 if (x) {
69 closedir(x);
70 }
71 }
72 };
73 typedef scoped_ptr_malloc<DIR, ScopedDIRClose> ScopedDIR;
74
75 // Sets all file descriptors to close on exec except for stdin, stdout
76 // and stderr.
77 void SetAllFDsToCloseOnExec() {
78 #if defined(OS_LINUX)
79 const char fd_dir[] = "/proc/self/fd";
80 #elif defined(OS_MACOSX)
81 const char fd_dir[] = "/dev/fd";
82 #endif
83 ScopedDIR dir_closer(opendir(fd_dir));
84 DIR *dir = dir_closer.get();
85 if (NULL == dir) {
86 DLOG(ERROR) << "Unable to open " << fd_dir;
87 return;
65 } 88 }
66 89
67 // rlim_t is a uint64 - clip to maxint. 90 struct dirent *ent;
68 // We do this since we use the value of this function to close FD #s in a loop 91 while ((ent = readdir(dir))) {
69 // if we didn't clamp the value, doing this would be too time consuming. 92 // Skip . and .. entries.
70 rlim_t max_int = static_cast<rlim_t>(std::numeric_limits<int32>::max()); 93 if (ent->d_name[0] == '.')
71 if (rlimit.rlim_cur > max_int) { 94 continue;
72 return max_int; 95 int i = atoi(ent->d_name);
96 // We don't close stdin, stdout or stderr.
97 if (i <= STDERR_FILENO)
98 continue;
99
100 int flags = fcntl(i, F_GETFD);
101 if ((flags == -1) || (fcntl(i, F_SETFD, flags | FD_CLOEXEC) == -1)) {
102 DLOG(ERROR) << "fcntl failure.";
103 }
73 } 104 }
74
75 return rlimit.rlim_cur;
76 } 105 }
77 106
78 ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process), 107 ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process),
79 last_time_(0), 108 last_time_(0),
80 last_system_time_(0) { 109 last_system_time_(0) {
81 processor_count_ = base::SysInfo::NumberOfProcessors(); 110 processor_count_ = base::SysInfo::NumberOfProcessors();
82 } 111 }
83 112
84 // static 113 // static
85 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { 114 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) / 264 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) /
236 time_delta); 265 time_delta);
237 266
238 last_system_time_ = system_time; 267 last_system_time_ = system_time;
239 last_time_ = time; 268 last_time_ = time;
240 269
241 return cpu; 270 return cpu;
242 } 271 }
243 272
244 } // namespace base 273 } // namespace base
OLDNEW
« no previous file with comments | « base/process_util_mac.mm ('k') | base/process_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698