1149959Srwatson/*- 2179934Srwatson * Copyright (c) 2005-2008 Robert N. M. Watson 3149959Srwatson * All rights reserved. 4149959Srwatson * 5149959Srwatson * Redistribution and use in source and binary forms, with or without 6149959Srwatson * modification, are permitted provided that the following conditions 7149959Srwatson * are met: 8149959Srwatson * 1. Redistributions of source code must retain the above copyright 9149959Srwatson * notice, this list of conditions and the following disclaimer. 10149959Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11149959Srwatson * notice, this list of conditions and the following disclaimer in the 12149959Srwatson * documentation and/or other materials provided with the distribution. 13149959Srwatson * 14149959Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15149959Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16149959Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17149959Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18149959Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19149959Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20149959Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21149959Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22149959Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23149959Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24149959Srwatson * SUCH DAMAGE. 25149959Srwatson * 26149959Srwatson * $FreeBSD$ 27149959Srwatson */ 28149959Srwatson 29149959Srwatson#include <sys/stat.h> 30149959Srwatson 31149959Srwatson#include <err.h> 32149959Srwatson#include <errno.h> 33149959Srwatson#include <limits.h> 34149959Srwatson#include <stdio.h> 35149959Srwatson#include <stdlib.h> 36149959Srwatson#include <string.h> 37149959Srwatson#include <unistd.h> 38149959Srwatson 39149959Srwatson/* 40149959Srwatson * Simple regression test for the creation and destruction of POSIX fifos in 41149959Srwatson * the file system name space. Using a specially created directory, create 42149959Srwatson * a fifo in it and check that the following properties are present, as 43149959Srwatson * specified in IEEE Std 1003.1, 2004 Edition: 44149959Srwatson * 45179934Srwatson * - When mkfifo() or mknod(S_IFIFO) is called, on success, a fifo is 46179934Srwatson * created. 47149959Srwatson * 48149959Srwatson * - On an error, no fifo is created. (XXX: Not tested) 49149959Srwatson * 50149959Srwatson * - The mode bits on the fifo are a product of combining the umask and 51149959Srwatson * requested mode. 52149959Srwatson * 53149959Srwatson * - The fifo's owner will be the processes effective user ID. (XXX: Not 54149959Srwatson * tested) 55149959Srwatson * 56149959Srwatson * - The fifo's group will be the parent directory's group or the effective 57149959Srwatson * group ID of the process. For historical reasons, BSD prefers the group 58149959Srwatson * ID of the process, so we will generate an error if it's not that. (XXX: 59149959Srwatson * Not tested) 60149959Srwatson * 61149959Srwatson * - The st_atime, st_ctime, st_mtime of the fifo will be set appropriately, 62149959Srwatson * and st_ctime and st_mtime on the directory will be updated. (XXX: We 63149959Srwatson * test they are updated, not correct) 64149959Srwatson * 65149959Srwatson * - EEXIST is returned if the named file already exists. 66149959Srwatson * 67149959Srwatson * In addition, we check that we can unlink the fifo, and that if we do, it 68149959Srwatson * disappears. 69149959Srwatson * 70149959Srwatson * This test must run as root in order to usefully frob the process 71149959Srwatson * credential to test permission parts. 72149959Srwatson */ 73149959Srwatson 74149959Srwatson/* 75149959Srwatson * All activity occurs within a temporary directory created early in the 76149959Srwatson * test. 77149959Srwatson */ 78149959Srwatsonchar temp_dir[PATH_MAX]; 79149959Srwatson 80149959Srwatsonstatic void __unused 81149959Srwatsonatexit_temp_dir(void) 82149959Srwatson{ 83149959Srwatson 84149959Srwatson rmdir(temp_dir); 85149959Srwatson} 86149959Srwatson 87149959Srwatson/* 88179934Srwatson * Basic creation tests: verify that mkfifo(2) (or mknod(2)) creates a fifo, 89179934Srwatson * that the time stamps on the directory are updated, that if we try twice we 90179934Srwatson * get EEXIST, and that we can unlink it. 91149959Srwatson */ 92149959Srwatsonstatic void 93179934Srwatsonfifo_create_test(int use_mkfifo) 94149959Srwatson{ 95149959Srwatson struct stat old_dirsb, dirsb, fifosb; 96179934Srwatson const char *testname; 97149959Srwatson char path[PATH_MAX]; 98149959Srwatson int error; 99149959Srwatson 100179934Srwatson if (use_mkfifo) 101179934Srwatson testname = "mkfifo"; 102179934Srwatson else 103179934Srwatson testname = "mknod"; 104179934Srwatson 105149959Srwatson /* 106149959Srwatson * Sleep to make sure that the time stamp on the directory will be 107149959Srwatson * updated. 108149959Srwatson */ 109179934Srwatson if (stat(temp_dir, &old_dirsb) < 0) 110179934Srwatson err(-1, "basic_create_test: %s: stat: %s", testname, 111179934Srwatson temp_dir); 112179934Srwatson 113149959Srwatson sleep(2); 114149959Srwatson 115149959Srwatson snprintf(path, PATH_MAX, "%s/testfifo", temp_dir); 116149959Srwatson 117179934Srwatson if (use_mkfifo) { 118179934Srwatson if (mkfifo(path, 0600) < 0) 119179934Srwatson err(-1, "basic_create_test: %s: %s", testname, path); 120179934Srwatson } else { 121179934Srwatson if (mknod(path, S_IFIFO | 0600, 0) < 0) 122179934Srwatson err(-1, "basic_create_test: %s: %s", testname, path); 123179934Srwatson } 124149959Srwatson 125149959Srwatson if (stat(path, &fifosb) < 0) { 126149959Srwatson error = errno; 127149959Srwatson (void)unlink(path); 128149959Srwatson errno = error; 129179934Srwatson err(-1, "basic_create_test: %s: stat: %s", testname, path); 130149959Srwatson } 131149959Srwatson 132149959Srwatson if (!(S_ISFIFO(fifosb.st_mode))) { 133149959Srwatson (void)unlink(path); 134179934Srwatson errx(-1, "basic_create_test: %s produced non-fifo", 135179934Srwatson testname); 136149959Srwatson } 137149959Srwatson 138179934Srwatson if (use_mkfifo) { 139179934Srwatson if (mkfifo(path, 0600) == 0) 140179934Srwatson errx(-1, "basic_create_test: dup %s succeeded", 141179934Srwatson testname); 142179934Srwatson } else { 143179934Srwatson if (mknod(path, S_IFIFO | 0600, 0) == 0) 144179934Srwatson errx(-1, "basic_create_test: dup %s succeeded", 145179934Srwatson testname); 146179934Srwatson } 147149959Srwatson 148149959Srwatson if (errno != EEXIST) 149179934Srwatson err(-1, "basic_create_test: dup %s unexpected error", 150179934Srwatson testname); 151149959Srwatson 152149959Srwatson if (stat(temp_dir, &dirsb) < 0) { 153149959Srwatson error = errno; 154149959Srwatson (void)unlink(path); 155149959Srwatson errno = error; 156179934Srwatson err(-1, "basic_create_test: %s: stat: %s", testname, 157179934Srwatson temp_dir); 158149959Srwatson } 159149959Srwatson 160149959Srwatson if (old_dirsb.st_ctime == dirsb.st_ctime) { 161149959Srwatson (void)unlink(path); 162179934Srwatson errx(-1, "basic_create_test: %s: old_dirsb.st_ctime == " 163179934Srwatson "dirsb.st_ctime", testname); 164149959Srwatson } 165149959Srwatson 166149959Srwatson if (old_dirsb.st_mtime == dirsb.st_mtime) { 167149959Srwatson (void)unlink(path); 168179934Srwatson errx(-1, "basic_create_test: %s: old_dirsb.st_mtime == " 169179934Srwatson "dirsb.st_mtime", testname); 170149959Srwatson } 171149959Srwatson 172149959Srwatson if (unlink(path) < 0) 173179934Srwatson err(-1, "basic_create_test: %s: unlink: %s", testname, path); 174149959Srwatson 175149959Srwatson if (stat(path, &fifosb) == 0) 176179934Srwatson errx(-1, "basic_create_test: %s: unlink failed to unlink", 177179934Srwatson testname); 178149959Srwatson if (errno != ENOENT) 179179934Srwatson err(-1, "basic_create_test: %s: unlink unexpected error", 180179934Srwatson testname); 181149959Srwatson} 182149959Srwatson 183149959Srwatson/* 184149959Srwatson * Having determined that basic create/remove/etc functionality is present 185149959Srwatson * for fifos, now make sure that the umask, requested permissions, and 186149959Srwatson * resulting mode are handled properly. 187149959Srwatson */ 188149959Srwatsonstatic const struct permission_test { 189149959Srwatson mode_t pt_umask; 190149959Srwatson mode_t pt_reqmode; 191149959Srwatson mode_t pt_mode; 192149959Srwatson} permission_test[] = { 193149959Srwatson {0000, 0, S_IFIFO}, 194149959Srwatson {0000, S_IRWXU, S_IFIFO | S_IRWXU}, 195149959Srwatson {0000, S_IRWXU | S_IRWXG | S_IRWXO, S_IFIFO | S_IRWXU | S_IRWXG | 196149959Srwatson S_IRWXO }, 197149959Srwatson {0077, S_IRWXU, S_IFIFO | S_IRWXU}, 198149959Srwatson {0077, S_IRWXU | S_IRWXG | S_IRWXO, S_IFIFO | S_IRWXU}, 199149959Srwatson}; 200149959Srwatsonstatic const int permission_test_count = sizeof(permission_test) / 201149959Srwatson sizeof(struct permission_test); 202149959Srwatson 203149959Srwatsonstatic void 204179934Srwatsonfifo_permission_test(int use_mkfifo) 205149959Srwatson{ 206149959Srwatson const struct permission_test *ptp; 207149959Srwatson mode_t __unused old_umask; 208149959Srwatson char path[PATH_MAX]; 209179934Srwatson const char *testname; 210149959Srwatson struct stat sb; 211149959Srwatson int error, i; 212149959Srwatson 213179934Srwatson if (use_mkfifo) 214179934Srwatson testname = "mkfifo"; 215179934Srwatson else 216179934Srwatson testname = "mknod"; 217179934Srwatson 218149959Srwatson snprintf(path, PATH_MAX, "%s/testfifo", temp_dir); 219149959Srwatson old_umask = umask(0022); 220149959Srwatson for (i = 0; i < permission_test_count; i++) { 221149959Srwatson ptp = &permission_test[i]; 222149959Srwatson 223149959Srwatson umask(ptp->pt_umask); 224179934Srwatson if (use_mkfifo) { 225179934Srwatson if (mkfifo(path, ptp->pt_reqmode) < 0) 226179934Srwatson err(-1, "fifo_permission_test: %s: %08o " 227179934Srwatson "%08o %08o\n", testname, ptp->pt_umask, 228179934Srwatson ptp->pt_reqmode, ptp->pt_mode); 229179934Srwatson } else { 230179934Srwatson if (mknod(path, S_IFIFO | ptp->pt_reqmode, 0) < 0) 231179934Srwatson err(-1, "fifo_permission_test: %s: %08o " 232179934Srwatson "%08o %08o\n", testname, ptp->pt_umask, 233179934Srwatson ptp->pt_reqmode, ptp->pt_mode); 234179934Srwatson } 235149959Srwatson 236149959Srwatson if (stat(path, &sb) < 0) { 237149959Srwatson error = errno; 238149959Srwatson (void)unlink(path); 239149959Srwatson errno = error; 240179934Srwatson err(-1, "fifo_permission_test: %s: %s", testname, 241179934Srwatson path); 242149959Srwatson } 243149959Srwatson 244149959Srwatson if (sb.st_mode != ptp->pt_mode) { 245149959Srwatson (void)unlink(path); 246179934Srwatson errx(-1, "fifo_permission_test: %s: %08o %08o %08o " 247179934Srwatson "got %08o", testname, ptp->pt_umask, 248179934Srwatson ptp->pt_reqmode, ptp->pt_mode, sb.st_mode); 249149959Srwatson } 250149959Srwatson 251149959Srwatson if (unlink(path) < 0) 252179934Srwatson err(-1, "fifo_permission_test: %s: unlink: %s", 253179934Srwatson testname, path); 254149959Srwatson } 255149959Srwatson umask(old_umask); 256149959Srwatson} 257149959Srwatson 258149959Srwatsonint 259149959Srwatsonmain(int argc, char *argv[]) 260149959Srwatson{ 261179934Srwatson int i; 262149959Srwatson 263149959Srwatson if (geteuid() != 0) 264149959Srwatson errx(-1, "must be run as root"); 265149959Srwatson 266149959Srwatson strcpy(temp_dir, "/tmp/fifo_create.XXXXXXXXXXX"); 267149959Srwatson if (mkdtemp(temp_dir) == NULL) 268149959Srwatson err(-1, "mkdtemp"); 269149959Srwatson atexit(atexit_temp_dir); 270149959Srwatson 271149959Srwatson if (chdir(temp_dir) < 0) 272149959Srwatson err(-1, "chdir"); 273149959Srwatson 274179934Srwatson /* 275179934Srwatson * Run each test twice, once with mknod(2) and a second time with 276179934Srwatson * mkfifo(2). Historically, BSD has not allowed mknod(2) to be used 277179934Srwatson * to create fifos, but the Single UNIX Specification requires it. 278179934Srwatson */ 279179934Srwatson for (i = 0; i < 2; i++) { 280179934Srwatson fifo_create_test(i); 281179934Srwatson fifo_permission_test(i); 282179934Srwatson } 283149959Srwatson 284149959Srwatson return (0); 285149959Srwatson} 286