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
29 #include <sys/types.h>
44 * Regression test to exercise various POSIX-defined parts of fifo behavior
45 * described for open(2):
48 * When opening a FIFO with O_RDONLY or O_WRONLY set:
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.
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.
59 * When opening a block special or character special file that supports
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.
66 * - If O_NONBLOCK is clear, the open() function shall block the calling
67 * thread until the device is ready or available before returning.
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.
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
87 * All activity occurs within a temporary directory created early in the
90 static char temp_dir[PATH_MAX];
100 * Run a function in a particular test process.
103 run_in_process(int (*func)(void), pid_t *pidp, const char *errstr)
109 warn("%s: run_in_process: fork", errstr);
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.
128 wait_and_timeout(pid_t pid, int timeout, int *status, const char *errstr)
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.
137 for (i = 0; i < timeout; i++) {
138 wpid = waitpid(pid, status, WNOHANG);
140 warn("%s: wait_and_timeout: waitpid %d", errstr, pid);
150 wpid = waitpid(pid, status, WNOHANG);
152 warn("%s: wait_and_timeout: waitpid %d", errstr, pid);
159 if (kill(pid, SIGTERM) < 0) {
160 warn("%s: wait_and_timeout: kill %d", errstr, pid);
164 wpid = waitpid(pid, status, 0);
166 warn("%s: wait_and_timeout: waitpid %d", errstr, pid);
171 warn("%s: waitpid: returned %d not %d", errstr, wpid, pid);
175 warnx("%s: process blocked", errstr);
180 non_blocking_open_reader(void)
184 fd = open("testfifo", O_RDONLY | O_NONBLOCK);
193 non_blocking_open_writer(void)
197 fd = open("testfifo", O_WRONLY | O_NONBLOCK);
206 blocking_open_reader(void)
210 fd = open("testfifo", O_RDONLY);
219 blocking_open_writer(void)
223 fd = open("testfifo", O_WRONLY);
232 test_blocking_reader(void)
234 pid_t reader_pid, writer_pid, wpid;
237 if (mkfifo("testfifo", 0600) < 0)
238 err(-1, "test_blocking_reader: mkfifo: testfifo");
241 * Block a process in opening the fifo.
243 if (run_in_process(blocking_open_reader, &reader_pid,
244 "test_blocking_reader: blocking_open_reader") < 0) {
245 (void)unlink("testfifo");
250 * Test that it blocked.
253 wpid = waitpid(reader_pid, &status, WNOHANG);
256 (void)unlink("testfifo");
258 err(-1, "test_blocking_reader: waitpid %d", reader_pid);
261 if (wpid != 0 && wpid != reader_pid) {
262 (void)unlink("testfifo");
263 errx(-1, "test_blocking_reader: waitpid %d returned %d",
267 if (wpid == reader_pid) {
268 (void)unlink("testfifo");
269 errx(-1, "test_blocking_reader: blocking child didn't "
274 * Unblock the blocking reader.
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);
285 * Make sure both processes exited quickly (<1 second) to make sure
286 * they didn't block, and GC.
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);
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);
303 if (unlink("testfifo") < 0)
304 err(-1, "test_blocking_reader: unlink: testfifo");
307 test_blocking_writer(void)
309 pid_t reader_pid, writer_pid, wpid;
312 if (mkfifo("testfifo", 0600) < 0)
313 err(-1, "test_blocking_writer: mkfifo: testfifo");
316 * Block a process in opening the fifo.
318 if (run_in_process(blocking_open_writer, &writer_pid,
319 "test_blocking_writer: blocking_open_writer") < 0) {
320 (void)unlink("testfifo");
325 * Test that it blocked.
328 wpid = waitpid(writer_pid, &status, WNOHANG);
331 (void)unlink("testfifo");
333 err(-1, "test_blocking_writer: waitpid %d", writer_pid);
336 if (wpid != 0 && wpid != writer_pid) {
337 (void)unlink("testfifo");
338 errx(-1, "test_blocking_writer: waitpid %d returned %d",
342 if (wpid == writer_pid) {
343 (void)unlink("testfifo");
344 errx(-1, "test_blocking_writer: blocking child didn't "
349 * Unblock the blocking writer.
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);
360 * Make sure both processes exited quickly (<1 second) to make sure
361 * they didn't block, and GC.
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);
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);
381 if (unlink("testfifo") < 0)
382 err(-1, "test_blocking_writer: unlink: testfifo");
386 test_non_blocking_reader(void)
391 if (mkfifo("testfifo", 0600) < 0)
392 err(-1, "test_non_blocking_reader: mkfifo: testfifo");
394 if (run_in_process(non_blocking_open_reader, &pid,
395 "test_non_blocking_reader: non_blocking_open_reader") < 0) {
396 (void)unlink("testfifo");
401 if (wait_and_timeout(pid, 5, &status,
402 "test_non_blocking_reader: non_blocking_open_reader") < 0) {
403 (void)unlink("testfifo");
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");
414 if (unlink("testfifo") < 0)
415 err(-1, "test_non_blocking_reader: unlink: testfifo");
419 test_non_blocking_writer(void)
424 if (mkfifo("testfifo", 0600) < 0)
425 err(-1, "test_non_blocking_writer: mkfifo: testfifo");
427 if (run_in_process(non_blocking_open_writer, &pid,
428 "test_non_blocking_writer: non_blocking_open_writer") < 0) {
429 (void)unlink("testfifo");
434 if (wait_and_timeout(pid, 5, &status,
435 "test_non_blocking_writer: non_blocking_open_writer") < 0) {
436 (void)unlink("testfifo");
440 if (WEXITSTATUS(status) != ENXIO) {
441 (void)unlink("testfifo");
443 errno = WEXITSTATUS(status);
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");
451 if (unlink("testfifo") < 0)
452 err(-1, "test_non_blocking_writer: unlink: testfifo");
460 errx(-1, "must be run as root");
462 strcpy(temp_dir, "fifo_open.XXXXXXXXXXX");
463 if (mkdtemp(temp_dir) == NULL)
465 if (chdir(temp_dir) < 0)
466 err(-1, "chdir: %s", temp_dir);
467 atexit(atexit_temp_dir);
469 test_non_blocking_reader();
470 test_non_blocking_writer();
472 test_blocking_reader();
473 test_blocking_writer();