• 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 <assert.h>
27 #include <errno.h>
28 #include <dirent.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <time.h>
32 
33 #include "test-runner.h"
34 
35 int
count_open_fds(void)36 count_open_fds(void)
37 {
38 	DIR *dir;
39 	struct dirent *ent;
40 	int count = 0;
41 
42 	dir = opendir("/proc/self/fd");
43 	assert(dir && "opening /proc/self/fd failed.");
44 
45 	errno = 0;
46 	while ((ent = readdir(dir))) {
47 		const char *s = ent->d_name;
48 		if (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0)))
49 			continue;
50 		count++;
51 	}
52 	assert(errno == 0 && "reading /proc/self/fd failed.");
53 
54 	closedir(dir);
55 
56 	return count;
57 }
58 
59 void
exec_fd_leak_check(int nr_expected_fds)60 exec_fd_leak_check(int nr_expected_fds)
61 {
62 	const char *exe = "./exec-fd-leak-checker";
63 	char number[16] = { 0 };
64 
65 	snprintf(number, sizeof number - 1, "%d", nr_expected_fds);
66 	execl(exe, exe, number, (char *)NULL);
67 	assert(0 && "execing fd leak checker failed");
68 }
69 
70 #define USEC_TO_NSEC(n) (1000 * (n))
71 
72 /* our implementation of usleep and sleep functions that are safe to use with
73  * timeouts (timeouts are implemented using alarm(), so it is not safe use
74  * usleep and sleep. See man pages of these functions)
75  */
76 void
test_usleep(useconds_t usec)77 test_usleep(useconds_t usec)
78 {
79 	struct timespec ts = {
80 		.tv_sec = 0,
81 		.tv_nsec = USEC_TO_NSEC(usec)
82 	};
83 
84 	assert(nanosleep(&ts, NULL) == 0);
85 }
86 
87 /* we must write the whole function instead of
88  * wrapping test_usleep, because useconds_t may not
89  * be able to contain such a big number of microseconds */
90 void
test_sleep(unsigned int sec)91 test_sleep(unsigned int sec)
92 {
93 	struct timespec ts = {
94 		.tv_sec = sec,
95 		.tv_nsec = 0
96 	};
97 
98 	assert(nanosleep(&ts, NULL) == 0);
99 }
100