mlock2.h 902 B

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