1 /* $NetBSD: t_closefrom.c,v 1.4 2011/05/11 08:11:36 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
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.
18 *
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.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_closefrom.c,v 1.4 2011/05/11 08:11:36 jruoho Exp $");
33
34 #include <sys/wait.h>
35
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <unistd.h>
41
42 static const char path[] = "closefrom";
43
44 ATF_TC_WITH_CLEANUP(closefrom_basic);
ATF_TC_HEAD(closefrom_basic,tc)45 ATF_TC_HEAD(closefrom_basic, tc)
46 {
47 atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #1");
48 }
49
ATF_TC_BODY(closefrom_basic,tc)50 ATF_TC_BODY(closefrom_basic, tc)
51 {
52 int fd, cur1, cur2;
53
54 (void)closefrom(STDERR_FILENO + 1);
55
56 fd = open(path, O_RDONLY | O_CREAT, 0400);
57 ATF_REQUIRE(fd >= 0);
58
59 cur1 = fcntl(0, F_MAXFD);
60
61 ATF_REQUIRE(cur1 == STDERR_FILENO + 1);
62 ATF_REQUIRE(closefrom(cur1) == 0);
63
64 cur2 = fcntl(0, F_MAXFD);
65
66 ATF_REQUIRE(cur1 - 1 == cur2);
67 ATF_REQUIRE(close(fd) == -1);
68 ATF_REQUIRE(unlink(path) == 0);
69 }
70
ATF_TC_CLEANUP(closefrom_basic,tc)71 ATF_TC_CLEANUP(closefrom_basic, tc)
72 {
73 (void)unlink(path);
74 }
75
76 ATF_TC_WITH_CLEANUP(closefrom_buffer);
ATF_TC_HEAD(closefrom_buffer,tc)77 ATF_TC_HEAD(closefrom_buffer, tc)
78 {
79 atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #2");
80 }
81
ATF_TC_BODY(closefrom_buffer,tc)82 ATF_TC_BODY(closefrom_buffer, tc)
83 {
84 int buf[16], cur, half;
85 size_t i;
86
87 /*
88 * Open a buffer of descriptors, close the half of
89 * these and verify that the result is consistent.
90 */
91 ATF_REQUIRE(closefrom(STDERR_FILENO + 1) == 0);
92
93 cur = fcntl(0, F_MAXFD);
94 ATF_REQUIRE(cur == STDERR_FILENO);
95
96 for (i = 0; i < __arraycount(buf); i++) {
97 buf[i] = open(path, O_RDWR | O_CREAT, 0600);
98 ATF_REQUIRE(buf[i] >= 0);
99 }
100
101 cur = fcntl(0, F_MAXFD);
102 ATF_REQUIRE(cur == __arraycount(buf) + STDERR_FILENO);
103
104 half = STDERR_FILENO + __arraycount(buf) / 2;
105 ATF_REQUIRE(closefrom(half) == 0);
106
107 cur = fcntl(0, F_MAXFD);
108 ATF_REQUIRE(cur == half - 1);
109
110 for (i = 0; i < __arraycount(buf); i++)
111 (void)close(buf[i]);
112 }
113
ATF_TC_CLEANUP(closefrom_buffer,tc)114 ATF_TC_CLEANUP(closefrom_buffer, tc)
115 {
116 (void)unlink(path);
117 }
118
119 ATF_TC(closefrom_err);
ATF_TC_HEAD(closefrom_err,tc)120 ATF_TC_HEAD(closefrom_err, tc)
121 {
122 atf_tc_set_md_var(tc, "descr", "Test errors from closefrom(3)");
123 }
124
ATF_TC_BODY(closefrom_err,tc)125 ATF_TC_BODY(closefrom_err, tc)
126 {
127
128 errno = 0;
129 ATF_REQUIRE_ERRNO(EBADF, closefrom(-INT_MAX) == -1);
130 }
131
132 ATF_TC(closefrom_one);
ATF_TC_HEAD(closefrom_one,tc)133 ATF_TC_HEAD(closefrom_one, tc)
134 {
135 atf_tc_set_md_var(tc, "descr", "Test closefrom(1)");
136 }
137
ATF_TC_BODY(closefrom_one,tc)138 ATF_TC_BODY(closefrom_one, tc)
139 {
140 pid_t pid;
141 int sta;
142
143 pid = fork();
144 ATF_REQUIRE(pid >= 0);
145
146 if (pid == 0) {
147
148 if (closefrom(1) != 0)
149 _exit(10);
150
151 _exit(fcntl(0, F_MAXFD));
152 }
153
154
155 (void)wait(&sta);
156
157 /*
158 * STDIN_FILENO sould still be open; WEXITSTATUS(1) == 0.
159 */
160 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != 0)
161 atf_tc_fail("not all descriptors were closed");
162 }
163
ATF_TP_ADD_TCS(tp)164 ATF_TP_ADD_TCS(tp)
165 {
166
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);
171
172 return atf_no_error();
173 }
174