summaryrefslogtreecommitdiffstats
path: root/tests/sys/fifo/fifo_create.c
blob: 2eb01e577c53c0abef49f5a682c42851e256c87f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*-
 * Copyright (c) 2005-2008 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/stat.h>

#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/*
 * Simple regression test for the creation and destruction of POSIX fifos in
 * the file system name space.  Using a specially created directory, create
 * a fifo in it and check that the following properties are present, as
 * specified in IEEE Std 1003.1, 2004 Edition:
 *
 * - When mkfifo() or mknod(S_IFIFO) is called, on success, a fifo is
 *   created.
 *
 * - On an error, no fifo is created. (XXX: Not tested)
 *
 * - The mode bits on the fifo are a product of combining the umask and
 *   requested mode.
 *
 * - The fifo's owner will be the processes effective user ID. (XXX: Not
 *   tested)
 *
 * - The fifo's group will be the parent directory's group or the effective
 *   group ID of the process.  For historical reasons, BSD prefers the group
 *   ID of the process, so we will generate an error if it's not that. (XXX:
 *   Not tested)
 *
 * - The st_atime, st_ctime, st_mtime of the fifo will be set appropriately,
 *   and st_ctime and st_mtime on the directory will be updated. (XXX: We
 *   test they are updated, not correct)
 *
 * - EEXIST is returned if the named file already exists.
 *
 * In addition, we check that we can unlink the fifo, and that if we do, it
 * disappears.
 *
 * This test must run as root in order to usefully frob the process
 * credential to test permission parts.
 */

/*
 * All activity occurs within a temporary directory created early in the
 * test.
 */
static char	temp_dir[PATH_MAX];

static void __unused
atexit_temp_dir(void)
{

	rmdir(temp_dir);
}

/*
 * Basic creation tests: verify that mkfifo(2) (or mknod(2)) creates a fifo,
 * that the time stamps on the directory are updated, that if we try twice we
 * get EEXIST, and that we can unlink it.
 */
static void
fifo_create_test(int use_mkfifo)
{
	struct stat old_dirsb, dirsb, fifosb;
	const char *testname;
	char path[] = "testfifo";
	int error;

	if (use_mkfifo)
		testname = "mkfifo";
	else
		testname = "mknod";

	/*
	 * Sleep to make sure that the time stamp on the directory will be
	 * updated.
	 */
	if (stat(".", &old_dirsb) < 0)
		err(-1, "basic_create_test: %s: stat: %s", testname,
		    temp_dir);

	sleep(2);

	if (use_mkfifo) {
		if (mkfifo(path, 0600) < 0)
			err(-1, "basic_create_test: %s: %s", testname, path);
	} else {
		if (mknod(path, S_IFIFO | 0600, 0) < 0)
			err(-1, "basic_create_test: %s: %s", testname, path);
	}

	if (stat(path, &fifosb) < 0) {
		error = errno;
		(void)unlink(path);
		errno = error;
		err(-1, "basic_create_test: %s: stat: %s", testname, path);
	}

	if (!(S_ISFIFO(fifosb.st_mode))) {
		(void)unlink(path);
		errx(-1, "basic_create_test: %s produced non-fifo",
		    testname);
	}

	if (use_mkfifo) {
		if (mkfifo(path, 0600) == 0)
			errx(-1, "basic_create_test: dup %s succeeded",
			    testname);
	} else {
		if (mknod(path, S_IFIFO | 0600, 0) == 0)
			errx(-1, "basic_create_test: dup %s succeeded",
			    testname);
	}

	if (errno != EEXIST)
		err(-1, "basic_create_test: dup %s unexpected error",
		    testname);

	if (stat(".", &dirsb) < 0) {
		error = errno;
		(void)unlink(path);
		errno = error;
		err(-1, "basic_create_test: %s: stat: %s", testname,
		    temp_dir);
	}

	if (old_dirsb.st_ctime == dirsb.st_ctime) {
		(void)unlink(path);
		errx(-1, "basic_create_test: %s: old_dirsb.st_ctime == "
		    "dirsb.st_ctime", testname);
	}

	if (old_dirsb.st_mtime == dirsb.st_mtime) {
		(void)unlink(path);
		errx(-1, "basic_create_test: %s: old_dirsb.st_mtime == "
		    "dirsb.st_mtime", testname);
	}

	if (unlink(path) < 0)
		err(-1, "basic_create_test: %s: unlink: %s", testname, path);

	if (stat(path, &fifosb) == 0)
		errx(-1, "basic_create_test: %s: unlink failed to unlink",
		    testname);
	if (errno != ENOENT)
		err(-1, "basic_create_test: %s: unlink unexpected error",
		    testname);
}

/*
 * Having determined that basic create/remove/etc functionality is present
 * for fifos, now make sure that the umask, requested permissions, and
 * resulting mode are handled properly.
 */
static const struct permission_test {
	mode_t	pt_umask;
	mode_t	pt_reqmode;
	mode_t	pt_mode;
} permission_test[] = {
	{0000, 0, S_IFIFO},
	{0000, S_IRWXU, S_IFIFO | S_IRWXU},
	{0000, S_IRWXU | S_IRWXG | S_IRWXO, S_IFIFO | S_IRWXU | S_IRWXG |
	    S_IRWXO },
	{0077, S_IRWXU, S_IFIFO | S_IRWXU},
	{0077, S_IRWXU | S_IRWXG | S_IRWXO, S_IFIFO | S_IRWXU},
};
static const int permission_test_count = sizeof(permission_test) /
    sizeof(struct permission_test);

static void
fifo_permission_test(int use_mkfifo)
{
	const struct permission_test *ptp;
	mode_t __unused old_umask;
	char path[] = "testfifo";
	const char *testname;
	struct stat sb;
	int error, i;

	if (use_mkfifo)
		testname = "mkfifo";
	else
		testname = "mknod";

	old_umask = umask(0022);
	for (i = 0; i < permission_test_count; i++) {
		ptp = &permission_test[i];

		umask(ptp->pt_umask);
		if (use_mkfifo) {
			if (mkfifo(path, ptp->pt_reqmode) < 0)
				err(-1, "fifo_permission_test: %s: %08o "
				    "%08o %08o\n", testname, ptp->pt_umask,
				    ptp->pt_reqmode, ptp->pt_mode);
		} else {
			if (mknod(path, S_IFIFO | ptp->pt_reqmode, 0) < 0)
				err(-1, "fifo_permission_test: %s: %08o "
				    "%08o %08o\n", testname, ptp->pt_umask,
				    ptp->pt_reqmode, ptp->pt_mode);
		}

		if (stat(path, &sb) < 0) {
			error = errno;
			(void)unlink(path);
			errno = error;
			err(-1, "fifo_permission_test: %s: %s", testname,
			    path);
		}

		if (sb.st_mode != ptp->pt_mode) {
			(void)unlink(path);
			errx(-1, "fifo_permission_test: %s: %08o %08o %08o "
			    "got %08o", testname, ptp->pt_umask,
			    ptp->pt_reqmode, ptp->pt_mode, sb.st_mode);
		}

		if (unlink(path) < 0)
			err(-1, "fifo_permission_test: %s: unlink: %s",
			    testname, path);
	}
	umask(old_umask);
}

int
main(void)
{
	int i;

	if (geteuid() != 0)
		errx(-1, "must be run as root");

	strcpy(temp_dir, "fifo_create.XXXXXXXXXXX");
	if (mkdtemp(temp_dir) == NULL)
		err(-1, "mkdtemp");
	atexit(atexit_temp_dir);

	if (chdir(temp_dir) < 0)
		err(-1, "chdir");

	/*
	 * Run each test twice, once with mknod(2) and a second time with
	 * mkfifo(2).  Historically, BSD has not allowed mknod(2) to be used
	 * to create fifos, but the Single UNIX Specification requires it.
	 */
	for (i = 0; i < 2; i++) {
		fifo_create_test(i);
		fifo_permission_test(i);
	}

	return (0);
}
OpenPOWER on IntegriCloud