xref: /linux-6.15/tools/perf/tests/tests-scripts.c (revision 9ba3462c)
1d5bcade9SIan Rogers /* SPDX-License-Identifier: GPL-2.0 */
2d5bcade9SIan Rogers #include <dirent.h>
3d5bcade9SIan Rogers #include <errno.h>
4d5bcade9SIan Rogers #include <fcntl.h>
5d5bcade9SIan Rogers #include <linux/ctype.h>
6d5bcade9SIan Rogers #include <linux/kernel.h>
7d5bcade9SIan Rogers #include <linux/string.h>
8d5bcade9SIan Rogers #include <linux/zalloc.h>
9d5bcade9SIan Rogers #include <string.h>
10d5bcade9SIan Rogers #include <stdlib.h>
11d5bcade9SIan Rogers #include <sys/types.h>
12d5bcade9SIan Rogers #include <unistd.h>
13d5bcade9SIan Rogers #include <subcmd/exec-cmd.h>
14d5bcade9SIan Rogers #include <subcmd/parse-options.h>
15d5bcade9SIan Rogers #include <sys/wait.h>
16d5bcade9SIan Rogers #include <sys/stat.h>
17f3295f5bSIan Rogers #include <api/io.h>
18d5bcade9SIan Rogers #include "builtin.h"
19d5bcade9SIan Rogers #include "tests-scripts.h"
20d5bcade9SIan Rogers #include "color.h"
21d5bcade9SIan Rogers #include "debug.h"
22d5bcade9SIan Rogers #include "hist.h"
23d5bcade9SIan Rogers #include "intlist.h"
24d5bcade9SIan Rogers #include "string2.h"
25d5bcade9SIan Rogers #include "symbol.h"
26d5bcade9SIan Rogers #include "tests.h"
27d5bcade9SIan Rogers #include "util/rlimit.h"
28f3295f5bSIan Rogers #include "util/util.h"
29d5bcade9SIan Rogers 
shell_tests__dir_fd(void)30f3295f5bSIan Rogers static int shell_tests__dir_fd(void)
31d5bcade9SIan Rogers {
32f133c764SAndi Kleen 	struct stat st;
33f133c764SAndi Kleen 	char path[PATH_MAX], path2[PATH_MAX], *exec_path;
34f133c764SAndi Kleen 	static const char * const devel_dirs[] = {
35f133c764SAndi Kleen 		"./tools/perf/tests/shell",
36f133c764SAndi Kleen 		"./tests/shell",
37f133c764SAndi Kleen 		"./source/tests/shell"
38f133c764SAndi Kleen 	};
39f133c764SAndi Kleen 	int fd;
40f133c764SAndi Kleen 	char *p;
41d5bcade9SIan Rogers 
42f3295f5bSIan Rogers 	for (size_t i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
43f133c764SAndi Kleen 		fd = open(devel_dirs[i], O_PATH);
44d5bcade9SIan Rogers 
45f3295f5bSIan Rogers 		if (fd >= 0)
46f3295f5bSIan Rogers 			return fd;
47d5bcade9SIan Rogers 	}
48d5bcade9SIan Rogers 
49f133c764SAndi Kleen 	/* Use directory of executable */
50f133c764SAndi Kleen 	if (readlink("/proc/self/exe", path2, sizeof path2) < 0)
51f133c764SAndi Kleen 		return -1;
52f133c764SAndi Kleen 	/* Follow another level of symlink if there */
53f133c764SAndi Kleen 	if (lstat(path2, &st) == 0 && (st.st_mode & S_IFMT) == S_IFLNK) {
54f133c764SAndi Kleen 		scnprintf(path, sizeof(path), path2);
55f133c764SAndi Kleen 		if (readlink(path, path2, sizeof path2) < 0)
56f133c764SAndi Kleen 			return -1;
57f133c764SAndi Kleen 	}
58f133c764SAndi Kleen 	/* Get directory */
59f133c764SAndi Kleen 	p = strrchr(path2, '/');
60f133c764SAndi Kleen 	if (p)
61f133c764SAndi Kleen 		*p = 0;
62f133c764SAndi Kleen 	scnprintf(path, sizeof(path), "%s/tests/shell", path2);
63f133c764SAndi Kleen 	fd = open(path, O_PATH);
64f133c764SAndi Kleen 	if (fd >= 0)
65f133c764SAndi Kleen 		return fd;
66f133c764SAndi Kleen 	scnprintf(path, sizeof(path), "%s/source/tests/shell", path2);
67f133c764SAndi Kleen 	fd = open(path, O_PATH);
68f133c764SAndi Kleen 	if (fd >= 0)
69f133c764SAndi Kleen 		return fd;
70f133c764SAndi Kleen 
71d5bcade9SIan Rogers 	/* Then installed path. */
72d5bcade9SIan Rogers 	exec_path = get_argv_exec_path();
73f3295f5bSIan Rogers 	scnprintf(path, sizeof(path), "%s/tests/shell", exec_path);
74d5bcade9SIan Rogers 	free(exec_path);
75f3295f5bSIan Rogers 	return open(path, O_PATH);
76d5bcade9SIan Rogers }
77d5bcade9SIan Rogers 
shell_test__description(int dir_fd,const char * name)78f3295f5bSIan Rogers static char *shell_test__description(int dir_fd, const char *name)
79d5bcade9SIan Rogers {
80f3295f5bSIan Rogers 	struct io io;
81f3295f5bSIan Rogers 	char buf[128], desc[256];
82f3295f5bSIan Rogers 	int ch, pos = 0;
83d5bcade9SIan Rogers 
84f3295f5bSIan Rogers 	io__init(&io, openat(dir_fd, name, O_RDONLY), buf, sizeof(buf));
85f3295f5bSIan Rogers 	if (io.fd < 0)
86d5bcade9SIan Rogers 		return NULL;
87d5bcade9SIan Rogers 
88d5bcade9SIan Rogers 	/* Skip first line - should be #!/bin/sh Shebang */
89f3295f5bSIan Rogers 	if (io__get_char(&io) != '#')
90f3295f5bSIan Rogers 		goto err_out;
91f3295f5bSIan Rogers 	if (io__get_char(&io) != '!')
92f3295f5bSIan Rogers 		goto err_out;
93d5bcade9SIan Rogers 	do {
94f3295f5bSIan Rogers 		ch = io__get_char(&io);
95f3295f5bSIan Rogers 		if (ch < 0)
96f3295f5bSIan Rogers 			goto err_out;
97f3295f5bSIan Rogers 	} while (ch != '\n');
98d5bcade9SIan Rogers 
99f3295f5bSIan Rogers 	do {
100f3295f5bSIan Rogers 		ch = io__get_char(&io);
101f3295f5bSIan Rogers 		if (ch < 0)
102f3295f5bSIan Rogers 			goto err_out;
103f3295f5bSIan Rogers 	} while (ch == '#' || isspace(ch));
104f3295f5bSIan Rogers 	while (ch > 0 && ch != '\n') {
105f3295f5bSIan Rogers 		desc[pos++] = ch;
106f3295f5bSIan Rogers 		if (pos >= (int)sizeof(desc) - 1)
107f3295f5bSIan Rogers 			break;
108f3295f5bSIan Rogers 		ch = io__get_char(&io);
109f3295f5bSIan Rogers 	}
110f3295f5bSIan Rogers 	while (pos > 0 && isspace(desc[--pos]))
111f3295f5bSIan Rogers 		;
112f3295f5bSIan Rogers 	desc[++pos] = '\0';
113f3295f5bSIan Rogers 	close(io.fd);
114f3295f5bSIan Rogers 	return strdup(desc);
115f3295f5bSIan Rogers err_out:
116f3295f5bSIan Rogers 	close(io.fd);
117f3295f5bSIan Rogers 	return NULL;
118d5bcade9SIan Rogers }
119d5bcade9SIan Rogers 
120d5bcade9SIan Rogers /* Is this full file path a shell script */
is_shell_script(int dir_fd,const char * path)121f3295f5bSIan Rogers static bool is_shell_script(int dir_fd, const char *path)
122d5bcade9SIan Rogers {
123d5bcade9SIan Rogers 	const char *ext;
124d5bcade9SIan Rogers 
125d5bcade9SIan Rogers 	ext = strrchr(path, '.');
126d5bcade9SIan Rogers 	if (!ext)
127d5bcade9SIan Rogers 		return false;
128d5bcade9SIan Rogers 	if (!strcmp(ext, ".sh")) { /* Has .sh extension */
129f3295f5bSIan Rogers 		if (faccessat(dir_fd, path, R_OK | X_OK, 0) == 0) /* Is executable */
130d5bcade9SIan Rogers 			return true;
131d5bcade9SIan Rogers 	}
132d5bcade9SIan Rogers 	return false;
133d5bcade9SIan Rogers }
134d5bcade9SIan Rogers 
135d5bcade9SIan Rogers /* Is this file in this dir a shell script (for test purposes) */
is_test_script(int dir_fd,const char * name)136f3295f5bSIan Rogers static bool is_test_script(int dir_fd, const char *name)
137d5bcade9SIan Rogers {
138f3295f5bSIan Rogers 	return is_shell_script(dir_fd, name);
139d5bcade9SIan Rogers }
140d5bcade9SIan Rogers 
141d5bcade9SIan Rogers /* Duplicate a string and fall over and die if we run out of memory */
strdup_check(const char * str)142d5bcade9SIan Rogers static char *strdup_check(const char *str)
143d5bcade9SIan Rogers {
144d5bcade9SIan Rogers 	char *newstr;
145d5bcade9SIan Rogers 
146d5bcade9SIan Rogers 	newstr = strdup(str);
147d5bcade9SIan Rogers 	if (!newstr) {
148d5bcade9SIan Rogers 		pr_err("Out of memory while duplicating test script string\n");
149d5bcade9SIan Rogers 		abort();
150d5bcade9SIan Rogers 	}
151d5bcade9SIan Rogers 	return newstr;
152d5bcade9SIan Rogers }
153d5bcade9SIan Rogers 
shell_test__run(struct test_suite * test,int subtest __maybe_unused)154964461eeSIan Rogers static int shell_test__run(struct test_suite *test, int subtest __maybe_unused)
155964461eeSIan Rogers {
156964461eeSIan Rogers 	const char *file = test->priv;
157964461eeSIan Rogers 	int err;
158964461eeSIan Rogers 	char *cmd = NULL;
159964461eeSIan Rogers 
160964461eeSIan Rogers 	if (asprintf(&cmd, "%s%s", file, verbose ? " -v" : "") < 0)
161964461eeSIan Rogers 		return TEST_FAIL;
162964461eeSIan Rogers 	err = system(cmd);
163964461eeSIan Rogers 	free(cmd);
164964461eeSIan Rogers 	if (!err)
165964461eeSIan Rogers 		return TEST_OK;
166964461eeSIan Rogers 
167964461eeSIan Rogers 	return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
168964461eeSIan Rogers }
169964461eeSIan Rogers 
append_script(int dir_fd,const char * name,char * desc,struct test_suite *** result,size_t * result_sz)170964461eeSIan Rogers static void append_script(int dir_fd, const char *name, char *desc,
171964461eeSIan Rogers 			  struct test_suite ***result,
172964461eeSIan Rogers 			  size_t *result_sz)
173d5bcade9SIan Rogers {
174f3295f5bSIan Rogers 	char filename[PATH_MAX], link[128];
175964461eeSIan Rogers 	struct test_suite *test_suite, **result_tmp;
176964461eeSIan Rogers 	struct test_case *tests;
177*9ba3462cSJiapeng Chong 	ssize_t len;
17879e72f38SIan Rogers 	char *exclusive;
179d5bcade9SIan Rogers 
180f3295f5bSIan Rogers 	snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd);
181f3295f5bSIan Rogers 	len = readlink(link, filename, sizeof(filename));
182f3295f5bSIan Rogers 	if (len < 0) {
183f3295f5bSIan Rogers 		pr_err("Failed to readlink %s", link);
184f3295f5bSIan Rogers 		return;
185f3295f5bSIan Rogers 	}
186f3295f5bSIan Rogers 	filename[len++] = '/';
187f3295f5bSIan Rogers 	strcpy(&filename[len], name);
188964461eeSIan Rogers 
189964461eeSIan Rogers 	tests = calloc(2, sizeof(*tests));
190964461eeSIan Rogers 	if (!tests) {
191964461eeSIan Rogers 		pr_err("Out of memory while building script test suite list\n");
192964461eeSIan Rogers 		return;
193d5bcade9SIan Rogers 	}
194964461eeSIan Rogers 	tests[0].name = strdup_check(name);
19579e72f38SIan Rogers 	exclusive = strstr(desc, " (exclusive)");
19679e72f38SIan Rogers 	if (exclusive != NULL) {
19779e72f38SIan Rogers 		tests[0].exclusive = true;
19879e72f38SIan Rogers 		exclusive[0] = '\0';
19979e72f38SIan Rogers 	}
200964461eeSIan Rogers 	tests[0].desc = strdup_check(desc);
201964461eeSIan Rogers 	tests[0].run_case = shell_test__run;
202964461eeSIan Rogers 	test_suite = zalloc(sizeof(*test_suite));
203964461eeSIan Rogers 	if (!test_suite) {
204964461eeSIan Rogers 		pr_err("Out of memory while building script test suite list\n");
205964461eeSIan Rogers 		free(tests);
206964461eeSIan Rogers 		return;
207964461eeSIan Rogers 	}
208964461eeSIan Rogers 	test_suite->desc = desc;
209964461eeSIan Rogers 	test_suite->test_cases = tests;
210964461eeSIan Rogers 	test_suite->priv = strdup_check(filename);
211d5bcade9SIan Rogers 	/* Realloc is good enough, though we could realloc by chunks, not that
212d5bcade9SIan Rogers 	 * anyone will ever measure performance here */
213964461eeSIan Rogers 	result_tmp = realloc(*result, (*result_sz + 1) * sizeof(*result_tmp));
214964461eeSIan Rogers 	if (result_tmp == NULL) {
215964461eeSIan Rogers 		pr_err("Out of memory while building script test suite list\n");
216964461eeSIan Rogers 		free(tests);
217964461eeSIan Rogers 		free(test_suite);
218964461eeSIan Rogers 		return;
219d5bcade9SIan Rogers 	}
220d5bcade9SIan Rogers 	/* Add file to end and NULL terminate the struct array */
221964461eeSIan Rogers 	*result = result_tmp;
222964461eeSIan Rogers 	(*result)[*result_sz] = test_suite;
223964461eeSIan Rogers 	(*result_sz)++;
224d5bcade9SIan Rogers }
225d5bcade9SIan Rogers 
append_scripts_in_dir(int dir_fd,struct test_suite *** result,size_t * result_sz)226964461eeSIan Rogers static void append_scripts_in_dir(int dir_fd,
227964461eeSIan Rogers 				  struct test_suite ***result,
228964461eeSIan Rogers 				  size_t *result_sz)
229d5bcade9SIan Rogers {
230d5bcade9SIan Rogers 	struct dirent **entlist;
231d5bcade9SIan Rogers 	struct dirent *ent;
232d5bcade9SIan Rogers 	int n_dirs, i;
233d5bcade9SIan Rogers 
234d5bcade9SIan Rogers 	/* List files, sorted by alpha */
235f3295f5bSIan Rogers 	n_dirs = scandirat(dir_fd, ".", &entlist, NULL, alphasort);
236d5bcade9SIan Rogers 	if (n_dirs == -1)
237d5bcade9SIan Rogers 		return;
238d5bcade9SIan Rogers 	for (i = 0; i < n_dirs && (ent = entlist[i]); i++) {
239f3295f5bSIan Rogers 		int fd;
240f3295f5bSIan Rogers 
241d5bcade9SIan Rogers 		if (ent->d_name[0] == '.')
242d5bcade9SIan Rogers 			continue; /* Skip hidden files */
243f3295f5bSIan Rogers 		if (is_test_script(dir_fd, ent->d_name)) { /* It's a test */
244f3295f5bSIan Rogers 			char *desc = shell_test__description(dir_fd, ent->d_name);
245d5bcade9SIan Rogers 
246d5bcade9SIan Rogers 			if (desc) /* It has a desc line - valid script */
247964461eeSIan Rogers 				append_script(dir_fd, ent->d_name, desc, result, result_sz);
248f3295f5bSIan Rogers 			continue;
249d5bcade9SIan Rogers 		}
250f3295f5bSIan Rogers 		if (ent->d_type != DT_DIR) {
251f3295f5bSIan Rogers 			struct stat st;
252f3295f5bSIan Rogers 
253f3295f5bSIan Rogers 			if (ent->d_type != DT_UNKNOWN)
254f3295f5bSIan Rogers 				continue;
255f3295f5bSIan Rogers 			fstatat(dir_fd, ent->d_name, &st, 0);
256f3295f5bSIan Rogers 			if (!S_ISDIR(st.st_mode))
257f3295f5bSIan Rogers 				continue;
258f3295f5bSIan Rogers 		}
2595a02447cSMichael Petlan 		if (strncmp(ent->d_name, "base_", 5) == 0)
2605a02447cSMichael Petlan 			continue; /* Skip scripts that have a separate driver. */
261f3295f5bSIan Rogers 		fd = openat(dir_fd, ent->d_name, O_PATH);
262964461eeSIan Rogers 		append_scripts_in_dir(fd, result, result_sz);
263d5bcade9SIan Rogers 	}
264d5bcade9SIan Rogers 	for (i = 0; i < n_dirs; i++) /* Clean up */
265d5bcade9SIan Rogers 		zfree(&entlist[i]);
266d5bcade9SIan Rogers 	free(entlist);
267d5bcade9SIan Rogers }
268d5bcade9SIan Rogers 
create_script_test_suites(void)269964461eeSIan Rogers struct test_suite **create_script_test_suites(void)
270d5bcade9SIan Rogers {
271964461eeSIan Rogers 	struct test_suite **result = NULL, **result_tmp;
272964461eeSIan Rogers 	size_t result_sz = 0;
273964461eeSIan Rogers 	int dir_fd = shell_tests__dir_fd(); /* Walk  dir */
274d5bcade9SIan Rogers 
275964461eeSIan Rogers 	/*
276964461eeSIan Rogers 	 * Append scripts if fd is good, otherwise return a NULL terminated zero
277964461eeSIan Rogers 	 * length array.
278964461eeSIan Rogers 	 */
279964461eeSIan Rogers 	if (dir_fd >= 0)
280964461eeSIan Rogers 		append_scripts_in_dir(dir_fd, &result, &result_sz);
281d5bcade9SIan Rogers 
282964461eeSIan Rogers 	result_tmp = realloc(result, (result_sz + 1) * sizeof(*result_tmp));
283964461eeSIan Rogers 	if (result_tmp == NULL) {
284964461eeSIan Rogers 		pr_err("Out of memory while building script test suite list\n");
285964461eeSIan Rogers 		abort();
286d5bcade9SIan Rogers 	}
287964461eeSIan Rogers 	/* NULL terminate the test suite array. */
288964461eeSIan Rogers 	result = result_tmp;
289964461eeSIan Rogers 	result[result_sz] = NULL;
290964461eeSIan Rogers 	if (dir_fd >= 0)
291964461eeSIan Rogers 		close(dir_fd);
292964461eeSIan Rogers 	return result;
293d5bcade9SIan Rogers }
294