fifo_create.c (281414) | fifo_create.c (281450) |
---|---|
1/*- 2 * Copyright (c) 2005-2008 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * | 1/*- 2 * Copyright (c) 2005-2008 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * |
26 * $FreeBSD: user/ngie/more-tests/tools/regression/fifo/fifo_create/fifo_create.c 179934 2008-06-22 21:03:26Z rwatson $ | 26 * $FreeBSD: user/ngie/more-tests/tests/sys/fifo/fifo_create.c 281450 2015-04-12 06:18:24Z ngie $ |
27 */ 28 29#include <sys/stat.h> 30 31#include <err.h> 32#include <errno.h> 33#include <limits.h> 34#include <stdio.h> --- 35 unchanged lines hidden (view full) --- 70 * This test must run as root in order to usefully frob the process 71 * credential to test permission parts. 72 */ 73 74/* 75 * All activity occurs within a temporary directory created early in the 76 * test. 77 */ | 27 */ 28 29#include <sys/stat.h> 30 31#include <err.h> 32#include <errno.h> 33#include <limits.h> 34#include <stdio.h> --- 35 unchanged lines hidden (view full) --- 70 * This test must run as root in order to usefully frob the process 71 * credential to test permission parts. 72 */ 73 74/* 75 * All activity occurs within a temporary directory created early in the 76 * test. 77 */ |
78char temp_dir[PATH_MAX]; | 78static char temp_dir[PATH_MAX]; |
79 80static void __unused 81atexit_temp_dir(void) 82{ 83 84 rmdir(temp_dir); 85} 86 87/* 88 * Basic creation tests: verify that mkfifo(2) (or mknod(2)) creates a fifo, 89 * that the time stamps on the directory are updated, that if we try twice we 90 * get EEXIST, and that we can unlink it. 91 */ 92static void 93fifo_create_test(int use_mkfifo) 94{ 95 struct stat old_dirsb, dirsb, fifosb; 96 const char *testname; | 79 80static void __unused 81atexit_temp_dir(void) 82{ 83 84 rmdir(temp_dir); 85} 86 87/* 88 * Basic creation tests: verify that mkfifo(2) (or mknod(2)) creates a fifo, 89 * that the time stamps on the directory are updated, that if we try twice we 90 * get EEXIST, and that we can unlink it. 91 */ 92static void 93fifo_create_test(int use_mkfifo) 94{ 95 struct stat old_dirsb, dirsb, fifosb; 96 const char *testname; |
97 char path[PATH_MAX]; | 97 char path[] = "testfifo"; |
98 int error; 99 100 if (use_mkfifo) 101 testname = "mkfifo"; 102 else 103 testname = "mknod"; 104 105 /* 106 * Sleep to make sure that the time stamp on the directory will be 107 * updated. 108 */ | 98 int error; 99 100 if (use_mkfifo) 101 testname = "mkfifo"; 102 else 103 testname = "mknod"; 104 105 /* 106 * Sleep to make sure that the time stamp on the directory will be 107 * updated. 108 */ |
109 if (stat(temp_dir, &old_dirsb) < 0) | 109 if (stat(".", &old_dirsb) < 0) |
110 err(-1, "basic_create_test: %s: stat: %s", testname, 111 temp_dir); 112 113 sleep(2); 114 | 110 err(-1, "basic_create_test: %s: stat: %s", testname, 111 temp_dir); 112 113 sleep(2); 114 |
115 snprintf(path, PATH_MAX, "%s/testfifo", temp_dir); 116 | |
117 if (use_mkfifo) { 118 if (mkfifo(path, 0600) < 0) 119 err(-1, "basic_create_test: %s: %s", testname, path); 120 } else { 121 if (mknod(path, S_IFIFO | 0600, 0) < 0) 122 err(-1, "basic_create_test: %s: %s", testname, path); 123 } 124 --- 19 unchanged lines hidden (view full) --- 144 errx(-1, "basic_create_test: dup %s succeeded", 145 testname); 146 } 147 148 if (errno != EEXIST) 149 err(-1, "basic_create_test: dup %s unexpected error", 150 testname); 151 | 115 if (use_mkfifo) { 116 if (mkfifo(path, 0600) < 0) 117 err(-1, "basic_create_test: %s: %s", testname, path); 118 } else { 119 if (mknod(path, S_IFIFO | 0600, 0) < 0) 120 err(-1, "basic_create_test: %s: %s", testname, path); 121 } 122 --- 19 unchanged lines hidden (view full) --- 142 errx(-1, "basic_create_test: dup %s succeeded", 143 testname); 144 } 145 146 if (errno != EEXIST) 147 err(-1, "basic_create_test: dup %s unexpected error", 148 testname); 149 |
152 if (stat(temp_dir, &dirsb) < 0) { | 150 if (stat(".", &dirsb) < 0) { |
153 error = errno; 154 (void)unlink(path); 155 errno = error; 156 err(-1, "basic_create_test: %s: stat: %s", testname, 157 temp_dir); 158 } 159 160 if (old_dirsb.st_ctime == dirsb.st_ctime) { --- 39 unchanged lines hidden (view full) --- 200static const int permission_test_count = sizeof(permission_test) / 201 sizeof(struct permission_test); 202 203static void 204fifo_permission_test(int use_mkfifo) 205{ 206 const struct permission_test *ptp; 207 mode_t __unused old_umask; | 151 error = errno; 152 (void)unlink(path); 153 errno = error; 154 err(-1, "basic_create_test: %s: stat: %s", testname, 155 temp_dir); 156 } 157 158 if (old_dirsb.st_ctime == dirsb.st_ctime) { --- 39 unchanged lines hidden (view full) --- 198static const int permission_test_count = sizeof(permission_test) / 199 sizeof(struct permission_test); 200 201static void 202fifo_permission_test(int use_mkfifo) 203{ 204 const struct permission_test *ptp; 205 mode_t __unused old_umask; |
208 char path[PATH_MAX]; | 206 char path[] = "testfifo"; |
209 const char *testname; 210 struct stat sb; 211 int error, i; 212 213 if (use_mkfifo) 214 testname = "mkfifo"; 215 else 216 testname = "mknod"; 217 | 207 const char *testname; 208 struct stat sb; 209 int error, i; 210 211 if (use_mkfifo) 212 testname = "mkfifo"; 213 else 214 testname = "mknod"; 215 |
218 snprintf(path, PATH_MAX, "%s/testfifo", temp_dir); | |
219 old_umask = umask(0022); 220 for (i = 0; i < permission_test_count; i++) { 221 ptp = &permission_test[i]; 222 223 umask(ptp->pt_umask); 224 if (use_mkfifo) { 225 if (mkfifo(path, ptp->pt_reqmode) < 0) 226 err(-1, "fifo_permission_test: %s: %08o " --- 24 unchanged lines hidden (view full) --- 251 if (unlink(path) < 0) 252 err(-1, "fifo_permission_test: %s: unlink: %s", 253 testname, path); 254 } 255 umask(old_umask); 256} 257 258int | 216 old_umask = umask(0022); 217 for (i = 0; i < permission_test_count; i++) { 218 ptp = &permission_test[i]; 219 220 umask(ptp->pt_umask); 221 if (use_mkfifo) { 222 if (mkfifo(path, ptp->pt_reqmode) < 0) 223 err(-1, "fifo_permission_test: %s: %08o " --- 24 unchanged lines hidden (view full) --- 248 if (unlink(path) < 0) 249 err(-1, "fifo_permission_test: %s: unlink: %s", 250 testname, path); 251 } 252 umask(old_umask); 253} 254 255int |
259main(int argc, char *argv[]) | 256main(void) |
260{ 261 int i; 262 263 if (geteuid() != 0) 264 errx(-1, "must be run as root"); 265 | 257{ 258 int i; 259 260 if (geteuid() != 0) 261 errx(-1, "must be run as root"); 262 |
266 strcpy(temp_dir, "/tmp/fifo_create.XXXXXXXXXXX"); | 263 strcpy(temp_dir, "fifo_create.XXXXXXXXXXX"); |
267 if (mkdtemp(temp_dir) == NULL) 268 err(-1, "mkdtemp"); 269 atexit(atexit_temp_dir); 270 271 if (chdir(temp_dir) < 0) 272 err(-1, "chdir"); 273 274 /* 275 * Run each test twice, once with mknod(2) and a second time with 276 * mkfifo(2). Historically, BSD has not allowed mknod(2) to be used 277 * to create fifos, but the Single UNIX Specification requires it. 278 */ 279 for (i = 0; i < 2; i++) { 280 fifo_create_test(i); 281 fifo_permission_test(i); 282 } 283 284 return (0); 285} | 264 if (mkdtemp(temp_dir) == NULL) 265 err(-1, "mkdtemp"); 266 atexit(atexit_temp_dir); 267 268 if (chdir(temp_dir) < 0) 269 err(-1, "chdir"); 270 271 /* 272 * Run each test twice, once with mknod(2) and a second time with 273 * mkfifo(2). Historically, BSD has not allowed mknod(2) to be used 274 * to create fifos, but the Single UNIX Specification requires it. 275 */ 276 for (i = 0; i < 2; i++) { 277 fifo_create_test(i); 278 fifo_permission_test(i); 279 } 280 281 return (0); 282} |