Skip to content

Commit 2b4f81f

Browse files
acmelgregkh
authored andcommitted
perf tools: Use readdir() instead of deprecated readdir_r() again
commit 22a9f41b555673e7499b97acf3ffb07bf0af31ad upstream. The readdir() function is thread safe as long as just one thread uses a DIR, which is the case when parsing tracepoint event definitions, to avoid breaking the build with glibc-2.23.90 (upcoming 2.24), use it instead of readdir_r(). See: http://man7.org/linux/man-pages/man3/readdir.3.html "However, in modern implementations (including the glibc implementation), concurrent calls to readdir() that specify different directory streams are thread-safe. In cases where multiple threads must read from the same directory stream, using readdir() with external synchronization is still preferable to the use of the deprecated readdir_r(3) function." Noticed while building on a Fedora Rawhide docker container. Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Wang Nan <wangnan0@huawei.com> Link: http://lkml.kernel.org/n/tip-wddn49r6bz6wq4ee3dxbl7lo@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c04b8bb commit 2b4f81f

1 file changed

Lines changed: 30 additions & 30 deletions

File tree

tools/perf/util/parse-events.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
138138
#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
139139
#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
140140

141-
#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
142-
while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
143-
if (sys_dirent.d_type == DT_DIR && \
144-
(strcmp(sys_dirent.d_name, ".")) && \
145-
(strcmp(sys_dirent.d_name, "..")))
141+
#define for_each_subsystem(sys_dir, sys_dirent) \
142+
while ((sys_dirent = readdir(sys_dir)) != NULL) \
143+
if (sys_dirent->d_type == DT_DIR && \
144+
(strcmp(sys_dirent->d_name, ".")) && \
145+
(strcmp(sys_dirent->d_name, "..")))
146146

147147
static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
148148
{
@@ -159,12 +159,12 @@ static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
159159
return 0;
160160
}
161161

162-
#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
163-
while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
164-
if (evt_dirent.d_type == DT_DIR && \
165-
(strcmp(evt_dirent.d_name, ".")) && \
166-
(strcmp(evt_dirent.d_name, "..")) && \
167-
(!tp_event_has_id(&sys_dirent, &evt_dirent)))
162+
#define for_each_event(sys_dirent, evt_dir, evt_dirent) \
163+
while ((evt_dirent = readdir(evt_dir)) != NULL) \
164+
if (evt_dirent->d_type == DT_DIR && \
165+
(strcmp(evt_dirent->d_name, ".")) && \
166+
(strcmp(evt_dirent->d_name, "..")) && \
167+
(!tp_event_has_id(sys_dirent, evt_dirent)))
168168

169169
#define MAX_EVENT_LENGTH 512
170170

@@ -173,7 +173,7 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
173173
{
174174
struct tracepoint_path *path = NULL;
175175
DIR *sys_dir, *evt_dir;
176-
struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
176+
struct dirent *sys_dirent, *evt_dirent;
177177
char id_buf[24];
178178
int fd;
179179
u64 id;
@@ -184,18 +184,18 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
184184
if (!sys_dir)
185185
return NULL;
186186

187-
for_each_subsystem(sys_dir, sys_dirent, sys_next) {
187+
for_each_subsystem(sys_dir, sys_dirent) {
188188

189189
snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
190-
sys_dirent.d_name);
190+
sys_dirent->d_name);
191191
evt_dir = opendir(dir_path);
192192
if (!evt_dir)
193193
continue;
194194

195-
for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
195+
for_each_event(sys_dirent, evt_dir, evt_dirent) {
196196

197197
snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
198-
evt_dirent.d_name);
198+
evt_dirent->d_name);
199199
fd = open(evt_path, O_RDONLY);
200200
if (fd < 0)
201201
continue;
@@ -220,9 +220,9 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
220220
free(path);
221221
return NULL;
222222
}
223-
strncpy(path->system, sys_dirent.d_name,
223+
strncpy(path->system, sys_dirent->d_name,
224224
MAX_EVENT_LENGTH);
225-
strncpy(path->name, evt_dirent.d_name,
225+
strncpy(path->name, evt_dirent->d_name,
226226
MAX_EVENT_LENGTH);
227227
return path;
228228
}
@@ -1629,7 +1629,7 @@ void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
16291629
bool name_only)
16301630
{
16311631
DIR *sys_dir, *evt_dir;
1632-
struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1632+
struct dirent *sys_dirent, *evt_dirent;
16331633
char evt_path[MAXPATHLEN];
16341634
char dir_path[MAXPATHLEN];
16351635
char **evt_list = NULL;
@@ -1647,20 +1647,20 @@ void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
16471647
goto out_close_sys_dir;
16481648
}
16491649

1650-
for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1650+
for_each_subsystem(sys_dir, sys_dirent) {
16511651
if (subsys_glob != NULL &&
1652-
!strglobmatch(sys_dirent.d_name, subsys_glob))
1652+
!strglobmatch(sys_dirent->d_name, subsys_glob))
16531653
continue;
16541654

16551655
snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1656-
sys_dirent.d_name);
1656+
sys_dirent->d_name);
16571657
evt_dir = opendir(dir_path);
16581658
if (!evt_dir)
16591659
continue;
16601660

1661-
for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1661+
for_each_event(sys_dirent, evt_dir, evt_dirent) {
16621662
if (event_glob != NULL &&
1663-
!strglobmatch(evt_dirent.d_name, event_glob))
1663+
!strglobmatch(evt_dirent->d_name, event_glob))
16641664
continue;
16651665

16661666
if (!evt_num_known) {
@@ -1669,7 +1669,7 @@ void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
16691669
}
16701670

16711671
snprintf(evt_path, MAXPATHLEN, "%s:%s",
1672-
sys_dirent.d_name, evt_dirent.d_name);
1672+
sys_dirent->d_name, evt_dirent->d_name);
16731673

16741674
evt_list[evt_i] = strdup(evt_path);
16751675
if (evt_list[evt_i] == NULL)
@@ -1722,25 +1722,25 @@ void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
17221722
int is_valid_tracepoint(const char *event_string)
17231723
{
17241724
DIR *sys_dir, *evt_dir;
1725-
struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1725+
struct dirent *sys_dirent, *evt_dirent;
17261726
char evt_path[MAXPATHLEN];
17271727
char dir_path[MAXPATHLEN];
17281728

17291729
sys_dir = opendir(tracing_events_path);
17301730
if (!sys_dir)
17311731
return 0;
17321732

1733-
for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1733+
for_each_subsystem(sys_dir, sys_dirent) {
17341734

17351735
snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1736-
sys_dirent.d_name);
1736+
sys_dirent->d_name);
17371737
evt_dir = opendir(dir_path);
17381738
if (!evt_dir)
17391739
continue;
17401740

1741-
for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1741+
for_each_event(sys_dirent, evt_dir, evt_dirent) {
17421742
snprintf(evt_path, MAXPATHLEN, "%s:%s",
1743-
sys_dirent.d_name, evt_dirent.d_name);
1743+
sys_dirent->d_name, evt_dirent->d_name);
17441744
if (!strcmp(evt_path, event_string)) {
17451745
closedir(evt_dir);
17461746
closedir(sys_dir);

0 commit comments

Comments
 (0)