1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <syscall.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 
mlock2_(void * start,size_t len,int flags)7 static int mlock2_(void *start, size_t len, int flags)
8 {
9 #ifdef __NR_mlock2
10 	return syscall(__NR_mlock2, start, len, flags);
11 #else
12 	errno = ENOSYS;
13 	return -1;
14 #endif
15 }
16 
seek_to_smaps_entry(unsigned long addr)17 static FILE *seek_to_smaps_entry(unsigned long addr)
18 {
19 	FILE *file;
20 	char *line = NULL;
21 	size_t size = 0;
22 	unsigned long start, end;
23 	char perms[5];
24 	unsigned long offset;
25 	char dev[32];
26 	unsigned long inode;
27 	char path[BUFSIZ];
28 
29 	file = fopen("/proc/self/smaps", "r");
30 	if (!file) {
31 		perror("fopen smaps");
32 		_exit(1);
33 	}
34 
35 	while (getline(&line, &size, file) > 0) {
36 		if (sscanf(line, "%lx-%lx %s %lx %s %lu %s\n",
37 			   &start, &end, perms, &offset, dev, &inode, path) < 6)
38 			goto next;
39 
40 		if (start <= addr && addr < end)
41 			goto out;
42 
43 next:
44 		free(line);
45 		line = NULL;
46 		size = 0;
47 	}
48 
49 	fclose(file);
50 	file = NULL;
51 
52 out:
53 	free(line);
54 	return file;
55 }
56