1 /* Monitoring file descriptor usage.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
23 #include <support/check.h>
24 #include <support/descriptors.h>
25 #include <support/support.h>
27 #include <sys/sysmacros.h>
30 struct procfs_descriptor
38 /* Used with qsort. */
40 descriptor_compare (const void *l
, const void *r
)
42 const struct procfs_descriptor
*left
= l
;
43 const struct procfs_descriptor
*right
= r
;
44 /* Cannot overflow due to limited file descriptor range. */
45 return left
->fd
- right
->fd
;
48 #define DYNARRAY_STRUCT descriptor_list
49 #define DYNARRAY_ELEMENT struct procfs_descriptor
50 #define DYNARRAY_PREFIX descriptor_list_
51 #define DYNARRAY_ELEMENT_FREE(e) free ((e)->link_target)
52 #define DYNARRAY_INITIAL_SIZE 0
53 #include <malloc/dynarray-skeleton.c>
55 struct support_descriptors
57 struct descriptor_list list
;
60 struct support_descriptors
*
61 support_descriptors_list (void)
63 struct support_descriptors
*result
= xmalloc (sizeof (*result
));
64 descriptor_list_init (&result
->list
);
66 DIR *fds
= opendir ("/proc/self/fd");
68 FAIL_EXIT1 ("opendir (\"/proc/self/fd\"): %m");
73 struct dirent64
*e
= readdir64 (fds
);
77 FAIL_EXIT1 ("readdir: %m");
81 if (e
->d_name
[0] == '.')
85 long int fd
= strtol (e
->d_name
, &endptr
, 10);
86 if (*endptr
!= '\0' || fd
< 0 || fd
> INT_MAX
)
87 FAIL_EXIT1 ("readdir: invalid file descriptor name: /proc/self/fd/%s",
90 /* Skip the descriptor which is used to enumerate the
92 if (fd
== dirfd (fds
))
97 char *path
= xasprintf ("/proc/self/fd/%ld", fd
);
98 target
= xreadlink (path
);
102 if (fstat64 (fd
, &st
) != 0)
103 FAIL_EXIT1 ("readdir: fstat64 (%ld) failed: %m", fd
);
105 struct procfs_descriptor
*item
= descriptor_list_emplace (&result
->list
);
107 FAIL_EXIT1 ("descriptor_list_emplace: %m");
109 item
->link_target
= target
;
110 item
->dev
= st
.st_dev
;
111 item
->ino
= st
.st_ino
;
116 /* Perform a merge join between descrs and current. This assumes
117 that the arrays are sorted by file descriptor. */
119 qsort (descriptor_list_begin (&result
->list
),
120 descriptor_list_size (&result
->list
),
121 sizeof (struct procfs_descriptor
), descriptor_compare
);
127 support_descriptors_free (struct support_descriptors
*descrs
)
129 descriptor_list_free (&descrs
->list
);
134 support_descriptors_dump (struct support_descriptors
*descrs
,
135 const char *prefix
, FILE *fp
)
137 struct procfs_descriptor
*end
= descriptor_list_end (&descrs
->list
);
138 for (struct procfs_descriptor
*d
= descriptor_list_begin (&descrs
->list
);
141 char *quoted
= support_quote_string (d
->link_target
);
142 fprintf (fp
, "%s%d: target=\"%s\" major=%lld minor=%lld ino=%lld\n",
143 prefix
, d
->fd
, quoted
,
144 (long long int) major (d
->dev
),
145 (long long int) minor (d
->dev
),
146 (long long int) d
->ino
);
152 dump_mismatch (bool *first
,
153 struct support_descriptors
*descrs
,
154 struct support_descriptors
*current
)
161 puts ("error: Differences found in descriptor set");
162 puts ("Reference descriptor set:");
163 support_descriptors_dump (descrs
, " ", stdout
);
164 puts ("Current descriptor set:");
165 support_descriptors_dump (current
, " ", stdout
);
166 puts ("Differences:");
170 report_closed_descriptor (bool *first
,
171 struct support_descriptors
*descrs
,
172 struct support_descriptors
*current
,
173 struct procfs_descriptor
*left
)
175 support_record_failure ();
176 dump_mismatch (first
, descrs
, current
);
177 printf ("error: descriptor %d was closed\n", left
->fd
);
181 report_opened_descriptor (bool *first
,
182 struct support_descriptors
*descrs
,
183 struct support_descriptors
*current
,
184 struct procfs_descriptor
*right
)
186 support_record_failure ();
187 dump_mismatch (first
, descrs
, current
);
188 char *quoted
= support_quote_string (right
->link_target
);
189 printf ("error: descriptor %d was opened (\"%s\")\n", right
->fd
, quoted
);
194 support_descriptors_check (struct support_descriptors
*descrs
)
196 struct support_descriptors
*current
= support_descriptors_list ();
198 /* Perform a merge join between descrs and current. This assumes
199 that the arrays are sorted by file descriptor. */
201 struct procfs_descriptor
*left
= descriptor_list_begin (&descrs
->list
);
202 struct procfs_descriptor
*left_end
= descriptor_list_end (&descrs
->list
);
203 struct procfs_descriptor
*right
= descriptor_list_begin (¤t
->list
);
204 struct procfs_descriptor
*right_end
= descriptor_list_end (¤t
->list
);
207 while (left
!= left_end
&& right
!= right_end
)
209 if (left
->fd
== right
->fd
)
211 if (strcmp (left
->link_target
, right
->link_target
) != 0)
213 support_record_failure ();
214 char *left_quoted
= support_quote_string (left
->link_target
);
215 char *right_quoted
= support_quote_string (right
->link_target
);
216 dump_mismatch (&first
, descrs
, current
);
217 printf ("error: descriptor %d changed from \"%s\" to \"%s\"\n",
218 left
->fd
, left_quoted
, right_quoted
);
222 if (left
->dev
!= right
->dev
)
224 support_record_failure ();
225 dump_mismatch (&first
, descrs
, current
);
226 printf ("error: descriptor %d changed device"
227 " from %lld:%lld to %lld:%lld\n",
229 (long long int) major (left
->dev
),
230 (long long int) minor (left
->dev
),
231 (long long int) major (right
->dev
),
232 (long long int) minor (right
->dev
));
234 if (left
->ino
!= right
->ino
)
236 support_record_failure ();
237 dump_mismatch (&first
, descrs
, current
);
238 printf ("error: descriptor %d changed ino from %lld to %lld\n",
240 (long long int) left
->ino
, (long long int) right
->ino
);
245 else if (left
->fd
< right
->fd
)
247 /* Gap on the right. */
248 report_closed_descriptor (&first
, descrs
, current
, left
);
253 /* Gap on the left. */
254 TEST_VERIFY_EXIT (left
->fd
> right
->fd
);
255 report_opened_descriptor (&first
, descrs
, current
, right
);
260 while (left
!= left_end
)
262 /* Closed descriptors (more descriptors on the left). */
263 report_closed_descriptor (&first
, descrs
, current
, left
);
267 while (right
!= right_end
)
269 /* Opened descriptors (more descriptors on the right). */
270 report_opened_descriptor (&first
, descrs
, current
, right
);
274 support_descriptors_free (current
);