1/* Monitoring file descriptor usage.
2 Copyright (C) 2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
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>
28
29struct procfs_descriptor
30{
31 int fd;
32 char *link_target;
33 dev_t dev;
34 ino64_t ino;
35};
36
37/* Used with qsort. */
38static int
39descriptor_compare (const void *l, const void *r)
40{
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;
45}
46
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>
53
54struct support_descriptors
55{
56 struct descriptor_list list;
57};
58
59struct support_descriptors *
60support_descriptors_list (void)
61{
62 struct support_descriptors *result = xmalloc (sizeof (*result));
63 descriptor_list_init (&result->list);
64
65 DIR *fds = opendir ("/proc/self/fd");
66 if (fds == NULL)
67 FAIL_EXIT1 ("opendir (\"/proc/self/fd\"): %m");
68
69 while (true)
70 {
71 errno = 0;
72 struct dirent64 *e = readdir64 (fds);
73 if (e == NULL)
74 {
75 if (errno != 0)
76 FAIL_EXIT1 ("readdir: %m");
77 break;
78 }
79
80 if (e->d_name[0] == '.')
81 continue;
82
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);
88
89 /* Skip the descriptor which is used to enumerate the
90 descriptors. */
91 if (fd == dirfd (fds))
92 continue;
93
94 char *target;
95 {
96 char *path = xasprintf ("/proc/self/fd/%ld", fd);
97 target = xreadlink (path);
98 free (path);
99 }
100 struct stat64 st;
101 if (fstat64 (fd, &st) != 0)
102 FAIL_EXIT1 ("readdir: fstat64 (%ld) failed: %m", fd);
103
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;
111 }
112
113 closedir (fds);
114
115 /* Perform a merge join between descrs and current. This assumes
116 that the arrays are sorted by file descriptor. */
117
118 qsort (descriptor_list_begin (&result->list),
119 descriptor_list_size (&result->list),
120 sizeof (struct procfs_descriptor), descriptor_compare);
121
122 return result;
123}
124
125void
126support_descriptors_free (struct support_descriptors *descrs)
127{
128 descriptor_list_free (&descrs->list);
129 free (descrs);
130}
131
132void
133support_descriptors_dump (struct support_descriptors *descrs,
134 const char *prefix, FILE *fp)
135{
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)
139 {
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);
147 }
148}
149
150static void
151dump_mismatch (bool *first,
152 struct support_descriptors *descrs,
153 struct support_descriptors *current)
154{
155 if (*first)
156 *first = false;
157 else
158 return;
159
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:");
166}
167
168static void
169report_closed_descriptor (bool *first,
170 struct support_descriptors *descrs,
171 struct support_descriptors *current,
172 struct procfs_descriptor *left)
173{
174 support_record_failure ();
175 dump_mismatch (first, descrs, current);
176 printf ("error: descriptor %d was closed\n", left->fd);
177}
178
179static void
180report_opened_descriptor (bool *first,
181 struct support_descriptors *descrs,
182 struct support_descriptors *current,
183 struct procfs_descriptor *right)
184{
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);
190}
191
192void
193support_descriptors_check (struct support_descriptors *descrs)
194{
195 struct support_descriptors *current = support_descriptors_list ();
196
197 /* Perform a merge join between descrs and current. This assumes
198 that the arrays are sorted by file descriptor. */
199
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);
204
205 bool first = true;
206 while (left != left_end && right != right_end)
207 {
208 if (left->fd == right->fd)
209 {
210 if (strcmp (left->link_target, right->link_target) != 0)
211 {
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);
220 }
221 if (left->dev != right->dev)
222 {
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));
232 }
233 if (left->ino != right->ino)
234 {
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);
240 }
241 ++left;
242 ++right;
243 }
244 else if (left->fd < right->fd)
245 {
246 /* Gap on the right. */
247 report_closed_descriptor (&first, descrs, current, left);
248 ++left;
249 }
250 else
251 {
252 /* Gap on the left. */
253 TEST_VERIFY_EXIT (left->fd > right->fd);
254 report_opened_descriptor (&first, descrs, current, right);
255 ++right;
256 }
257 }
258
259 while (left != left_end)
260 {
261 /* Closed descriptors (more descriptors on the left). */
262 report_closed_descriptor (&first, descrs, current, left);
263 ++left;
264 }
265
266 while (right != right_end)
267 {
268 /* Opened descriptors (more descriptors on the right). */
269 report_opened_descriptor (&first, descrs, current, right);
270 ++right;
271 }
272
273 support_descriptors_free (current);
274}
275