xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_mkfifo.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /* $NetBSD: t_mkfifo.c,v 1.2 2011/11/02 06:04:48 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_mkfifo.c,v 1.2 2011/11/02 06:04:48 jruoho Exp $");
33*57718be8SEnji Cooper 
34*57718be8SEnji Cooper #include <sys/stat.h>
35*57718be8SEnji Cooper #include <sys/wait.h>
36*57718be8SEnji Cooper 
37*57718be8SEnji Cooper #include <atf-c.h>
38*57718be8SEnji Cooper #include <errno.h>
39*57718be8SEnji Cooper #include <fcntl.h>
40*57718be8SEnji Cooper #include <limits.h>
41*57718be8SEnji Cooper #include <stdlib.h>
42*57718be8SEnji Cooper #include <signal.h>
43*57718be8SEnji Cooper #include <string.h>
44*57718be8SEnji Cooper #include <unistd.h>
45*57718be8SEnji Cooper 
46*57718be8SEnji Cooper static const char	path[] = "fifo";
47*57718be8SEnji Cooper static void		support(void);
48*57718be8SEnji Cooper 
49*57718be8SEnji Cooper static void
support(void)50*57718be8SEnji Cooper support(void)
51*57718be8SEnji Cooper {
52*57718be8SEnji Cooper 
53*57718be8SEnji Cooper 	errno = 0;
54*57718be8SEnji Cooper 
55*57718be8SEnji Cooper 	if (mkfifo(path, 0600) == 0) {
56*57718be8SEnji Cooper 		ATF_REQUIRE(unlink(path) == 0);
57*57718be8SEnji Cooper 		return;
58*57718be8SEnji Cooper 	}
59*57718be8SEnji Cooper 
60*57718be8SEnji Cooper 	if (errno == EOPNOTSUPP)
61*57718be8SEnji Cooper 		atf_tc_skip("the kernel does not support FIFOs");
62*57718be8SEnji Cooper 	else {
63*57718be8SEnji Cooper 		atf_tc_fail("mkfifo(2) failed");
64*57718be8SEnji Cooper 	}
65*57718be8SEnji Cooper }
66*57718be8SEnji Cooper 
67*57718be8SEnji Cooper ATF_TC_WITH_CLEANUP(mkfifo_block);
ATF_TC_HEAD(mkfifo_block,tc)68*57718be8SEnji Cooper ATF_TC_HEAD(mkfifo_block, tc)
69*57718be8SEnji Cooper {
70*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test that FIFOs block");
71*57718be8SEnji Cooper }
72*57718be8SEnji Cooper 
ATF_TC_BODY(mkfifo_block,tc)73*57718be8SEnji Cooper ATF_TC_BODY(mkfifo_block, tc)
74*57718be8SEnji Cooper {
75*57718be8SEnji Cooper 	int sta, fd = -1;
76*57718be8SEnji Cooper 	pid_t pid;
77*57718be8SEnji Cooper 
78*57718be8SEnji Cooper 	support();
79*57718be8SEnji Cooper 
80*57718be8SEnji Cooper 	ATF_REQUIRE(mkfifo(path, 0600) == 0);
81*57718be8SEnji Cooper 
82*57718be8SEnji Cooper 	pid = fork();
83*57718be8SEnji Cooper 	ATF_REQUIRE(pid >= 0);
84*57718be8SEnji Cooper 
85*57718be8SEnji Cooper 	if (pid == 0) {
86*57718be8SEnji Cooper 
87*57718be8SEnji Cooper 		/*
88*57718be8SEnji Cooper 		 * If we open the FIFO as read-only (write-only),
89*57718be8SEnji Cooper 		 * the call should block until another process
90*57718be8SEnji Cooper 		 * opens the FIFO for writing (reading).
91*57718be8SEnji Cooper 		 */
92*57718be8SEnji Cooper 		fd = open(path, O_RDONLY);
93*57718be8SEnji Cooper 
94*57718be8SEnji Cooper 		_exit(EXIT_FAILURE); /* NOTREACHED */
95*57718be8SEnji Cooper 	}
96*57718be8SEnji Cooper 
97*57718be8SEnji Cooper 	(void)sleep(1);
98*57718be8SEnji Cooper 
99*57718be8SEnji Cooper 	ATF_REQUIRE(kill(pid, SIGKILL) == 0);
100*57718be8SEnji Cooper 
101*57718be8SEnji Cooper 	(void)wait(&sta);
102*57718be8SEnji Cooper 
103*57718be8SEnji Cooper 	if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
104*57718be8SEnji Cooper 		atf_tc_fail("FIFO did not block");
105*57718be8SEnji Cooper 
106*57718be8SEnji Cooper 	(void)close(fd);
107*57718be8SEnji Cooper 	(void)unlink(path);
108*57718be8SEnji Cooper }
109*57718be8SEnji Cooper 
ATF_TC_CLEANUP(mkfifo_block,tc)110*57718be8SEnji Cooper ATF_TC_CLEANUP(mkfifo_block, tc)
111*57718be8SEnji Cooper {
112*57718be8SEnji Cooper 	(void)unlink(path);
113*57718be8SEnji Cooper }
114*57718be8SEnji Cooper 
115*57718be8SEnji Cooper ATF_TC_WITH_CLEANUP(mkfifo_err);
ATF_TC_HEAD(mkfifo_err,tc)116*57718be8SEnji Cooper ATF_TC_HEAD(mkfifo_err, tc)
117*57718be8SEnji Cooper {
118*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test erros from mkfifo(2)");
119*57718be8SEnji Cooper }
120*57718be8SEnji Cooper 
ATF_TC_BODY(mkfifo_err,tc)121*57718be8SEnji Cooper ATF_TC_BODY(mkfifo_err, tc)
122*57718be8SEnji Cooper {
123*57718be8SEnji Cooper 	char buf[PATH_MAX + 1];
124*57718be8SEnji Cooper 
125*57718be8SEnji Cooper 	support();
126*57718be8SEnji Cooper 
127*57718be8SEnji Cooper 	(void)memset(buf, 'x', sizeof(buf));
128*57718be8SEnji Cooper 	ATF_REQUIRE(mkfifo(path, 0600) == 0);
129*57718be8SEnji Cooper 
130*57718be8SEnji Cooper 	errno = 0;
131*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(EFAULT, mkfifo((char *)-1, 0600) == -1);
132*57718be8SEnji Cooper 
133*57718be8SEnji Cooper 	errno = 0;
134*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(EEXIST, mkfifo("/etc/passwd", 0600) == -1);
135*57718be8SEnji Cooper 
136*57718be8SEnji Cooper 	errno = 0;
137*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(EEXIST, mkfifo(path, 0600) == -1);
138*57718be8SEnji Cooper 
139*57718be8SEnji Cooper 	errno = 0;
140*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, mkfifo(buf, 0600) == -1);
141*57718be8SEnji Cooper 
142*57718be8SEnji Cooper 	errno = 0;
143*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(ENOENT, mkfifo("/a/b/c/d/e/f/g", 0600) == -1);
144*57718be8SEnji Cooper 
145*57718be8SEnji Cooper 	ATF_REQUIRE(unlink(path) == 0);
146*57718be8SEnji Cooper }
147*57718be8SEnji Cooper 
ATF_TC_CLEANUP(mkfifo_err,tc)148*57718be8SEnji Cooper ATF_TC_CLEANUP(mkfifo_err, tc)
149*57718be8SEnji Cooper {
150*57718be8SEnji Cooper 	(void)unlink(path);
151*57718be8SEnji Cooper }
152*57718be8SEnji Cooper 
153*57718be8SEnji Cooper ATF_TC_WITH_CLEANUP(mkfifo_nonblock);
ATF_TC_HEAD(mkfifo_nonblock,tc)154*57718be8SEnji Cooper ATF_TC_HEAD(mkfifo_nonblock, tc)
155*57718be8SEnji Cooper {
156*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test O_NONBLOCK with FIFOs");
157*57718be8SEnji Cooper }
158*57718be8SEnji Cooper 
ATF_TC_BODY(mkfifo_nonblock,tc)159*57718be8SEnji Cooper ATF_TC_BODY(mkfifo_nonblock, tc)
160*57718be8SEnji Cooper {
161*57718be8SEnji Cooper 	int fd, sta;
162*57718be8SEnji Cooper 	pid_t pid;
163*57718be8SEnji Cooper 
164*57718be8SEnji Cooper 	support();
165*57718be8SEnji Cooper 
166*57718be8SEnji Cooper 	fd = -1;
167*57718be8SEnji Cooper 	ATF_REQUIRE(mkfifo(path, 0600) == 0);
168*57718be8SEnji Cooper 
169*57718be8SEnji Cooper 	pid = fork();
170*57718be8SEnji Cooper 	ATF_REQUIRE(pid >= 0);
171*57718be8SEnji Cooper 
172*57718be8SEnji Cooper 	if (pid == 0) {
173*57718be8SEnji Cooper 
174*57718be8SEnji Cooper 		/*
175*57718be8SEnji Cooper 		 * If we open the FIFO as O_NONBLOCK, the O_RDONLY
176*57718be8SEnji Cooper 		 * call should return immediately, whereas the call
177*57718be8SEnji Cooper 		 * for write-only should fail with ENXIO.
178*57718be8SEnji Cooper 		 */
179*57718be8SEnji Cooper 		fd = open(path, O_RDONLY | O_NONBLOCK);
180*57718be8SEnji Cooper 
181*57718be8SEnji Cooper 		if (fd >= 0)
182*57718be8SEnji Cooper 			_exit(EXIT_SUCCESS);
183*57718be8SEnji Cooper 
184*57718be8SEnji Cooper 		(void)pause();	/* NOTREACHED */
185*57718be8SEnji Cooper 	}
186*57718be8SEnji Cooper 
187*57718be8SEnji Cooper 	(void)sleep(1);
188*57718be8SEnji Cooper 
189*57718be8SEnji Cooper 	errno = 0;
190*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(ENXIO, open(path, O_WRONLY | O_NONBLOCK) == -1);
191*57718be8SEnji Cooper 
192*57718be8SEnji Cooper 	(void)kill(pid, SIGKILL);
193*57718be8SEnji Cooper 	(void)wait(&sta);
194*57718be8SEnji Cooper 
195*57718be8SEnji Cooper 	if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL)
196*57718be8SEnji Cooper 		atf_tc_fail("FIFO blocked for O_NONBLOCK open(2)");
197*57718be8SEnji Cooper 
198*57718be8SEnji Cooper 	(void)close(fd);
199*57718be8SEnji Cooper 	(void)unlink(path);
200*57718be8SEnji Cooper }
201*57718be8SEnji Cooper 
ATF_TC_CLEANUP(mkfifo_nonblock,tc)202*57718be8SEnji Cooper ATF_TC_CLEANUP(mkfifo_nonblock, tc)
203*57718be8SEnji Cooper {
204*57718be8SEnji Cooper 	(void)unlink(path);
205*57718be8SEnji Cooper }
206*57718be8SEnji Cooper 
207*57718be8SEnji Cooper ATF_TC_WITH_CLEANUP(mkfifo_perm);
ATF_TC_HEAD(mkfifo_perm,tc)208*57718be8SEnji Cooper ATF_TC_HEAD(mkfifo_perm, tc)
209*57718be8SEnji Cooper {
210*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test permissions with mkfifo(2)");
211*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
212*57718be8SEnji Cooper }
213*57718be8SEnji Cooper 
ATF_TC_BODY(mkfifo_perm,tc)214*57718be8SEnji Cooper ATF_TC_BODY(mkfifo_perm, tc)
215*57718be8SEnji Cooper {
216*57718be8SEnji Cooper 
217*57718be8SEnji Cooper 	support();
218*57718be8SEnji Cooper 
219*57718be8SEnji Cooper 	errno = 0;
220*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(EACCES, mkfifo("/root/fifo", 0600) == -1);
221*57718be8SEnji Cooper 
222*57718be8SEnji Cooper 	ATF_REQUIRE(mkfifo(path, 0600) == 0);
223*57718be8SEnji Cooper 
224*57718be8SEnji Cooper 	/*
225*57718be8SEnji Cooper 	 * For some reason this fails with EFTYPE...
226*57718be8SEnji Cooper 	 */
227*57718be8SEnji Cooper 	errno = 0;
228*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(EFTYPE, chmod(path, 1777) == -1);
229*57718be8SEnji Cooper 
230*57718be8SEnji Cooper 	ATF_REQUIRE(unlink(path) == 0);
231*57718be8SEnji Cooper }
232*57718be8SEnji Cooper 
ATF_TC_CLEANUP(mkfifo_perm,tc)233*57718be8SEnji Cooper ATF_TC_CLEANUP(mkfifo_perm, tc)
234*57718be8SEnji Cooper {
235*57718be8SEnji Cooper 	(void)unlink(path);
236*57718be8SEnji Cooper }
237*57718be8SEnji Cooper 
238*57718be8SEnji Cooper ATF_TC_WITH_CLEANUP(mkfifo_stat);
ATF_TC_HEAD(mkfifo_stat,tc)239*57718be8SEnji Cooper ATF_TC_HEAD(mkfifo_stat, tc)
240*57718be8SEnji Cooper {
241*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test mkfifo(2) with stat");
242*57718be8SEnji Cooper }
243*57718be8SEnji Cooper 
ATF_TC_BODY(mkfifo_stat,tc)244*57718be8SEnji Cooper ATF_TC_BODY(mkfifo_stat, tc)
245*57718be8SEnji Cooper {
246*57718be8SEnji Cooper 	struct stat st;
247*57718be8SEnji Cooper 
248*57718be8SEnji Cooper 	support();
249*57718be8SEnji Cooper 
250*57718be8SEnji Cooper 	(void)memset(&st, 0, sizeof(struct stat));
251*57718be8SEnji Cooper 
252*57718be8SEnji Cooper 	ATF_REQUIRE(mkfifo(path, 0600) == 0);
253*57718be8SEnji Cooper 	ATF_REQUIRE(stat(path, &st) == 0);
254*57718be8SEnji Cooper 
255*57718be8SEnji Cooper 	if (S_ISFIFO(st.st_mode) == 0)
256*57718be8SEnji Cooper 		atf_tc_fail("invalid mode from mkfifo(2)");
257*57718be8SEnji Cooper 
258*57718be8SEnji Cooper 	ATF_REQUIRE(unlink(path) == 0);
259*57718be8SEnji Cooper }
260*57718be8SEnji Cooper 
ATF_TC_CLEANUP(mkfifo_stat,tc)261*57718be8SEnji Cooper ATF_TC_CLEANUP(mkfifo_stat, tc)
262*57718be8SEnji Cooper {
263*57718be8SEnji Cooper 	(void)unlink(path);
264*57718be8SEnji Cooper }
265*57718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)266*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
267*57718be8SEnji Cooper {
268*57718be8SEnji Cooper 
269*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mkfifo_block);
270*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mkfifo_err);
271*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mkfifo_nonblock);
272*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mkfifo_perm);
273*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mkfifo_stat);
274*57718be8SEnji Cooper 
275*57718be8SEnji Cooper 	return atf_no_error();
276*57718be8SEnji Cooper }
277