1/*-
2 * Copyright (c) 2005 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 *    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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/wait.h>
32
33#include <err.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <limits.h>
37#include <signal.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43/*
44 * Regression test to exercise various POSIX-defined parts of fifo behavior
45 * described for open(2):
46 *
47 * O_NONBLOCK
48 * When opening a FIFO with O_RDONLY or O_WRONLY set:
49 *
50 * - If O_NONBLOCK is set, an open() for reading-only shall return without
51 *   delay. An open() for writing-only shall return an error if no process
52 *   currently has the file open for reading.
53 *
54 * - If O_NONBLOCK is clear, an open() for reading-only shall block the
55 *   calling thread until a thread opens the file for writing. An open()
56 *   for writing-only shall block the calling thread until a thread opens
57 *   the file for reading.
58 *
59 * When opening a block special or character special file that supports
60 * non-blocking opens:
61 *
62 * - If O_NONBLOCK is set, the open() function shall return without blocking
63 *   for the device to be ready or available. Subsequent behavior of the
64 *   device is device-specific.
65 *
66 * - If O_NONBLOCK is clear, the open() function shall block the calling
67 *   thread until the device is ready or available before returning.
68 *
69 * Special errors:
70 *
71 * [ENXIO]
72 * O_NONBLOCK is set, the named file is a FIFO, O_WRONLY is set, and no
73 * process has the file open for reading.
74 */
75
76/*
77 * In order to test blocking/non-blocking behavior, test processes must
78 * potentially block themselves until released.  As bugs in blocking result
79 * in processes that won't un-block, we must sacrifice a process to the task,
80 * watching and potentially killing it after a time-out.  The main test
81 * process is never used to open or act directly on a fifo (other than to
82 * create or unlink it) in order to avoid the main test process being
83 * blocked.
84 */
85
86/*
87 * All activity occurs within a temporary directory created early in the
88 * test.
89 */
90static char	temp_dir[PATH_MAX];
91
92static void __unused
93atexit_temp_dir(void)
94{
95
96	rmdir(temp_dir);
97}
98
99/*
100 * Run a function in a particular test process.
101 */
102static int
103run_in_process(int (*func)(void), pid_t *pidp, const char *errstr)
104{
105	pid_t pid;
106
107	pid = fork();
108	if (pid < 0) {
109		warn("%s: run_in_process: fork", errstr);
110		return (-1);
111	}
112
113	if (pid == 0)
114		exit(func());
115
116	if (pidp != NULL)
117		*pidp = pid;
118
119	return (0);
120}
121
122/*
123 * Wait for a process on a timeout, and if the timeout expires, kill the
124 * process.  Test each second rather than waiting the full timeout at once to
125 * minimize the amount of time spent hanging around unnecessarily.
126 */
127static int
128wait_and_timeout(pid_t pid, int timeout, int *status, const char *errstr)
129{
130	pid_t wpid;
131	int i;
132
133	/*
134	 * Count up to the timeout, but do a non-hanging waitpid() after each
135	 * second so we can avoid waiting a lot of extra time.
136	 */
137	for (i = 0; i < timeout; i++) {
138		wpid = waitpid(pid, status, WNOHANG);
139		if (wpid < 0) {
140			warn("%s: wait_and_timeout: waitpid %d", errstr, pid);
141			return (-1);
142		}
143
144		if (wpid == pid)
145			return (0);
146
147		sleep(1);
148	}
149
150	wpid = waitpid(pid, status, WNOHANG);
151	if (wpid < 0) {
152		warn("%s: wait_and_timeout: waitpid %d", errstr, pid);
153		return (-1);
154	}
155
156	if (wpid == pid)
157		return (0);
158
159	if (kill(pid, SIGTERM) < 0) {
160		warn("%s: wait_and_timeout: kill %d", errstr, pid);
161		return (-1);
162	}
163
164	wpid = waitpid(pid, status, 0);
165	if (wpid < 0) {
166		warn("%s: wait_and_timeout: waitpid %d", errstr, pid);
167		return (-1);
168	}
169
170	if (wpid != pid) {
171		warn("%s: waitpid: returned %d not %d", errstr, wpid, pid);
172		return (-1);
173	}
174
175	warnx("%s: process blocked", errstr);
176	return (-1);
177}
178
179static int
180non_blocking_open_reader(void)
181{
182	int fd;
183
184	fd = open("testfifo", O_RDONLY | O_NONBLOCK);
185	if (fd < 0)
186		return (errno);
187	close(fd);
188
189	return (0);
190}
191
192static int
193non_blocking_open_writer(void)
194{
195	int fd;
196
197	fd = open("testfifo", O_WRONLY | O_NONBLOCK);
198	if (fd < 0)
199		return (errno);
200	close(fd);
201
202	return (0);
203}
204
205static int
206blocking_open_reader(void)
207{
208	int fd;
209
210	fd = open("testfifo", O_RDONLY);
211	if (fd < 0)
212		return (errno);
213	close(fd);
214
215	return (0);
216}
217
218static int
219blocking_open_writer(void)
220{
221	int fd;
222
223	fd = open("testfifo", O_WRONLY);
224	if (fd < 0)
225		return (errno);
226	close(fd);
227
228	return (0);
229}
230
231static void
232test_blocking_reader(void)
233{
234	pid_t reader_pid, writer_pid, wpid;
235	int error, status;
236
237	if (mkfifo("testfifo", 0600) < 0)
238		err(-1, "test_blocking_reader: mkfifo: testfifo");
239
240	/*
241	 * Block a process in opening the fifo.
242	 */
243	if (run_in_process(blocking_open_reader, &reader_pid,
244	    "test_blocking_reader: blocking_open_reader") < 0) {
245		(void)unlink("testfifo");
246		exit(-1);
247	}
248
249	/*
250	 * Test that it blocked.
251	 */
252	sleep(5);
253	wpid = waitpid(reader_pid, &status, WNOHANG);
254	if (wpid < 0) {
255		error = errno;
256		(void)unlink("testfifo");
257		errno = error;
258		err(-1, "test_blocking_reader: waitpid %d", reader_pid);
259	}
260
261	if (wpid != 0 && wpid != reader_pid) {
262		(void)unlink("testfifo");
263		errx(-1, "test_blocking_reader: waitpid %d returned %d",
264		    reader_pid, wpid);
265	}
266
267	if (wpid == reader_pid) {
268		(void)unlink("testfifo");
269		errx(-1, "test_blocking_reader: blocking child didn't "
270		    "block");
271	}
272
273	/*
274	 * Unblock the blocking reader.
275	 */
276	if (run_in_process(blocking_open_writer, &writer_pid,
277	    "test_blocking_reader: blocking_open_writer") < 0) {
278		(void)unlink("testfifo");
279		(void)kill(reader_pid, SIGTERM);
280		(void)waitpid(reader_pid, &status, 0);
281		exit(-1);
282	}
283
284	/*
285	 * Make sure both processes exited quickly (<1 second) to make sure
286	 * they didn't block, and GC.
287	 */
288	if (wait_and_timeout(reader_pid, 1, &status,
289	    "test_blocking_reader: blocking_open_reader") < 0) {
290		(void)unlink("testinfo");
291		(void)kill(reader_pid, SIGTERM);
292		(void)kill(writer_pid, SIGTERM);
293		exit(-1);
294	}
295
296	if (wait_and_timeout(writer_pid, 1, &status,
297	    "test_blocking_reader: blocking_open_writer") < 0) {
298		(void)unlink("testinfo");
299		(void)kill(writer_pid, SIGTERM);
300		exit(-1);
301	}
302
303	if (unlink("testfifo") < 0)
304		err(-1, "test_blocking_reader: unlink: testfifo");
305}
306static void
307test_blocking_writer(void)
308{
309	pid_t reader_pid, writer_pid, wpid;
310	int error, status;
311
312	if (mkfifo("testfifo", 0600) < 0)
313		err(-1, "test_blocking_writer: mkfifo: testfifo");
314
315	/*
316	 * Block a process in opening the fifo.
317	 */
318	if (run_in_process(blocking_open_writer, &writer_pid,
319	    "test_blocking_writer: blocking_open_writer") < 0) {
320		(void)unlink("testfifo");
321		exit(-1);
322	}
323
324	/*
325	 * Test that it blocked.
326	 */
327	sleep(5);
328	wpid = waitpid(writer_pid, &status, WNOHANG);
329	if (wpid < 0) {
330		error = errno;
331		(void)unlink("testfifo");
332		errno = error;
333		err(-1, "test_blocking_writer: waitpid %d", writer_pid);
334	}
335
336	if (wpid != 0 && wpid != writer_pid) {
337		(void)unlink("testfifo");
338		errx(-1, "test_blocking_writer: waitpid %d returned %d",
339		    writer_pid, wpid);
340	}
341
342	if (wpid == writer_pid) {
343		(void)unlink("testfifo");
344		errx(-1, "test_blocking_writer: blocking child didn't "
345		    "block");
346	}
347
348	/*
349	 * Unblock the blocking writer.
350	 */
351	if (run_in_process(blocking_open_reader, &reader_pid,
352	    "test_blocking_writer: blocking_open_reader") < 0) {
353		(void)unlink("testfifo");
354		(void)kill(writer_pid, SIGTERM);
355		(void)waitpid(writer_pid, &status, 0);
356		exit(-1);
357	}
358
359	/*
360	 * Make sure both processes exited quickly (<1 second) to make sure
361	 * they didn't block, and GC.
362	 */
363	if (wait_and_timeout(writer_pid, 1, &status,
364	    "test_blocking_writer: blocking_open_writer") < 0) {
365		(void)unlink("testinfo");
366		(void)kill(writer_pid, SIGTERM);
367		(void)kill(reader_pid, SIGTERM);
368		(void)waitpid(writer_pid, &status, 0);
369		(void)waitpid(reader_pid, &status, 0);
370		exit(-1);
371	}
372
373	if (wait_and_timeout(reader_pid, 1, &status,
374	    "test_blocking_writer: blocking_open_reader") < 0) {
375		(void)unlink("testinfo");
376		(void)kill(reader_pid, SIGTERM);
377		(void)waitpid(reader_pid, &status, 0);
378		exit(-1);
379	}
380
381	if (unlink("testfifo") < 0)
382		err(-1, "test_blocking_writer: unlink: testfifo");
383}
384
385static void
386test_non_blocking_reader(void)
387{
388	int status;
389	pid_t pid;
390
391	if (mkfifo("testfifo", 0600) < 0)
392		err(-1, "test_non_blocking_reader: mkfifo: testfifo");
393
394	if (run_in_process(non_blocking_open_reader, &pid,
395	    "test_non_blocking_reader: non_blocking_open_reader") < 0) {
396		(void)unlink("testfifo");
397		exit(-1);
398	}
399
400	status = -1;
401	if (wait_and_timeout(pid, 5, &status,
402	    "test_non_blocking_reader: non_blocking_open_reader") < 0) {
403		(void)unlink("testfifo");
404		exit(-1);
405	}
406
407	if (WEXITSTATUS(status) != 0) {
408		(void)unlink("testfifo");
409		errno = WEXITSTATUS(status);
410		err(-1, "test_non_blocking_reader: "
411		    "non_blocking_open_reader: open: testfifo");
412	}
413
414	if (unlink("testfifo") < 0)
415		err(-1, "test_non_blocking_reader: unlink: testfifo");
416}
417
418static void
419test_non_blocking_writer(void)
420{
421	int status;
422	pid_t pid;
423
424	if (mkfifo("testfifo", 0600) < 0)
425		err(-1, "test_non_blocking_writer: mkfifo: testfifo");
426
427	if (run_in_process(non_blocking_open_writer, &pid,
428	    "test_non_blocking_writer: non_blocking_open_writer") < 0) {
429		(void)unlink("testfifo");
430		exit(-1);
431	}
432
433	status = -1;
434	if (wait_and_timeout(pid, 5, &status,
435	    "test_non_blocking_writer: non_blocking_open_writer") < 0) {
436		(void)unlink("testfifo");
437		exit(-1);
438	}
439
440	if (WEXITSTATUS(status) != ENXIO) {
441		(void)unlink("testfifo");
442
443		errno = WEXITSTATUS(status);
444		if (errno == 0)
445			errx(-1, "test_non_blocking_writer: "
446			    "non_blocking_open_writer: open succeeded");
447		err(-1, "test_non_blocking_writer: "
448		    "non_blocking_open_writer: open: testfifo");
449	}
450
451	if (unlink("testfifo") < 0)
452		err(-1, "test_non_blocking_writer: unlink: testfifo");
453}
454
455int
456main(void)
457{
458
459	if (geteuid() != 0)
460		errx(-1, "must be run as root");
461
462	strcpy(temp_dir, "fifo_open.XXXXXXXXXXX");
463	if (mkdtemp(temp_dir) == NULL)
464		err(-1, "mkdtemp");
465	if (chdir(temp_dir) < 0)
466		err(-1, "chdir: %s", temp_dir);
467	atexit(atexit_temp_dir);
468
469	test_non_blocking_reader();
470	test_non_blocking_writer();
471
472	test_blocking_reader();
473	test_blocking_writer();
474
475	return (0);
476}
477