diff options
author | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-04-08 10:25:59 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-04-08 10:25:59 -0400 |
commit | a5e8e825bd1704c488bf6a46936aaf3b9f203d6a (patch) | |
tree | f88dbde3e079941e2f5e8819c74c945a0fde467d /tools/perf/builtin-script.c | |
parent | 99e87f7bb7268cf644add87130590966fd5d0d17 (diff) |
perf script: Use readdir() instead of deprecated readdir_r()
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>
Diffstat (limited to 'tools/perf/builtin-script.c')
-rw-r--r-- | tools/perf/builtin-script.c | 70 |
1 files changed, 34 insertions, 36 deletions
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 59009aa7e2ca..8f6ab2ac855a 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c | |||
@@ -1415,21 +1415,19 @@ static int is_directory(const char *base_path, const struct dirent *dent) | |||
1415 | return S_ISDIR(st.st_mode); | 1415 | return S_ISDIR(st.st_mode); |
1416 | } | 1416 | } |
1417 | 1417 | ||
1418 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\ | 1418 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \ |
1419 | while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ | 1419 | while ((lang_dirent = readdir(scripts_dir)) != NULL) \ |
1420 | lang_next) \ | 1420 | if ((lang_dirent->d_type == DT_DIR || \ |
1421 | if ((lang_dirent.d_type == DT_DIR || \ | 1421 | (lang_dirent->d_type == DT_UNKNOWN && \ |
1422 | (lang_dirent.d_type == DT_UNKNOWN && \ | 1422 | is_directory(scripts_path, lang_dirent))) && \ |
1423 | is_directory(scripts_path, &lang_dirent))) && \ | 1423 | (strcmp(lang_dirent->d_name, ".")) && \ |
1424 | (strcmp(lang_dirent.d_name, ".")) && \ | 1424 | (strcmp(lang_dirent->d_name, ".."))) |
1425 | (strcmp(lang_dirent.d_name, ".."))) | 1425 | |
1426 | 1426 | #define for_each_script(lang_path, lang_dir, script_dirent) \ | |
1427 | #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\ | 1427 | while ((script_dirent = readdir(lang_dir)) != NULL) \ |
1428 | while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ | 1428 | if (script_dirent->d_type != DT_DIR && \ |
1429 | script_next) \ | 1429 | (script_dirent->d_type != DT_UNKNOWN || \ |
1430 | if (script_dirent.d_type != DT_DIR && \ | 1430 | !is_directory(lang_path, script_dirent))) |
1431 | (script_dirent.d_type != DT_UNKNOWN || \ | ||
1432 | !is_directory(lang_path, &script_dirent))) | ||
1433 | 1431 | ||
1434 | 1432 | ||
1435 | #define RECORD_SUFFIX "-record" | 1433 | #define RECORD_SUFFIX "-record" |
@@ -1575,7 +1573,7 @@ static int list_available_scripts(const struct option *opt __maybe_unused, | |||
1575 | const char *s __maybe_unused, | 1573 | const char *s __maybe_unused, |
1576 | int unset __maybe_unused) | 1574 | int unset __maybe_unused) |
1577 | { | 1575 | { |
1578 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | 1576 | struct dirent *script_dirent, *lang_dirent; |
1579 | char scripts_path[MAXPATHLEN]; | 1577 | char scripts_path[MAXPATHLEN]; |
1580 | DIR *scripts_dir, *lang_dir; | 1578 | DIR *scripts_dir, *lang_dir; |
1581 | char script_path[MAXPATHLEN]; | 1579 | char script_path[MAXPATHLEN]; |
@@ -1590,19 +1588,19 @@ static int list_available_scripts(const struct option *opt __maybe_unused, | |||
1590 | if (!scripts_dir) | 1588 | if (!scripts_dir) |
1591 | return -1; | 1589 | return -1; |
1592 | 1590 | ||
1593 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { | 1591 | for_each_lang(scripts_path, scripts_dir, lang_dirent) { |
1594 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, | 1592 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
1595 | lang_dirent.d_name); | 1593 | lang_dirent->d_name); |
1596 | lang_dir = opendir(lang_path); | 1594 | lang_dir = opendir(lang_path); |
1597 | if (!lang_dir) | 1595 | if (!lang_dir) |
1598 | continue; | 1596 | continue; |
1599 | 1597 | ||
1600 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { | 1598 | for_each_script(lang_path, lang_dir, script_dirent) { |
1601 | script_root = get_script_root(&script_dirent, REPORT_SUFFIX); | 1599 | script_root = get_script_root(script_dirent, REPORT_SUFFIX); |
1602 | if (script_root) { | 1600 | if (script_root) { |
1603 | desc = script_desc__findnew(script_root); | 1601 | desc = script_desc__findnew(script_root); |
1604 | snprintf(script_path, MAXPATHLEN, "%s/%s", | 1602 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
1605 | lang_path, script_dirent.d_name); | 1603 | lang_path, script_dirent->d_name); |
1606 | read_script_info(desc, script_path); | 1604 | read_script_info(desc, script_path); |
1607 | free(script_root); | 1605 | free(script_root); |
1608 | } | 1606 | } |
@@ -1690,7 +1688,7 @@ static int check_ev_match(char *dir_name, char *scriptname, | |||
1690 | */ | 1688 | */ |
1691 | int find_scripts(char **scripts_array, char **scripts_path_array) | 1689 | int find_scripts(char **scripts_array, char **scripts_path_array) |
1692 | { | 1690 | { |
1693 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | 1691 | struct dirent *script_dirent, *lang_dirent; |
1694 | char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN]; | 1692 | char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN]; |
1695 | DIR *scripts_dir, *lang_dir; | 1693 | DIR *scripts_dir, *lang_dir; |
1696 | struct perf_session *session; | 1694 | struct perf_session *session; |
@@ -1713,9 +1711,9 @@ int find_scripts(char **scripts_array, char **scripts_path_array) | |||
1713 | return -1; | 1711 | return -1; |
1714 | } | 1712 | } |
1715 | 1713 | ||
1716 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { | 1714 | for_each_lang(scripts_path, scripts_dir, lang_dirent) { |
1717 | snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, | 1715 | snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, |
1718 | lang_dirent.d_name); | 1716 | lang_dirent->d_name); |
1719 | #ifdef NO_LIBPERL | 1717 | #ifdef NO_LIBPERL |
1720 | if (strstr(lang_path, "perl")) | 1718 | if (strstr(lang_path, "perl")) |
1721 | continue; | 1719 | continue; |
@@ -1729,16 +1727,16 @@ int find_scripts(char **scripts_array, char **scripts_path_array) | |||
1729 | if (!lang_dir) | 1727 | if (!lang_dir) |
1730 | continue; | 1728 | continue; |
1731 | 1729 | ||
1732 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { | 1730 | for_each_script(lang_path, lang_dir, script_dirent) { |
1733 | /* Skip those real time scripts: xxxtop.p[yl] */ | 1731 | /* Skip those real time scripts: xxxtop.p[yl] */ |
1734 | if (strstr(script_dirent.d_name, "top.")) | 1732 | if (strstr(script_dirent->d_name, "top.")) |
1735 | continue; | 1733 | continue; |
1736 | sprintf(scripts_path_array[i], "%s/%s", lang_path, | 1734 | sprintf(scripts_path_array[i], "%s/%s", lang_path, |
1737 | script_dirent.d_name); | 1735 | script_dirent->d_name); |
1738 | temp = strchr(script_dirent.d_name, '.'); | 1736 | temp = strchr(script_dirent->d_name, '.'); |
1739 | snprintf(scripts_array[i], | 1737 | snprintf(scripts_array[i], |
1740 | (temp - script_dirent.d_name) + 1, | 1738 | (temp - script_dirent->d_name) + 1, |
1741 | "%s", script_dirent.d_name); | 1739 | "%s", script_dirent->d_name); |
1742 | 1740 | ||
1743 | if (check_ev_match(lang_path, | 1741 | if (check_ev_match(lang_path, |
1744 | scripts_array[i], session)) | 1742 | scripts_array[i], session)) |
@@ -1756,7 +1754,7 @@ int find_scripts(char **scripts_array, char **scripts_path_array) | |||
1756 | 1754 | ||
1757 | static char *get_script_path(const char *script_root, const char *suffix) | 1755 | static char *get_script_path(const char *script_root, const char *suffix) |
1758 | { | 1756 | { |
1759 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | 1757 | struct dirent *script_dirent, *lang_dirent; |
1760 | char scripts_path[MAXPATHLEN]; | 1758 | char scripts_path[MAXPATHLEN]; |
1761 | char script_path[MAXPATHLEN]; | 1759 | char script_path[MAXPATHLEN]; |
1762 | DIR *scripts_dir, *lang_dir; | 1760 | DIR *scripts_dir, *lang_dir; |
@@ -1769,21 +1767,21 @@ static char *get_script_path(const char *script_root, const char *suffix) | |||
1769 | if (!scripts_dir) | 1767 | if (!scripts_dir) |
1770 | return NULL; | 1768 | return NULL; |
1771 | 1769 | ||
1772 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { | 1770 | for_each_lang(scripts_path, scripts_dir, lang_dirent) { |
1773 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, | 1771 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
1774 | lang_dirent.d_name); | 1772 | lang_dirent->d_name); |
1775 | lang_dir = opendir(lang_path); | 1773 | lang_dir = opendir(lang_path); |
1776 | if (!lang_dir) | 1774 | if (!lang_dir) |
1777 | continue; | 1775 | continue; |
1778 | 1776 | ||
1779 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { | 1777 | for_each_script(lang_path, lang_dir, script_dirent) { |
1780 | __script_root = get_script_root(&script_dirent, suffix); | 1778 | __script_root = get_script_root(script_dirent, suffix); |
1781 | if (__script_root && !strcmp(script_root, __script_root)) { | 1779 | if (__script_root && !strcmp(script_root, __script_root)) { |
1782 | free(__script_root); | 1780 | free(__script_root); |
1783 | closedir(lang_dir); | 1781 | closedir(lang_dir); |
1784 | closedir(scripts_dir); | 1782 | closedir(scripts_dir); |
1785 | snprintf(script_path, MAXPATHLEN, "%s/%s", | 1783 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
1786 | lang_path, script_dirent.d_name); | 1784 | lang_path, script_dirent->d_name); |
1787 | return strdup(script_path); | 1785 | return strdup(script_path); |
1788 | } | 1786 | } |
1789 | free(__script_root); | 1787 | free(__script_root); |