aboutsummaryrefslogtreecommitdiff
blob: 00e437e9dc5fbf5f03181d189ba6167bc728436a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Monitoring file descriptor usage.
   Copyright (C) 2018-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <xunistd.h>

struct procfs_descriptor
{
  int fd;
  char *link_target;
  dev_t dev;
  ino64_t ino;
};

/* Used with qsort.  */
static int
descriptor_compare (const void *l, const void *r)
{
  const struct procfs_descriptor *left = l;
  const struct procfs_descriptor *right = r;
  /* Cannot overflow due to limited file descriptor range.  */
  return left->fd - right->fd;
}

#define DYNARRAY_STRUCT descriptor_list
#define DYNARRAY_ELEMENT struct procfs_descriptor
#define DYNARRAY_PREFIX descriptor_list_
#define DYNARRAY_ELEMENT_FREE(e) free ((e)->link_target)
#define DYNARRAY_INITIAL_SIZE 0
#include <malloc/dynarray-skeleton.c>

struct support_descriptors
{
  struct descriptor_list list;
};

struct support_descriptors *
support_descriptors_list (void)
{
  struct support_descriptors *result = xmalloc (sizeof (*result));
  descriptor_list_init (&result->list);

  DIR *fds = opendir ("/proc/self/fd");
  if (fds == NULL)
    FAIL_EXIT1 ("opendir (\"/proc/self/fd\"): %m");

  while (true)
    {
      errno = 0;
      struct dirent64 *e = readdir64 (fds);
      if (e == NULL)
        {
          if (errno != 0)
            FAIL_EXIT1 ("readdir: %m");
          break;
        }

      if (e->d_name[0] == '.')
        continue;

      char *endptr;
      long int fd = strtol (e->d_name, &endptr, 10);
      if (*endptr != '\0' || fd < 0 || fd > INT_MAX)
        FAIL_EXIT1 ("readdir: invalid file descriptor name: /proc/self/fd/%s",
                    e->d_name);

      /* Skip the descriptor which is used to enumerate the
         descriptors.  */
      if (fd == dirfd (fds))
        continue;

      char *target;
      {
        char *path = xasprintf ("/proc/self/fd/%ld", fd);
        target = xreadlink (path);
        free (path);
      }
      struct stat64 st;
      if (fstat64 (fd, &st) != 0)
        FAIL_EXIT1 ("readdir: fstat64 (%ld) failed: %m", fd);

      struct procfs_descriptor *item = descriptor_list_emplace (&result->list);
      if (item == NULL)
        FAIL_EXIT1 ("descriptor_list_emplace: %m");
      item->fd = fd;
      item->link_target = target;
      item->dev = st.st_dev;
      item->ino = st.st_ino;
    }

  closedir (fds);

  /* Perform a merge join between descrs and current.  This assumes
     that the arrays are sorted by file descriptor.  */

  qsort (descriptor_list_begin (&result->list),
         descriptor_list_size (&result->list),
         sizeof (struct procfs_descriptor), descriptor_compare);

  return result;
}

void
support_descriptors_free (struct support_descriptors *descrs)
{
  descriptor_list_free (&descrs->list);
  free (descrs);
}

void
support_descriptors_dump (struct support_descriptors *descrs,
                          const char *prefix, FILE *fp)
{
  struct procfs_descriptor *end = descriptor_list_end (&descrs->list);
  for (struct procfs_descriptor *d = descriptor_list_begin (&descrs->list);
       d != end; ++d)
    {
      char *quoted = support_quote_string (d->link_target);
      fprintf (fp, "%s%d: target=\"%s\" major=%lld minor=%lld ino=%lld\n",
               prefix, d->fd, quoted,
               (long long int) major (d->dev),
               (long long int) minor (d->dev),
               (long long int) d->ino);
      free (quoted);
    }
}

static void
dump_mismatch (bool *first,
               struct support_descriptors *descrs,
               struct support_descriptors *current)
{
  if (*first)
    *first = false;
  else
    return;

  puts ("error: Differences found in descriptor set");
  puts ("Reference descriptor set:");
  support_descriptors_dump (descrs, "  ", stdout);
  puts ("Current descriptor set:");
  support_descriptors_dump (current, "  ", stdout);
  puts ("Differences:");
}

static void
report_closed_descriptor (bool *first,
                          struct support_descriptors *descrs,
                          struct support_descriptors *current,
                          struct procfs_descriptor *left)
{
  support_record_failure ();
  dump_mismatch (first, descrs, current);
  printf ("error: descriptor %d was closed\n", left->fd);
}

static void
report_opened_descriptor (bool *first,
                          struct support_descriptors *descrs,
                          struct support_descriptors *current,
                          struct procfs_descriptor *right)
{
  support_record_failure ();
  dump_mismatch (first, descrs, current);
  char *quoted = support_quote_string (right->link_target);
  printf ("error: descriptor %d was opened (\"%s\")\n", right->fd, quoted);
  free (quoted);
}

void
support_descriptors_check (struct support_descriptors *descrs)
{
  struct support_descriptors *current = support_descriptors_list ();

  /* Perform a merge join between descrs and current.  This assumes
     that the arrays are sorted by file descriptor.  */

  struct procfs_descriptor *left = descriptor_list_begin (&descrs->list);
  struct procfs_descriptor *left_end = descriptor_list_end (&descrs->list);
  struct procfs_descriptor *right = descriptor_list_begin (&current->list);
  struct procfs_descriptor *right_end = descriptor_list_end (&current->list);

  bool first = true;
  while (left != left_end && right != right_end)
    {
      if (left->fd == right->fd)
        {
          if (strcmp (left->link_target, right->link_target) != 0)
            {
              support_record_failure ();
              char *left_quoted = support_quote_string (left->link_target);
              char *right_quoted = support_quote_string (right->link_target);
              dump_mismatch (&first, descrs, current);
              printf ("error: descriptor %d changed from \"%s\" to \"%s\"\n",
                      left->fd, left_quoted, right_quoted);
              free (left_quoted);
              free (right_quoted);
            }
          if (left->dev != right->dev)
            {
              support_record_failure ();
              dump_mismatch (&first, descrs, current);
              printf ("error: descriptor %d changed device"
                      " from %lld:%lld to %lld:%lld\n",
                      left->fd,
                      (long long int) major (left->dev),
                      (long long int) minor (left->dev),
                      (long long int) major (right->dev),
                      (long long int) minor (right->dev));
            }
          if (left->ino != right->ino)
            {
              support_record_failure ();
              dump_mismatch (&first, descrs, current);
              printf ("error: descriptor %d changed ino from %lld to %lld\n",
                      left->fd,
                      (long long int) left->ino, (long long int) right->ino);
            }
          ++left;
          ++right;
        }
      else if (left->fd < right->fd)
        {
          /* Gap on the right.  */
          report_closed_descriptor (&first, descrs, current, left);
          ++left;
        }
      else
        {
          /* Gap on the left.  */
          TEST_VERIFY_EXIT (left->fd > right->fd);
          report_opened_descriptor (&first, descrs, current, right);
          ++right;
        }
    }

  while (left != left_end)
    {
      /* Closed descriptors (more descriptors on the left).  */
      report_closed_descriptor (&first, descrs, current, left);
      ++left;
    }

  while (right != right_end)
    {
      /* Opened descriptors (more descriptors on the right).  */
      report_opened_descriptor (&first, descrs, current, right);
      ++right;
    }

  support_descriptors_free (current);
}