mlock2.h 960 B

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