aboutsummaryrefslogtreecommitdiff
path: root/io/tst-close_range.c
blob: e3feeb9bcaa3614bdbba47b9dec5be89d5aac4f3 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* Test for the close_range system call.
   Copyright (C) 2021-2023 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
   <https://www.gnu.org/licenses/>.  */

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>

#include <array_length.h>
#include <support/capture_subprocess.h>
#include <support/check.h>
#include <support/descriptors.h>
#include <support/support.h>
#include <support/xsched.h>
#include <support/xunistd.h>

#define NFDS 100

static void
close_range_test_max_upper_limit (void)
{
  struct support_descriptors *descrs = support_descriptors_list ();

  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);

  {
    int r = close_range (lowfd, ~0U, 0);
    if (r == -1 && errno == ENOSYS)
      FAIL_UNSUPPORTED ("close_range not supported");
    TEST_COMPARE (r, 0);
  }

  support_descriptors_check (descrs);
  support_descriptors_free (descrs);
}

static void
close_range_test_common (int lowfd, unsigned int flags)
{
  const int maximum_fd = lowfd + NFDS - 1;
  const int half_fd = lowfd + NFDS / 2;
  const int gap_1 = maximum_fd - 8;

  /* Close half of the descriptors and check result.  */
  TEST_COMPARE (close_range (lowfd, half_fd, flags), 0);
  for (int i = lowfd; i <= half_fd; i++)
    {
      TEST_COMPARE (fcntl (i, F_GETFL), -1);
      TEST_COMPARE (errno, EBADF);
    }
  for (int i = half_fd + 1; i < maximum_fd; i++)
    TEST_VERIFY (fcntl (i, F_GETFL) > -1);

  /* Create some gaps, close up to a threshold, and check result.  */
  xclose (lowfd + 57);
  xclose (lowfd + 78);
  xclose (lowfd + 81);
  xclose (lowfd + 82);
  xclose (lowfd + 84);
  xclose (lowfd + 90);

  TEST_COMPARE (close_range (half_fd + 1, gap_1, flags), 0);
  for (int i = half_fd + 1; i < gap_1; i++)
    {
      TEST_COMPARE (fcntl (i, F_GETFL), -1);
      TEST_COMPARE (errno, EBADF);
    }
  for (int i = gap_1 + 1; i < maximum_fd; i++)
    TEST_VERIFY (fcntl (i, F_GETFL) > -1);

  /* Close the remaining but the last one.  */
  TEST_COMPARE (close_range (gap_1 + 1, maximum_fd - 1, flags), 0);
  for (int i = gap_1 + 1; i < maximum_fd - 1; i++)
    {
      TEST_COMPARE (fcntl (i, F_GETFL), -1);
      TEST_COMPARE (errno, EBADF);
    }
  TEST_VERIFY (fcntl (maximum_fd, F_GETFL) > -1);

  /* Close the last one.  */
  TEST_COMPARE (close_range (maximum_fd, maximum_fd, flags), 0);
  TEST_COMPARE (fcntl (maximum_fd, F_GETFL), -1);
  TEST_COMPARE (errno, EBADF);
}

/* Basic tests: check if the syscall close ranges with and without gaps.  */
static void
close_range_test (void)
{
  struct support_descriptors *descrs = support_descriptors_list ();

  /* Check if the temporary file descriptor has no no gaps.  */
  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);

  close_range_test_common (lowfd, 0);

  /* Double check by check the /proc.  */
  support_descriptors_check (descrs);
  support_descriptors_free (descrs);
}

#ifdef __linux__
_Noreturn static int
close_range_test_fn (void *arg)
{
  int lowfd = (int) ((uintptr_t) arg);
  close_range_test_common (lowfd, 0);
  exit (EXIT_SUCCESS);
}

/* Check if a clone_range on a subprocess created with CLONE_FILES close
   the shared file descriptor table entries in the parent.  */
static void
close_range_test_subprocess (void)
{
  struct support_descriptors *descrs = support_descriptors_list ();

  /* Check if the temporary file descriptor has no no gaps.  */
  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);

  struct support_stack stack = support_stack_alloc (4096);

  pid_t pid = xclone (close_range_test_fn, (void*) (uintptr_t) lowfd,
		      stack.stack, stack.size, CLONE_FILES | SIGCHLD);
  TEST_VERIFY_EXIT (pid > 0);
  int status;
  xwaitpid (pid, &status, 0);
  TEST_VERIFY (WIFEXITED (status));
  TEST_COMPARE (WEXITSTATUS (status), 0);

  support_stack_free (&stack);

  for (int i = lowfd; i < NFDS; i++)
    TEST_VERIFY (fcntl (i, F_GETFL) < 0);

  support_descriptors_check (descrs);
  support_descriptors_free (descrs);
}
#endif


#ifdef CLOSE_RANGE_UNSHARE
_Noreturn static int
close_range_unshare_test_fn (void *arg)
{
  int lowfd = (int) ((uintptr_t) arg);
  close_range_test_common (lowfd, CLOSE_RANGE_UNSHARE);
  exit (EXIT_SUCCESS);
}

/* Check if a close_range with CLOSE_RANGE_UNSHARE issued from a subprocess
   created with CLONE_FILES does not close the parent file descriptor list.  */
static void
close_range_unshare_test (void)
{
  struct support_descriptors *descrs1 = support_descriptors_list ();

  /* Check if the temporary file descriptor has no no gaps.  */
  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);

  struct support_descriptors *descrs2 = support_descriptors_list ();

  struct support_stack stack = support_stack_alloc (4096);

  pid_t pid = xclone (close_range_unshare_test_fn, (void*) (uintptr_t) lowfd,
		      stack.stack, stack.size, CLONE_FILES | SIGCHLD);
  TEST_VERIFY_EXIT (pid > 0);
  int status;
  xwaitpid (pid, &status, 0);
  TEST_VERIFY (WIFEXITED (status));
  TEST_COMPARE (WEXITSTATUS (status), 0);

  support_stack_free (&stack);

  for (int i = lowfd; i < lowfd + NFDS; i++)
    TEST_VERIFY (fcntl (i, F_GETFL) > -1);

  support_descriptors_check (descrs2);
  support_descriptors_free (descrs2);

  TEST_COMPARE (close_range (lowfd, lowfd + NFDS, 0), 0);

  support_descriptors_check (descrs1);
  support_descriptors_free (descrs1);
}
#endif

static bool
is_in_array (int *arr, size_t len, int fd)
{
  bool r = false;
  for (int i = 0; i < len; i++)
    if (arr[i] == fd)
      return true;
  return r;
}

static void
close_range_cloexec_test (void)
{
  /* Check if the temporary file descriptor has no no gaps.  */
  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);

  const int maximum_fd = lowfd + NFDS - 1;
  const int half_fd = lowfd + NFDS / 2;
  const int gap_1 = maximum_fd - 8;

  /* Close half of the descriptors and check result.  */
  int r = close_range (lowfd, half_fd, CLOSE_RANGE_CLOEXEC);
  if (r == -1 && errno == EINVAL)
    {
      printf ("%s: CLOSE_RANGE_CLOEXEC not supported\n", __func__);
      return;
    }
  for (int i = lowfd; i <= half_fd; i++)
    {
      int flags = fcntl (i, F_GETFD);
      TEST_VERIFY (flags > -1);
      TEST_COMPARE (flags & FD_CLOEXEC, FD_CLOEXEC);
    }
  for (int i = half_fd + 1; i < maximum_fd; i++)
    TEST_VERIFY (fcntl (i, F_GETFL) > -1);

  /* Create some gaps, close up to a threshold, and check result.  */
  static int gap_close[] = { 57, 78, 81, 82, 84, 90 };
  for (int i = 0; i < array_length (gap_close); i++)
    xclose (lowfd + gap_close[i]);

  TEST_COMPARE (close_range (half_fd + 1, gap_1, CLOSE_RANGE_CLOEXEC), 0);
  for (int i = half_fd + 1; i < gap_1; i++)
    {
      int flags = fcntl (i, F_GETFD);
      if (is_in_array (gap_close, array_length (gap_close), i - lowfd))
        TEST_COMPARE (flags, -1);
      else
        {
          TEST_VERIFY (flags > -1);
          TEST_COMPARE (flags & FD_CLOEXEC, FD_CLOEXEC);
        }
    }
  for (int i = gap_1 + 1; i < maximum_fd; i++)
    TEST_VERIFY (fcntl (i, F_GETFL) > -1);

  /* Close the remaining but the last one.  */
  TEST_COMPARE (close_range (gap_1 + 1, maximum_fd - 1, CLOSE_RANGE_CLOEXEC),
                0);
  for (int i = gap_1 + 1; i < maximum_fd - 1; i++)
    {
      int flags = fcntl (i, F_GETFD);
      TEST_VERIFY (flags > -1);
      TEST_COMPARE (flags & FD_CLOEXEC, FD_CLOEXEC);
    }
  TEST_VERIFY (fcntl (maximum_fd, F_GETFL) > -1);

  /* Close the last one.  */
  TEST_COMPARE (close_range (maximum_fd, maximum_fd, CLOSE_RANGE_CLOEXEC), 0);
  {
    int flags = fcntl (maximum_fd, F_GETFD);
    TEST_VERIFY (flags > -1);
    TEST_COMPARE (flags & FD_CLOEXEC, FD_CLOEXEC);
  }
}

static int
do_test (void)
{
  close_range_test_max_upper_limit ();
  close_range_test ();
#ifdef __linux__
  close_range_test_subprocess ();
#endif
#ifdef CLOSE_RANGE_UNSHARE
  close_range_unshare_test ();
#endif
  close_range_cloexec_test ();

  return 0;
}

#include <support/test-driver.c>