xref: /aosp_15_r20/external/wayland/tests/test-runner.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique  * Copyright © 2012 Intel Corporation
3*84e872a0SLloyd Pique  *
4*84e872a0SLloyd Pique  * Permission is hereby granted, free of charge, to any person obtaining
5*84e872a0SLloyd Pique  * a copy of this software and associated documentation files (the
6*84e872a0SLloyd Pique  * "Software"), to deal in the Software without restriction, including
7*84e872a0SLloyd Pique  * without limitation the rights to use, copy, modify, merge, publish,
8*84e872a0SLloyd Pique  * distribute, sublicense, and/or sell copies of the Software, and to
9*84e872a0SLloyd Pique  * permit persons to whom the Software is furnished to do so, subject to
10*84e872a0SLloyd Pique  * the following conditions:
11*84e872a0SLloyd Pique  *
12*84e872a0SLloyd Pique  * The above copyright notice and this permission notice (including the
13*84e872a0SLloyd Pique  * next paragraph) shall be included in all copies or substantial
14*84e872a0SLloyd Pique  * portions of the Software.
15*84e872a0SLloyd Pique  *
16*84e872a0SLloyd Pique  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*84e872a0SLloyd Pique  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*84e872a0SLloyd Pique  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*84e872a0SLloyd Pique  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20*84e872a0SLloyd Pique  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21*84e872a0SLloyd Pique  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22*84e872a0SLloyd Pique  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*84e872a0SLloyd Pique  * SOFTWARE.
24*84e872a0SLloyd Pique  */
25*84e872a0SLloyd Pique #include "../config.h"
26*84e872a0SLloyd Pique 
27*84e872a0SLloyd Pique #define _GNU_SOURCE
28*84e872a0SLloyd Pique 
29*84e872a0SLloyd Pique #include <unistd.h>
30*84e872a0SLloyd Pique #include <stdio.h>
31*84e872a0SLloyd Pique #include <stdlib.h>
32*84e872a0SLloyd Pique #include <sys/types.h>
33*84e872a0SLloyd Pique #include <sys/wait.h>
34*84e872a0SLloyd Pique #include <sys/stat.h>
35*84e872a0SLloyd Pique #include <string.h>
36*84e872a0SLloyd Pique #include <assert.h>
37*84e872a0SLloyd Pique #include <dlfcn.h>
38*84e872a0SLloyd Pique #include <errno.h>
39*84e872a0SLloyd Pique #include <limits.h>
40*84e872a0SLloyd Pique #include <signal.h>
41*84e872a0SLloyd Pique #include <sys/ptrace.h>
42*84e872a0SLloyd Pique #ifdef HAVE_SYS_PROCCTL_H
43*84e872a0SLloyd Pique #include <sys/procctl.h>
44*84e872a0SLloyd Pique #elif defined(HAVE_SYS_PRCTL_H)
45*84e872a0SLloyd Pique #include <sys/prctl.h>
46*84e872a0SLloyd Pique #ifndef PR_SET_PTRACER
47*84e872a0SLloyd Pique # define PR_SET_PTRACER 0x59616d61
48*84e872a0SLloyd Pique #endif
49*84e872a0SLloyd Pique #endif
50*84e872a0SLloyd Pique 
51*84e872a0SLloyd Pique #include "test-runner.h"
52*84e872a0SLloyd Pique 
53*84e872a0SLloyd Pique /* when set to 1, check if tests are not leaking opened files.
54*84e872a0SLloyd Pique  * It is turned on by default. It can be turned off by
55*84e872a0SLloyd Pique  * WAYLAND_TEST_NO_LEAK_CHECK environment variable. */
56*84e872a0SLloyd Pique int fd_leak_check_enabled;
57*84e872a0SLloyd Pique 
58*84e872a0SLloyd Pique /* when this var is set to 0, every call to test_set_timeout() is
59*84e872a0SLloyd Pique  * suppressed - handy when debugging the test. Can be set by
60*84e872a0SLloyd Pique  * WAYLAND_TEST_NO_TIMEOUTS environment variable. */
61*84e872a0SLloyd Pique static int timeouts_enabled = 1;
62*84e872a0SLloyd Pique 
63*84e872a0SLloyd Pique /* set to one if the output goes to the terminal */
64*84e872a0SLloyd Pique static int is_atty = 0;
65*84e872a0SLloyd Pique 
66*84e872a0SLloyd Pique extern const struct test __start_test_section, __stop_test_section;
67*84e872a0SLloyd Pique 
68*84e872a0SLloyd Pique static const struct test *
find_test(const char * name)69*84e872a0SLloyd Pique find_test(const char *name)
70*84e872a0SLloyd Pique {
71*84e872a0SLloyd Pique 	const struct test *t;
72*84e872a0SLloyd Pique 
73*84e872a0SLloyd Pique 	for (t = &__start_test_section; t < &__stop_test_section; t++)
74*84e872a0SLloyd Pique 		if (strcmp(t->name, name) == 0)
75*84e872a0SLloyd Pique 			return t;
76*84e872a0SLloyd Pique 
77*84e872a0SLloyd Pique 	return NULL;
78*84e872a0SLloyd Pique }
79*84e872a0SLloyd Pique 
80*84e872a0SLloyd Pique static void
usage(const char * name,int status)81*84e872a0SLloyd Pique usage(const char *name, int status)
82*84e872a0SLloyd Pique {
83*84e872a0SLloyd Pique 	const struct test *t;
84*84e872a0SLloyd Pique 
85*84e872a0SLloyd Pique 	fprintf(stderr, "Usage: %s [TEST]\n\n"
86*84e872a0SLloyd Pique 		"With no arguments, run all test.  Specify test case to run\n"
87*84e872a0SLloyd Pique 		"only that test without forking.  Available tests:\n\n",
88*84e872a0SLloyd Pique 		name);
89*84e872a0SLloyd Pique 
90*84e872a0SLloyd Pique 	for (t = &__start_test_section; t < &__stop_test_section; t++)
91*84e872a0SLloyd Pique 		fprintf(stderr, "  %s\n", t->name);
92*84e872a0SLloyd Pique 
93*84e872a0SLloyd Pique 	fprintf(stderr, "\n");
94*84e872a0SLloyd Pique 
95*84e872a0SLloyd Pique 	exit(status);
96*84e872a0SLloyd Pique }
97*84e872a0SLloyd Pique 
98*84e872a0SLloyd Pique void
test_set_timeout(unsigned int to)99*84e872a0SLloyd Pique test_set_timeout(unsigned int to)
100*84e872a0SLloyd Pique {
101*84e872a0SLloyd Pique 	int re;
102*84e872a0SLloyd Pique 
103*84e872a0SLloyd Pique 	if (!timeouts_enabled) {
104*84e872a0SLloyd Pique 		fprintf(stderr, "Timeouts suppressed.\n");
105*84e872a0SLloyd Pique 		return;
106*84e872a0SLloyd Pique 	}
107*84e872a0SLloyd Pique 
108*84e872a0SLloyd Pique 	re = alarm(to);
109*84e872a0SLloyd Pique 	fprintf(stderr, "Timeout was %sset", re ? "re-" : "");
110*84e872a0SLloyd Pique 
111*84e872a0SLloyd Pique 	if (to != 0)
112*84e872a0SLloyd Pique 		fprintf(stderr, " to %d second%s from now.\n",
113*84e872a0SLloyd Pique 			to, to > 1 ? "s" : "");
114*84e872a0SLloyd Pique 	else
115*84e872a0SLloyd Pique 		fprintf(stderr, " off.\n");
116*84e872a0SLloyd Pique }
117*84e872a0SLloyd Pique 
118*84e872a0SLloyd Pique static void
sigalrm_handler(int signum)119*84e872a0SLloyd Pique sigalrm_handler(int signum)
120*84e872a0SLloyd Pique {
121*84e872a0SLloyd Pique 	fprintf(stderr, "Test timed out.\n");
122*84e872a0SLloyd Pique 	abort();
123*84e872a0SLloyd Pique }
124*84e872a0SLloyd Pique 
125*84e872a0SLloyd Pique void
check_fd_leaks(int supposed_fds)126*84e872a0SLloyd Pique check_fd_leaks(int supposed_fds)
127*84e872a0SLloyd Pique {
128*84e872a0SLloyd Pique 	int num_fds;
129*84e872a0SLloyd Pique 
130*84e872a0SLloyd Pique 	if (fd_leak_check_enabled) {
131*84e872a0SLloyd Pique 		num_fds = count_open_fds();
132*84e872a0SLloyd Pique 		if (supposed_fds != num_fds) {
133*84e872a0SLloyd Pique 			fprintf(stderr, "fd leak detected in test. "
134*84e872a0SLloyd Pique 				"Opened %d files, unclosed %d\n", num_fds,
135*84e872a0SLloyd Pique 				num_fds - supposed_fds);
136*84e872a0SLloyd Pique 			abort();
137*84e872a0SLloyd Pique 		}
138*84e872a0SLloyd Pique 	} else {
139*84e872a0SLloyd Pique 		fprintf(stderr, "FD leak checks disabled\n");
140*84e872a0SLloyd Pique 	}
141*84e872a0SLloyd Pique }
142*84e872a0SLloyd Pique 
143*84e872a0SLloyd Pique static void
run_test(const struct test * t)144*84e872a0SLloyd Pique run_test(const struct test *t)
145*84e872a0SLloyd Pique {
146*84e872a0SLloyd Pique 	int cur_fds;
147*84e872a0SLloyd Pique 	struct sigaction sa;
148*84e872a0SLloyd Pique 
149*84e872a0SLloyd Pique 	if (timeouts_enabled) {
150*84e872a0SLloyd Pique 		sa.sa_handler = sigalrm_handler;
151*84e872a0SLloyd Pique 		sa.sa_flags = 0;
152*84e872a0SLloyd Pique 		sigemptyset(&sa.sa_mask);
153*84e872a0SLloyd Pique 		assert(sigaction(SIGALRM, &sa, NULL) == 0);
154*84e872a0SLloyd Pique 	}
155*84e872a0SLloyd Pique 
156*84e872a0SLloyd Pique 	//cur_alloc = get_current_alloc_num();
157*84e872a0SLloyd Pique 	cur_fds = count_open_fds();
158*84e872a0SLloyd Pique 
159*84e872a0SLloyd Pique 	t->run();
160*84e872a0SLloyd Pique 
161*84e872a0SLloyd Pique 	/* turn off timeout (if any) after test completion */
162*84e872a0SLloyd Pique 	if (timeouts_enabled)
163*84e872a0SLloyd Pique 		alarm(0);
164*84e872a0SLloyd Pique 
165*84e872a0SLloyd Pique 	check_fd_leaks(cur_fds);
166*84e872a0SLloyd Pique 
167*84e872a0SLloyd Pique 	exit(EXIT_SUCCESS);
168*84e872a0SLloyd Pique }
169*84e872a0SLloyd Pique 
170*84e872a0SLloyd Pique #ifndef PATH_MAX
171*84e872a0SLloyd Pique #define PATH_MAX 256
172*84e872a0SLloyd Pique #endif
173*84e872a0SLloyd Pique 
174*84e872a0SLloyd Pique static void
set_xdg_runtime_dir(void)175*84e872a0SLloyd Pique set_xdg_runtime_dir(void)
176*84e872a0SLloyd Pique {
177*84e872a0SLloyd Pique 	char xdg_runtime_dir[PATH_MAX];
178*84e872a0SLloyd Pique 	const char *xrd_env;
179*84e872a0SLloyd Pique 
180*84e872a0SLloyd Pique 	xrd_env = getenv("XDG_RUNTIME_DIR");
181*84e872a0SLloyd Pique 	/* if XDG_RUNTIME_DIR is not set in environ, fallback to /tmp */
182*84e872a0SLloyd Pique 	assert((snprintf(xdg_runtime_dir, PATH_MAX, "%s/wayland-tests-XXXXXX",
183*84e872a0SLloyd Pique 			 (xrd_env && xrd_env[0] == '/') ? xrd_env : "/tmp") < PATH_MAX)
184*84e872a0SLloyd Pique 		&& "test error: XDG_RUNTIME_DIR too long");
185*84e872a0SLloyd Pique 
186*84e872a0SLloyd Pique 	assert(mkdtemp(xdg_runtime_dir) && "test error: mkdtemp failed");
187*84e872a0SLloyd Pique 	if (mkdir(xdg_runtime_dir, 0700) == -1)
188*84e872a0SLloyd Pique 		if (errno != EEXIST) {
189*84e872a0SLloyd Pique 			perror("Creating XDG_RUNTIME_DIR");
190*84e872a0SLloyd Pique 			abort();
191*84e872a0SLloyd Pique 		}
192*84e872a0SLloyd Pique 
193*84e872a0SLloyd Pique 	if (setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1) == -1) {
194*84e872a0SLloyd Pique 		perror("Setting XDG_RUNTIME_DIR");
195*84e872a0SLloyd Pique 		abort();
196*84e872a0SLloyd Pique 	}
197*84e872a0SLloyd Pique }
198*84e872a0SLloyd Pique 
199*84e872a0SLloyd Pique static void
rmdir_xdg_runtime_dir(void)200*84e872a0SLloyd Pique rmdir_xdg_runtime_dir(void)
201*84e872a0SLloyd Pique {
202*84e872a0SLloyd Pique 	const char *xrd_env = getenv("XDG_RUNTIME_DIR");
203*84e872a0SLloyd Pique 	assert(xrd_env && xrd_env[0] == '/' && "No XDG_RUNTIME_DIR set");
204*84e872a0SLloyd Pique 
205*84e872a0SLloyd Pique 	/* rmdir may fail if some test didn't do clean up */
206*84e872a0SLloyd Pique 	if (rmdir(xrd_env) == -1)
207*84e872a0SLloyd Pique 		perror("Cleaning XDG_RUNTIME_DIR");
208*84e872a0SLloyd Pique }
209*84e872a0SLloyd Pique 
210*84e872a0SLloyd Pique #define RED	"\033[31m"
211*84e872a0SLloyd Pique #define GREEN	"\033[32m"
212*84e872a0SLloyd Pique 
213*84e872a0SLloyd Pique static void
stderr_set_color(const char * color)214*84e872a0SLloyd Pique stderr_set_color(const char *color)
215*84e872a0SLloyd Pique {
216*84e872a0SLloyd Pique 	/* use colors only when the output is connected to
217*84e872a0SLloyd Pique 	 * the terminal */
218*84e872a0SLloyd Pique 	if (is_atty)
219*84e872a0SLloyd Pique 		fprintf(stderr, "%s", color);
220*84e872a0SLloyd Pique }
221*84e872a0SLloyd Pique 
222*84e872a0SLloyd Pique static void
stderr_reset_color(void)223*84e872a0SLloyd Pique stderr_reset_color(void)
224*84e872a0SLloyd Pique {
225*84e872a0SLloyd Pique 	if (is_atty)
226*84e872a0SLloyd Pique 		fprintf(stderr, "\033[0m");
227*84e872a0SLloyd Pique }
228*84e872a0SLloyd Pique 
229*84e872a0SLloyd Pique /* this function is taken from libinput/test/litest.c
230*84e872a0SLloyd Pique  * (rev 028513a0a723e97941c39)
231*84e872a0SLloyd Pique  *
232*84e872a0SLloyd Pique  * Returns: 1 if a debugger is confirmed present; 0 if no debugger is
233*84e872a0SLloyd Pique  * present or if it can't be determined.
234*84e872a0SLloyd Pique  */
235*84e872a0SLloyd Pique #if defined(HAVE_SYS_PROCCTL_H) && defined(PROC_TRACE_STATUS)
236*84e872a0SLloyd Pique static int
is_debugger_attached(void)237*84e872a0SLloyd Pique is_debugger_attached(void)
238*84e872a0SLloyd Pique {
239*84e872a0SLloyd Pique 	int rc;
240*84e872a0SLloyd Pique 	int status;
241*84e872a0SLloyd Pique 	rc = procctl(P_PID, getpid(), PROC_TRACE_STATUS, &status);
242*84e872a0SLloyd Pique 	if (rc == -1) {
243*84e872a0SLloyd Pique 		perror("procctl");
244*84e872a0SLloyd Pique 		return 0;
245*84e872a0SLloyd Pique 	}
246*84e872a0SLloyd Pique 	/* -1=tracing disabled, 0=no debugger attached, >0=pid of debugger. */
247*84e872a0SLloyd Pique 	return status > 0;
248*84e872a0SLloyd Pique }
249*84e872a0SLloyd Pique #elif defined(HAVE_SYS_PRCTL_H)
250*84e872a0SLloyd Pique static int
is_debugger_attached(void)251*84e872a0SLloyd Pique is_debugger_attached(void)
252*84e872a0SLloyd Pique {
253*84e872a0SLloyd Pique 	int status;
254*84e872a0SLloyd Pique 	int rc;
255*84e872a0SLloyd Pique 	pid_t pid;
256*84e872a0SLloyd Pique 	int pipefd[2];
257*84e872a0SLloyd Pique 
258*84e872a0SLloyd Pique 	if (pipe(pipefd) == -1) {
259*84e872a0SLloyd Pique 		perror("pipe");
260*84e872a0SLloyd Pique 		return 0;
261*84e872a0SLloyd Pique 	}
262*84e872a0SLloyd Pique 
263*84e872a0SLloyd Pique 	pid = fork();
264*84e872a0SLloyd Pique 	if (pid == -1) {
265*84e872a0SLloyd Pique 		perror("fork");
266*84e872a0SLloyd Pique 		close(pipefd[0]);
267*84e872a0SLloyd Pique 		close(pipefd[1]);
268*84e872a0SLloyd Pique 		return 0;
269*84e872a0SLloyd Pique 	} else if (pid == 0) {
270*84e872a0SLloyd Pique 		char buf;
271*84e872a0SLloyd Pique 		pid_t ppid = getppid();
272*84e872a0SLloyd Pique 
273*84e872a0SLloyd Pique 		/* Wait until parent is ready */
274*84e872a0SLloyd Pique 		close(pipefd[1]);  /* Close unused write end */
275*84e872a0SLloyd Pique 		read(pipefd[0], &buf, 1);
276*84e872a0SLloyd Pique 		close(pipefd[0]);
277*84e872a0SLloyd Pique 		if (buf == '-')
278*84e872a0SLloyd Pique 			_exit(1);
279*84e872a0SLloyd Pique 		if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) != 0)
280*84e872a0SLloyd Pique 			_exit(1);
281*84e872a0SLloyd Pique 		if (!waitpid(-1, NULL, 0))
282*84e872a0SLloyd Pique 			_exit(1);
283*84e872a0SLloyd Pique 		ptrace(PTRACE_CONT, NULL, NULL);
284*84e872a0SLloyd Pique 		ptrace(PTRACE_DETACH, ppid, NULL, NULL);
285*84e872a0SLloyd Pique 		_exit(0);
286*84e872a0SLloyd Pique 	} else {
287*84e872a0SLloyd Pique 		close(pipefd[0]);
288*84e872a0SLloyd Pique 
289*84e872a0SLloyd Pique 		/* Enable child to ptrace the parent process */
290*84e872a0SLloyd Pique 		rc = prctl(PR_SET_PTRACER, pid);
291*84e872a0SLloyd Pique 		if (rc != 0 && errno != EINVAL) {
292*84e872a0SLloyd Pique 			/* An error prevents us from telling if a debugger is attached.
293*84e872a0SLloyd Pique 			 * Instead of propagating the error, assume no debugger present.
294*84e872a0SLloyd Pique 			 * But note the error to the log as a clue for troubleshooting.
295*84e872a0SLloyd Pique 			 * Then flag the error state to the client by sending '-'.
296*84e872a0SLloyd Pique 			 */
297*84e872a0SLloyd Pique 			perror("prctl");
298*84e872a0SLloyd Pique 			write(pipefd[1], "-", 1);
299*84e872a0SLloyd Pique 		} else {
300*84e872a0SLloyd Pique 			/* Signal to client that parent is ready by passing '+' */
301*84e872a0SLloyd Pique 			write(pipefd[1], "+", 1);
302*84e872a0SLloyd Pique 		}
303*84e872a0SLloyd Pique 		close(pipefd[1]);
304*84e872a0SLloyd Pique 
305*84e872a0SLloyd Pique 		waitpid(pid, &status, 0);
306*84e872a0SLloyd Pique 		rc = WEXITSTATUS(status);
307*84e872a0SLloyd Pique 	}
308*84e872a0SLloyd Pique 
309*84e872a0SLloyd Pique 	return rc;
310*84e872a0SLloyd Pique }
311*84e872a0SLloyd Pique #endif
312*84e872a0SLloyd Pique 
main(int argc,char * argv[])313*84e872a0SLloyd Pique int main(int argc, char *argv[])
314*84e872a0SLloyd Pique {
315*84e872a0SLloyd Pique 	const struct test *t;
316*84e872a0SLloyd Pique 	pid_t pid;
317*84e872a0SLloyd Pique 	int total, pass;
318*84e872a0SLloyd Pique 	siginfo_t info;
319*84e872a0SLloyd Pique 
320*84e872a0SLloyd Pique 	if (isatty(fileno(stderr)))
321*84e872a0SLloyd Pique 		is_atty = 1;
322*84e872a0SLloyd Pique 
323*84e872a0SLloyd Pique 	if (is_debugger_attached()) {
324*84e872a0SLloyd Pique 		fd_leak_check_enabled = 0;
325*84e872a0SLloyd Pique 		timeouts_enabled = 0;
326*84e872a0SLloyd Pique 	} else {
327*84e872a0SLloyd Pique 		fd_leak_check_enabled = !getenv("WAYLAND_TEST_NO_LEAK_CHECK");
328*84e872a0SLloyd Pique 		timeouts_enabled = !getenv("WAYLAND_TEST_NO_TIMEOUTS");
329*84e872a0SLloyd Pique 	}
330*84e872a0SLloyd Pique 
331*84e872a0SLloyd Pique 	if (argc == 2 && strcmp(argv[1], "--help") == 0)
332*84e872a0SLloyd Pique 		usage(argv[0], EXIT_SUCCESS);
333*84e872a0SLloyd Pique 
334*84e872a0SLloyd Pique 	if (argc == 2) {
335*84e872a0SLloyd Pique 		t = find_test(argv[1]);
336*84e872a0SLloyd Pique 		if (t == NULL) {
337*84e872a0SLloyd Pique 			fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
338*84e872a0SLloyd Pique 			usage(argv[0], EXIT_FAILURE);
339*84e872a0SLloyd Pique 		}
340*84e872a0SLloyd Pique 
341*84e872a0SLloyd Pique 		set_xdg_runtime_dir();
342*84e872a0SLloyd Pique 		/* run_test calls exit() */
343*84e872a0SLloyd Pique 		assert(atexit(rmdir_xdg_runtime_dir) == 0);
344*84e872a0SLloyd Pique 
345*84e872a0SLloyd Pique 		run_test(t);
346*84e872a0SLloyd Pique 	}
347*84e872a0SLloyd Pique 
348*84e872a0SLloyd Pique 	/* set our own XDG_RUNTIME_DIR */
349*84e872a0SLloyd Pique 	set_xdg_runtime_dir();
350*84e872a0SLloyd Pique 
351*84e872a0SLloyd Pique 	pass = 0;
352*84e872a0SLloyd Pique 	for (t = &__start_test_section; t < &__stop_test_section; t++) {
353*84e872a0SLloyd Pique 		int success = 0;
354*84e872a0SLloyd Pique 
355*84e872a0SLloyd Pique 		pid = fork();
356*84e872a0SLloyd Pique 		assert(pid >= 0);
357*84e872a0SLloyd Pique 
358*84e872a0SLloyd Pique 		if (pid == 0)
359*84e872a0SLloyd Pique 			run_test(t); /* never returns */
360*84e872a0SLloyd Pique 
361*84e872a0SLloyd Pique 		if (waitid(P_PID, pid, &info, WEXITED)) {
362*84e872a0SLloyd Pique 			stderr_set_color(RED);
363*84e872a0SLloyd Pique 			fprintf(stderr, "waitid failed: %s\n",
364*84e872a0SLloyd Pique 				strerror(errno));
365*84e872a0SLloyd Pique 			stderr_reset_color();
366*84e872a0SLloyd Pique 
367*84e872a0SLloyd Pique 			abort();
368*84e872a0SLloyd Pique 		}
369*84e872a0SLloyd Pique 
370*84e872a0SLloyd Pique 		switch (info.si_code) {
371*84e872a0SLloyd Pique 		case CLD_EXITED:
372*84e872a0SLloyd Pique 			if (info.si_status == EXIT_SUCCESS)
373*84e872a0SLloyd Pique 				success = !t->must_fail;
374*84e872a0SLloyd Pique 			else
375*84e872a0SLloyd Pique 				success = t->must_fail;
376*84e872a0SLloyd Pique 
377*84e872a0SLloyd Pique 			stderr_set_color(success ? GREEN : RED);
378*84e872a0SLloyd Pique 			fprintf(stderr, "test \"%s\":\texit status %d",
379*84e872a0SLloyd Pique 				t->name, info.si_status);
380*84e872a0SLloyd Pique 
381*84e872a0SLloyd Pique 			break;
382*84e872a0SLloyd Pique 		case CLD_KILLED:
383*84e872a0SLloyd Pique 		case CLD_DUMPED:
384*84e872a0SLloyd Pique 			if (t->must_fail)
385*84e872a0SLloyd Pique 				success = 1;
386*84e872a0SLloyd Pique 
387*84e872a0SLloyd Pique 			stderr_set_color(success ? GREEN : RED);
388*84e872a0SLloyd Pique 			fprintf(stderr, "test \"%s\":\tsignal %d",
389*84e872a0SLloyd Pique 				t->name, info.si_status);
390*84e872a0SLloyd Pique 
391*84e872a0SLloyd Pique 			break;
392*84e872a0SLloyd Pique 		}
393*84e872a0SLloyd Pique 
394*84e872a0SLloyd Pique 		if (success) {
395*84e872a0SLloyd Pique 			pass++;
396*84e872a0SLloyd Pique 			fprintf(stderr, ", pass.\n");
397*84e872a0SLloyd Pique 		} else
398*84e872a0SLloyd Pique 			fprintf(stderr, ", fail.\n");
399*84e872a0SLloyd Pique 
400*84e872a0SLloyd Pique 		stderr_reset_color();
401*84e872a0SLloyd Pique 
402*84e872a0SLloyd Pique 		/* print separator line */
403*84e872a0SLloyd Pique 		fprintf(stderr, "----------------------------------------\n");
404*84e872a0SLloyd Pique 	}
405*84e872a0SLloyd Pique 
406*84e872a0SLloyd Pique 	total = &__stop_test_section - &__start_test_section;
407*84e872a0SLloyd Pique 	fprintf(stderr, "%d tests, %d pass, %d fail\n",
408*84e872a0SLloyd Pique 		total, pass, total - pass);
409*84e872a0SLloyd Pique 
410*84e872a0SLloyd Pique 	/* cleaning */
411*84e872a0SLloyd Pique 	rmdir_xdg_runtime_dir();
412*84e872a0SLloyd Pique 
413*84e872a0SLloyd Pique 	return pass == total ? EXIT_SUCCESS : EXIT_FAILURE;
414*84e872a0SLloyd Pique }
415