File: litest-runner.c

package info (click to toggle)
libinput 1.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,404 kB
  • sloc: ansic: 104,881; python: 3,570; sh: 183; makefile: 37; cpp: 7
file content (1134 lines) | stat: -rw-r--r-- 26,137 bytes parent folder | download
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
/*
 * Copyright © 2024 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "config.h"

#include <errno.h>
#include <inttypes.h>
#include <sys/epoll.h>
#include <sys/sysinfo.h>
#include <sys/timerfd.h>
#include <sys/wait.h>
#ifdef HAVE_PIDFD_OPEN
#include <sys/syscall.h>
#endif
#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <valgrind/valgrind.h>

#include "util-files.h"
#include "util-list.h"
#include "util-multivalue.h"
#include "util-stringbuf.h"

#include "litest-runner.h"

static bool use_jmpbuf; /* only used for max_forks = 0 */
static jmp_buf jmpbuf;

/* musl doesn't have this one but it's not that important */
#ifndef HAVE_SIGABBREV_NP
#define sigabbrev_np(...) "???"
#endif

static struct litest_runner *global_runner = NULL;

enum litest_runner_logfds {
	FD_STDOUT,
	FD_STDERR,
	FD_LOG,
	FD_VALGRIND,
	_FD_LAST,
};

struct litest_runner_test {
	struct litest_runner_test_description desc;
	struct list node;

	enum litest_runner_result result;
	int sig_or_errno;

	struct stringbuf logs[_FD_LAST];
	pid_t pid;              /* the test's PID, if any */
	int read_fds[_FD_LAST]; /* logging fds while the test is running */

	int epollfd;
	int pidfd;
	int timerfd;

	struct {
		uint64_t start_millis;
		uint64_t end_millis;
	} times;
};

struct litest_runner {
	size_t max_forks;
	unsigned int timeout;
	bool verbose;
	bool use_colors;
	bool exit_on_fail;
	FILE *fp;

	int terminating;

	struct list tests;          /* struct litest_runner_test */
	struct list tests_running;  /* struct litest_runner_test */
	struct list tests_complete; /* struct litest_runner_test */

	struct {
		time_t start;
		time_t end;
		uint64_t start_millis;
	} times;

	struct {
		litest_runner_global_setup_func_t setup;
		litest_runner_global_teardown_func_t teardown;
		void *userdata;
	} global;
};

/**
 * A global variable that the tests can use
 * to write log data to. Defaults to stdout
 * but if used by the tests it shows up as separate
 * from stdout in the logs.
 */
int testlog_fd = STDOUT_FILENO;

static void
close_pipes(int fds[_FD_LAST])
{
	for (int i = 0; i < _FD_LAST; i++) {
		fsync(fds[i]);
		xclose(&fds[i]);
	}
}

static int
init_pipes(int read_fds[_FD_LAST], int write_fds[_FD_LAST])
{
	int r;
	int i;
	int pipe_max_size = 4194304;

	for (i = 0; i < _FD_LAST; i++) {
		read_fds[i] = -1;
		write_fds[i] = -1;
	}

#ifdef __linux__
	{
		FILE *f;
		f = fopen("/proc/sys/fs/pipe-max-size", "re");
		if (f) {
			if (fscanf(f, "%d", &r) == 1)
				pipe_max_size = min(r, pipe_max_size);
			fclose(f);
		}
	}
#endif

	for (i = 0; i < _FD_LAST; i++) {
		int pipe[2];

		r = pipe2(pipe, O_CLOEXEC | O_NONBLOCK);
		if (r < 0)
			goto error;
		read_fds[i] = pipe[0];
		write_fds[i] = pipe[1];
#ifdef __linux__
		/* Max pipe buffers, to avoid scrambling if reading lags.
		 * Can't use blocking write fds, since reading too slow
		 * then affects execution.
		 */
		fcntl(write_fds[i], F_SETPIPE_SZ, pipe_max_size);
#endif
	}

	return 0;
error:
	r = -errno;
	close_pipes(read_fds);
	close_pipes(write_fds);
	return r;
}

static const char *
litest_runner_result_as_str(enum litest_runner_result result)
{
	switch (result) {
	CASE_RETURN_STRING(LITEST_PASS);
	CASE_RETURN_STRING(LITEST_NOT_APPLICABLE);
	CASE_RETURN_STRING(LITEST_FAIL);
	CASE_RETURN_STRING(LITEST_SYSTEM_ERROR);
	CASE_RETURN_STRING(LITEST_TIMEOUT);
	CASE_RETURN_STRING(LITEST_SKIP);
	}

	litest_abort_msg("Unknown result %d", result);
	return NULL;
}

static void
litest_runner_test_close(struct litest_runner_test *t)
{
	for (size_t i = 0; i < ARRAY_LENGTH(t->read_fds); i++) {
		xclose(&t->read_fds[i]);
	}
	xclose(&t->epollfd);
	xclose(&t->pidfd);
	xclose(&t->timerfd);
}

static void
litest_runner_test_destroy(struct litest_runner_test *t)
{
	list_remove(&t->node);
	close_pipes(t->read_fds);
	if (t->pid != 0) {
		kill(t->pid, SIGTERM);
		t->pid = 0;
	}
	litest_runner_test_close(t);
	for (int i = 0; i < _FD_LAST; i++) {
		stringbuf_reset(&t->logs[i]);
	}
	free(t);
}

static void
litest_runner_detach_tests(struct litest_runner *runner)
{
	struct litest_runner_test *t;

	list_for_each_safe(t, &runner->tests, node) {
		t->pid = 0;
	}
	list_for_each_safe(t, &runner->tests_complete, node) {
		t->pid = 0;
	}
	list_for_each_safe(t, &runner->tests_running, node) {
		t->pid = 0;
	}
}

void
litest_runner_destroy(struct litest_runner *runner)
{
	struct litest_runner_test *t;
	list_for_each_safe(t, &runner->tests, node) {
		litest_runner_test_destroy(t);
	}
	list_for_each_safe(t, &runner->tests_complete, node) {
		litest_runner_test_destroy(t);
	}
	list_for_each_safe(t, &runner->tests_running, node) {
		litest_runner_test_destroy(t);
	}
	free(runner);
}

static enum litest_runner_result
litest_runner_test_run(const struct litest_runner_test_description *desc)
{
	const struct litest_runner_test_env env = {
		.rangeval = desc->rangeval,
		.params = desc->params,
	};

	if (desc->setup)
		desc->setup(desc);

	enum litest_runner_result result = desc->func(&env);

	if (desc->teardown)
		desc->teardown(desc);

	return result;
}

static void
sighandler_forked_child(int signal)
{
	struct sigaction act;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	act.sa_handler = SIG_DFL;
	sigaction(signal, &act, NULL);

	/* abort() was probably called by litest_assert... which inserts
	 * the backtrace anyway -  we only need to backtrace the other signals
	 */
	if (signal != SIGABRT)
		litest_backtrace(NULL);

	raise(signal);
}

static int
litest_runner_fork_test(struct litest_runner *runner, struct litest_runner_test *t)
{
	pid_t pid;
	struct sigaction act;
	int write_fds[_FD_LAST];
	int r;

	r = init_pipes(t->read_fds, write_fds);
	if (r < 0) {
		return -1;
	}

	pid = fork();
	if (pid < 0) {
		int r = -errno;
		close_pipes(t->read_fds);
		close_pipes(write_fds);
		return r;
	}

	if (pid > 0) { /* parent */
		close_pipes(write_fds);
		t->pid = pid;
		return pid;
	}

	/* child */
	close_pipes(t->read_fds);

	/* Catch any crashers so we can insert a backtrace */
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	act.sa_handler = sighandler_forked_child;
	sigaction(SIGSEGV, &act, NULL);
	sigaction(SIGBUS, &act, NULL);
	sigaction(SIGSEGV, &act, NULL);
	sigaction(SIGABRT, &act, NULL);
	/* SIGALARM is used for our timeout */
	sigaction(SIGALRM, &act, NULL);

	r = dup2(write_fds[FD_STDERR], STDERR_FILENO);
	litest_assert_errno_success(r);
	setlinebuf(stderr);
	r = dup2(write_fds[FD_STDOUT], STDOUT_FILENO);
	litest_assert_errno_success(r);
	setlinebuf(stdout);

	/* For convenience in the tests, let this be a global variable. */
	testlog_fd = write_fds[FD_LOG];

	/* We're forked so we have a full copy of everything - but all we want
	 * to do is avoid valgrind complaining about memleaks in the child
	 * process. So let's cleanup everything, copy the one thing we
	 * care about and proceed to the exit */
	struct litest_runner_test_description desc = t->desc;
	litest_runner_detach_tests(runner);
	litest_runner_destroy(runner);

	/* Now run the actual test */
	enum litest_runner_result result = litest_runner_test_run(&desc);

	close_pipes(write_fds);

	exit(result);
}

static char *
valgrind_logfile(pid_t pid)
{
	const char *prefix = getenv("LITEST_VALGRIND_LOGDIR");
	if (!prefix)
		prefix = ".";

	char *filename = strdup_printf("%s/valgrind.%d.log", prefix, pid);
	litest_assert_ptr_notnull(filename);

	return filename;
}

static void
collect_file(const char *filename, struct stringbuf *b)
{
	int fd = open(filename, O_RDONLY);
	if (fd == -1) {
		_autofree_ char *msg =
			strdup_printf("Failed to find '%s': %m", filename);
		stringbuf_append_string(b, msg);
	} else {
		stringbuf_append_from_fd(b, fd, 0);
		close(fd);
	}
}

static bool
litest_runner_test_collect_child(struct litest_runner_test *t)
{
	int r;
	int status;

	r = waitpid(t->pid, &status, WNOHANG);
	if (r <= 0)
		return false;

	if (WIFEXITED(status)) {
		t->result = WEXITSTATUS(status);
		if (RUNNING_ON_VALGRIND && t->result == 3) {
			char msg[64];
			snprintf(msg,
				 sizeof(msg),
				 "valgrind exited with an error code, see logs\n");
			stringbuf_append_string(&t->logs[FD_LOG], msg);
			t->result = LITEST_SYSTEM_ERROR;
		}
		switch (t->result) {
		case LITEST_PASS:
		case LITEST_SKIP:
		case LITEST_NOT_APPLICABLE:
		case LITEST_FAIL:
		case LITEST_TIMEOUT:
		case LITEST_SYSTEM_ERROR:
			break;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
		case 0:
			/* if a test execve's itself allow for the normal
			 * exit codes to map to the results */
			t->result = LITEST_PASS;
			break;
#pragma GCC diagnostic pop
		default: {
			char msg[64];
			snprintf(msg,
				 sizeof(msg),
				 "Invalid test exit status %d",
				 t->result);
			stringbuf_append_string(&t->logs[FD_LOG], msg);
			t->result = LITEST_FAIL;
			break;
		}
		}
	} else {
		if (WIFSIGNALED(status)) {
			t->sig_or_errno = WTERMSIG(status);
			t->result = (t->sig_or_errno == t->desc.args.signal)
					    ? LITEST_PASS
					    : LITEST_FAIL;
		} else {
			t->result = LITEST_FAIL;
		}
	}

	uint64_t now = 0;
	now_in_us(&now);
	t->times.end_millis = us2ms(now);

	if (RUNNING_ON_VALGRIND) {
		_autofree_ char *filename = valgrind_logfile(t->pid);
		collect_file(filename, &t->logs[FD_VALGRIND]);
	}

	t->pid = 0;

	return true;
}

static int
litest_runner_test_setup_monitoring(struct litest_runner *runner,
				    struct litest_runner_test *t)
{
	int pidfd = -1, timerfd = -1, epollfd = -1;
	struct epoll_event ev[10];
	size_t nevents = 0;
	int r = 0;

#ifdef HAVE_PIDFD_OPEN
	pidfd = syscall(SYS_pidfd_open, t->pid, 0);
#else
	errno = ENOSYS;
#endif
	/* If we don't have pidfd, we use a timerfd to ping us every 200ms */
	if (pidfd < 0 && errno == ENOSYS) {
		pidfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
		if (pidfd == -1) {
			r = -errno;
			goto error;
		}
		r = timerfd_settime(pidfd,
				    0,
				    &((struct itimerspec){
					    .it_interval.tv_nsec = 200 * 1000 * 1000,
					    .it_value.tv_nsec = 200 * 1000 * 1000,
				    }),
				    NULL);
		if (r < 0) {
			r = -errno;
			goto error;
		}
	}

	/* Each test has an epollfd with:
	 *   - a timerfd so we can kill() it if it hangs
	 *   - a pidfd so we get notified when the test exits
	 *   - a pipe for stdout and a pipe for stderr
	 *   - a pipe for logging (the various pwtest functions)
	 *   - a pipe for the daemon's stdout
	 */
	timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
	if (timerfd < 0) {
		r = -errno;
		goto error;
	}
	timerfd_settime(timerfd,
			0,
			&((struct itimerspec){ .it_value.tv_sec = runner->timeout }),
			NULL);

	epollfd = epoll_create(1);
	if (epollfd < 0)
		goto error;
	ev[nevents++] = (struct epoll_event){ .events = EPOLLIN, .data.fd = pidfd };
	ev[nevents++] = (struct epoll_event){ .events = EPOLLIN,
					      .data.fd = t->read_fds[FD_STDOUT] };
	ev[nevents++] = (struct epoll_event){ .events = EPOLLIN,
					      .data.fd = t->read_fds[FD_STDERR] };
	ev[nevents++] = (struct epoll_event){ .events = EPOLLIN,
					      .data.fd = t->read_fds[FD_LOG] };
	ev[nevents++] = (struct epoll_event){ .events = EPOLLIN, .data.fd = timerfd };

	for (size_t i = 0; i < nevents; i++) {
		r = epoll_ctl(epollfd, EPOLL_CTL_ADD, ev[i].data.fd, &ev[i]);
		if (r < 0) {
			r = -errno;
			goto error;
		}
	}

	t->epollfd = epollfd;
	t->pidfd = pidfd;
	t->timerfd = timerfd;

	r = 0;

error:
	if (r) {
		xclose(&pidfd);
		xclose(&timerfd);
		xclose(&epollfd);
	}

	return -r;
}

static int
litest_runner_test_check_status(struct litest_runner_test *t)
{
	struct epoll_event e;

	if (t->pid == 0) /* nofork case */
		return 0;

	while (true) {
		/* FIXME: this is a busy wait ... */
		int r = epoll_wait(t->epollfd, &e, 1, 50);
		if (r == 0)
			return -EAGAIN;

		if (r == -1) {
			goto error;
		}

		if (e.data.fd == t->pidfd) {
			uint64_t buf;
			int ignore = read(t->pidfd,
					  &buf,
					  sizeof(buf)); /* for timerfd fallback */
			(void)ignore;
			if (litest_runner_test_collect_child(t)) {
				break;
			}
		} else if (e.data.fd == t->timerfd) {
			/* SIGALARM so we get the backtrace */
			kill(t->pid, SIGALRM);
			t->result = LITEST_TIMEOUT;
			waitpid(t->pid, NULL, 0);
			t->pid = 0;
			break;
		} else {
			for (int i = 0; i < _FD_LAST; i++) {
				if (e.data.fd == t->read_fds[i]) {
					stringbuf_append_from_fd(&t->logs[i],
								 e.data.fd,
								 1024);
				}
			}
		}
	};

	errno = 0;
error:
	return -errno;
}

static void
litest_runner_test_update_errno(struct litest_runner_test *t, int error)
{
	if (error)
		t->sig_or_errno = -error;

	if (error == SIGTERM) {
		char msg[64];
		snprintf(msg, sizeof(msg), "litest: tests terminated by signal\n");
		stringbuf_append_string(&t->logs[FD_LOG], msg);
		t->result = LITEST_SYSTEM_ERROR;
	}

	for (size_t i = 0; i < ARRAY_LENGTH(t->read_fds); i++) {
		stringbuf_append_from_fd(&t->logs[i], t->read_fds[i], 1024);
	}
}

__attribute__((noreturn)) void
litest_runner_abort(void)
{
	if (use_jmpbuf) {
		longjmp(jmpbuf, SIGABRT);
	} else {
		abort();
	}
}

static int
litest_runner_run_test(struct litest_runner *runner, struct litest_runner_test *t)
{
	int r;

	t->result = LITEST_SYSTEM_ERROR;

	uint64_t now = 0;
	now_in_us(&now);
	t->times.start_millis = us2ms(now);

	if (runner->max_forks == 0) {
		if (use_jmpbuf && setjmp(jmpbuf) == 0) {
			t->result = litest_runner_test_run(&t->desc);
		} else {
			t->result = LITEST_FAIL;
		}
		r = 0; /* -Wclobbered */
	} else {
		r = litest_runner_fork_test(runner, t);
		if (r >= 0)
			r = litest_runner_test_setup_monitoring(runner, t);
		litest_runner_test_update_errno(t, -r);
	}

	if (r >= 0) {
		list_remove(&t->node);
		list_append(&runner->tests_running, &t->node);
	}

	return r;
}

static void
print_lines(FILE *fp, const char *log, const char *prefix)
{
	size_t nlines = 0;
	_autostrvfree_ char **lines = strv_from_string(log, "\n", &nlines);

	for (size_t i = 0; i < nlines; i++) {
		fprintf(fp, "%s%s\n", prefix, lines[i]);
	}
}

void
_litest_test_param_fetch(const struct litest_test_parameters *params, ...)
{
	struct litest_test_param *p;

	const char *name;

	va_list args;
	va_start(args, params);

	while ((name = va_arg(args, const char *))) {
		bool found = false;
		char type = (char)va_arg(args, int);
		void **ptr = va_arg(args, void *);
		list_for_each(p, &params->test_params, link) {
			if (streq(p->name, name)) {
				if (tolower(p->value.type) != tolower(type))
					litest_abort_msg(
						"Paramter type mismatch: parameter '%s' is of type %c",
						p->name,
						p->value.type);
				found = true;
				multivalue_extract(&p->value, ptr);
				break;
			}
		}
		if (!found)
			litest_abort_msg("Unknown test parameter name '%s'", name);
	}

	va_end(args);
}

struct litest_test_parameters *
litest_test_parameters_new(void)
{
	struct litest_test_parameters *params = zalloc(sizeof *params);
	params->refcnt = 1;
	list_init(&params->test_params);
	return params;
}

struct litest_test_parameters *
litest_test_parameters_unref(struct litest_test_parameters *params)
{
	if (params) {
		assert(params->refcnt > 0);
		if (--params->refcnt == 0) {
			struct litest_test_param *p;
			list_for_each_safe(p, &params->test_params, link) {
				free(p);
			}
			free(params);
		}
	}
	return NULL;
}

static void
litest_runner_log_test_result(struct litest_runner *runner,
			      struct litest_runner_test *t)
{
	const char *color = NULL;
	const char *status = NULL;

	litest_assert_int_ge(t->result, (enum litest_runner_result)LITEST_PASS);
	litest_assert_int_le(t->result, (enum litest_runner_result)LITEST_SYSTEM_ERROR);

	switch (t->result) {
	case LITEST_PASS:
		color = ANSI_BOLD_GREEN;
		break;
	case LITEST_FAIL:
		color = ANSI_BOLD_RED;
		break;
	case LITEST_SKIP:
		color = ANSI_BOLD_YELLOW;
		break;
	case LITEST_NOT_APPLICABLE:
		color = ANSI_BLUE;
		break;
	case LITEST_TIMEOUT:
		color = ANSI_BOLD_CYAN;
		break;
	case LITEST_SYSTEM_ERROR:
		color = ANSI_BOLD_MAGENTA;
		break;
	}

	fprintf(runner->fp, "  - name: \"%s\"\n", t->desc.name);
	int min = t->desc.args.range.lower, max = t->desc.args.range.upper;
	if (range_is_valid(&t->desc.args.range))
		fprintf(runner->fp,
			"    rangeval: %d  # %d..%d\n",
			t->desc.rangeval,
			min,
			max);

	if (t->desc.params) {
		fprintf(runner->fp, "    params:\n");
		struct litest_test_param *p;
		list_for_each(p, &t->desc.params->test_params, link) {
			_autofree_ char *val = multivalue_as_str(&p->value);
			fprintf(runner->fp, "      %s: %s\n", p->name, val);
		}
	}

	fprintf(runner->fp,
		"    duration: %" PRIu64 "  # (ms), total test run time: %02" PRIu32
		":%02" PRIu32 "\n",
		t->times.end_millis - t->times.start_millis,
		(ms2s(t->times.end_millis - runner->times.start_millis)) / 60,
		(ms2s(t->times.end_millis - runner->times.start_millis)) % 60);

	status = litest_runner_result_as_str(t->result);
	fprintf(runner->fp,
		"    status: %s%s%s\n",
		runner->use_colors ? color : "",
		&status[7], /* skip LITEST_ prefix */
		runner->use_colors ? ANSI_NORMAL : "");

	switch (t->result) {
	case LITEST_PASS:
	case LITEST_SKIP:
	case LITEST_NOT_APPLICABLE:
		if (!runner->verbose)
			return;
		break;
	default:
		break;
	}

	if (t->sig_or_errno > 0)
		fprintf(runner->fp,
			"    signal: %d # SIG%s \n",
			t->sig_or_errno,
			sigabbrev_np(t->sig_or_errno));
	else if (t->sig_or_errno < 0)
		fprintf(runner->fp,
			"    errno: %d # %s\n",
			-t->sig_or_errno,
			strerror(-t->sig_or_errno));
	if (!stringbuf_is_empty(&t->logs[FD_LOG])) {
		fprintf(runner->fp, "    log: |\n");
		print_lines(runner->fp, t->logs[FD_LOG].data, "      ");
	}
	if (!stringbuf_is_empty(&t->logs[FD_STDOUT])) {
		fprintf(runner->fp, "    stdout: |\n");
		print_lines(runner->fp, t->logs[FD_STDOUT].data, "      ");
	}
	if (!stringbuf_is_empty(&t->logs[FD_STDERR])) {
		fprintf(runner->fp, "    stderr: |\n");
		print_lines(runner->fp, t->logs[FD_STDERR].data, "      ");
	}
	if (!stringbuf_is_empty(&t->logs[FD_VALGRIND])) {
		fprintf(runner->fp, "    valgrind: |\n");
		print_lines(runner->fp, t->logs[FD_VALGRIND].data, "      ");
	}
}

struct litest_runner *
litest_runner_new(void)
{
	struct litest_runner *runner = zalloc(sizeof *runner);

	list_init(&runner->tests);
	list_init(&runner->tests_complete);
	list_init(&runner->tests_running);
	runner->timeout = LITEST_RUNNER_DEFAULT_TIMEOUT;
	runner->max_forks = get_nprocs() * 2;
	runner->fp = stderr;

	return runner;
}

void
litest_runner_set_timeout(struct litest_runner *runner, unsigned int timeout)
{
	runner->timeout = timeout;
}

void
litest_runner_set_output_file(struct litest_runner *runner, FILE *fp)
{
	setlinebuf(fp);
	runner->fp = fp;
}

void
litest_runner_set_num_parallel(struct litest_runner *runner, size_t num_jobs)
{
	runner->max_forks = num_jobs;
}

void
litest_runner_set_verbose(struct litest_runner *runner, bool verbose)
{
	runner->verbose = verbose;
}

void
litest_runner_set_use_colors(struct litest_runner *runner, bool use_colors)
{
	runner->use_colors = use_colors;
}

void
litest_runner_set_exit_on_fail(struct litest_runner *runner, bool do_exit)
{
	runner->exit_on_fail = do_exit;
}

void
litest_runner_set_setup_funcs(struct litest_runner *runner,
			      litest_runner_global_setup_func_t setup,
			      litest_runner_global_teardown_func_t teardown,
			      void *userdata)
{
	runner->global.setup = setup;
	runner->global.teardown = teardown;
	runner->global.userdata = userdata;
}

void
litest_runner_add_test(struct litest_runner *runner,
		       const struct litest_runner_test_description *desc)
{
	struct litest_runner_test *t = zalloc(sizeof(*t));

	t->desc = *desc;
	t->epollfd = -1;
	t->pidfd = -1;
	t->timerfd = -1;

	for (int i = 0; i < _FD_LAST; i++) {
		stringbuf_init(&t->logs[i]);
	}

	for (size_t i = 0; i < ARRAY_LENGTH(t->read_fds); i++) {
		t->read_fds[i] = -1;
	}

	list_append(&runner->tests, &t->node);
}

static int
litest_runner_check_finished_tests(struct litest_runner *runner)
{
	struct litest_runner_test *running;
	size_t count = 0;

	list_for_each_safe(running, &runner->tests_running, node) {
		int r = litest_runner_test_check_status(running);
		if (r == -EAGAIN)
			continue;

		uint64_t now = 0;
		now_in_us(&now);
		running->times.end_millis = us2ms(now);

		if (r < 0)
			litest_runner_test_update_errno(running, -r);

		litest_runner_log_test_result(runner, running);
		litest_runner_test_close(running);
		list_remove(&running->node);
		list_append(&runner->tests_complete, &running->node);
		count++;
	}

	return count;
}

static void
runner_sighandler(int sig)
{
	struct litest_runner_test *t;
	struct litest_runner *runner = global_runner;

	list_for_each(t, &runner->tests_running, node) {
		if (t->pid != 0) {
			kill(t->pid, SIGTERM);
			t->pid = 0;
		}
	}

	global_runner->terminating = true;
}

static inline void
setup_sighandler(int sig)
{
	struct sigaction act, oact;
	int rc;

	sigemptyset(&act.sa_mask);
	sigaddset(&act.sa_mask, sig);
	act.sa_flags = 0;
	act.sa_handler = runner_sighandler;
	rc = sigaction(sig, &act, &oact);
	litest_assert_int_ne(rc, -1);
}

enum litest_runner_result
litest_runner_run_tests(struct litest_runner *runner)
{
	struct litest_runner_test *t;
	size_t available_jobs = max(runner->max_forks, 1);
	char timestamp[64];
	struct tm *ltime;

	global_runner = runner; /* sigh, need this for signal handling */

	if (runner->global.setup)
		runner->global.setup(runner->global.userdata);

	use_jmpbuf = runner->max_forks == 0;

	setup_sighandler(SIGINT);

	uint64_t now = 0;
	now_in_us(&now);

	runner->times.start_millis = us2ms(now);
	runner->times.start = time(NULL);
	ltime = localtime(&runner->times.start);
	strftime(timestamp, sizeof(timestamp), "%FT%H:%M", ltime);
	fprintf(runner->fp,
		"start: %lld  # \"%s\"\n",
		(long long)runner->times.start,
		timestamp);
	fprintf(runner->fp, "jobs: %zd\n", runner->max_forks);
	fprintf(runner->fp, "tests:\n");
	list_for_each_safe(t, &runner->tests, node) {
		int r = litest_runner_run_test(runner, t);
		if (r >= 0) {
			available_jobs--;
		}

		/* Wait for something to become available */
		while (available_jobs == 0 && !runner->terminating) {
			int complete = litest_runner_check_finished_tests(runner);
			available_jobs += complete;
		}

		if (runner->terminating) {
			break;
		}

		if (runner->exit_on_fail) {
			bool do_exit = false;
			struct litest_runner_test *complete;
			list_for_each(complete, &runner->tests_complete, node) {
				switch (complete->result) {
				case LITEST_FAIL:
				case LITEST_SYSTEM_ERROR:
				case LITEST_TIMEOUT:
					do_exit = true;
					break;
				default:
					break;
				}
				if (do_exit)
					break;
			}
			if (do_exit)
				break;
		}
	}

	while (!runner->terminating && !list_empty(&runner->tests_running)) {
		litest_runner_check_finished_tests(runner);
	}

	if (runner->global.teardown)
		runner->global.teardown(runner->global.userdata);

	size_t npass = 0, nfail = 0, nskip = 0, nna = 0;
	size_t ncomplete = 0;

	list_for_each(t, &runner->tests_complete, node) {
		ncomplete++;
		switch (t->result) {
		case LITEST_PASS:
			npass++;
			break;
		case LITEST_NOT_APPLICABLE:
			nna++;
			break;
		case LITEST_FAIL:
		case LITEST_SYSTEM_ERROR:
		case LITEST_TIMEOUT:
			nfail++;
			break;
		case LITEST_SKIP:
			nskip++;
			break;
		}
	}

	runner->times.end = time(NULL);
	ltime = localtime(&runner->times.end);
	strftime(timestamp, sizeof(timestamp), "%FT%H:%M", ltime);
	fprintf(runner->fp,
		"end: %lld  # \"%s\"\n",
		(long long)runner->times.end,
		timestamp);
	fprintf(runner->fp,
		"duration: %lld  # (s) %02lld:%02lld\n",
		(long long)(runner->times.end - runner->times.start),
		(long long)((runner->times.end - runner->times.start) / 60),
		(long long)((runner->times.end - runner->times.start) % 60));
	fprintf(runner->fp, "summary:\n");
	fprintf(runner->fp, "  completed: %zd\n", ncomplete);
	fprintf(runner->fp, "  pass: %zd\n", npass);
	fprintf(runner->fp, "  na: %zd\n", nna);
	fprintf(runner->fp, "  fail: %zd\n", nfail);
	fprintf(runner->fp, "  skip: %zd\n", nskip);
	if (nfail > 0) {
		fprintf(runner->fp, "  failed:\n");
		list_for_each(t, &runner->tests_complete, node) {
			switch (t->result) {
			case LITEST_FAIL:
			case LITEST_SYSTEM_ERROR:
			case LITEST_TIMEOUT:
				litest_runner_log_test_result(runner, t);
				break;
			default:
				break;
			}
		}
	}

	if (RUNNING_ON_VALGRIND) {
		_autofree_ char *filename = valgrind_logfile(getpid());
		_destroy_(stringbuf) *b = stringbuf_new();

		collect_file(filename, b);
		fprintf(runner->fp, "valgrind:\n");
		print_lines(runner->fp, b->data, "  ");
		fprintf(runner->fp,
			"# Valgrind log is incomplete, see %s for full log\n",
			filename);
	}

	enum litest_runner_result result = LITEST_PASS;

	/* Didn't finish */
	if (!list_empty(&runner->tests) || !list_empty(&runner->tests_running)) {
		result = LITEST_SYSTEM_ERROR;
	} else {
		list_for_each(t, &runner->tests_complete, node) {
			switch (t->result) {
			case LITEST_PASS:
			case LITEST_SKIP:
			case LITEST_NOT_APPLICABLE:
				break;
			default:
				result = LITEST_FAIL;
				break;
			}
		}
	}
	/* Status is always prefixed with LITEST_ */
	fprintf(runner->fp, "  status: %s\n", &litest_runner_result_as_str(result)[7]);

	return result;
}