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