• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include "config.h"
27 
28 #include <assert.h>
29 #include <errno.h>
30 #include <dirent.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <time.h>
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 
38 #ifdef HAVE_SYS_PRCTL_H
39 #include <sys/prctl.h>
40 #endif
41 
42 #include "test-runner.h"
43 
44 int
count_open_fds(void)45 count_open_fds(void)
46 {
47 	DIR *dir;
48 	struct dirent *ent;
49 	int count = 0;
50 
51 	dir = opendir("/proc/self/fd");
52 	assert(dir && "opening /proc/self/fd failed.");
53 
54 	errno = 0;
55 	while ((ent = readdir(dir))) {
56 		const char *s = ent->d_name;
57 		if (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0)))
58 			continue;
59 		count++;
60 	}
61 	assert(errno == 0 && "reading /proc/self/fd failed.");
62 
63 	closedir(dir);
64 
65 	return count;
66 }
67 
68 void
exec_fd_leak_check(int nr_expected_fds)69 exec_fd_leak_check(int nr_expected_fds)
70 {
71 	const char *exe = "exec-fd-leak-checker";
72 	char number[16] = { 0 };
73 	const char *test_build_dir = getenv("TEST_BUILD_DIR");
74 	char exe_path[256] = { 0 };
75 
76 	if (test_build_dir == NULL || test_build_dir[0] == 0) {
77 	        test_build_dir = ".";
78 	}
79 
80 	snprintf(exe_path, sizeof exe_path - 1, "%s/%s", test_build_dir, exe);
81 
82 	snprintf(number, sizeof number - 1, "%d", nr_expected_fds);
83 	execl(exe_path, exe, number, (char *)NULL);
84 	assert(0 && "execing fd leak checker failed");
85 }
86 
87 #define USEC_TO_NSEC(n) (1000 * (n))
88 
89 /* our implementation of usleep and sleep functions that are safe to use with
90  * timeouts (timeouts are implemented using alarm(), so it is not safe use
91  * usleep and sleep. See man pages of these functions)
92  */
93 void
test_usleep(useconds_t usec)94 test_usleep(useconds_t usec)
95 {
96 	struct timespec ts = {
97 		.tv_sec = 0,
98 		.tv_nsec = USEC_TO_NSEC(usec)
99 	};
100 
101 	assert(nanosleep(&ts, NULL) == 0);
102 }
103 
104 /* we must write the whole function instead of
105  * wrapping test_usleep, because useconds_t may not
106  * be able to contain such a big number of microseconds */
107 void
test_sleep(unsigned int sec)108 test_sleep(unsigned int sec)
109 {
110 	struct timespec ts = {
111 		.tv_sec = sec,
112 		.tv_nsec = 0
113 	};
114 
115 	assert(nanosleep(&ts, NULL) == 0);
116 }
117 
118 /** Try to disable coredumps
119  *
120  * Useful for tests that crash on purpose, to avoid creating a core file
121  * or launching an application crash handler service or cluttering coredumpctl.
122  *
123  * NOTE: Calling this may make the process undebuggable.
124  */
125 void
test_disable_coredumps(void)126 test_disable_coredumps(void)
127 {
128 	struct rlimit r;
129 
130 	if (getrlimit(RLIMIT_CORE, &r) == 0) {
131 		r.rlim_cur = 0;
132 		setrlimit(RLIMIT_CORE, &r);
133 	}
134 
135 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
136 	prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
137 #endif
138 }
139