1 /* Monitoring file descriptor usage.
2 Copyright (C) 2018-2019 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 <http://www.gnu.org/licenses/>. */
23 #include <support/check.h>
24 #include <support/support.h>
26 #include <sys/sysmacros.h>
29 struct procfs_descriptor
37 /* Used with qsort. */
39 descriptor_compare (const void *l
, const void *r
)
41 const struct procfs_descriptor
*left
= l
;
42 const struct procfs_descriptor
*right
= r
;
43 /* Cannot overflow due to limited file descriptor range. */
44 return left
->fd
- right
->fd
;
47 #define DYNARRAY_STRUCT descriptor_list
48 #define DYNARRAY_ELEMENT struct procfs_descriptor
49 #define DYNARRAY_PREFIX descriptor_list_
50 #define DYNARRAY_ELEMENT_FREE(e) free ((e)->link_target)
51 #define DYNARRAY_INITIAL_SIZE 0
52 #include <malloc/dynarray-skeleton.c>
54 struct support_descriptors
56 struct descriptor_list list
;
59 struct support_descriptors
*
60 support_descriptors_list (void)
62 struct support_descriptors
*result
= xmalloc (sizeof (*result
));
63 descriptor_list_init (&result
->list
);
65 DIR *fds
= opendir ("/proc/self/fd");
67 FAIL_EXIT1 ("opendir (\"/proc/self/fd\"): %m");
72 struct dirent64
*e
= readdir64 (fds
);
76 FAIL_EXIT1 ("readdir: %m");
80 if (e
->d_name
[0] == '.')
84 long int fd
= strtol (e
->d_name
, &endptr
, 10);
85 if (*endptr
!= '\0' || fd
< 0 || fd
> INT_MAX
)
86 FAIL_EXIT1 ("readdir: invalid file descriptor name: /proc/self/fd/%s",
89 /* Skip the descriptor which is used to enumerate the
91 if (fd
== dirfd (fds
))
96 char *path
= xasprintf ("/proc/self/fd/%ld", fd
);
97 target
= xreadlink (path
);
101 if (fstat64 (fd
, &st
) != 0)
102 FAIL_EXIT1 ("readdir: fstat64 (%ld) failed: %m", fd
);
104 struct procfs_descriptor
*item
= descriptor_list_emplace (&result
->list
);
106 FAIL_EXIT1 ("descriptor_list_emplace: %m");
108 item
->link_target
= target
;
109 item
->dev
= st
.st_dev
;
110 item
->ino
= st
.st_ino
;
115 /* Perform a merge join between descrs and current. This assumes
116 that the arrays are sorted by file descriptor. */
118 qsort (descriptor_list_begin (&result
->list
),
119 descriptor_list_size (&result
->list
),
120 sizeof (struct procfs_descriptor
), descriptor_compare
);
126 support_descriptors_free (struct support_descriptors
*descrs
)
128 descriptor_list_free (&descrs
->list
);
133 support_descriptors_dump (struct support_descriptors
*descrs
,
134 const char *prefix
, FILE *fp
)
136 struct procfs_descriptor
*end
= descriptor_list_end (&descrs
->list
);
137 for (struct procfs_descriptor
*d
= descriptor_list_begin (&descrs
->list
);
140 char *quoted
= support_quote_string (d
->link_target
);
141 fprintf (fp
, "%s%d: target=\"%s\" major=%lld minor=%lld ino=%lld\n",
142 prefix
, d
->fd
, quoted
,
143 (long long int) major (d
->dev
),
144 (long long int) minor (d
->dev
),
145 (long long int) d
->ino
);
151 dump_mismatch (bool *first
,
152 struct support_descriptors
*descrs
,
153 struct support_descriptors
*current
)
160 puts ("error: Differences found in descriptor set");
161 puts ("Reference descriptor set:");
162 support_descriptors_dump (descrs
, " ", stdout
);
163 puts ("Current descriptor set:");
164 support_descriptors_dump (current
, " ", stdout
);
165 puts ("Differences:");
169 report_closed_descriptor (bool *first
,
170 struct support_descriptors
*descrs
,
171 struct support_descriptors
*current
,
172 struct procfs_descriptor
*left
)
174 support_record_failure ();
175 dump_mismatch (first
, descrs
, current
);
176 printf ("error: descriptor %d was closed\n", left
->fd
);
180 report_opened_descriptor (bool *first
,
181 struct support_descriptors
*descrs
,
182 struct support_descriptors
*current
,
183 struct procfs_descriptor
*right
)
185 support_record_failure ();
186 dump_mismatch (first
, descrs
, current
);
187 char *quoted
= support_quote_string (right
->link_target
);
188 printf ("error: descriptor %d was opened (\"%s\")\n", right
->fd
, quoted
);
193 support_descriptors_check (struct support_descriptors
*descrs
)
195 struct support_descriptors
*current
= support_descriptors_list ();
197 /* Perform a merge join between descrs and current. This assumes
198 that the arrays are sorted by file descriptor. */
200 struct procfs_descriptor
*left
= descriptor_list_begin (&descrs
->list
);
201 struct procfs_descriptor
*left_end
= descriptor_list_end (&descrs
->list
);
202 struct procfs_descriptor
*right
= descriptor_list_begin (¤t
->list
);
203 struct procfs_descriptor
*right_end
= descriptor_list_end (¤t
->list
);
206 while (left
!= left_end
&& right
!= right_end
)
208 if (left
->fd
== right
->fd
)
210 if (strcmp (left
->link_target
, right
->link_target
) != 0)
212 support_record_failure ();
213 char *left_quoted
= support_quote_string (left
->link_target
);
214 char *right_quoted
= support_quote_string (right
->link_target
);
215 dump_mismatch (&first
, descrs
, current
);
216 printf ("error: descriptor %d changed from \"%s\" to \"%s\"\n",
217 left
->fd
, left_quoted
, right_quoted
);
221 if (left
->dev
!= right
->dev
)
223 support_record_failure ();
224 dump_mismatch (&first
, descrs
, current
);
225 printf ("error: descriptor %d changed device"
226 " from %lld:%lld to %lld:%lld\n",
228 (long long int) major (left
->dev
),
229 (long long int) minor (left
->dev
),
230 (long long int) major (right
->dev
),
231 (long long int) minor (right
->dev
));
233 if (left
->ino
!= right
->ino
)
235 support_record_failure ();
236 dump_mismatch (&first
, descrs
, current
);
237 printf ("error: descriptor %d changed ino from %lld to %lld\n",
239 (long long int) left
->ino
, (long long int) right
->ino
);
244 else if (left
->fd
< right
->fd
)
246 /* Gap on the right. */
247 report_closed_descriptor (&first
, descrs
, current
, left
);
252 /* Gap on the left. */
253 TEST_VERIFY_EXIT (left
->fd
> right
->fd
);
254 report_opened_descriptor (&first
, descrs
, current
, right
);
259 while (left
!= left_end
)
261 /* Closed descriptors (more descriptors on the left). */
262 report_closed_descriptor (&first
, descrs
, current
, left
);
266 while (right
!= right_end
)
268 /* Opened descriptors (more descriptors on the right). */
269 report_opened_descriptor (&first
, descrs
, current
, right
);
273 support_descriptors_free (current
);