1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/ctype.h>
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/zalloc.h>
11 #include <sys/types.h>
13 #include <subcmd/exec-cmd.h>
14 #include <subcmd/parse-options.h>
19 #include "tests-scripts.h"
27 #include "util/rlimit.h"
28 #include "util/util.h"
30 static int shell_tests__dir_fd(void)
32 char path[PATH_MAX], *exec_path;
33 static const char * const devel_dirs[] = { "./tools/perf/tests/shell", "./tests/shell", };
35 for (size_t i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
36 int fd = open(devel_dirs[i], O_PATH);
42 /* Then installed path. */
43 exec_path = get_argv_exec_path();
44 scnprintf(path, sizeof(path), "%s/tests/shell", exec_path);
46 return open(path, O_PATH);
49 static char *shell_test__description(int dir_fd, const char *name)
52 char buf[128], desc[256];
55 io__init(&io, openat(dir_fd, name, O_RDONLY), buf, sizeof(buf));
59 /* Skip first line - should be #!/bin/sh Shebang */
60 if (io__get_char(&io) != '#')
62 if (io__get_char(&io) != '!')
65 ch = io__get_char(&io);
71 ch = io__get_char(&io);
74 } while (ch == '#' || isspace(ch));
75 while (ch > 0 && ch != '\n') {
77 if (pos >= (int)sizeof(desc) - 1)
79 ch = io__get_char(&io);
81 while (pos > 0 && isspace(desc[--pos]))
91 /* Is this full file path a shell script */
92 static bool is_shell_script(int dir_fd, const char *path)
96 ext = strrchr(path, '.');
99 if (!strcmp(ext, ".sh")) { /* Has .sh extension */
100 if (faccessat(dir_fd, path, R_OK | X_OK, 0) == 0) /* Is executable */
106 /* Is this file in this dir a shell script (for test purposes) */
107 static bool is_test_script(int dir_fd, const char *name)
109 return is_shell_script(dir_fd, name);
112 /* Duplicate a string and fall over and die if we run out of memory */
113 static char *strdup_check(const char *str)
117 newstr = strdup(str);
119 pr_err("Out of memory while duplicating test script string\n");
125 static int shell_test__run(struct test_suite *test, int subtest __maybe_unused)
127 const char *file = test->priv;
131 if (asprintf(&cmd, "%s%s", file, verbose ? " -v" : "") < 0)
138 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
141 static void append_script(int dir_fd, const char *name, char *desc,
142 struct test_suite ***result,
145 char filename[PATH_MAX], link[128];
146 struct test_suite *test_suite, **result_tmp;
147 struct test_case *tests;
150 snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd);
151 len = readlink(link, filename, sizeof(filename));
153 pr_err("Failed to readlink %s", link);
156 filename[len++] = '/';
157 strcpy(&filename[len], name);
159 tests = calloc(2, sizeof(*tests));
161 pr_err("Out of memory while building script test suite list\n");
164 tests[0].name = strdup_check(name);
165 tests[0].desc = strdup_check(desc);
166 tests[0].run_case = shell_test__run;
168 test_suite = zalloc(sizeof(*test_suite));
170 pr_err("Out of memory while building script test suite list\n");
174 test_suite->desc = desc;
175 test_suite->test_cases = tests;
176 test_suite->priv = strdup_check(filename);
177 /* Realloc is good enough, though we could realloc by chunks, not that
178 * anyone will ever measure performance here */
179 result_tmp = realloc(*result, (*result_sz + 1) * sizeof(*result_tmp));
180 if (result_tmp == NULL) {
181 pr_err("Out of memory while building script test suite list\n");
186 /* Add file to end and NULL terminate the struct array */
187 *result = result_tmp;
188 (*result)[*result_sz] = test_suite;
192 static void append_scripts_in_dir(int dir_fd,
193 struct test_suite ***result,
196 struct dirent **entlist;
200 /* List files, sorted by alpha */
201 n_dirs = scandirat(dir_fd, ".", &entlist, NULL, alphasort);
204 for (i = 0; i < n_dirs && (ent = entlist[i]); i++) {
207 if (ent->d_name[0] == '.')
208 continue; /* Skip hidden files */
209 if (is_test_script(dir_fd, ent->d_name)) { /* It's a test */
210 char *desc = shell_test__description(dir_fd, ent->d_name);
212 if (desc) /* It has a desc line - valid script */
213 append_script(dir_fd, ent->d_name, desc, result, result_sz);
216 if (ent->d_type != DT_DIR) {
219 if (ent->d_type != DT_UNKNOWN)
221 fstatat(dir_fd, ent->d_name, &st, 0);
222 if (!S_ISDIR(st.st_mode))
225 fd = openat(dir_fd, ent->d_name, O_PATH);
226 append_scripts_in_dir(fd, result, result_sz);
228 for (i = 0; i < n_dirs; i++) /* Clean up */
233 struct test_suite **create_script_test_suites(void)
235 struct test_suite **result = NULL, **result_tmp;
236 size_t result_sz = 0;
237 int dir_fd = shell_tests__dir_fd(); /* Walk dir */
240 * Append scripts if fd is good, otherwise return a NULL terminated zero
244 append_scripts_in_dir(dir_fd, &result, &result_sz);
246 result_tmp = realloc(result, (result_sz + 1) * sizeof(*result_tmp));
247 if (result_tmp == NULL) {
248 pr_err("Out of memory while building script test suite list\n");
251 /* NULL terminate the test suite array. */
253 result[result_sz] = NULL;