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