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
|
#define _GNU_SOURCE
#include <ctype.h>
#include <getopt.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define die(msg, ...) \
do { \
fprintf(stderr, msg "\n", ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
static char *mprintf(const char *fmt, ...)
{
va_list args;
char *str;
int ret;
va_start(args, fmt);
ret = vasprintf(&str, fmt, args);
va_end(args);
if (ret < 0)
die("insufficient memory");
return str;
}
static struct timespec xclock_gettime(clockid_t clockid)
{
struct timespec ts;
if (clock_gettime(clockid, &ts))
die("clock_gettime error: %m");
return ts;
}
static pid_t child;
static int childfd;
static unsigned long default_timeout;
static unsigned long timeout;
static char *logdir;
static char *test_basename;
static char *full_log;
static char *current_test;
static struct timespec current_test_start;
static FILE *current_test_log;
static void alarm_handler(int sig)
{
char *msg = mprintf("========= FAILED TIMEOUT %s in %lus\n",
current_test ?: "(no test)", timeout);
if (write(childfd, msg, strlen(msg)) != strlen(msg))
die("write error in alarm handler");
free(msg);
}
static void set_timeout(unsigned long new_timeout)
{
timeout = new_timeout;
alarm(new_timeout);
}
static FILE *test_file_open(const char *fname)
{
char *path = mprintf("%s/%s.%s/%s", logdir, test_basename,
current_test, fname);
FILE *f = fopen(path, "w");
if (!f)
die("error opening %s: %m", path);
free(path);
setlinebuf(f);
return f;
}
static FILE *log_open()
{
char *path = mprintf("%s/%s", logdir, full_log);
FILE *f = fopen(path, "w");
if (!f)
die("error opening %s: %m", path);
free(path);
setlinebuf(f);
return f;
}
static void strim(char *line)
{
char *p = line;
while (!iscntrl(*p))
p++;
*p = 0;
}
static const char *str_starts_with(const char *str, const char *prefix)
{
unsigned len = strlen(prefix);
if (strncmp(str, prefix, len))
return NULL;
return str + len;
}
static char *test_is_starting(const char *line)
{
const char *testname = str_starts_with(line, "========= TEST ");
char *ret, *p;
if (!testname)
return NULL;
ret = strdup(testname);
while ((p = strchr(ret, '/')))
*p = '.';
return ret;
}
static bool test_is_ending(char *line)
{
return str_starts_with(line, "========= PASSED ") ||
str_starts_with(line, "========= FAILED ") ||
str_starts_with(line, "========= NOTRUN");
}
static FILE *popen_with_pid(char *argv[], pid_t *child)
{
int pipefd[2];
if (pipe(pipefd))
die("error creating pipe: %m");
*child = fork();
if (*child < 0)
die("fork error: %m");
if (!*child) {
if (dup2(pipefd[1], STDOUT_FILENO) < 0)
die("dup2 error: %m");
if (dup2(pipefd[1], STDERR_FILENO) < 0)
die("dup2 error: %m");
close(pipefd[1]);
int devnull = open("/dev/null", O_RDONLY);
if (devnull < 0)
die("error opening /dev/null; %m");
if (dup2(devnull, STDIN_FILENO) < 0)
die("dup2 error: %m");
close(devnull);
execvp(argv[0], argv);
die("error execing %s: %m", argv[0]);
}
childfd = pipefd[1];
FILE *childf = fdopen(pipefd[0], "r");
if (!childf)
die("fdopen error: %m");
return childf;
}
static void read_watchdog(const char *line)
{
const char *new_watchdog = str_starts_with(line, "WATCHDOG ");
if (new_watchdog)
set_timeout(atol(new_watchdog));
}
static void write_test_file(const char *file, const char *fmt, ...)
{
va_list args;
FILE *f = test_file_open(file);
va_start(args, fmt);
vfprintf(f, fmt, args);
va_end(args);
fclose(f);
}
static void test_start(char *new_test, struct timespec now)
{
free(current_test);
current_test = new_test;
current_test_start = now;
current_test_log = test_file_open("log");
write_test_file("status", "TEST FAILED");
set_timeout(default_timeout);
}
static void test_end(struct timespec now)
{
write_test_file("duration", "%li", now.tv_sec - current_test_start.tv_sec);
fclose(current_test_log);
current_test_log = NULL;
set_timeout(default_timeout);
}
static void usage(void)
{
puts("qemu-wrapper - wrapper for qemu to catch test success/failure\n"
"Usage: qemu-wrapper [OPTIONS] -- <qemu-command>\n"
"\n"
"Options\n"
" -S Exit on success\n"
" -F Exit on failure\n"
" -T TIMEOUT Timeout after TIMEOUT seconds\n"
" -b name base name for log files\n"
" -o dir output directory for log files\n"
" -h Display this help and exit");
}
int main(int argc, char *argv[])
{
bool exit_on_success = false;
bool exit_on_failure = false;
int opt, ret = EXIT_FAILURE;
struct timespec start;
setlinebuf(stdin);
setlinebuf(stdout);
if (clock_gettime(CLOCK_MONOTONIC, &start))
die("clock_gettime error: %m");
while ((opt = getopt(argc, argv, "SFT:b:o:f:h")) != -1) {
switch (opt) {
case 'S':
exit_on_success = true;
break;
case 'F':
exit_on_failure = true;
break;
case 'T':
errno = 0;
default_timeout = strtoul(optarg, NULL, 10);
if (errno)
die("error parsing timeout: %m");
break;
case 'b':
test_basename = strdup(optarg);
break;
case 'f':
full_log = strdup(optarg);
break;
case 'o':
logdir = strdup(optarg);
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
case '?':
usage();
exit(EXIT_FAILURE);
}
}
if (!test_basename)
die("Required option -b missing");
if (!logdir)
die("Required option -o missing");
FILE *childf = popen_with_pid(argv + optind, &child);
FILE *logfile = log_open();
size_t n = 0;
ssize_t len;
char *line = NULL;
struct sigaction alarm_action = { .sa_handler = alarm_handler };
if (sigaction(SIGALRM, &alarm_action, NULL))
die("sigaction error: %m");
set_timeout(default_timeout);
again:
while ((len = getline(&line, &n, childf)) >= 0) {
struct timespec now = xclock_gettime(CLOCK_MONOTONIC);
strim(line);
char *output = mprintf("%.5lu %s\n", now.tv_sec - start.tv_sec, line);
read_watchdog(line);
char *new_test = test_is_starting(line);
/* If a test is starting, close logfile for previous test: */
if (current_test_log && new_test)
test_end(now);
if (new_test)
test_start(new_test, now);
if (current_test_log)
fputs(output, current_test_log);
fputs(output, logfile);
fputs(output, stdout);
if (current_test_log && test_is_ending(line)) {
write_test_file("status", "%s", line);
test_end(now);
}
if (exit_on_failure && str_starts_with(line, "TEST FAILED"))
break;
if (exit_on_failure && strstr(line, "FAILED TIMEOUT"))
break;
if (exit_on_success && str_starts_with(line, "TEST SUCCESS")) {
ret = 0;
break;
}
if (exit_on_failure &&
(strstr(line, "Kernel panic") ||
strstr(line, "BUG")))
alarm(5);
free(output);
}
if (len == -1 && errno == EINTR) {
clearerr(childf);
goto again;
}
kill(child, SIGKILL);
exit(ret);
}
|