iconv, localedef: avoid floating point rounding differences [BZ #24372]
[glibc.git] / support / support_descriptors.c
blob00e437e9dc5fbf5f03181d189ba6167bc728436a
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/>. */
19 #include <dirent.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <support/check.h>
24 #include <support/support.h>
25 #include <sys/stat.h>
26 #include <sys/sysmacros.h>
27 #include <xunistd.h>
29 struct procfs_descriptor
31 int fd;
32 char *link_target;
33 dev_t dev;
34 ino64_t ino;
37 /* Used with qsort. */
38 static int
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");
66 if (fds == NULL)
67 FAIL_EXIT1 ("opendir (\"/proc/self/fd\"): %m");
69 while (true)
71 errno = 0;
72 struct dirent64 *e = readdir64 (fds);
73 if (e == NULL)
75 if (errno != 0)
76 FAIL_EXIT1 ("readdir: %m");
77 break;
80 if (e->d_name[0] == '.')
81 continue;
83 char *endptr;
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",
87 e->d_name);
89 /* Skip the descriptor which is used to enumerate the
90 descriptors. */
91 if (fd == dirfd (fds))
92 continue;
94 char *target;
96 char *path = xasprintf ("/proc/self/fd/%ld", fd);
97 target = xreadlink (path);
98 free (path);
100 struct stat64 st;
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);
105 if (item == NULL)
106 FAIL_EXIT1 ("descriptor_list_emplace: %m");
107 item->fd = fd;
108 item->link_target = target;
109 item->dev = st.st_dev;
110 item->ino = st.st_ino;
113 closedir (fds);
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);
122 return result;
125 void
126 support_descriptors_free (struct support_descriptors *descrs)
128 descriptor_list_free (&descrs->list);
129 free (descrs);
132 void
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);
138 d != end; ++d)
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);
146 free (quoted);
150 static void
151 dump_mismatch (bool *first,
152 struct support_descriptors *descrs,
153 struct support_descriptors *current)
155 if (*first)
156 *first = false;
157 else
158 return;
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:");
168 static void
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);
179 static void
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);
189 free (quoted);
192 void
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 (&current->list);
203 struct procfs_descriptor *right_end = descriptor_list_end (&current->list);
205 bool first = true;
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);
218 free (left_quoted);
219 free (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",
227 left->fd,
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",
238 left->fd,
239 (long long int) left->ino, (long long int) right->ino);
241 ++left;
242 ++right;
244 else if (left->fd < right->fd)
246 /* Gap on the right. */
247 report_closed_descriptor (&first, descrs, current, left);
248 ++left;
250 else
252 /* Gap on the left. */
253 TEST_VERIFY_EXIT (left->fd > right->fd);
254 report_opened_descriptor (&first, descrs, current, right);
255 ++right;
259 while (left != left_end)
261 /* Closed descriptors (more descriptors on the left). */
262 report_closed_descriptor (&first, descrs, current, left);
263 ++left;
266 while (right != right_end)
268 /* Opened descriptors (more descriptors on the right). */
269 report_opened_descriptor (&first, descrs, current, right);
270 ++right;
273 support_descriptors_free (current);