1*11be35a1SLionel Sambuc /* $NetBSD: t_mkfifo.c,v 1.2 2011/11/02 06:04:48 jruoho Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*11be35a1SLionel Sambuc * by Jukka Ruohonen.
9*11be35a1SLionel Sambuc *
10*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc * are met:
13*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*11be35a1SLionel Sambuc *
19*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*11be35a1SLionel Sambuc */
31*11be35a1SLionel Sambuc #include <sys/cdefs.h>
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_mkfifo.c,v 1.2 2011/11/02 06:04:48 jruoho Exp $");
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <sys/stat.h>
35*11be35a1SLionel Sambuc #include <sys/wait.h>
36*11be35a1SLionel Sambuc
37*11be35a1SLionel Sambuc #include <atf-c.h>
38*11be35a1SLionel Sambuc #include <errno.h>
39*11be35a1SLionel Sambuc #include <fcntl.h>
40*11be35a1SLionel Sambuc #include <limits.h>
41*11be35a1SLionel Sambuc #include <stdlib.h>
42*11be35a1SLionel Sambuc #include <signal.h>
43*11be35a1SLionel Sambuc #include <string.h>
44*11be35a1SLionel Sambuc #include <unistd.h>
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc static const char path[] = "fifo";
47*11be35a1SLionel Sambuc static void support(void);
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc static void
support(void)50*11be35a1SLionel Sambuc support(void)
51*11be35a1SLionel Sambuc {
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc errno = 0;
54*11be35a1SLionel Sambuc
55*11be35a1SLionel Sambuc if (mkfifo(path, 0600) == 0) {
56*11be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
57*11be35a1SLionel Sambuc return;
58*11be35a1SLionel Sambuc }
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc if (errno == EOPNOTSUPP)
61*11be35a1SLionel Sambuc atf_tc_skip("the kernel does not support FIFOs");
62*11be35a1SLionel Sambuc else {
63*11be35a1SLionel Sambuc atf_tc_fail("mkfifo(2) failed");
64*11be35a1SLionel Sambuc }
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc
67*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(mkfifo_block);
ATF_TC_HEAD(mkfifo_block,tc)68*11be35a1SLionel Sambuc ATF_TC_HEAD(mkfifo_block, tc)
69*11be35a1SLionel Sambuc {
70*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test that FIFOs block");
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc
ATF_TC_BODY(mkfifo_block,tc)73*11be35a1SLionel Sambuc ATF_TC_BODY(mkfifo_block, tc)
74*11be35a1SLionel Sambuc {
75*11be35a1SLionel Sambuc int sta, fd = -1;
76*11be35a1SLionel Sambuc pid_t pid;
77*11be35a1SLionel Sambuc
78*11be35a1SLionel Sambuc support();
79*11be35a1SLionel Sambuc
80*11be35a1SLionel Sambuc ATF_REQUIRE(mkfifo(path, 0600) == 0);
81*11be35a1SLionel Sambuc
82*11be35a1SLionel Sambuc pid = fork();
83*11be35a1SLionel Sambuc ATF_REQUIRE(pid >= 0);
84*11be35a1SLionel Sambuc
85*11be35a1SLionel Sambuc if (pid == 0) {
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc /*
88*11be35a1SLionel Sambuc * If we open the FIFO as read-only (write-only),
89*11be35a1SLionel Sambuc * the call should block until another process
90*11be35a1SLionel Sambuc * opens the FIFO for writing (reading).
91*11be35a1SLionel Sambuc */
92*11be35a1SLionel Sambuc fd = open(path, O_RDONLY);
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc _exit(EXIT_FAILURE); /* NOTREACHED */
95*11be35a1SLionel Sambuc }
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc (void)sleep(1);
98*11be35a1SLionel Sambuc
99*11be35a1SLionel Sambuc ATF_REQUIRE(kill(pid, SIGKILL) == 0);
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc (void)wait(&sta);
102*11be35a1SLionel Sambuc
103*11be35a1SLionel Sambuc if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
104*11be35a1SLionel Sambuc atf_tc_fail("FIFO did not block");
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc (void)close(fd);
107*11be35a1SLionel Sambuc (void)unlink(path);
108*11be35a1SLionel Sambuc }
109*11be35a1SLionel Sambuc
ATF_TC_CLEANUP(mkfifo_block,tc)110*11be35a1SLionel Sambuc ATF_TC_CLEANUP(mkfifo_block, tc)
111*11be35a1SLionel Sambuc {
112*11be35a1SLionel Sambuc (void)unlink(path);
113*11be35a1SLionel Sambuc }
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(mkfifo_err);
ATF_TC_HEAD(mkfifo_err,tc)116*11be35a1SLionel Sambuc ATF_TC_HEAD(mkfifo_err, tc)
117*11be35a1SLionel Sambuc {
118*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test erros from mkfifo(2)");
119*11be35a1SLionel Sambuc }
120*11be35a1SLionel Sambuc
ATF_TC_BODY(mkfifo_err,tc)121*11be35a1SLionel Sambuc ATF_TC_BODY(mkfifo_err, tc)
122*11be35a1SLionel Sambuc {
123*11be35a1SLionel Sambuc char buf[PATH_MAX + 1];
124*11be35a1SLionel Sambuc
125*11be35a1SLionel Sambuc support();
126*11be35a1SLionel Sambuc
127*11be35a1SLionel Sambuc (void)memset(buf, 'x', sizeof(buf));
128*11be35a1SLionel Sambuc ATF_REQUIRE(mkfifo(path, 0600) == 0);
129*11be35a1SLionel Sambuc
130*11be35a1SLionel Sambuc errno = 0;
131*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EFAULT, mkfifo((char *)-1, 0600) == -1);
132*11be35a1SLionel Sambuc
133*11be35a1SLionel Sambuc errno = 0;
134*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EEXIST, mkfifo("/etc/passwd", 0600) == -1);
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc errno = 0;
137*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EEXIST, mkfifo(path, 0600) == -1);
138*11be35a1SLionel Sambuc
139*11be35a1SLionel Sambuc errno = 0;
140*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENAMETOOLONG, mkfifo(buf, 0600) == -1);
141*11be35a1SLionel Sambuc
142*11be35a1SLionel Sambuc errno = 0;
143*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENOENT, mkfifo("/a/b/c/d/e/f/g", 0600) == -1);
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
146*11be35a1SLionel Sambuc }
147*11be35a1SLionel Sambuc
ATF_TC_CLEANUP(mkfifo_err,tc)148*11be35a1SLionel Sambuc ATF_TC_CLEANUP(mkfifo_err, tc)
149*11be35a1SLionel Sambuc {
150*11be35a1SLionel Sambuc (void)unlink(path);
151*11be35a1SLionel Sambuc }
152*11be35a1SLionel Sambuc
153*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(mkfifo_nonblock);
ATF_TC_HEAD(mkfifo_nonblock,tc)154*11be35a1SLionel Sambuc ATF_TC_HEAD(mkfifo_nonblock, tc)
155*11be35a1SLionel Sambuc {
156*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test O_NONBLOCK with FIFOs");
157*11be35a1SLionel Sambuc }
158*11be35a1SLionel Sambuc
ATF_TC_BODY(mkfifo_nonblock,tc)159*11be35a1SLionel Sambuc ATF_TC_BODY(mkfifo_nonblock, tc)
160*11be35a1SLionel Sambuc {
161*11be35a1SLionel Sambuc int fd, sta;
162*11be35a1SLionel Sambuc pid_t pid;
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc support();
165*11be35a1SLionel Sambuc
166*11be35a1SLionel Sambuc fd = -1;
167*11be35a1SLionel Sambuc ATF_REQUIRE(mkfifo(path, 0600) == 0);
168*11be35a1SLionel Sambuc
169*11be35a1SLionel Sambuc pid = fork();
170*11be35a1SLionel Sambuc ATF_REQUIRE(pid >= 0);
171*11be35a1SLionel Sambuc
172*11be35a1SLionel Sambuc if (pid == 0) {
173*11be35a1SLionel Sambuc
174*11be35a1SLionel Sambuc /*
175*11be35a1SLionel Sambuc * If we open the FIFO as O_NONBLOCK, the O_RDONLY
176*11be35a1SLionel Sambuc * call should return immediately, whereas the call
177*11be35a1SLionel Sambuc * for write-only should fail with ENXIO.
178*11be35a1SLionel Sambuc */
179*11be35a1SLionel Sambuc fd = open(path, O_RDONLY | O_NONBLOCK);
180*11be35a1SLionel Sambuc
181*11be35a1SLionel Sambuc if (fd >= 0)
182*11be35a1SLionel Sambuc _exit(EXIT_SUCCESS);
183*11be35a1SLionel Sambuc
184*11be35a1SLionel Sambuc (void)pause(); /* NOTREACHED */
185*11be35a1SLionel Sambuc }
186*11be35a1SLionel Sambuc
187*11be35a1SLionel Sambuc (void)sleep(1);
188*11be35a1SLionel Sambuc
189*11be35a1SLionel Sambuc errno = 0;
190*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENXIO, open(path, O_WRONLY | O_NONBLOCK) == -1);
191*11be35a1SLionel Sambuc
192*11be35a1SLionel Sambuc (void)kill(pid, SIGKILL);
193*11be35a1SLionel Sambuc (void)wait(&sta);
194*11be35a1SLionel Sambuc
195*11be35a1SLionel Sambuc if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL)
196*11be35a1SLionel Sambuc atf_tc_fail("FIFO blocked for O_NONBLOCK open(2)");
197*11be35a1SLionel Sambuc
198*11be35a1SLionel Sambuc (void)close(fd);
199*11be35a1SLionel Sambuc (void)unlink(path);
200*11be35a1SLionel Sambuc }
201*11be35a1SLionel Sambuc
ATF_TC_CLEANUP(mkfifo_nonblock,tc)202*11be35a1SLionel Sambuc ATF_TC_CLEANUP(mkfifo_nonblock, tc)
203*11be35a1SLionel Sambuc {
204*11be35a1SLionel Sambuc (void)unlink(path);
205*11be35a1SLionel Sambuc }
206*11be35a1SLionel Sambuc
207*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(mkfifo_perm);
ATF_TC_HEAD(mkfifo_perm,tc)208*11be35a1SLionel Sambuc ATF_TC_HEAD(mkfifo_perm, tc)
209*11be35a1SLionel Sambuc {
210*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test permissions with mkfifo(2)");
211*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "require.user", "unprivileged");
212*11be35a1SLionel Sambuc }
213*11be35a1SLionel Sambuc
ATF_TC_BODY(mkfifo_perm,tc)214*11be35a1SLionel Sambuc ATF_TC_BODY(mkfifo_perm, tc)
215*11be35a1SLionel Sambuc {
216*11be35a1SLionel Sambuc
217*11be35a1SLionel Sambuc support();
218*11be35a1SLionel Sambuc
219*11be35a1SLionel Sambuc errno = 0;
220*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EACCES, mkfifo("/root/fifo", 0600) == -1);
221*11be35a1SLionel Sambuc
222*11be35a1SLionel Sambuc ATF_REQUIRE(mkfifo(path, 0600) == 0);
223*11be35a1SLionel Sambuc
224*11be35a1SLionel Sambuc /*
225*11be35a1SLionel Sambuc * For some reason this fails with EFTYPE...
226*11be35a1SLionel Sambuc */
227*11be35a1SLionel Sambuc errno = 0;
228*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EFTYPE, chmod(path, 1777) == -1);
229*11be35a1SLionel Sambuc
230*11be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
231*11be35a1SLionel Sambuc }
232*11be35a1SLionel Sambuc
ATF_TC_CLEANUP(mkfifo_perm,tc)233*11be35a1SLionel Sambuc ATF_TC_CLEANUP(mkfifo_perm, tc)
234*11be35a1SLionel Sambuc {
235*11be35a1SLionel Sambuc (void)unlink(path);
236*11be35a1SLionel Sambuc }
237*11be35a1SLionel Sambuc
238*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(mkfifo_stat);
ATF_TC_HEAD(mkfifo_stat,tc)239*11be35a1SLionel Sambuc ATF_TC_HEAD(mkfifo_stat, tc)
240*11be35a1SLionel Sambuc {
241*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test mkfifo(2) with stat");
242*11be35a1SLionel Sambuc }
243*11be35a1SLionel Sambuc
ATF_TC_BODY(mkfifo_stat,tc)244*11be35a1SLionel Sambuc ATF_TC_BODY(mkfifo_stat, tc)
245*11be35a1SLionel Sambuc {
246*11be35a1SLionel Sambuc struct stat st;
247*11be35a1SLionel Sambuc
248*11be35a1SLionel Sambuc support();
249*11be35a1SLionel Sambuc
250*11be35a1SLionel Sambuc (void)memset(&st, 0, sizeof(struct stat));
251*11be35a1SLionel Sambuc
252*11be35a1SLionel Sambuc ATF_REQUIRE(mkfifo(path, 0600) == 0);
253*11be35a1SLionel Sambuc ATF_REQUIRE(stat(path, &st) == 0);
254*11be35a1SLionel Sambuc
255*11be35a1SLionel Sambuc if (S_ISFIFO(st.st_mode) == 0)
256*11be35a1SLionel Sambuc atf_tc_fail("invalid mode from mkfifo(2)");
257*11be35a1SLionel Sambuc
258*11be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
259*11be35a1SLionel Sambuc }
260*11be35a1SLionel Sambuc
ATF_TC_CLEANUP(mkfifo_stat,tc)261*11be35a1SLionel Sambuc ATF_TC_CLEANUP(mkfifo_stat, tc)
262*11be35a1SLionel Sambuc {
263*11be35a1SLionel Sambuc (void)unlink(path);
264*11be35a1SLionel Sambuc }
265*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)266*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
267*11be35a1SLionel Sambuc {
268*11be35a1SLionel Sambuc
269*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, mkfifo_block);
270*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, mkfifo_err);
271*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, mkfifo_nonblock);
272*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, mkfifo_perm);
273*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, mkfifo_stat);
274*11be35a1SLionel Sambuc
275*11be35a1SLionel Sambuc return atf_no_error();
276*11be35a1SLionel Sambuc }
277