1272343Sngie/* $NetBSD: t_closefrom.c,v 1.4 2011/05/11 08:11:36 jruoho Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Jukka Ruohonen.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie#include <sys/cdefs.h>
32272343Sngie__RCSID("$NetBSD: t_closefrom.c,v 1.4 2011/05/11 08:11:36 jruoho Exp $");
33272343Sngie
34272343Sngie#include <sys/wait.h>
35272343Sngie
36272343Sngie#include <atf-c.h>
37272343Sngie#include <errno.h>
38272343Sngie#include <fcntl.h>
39272343Sngie#include <limits.h>
40272343Sngie#include <unistd.h>
41272343Sngie
42272343Sngiestatic const char  path[] = "closefrom";
43272343Sngie
44272343SngieATF_TC_WITH_CLEANUP(closefrom_basic);
45272343SngieATF_TC_HEAD(closefrom_basic, tc)
46272343Sngie{
47272343Sngie	atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #1");
48272343Sngie}
49272343Sngie
50272343SngieATF_TC_BODY(closefrom_basic, tc)
51272343Sngie{
52272343Sngie	int fd, cur1, cur2;
53272343Sngie
54272343Sngie	(void)closefrom(STDERR_FILENO + 1);
55272343Sngie
56272343Sngie	fd = open(path, O_RDONLY | O_CREAT, 0400);
57272343Sngie	ATF_REQUIRE(fd >= 0);
58272343Sngie
59272343Sngie	cur1 = fcntl(0, F_MAXFD);
60272343Sngie
61272343Sngie	ATF_REQUIRE(cur1 == STDERR_FILENO + 1);
62272343Sngie	ATF_REQUIRE(closefrom(cur1) == 0);
63272343Sngie
64272343Sngie	cur2 = fcntl(0, F_MAXFD);
65272343Sngie
66272343Sngie	ATF_REQUIRE(cur1 - 1 == cur2);
67272343Sngie	ATF_REQUIRE(close(fd) == -1);
68272343Sngie	ATF_REQUIRE(unlink(path) == 0);
69272343Sngie}
70272343Sngie
71272343SngieATF_TC_CLEANUP(closefrom_basic, tc)
72272343Sngie{
73272343Sngie	(void)unlink(path);
74272343Sngie}
75272343Sngie
76272343SngieATF_TC_WITH_CLEANUP(closefrom_buffer);
77272343SngieATF_TC_HEAD(closefrom_buffer, tc)
78272343Sngie{
79272343Sngie	atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #2");
80272343Sngie}
81272343Sngie
82272343SngieATF_TC_BODY(closefrom_buffer, tc)
83272343Sngie{
84272343Sngie	int buf[16], cur, half;
85272343Sngie	size_t i;
86272343Sngie
87272343Sngie	/*
88272343Sngie	 * Open a buffer of descriptors, close the half of
89272343Sngie	 * these and verify that the result is consistent.
90272343Sngie	 */
91272343Sngie	ATF_REQUIRE(closefrom(STDERR_FILENO + 1) == 0);
92272343Sngie
93272343Sngie	cur = fcntl(0, F_MAXFD);
94272343Sngie	ATF_REQUIRE(cur == STDERR_FILENO);
95272343Sngie
96272343Sngie	for (i = 0; i < __arraycount(buf); i++) {
97272343Sngie		buf[i] = open(path, O_RDWR | O_CREAT, 0600);
98272343Sngie		ATF_REQUIRE(buf[i] >= 0);
99272343Sngie	}
100272343Sngie
101272343Sngie	cur = fcntl(0, F_MAXFD);
102272343Sngie	ATF_REQUIRE(cur == __arraycount(buf) + STDERR_FILENO);
103272343Sngie
104272343Sngie	half = STDERR_FILENO + __arraycount(buf) / 2;
105272343Sngie	ATF_REQUIRE(closefrom(half) == 0);
106272343Sngie
107272343Sngie	cur = fcntl(0, F_MAXFD);
108272343Sngie	ATF_REQUIRE(cur == half - 1);
109272343Sngie
110272343Sngie	for (i = 0; i < __arraycount(buf); i++)
111272343Sngie		(void)close(buf[i]);
112272343Sngie}
113272343Sngie
114272343SngieATF_TC_CLEANUP(closefrom_buffer, tc)
115272343Sngie{
116272343Sngie	(void)unlink(path);
117272343Sngie}
118272343Sngie
119272343SngieATF_TC(closefrom_err);
120272343SngieATF_TC_HEAD(closefrom_err, tc)
121272343Sngie{
122272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from closefrom(3)");
123272343Sngie}
124272343Sngie
125272343SngieATF_TC_BODY(closefrom_err, tc)
126272343Sngie{
127272343Sngie
128272343Sngie	errno = 0;
129272343Sngie	ATF_REQUIRE_ERRNO(EBADF, closefrom(-INT_MAX) == -1);
130272343Sngie}
131272343Sngie
132272343SngieATF_TC(closefrom_one);
133272343SngieATF_TC_HEAD(closefrom_one, tc)
134272343Sngie{
135272343Sngie	atf_tc_set_md_var(tc, "descr", "Test closefrom(1)");
136272343Sngie}
137272343Sngie
138272343SngieATF_TC_BODY(closefrom_one, tc)
139272343Sngie{
140272343Sngie	pid_t pid;
141272343Sngie	int sta;
142272343Sngie
143272343Sngie	pid = fork();
144272343Sngie	ATF_REQUIRE(pid >= 0);
145272343Sngie
146272343Sngie	if (pid == 0) {
147272343Sngie
148272343Sngie		if (closefrom(1) != 0)
149272343Sngie			_exit(10);
150272343Sngie
151272343Sngie		_exit(fcntl(0, F_MAXFD));
152272343Sngie	}
153272343Sngie
154272343Sngie
155272343Sngie	(void)wait(&sta);
156272343Sngie
157272343Sngie	/*
158272343Sngie	 * STDIN_FILENO sould still be open; WEXITSTATUS(1) == 0.
159272343Sngie	 */
160272343Sngie	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != 0)
161272343Sngie		atf_tc_fail("not all descriptors were closed");
162272343Sngie}
163272343Sngie
164272343SngieATF_TP_ADD_TCS(tp)
165272343Sngie{
166272343Sngie
167272343Sngie	ATF_TP_ADD_TC(tp, closefrom_basic);
168272343Sngie	ATF_TP_ADD_TC(tp, closefrom_buffer);
169272343Sngie	ATF_TP_ADD_TC(tp, closefrom_err);
170272343Sngie	ATF_TP_ADD_TC(tp, closefrom_one);
171272343Sngie
172272343Sngie	return atf_no_error();
173272343Sngie}
174