You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
885 B

  1. #ifndef _GNU_SOURCE
  2. #define _GNU_SOURCE
  3. #endif
  4. #include "procfs_get_name.hpp"
  5. #include "errno.hpp"
  6. #include "fs_close.hpp"
  7. #include "fs_open.hpp"
  8. #include "fs_openat.hpp"
  9. #include "fs_read.hpp"
  10. #include <pthread.h>
  11. static int g_PROCFS_DIR_FD = -1;
  12. const char PROCFS_PATH[] = "/proc";
  13. int
  14. procfs::init()
  15. {
  16. if(g_PROCFS_DIR_FD != -1)
  17. return 0;
  18. g_PROCFS_DIR_FD = fs::open(PROCFS_PATH,O_PATH|O_DIRECTORY);
  19. if(g_PROCFS_DIR_FD == -1)
  20. return -errno;
  21. return 0;
  22. }
  23. std::string
  24. procfs::get_name(const int tid_)
  25. {
  26. int fd;
  27. int rv;
  28. char commpath[256];
  29. snprintf(commpath,sizeof(commpath),"%d/comm",tid_);
  30. fd = fs::openat(g_PROCFS_DIR_FD,commpath,O_RDONLY);
  31. if(fd < 0)
  32. return {};
  33. rv = fs::read(fd,commpath,sizeof(commpath));
  34. if(rv == -1)
  35. return {};
  36. // Overwrite the newline with NUL
  37. commpath[rv-1] = '\0';
  38. fs::close(fd);
  39. return commpath;
  40. }