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