2 * Copyright (c) 2005 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
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
40 * Simple regression test for the creation and destruction of POSIX fifos in
41 * the file system name space. Using a specially created directory, create
42 * a fifo in it and check that the following properties are present, as
43 * specified in IEEE Std 1003.1, 2004 Edition:
45 * - When mkfifo() is called, on success, a fifo is created.
47 * - On an error, no fifo is created. (XXX: Not tested)
49 * - The mode bits on the fifo are a product of combining the umask and
52 * - The fifo's owner will be the processes effective user ID. (XXX: Not
55 * - The fifo's group will be the parent directory's group or the effective
56 * group ID of the process. For historical reasons, BSD prefers the group
57 * ID of the process, so we will generate an error if it's not that. (XXX:
60 * - The st_atime, st_ctime, st_mtime of the fifo will be set appropriately,
61 * and st_ctime and st_mtime on the directory will be updated. (XXX: We
62 * test they are updated, not correct)
64 * - EEXIST is returned if the named file already exists.
66 * In addition, we check that we can unlink the fifo, and that if we do, it
69 * This test must run as root in order to usefully frob the process
70 * credential to test permission parts.
74 * All activity occurs within a temporary directory created early in the
77 char temp_dir[PATH_MAX];
87 * Basic creation tests: verify that mkfifo() creates a fifo, that the time
88 * stamps on the directory are updated, that if we try twice we get EEXIST,
89 * and that we can unlink it.
92 fifo_create_test(void)
94 struct stat old_dirsb, dirsb, fifosb;
99 * Sleep to make sure that the time stamp on the directory will be
104 if (stat(temp_dir, &old_dirsb) < 0)
105 err(-1, "basic_create_test: stat: %s", temp_dir);
107 snprintf(path, PATH_MAX, "%s/testfifo", temp_dir);
109 if (mkfifo(path, 0600) < 0)
110 err(-1, "basic_create_test: mkfifo: %s", path);
112 if (stat(path, &fifosb) < 0) {
116 err(-1, "basic_create_test: stat: %s", path);
119 if (!(S_ISFIFO(fifosb.st_mode))) {
121 errx(-1, "basic_create_test: mkfifo produced non-fifo");
124 if (mkfifo(path, 0600) == 0)
125 errx(-1, "basic_create_test: dup mkfifo succeeded");
128 err(-1, "basic_create_test: dup mkfifo unexpected error");
130 if (stat(temp_dir, &dirsb) < 0) {
134 err(-1, "basic_create_test: stat: %s", temp_dir);
137 if (old_dirsb.st_ctime == dirsb.st_ctime) {
139 errx(-1, "basic_create_test: old_dirsb.st_ctime == "
143 if (old_dirsb.st_mtime == dirsb.st_mtime) {
145 errx(-1, "basic_create_test: old_dirsb.st_mtime == "
149 if (unlink(path) < 0)
150 err(-1, "basic_create_test: unlink: %s", path);
152 if (stat(path, &fifosb) == 0)
153 errx(-1, "basic_create_test: unlink failed to unlink");
155 err(-1, "basic_create_test: unlink unexpected error");
159 * Having determined that basic create/remove/etc functionality is present
160 * for fifos, now make sure that the umask, requested permissions, and
161 * resulting mode are handled properly.
163 static const struct permission_test {
167 } permission_test[] = {
169 {0000, S_IRWXU, S_IFIFO | S_IRWXU},
170 {0000, S_IRWXU | S_IRWXG | S_IRWXO, S_IFIFO | S_IRWXU | S_IRWXG |
172 {0077, S_IRWXU, S_IFIFO | S_IRWXU},
173 {0077, S_IRWXU | S_IRWXG | S_IRWXO, S_IFIFO | S_IRWXU},
175 static const int permission_test_count = sizeof(permission_test) /
176 sizeof(struct permission_test);
179 fifo_permission_test(void)
181 const struct permission_test *ptp;
182 mode_t __unused old_umask;
187 snprintf(path, PATH_MAX, "%s/testfifo", temp_dir);
188 old_umask = umask(0022);
189 for (i = 0; i < permission_test_count; i++) {
190 ptp = &permission_test[i];
192 umask(ptp->pt_umask);
193 if (mkfifo(path, ptp->pt_reqmode) < 0)
194 err(-1, "fifo_permission_test: %08o %08o %08o\n",
195 ptp->pt_umask, ptp->pt_reqmode, ptp->pt_mode);
197 if (stat(path, &sb) < 0) {
201 err(-1, "fifo_permission_test: %s", path);
204 if (sb.st_mode != ptp->pt_mode) {
206 errx(-1, "fifo_permission_test: %08o %08o %08o "
207 "got %08o", ptp->pt_umask, ptp->pt_reqmode,
208 ptp->pt_mode, sb.st_mode);
211 if (unlink(path) < 0)
212 err(-1, "fifo_permission_test: unlink: %s", path);
218 main(int argc, char *argv[])
222 errx(-1, "must be run as root");
224 strcpy(temp_dir, "/tmp/fifo_create.XXXXXXXXXXX");
225 if (mkdtemp(temp_dir) == NULL)
227 atexit(atexit_temp_dir);
229 if (chdir(temp_dir) < 0)
234 fifo_permission_test();