stdlib: Add more qsort{_r} coverage
[glibc.git] / support / support-open-dev-null-range.c
blob8a8125660ec630da0f2f7bc83480447489911d1b
1 /* Return a range of open file descriptors.
2 Copyright (C) 2021-2023 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 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <support/support.h>
22 #include <support/check.h>
23 #include <support/xunistd.h>
24 #include <stdlib.h>
25 #include <sys/resource.h>
27 static void
28 increase_nofile (void)
30 struct rlimit rl;
31 if (getrlimit (RLIMIT_NOFILE, &rl) == -1)
32 FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
34 rl.rlim_cur += 128;
36 if (setrlimit (RLIMIT_NOFILE, &rl) == 1)
37 FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
40 static int
41 open_dev_null (int flags, mode_t mode)
43 int fd = open64 ("/dev/null", flags, mode);
44 if (fd >= 0)
45 return fd;
47 if (fd < 0 && errno != EMFILE)
48 FAIL_EXIT1 ("open64 (\"/dev/null\", 0x%x, 0%o): %m", flags, mode);
50 increase_nofile ();
52 return xopen ("/dev/null", flags, mode);
55 struct range
57 int lowfd;
58 size_t len;
61 struct range_list
63 size_t total;
64 size_t used;
65 struct range *ranges;
68 static void
69 range_init (struct range_list *r)
71 r->total = 8;
72 r->used = 0;
73 r->ranges = xmalloc (r->total * sizeof (struct range));
76 static void
77 range_add (struct range_list *r, int lowfd, size_t len)
79 if (r->used == r->total)
81 r->total *= 2;
82 r->ranges = xrealloc (r->ranges, r->total * sizeof (struct range));
84 r->ranges[r->used].lowfd = lowfd;
85 r->ranges[r->used].len = len;
86 r->used++;
89 static void
90 range_close (struct range_list *r)
92 for (size_t i = 0; i < r->used; i++)
94 int minfd = r->ranges[i].lowfd;
95 int maxfd = r->ranges[i].lowfd + r->ranges[i].len;
96 for (int fd = minfd; fd < maxfd; fd++)
97 xclose (fd);
99 free (r->ranges);
103 support_open_dev_null_range (int num, int flags, mode_t mode)
105 /* We keep track of the ranges that hit an already opened descriptor, so
106 we close them after we get a working range. */
107 struct range_list rl;
108 range_init (&rl);
110 int lowfd = open_dev_null (flags, mode);
111 int prevfd = lowfd;
112 while (true)
114 int i = 1;
115 for (; i < num; i++)
117 int fd = open_dev_null (flags, mode);
118 if (fd != lowfd + i)
120 range_add (&rl, lowfd, prevfd - lowfd + 1);
122 prevfd = lowfd = fd;
123 break;
125 prevfd = fd;
127 if (i == num)
128 break;
131 range_close (&rl);
133 return lowfd;