2 * Copyright (c) 2009 Advanced Computing Technologies LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Regression tests for the closefrom(2) system call.
35 #include <sys/param.h>
60 printf("ok %d - %s\n", test, descr);
65 fail(const char *descr, const char *fmt, ...)
69 printf("not ok %d - %s", test, descr);
81 #define fail_err(descr) fail((descr), "%s", strerror(errno))
84 cok(struct shared_info *info, const char *descr)
88 strlcpy(info->tag, descr, sizeof(info->tag));
93 cfail(struct shared_info *info, const char *descr, const char *fmt, ...)
98 strlcpy(info->tag, descr, sizeof(info->tag));
101 vsprintf(info->message, fmt, ap);
107 #define cfail_err(info, descr) cfail((info), (descr), "%s", strerror(errno))
110 * Use kinfo_getfile() to fetch the list of file descriptors and figure out
111 * the highest open file descriptor.
116 struct kinfo_file *kif;
119 kif = kinfo_getfile(getpid(), &cnt);
121 fail_err("kinfo_getfile");
123 for (i = 0; i < cnt; i++)
124 if (kif[i].kf_fd > highest)
125 highest = kif[i].kf_fd;
135 fd = open("/dev/null", O_RDONLY);
137 fail_err("open(\"/dev/null\")");
142 main(int __unused argc, char __unused *argv[])
144 struct shared_info *info;
150 /* We better start up with fd's 0, 1, and 2 open. */
153 fail("open", "bad descriptor %d", fd);
156 /* Make sure highest_fd() works. */
159 fail("highest_fd", "bad descriptor %d", fd);
162 /* Try to use closefrom() for just closing fd 3. */
166 fail("closefrom", "highest fd %d", fd);
169 /* Eat up 16 descriptors. */
170 for (i = 0; i < 16; i++)
174 fail("open 16", "highest fd %d", fd);
177 /* Close half of them. */
181 fail("closefrom", "highest fd %d", fd);
184 /* Explicitly close descriptors 6 and 8 to create holes. */
185 if (close(6) < 0 || close(8) < 0)
189 /* Verify that close on 6 and 8 fails with EBADF. */
191 fail("close(6)", "did not fail");
193 fail_err("close(6)");
196 fail("close(8)", "did not fail");
198 fail_err("close(8)");
201 /* Close from 4 on. */
205 fail("closefrom", "highest fd %d", fd);
208 /* Allocate a small SHM region for IPC with our child. */
209 info = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANON |
211 if (info == MAP_FAILED)
215 /* Fork a child process to test closefrom(0). */
224 cfail(info, "closefrom(0)", "highest fd %d", fd);
225 cok(info, "closefrom(0)");
230 fail(info->tag, "%s", info->message);
233 /* Fork a child process to test closefrom(-1). */
242 cfail(info, "closefrom(-1)", "highest fd %d", fd);
243 cok(info, "closefrom(-1)");
248 fail(info->tag, "%s", info->message);
251 /* Dup stdout to 6. */
256 fail("dup2", "highest fd %d", fd);
259 /* Do a closefrom() starting in a hole. */
263 fail("closefrom", "highest fd %d", fd);
266 /* Do a closefrom() beyond our highest open fd. */
270 fail("closefrom", "highest fd %d", fd);