0001
0002 #include <syscall.h>
0003 #include <errno.h>
0004 #include <stdio.h>
0005 #include <stdlib.h>
0006
0007 #ifndef MLOCK_ONFAULT
0008 #define MLOCK_ONFAULT 1
0009 #endif
0010
0011 #ifndef MCL_ONFAULT
0012 #define MCL_ONFAULT (MCL_FUTURE << 1)
0013 #endif
0014
0015 static int mlock2_(void *start, size_t len, int flags)
0016 {
0017 #ifdef __NR_mlock2
0018 return syscall(__NR_mlock2, start, len, flags);
0019 #else
0020 errno = ENOSYS;
0021 return -1;
0022 #endif
0023 }
0024
0025 static FILE *seek_to_smaps_entry(unsigned long addr)
0026 {
0027 FILE *file;
0028 char *line = NULL;
0029 size_t size = 0;
0030 unsigned long start, end;
0031 char perms[5];
0032 unsigned long offset;
0033 char dev[32];
0034 unsigned long inode;
0035 char path[BUFSIZ];
0036
0037 file = fopen("/proc/self/smaps", "r");
0038 if (!file) {
0039 perror("fopen smaps");
0040 _exit(1);
0041 }
0042
0043 while (getline(&line, &size, file) > 0) {
0044 if (sscanf(line, "%lx-%lx %s %lx %s %lu %s\n",
0045 &start, &end, perms, &offset, dev, &inode, path) < 6)
0046 goto next;
0047
0048 if (start <= addr && addr < end)
0049 goto out;
0050
0051 next:
0052 free(line);
0053 line = NULL;
0054 size = 0;
0055 }
0056
0057 fclose(file);
0058 file = NULL;
0059
0060 out:
0061 free(line);
0062 return file;
0063 }