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