Debian Codegraph @

tests/testutils.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #define _LARGEFILE64_SOURCE
21 #define _GNU_SOURCE
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/vfs.h>
33 #include <sys/ipc.h>
34 #include <sys/shm.h>
35 #include <sys/stat.h>
36 #include <sys/sysinfo.h>
37 #include <sys/types.h>
38 #include <sys/mman.h>
39 #include <fcntl.h>
40
41 #include "hugetlbfs.h"
42 #include "hugetests.h"
43
44 #define HUGETLBFS_MAGIC 0x958458f6
45 #define BUF_SZ 1024
46 #define MEMINFO_SZ 2048
47
48 int verbose_test = 1;
49 char *test_name;
50
51 void check_must_be_root(void)
52 {
53 uid_t uid = getuid();
54 if (uid != 0)
55 CONFIG("Must be root");
56 }
57
58 void check_hugetlb_shm_group(void)
59 {
60 int fd;
61 ssize_t ret;
62 char gid_buffer[64] = {0};
63 gid_t hugetlb_shm_group;
64 gid_t gid = getgid();
65 uid_t uid = getuid();
66
67 /* root is an exception */
68 if (uid == 0)
69 return;
70
71 fd = open("/proc/sys/vm/hugetlb_shm_group", O_RDONLY);
72 if (fd < 0)
73 ERROR("Unable to open /proc/sys/vm/hugetlb_shm_group: %s",
74 strerror(errno));
75 ret = read(fd, &gid_buffer, sizeof(gid_buffer));
76 if (ret < 0)
77 ERROR("Unable to read /proc/sys/vm/hugetlb_shm_group: %s",
78 strerror(errno));
79 hugetlb_shm_group = atoi(gid_buffer);
80 close(fd);
81 if (hugetlb_shm_group != gid)
82 CONFIG("Do not have permission to use SHM_HUGETLB");
83 }
84
85 #define SYSFS_CPU_ONLINE_FMT "/sys/devices/system/cpu/cpu%d/online"
86
87 void check_online_cpus(int online_cpus[], int nr_cpus_needed)
88 {
89 char cpu_state, path_buf[64];
90 int total_cpus, cpu_idx, fd, ret, i;
91
92 total_cpus = get_nprocs_conf();
93 cpu_idx = 0;
94
95 if (get_nprocs() < nr_cpus_needed)
96 CONFIG("Atleast online %d cpus are required", nr_cpus_needed);
97
98 for (i = 0; i < total_cpus && cpu_idx < nr_cpus_needed; i++) {
99 errno = 0;
100 sprintf(path_buf, SYSFS_CPU_ONLINE_FMT, i);
101 fd = open(path_buf, O_RDONLY);
102 if (fd < 0) {
103 /* If 'online' is absent, the cpu cannot be offlined */
104 if (errno == ENOENT) {
105 online_cpus[cpu_idx] = i;
106 cpu_idx++;
107 continue;
108 } else {
109 FAIL("Unable to open %s: %s", path_buf,
110 strerror(errno));
111 }
112 }
113
114 ret = read(fd, &cpu_state, 1);
115 if (ret < 1)
116 FAIL("Unable to read %s: %s", path_buf,
117 strerror(errno));
118
119 if (cpu_state == '1') {
120 online_cpus[cpu_idx] = i;
121 cpu_idx++;
122 }
123
124 close(fd);
125 }
126
127 if (cpu_idx < nr_cpus_needed)
128 CONFIG("Atleast %d online cpus were not found", nr_cpus_needed);
129 }
130
131 void __attribute__((weak)) cleanup(void)
132 {
133 }
134
135 #if 0
136 static void segv_handler(int signum, siginfo_t *si, void *uc)
137 {
138 FAIL("Segmentation fault");
139 }
140 #endif
141
142 static void sigint_handler(int signum, siginfo_t *si, void *uc)
143 {
144 cleanup();
145 fprintf(stderr, "%s: %s (pid=%d)\n", test_name,
146 strsignal(signum), getpid());
147 exit(RC_BUG);
148 }
149
150 void test_init(int argc, char *argv[])
151 {
152 int err;
153 struct sigaction sa_int = {
154 .sa_sigaction = sigint_handler,
155 };
156
157 test_name = argv[0];
158
159 err = sigaction(SIGINT, &sa_int, NULL);
160 if (err)
161 FAIL("Can't install SIGINT handler: %s", strerror(errno));
162
163 if (getenv("QUIET_TEST"))
164 verbose_test = 0;
165
166 verbose_printf("Starting testcase \"%s\", pid %d\n",
167 test_name, getpid());
168 }
169
170 #define MAPS_BUF_SZ 4096
171
172 static int read_maps(unsigned long addr, char *buf)
173 {
174 FILE *f;
175 char line[MAPS_BUF_SZ];
176 char *tmp;
177
178 f = fopen("/proc/self/maps", "r");
179 if (!f) {
180 ERROR("Failed to open /proc/self/maps: %s\n", strerror(errno));
181 return -1;
182 }
183
184 while (1) {
185 unsigned long start, end, off, ino;
186 int ret;
187
188 tmp = fgets(line, MAPS_BUF_SZ, f);
189 if (!tmp)
190 break;
191
192 buf[0] = '\0';
193 ret = sscanf(line, "%lx-%lx %*s %lx %*s %ld %255s",
194 &start, &end, &off, &ino,
195 buf);
196 if ((ret < 4) || (ret > 5)) {
197 ERROR("Couldn't parse /proc/self/maps line: %s\n",
198 line);
199 fclose(f);
200 return -1;
201 }
202
203 if ((start <= addr) && (addr < end)) {
204 fclose(f);
205 return 1;
206 }
207 }
208
209 fclose(f);
210 return 0;
211 }
212
213 int range_is_mapped(unsigned long low, unsigned long high)
214 {
215 FILE *f;
216 char line[MAPS_BUF_SZ];
217 char *tmp;
218
219 f = fopen("/proc/self/maps", "r");
220 if (!f) {
221 ERROR("Failed to open /proc/self/maps: %s\n", strerror(errno));
222 return -1;
223 }
224
225 while (1) {
226 unsigned long start, end;
227 int ret;
228
229 tmp = fgets(line, MAPS_BUF_SZ, f);
230 if (!tmp)
231 break;
232
233 ret = sscanf(line, "%lx-%lx", &start, &end);
234 if (ret != 2) {
235 ERROR("Couldn't parse /proc/self/maps line: %s\n",
236 line);
237 fclose(f);
238 return -1;
239 }
240
241 if ((start >= low) && (start < high)) {
242 fclose(f);
243 return 1;
244 }
245 if ((end >= low) && (end < high)) {
246 fclose(f);
247 return 1;
248 }
249
250 }
251
252 fclose(f);
253 return 0;
254 }
255
256 /*
257 * With the inclusion of MAP_HUGETLB it is now possible to have huge pages
258 * without using hugetlbfs, so not all huge page regions will show with the
259 * test that reads /proc/self/maps. Instead we ask /proc/self/smaps for
260 * the KernelPageSize. On success we return the page size (in bytes) for the
261 * mapping that contains addr, on failure we return 0
262 */
263 unsigned long long get_mapping_page_size(void *p)
264 {
265 FILE *f;
266 char line[MAPS_BUF_SZ];
267 char *tmp;
268 unsigned long addr = (unsigned long)p;
269
270 f = fopen("/proc/self/smaps", "r");
271 if (!f) {
272 ERROR("Unable to open /proc/self/smaps\n");
273 return 0;
274 }
275
276 while ((tmp = fgets(line, MAPS_BUF_SZ, f))) {
277 unsigned long start, end, dummy;
278 char map_name[256];
279 char buf[64];
280 int ret;
281
282 ret = sscanf(line, "%lx-%lx %s %lx %s %ld %s", &start, &end,
283 buf, &dummy, buf, &dummy, map_name);
284 if (ret < 7 || start > addr || end <= addr)
285 continue;
286
287 while ((tmp = fgets(line, MAPS_BUF_SZ, f))) {
288 unsigned long long page_size;
289
290 ret = sscanf(line, "KernelPageSize: %lld kB",
291 &page_size);
292 if (ret == 0 )
293 continue;
294 if (ret < 1 || page_size <= 0) {
295 ERROR("Cannot parse /proc/self/smaps\n");
296 page_size = 0;
297 }
298
299 fclose(f);
300 /* page_size is reported in kB, we return B */
301 return page_size * 1024;
302 }
303 }
304
305 /* We couldn't find an entry for this addr in smaps */
306 fclose(f);
307 return 0;
308 }
309
310 /* We define this function standalone, rather than in terms of
311 * hugetlbfs_test_path() so that we can use it without -lhugetlbfs for
312 * testing PRELOAD */
313 int test_addr_huge(void *p)
314 {
315 char name[256];
316 char *dirend;
317 int ret;
318 struct statfs64 sb;
319
320 ret = read_maps((unsigned long)p, name);
321 if (ret < 0)
322 return ret;
323 if (ret == 0) {
324 verbose_printf("Couldn't find address %p in /proc/self/maps\n",
325 p);
326 return -1;
327 }
328
329 /* looks like a filename? */
330 if (name[0] != '/')
331 return 0;
332
333 /* Truncate the filename portion */
334
335 dirend = strrchr(name, '/');
336 if (dirend && dirend > name) {
337 *dirend = '\0';
338 }
339
340 ret = statfs64(name, &sb);
341 if (ret)
342 return -1;
343
344 return (sb.f_type == HUGETLBFS_MAGIC);
345 }
346
347 ino_t get_addr_inode(void *p)
348 {
349 char name[256];
350 int ret;
351 struct stat sb;
352
353 ret = read_maps((unsigned long)p, name);
354 if (ret < 0)
355 return ret;
356 if (ret == 0) {
357 ERROR("Couldn't find address %p in /proc/self/maps\n", p);
358 return -1;
359 }
360
361 /* Don't care about non-filenames */
362 if (name[0] != '/')
363 return 0;
364
365 /* Truncate the filename portion */
366
367 ret = stat(name, &sb);
368 if (ret < 0) {
369 /* Don't care about unlinked files */
370 if (errno == ENOENT)
371 return 0;
372 ERROR("stat failed: %s\n", strerror(errno));
373 return -1;
374 }
375
376 return sb.st_ino;
377 }
378
379 int remove_shmid(int shmid)
380 {
381 if (shmid >= 0) {
382 if (shmctl(shmid, IPC_RMID, NULL) != 0) {
383 ERROR("shmctl(%x, IPC_RMID) failed (%s)\n",
384 shmid, strerror(errno));
385 return -1;
386 }
387 }
388 return 0;
389 }