etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_mkfifo.c
blob56bc4c831fcba46e0f6a3ff95dc84a801ad8f1c5
1 /* $NetBSD: t_mkfifo.c,v 1.2 2011/11/02 06:04:48 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_mkfifo.c,v 1.2 2011/11/02 06:04:48 jruoho Exp $");
34 #include <sys/stat.h>
35 #include <sys/wait.h>
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>
46 static const char path[] = "fifo";
47 static void support(void);
49 static void
50 support(void)
53 errno = 0;
55 if (mkfifo(path, 0600) == 0) {
56 ATF_REQUIRE(unlink(path) == 0);
57 return;
60 if (errno == EOPNOTSUPP)
61 atf_tc_skip("the kernel does not support FIFOs");
62 else {
63 atf_tc_fail("mkfifo(2) failed");
67 ATF_TC_WITH_CLEANUP(mkfifo_block);
68 ATF_TC_HEAD(mkfifo_block, tc)
70 atf_tc_set_md_var(tc, "descr", "Test that FIFOs block");
73 ATF_TC_BODY(mkfifo_block, tc)
75 int sta, fd = -1;
76 pid_t pid;
78 support();
80 ATF_REQUIRE(mkfifo(path, 0600) == 0);
82 pid = fork();
83 ATF_REQUIRE(pid >= 0);
85 if (pid == 0) {
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).
92 fd = open(path, O_RDONLY);
94 _exit(EXIT_FAILURE); /* NOTREACHED */
97 (void)sleep(1);
99 ATF_REQUIRE(kill(pid, SIGKILL) == 0);
101 (void)wait(&sta);
103 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
104 atf_tc_fail("FIFO did not block");
106 (void)close(fd);
107 (void)unlink(path);
110 ATF_TC_CLEANUP(mkfifo_block, tc)
112 (void)unlink(path);
115 ATF_TC_WITH_CLEANUP(mkfifo_err);
116 ATF_TC_HEAD(mkfifo_err, tc)
118 atf_tc_set_md_var(tc, "descr", "Test erros from mkfifo(2)");
121 ATF_TC_BODY(mkfifo_err, tc)
123 char buf[PATH_MAX + 1];
125 support();
127 (void)memset(buf, 'x', sizeof(buf));
128 ATF_REQUIRE(mkfifo(path, 0600) == 0);
130 errno = 0;
131 ATF_REQUIRE_ERRNO(EFAULT, mkfifo((char *)-1, 0600) == -1);
133 errno = 0;
134 ATF_REQUIRE_ERRNO(EEXIST, mkfifo("/etc/passwd", 0600) == -1);
136 errno = 0;
137 ATF_REQUIRE_ERRNO(EEXIST, mkfifo(path, 0600) == -1);
139 errno = 0;
140 ATF_REQUIRE_ERRNO(ENAMETOOLONG, mkfifo(buf, 0600) == -1);
142 errno = 0;
143 ATF_REQUIRE_ERRNO(ENOENT, mkfifo("/a/b/c/d/e/f/g", 0600) == -1);
145 ATF_REQUIRE(unlink(path) == 0);
148 ATF_TC_CLEANUP(mkfifo_err, tc)
150 (void)unlink(path);
153 ATF_TC_WITH_CLEANUP(mkfifo_nonblock);
154 ATF_TC_HEAD(mkfifo_nonblock, tc)
156 atf_tc_set_md_var(tc, "descr", "Test O_NONBLOCK with FIFOs");
159 ATF_TC_BODY(mkfifo_nonblock, tc)
161 int fd, sta;
162 pid_t pid;
164 support();
166 fd = -1;
167 ATF_REQUIRE(mkfifo(path, 0600) == 0);
169 pid = fork();
170 ATF_REQUIRE(pid >= 0);
172 if (pid == 0) {
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.
179 fd = open(path, O_RDONLY | O_NONBLOCK);
181 if (fd >= 0)
182 _exit(EXIT_SUCCESS);
184 (void)pause(); /* NOTREACHED */
187 (void)sleep(1);
189 errno = 0;
190 ATF_REQUIRE_ERRNO(ENXIO, open(path, O_WRONLY | O_NONBLOCK) == -1);
192 (void)kill(pid, SIGKILL);
193 (void)wait(&sta);
195 if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL)
196 atf_tc_fail("FIFO blocked for O_NONBLOCK open(2)");
198 (void)close(fd);
199 (void)unlink(path);
202 ATF_TC_CLEANUP(mkfifo_nonblock, tc)
204 (void)unlink(path);
207 ATF_TC_WITH_CLEANUP(mkfifo_perm);
208 ATF_TC_HEAD(mkfifo_perm, tc)
210 atf_tc_set_md_var(tc, "descr", "Test permissions with mkfifo(2)");
211 atf_tc_set_md_var(tc, "require.user", "unprivileged");
214 ATF_TC_BODY(mkfifo_perm, tc)
217 support();
219 errno = 0;
220 ATF_REQUIRE_ERRNO(EACCES, mkfifo("/root/fifo", 0600) == -1);
222 ATF_REQUIRE(mkfifo(path, 0600) == 0);
225 * For some reason this fails with EFTYPE...
227 errno = 0;
228 ATF_REQUIRE_ERRNO(EFTYPE, chmod(path, 1777) == -1);
230 ATF_REQUIRE(unlink(path) == 0);
233 ATF_TC_CLEANUP(mkfifo_perm, tc)
235 (void)unlink(path);
238 ATF_TC_WITH_CLEANUP(mkfifo_stat);
239 ATF_TC_HEAD(mkfifo_stat, tc)
241 atf_tc_set_md_var(tc, "descr", "Test mkfifo(2) with stat");
244 ATF_TC_BODY(mkfifo_stat, tc)
246 struct stat st;
248 support();
250 (void)memset(&st, 0, sizeof(struct stat));
252 ATF_REQUIRE(mkfifo(path, 0600) == 0);
253 ATF_REQUIRE(stat(path, &st) == 0);
255 if (S_ISFIFO(st.st_mode) == 0)
256 atf_tc_fail("invalid mode from mkfifo(2)");
258 ATF_REQUIRE(unlink(path) == 0);
261 ATF_TC_CLEANUP(mkfifo_stat, tc)
263 (void)unlink(path);
266 ATF_TP_ADD_TCS(tp)
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);
275 return atf_no_error();