xref: /aosp_15_r20/external/wayland/tests/test-helpers.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique  * Copyright © 2012 Collabora, Ltd.
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 
26*84e872a0SLloyd Pique #include "config.h"
27*84e872a0SLloyd Pique 
28*84e872a0SLloyd Pique #include <assert.h>
29*84e872a0SLloyd Pique #include <errno.h>
30*84e872a0SLloyd Pique #include <dirent.h>
31*84e872a0SLloyd Pique #include <stdio.h>
32*84e872a0SLloyd Pique #include <stdlib.h>
33*84e872a0SLloyd Pique #include <unistd.h>
34*84e872a0SLloyd Pique #include <time.h>
35*84e872a0SLloyd Pique #include <sys/time.h>
36*84e872a0SLloyd Pique #include <sys/resource.h>
37*84e872a0SLloyd Pique 
38*84e872a0SLloyd Pique #ifdef HAVE_SYS_PRCTL_H
39*84e872a0SLloyd Pique #include <sys/prctl.h>
40*84e872a0SLloyd Pique #endif
41*84e872a0SLloyd Pique 
42*84e872a0SLloyd Pique #include "test-runner.h"
43*84e872a0SLloyd Pique 
44*84e872a0SLloyd Pique #if defined(__FreeBSD__)
45*84e872a0SLloyd Pique #include <sys/sysctl.h>
46*84e872a0SLloyd Pique 
47*84e872a0SLloyd Pique /*
48*84e872a0SLloyd Pique  * On FreeBSD, get file descriptor information using sysctl() since that does
49*84e872a0SLloyd Pique  * not depend on a mounted fdescfs (which provides /dev/fd/N for N > 2).
50*84e872a0SLloyd Pique  */
51*84e872a0SLloyd Pique int
count_open_fds(void)52*84e872a0SLloyd Pique count_open_fds(void)
53*84e872a0SLloyd Pique {
54*84e872a0SLloyd Pique 	int error;
55*84e872a0SLloyd Pique 	int nfds;
56*84e872a0SLloyd Pique 	int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_NFDS, 0 };
57*84e872a0SLloyd Pique 	size_t len;
58*84e872a0SLloyd Pique 
59*84e872a0SLloyd Pique 	len = sizeof(nfds);
60*84e872a0SLloyd Pique 	error = sysctl(mib, 4, &nfds, &len, NULL, 0);
61*84e872a0SLloyd Pique 	assert(error == 0 && "sysctl KERN_PROC_NFDS failed.");
62*84e872a0SLloyd Pique 	return nfds;
63*84e872a0SLloyd Pique }
64*84e872a0SLloyd Pique #else
65*84e872a0SLloyd Pique int
count_open_fds(void)66*84e872a0SLloyd Pique count_open_fds(void)
67*84e872a0SLloyd Pique {
68*84e872a0SLloyd Pique 	DIR *dir;
69*84e872a0SLloyd Pique 	struct dirent *ent;
70*84e872a0SLloyd Pique 	int count = 0;
71*84e872a0SLloyd Pique 
72*84e872a0SLloyd Pique 	/*
73*84e872a0SLloyd Pique 	 * Using /dev/fd instead of /proc/self/fd should allow this code to
74*84e872a0SLloyd Pique 	 * work on non-Linux operating systems.
75*84e872a0SLloyd Pique 	 */
76*84e872a0SLloyd Pique 	dir = opendir("/dev/fd");
77*84e872a0SLloyd Pique 	assert(dir && "opening /dev/fd failed.");
78*84e872a0SLloyd Pique 
79*84e872a0SLloyd Pique 	errno = 0;
80*84e872a0SLloyd Pique 	while ((ent = readdir(dir))) {
81*84e872a0SLloyd Pique 		const char *s = ent->d_name;
82*84e872a0SLloyd Pique 		if (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0)))
83*84e872a0SLloyd Pique 			continue;
84*84e872a0SLloyd Pique 		count++;
85*84e872a0SLloyd Pique 	}
86*84e872a0SLloyd Pique 	assert(errno == 0 && "reading /dev/fd failed.");
87*84e872a0SLloyd Pique 
88*84e872a0SLloyd Pique 	closedir(dir);
89*84e872a0SLloyd Pique 
90*84e872a0SLloyd Pique 	return count;
91*84e872a0SLloyd Pique }
92*84e872a0SLloyd Pique #endif
93*84e872a0SLloyd Pique 
94*84e872a0SLloyd Pique void
exec_fd_leak_check(int nr_expected_fds)95*84e872a0SLloyd Pique exec_fd_leak_check(int nr_expected_fds)
96*84e872a0SLloyd Pique {
97*84e872a0SLloyd Pique 	const char *exe = "exec-fd-leak-checker";
98*84e872a0SLloyd Pique 	char number[16] = { 0 };
99*84e872a0SLloyd Pique 	const char *test_build_dir = getenv("TEST_BUILD_DIR");
100*84e872a0SLloyd Pique 	char exe_path[256] = { 0 };
101*84e872a0SLloyd Pique 
102*84e872a0SLloyd Pique 	if (test_build_dir == NULL || test_build_dir[0] == 0) {
103*84e872a0SLloyd Pique 	        test_build_dir = ".";
104*84e872a0SLloyd Pique 	}
105*84e872a0SLloyd Pique 
106*84e872a0SLloyd Pique 	snprintf(exe_path, sizeof exe_path - 1, "%s/%s", test_build_dir, exe);
107*84e872a0SLloyd Pique 
108*84e872a0SLloyd Pique 	snprintf(number, sizeof number - 1, "%d", nr_expected_fds);
109*84e872a0SLloyd Pique 	execl(exe_path, exe, number, (char *)NULL);
110*84e872a0SLloyd Pique 	assert(0 && "execing fd leak checker failed");
111*84e872a0SLloyd Pique }
112*84e872a0SLloyd Pique 
113*84e872a0SLloyd Pique #define USEC_TO_NSEC(n) (1000 * (n))
114*84e872a0SLloyd Pique 
115*84e872a0SLloyd Pique /* our implementation of usleep and sleep functions that are safe to use with
116*84e872a0SLloyd Pique  * timeouts (timeouts are implemented using alarm(), so it is not safe use
117*84e872a0SLloyd Pique  * usleep and sleep. See man pages of these functions)
118*84e872a0SLloyd Pique  */
119*84e872a0SLloyd Pique void
test_usleep(useconds_t usec)120*84e872a0SLloyd Pique test_usleep(useconds_t usec)
121*84e872a0SLloyd Pique {
122*84e872a0SLloyd Pique 	struct timespec ts = {
123*84e872a0SLloyd Pique 		.tv_sec = 0,
124*84e872a0SLloyd Pique 		.tv_nsec = USEC_TO_NSEC(usec)
125*84e872a0SLloyd Pique 	};
126*84e872a0SLloyd Pique 
127*84e872a0SLloyd Pique 	assert(nanosleep(&ts, NULL) == 0);
128*84e872a0SLloyd Pique }
129*84e872a0SLloyd Pique 
130*84e872a0SLloyd Pique /* we must write the whole function instead of
131*84e872a0SLloyd Pique  * wrapping test_usleep, because useconds_t may not
132*84e872a0SLloyd Pique  * be able to contain such a big number of microseconds */
133*84e872a0SLloyd Pique void
test_sleep(unsigned int sec)134*84e872a0SLloyd Pique test_sleep(unsigned int sec)
135*84e872a0SLloyd Pique {
136*84e872a0SLloyd Pique 	struct timespec ts = {
137*84e872a0SLloyd Pique 		.tv_sec = sec,
138*84e872a0SLloyd Pique 		.tv_nsec = 0
139*84e872a0SLloyd Pique 	};
140*84e872a0SLloyd Pique 
141*84e872a0SLloyd Pique 	assert(nanosleep(&ts, NULL) == 0);
142*84e872a0SLloyd Pique }
143*84e872a0SLloyd Pique 
144*84e872a0SLloyd Pique /** Try to disable coredumps
145*84e872a0SLloyd Pique  *
146*84e872a0SLloyd Pique  * Useful for tests that crash on purpose, to avoid creating a core file
147*84e872a0SLloyd Pique  * or launching an application crash handler service or cluttering coredumpctl.
148*84e872a0SLloyd Pique  *
149*84e872a0SLloyd Pique  * NOTE: Calling this may make the process undebuggable.
150*84e872a0SLloyd Pique  */
151*84e872a0SLloyd Pique void
test_disable_coredumps(void)152*84e872a0SLloyd Pique test_disable_coredumps(void)
153*84e872a0SLloyd Pique {
154*84e872a0SLloyd Pique 	struct rlimit r;
155*84e872a0SLloyd Pique 
156*84e872a0SLloyd Pique 	if (getrlimit(RLIMIT_CORE, &r) == 0) {
157*84e872a0SLloyd Pique 		r.rlim_cur = 0;
158*84e872a0SLloyd Pique 		setrlimit(RLIMIT_CORE, &r);
159*84e872a0SLloyd Pique 	}
160*84e872a0SLloyd Pique 
161*84e872a0SLloyd Pique #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
162*84e872a0SLloyd Pique 	prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
163*84e872a0SLloyd Pique #endif
164*84e872a0SLloyd Pique }
165