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

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

Issue 2411833004: Replace some deprecated usages of readdir_r with readdir (Closed)
Patch Set: Address review comments by Jorge Lucangeli Obes. Created 4 years, 1 month 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 | « net/disk_cache/simple/simple_index_file_posix.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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* de;
55 #if defined(OS_NACL_NONSFI)
56 // NaCl has not implemented readdir.
54 struct dirent e; 57 struct dirent e;
55 struct dirent* de;
56 while (!readdir_r(dir.get(), &e, &de) && de) { 58 while (!readdir_r(dir.get(), &e, &de) && de) {
57 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) { 59 #else
60 // In all implementations of the C library that Chromium can run with,
61 // concurrent calls to readdir that specify different directory streams are
62 // thread-safe. This is the case here, since the directory stream is scoped to
63 // the current function. See https://codereview.chromium.org/2411833004/#msg3
64 for (de = readdir(dir.get()); de; de = readdir(dir.get())) {
65 #endif
66 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) {
58 continue; 67 continue;
59 } 68 }
60 69
61 int fd_num; 70 int fd_num;
62 CHECK(base::StringToInt(e.d_name, &fd_num)); 71 CHECK(base::StringToInt(de->d_name, &fd_num));
63 if (fd_num == proc_fd || fd_num == proc_self_fd) { 72 if (fd_num == proc_fd || fd_num == proc_self_fd) {
64 continue; 73 continue;
65 } 74 }
66 75
67 ++count; 76 ++count;
68 } 77 }
69 return count; 78 return count;
70 } 79 }
71 80
72 bool ProcUtil::HasOpenDirectory(int proc_fd) { 81 bool ProcUtil::HasOpenDirectory(int proc_fd) {
73 DCHECK_LE(0, proc_fd); 82 DCHECK_LE(0, proc_fd);
74 int proc_self_fd = 83 int proc_self_fd =
75 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); 84 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
76 85
77 PCHECK(0 <= proc_self_fd); 86 PCHECK(0 <= proc_self_fd);
78 87
79 // Ownership of proc_self_fd is transferred here, it must not be closed 88 // Ownership of proc_self_fd is transferred here, it must not be closed
80 // or modified afterwards except via dir. 89 // or modified afterwards except via dir.
81 ScopedDIR dir(fdopendir(proc_self_fd)); 90 ScopedDIR dir(fdopendir(proc_self_fd));
82 CHECK(dir); 91 CHECK(dir);
83 92
93 struct dirent* de;
94 #if defined(OS_NACL_NONSFI)
95 // NaCl has not implemented readdir.
84 struct dirent e; 96 struct dirent e;
85 struct dirent* de;
86 while (!readdir_r(dir.get(), &e, &de) && de) { 97 while (!readdir_r(dir.get(), &e, &de) && de) {
87 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) { 98 #else
99 // In all implementations of the C library that Chromium can run with,
100 // concurrent calls to readdir that specify different directory streams are
101 // thread-safe. This is the case here, since the directory stream is scoped to
102 // the current function. See https://codereview.chromium.org/2411833004/#msg3
103 for (de = readdir(dir.get()); de; de = readdir(dir.get())) {
104 #endif
105 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) {
88 continue; 106 continue;
89 } 107 }
90 108
91 int fd_num; 109 int fd_num;
92 CHECK(base::StringToInt(e.d_name, &fd_num)); 110 CHECK(base::StringToInt(de->d_name, &fd_num));
93 if (fd_num == proc_fd || fd_num == proc_self_fd) { 111 if (fd_num == proc_fd || fd_num == proc_self_fd) {
94 continue; 112 continue;
95 } 113 }
96 114
97 struct stat s; 115 struct stat s;
98 // It's OK to use proc_self_fd here, fstatat won't modify it. 116 // 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); 117 CHECK(fstatat(proc_self_fd, de->d_name, &s, 0) == 0);
100 if (S_ISDIR(s.st_mode)) { 118 if (S_ISDIR(s.st_mode)) {
101 return true; 119 return true;
102 } 120 }
103 } 121 }
104 122
105 // No open unmanaged directories found. 123 // No open unmanaged directories found.
106 return false; 124 return false;
107 } 125 }
108 126
109 bool ProcUtil::HasOpenDirectory() { 127 bool ProcUtil::HasOpenDirectory() {
110 base::ScopedFD proc_fd( 128 base::ScopedFD proc_fd(
111 HANDLE_EINTR(open("/proc/", O_DIRECTORY | O_RDONLY | O_CLOEXEC))); 129 HANDLE_EINTR(open("/proc/", O_DIRECTORY | O_RDONLY | O_CLOEXEC)));
112 return HasOpenDirectory(proc_fd.get()); 130 return HasOpenDirectory(proc_fd.get());
113 } 131 }
114 132
115 // static 133 // static
116 base::ScopedFD ProcUtil::OpenProc() { 134 base::ScopedFD ProcUtil::OpenProc() {
117 return OpenDirectory("/proc/"); 135 return OpenDirectory("/proc/");
118 } 136 }
119 137
120 } // namespace sandbox 138 } // namespace sandbox
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index_file_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698