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

Side by Side Diff: sandbox/linux/services/proc_util.cc

Issue 761903003: Update from https://crrev.com/306655 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 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 | « sandbox/linux/services/proc_util.h ('k') | sandbox/linux/services/proc_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
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "sandbox/linux/services/proc_util.h"
6
7 #include <dirent.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_number_conversions.h"
17
18 namespace sandbox {
19 namespace {
20
21 struct DIRCloser {
22 void operator()(DIR* d) const {
23 DCHECK(d);
24 PCHECK(0 == closedir(d));
25 }
26 };
27
28 typedef scoped_ptr<DIR, DIRCloser> ScopedDIR;
29
30 } // namespace
31
32 int ProcUtil::CountOpenFds(int proc_fd) {
33 DCHECK_LE(0, proc_fd);
34 int proc_self_fd = openat(proc_fd, "self/fd", O_DIRECTORY | O_RDONLY);
35 PCHECK(0 <= proc_self_fd);
36
37 // Ownership of proc_self_fd is transferred here, it must not be closed
38 // or modified afterwards except via dir.
39 ScopedDIR dir(fdopendir(proc_self_fd));
40 CHECK(dir);
41
42 int count = 0;
43 struct dirent e;
44 struct dirent* de;
45 while (!readdir_r(dir.get(), &e, &de) && de) {
46 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) {
47 continue;
48 }
49
50 int fd_num;
51 CHECK(base::StringToInt(e.d_name, &fd_num));
52 if (fd_num == proc_fd || fd_num == proc_self_fd) {
53 continue;
54 }
55
56 ++count;
57 }
58 return count;
59 }
60
61 bool ProcUtil::HasOpenDirectory(int proc_fd) {
62 int proc_self_fd = -1;
63 if (proc_fd >= 0) {
64 proc_self_fd = openat(proc_fd, "self/fd", O_DIRECTORY | O_RDONLY);
65 } else {
66 proc_self_fd = openat(AT_FDCWD, "/proc/self/fd", O_DIRECTORY | O_RDONLY);
67 if (proc_self_fd < 0) {
68 // If this process has been chrooted (eg into /proc/self/fdinfo) then
69 // the new root dir will not have directory listing permissions for us
70 // (hence EACCES). And if we do have this permission, then /proc won't
71 // exist anyway (hence ENOENT).
72 DPCHECK(errno == EACCES || errno == ENOENT)
73 << "Unexpected failure when trying to open /proc/self/fd: ("
74 << errno << ") " << strerror(errno);
75
76 // If not available, guess false.
77 return false;
78 }
79 }
80 PCHECK(0 <= proc_self_fd);
81
82 // Ownership of proc_self_fd is transferred here, it must not be closed
83 // or modified afterwards except via dir.
84 ScopedDIR dir(fdopendir(proc_self_fd));
85 CHECK(dir);
86
87 struct dirent e;
88 struct dirent* de;
89 while (!readdir_r(dir.get(), &e, &de) && de) {
90 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) {
91 continue;
92 }
93
94 int fd_num;
95 CHECK(base::StringToInt(e.d_name, &fd_num));
96 if (fd_num == proc_fd || fd_num == proc_self_fd) {
97 continue;
98 }
99
100 struct stat s;
101 // It's OK to use proc_self_fd here, fstatat won't modify it.
102 CHECK(fstatat(proc_self_fd, e.d_name, &s, 0) == 0);
103 if (S_ISDIR(s.st_mode)) {
104 return true;
105 }
106 }
107
108 // No open unmanaged directories found.
109 return false;
110 }
111
112 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/services/proc_util.h ('k') | sandbox/linux/services/proc_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698