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 */ 78281450Sngiestatic char 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; 97281450Sngie char path[] = "testfifo"; 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 */ 109281450Sngie if (stat(".", &old_dirsb) < 0) 110179934Srwatson err(-1, "basic_create_test: %s: stat: %s", testname, 111179934Srwatson temp_dir); 112179934Srwatson 113149959Srwatson sleep(2); 114149959Srwatson 115179934Srwatson if (use_mkfifo) { 116179934Srwatson if (mkfifo(path, 0600) < 0) 117179934Srwatson err(-1, "basic_create_test: %s: %s", testname, path); 118179934Srwatson } else { 119179934Srwatson if (mknod(path, S_IFIFO | 0600, 0) < 0) 120179934Srwatson err(-1, "basic_create_test: %s: %s", testname, path); 121179934Srwatson } 122149959Srwatson 123149959Srwatson if (stat(path, &fifosb) < 0) { 124149959Srwatson error = errno; 125149959Srwatson (void)unlink(path); 126149959Srwatson errno = error; 127179934Srwatson err(-1, "basic_create_test: %s: stat: %s", testname, path); 128149959Srwatson } 129149959Srwatson 130149959Srwatson if (!(S_ISFIFO(fifosb.st_mode))) { 131149959Srwatson (void)unlink(path); 132179934Srwatson errx(-1, "basic_create_test: %s produced non-fifo", 133179934Srwatson testname); 134149959Srwatson } 135149959Srwatson 136179934Srwatson if (use_mkfifo) { 137179934Srwatson if (mkfifo(path, 0600) == 0) 138179934Srwatson errx(-1, "basic_create_test: dup %s succeeded", 139179934Srwatson testname); 140179934Srwatson } else { 141179934Srwatson if (mknod(path, S_IFIFO | 0600, 0) == 0) 142179934Srwatson errx(-1, "basic_create_test: dup %s succeeded", 143179934Srwatson testname); 144179934Srwatson } 145149959Srwatson 146149959Srwatson if (errno != EEXIST) 147179934Srwatson err(-1, "basic_create_test: dup %s unexpected error", 148179934Srwatson testname); 149149959Srwatson 150281450Sngie if (stat(".", &dirsb) < 0) { 151149959Srwatson error = errno; 152149959Srwatson (void)unlink(path); 153149959Srwatson errno = error; 154179934Srwatson err(-1, "basic_create_test: %s: stat: %s", testname, 155179934Srwatson temp_dir); 156149959Srwatson } 157149959Srwatson 158149959Srwatson if (old_dirsb.st_ctime == dirsb.st_ctime) { 159149959Srwatson (void)unlink(path); 160179934Srwatson errx(-1, "basic_create_test: %s: old_dirsb.st_ctime == " 161179934Srwatson "dirsb.st_ctime", testname); 162149959Srwatson } 163149959Srwatson 164149959Srwatson if (old_dirsb.st_mtime == dirsb.st_mtime) { 165149959Srwatson (void)unlink(path); 166179934Srwatson errx(-1, "basic_create_test: %s: old_dirsb.st_mtime == " 167179934Srwatson "dirsb.st_mtime", testname); 168149959Srwatson } 169149959Srwatson 170149959Srwatson if (unlink(path) < 0) 171179934Srwatson err(-1, "basic_create_test: %s: unlink: %s", testname, path); 172149959Srwatson 173149959Srwatson if (stat(path, &fifosb) == 0) 174179934Srwatson errx(-1, "basic_create_test: %s: unlink failed to unlink", 175179934Srwatson testname); 176149959Srwatson if (errno != ENOENT) 177179934Srwatson err(-1, "basic_create_test: %s: unlink unexpected error", 178179934Srwatson testname); 179149959Srwatson} 180149959Srwatson 181149959Srwatson/* 182149959Srwatson * Having determined that basic create/remove/etc functionality is present 183149959Srwatson * for fifos, now make sure that the umask, requested permissions, and 184149959Srwatson * resulting mode are handled properly. 185149959Srwatson */ 186149959Srwatsonstatic const struct permission_test { 187149959Srwatson mode_t pt_umask; 188149959Srwatson mode_t pt_reqmode; 189149959Srwatson mode_t pt_mode; 190149959Srwatson} permission_test[] = { 191149959Srwatson {0000, 0, S_IFIFO}, 192149959Srwatson {0000, S_IRWXU, S_IFIFO | S_IRWXU}, 193149959Srwatson {0000, S_IRWXU | S_IRWXG | S_IRWXO, S_IFIFO | S_IRWXU | S_IRWXG | 194149959Srwatson S_IRWXO }, 195149959Srwatson {0077, S_IRWXU, S_IFIFO | S_IRWXU}, 196149959Srwatson {0077, S_IRWXU | S_IRWXG | S_IRWXO, S_IFIFO | S_IRWXU}, 197149959Srwatson}; 198149959Srwatsonstatic const int permission_test_count = sizeof(permission_test) / 199149959Srwatson sizeof(struct permission_test); 200149959Srwatson 201149959Srwatsonstatic void 202179934Srwatsonfifo_permission_test(int use_mkfifo) 203149959Srwatson{ 204149959Srwatson const struct permission_test *ptp; 205149959Srwatson mode_t __unused old_umask; 206281450Sngie char path[] = "testfifo"; 207179934Srwatson const char *testname; 208149959Srwatson struct stat sb; 209149959Srwatson int error, i; 210149959Srwatson 211179934Srwatson if (use_mkfifo) 212179934Srwatson testname = "mkfifo"; 213179934Srwatson else 214179934Srwatson testname = "mknod"; 215179934Srwatson 216149959Srwatson old_umask = umask(0022); 217149959Srwatson for (i = 0; i < permission_test_count; i++) { 218149959Srwatson ptp = &permission_test[i]; 219149959Srwatson 220149959Srwatson umask(ptp->pt_umask); 221179934Srwatson if (use_mkfifo) { 222179934Srwatson if (mkfifo(path, ptp->pt_reqmode) < 0) 223179934Srwatson err(-1, "fifo_permission_test: %s: %08o " 224179934Srwatson "%08o %08o\n", testname, ptp->pt_umask, 225179934Srwatson ptp->pt_reqmode, ptp->pt_mode); 226179934Srwatson } else { 227179934Srwatson if (mknod(path, S_IFIFO | ptp->pt_reqmode, 0) < 0) 228179934Srwatson err(-1, "fifo_permission_test: %s: %08o " 229179934Srwatson "%08o %08o\n", testname, ptp->pt_umask, 230179934Srwatson ptp->pt_reqmode, ptp->pt_mode); 231179934Srwatson } 232149959Srwatson 233149959Srwatson if (stat(path, &sb) < 0) { 234149959Srwatson error = errno; 235149959Srwatson (void)unlink(path); 236149959Srwatson errno = error; 237179934Srwatson err(-1, "fifo_permission_test: %s: %s", testname, 238179934Srwatson path); 239149959Srwatson } 240149959Srwatson 241149959Srwatson if (sb.st_mode != ptp->pt_mode) { 242149959Srwatson (void)unlink(path); 243179934Srwatson errx(-1, "fifo_permission_test: %s: %08o %08o %08o " 244179934Srwatson "got %08o", testname, ptp->pt_umask, 245179934Srwatson ptp->pt_reqmode, ptp->pt_mode, sb.st_mode); 246149959Srwatson } 247149959Srwatson 248149959Srwatson if (unlink(path) < 0) 249179934Srwatson err(-1, "fifo_permission_test: %s: unlink: %s", 250179934Srwatson testname, path); 251149959Srwatson } 252149959Srwatson umask(old_umask); 253149959Srwatson} 254149959Srwatson 255149959Srwatsonint 256281450Sngiemain(void) 257149959Srwatson{ 258179934Srwatson int i; 259149959Srwatson 260149959Srwatson if (geteuid() != 0) 261149959Srwatson errx(-1, "must be run as root"); 262149959Srwatson 263281450Sngie strcpy(temp_dir, "fifo_create.XXXXXXXXXXX"); 264149959Srwatson if (mkdtemp(temp_dir) == NULL) 265149959Srwatson err(-1, "mkdtemp"); 266149959Srwatson atexit(atexit_temp_dir); 267149959Srwatson 268149959Srwatson if (chdir(temp_dir) < 0) 269149959Srwatson err(-1, "chdir"); 270149959Srwatson 271179934Srwatson /* 272179934Srwatson * Run each test twice, once with mknod(2) and a second time with 273179934Srwatson * mkfifo(2). Historically, BSD has not allowed mknod(2) to be used 274179934Srwatson * to create fifos, but the Single UNIX Specification requires it. 275179934Srwatson */ 276179934Srwatson for (i = 0; i < 2; i++) { 277179934Srwatson fifo_create_test(i); 278179934Srwatson fifo_permission_test(i); 279179934Srwatson } 280149959Srwatson 281149959Srwatson return (0); 282149959Srwatson} 283