OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "sandbox/linux/services/proc_util.h" | 5 #include "sandbox/linux/services/proc_util.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 int proc_self_fd = HANDLE_EINTR( | 44 int proc_self_fd = HANDLE_EINTR( |
45 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC)); | 45 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC)); |
46 PCHECK(0 <= proc_self_fd); | 46 PCHECK(0 <= proc_self_fd); |
47 | 47 |
48 // Ownership of proc_self_fd is transferred here, it must not be closed | 48 // Ownership of proc_self_fd is transferred here, it must not be closed |
49 // or modified afterwards except via dir. | 49 // or modified afterwards except via dir. |
50 ScopedDIR dir(fdopendir(proc_self_fd)); | 50 ScopedDIR dir(fdopendir(proc_self_fd)); |
51 CHECK(dir); | 51 CHECK(dir); |
52 | 52 |
53 int count = 0; | 53 int count = 0; |
54 struct dirent e; | |
55 struct dirent* de; | 54 struct dirent* de; |
56 while (!readdir_r(dir.get(), &e, &de) && de) { | 55 while ((de = readdir(dir.get()))) { |
57 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) { | 56 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) { |
58 continue; | 57 continue; |
59 } | 58 } |
60 | 59 |
61 int fd_num; | 60 int fd_num; |
62 CHECK(base::StringToInt(e.d_name, &fd_num)); | 61 CHECK(base::StringToInt(de->d_name, &fd_num)); |
63 if (fd_num == proc_fd || fd_num == proc_self_fd) { | 62 if (fd_num == proc_fd || fd_num == proc_self_fd) { |
64 continue; | 63 continue; |
65 } | 64 } |
66 | 65 |
67 ++count; | 66 ++count; |
68 } | 67 } |
69 return count; | 68 return count; |
70 } | 69 } |
71 | 70 |
72 bool ProcUtil::HasOpenDirectory(int proc_fd) { | 71 bool ProcUtil::HasOpenDirectory(int proc_fd) { |
73 DCHECK_LE(0, proc_fd); | 72 DCHECK_LE(0, proc_fd); |
74 int proc_self_fd = | 73 int proc_self_fd = |
75 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); | 74 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
76 | 75 |
77 PCHECK(0 <= proc_self_fd); | 76 PCHECK(0 <= proc_self_fd); |
78 | 77 |
79 // Ownership of proc_self_fd is transferred here, it must not be closed | 78 // Ownership of proc_self_fd is transferred here, it must not be closed |
80 // or modified afterwards except via dir. | 79 // or modified afterwards except via dir. |
81 ScopedDIR dir(fdopendir(proc_self_fd)); | 80 ScopedDIR dir(fdopendir(proc_self_fd)); |
82 CHECK(dir); | 81 CHECK(dir); |
83 | 82 |
84 struct dirent e; | |
85 struct dirent* de; | 83 struct dirent* de; |
86 while (!readdir_r(dir.get(), &e, &de) && de) { | 84 while ((de = readdir(dir.get()))) { |
87 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) { | 85 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) { |
88 continue; | 86 continue; |
89 } | 87 } |
90 | 88 |
91 int fd_num; | 89 int fd_num; |
92 CHECK(base::StringToInt(e.d_name, &fd_num)); | 90 CHECK(base::StringToInt(de->d_name, &fd_num)); |
93 if (fd_num == proc_fd || fd_num == proc_self_fd) { | 91 if (fd_num == proc_fd || fd_num == proc_self_fd) { |
94 continue; | 92 continue; |
95 } | 93 } |
96 | 94 |
97 struct stat s; | 95 struct stat s; |
98 // It's OK to use proc_self_fd here, fstatat won't modify it. | 96 // It's OK to use proc_self_fd here, fstatat won't modify it. |
99 CHECK(fstatat(proc_self_fd, e.d_name, &s, 0) == 0); | 97 CHECK(fstatat(proc_self_fd, de->d_name, &s, 0) == 0); |
100 if (S_ISDIR(s.st_mode)) { | 98 if (S_ISDIR(s.st_mode)) { |
101 return true; | 99 return true; |
102 } | 100 } |
103 } | 101 } |
104 | 102 |
105 // No open unmanaged directories found. | 103 // No open unmanaged directories found. |
106 return false; | 104 return false; |
107 } | 105 } |
108 | 106 |
109 bool ProcUtil::HasOpenDirectory() { | 107 bool ProcUtil::HasOpenDirectory() { |
110 base::ScopedFD proc_fd( | 108 base::ScopedFD proc_fd( |
111 HANDLE_EINTR(open("/proc/", O_DIRECTORY | O_RDONLY | O_CLOEXEC))); | 109 HANDLE_EINTR(open("/proc/", O_DIRECTORY | O_RDONLY | O_CLOEXEC))); |
112 return HasOpenDirectory(proc_fd.get()); | 110 return HasOpenDirectory(proc_fd.get()); |
113 } | 111 } |
114 | 112 |
115 // static | 113 // static |
116 base::ScopedFD ProcUtil::OpenProc() { | 114 base::ScopedFD ProcUtil::OpenProc() { |
117 return OpenDirectory("/proc/"); | 115 return OpenDirectory("/proc/"); |
118 } | 116 } |
119 | 117 |
120 } // namespace sandbox | 118 } // namespace sandbox |
OLD | NEW |