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