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 <unistd.h>
33 #include <time.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36
37 #ifdef HAVE_SYS_PRCTL_H
38 #include <sys/prctl.h>
39 #endif
40
41 #include "test-runner.h"
42
43 int
count_open_fds(void)44 count_open_fds(void)
45 {
46 DIR *dir;
47 struct dirent *ent;
48 int count = 0;
49
50 dir = opendir("/proc/self/fd");
51 assert(dir && "opening /proc/self/fd failed.");
52
53 errno = 0;
54 while ((ent = readdir(dir))) {
55 const char *s = ent->d_name;
56 if (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0)))
57 continue;
58 count++;
59 }
60 assert(errno == 0 && "reading /proc/self/fd failed.");
61
62 closedir(dir);
63
64 return count;
65 }
66
67 void
exec_fd_leak_check(int nr_expected_fds)68 exec_fd_leak_check(int nr_expected_fds)
69 {
70 const char *exe = "./exec-fd-leak-checker";
71 char number[16] = { 0 };
72
73 snprintf(number, sizeof number - 1, "%d", nr_expected_fds);
74 execl(exe, exe, number, (char *)NULL);
75 assert(0 && "execing fd leak checker failed");
76 }
77
78 #define USEC_TO_NSEC(n) (1000 * (n))
79
80 /* our implementation of usleep and sleep functions that are safe to use with
81 * timeouts (timeouts are implemented using alarm(), so it is not safe use
82 * usleep and sleep. See man pages of these functions)
83 */
84 void
test_usleep(useconds_t usec)85 test_usleep(useconds_t usec)
86 {
87 struct timespec ts = {
88 .tv_sec = 0,
89 .tv_nsec = USEC_TO_NSEC(usec)
90 };
91
92 assert(nanosleep(&ts, NULL) == 0);
93 }
94
95 /* we must write the whole function instead of
96 * wrapping test_usleep, because useconds_t may not
97 * be able to contain such a big number of microseconds */
98 void
test_sleep(unsigned int sec)99 test_sleep(unsigned int sec)
100 {
101 struct timespec ts = {
102 .tv_sec = sec,
103 .tv_nsec = 0
104 };
105
106 assert(nanosleep(&ts, NULL) == 0);
107 }
108
109 /** Try to disable coredumps
110 *
111 * Useful for tests that crash on purpose, to avoid creating a core file
112 * or launching an application crash handler service or cluttering coredumpctl.
113 *
114 * NOTE: Calling this may make the process undebuggable.
115 */
116 void
test_disable_coredumps(void)117 test_disable_coredumps(void)
118 {
119 struct rlimit r;
120
121 if (getrlimit(RLIMIT_CORE, &r) == 0) {
122 r.rlim_cur = 0;
123 setrlimit(RLIMIT_CORE, &r);
124 }
125
126 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
127 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
128 #endif
129 }
130