summaryrefslogtreecommitdiffstats
path: root/base/process_util_posix.cc
diff options
context:
space:
mode:
authorjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-29 17:51:51 +0000
committerjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-29 17:51:51 +0000
commit05d4b0ae3154d410443f5ac5d6f74c970ce8d700 (patch)
tree32662f7f9bbb25f81123fb7e34b8bd177d39f161 /base/process_util_posix.cc
parent3216fbdae4d6887bc87240879530fb9f688d013b (diff)
downloadchromium_src-05d4b0ae3154d410443f5ac5d6f74c970ce8d700.zip
chromium_src-05d4b0ae3154d410443f5ac5d6f74c970ce8d700.tar.gz
chromium_src-05d4b0ae3154d410443f5ac5d6f74c970ce8d700.tar.bz2
Add routine to close file descriptors on exec
for linux and mac. See BUG 6598. Review URL: http://codereview.chromium.org/18801 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8892 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/process_util_posix.cc')
-rw-r--r--base/process_util_posix.cc57
1 files changed, 43 insertions, 14 deletions
diff --git a/base/process_util_posix.cc b/base/process_util_posix.cc
index 87a4e36..d8fefca 100644
--- a/base/process_util_posix.cc
+++ b/base/process_util_posix.cc
@@ -2,19 +2,22 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/process_util.h"
-
+#include <dirent.h>
#include <errno.h>
+#include <fcntl.h>
#include <signal.h>
+#include <stdlib.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
+
#include <limits>
#include "base/basictypes.h"
#include "base/logging.h"
+#include "base/process_util.h"
#include "base/sys_info.h"
#include "base/time.h"
@@ -58,21 +61,47 @@ bool KillProcess(int process_id, int exit_code, bool wait) {
return result;
}
-int GetMaxFilesOpenInProcess() {
- struct rlimit rlimit;
- if (getrlimit(RLIMIT_NOFILE, &rlimit) != 0) {
- return 0;
+// A class to handle auto-closing of DIR*'s.
+class ScopedDIRClose {
+ public:
+ inline void operator()(DIR* x) const {
+ if (x) {
+ closedir(x);
+ }
}
-
- // rlim_t is a uint64 - clip to maxint.
- // We do this since we use the value of this function to close FD #s in a loop
- // if we didn't clamp the value, doing this would be too time consuming.
- rlim_t max_int = static_cast<rlim_t>(std::numeric_limits<int32>::max());
- if (rlimit.rlim_cur > max_int) {
- return max_int;
+};
+typedef scoped_ptr_malloc<DIR, ScopedDIRClose> ScopedDIR;
+
+// Sets all file descriptors to close on exec except for stdin, stdout
+// and stderr.
+void SetAllFDsToCloseOnExec() {
+#if defined(OS_LINUX)
+ const char fd_dir[] = "/proc/self/fd";
+#elif defined(OS_MACOSX)
+ const char fd_dir[] = "/dev/fd";
+#endif
+ ScopedDIR dir_closer(opendir(fd_dir));
+ DIR *dir = dir_closer.get();
+ if (NULL == dir) {
+ DLOG(ERROR) << "Unable to open " << fd_dir;
+ return;
}
- return rlimit.rlim_cur;
+ struct dirent *ent;
+ while ((ent = readdir(dir))) {
+ // Skip . and .. entries.
+ if (ent->d_name[0] == '.')
+ continue;
+ int i = atoi(ent->d_name);
+ // We don't close stdin, stdout or stderr.
+ if (i <= STDERR_FILENO)
+ continue;
+
+ int flags = fcntl(i, F_GETFD);
+ if ((flags == -1) || (fcntl(i, F_SETFD, flags | FD_CLOEXEC) == -1)) {
+ DLOG(ERROR) << "fcntl failure.";
+ }
+ }
}
ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process),