Skip to content

Commit c7b8d98

Browse files
acmelgregkh
authored andcommitted
perf script: Use readdir() instead of deprecated readdir_r()
commit a5e8e825bd1704c488bf6a46936aaf3b9f203d6a upstream. The readdir() function is thread safe as long as just one thread uses a DIR, which is the case in 'perf script', so, 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-mt3xz7n2hl49ni2vx7kuq74g@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 56e75ce commit c7b8d98

1 file changed

Lines changed: 34 additions & 36 deletions

File tree

tools/perf/builtin-script.c

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,21 +1252,19 @@ static int is_directory(const char *base_path, const struct dirent *dent)
12521252
return S_ISDIR(st.st_mode);
12531253
}
12541254

1255-
#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
1256-
while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1257-
lang_next) \
1258-
if ((lang_dirent.d_type == DT_DIR || \
1259-
(lang_dirent.d_type == DT_UNKNOWN && \
1260-
is_directory(scripts_path, &lang_dirent))) && \
1261-
(strcmp(lang_dirent.d_name, ".")) && \
1262-
(strcmp(lang_dirent.d_name, "..")))
1263-
1264-
#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
1265-
while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1266-
script_next) \
1267-
if (script_dirent.d_type != DT_DIR && \
1268-
(script_dirent.d_type != DT_UNKNOWN || \
1269-
!is_directory(lang_path, &script_dirent)))
1255+
#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1256+
while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1257+
if ((lang_dirent->d_type == DT_DIR || \
1258+
(lang_dirent->d_type == DT_UNKNOWN && \
1259+
is_directory(scripts_path, lang_dirent))) && \
1260+
(strcmp(lang_dirent->d_name, ".")) && \
1261+
(strcmp(lang_dirent->d_name, "..")))
1262+
1263+
#define for_each_script(lang_path, lang_dir, script_dirent) \
1264+
while ((script_dirent = readdir(lang_dir)) != NULL) \
1265+
if (script_dirent->d_type != DT_DIR && \
1266+
(script_dirent->d_type != DT_UNKNOWN || \
1267+
!is_directory(lang_path, script_dirent)))
12701268

12711269

12721270
#define RECORD_SUFFIX "-record"
@@ -1412,7 +1410,7 @@ static int list_available_scripts(const struct option *opt __maybe_unused,
14121410
const char *s __maybe_unused,
14131411
int unset __maybe_unused)
14141412
{
1415-
struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1413+
struct dirent *script_dirent, *lang_dirent;
14161414
char scripts_path[MAXPATHLEN];
14171415
DIR *scripts_dir, *lang_dir;
14181416
char script_path[MAXPATHLEN];
@@ -1427,19 +1425,19 @@ static int list_available_scripts(const struct option *opt __maybe_unused,
14271425
if (!scripts_dir)
14281426
return -1;
14291427

1430-
for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1428+
for_each_lang(scripts_path, scripts_dir, lang_dirent) {
14311429
snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1432-
lang_dirent.d_name);
1430+
lang_dirent->d_name);
14331431
lang_dir = opendir(lang_path);
14341432
if (!lang_dir)
14351433
continue;
14361434

1437-
for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1438-
script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1435+
for_each_script(lang_path, lang_dir, script_dirent) {
1436+
script_root = get_script_root(script_dirent, REPORT_SUFFIX);
14391437
if (script_root) {
14401438
desc = script_desc__findnew(script_root);
14411439
snprintf(script_path, MAXPATHLEN, "%s/%s",
1442-
lang_path, script_dirent.d_name);
1440+
lang_path, script_dirent->d_name);
14431441
read_script_info(desc, script_path);
14441442
free(script_root);
14451443
}
@@ -1527,7 +1525,7 @@ static int check_ev_match(char *dir_name, char *scriptname,
15271525
*/
15281526
int find_scripts(char **scripts_array, char **scripts_path_array)
15291527
{
1530-
struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1528+
struct dirent *script_dirent, *lang_dirent;
15311529
char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
15321530
DIR *scripts_dir, *lang_dir;
15331531
struct perf_session *session;
@@ -1550,9 +1548,9 @@ int find_scripts(char **scripts_array, char **scripts_path_array)
15501548
return -1;
15511549
}
15521550

1553-
for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1551+
for_each_lang(scripts_path, scripts_dir, lang_dirent) {
15541552
snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1555-
lang_dirent.d_name);
1553+
lang_dirent->d_name);
15561554
#ifdef NO_LIBPERL
15571555
if (strstr(lang_path, "perl"))
15581556
continue;
@@ -1566,16 +1564,16 @@ int find_scripts(char **scripts_array, char **scripts_path_array)
15661564
if (!lang_dir)
15671565
continue;
15681566

1569-
for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1567+
for_each_script(lang_path, lang_dir, script_dirent) {
15701568
/* Skip those real time scripts: xxxtop.p[yl] */
1571-
if (strstr(script_dirent.d_name, "top."))
1569+
if (strstr(script_dirent->d_name, "top."))
15721570
continue;
15731571
sprintf(scripts_path_array[i], "%s/%s", lang_path,
1574-
script_dirent.d_name);
1575-
temp = strchr(script_dirent.d_name, '.');
1572+
script_dirent->d_name);
1573+
temp = strchr(script_dirent->d_name, '.');
15761574
snprintf(scripts_array[i],
1577-
(temp - script_dirent.d_name) + 1,
1578-
"%s", script_dirent.d_name);
1575+
(temp - script_dirent->d_name) + 1,
1576+
"%s", script_dirent->d_name);
15791577

15801578
if (check_ev_match(lang_path,
15811579
scripts_array[i], session))
@@ -1593,7 +1591,7 @@ int find_scripts(char **scripts_array, char **scripts_path_array)
15931591

15941592
static char *get_script_path(const char *script_root, const char *suffix)
15951593
{
1596-
struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1594+
struct dirent *script_dirent, *lang_dirent;
15971595
char scripts_path[MAXPATHLEN];
15981596
char script_path[MAXPATHLEN];
15991597
DIR *scripts_dir, *lang_dir;
@@ -1606,21 +1604,21 @@ static char *get_script_path(const char *script_root, const char *suffix)
16061604
if (!scripts_dir)
16071605
return NULL;
16081606

1609-
for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1607+
for_each_lang(scripts_path, scripts_dir, lang_dirent) {
16101608
snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1611-
lang_dirent.d_name);
1609+
lang_dirent->d_name);
16121610
lang_dir = opendir(lang_path);
16131611
if (!lang_dir)
16141612
continue;
16151613

1616-
for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1617-
__script_root = get_script_root(&script_dirent, suffix);
1614+
for_each_script(lang_path, lang_dir, script_dirent) {
1615+
__script_root = get_script_root(script_dirent, suffix);
16181616
if (__script_root && !strcmp(script_root, __script_root)) {
16191617
free(__script_root);
16201618
closedir(lang_dir);
16211619
closedir(scripts_dir);
16221620
snprintf(script_path, MAXPATHLEN, "%s/%s",
1623-
lang_path, script_dirent.d_name);
1621+
lang_path, script_dirent->d_name);
16241622
return strdup(script_path);
16251623
}
16261624
free(__script_root);

0 commit comments

Comments
 (0)