1 /* $NetBSD: t_mkfifo.c,v 1.2 2011/11/02 06:04:48 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 $");
46 static const char path
[] = "fifo";
47 static void support(void);
55 if (mkfifo(path
, 0600) == 0) {
56 ATF_REQUIRE(unlink(path
) == 0);
60 if (errno
== EOPNOTSUPP
)
61 atf_tc_skip("the kernel does not support FIFOs");
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
)
80 ATF_REQUIRE(mkfifo(path
, 0600) == 0);
83 ATF_REQUIRE(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 */
99 ATF_REQUIRE(kill(pid
, SIGKILL
) == 0);
103 if (WIFSIGNALED(sta
) == 0 || WTERMSIG(sta
) != SIGKILL
)
104 atf_tc_fail("FIFO did not block");
110 ATF_TC_CLEANUP(mkfifo_block
, tc
)
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];
127 (void)memset(buf
, 'x', sizeof(buf
));
128 ATF_REQUIRE(mkfifo(path
, 0600) == 0);
131 ATF_REQUIRE_ERRNO(EFAULT
, mkfifo((char *)-1, 0600) == -1);
134 ATF_REQUIRE_ERRNO(EEXIST
, mkfifo("/etc/passwd", 0600) == -1);
137 ATF_REQUIRE_ERRNO(EEXIST
, mkfifo(path
, 0600) == -1);
140 ATF_REQUIRE_ERRNO(ENAMETOOLONG
, mkfifo(buf
, 0600) == -1);
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
)
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
)
167 ATF_REQUIRE(mkfifo(path
, 0600) == 0);
170 ATF_REQUIRE(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
);
184 (void)pause(); /* NOTREACHED */
190 ATF_REQUIRE_ERRNO(ENXIO
, open(path
, O_WRONLY
| O_NONBLOCK
) == -1);
192 (void)kill(pid
, SIGKILL
);
195 if (WIFSIGNALED(sta
) != 0 || WTERMSIG(sta
) == SIGKILL
)
196 atf_tc_fail("FIFO blocked for O_NONBLOCK open(2)");
202 ATF_TC_CLEANUP(mkfifo_nonblock
, tc
)
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
)
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...
228 ATF_REQUIRE_ERRNO(EFTYPE
, chmod(path
, 1777) == -1);
230 ATF_REQUIRE(unlink(path
) == 0);
233 ATF_TC_CLEANUP(mkfifo_perm
, tc
)
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
)
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
)
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();