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