t_dup.c revision 1.2
1/* $NetBSD: t_dup.c,v 1.2 2011/07/07 10:27:31 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_dup.c,v 1.2 2011/07/07 10:27:31 jruoho Exp $");
33
34#include <sys/resource.h>
35#include <sys/stat.h>
36#include <sys/wait.h>
37
38#include <atf-c.h>
39#include <errno.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45#include <sysexits.h>
46
47static char	 path[] = "dup";
48
49ATF_TC(dup_err);
50ATF_TC_HEAD(dup_err, tc)
51{
52	atf_tc_set_md_var(tc, "descr", "Test error conditions of dup(2)");
53}
54
55ATF_TC_BODY(dup_err, tc)
56{
57	ATF_REQUIRE(dup(-1) != 0);
58}
59
60ATF_TC_WITH_CLEANUP(dup_max);
61ATF_TC_HEAD(dup_max, tc)
62{
63	atf_tc_set_md_var(tc, "descr", "Test dup(2) against limits");
64}
65
66ATF_TC_BODY(dup_max, tc)
67{
68	struct rlimit res;
69	int *buf, fd, sta;
70	size_t i, n;
71	pid_t pid;
72
73	pid = fork();
74	ATF_REQUIRE(pid >= 0);
75
76	if (pid == 0) {
77
78		/*
79		 * Open a temporary file until the
80		 * maximum number of open files is
81		 * reached. Ater that dup(2) should
82		 * fail with EMFILE.
83		 */
84		(void)closefrom(0);
85		(void)memset(&res, 0, sizeof(struct rlimit));
86
87		if (getrlimit(RLIMIT_NOFILE, &res) != 0)
88			_exit(EX_OSERR);
89
90		if (res.rlim_cur == 0 || res.rlim_max == 0)
91			_exit(EX_OSERR);
92
93		n = res.rlim_cur;
94		buf = calloc(n, sizeof(int));
95
96		if (buf == NULL)
97			_exit(EX_OSERR);
98
99		buf[0] = mkstemp(path);
100
101		if (buf[0] < 0)
102			_exit(EX_OSERR);
103
104		for (i = 1; i < n; i++) {
105
106			buf[i] = open(path, O_RDONLY);
107
108			if (buf[i] < 0)
109				_exit(EX_OSERR);
110		}
111
112		errno = 0;
113		fd = dup(buf[0]);
114
115		if (fd != -1 || errno != EMFILE)
116			_exit(EX_DATAERR);
117
118		_exit(EXIT_SUCCESS);
119	}
120
121	(void)wait(&sta);
122
123	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
124
125		if (WEXITSTATUS(sta) == EX_OSERR)
126			atf_tc_fail("unknown error");
127
128		if (WEXITSTATUS(sta) == EX_DATAERR)
129			atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
130	}
131}
132
133ATF_TC_CLEANUP(dup_max, tc)
134{
135	(void)unlink(path);
136}
137
138ATF_TC_WITH_CLEANUP(dup_mode);
139ATF_TC_HEAD(dup_mode, tc)
140{
141	atf_tc_set_md_var(tc, "descr", "A basic test of dup(2)");
142}
143
144ATF_TC_BODY(dup_mode, tc)
145{
146	int mode[3] = { O_RDONLY, O_WRONLY, O_RDWR   };
147	int perm[5] = { 0700, 0400, 0600, 0444, 0666 };
148	struct stat st1, st2;
149	int fd1, fd2;
150	size_t i, j;
151
152	/*
153	 * Check that a duplicated descriptor
154	 * retains the mode of the original file.
155	 */
156	for (i = 0; i < __arraycount(mode); i++) {
157
158		for (j = 0; j < __arraycount(perm); j++) {
159
160			fd1 = open(path, mode[i] | O_CREAT, perm[j]);
161
162			if (fd1 < 0)
163				return;
164
165			fd2 = dup(fd1);
166			ATF_REQUIRE(fd2 >= 0);
167
168			(void)memset(&st1, 0, sizeof(struct stat));
169			(void)memset(&st2, 0, sizeof(struct stat));
170
171			ATF_REQUIRE(fstat(fd1, &st1) == 0);
172			ATF_REQUIRE(fstat(fd2, &st2) == 0);
173
174			if (st1.st_mode != st2.st_mode)
175				atf_tc_fail("invalid mode");
176
177			ATF_REQUIRE(close(fd1) == 0);
178			ATF_REQUIRE(close(fd2) == 0);
179			ATF_REQUIRE(unlink(path) == 0);
180		}
181	}
182}
183
184ATF_TC_CLEANUP(dup_mode, tc)
185{
186	(void)unlink(path);
187}
188
189ATF_TP_ADD_TCS(tp)
190{
191
192	ATF_TP_ADD_TC(tp, dup_err);
193	ATF_TP_ADD_TC(tp, dup_max);
194	ATF_TP_ADD_TC(tp, dup_mode);
195
196	return atf_no_error();
197}
198