1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/* SPDX-License-Identifier: GPL-2.0 */
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <linux/ctype.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/zalloc.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <subcmd/exec-cmd.h>
#include <subcmd/parse-options.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <api/io.h>
#include "builtin.h"
#include "tests-scripts.h"
#include "color.h"
#include "debug.h"
#include "hist.h"
#include "intlist.h"
#include "string2.h"
#include "symbol.h"
#include "tests.h"
#include "util/rlimit.h"
#include "util/util.h"
static int shell_tests__dir_fd(void)
{
struct stat st;
char path[PATH_MAX], path2[PATH_MAX], *exec_path;
ssize_t len;
static const char * const devel_dirs[] = {
"./tools/perf/tests/shell",
"./tests/shell",
"./source/tests/shell"
};
int fd;
char *p;
for (size_t i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
fd = open(devel_dirs[i], O_PATH);
if (fd >= 0)
return fd;
}
/* Use directory of executable */
len = readlink("/proc/self/exe", path2, sizeof(path2) - 1);
if (len < 0)
return -1;
path2[len] = '\0';
/* Follow another level of symlink if there */
if (lstat(path2, &st) == 0 && (st.st_mode & S_IFMT) == S_IFLNK) {
scnprintf(path, sizeof(path), "%s", path2);
len = readlink(path, path2, sizeof(path2) - 1);
if (len < 0)
return -1;
path2[len] = '\0';
}
/* Get directory */
p = strrchr(path2, '/');
if (p)
*p = 0;
scnprintf(path, sizeof(path), "%s/tests/shell", path2);
fd = open(path, O_PATH);
if (fd >= 0)
return fd;
scnprintf(path, sizeof(path), "%s/source/tests/shell", path2);
fd = open(path, O_PATH);
if (fd >= 0)
return fd;
/* Then installed path. */
exec_path = get_argv_exec_path();
scnprintf(path, sizeof(path), "%s/tests/shell", exec_path);
free(exec_path);
return open(path, O_PATH);
}
static char *shell_test__description(int dir_fd, const char *name)
{
struct io io;
char buf[128], *line = NULL;
size_t line_len = 0;
ssize_t len;
char *desc = NULL;
const char *spdx = "SPDX-License";
io__init(&io, openat(dir_fd, name, O_RDONLY), buf, sizeof(buf));
if (io.fd < 0)
return NULL;
while ((len = io__getline(&io, &line, &line_len)) > 0) {
char *p = line;
/* Skip leading whitespace */
while (*p && isspace(*p))
p++;
/* Must be a comment */
if (*p != '#')
continue;
p++;
/* Skip shebang or SPDX lines */
if (*p == '!' || (strstr(p, spdx) && strstr(p, "-Identifier:")))
continue;
/* Skip whitespace after # */
while (*p && isspace(*p))
p++;
/* If we found non-empty text, this is the description! */
if (*p && *p != '\n') {
char *end = p + strlen(p);
while (end > p && isspace(end[-1]))
end--;
*end = '\0';
desc = strdup(p);
break;
}
}
free(line);
close(io.fd);
return desc;
}
/* Is this full file path a shell script */
static bool is_shell_script(int dir_fd, const char *path)
{
const char *ext;
ext = strrchr(path, '.');
if (!ext)
return false;
if (!strcmp(ext, ".sh")) { /* Has .sh extension */
if (faccessat(dir_fd, path, R_OK | X_OK, 0) == 0) /* Is executable */
return true;
}
return false;
}
/* Is this file in this dir a shell script (for test purposes) */
static bool is_test_script(int dir_fd, const char *name)
{
return is_shell_script(dir_fd, name);
}
/* Duplicate a string and fall over and die if we run out of memory */
static char *strdup_check(const char *str)
{
char *newstr;
newstr = strdup(str);
if (!newstr) {
pr_err("Out of memory while duplicating test script string\n");
abort();
}
return newstr;
}
static int shell_test__run(struct test_suite *test, int subtest __maybe_unused)
{
const char *file = test->priv;
int err;
char *cmd = NULL;
if (asprintf(&cmd, "%s%s", file, verbose ? " -v" : "") < 0)
return TEST_FAIL;
err = system(cmd);
free(cmd);
if (!err)
return TEST_OK;
return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
}
/*
* The requisites are a comma separated list in the parens ending the
* description line, e.g. "perf stat tests (no_mlock)": "exclusive" is the
* catch-all, implying every other requisite, "no_mlock" waives the default of
* needing room in the per user budget set in kernel.perf_event_mlock_kb.
*/
static void shell_test__requisites(char *desc, bool *exclusive, bool *needs_mlock)
{
char *lparen, *rparen, *requisites, *token, *tokp;
bool known = false;
/* Only a list closing the description line is taken for requisites. */
rparen = strrchr(desc, ')');
if (rparen == NULL || rparen[1] != '\0')
return;
lparen = strrchr(desc, '(');
if (lparen == NULL || lparen > rparen)
return;
/* Tokenize a copy: a list with nothing recognized leaves desc alone. */
requisites = strndup(lparen + 1, rparen - lparen - 1);
if (requisites == NULL) {
pr_err("Failed to copy the requisites of '%s'\n", desc);
return;
}
for (token = strtok_r(requisites, ",", &tokp); token;
token = strtok_r(NULL, ",", &tokp)) {
char *end = token + strlen(token);
const char *requisite = token;
bool waived;
while (isspace(*token))
token++;
while (end > token && isspace(end[-1]))
end--;
*end = '\0';
/* A "no_" prefix waives the requisite named after it. */
waived = strncmp(token, "no_", sizeof("no_") - 1) == 0;
if (waived)
token += sizeof("no_") - 1;
if (!strcmp(token, "mlock")) {
*needs_mlock = !waived;
} else if (!strcmp(token, "exclusive") && !waived) {
*exclusive = true;
} else {
pr_debug("Ignoring %s requisite '%s' in '%s'\n",
strcmp(token, "exclusive") == 0 ? "a waived" : "an unknown",
requisite, desc);
continue;
}
known = true;
}
zfree(&requisites);
if (!known)
return;
/* Drop the list and the space before it from the description. */
*lparen = '\0';
if (lparen > desc && isspace(lparen[-1]))
lparen[-1] = '\0';
/* The catch-all implies every other requisite, whatever the order. */
if (*exclusive)
*needs_mlock = true;
}
static void append_script(int dir_fd, const char *name, char *desc,
struct test_suite ***result,
size_t *result_sz)
{
char filename[PATH_MAX], link[128];
struct test_suite *test_suite, **result_tmp;
struct test_case *tests;
ssize_t len;
/* Shell tests run perf, that maps ring buffers, so they need the budget. */
bool exclusive = false, needs_mlock = true;
snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd);
len = readlink(link, filename, sizeof(filename) - 1);
if (len < 0 || (size_t)len > sizeof(filename) - strlen(name) - 2) {
pr_err("Failed to readlink %s or path too long", link);
return;
}
filename[len++] = '/';
strcpy(&filename[len], name);
tests = calloc(2, sizeof(*tests));
if (!tests) {
pr_err("Out of memory while building script test suite list\n");
return;
}
tests[0].name = strdup_check(name);
shell_test__requisites(desc, &exclusive, &needs_mlock);
tests[0].exclusive = exclusive;
tests[0].desc = strdup_check(desc);
tests[0].run_case = shell_test__run;
test_suite = zalloc(sizeof(*test_suite));
if (!test_suite) {
pr_err("Out of memory while building script test suite list\n");
free(tests);
return;
}
test_suite->desc = desc;
test_suite->test_cases = tests;
test_suite->priv = strdup_check(filename);
test_suite->needs_mlock = needs_mlock;
/* Realloc is good enough, though we could realloc by chunks, not that
* anyone will ever measure performance here */
result_tmp = realloc(*result, (*result_sz + 1) * sizeof(*result_tmp));
if (result_tmp == NULL) {
pr_err("Out of memory while building script test suite list\n");
free(tests);
free(test_suite);
return;
}
/* Add file to end and NULL terminate the struct array */
*result = result_tmp;
(*result)[*result_sz] = test_suite;
(*result_sz)++;
}
static void append_scripts_in_dir(int dir_fd,
struct test_suite ***result,
size_t *result_sz)
{
struct dirent **entlist;
struct dirent *ent;
int n_dirs, i;
/* List files, sorted by alpha */
n_dirs = scandirat(dir_fd, ".", &entlist, NULL, alphasort);
if (n_dirs == -1)
return;
for (i = 0; i < n_dirs && (ent = entlist[i]); i++) {
int fd;
if (ent->d_name[0] == '.')
continue; /* Skip hidden files */
if (is_test_script(dir_fd, ent->d_name)) { /* It's a test */
char *desc = shell_test__description(dir_fd, ent->d_name);
if (desc) /* It has a desc line - valid script */
append_script(dir_fd, ent->d_name, desc, result, result_sz);
continue;
}
if (ent->d_type != DT_DIR) {
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;
}
if (strncmp(ent->d_name, "base_", 5) == 0)
continue; /* Skip scripts that have a separate driver. */
fd = openat(dir_fd, ent->d_name, O_PATH);
append_scripts_in_dir(fd, result, result_sz);
close(fd);
}
for (i = 0; i < n_dirs; i++) /* Clean up */
zfree(&entlist[i]);
free(entlist);
}
struct test_suite **create_script_test_suites(void)
{
struct test_suite **result = NULL, **result_tmp;
size_t result_sz = 0;
int dir_fd = shell_tests__dir_fd(); /* Walk dir */
/*
* Append scripts if fd is good, otherwise return a NULL terminated zero
* length array.
*/
if (dir_fd >= 0)
append_scripts_in_dir(dir_fd, &result, &result_sz);
result_tmp = realloc(result, (result_sz + 1) * sizeof(*result_tmp));
if (result_tmp == NULL) {
pr_err("Out of memory while building script test suite list\n");
abort();
}
/* NULL terminate the test suite array. */
result = result_tmp;
result[result_sz] = NULL;
if (dir_fd >= 0)
close(dir_fd);
return result;
}
|