tools/llvm: Do not build with symbols
[minix3.git] / tests / lib / libc / gen / t_closefrom.c
blob75ebabb75626b014de942866b84cd2376c76dcb0
1 /* $NetBSD: t_closefrom.c,v 1.4 2011/05/11 08:11:36 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_closefrom.c,v 1.4 2011/05/11 08:11:36 jruoho Exp $");
34 #include <sys/wait.h>
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <unistd.h>
42 static const char path[] = "closefrom";
44 ATF_TC_WITH_CLEANUP(closefrom_basic);
45 ATF_TC_HEAD(closefrom_basic, tc)
47 atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #1");
50 ATF_TC_BODY(closefrom_basic, tc)
52 int fd, cur1, cur2;
54 (void)closefrom(STDERR_FILENO + 1);
56 fd = open(path, O_RDONLY | O_CREAT, 0400);
57 ATF_REQUIRE(fd >= 0);
59 cur1 = fcntl(0, F_MAXFD);
61 ATF_REQUIRE(cur1 == STDERR_FILENO + 1);
62 ATF_REQUIRE(closefrom(cur1) == 0);
64 cur2 = fcntl(0, F_MAXFD);
66 ATF_REQUIRE(cur1 - 1 == cur2);
67 ATF_REQUIRE(close(fd) == -1);
68 ATF_REQUIRE(unlink(path) == 0);
71 ATF_TC_CLEANUP(closefrom_basic, tc)
73 (void)unlink(path);
76 ATF_TC_WITH_CLEANUP(closefrom_buffer);
77 ATF_TC_HEAD(closefrom_buffer, tc)
79 atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #2");
82 ATF_TC_BODY(closefrom_buffer, tc)
84 int buf[16], cur, half;
85 size_t i;
88 * Open a buffer of descriptors, close the half of
89 * these and verify that the result is consistent.
91 ATF_REQUIRE(closefrom(STDERR_FILENO + 1) == 0);
93 cur = fcntl(0, F_MAXFD);
94 ATF_REQUIRE(cur == STDERR_FILENO);
96 for (i = 0; i < __arraycount(buf); i++) {
97 buf[i] = open(path, O_RDWR | O_CREAT, 0600);
98 ATF_REQUIRE(buf[i] >= 0);
101 cur = fcntl(0, F_MAXFD);
102 ATF_REQUIRE(cur == __arraycount(buf) + STDERR_FILENO);
104 half = STDERR_FILENO + __arraycount(buf) / 2;
105 ATF_REQUIRE(closefrom(half) == 0);
107 cur = fcntl(0, F_MAXFD);
108 ATF_REQUIRE(cur == half - 1);
110 for (i = 0; i < __arraycount(buf); i++)
111 (void)close(buf[i]);
114 ATF_TC_CLEANUP(closefrom_buffer, tc)
116 (void)unlink(path);
119 ATF_TC(closefrom_err);
120 ATF_TC_HEAD(closefrom_err, tc)
122 atf_tc_set_md_var(tc, "descr", "Test errors from closefrom(3)");
125 ATF_TC_BODY(closefrom_err, tc)
128 errno = 0;
129 ATF_REQUIRE_ERRNO(EBADF, closefrom(-INT_MAX) == -1);
132 ATF_TC(closefrom_one);
133 ATF_TC_HEAD(closefrom_one, tc)
135 atf_tc_set_md_var(tc, "descr", "Test closefrom(1)");
138 ATF_TC_BODY(closefrom_one, tc)
140 pid_t pid;
141 int sta;
143 pid = fork();
144 ATF_REQUIRE(pid >= 0);
146 if (pid == 0) {
148 if (closefrom(1) != 0)
149 _exit(10);
151 _exit(fcntl(0, F_MAXFD));
155 (void)wait(&sta);
158 * STDIN_FILENO sould still be open; WEXITSTATUS(1) == 0.
160 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != 0)
161 atf_tc_fail("not all descriptors were closed");
164 ATF_TP_ADD_TCS(tp)
167 ATF_TP_ADD_TC(tp, closefrom_basic);
168 ATF_TP_ADD_TC(tp, closefrom_buffer);
169 ATF_TP_ADD_TC(tp, closefrom_err);
170 ATF_TP_ADD_TC(tp, closefrom_one);
172 return atf_no_error();