1 /* $NetBSD: t_closefrom.c,v 1.4 2011/05/11 08:11:36 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 $");
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
)
54 (void)closefrom(STDERR_FILENO
+ 1);
56 fd
= open(path
, O_RDONLY
| O_CREAT
, 0400);
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
)
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
;
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
++)
114 ATF_TC_CLEANUP(closefrom_buffer
, tc
)
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
)
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
)
144 ATF_REQUIRE(pid
>= 0);
148 if (closefrom(1) != 0)
151 _exit(fcntl(0, F_MAXFD
));
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");
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();