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