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 int ret = syscall(__NR_mlock2, start, len, flags); 10 11 if (ret) { 12 errno = ret; 13 return -1; 14 } 15 return 0; 16 } 17 18 static FILE *seek_to_smaps_entry(unsigned long addr) 19 { 20 FILE *file; 21 char *line = NULL; 22 size_t size = 0; 23 unsigned long start, end; 24 char perms[5]; 25 unsigned long offset; 26 char dev[32]; 27 unsigned long inode; 28 char path[BUFSIZ]; 29 30 file = fopen("/proc/self/smaps", "r"); 31 if (!file) 32 ksft_exit_fail_msg("fopen smaps: %s\n", strerror(errno)); 33 34 while (getline(&line, &size, file) > 0) { 35 if (sscanf(line, "%lx-%lx %s %lx %s %lu %s\n", 36 &start, &end, perms, &offset, dev, &inode, path) < 6) 37 goto next; 38 39 if (start <= addr && addr < end) 40 goto out; 41 42 next: 43 free(line); 44 line = NULL; 45 size = 0; 46 } 47 48 fclose(file); 49 file = NULL; 50 51 out: 52 free(line); 53 return file; 54 } 55