thread_map.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <dirent.h>
  2. #include <limits.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include "thread_map.h"
  10. /* Skip "." and ".." directories */
  11. static int filter(const struct dirent *dir)
  12. {
  13. if (dir->d_name[0] == '.')
  14. return 0;
  15. else
  16. return 1;
  17. }
  18. struct thread_map *thread_map__new_by_pid(pid_t pid)
  19. {
  20. struct thread_map *threads;
  21. char name[256];
  22. int items;
  23. struct dirent **namelist = NULL;
  24. int i;
  25. sprintf(name, "/proc/%d/task", pid);
  26. items = scandir(name, &namelist, filter, NULL);
  27. if (items <= 0)
  28. return NULL;
  29. threads = malloc(sizeof(*threads) + sizeof(pid_t) * items);
  30. if (threads != NULL) {
  31. for (i = 0; i < items; i++)
  32. threads->map[i] = atoi(namelist[i]->d_name);
  33. threads->nr = items;
  34. }
  35. for (i=0; i<items; i++)
  36. free(namelist[i]);
  37. free(namelist);
  38. return threads;
  39. }
  40. struct thread_map *thread_map__new_by_tid(pid_t tid)
  41. {
  42. struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t));
  43. if (threads != NULL) {
  44. threads->map[0] = tid;
  45. threads->nr = 1;
  46. }
  47. return threads;
  48. }
  49. struct thread_map *thread_map__new_by_uid(uid_t uid)
  50. {
  51. DIR *proc;
  52. int max_threads = 32, items, i;
  53. char path[256];
  54. struct dirent dirent, *next, **namelist = NULL;
  55. struct thread_map *threads = malloc(sizeof(*threads) +
  56. max_threads * sizeof(pid_t));
  57. if (threads == NULL)
  58. goto out;
  59. proc = opendir("/proc");
  60. if (proc == NULL)
  61. goto out_free_threads;
  62. threads->nr = 0;
  63. while (!readdir_r(proc, &dirent, &next) && next) {
  64. char *end;
  65. bool grow = false;
  66. struct stat st;
  67. pid_t pid = strtol(dirent.d_name, &end, 10);
  68. if (*end) /* only interested in proper numerical dirents */
  69. continue;
  70. snprintf(path, sizeof(path), "/proc/%s", dirent.d_name);
  71. if (stat(path, &st) != 0)
  72. continue;
  73. if (st.st_uid != uid)
  74. continue;
  75. snprintf(path, sizeof(path), "/proc/%d/task", pid);
  76. items = scandir(path, &namelist, filter, NULL);
  77. if (items <= 0)
  78. goto out_free_closedir;
  79. while (threads->nr + items >= max_threads) {
  80. max_threads *= 2;
  81. grow = true;
  82. }
  83. if (grow) {
  84. struct thread_map *tmp;
  85. tmp = realloc(threads, (sizeof(*threads) +
  86. max_threads * sizeof(pid_t)));
  87. if (tmp == NULL)
  88. goto out_free_namelist;
  89. threads = tmp;
  90. }
  91. for (i = 0; i < items; i++)
  92. threads->map[threads->nr + i] = atoi(namelist[i]->d_name);
  93. for (i = 0; i < items; i++)
  94. free(namelist[i]);
  95. free(namelist);
  96. threads->nr += items;
  97. }
  98. out_closedir:
  99. closedir(proc);
  100. out:
  101. return threads;
  102. out_free_threads:
  103. free(threads);
  104. return NULL;
  105. out_free_namelist:
  106. for (i = 0; i < items; i++)
  107. free(namelist[i]);
  108. free(namelist);
  109. out_free_closedir:
  110. free(threads);
  111. threads = NULL;
  112. goto out_closedir;
  113. }
  114. struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
  115. {
  116. if (pid != -1)
  117. return thread_map__new_by_pid(pid);
  118. if (tid == -1 && uid != UINT_MAX)
  119. return thread_map__new_by_uid(uid);
  120. return thread_map__new_by_tid(tid);
  121. }
  122. void thread_map__delete(struct thread_map *threads)
  123. {
  124. free(threads);
  125. }
  126. size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
  127. {
  128. int i;
  129. size_t printed = fprintf(fp, "%d thread%s: ",
  130. threads->nr, threads->nr > 1 ? "s" : "");
  131. for (i = 0; i < threads->nr; ++i)
  132. printed += fprintf(fp, "%s%d", i ? ", " : "", threads->map[i]);
  133. return printed + fprintf(fp, "\n");
  134. }