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