proc.h 846 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #undef NDEBUG
  3. #include <assert.h>
  4. #include <dirent.h>
  5. #include <errno.h>
  6. #include <stdbool.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <sys/syscall.h>
  11. static inline pid_t sys_getpid(void)
  12. {
  13. return syscall(SYS_getpid);
  14. }
  15. static inline pid_t sys_gettid(void)
  16. {
  17. return syscall(SYS_gettid);
  18. }
  19. static inline bool streq(const char *s1, const char *s2)
  20. {
  21. return strcmp(s1, s2) == 0;
  22. }
  23. static unsigned long long xstrtoull(const char *p, char **end)
  24. {
  25. if (*p == '0') {
  26. *end = (char *)p + 1;
  27. return 0;
  28. } else if ('1' <= *p && *p <= '9') {
  29. unsigned long long val;
  30. errno = 0;
  31. val = strtoull(p, end, 10);
  32. assert(errno == 0);
  33. return val;
  34. } else
  35. assert(0);
  36. }
  37. static struct dirent *xreaddir(DIR *d)
  38. {
  39. struct dirent *de;
  40. errno = 0;
  41. de = readdir(d);
  42. assert(de || errno == 0);
  43. return de;
  44. }