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

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

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