Commit f3295f5b authored by Ian Rogers's avatar Ian Rogers Committed by Namhyung Kim

perf tests: Use scandirat for shell script finding

Avoid filename appending buffers by using openat, faccessat and
scandirat more widely. Turn the script's path back to a file name
using readlink from /proc/<pid>/fd/<fd>.

Read the script's description using api/io.h to avoid fdopen
conversions. Whilst reading perform additional sanity checks on the
script's contents.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Cc: James Clark <james.clark@arm.com>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: llvm@lists.linux.dev
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240221034155.1500118-7-irogers@google.com
parent d5bcade9
...@@ -300,22 +300,19 @@ static int test_and_print(struct test_suite *t, int subtest) ...@@ -300,22 +300,19 @@ static int test_and_print(struct test_suite *t, int subtest)
} }
struct shell_test { struct shell_test {
const char *dir;
const char *file; const char *file;
}; };
static int shell_test__run(struct test_suite *test, int subdir __maybe_unused) static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
{ {
int err; int err;
char script[PATH_MAX];
struct shell_test *st = test->priv; struct shell_test *st = test->priv;
char *cmd = NULL;
path__join(script, sizeof(script) - 3, st->dir, st->file); if (asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : "") < 0)
return TEST_FAIL;
if (verbose > 0) err = system(cmd);
strncat(script, " -v", sizeof(script) - strlen(script) - 1); free(cmd);
err = system(script);
if (!err) if (!err)
return TEST_OK; return TEST_OK;
...@@ -331,7 +328,7 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width, ...@@ -331,7 +328,7 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width,
files = list_script_files(); files = list_script_files();
if (!files) if (!files)
return 0; return 0;
for (file = files; file->dir; file++) { for (file = files; file->file; file++) {
int curr = i++; int curr = i++;
struct test_case test_cases[] = { struct test_case test_cases[] = {
{ {
...@@ -345,13 +342,12 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width, ...@@ -345,13 +342,12 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width,
.test_cases = test_cases, .test_cases = test_cases,
.priv = &st, .priv = &st,
}; };
st.dir = file->dir; st.file = file->file;
if (test_suite.desc == NULL || if (test_suite.desc == NULL ||
!perf_test__matches(test_suite.desc, curr, argc, argv)) !perf_test__matches(test_suite.desc, curr, argc, argv))
continue; continue;
st.file = file->file;
pr_info("%3d: %-*s:", i, width, test_suite.desc); pr_info("%3d: %-*s:", i, width, test_suite.desc);
if (intlist__find(skiplist, i)) { if (intlist__find(skiplist, i)) {
...@@ -455,7 +451,7 @@ static int perf_test__list_shell(int argc, const char **argv, int i) ...@@ -455,7 +451,7 @@ static int perf_test__list_shell(int argc, const char **argv, int i)
files = list_script_files(); files = list_script_files();
if (!files) if (!files)
return 0; return 0;
for (file = files; file->dir; file++) { for (file = files; file->file; file++) {
int curr = i++; int curr = i++;
struct test_suite t = { struct test_suite t = {
.desc = file->desc .desc = file->desc
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <subcmd/parse-options.h> #include <subcmd/parse-options.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <api/io.h>
#include "builtin.h" #include "builtin.h"
#include "tests-scripts.h" #include "tests-scripts.h"
#include "color.h" #include "color.h"
...@@ -24,6 +25,7 @@ ...@@ -24,6 +25,7 @@
#include "symbol.h" #include "symbol.h"
#include "tests.h" #include "tests.h"
#include "util/rlimit.h" #include "util/rlimit.h"
#include "util/util.h"
/* /*
...@@ -35,55 +37,69 @@ static size_t files_num = 0; ...@@ -35,55 +37,69 @@ static size_t files_num = 0;
static struct script_file *files = NULL; static struct script_file *files = NULL;
static int files_max_width = 0; static int files_max_width = 0;
static const char *shell_tests__dir(char *path, size_t size) static int shell_tests__dir_fd(void)
{ {
const char *devel_dirs[] = { "./tools/perf/tests", "./tests", }; char path[PATH_MAX], *exec_path;
char *exec_path; static const char * const devel_dirs[] = { "./tools/perf/tests/shell", "./tests/shell", };
unsigned int i;
for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) { for (size_t i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
struct stat st; int fd = open(devel_dirs[i], O_PATH);
if (!lstat(devel_dirs[i], &st)) { if (fd >= 0)
scnprintf(path, size, "%s/shell", devel_dirs[i]); return fd;
if (!lstat(devel_dirs[i], &st))
return path;
}
} }
/* Then installed path. */ /* Then installed path. */
exec_path = get_argv_exec_path(); exec_path = get_argv_exec_path();
scnprintf(path, size, "%s/tests/shell", exec_path); scnprintf(path, sizeof(path), "%s/tests/shell", exec_path);
free(exec_path); free(exec_path);
return path; return open(path, O_PATH);
} }
static const char *shell_test__description(char *description, size_t size, static char *shell_test__description(int dir_fd, const char *name)
const char *path, const char *name)
{ {
FILE *fp; struct io io;
char filename[PATH_MAX]; char buf[128], desc[256];
int ch; int ch, pos = 0;
path__join(filename, sizeof(filename), path, name); io__init(&io, openat(dir_fd, name, O_RDONLY), buf, sizeof(buf));
fp = fopen(filename, "r"); if (io.fd < 0)
if (!fp)
return NULL; return NULL;
/* Skip first line - should be #!/bin/sh Shebang */ /* Skip first line - should be #!/bin/sh Shebang */
if (io__get_char(&io) != '#')
goto err_out;
if (io__get_char(&io) != '!')
goto err_out;
do { do {
ch = fgetc(fp); ch = io__get_char(&io);
} while (ch != EOF && ch != '\n'); if (ch < 0)
goto err_out;
description = fgets(description, size, fp); } while (ch != '\n');
fclose(fp);
/* Assume first char on line is omment everything after that desc */ do {
return description ? strim(description + 1) : NULL; ch = io__get_char(&io);
if (ch < 0)
goto err_out;
} while (ch == '#' || isspace(ch));
while (ch > 0 && ch != '\n') {
desc[pos++] = ch;
if (pos >= (int)sizeof(desc) - 1)
break;
ch = io__get_char(&io);
}
while (pos > 0 && isspace(desc[--pos]))
;
desc[++pos] = '\0';
close(io.fd);
return strdup(desc);
err_out:
close(io.fd);
return NULL;
} }
/* Is this full file path a shell script */ /* Is this full file path a shell script */
static bool is_shell_script(const char *path) static bool is_shell_script(int dir_fd, const char *path)
{ {
const char *ext; const char *ext;
...@@ -91,20 +107,16 @@ static bool is_shell_script(const char *path) ...@@ -91,20 +107,16 @@ static bool is_shell_script(const char *path)
if (!ext) if (!ext)
return false; return false;
if (!strcmp(ext, ".sh")) { /* Has .sh extension */ if (!strcmp(ext, ".sh")) { /* Has .sh extension */
if (access(path, R_OK | X_OK) == 0) /* Is executable */ if (faccessat(dir_fd, path, R_OK | X_OK, 0) == 0) /* Is executable */
return true; return true;
} }
return false; return false;
} }
/* Is this file in this dir a shell script (for test purposes) */ /* Is this file in this dir a shell script (for test purposes) */
static bool is_test_script(const char *path, const char *name) static bool is_test_script(int dir_fd, const char *name)
{ {
char filename[PATH_MAX]; return is_shell_script(dir_fd, name);
path__join(filename, sizeof(filename), path, name);
if (!is_shell_script(filename)) return false;
return true;
} }
/* Duplicate a string and fall over and die if we run out of memory */ /* Duplicate a string and fall over and die if we run out of memory */
...@@ -120,12 +132,21 @@ static char *strdup_check(const char *str) ...@@ -120,12 +132,21 @@ static char *strdup_check(const char *str)
return newstr; return newstr;
} }
static void append_script(const char *dir, const char *file, const char *desc) static void append_script(int dir_fd, const char *name, char *desc)
{ {
char filename[PATH_MAX], link[128];
struct script_file *files_tmp; struct script_file *files_tmp;
size_t files_num_tmp; size_t files_num_tmp, len;
int width; int width;
snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd);
len = readlink(link, filename, sizeof(filename));
if (len < 0) {
pr_err("Failed to readlink %s", link);
return;
}
filename[len++] = '/';
strcpy(&filename[len], name);
files_num_tmp = files_num + 1; files_num_tmp = files_num + 1;
if (files_num_tmp >= SIZE_MAX) { if (files_num_tmp >= SIZE_MAX) {
pr_err("Too many script files\n"); pr_err("Too many script files\n");
...@@ -142,10 +163,8 @@ static void append_script(const char *dir, const char *file, const char *desc) ...@@ -142,10 +163,8 @@ static void append_script(const char *dir, const char *file, const char *desc)
/* Add file to end and NULL terminate the struct array */ /* Add file to end and NULL terminate the struct array */
files = files_tmp; files = files_tmp;
files_num = files_num_tmp; files_num = files_num_tmp;
files[files_num - 1].dir = strdup_check(dir); files[files_num - 1].file = strdup_check(filename);
files[files_num - 1].file = strdup_check(file); files[files_num - 1].desc = desc;
files[files_num - 1].desc = strdup_check(desc);
files[files_num].dir = NULL;
files[files_num].file = NULL; files[files_num].file = NULL;
files[files_num].desc = NULL; files[files_num].desc = NULL;
...@@ -154,32 +173,39 @@ static void append_script(const char *dir, const char *file, const char *desc) ...@@ -154,32 +173,39 @@ static void append_script(const char *dir, const char *file, const char *desc)
files_max_width = width; files_max_width = width;
} }
static void append_scripts_in_dir(const char *path) static void append_scripts_in_dir(int dir_fd)
{ {
struct dirent **entlist; struct dirent **entlist;
struct dirent *ent; struct dirent *ent;
int n_dirs, i; int n_dirs, i;
char filename[PATH_MAX];
/* List files, sorted by alpha */ /* List files, sorted by alpha */
n_dirs = scandir(path, &entlist, NULL, alphasort); n_dirs = scandirat(dir_fd, ".", &entlist, NULL, alphasort);
if (n_dirs == -1) if (n_dirs == -1)
return; return;
for (i = 0; i < n_dirs && (ent = entlist[i]); i++) { for (i = 0; i < n_dirs && (ent = entlist[i]); i++) {
int fd;
if (ent->d_name[0] == '.') if (ent->d_name[0] == '.')
continue; /* Skip hidden files */ continue; /* Skip hidden files */
if (is_test_script(path, ent->d_name)) { /* It's a test */ if (is_test_script(dir_fd, ent->d_name)) { /* It's a test */
char bf[256]; char *desc = shell_test__description(dir_fd, ent->d_name);
const char *desc = shell_test__description
(bf, sizeof(bf), path, ent->d_name);
if (desc) /* It has a desc line - valid script */ if (desc) /* It has a desc line - valid script */
append_script(path, ent->d_name, desc); append_script(dir_fd, ent->d_name, desc);
} else if (is_directory(path, ent)) { /* Scan the subdir */ continue;
path__join(filename, sizeof(filename), }
path, ent->d_name); if (ent->d_type != DT_DIR) {
append_scripts_in_dir(filename); struct stat st;
if (ent->d_type != DT_UNKNOWN)
continue;
fstatat(dir_fd, ent->d_name, &st, 0);
if (!S_ISDIR(st.st_mode))
continue;
} }
fd = openat(dir_fd, ent->d_name, O_PATH);
append_scripts_in_dir(fd);
} }
for (i = 0; i < n_dirs; i++) /* Clean up */ for (i = 0; i < n_dirs; i++) /* Clean up */
zfree(&entlist[i]); zfree(&entlist[i]);
...@@ -188,14 +214,17 @@ static void append_scripts_in_dir(const char *path) ...@@ -188,14 +214,17 @@ static void append_scripts_in_dir(const char *path)
const struct script_file *list_script_files(void) const struct script_file *list_script_files(void)
{ {
char path_dir[PATH_MAX]; int dir_fd;
const char *path;
if (files) if (files)
return files; /* Singleton - we already know our list */ return files; /* Singleton - we already know our list */
path = shell_tests__dir(path_dir, sizeof(path_dir)); /* Walk dir */ dir_fd = shell_tests__dir_fd(); /* Walk dir */
append_scripts_in_dir(path); if (dir_fd < 0)
return NULL;
append_scripts_in_dir(dir_fd);
close(dir_fd);
return files; return files;
} }
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#define TESTS_SCRIPTS_H #define TESTS_SCRIPTS_H
struct script_file { struct script_file {
char *dir;
char *file; char *file;
char *desc; char *desc;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment