support_descriptors.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Monitoring file descriptor usage.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <dirent.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <support/check.h>
  20. #include <support/support.h>
  21. #include <sys/stat.h>
  22. #include <sys/sysmacros.h>
  23. #include <xunistd.h>
  24. struct procfs_descriptor
  25. {
  26. int fd;
  27. char *link_target;
  28. dev_t dev;
  29. ino64_t ino;
  30. };
  31. /* Used with qsort. */
  32. static int
  33. descriptor_compare (const void *l, const void *r)
  34. {
  35. const struct procfs_descriptor *left = l;
  36. const struct procfs_descriptor *right = r;
  37. /* Cannot overflow due to limited file descriptor range. */
  38. return left->fd - right->fd;
  39. }
  40. #define DYNARRAY_STRUCT descriptor_list
  41. #define DYNARRAY_ELEMENT struct procfs_descriptor
  42. #define DYNARRAY_PREFIX descriptor_list_
  43. #define DYNARRAY_ELEMENT_FREE(e) free ((e)->link_target)
  44. #define DYNARRAY_INITIAL_SIZE 0
  45. #include <malloc/dynarray-skeleton.c>
  46. struct support_descriptors
  47. {
  48. struct descriptor_list list;
  49. };
  50. struct support_descriptors *
  51. support_descriptors_list (void)
  52. {
  53. struct support_descriptors *result = xmalloc (sizeof (*result));
  54. descriptor_list_init (&result->list);
  55. DIR *fds = opendir ("/proc/self/fd");
  56. if (fds == NULL)
  57. FAIL_EXIT1 ("opendir (\"/proc/self/fd\"): %m");
  58. while (true)
  59. {
  60. errno = 0;
  61. struct dirent64 *e = readdir64 (fds);
  62. if (e == NULL)
  63. {
  64. if (errno != 0)
  65. FAIL_EXIT1 ("readdir: %m");
  66. break;
  67. }
  68. if (e->d_name[0] == '.')
  69. continue;
  70. char *endptr;
  71. long int fd = strtol (e->d_name, &endptr, 10);
  72. if (*endptr != '\0' || fd < 0 || fd > INT_MAX)
  73. FAIL_EXIT1 ("readdir: invalid file descriptor name: /proc/self/fd/%s",
  74. e->d_name);
  75. /* Skip the descriptor which is used to enumerate the
  76. descriptors. */
  77. if (fd == dirfd (fds))
  78. continue;
  79. char *target;
  80. {
  81. char *path = xasprintf ("/proc/self/fd/%ld", fd);
  82. target = xreadlink (path);
  83. free (path);
  84. }
  85. struct stat64 st;
  86. if (fstat64 (fd, &st) != 0)
  87. FAIL_EXIT1 ("readdir: fstat64 (%ld) failed: %m", fd);
  88. struct procfs_descriptor *item = descriptor_list_emplace (&result->list);
  89. if (item == NULL)
  90. FAIL_EXIT1 ("descriptor_list_emplace: %m");
  91. item->fd = fd;
  92. item->link_target = target;
  93. item->dev = st.st_dev;
  94. item->ino = st.st_ino;
  95. }
  96. closedir (fds);
  97. /* Perform a merge join between descrs and current. This assumes
  98. that the arrays are sorted by file descriptor. */
  99. qsort (descriptor_list_begin (&result->list),
  100. descriptor_list_size (&result->list),
  101. sizeof (struct procfs_descriptor), descriptor_compare);
  102. return result;
  103. }
  104. void
  105. support_descriptors_free (struct support_descriptors *descrs)
  106. {
  107. descriptor_list_free (&descrs->list);
  108. free (descrs);
  109. }
  110. void
  111. support_descriptors_dump (struct support_descriptors *descrs,
  112. const char *prefix, FILE *fp)
  113. {
  114. struct procfs_descriptor *end = descriptor_list_end (&descrs->list);
  115. for (struct procfs_descriptor *d = descriptor_list_begin (&descrs->list);
  116. d != end; ++d)
  117. {
  118. char *quoted = support_quote_string (d->link_target);
  119. fprintf (fp, "%s%d: target=\"%s\" major=%lld minor=%lld ino=%lld\n",
  120. prefix, d->fd, quoted,
  121. (long long int) major (d->dev),
  122. (long long int) minor (d->dev),
  123. (long long int) d->ino);
  124. free (quoted);
  125. }
  126. }
  127. static void
  128. dump_mismatch (bool *first,
  129. struct support_descriptors *descrs,
  130. struct support_descriptors *current)
  131. {
  132. if (*first)
  133. *first = false;
  134. else
  135. return;
  136. puts ("error: Differences found in descriptor set");
  137. puts ("Reference descriptor set:");
  138. support_descriptors_dump (descrs, " ", stdout);
  139. puts ("Current descriptor set:");
  140. support_descriptors_dump (current, " ", stdout);
  141. puts ("Differences:");
  142. }
  143. static void
  144. report_closed_descriptor (bool *first,
  145. struct support_descriptors *descrs,
  146. struct support_descriptors *current,
  147. struct procfs_descriptor *left)
  148. {
  149. support_record_failure ();
  150. dump_mismatch (first, descrs, current);
  151. printf ("error: descriptor %d was closed\n", left->fd);
  152. }
  153. static void
  154. report_opened_descriptor (bool *first,
  155. struct support_descriptors *descrs,
  156. struct support_descriptors *current,
  157. struct procfs_descriptor *right)
  158. {
  159. support_record_failure ();
  160. dump_mismatch (first, descrs, current);
  161. char *quoted = support_quote_string (right->link_target);
  162. printf ("error: descriptor %d was opened (\"%s\")\n", right->fd, quoted);
  163. free (quoted);
  164. }
  165. void
  166. support_descriptors_check (struct support_descriptors *descrs)
  167. {
  168. struct support_descriptors *current = support_descriptors_list ();
  169. /* Perform a merge join between descrs and current. This assumes
  170. that the arrays are sorted by file descriptor. */
  171. struct procfs_descriptor *left = descriptor_list_begin (&descrs->list);
  172. struct procfs_descriptor *left_end = descriptor_list_end (&descrs->list);
  173. struct procfs_descriptor *right = descriptor_list_begin (&current->list);
  174. struct procfs_descriptor *right_end = descriptor_list_end (&current->list);
  175. bool first = true;
  176. while (left != left_end && right != right_end)
  177. {
  178. if (left->fd == right->fd)
  179. {
  180. if (strcmp (left->link_target, right->link_target) != 0)
  181. {
  182. support_record_failure ();
  183. char *left_quoted = support_quote_string (left->link_target);
  184. char *right_quoted = support_quote_string (right->link_target);
  185. dump_mismatch (&first, descrs, current);
  186. printf ("error: descriptor %d changed from \"%s\" to \"%s\"\n",
  187. left->fd, left_quoted, right_quoted);
  188. free (left_quoted);
  189. free (right_quoted);
  190. }
  191. if (left->dev != right->dev)
  192. {
  193. support_record_failure ();
  194. dump_mismatch (&first, descrs, current);
  195. printf ("error: descriptor %d changed device"
  196. " from %lld:%lld to %lld:%lld\n",
  197. left->fd,
  198. (long long int) major (left->dev),
  199. (long long int) minor (left->dev),
  200. (long long int) major (right->dev),
  201. (long long int) minor (right->dev));
  202. }
  203. if (left->ino != right->ino)
  204. {
  205. support_record_failure ();
  206. dump_mismatch (&first, descrs, current);
  207. printf ("error: descriptor %d changed ino from %lld to %lld\n",
  208. left->fd,
  209. (long long int) left->ino, (long long int) right->ino);
  210. }
  211. ++left;
  212. ++right;
  213. }
  214. else if (left->fd < right->fd)
  215. {
  216. /* Gap on the right. */
  217. report_closed_descriptor (&first, descrs, current, left);
  218. ++left;
  219. }
  220. else
  221. {
  222. /* Gap on the left. */
  223. TEST_VERIFY_EXIT (left->fd > right->fd);
  224. report_opened_descriptor (&first, descrs, current, right);
  225. ++right;
  226. }
  227. }
  228. while (left != left_end)
  229. {
  230. /* Closed descriptors (more descriptors on the left). */
  231. report_closed_descriptor (&first, descrs, current, left);
  232. ++left;
  233. }
  234. while (right != right_end)
  235. {
  236. /* Opened descriptors (more descriptors on the right). */
  237. report_opened_descriptor (&first, descrs, current, right);
  238. ++right;
  239. }
  240. support_descriptors_free (current);
  241. }