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>
30 #include <sys/event.h>
31 #include <sys/ioctl.h>
32 #include <sys/select.h>
48 * Regression test to exercise POSIX fifo I/O.
50 * We test a number of aspect of behavior, including:
52 * - If there's no data to read, then for blocking fifos, we block, and for
53 * non-blocking, we return EAGAIN.
55 * - If we write ten bytes, ten bytes can be read, and they're the same
56 * bytes, in the same order.
58 * - If we write two batches of five bytes, we can read the same ten bytes in
59 * one read of ten bytes.
61 * - If we write ten bytes, we can read the same ten bytes in two reads of
64 * - If we over-fill a buffer (by writing 512k, which we take to be a large
65 * number above default buffer sizes), we block if there is no reader.
67 * - That once 512k (ish) is read from the other end, the blocked writer
70 * - When a fifo is empty, poll, select, kqueue, and fionread report it is
71 * writable but not readable.
73 * - When a fifo has data in it, poll, select, and kqueue report that it is
76 * - XXX: blocked reader semantics?
78 * - XXX: event behavior on remote close?
80 * Although behavior of O_RDWR isn't defined for fifos by POSIX, we expect
81 * "reasonable" behavior, and run some additional tests relating to event
82 * management on O_RDWR fifo descriptors.
85 #define KQUEUE_MAX_EVENT 8
88 * All activity occurs within a temporary directory created early in the
91 char temp_dir[PATH_MAX];
101 makefifo(const char *fifoname, const char *testname)
104 if (mkfifo(fifoname, 0700) < 0)
105 err(-1, "%s: makefifo: mkfifo: %s", testname, fifoname);
109 cleanfifo2(const char *fifoname, int fd1, int fd2)
116 (void)unlink(fifoname);
120 cleanfifo3(const char *fifoname, int fd1, int fd2, int fd3)
125 cleanfifo2(fifoname, fd1, fd2);
129 * Open two different file descriptors for a fifo: one read, one write. Do
130 * so using non-blocking opens in order to avoid deadlocking the process.
133 openfifo(const char *fifoname, const char *testname, int *reader_fdp,
138 fd1 = open(fifoname, O_RDONLY | O_NONBLOCK);
141 fd2 = open(fifoname, O_WRONLY | O_NONBLOCK);
155 * Open one file descriptor for the fifo, supporting both read and write.
158 openfifo_rw(const char *fifoname, const char *testname, int *fdp)
162 fd = open(fifoname, O_RDWR);
171 set_nonblocking(int fd, const char *testname)
175 flags = fcntl(fd, F_GETFL);
177 warn("%s: fcntl(fd, F_GETFL)", testname);
183 if (fcntl(fd, F_SETFL, flags) < 0) {
184 warn("%s: fcntl(fd, 0x%x)", testname, flags);
192 set_blocking(int fd, const char *testname)
196 flags = fcntl(fd, F_GETFL);
198 warn("%s: fcntl(fd, F_GETFL)", testname);
202 flags &= ~O_NONBLOCK;
204 if (fcntl(fd, F_SETFL, flags) < 0) {
205 warn("%s: fcntl(fd, 0x%x)", testname, flags);
213 * Drain a file descriptor (fifo) of any readable data. Note: resets the
217 drain_fd(int fd, const char *testname)
222 if (set_nonblocking(fd, testname) < 0)
225 while ((len = read(fd, &ch, sizeof(ch))) > 0);
231 warn("%s: drain_fd: read", testname);
235 warn("%s: drain_fd: read: returned 0 bytes", testname);
240 * Simple I/O test: write ten integers, and make sure we get back the same
241 * integers in the same order. This assumes a minimum fifo buffer > 10
242 * bytes in order to not block and deadlock.
247 int i, reader_fd, writer_fd;
251 makefifo("testfifo", __func__);
252 if (openfifo("testfifo", "test_simpleio", &reader_fd, &writer_fd)
254 warn("test_simpleio: openfifo: testfifo");
255 cleanfifo2("testfifo", -1, -1);
259 for (i = 0; i < 10; i++)
262 len = write(writer_fd, (char *)buffer, sizeof(buffer));
264 warn("test_simpleio: write");
265 cleanfifo2("testfifo", reader_fd, writer_fd);
268 if (len != sizeof(buffer)) {
269 warnx("test_simplio: tried %zu but wrote %zd", sizeof(buffer),
271 cleanfifo2("testfifo", reader_fd, writer_fd);
275 len = read(reader_fd, (char *)buffer, sizeof(buffer));
277 warn("test_simpleio: read");
278 cleanfifo2("testfifo", reader_fd, writer_fd);
281 if (len != sizeof(buffer)) {
282 warnx("test_simpleio: tried %zu but read %zd", sizeof(buffer),
284 cleanfifo2("testfifo", reader_fd, writer_fd);
287 for (i = 0; i < 10; i++) {
290 warnx("test_simpleio: write byte %d as 0x%02x, but read "
291 "0x%02x", i, i, buffer[i]);
292 cleanfifo2("testfifo", reader_fd, writer_fd);
296 cleanfifo2("testfifo", reader_fd, writer_fd);
299 static int alarm_fired;
301 * Non-destructive SIGALRM handler.
311 * Wrapper function for write, which uses a timer to interrupt any blocking.
312 * Because we can't reliably detect EINTR for blocking I/O, we also track
313 * whether or not our timeout fired.
316 timed_write(int fd, void *data, size_t len, ssize_t *written_lenp,
317 int timeout, int *timedoutp, const char *testname)
319 struct sigaction act, oact;
324 bzero(&act, sizeof(oact));
325 act.sa_handler = sigalarm;
326 if (sigaction(SIGALRM, &act, &oact) < 0) {
327 warn("%s: timed_write: sigaction", testname);
331 written_len = write(fd, data, len);
334 if (sigaction(SIGALRM, &oact, NULL) < 0) {
335 warn("%s: timed_write: sigaction", testname);
346 *written_lenp = written_len;
351 * Wrapper function for read, which uses a timer to interrupt any blocking.
352 * Because we can't reliably detect EINTR for blocking I/O, we also track
353 * whether or not our timeout fired.
356 timed_read(int fd, void *data, size_t len, ssize_t *read_lenp,
357 int timeout, int *timedoutp, const char *testname)
359 struct sigaction act, oact;
364 bzero(&act, sizeof(oact));
365 act.sa_handler = sigalarm;
366 if (sigaction(SIGALRM, &act, &oact) < 0) {
367 warn("%s: timed_write: sigaction", testname);
371 read_len = read(fd, data, len);
374 if (sigaction(SIGALRM, &oact, NULL) < 0) {
375 warn("%s: timed_write: sigaction", testname);
386 *read_lenp = read_len;
391 * This test operates on blocking and non-blocking fifo file descriptors, in
392 * order to determine whether they block at good moments or not. By good we
393 * mean: don't block for non-blocking sockets, and do block for blocking
394 * ones, assuming there isn't I/O buffer to satisfy the request.
396 * We use a timeout of 5 seconds, concluding that in 5 seconds either all I/O
397 * that can take place will, and that if we reach the end of the timeout,
398 * then blocking has occured.
400 * We assume that the buffer size on a fifo is <512K, and as such, that
401 * writing that much data without an active reader will result in blocking.
404 test_blocking_read_empty(void)
406 int reader_fd, ret, timedout, writer_fd;
410 makefifo("testfifo", __func__);
411 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
413 warn("test_blocking_read_empty: openfifo: testfifo");
414 cleanfifo2("testfifo", -1, -1);
419 * Read one byte from an empty blocking fifo, block as there is no
422 if (set_blocking(reader_fd, __func__) < 0) {
423 cleanfifo2("testfifo", reader_fd, writer_fd);
427 ret = timed_read(reader_fd, &ch, sizeof(ch), &len, 5, &timedout,
430 warnx("test_blocking_read_empty: timed_read: returned "
432 cleanfifo2("testfifo", reader_fd, writer_fd);
435 if (errno != EINTR) {
436 warn("test_blocking_read_empty: timed_read");
437 cleanfifo2("testfifo", reader_fd, writer_fd);
442 * Read one byte from an empty non-blocking fifo, return EAGAIN as
445 if (set_nonblocking(reader_fd, __func__) < 0) {
446 cleanfifo2("testfifo", reader_fd, writer_fd);
450 ret = timed_read(reader_fd, &ch, sizeof(ch), &len, 5, &timedout,
453 warnx("test_blocking_read_empty: timed_read: returned "
455 cleanfifo2("testfifo", reader_fd, writer_fd);
458 if (errno != EAGAIN) {
459 warn("test_blocking_read_empty: timed_read");
460 cleanfifo2("testfifo", reader_fd, writer_fd);
464 cleanfifo2("testfifo", reader_fd, writer_fd);
468 * Write one byte to an empty fifo, then try to read one byte and make sure
469 * we don't block in either the write or the read. This tests both for
470 * improper blocking in the send and receive code.
473 test_blocking_one_byte(void)
475 int reader_fd, ret, timedout, writer_fd;
479 makefifo("testfifo", __func__);
480 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
482 warn("test_blocking: openfifo: testfifo");
483 cleanfifo2("testfifo", -1, -1);
487 if (set_blocking(writer_fd, __func__) < 0) {
488 cleanfifo2("testfifo", reader_fd, writer_fd);
491 if (set_blocking(reader_fd, __func__) < 0) {
492 cleanfifo2("testfifo", reader_fd, writer_fd);
497 ret = timed_write(writer_fd, &ch, sizeof(ch), &len, 5, &timedout,
500 warn("test_blocking_one_byte: timed_write");
501 cleanfifo2("testfifo", reader_fd, writer_fd);
504 if (len != sizeof(ch)) {
505 warnx("test_blocking_one_byte: timed_write: tried to write "
506 "%zu, wrote %zd", sizeof(ch), len);
507 cleanfifo2("testfifo", reader_fd, writer_fd);
512 ret = timed_read(reader_fd, &ch, sizeof(ch), &len, 5, &timedout,
515 warn("test_blocking_one_byte: timed_read");
516 cleanfifo2("testfifo", reader_fd, writer_fd);
519 if (len != sizeof(ch)) {
520 warnx("test_blocking_one_byte: timed_read: wanted %zu, "
521 "read %zd", sizeof(ch), len);
522 cleanfifo2("testfifo", reader_fd, writer_fd);
526 warnx("test_blocking_one_byte: timed_read: expected to read "
527 "0x%02x, read 0x%02x", 0xfe, ch);
528 cleanfifo2("testfifo", reader_fd, writer_fd);
532 cleanfifo2("testfifo", reader_fd, writer_fd);
536 * Write one byte to an empty fifo, then try to read one byte and make sure
537 * we don't get back EAGAIN.
540 test_nonblocking_one_byte(void)
542 int reader_fd, ret, timedout, writer_fd;
546 makefifo("testfifo", __func__);
547 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
549 warn("test_nonblocking: openfifo: testfifo");
550 cleanfifo2("testfifo", -1, -1);
554 if (set_nonblocking(reader_fd, __func__) < 0) {
555 cleanfifo2("testfifo", reader_fd, writer_fd);
560 ret = timed_write(writer_fd, &ch, sizeof(ch), &len, 5, &timedout,
563 warn("test_nonblocking_one_byte: timed_write");
564 cleanfifo2("testfifo", reader_fd, writer_fd);
567 if (len != sizeof(ch)) {
568 warnx("test_nonblocking_one_byte: timed_write: tried to write "
569 "%zu, wrote %zd", sizeof(ch), len);
570 cleanfifo2("testfifo", reader_fd, writer_fd);
575 ret = timed_read(reader_fd, &ch, sizeof(ch), &len, 5, &timedout,
578 warn("test_nonblocking_one_byte: timed_read");
579 cleanfifo2("testfifo", reader_fd, writer_fd);
582 if (len != sizeof(ch)) {
583 warnx("test_nonblocking_one_byte: timed_read: wanted %zu, read "
584 "%zd", sizeof(ch), len);
585 cleanfifo2("testfifo", reader_fd, writer_fd);
589 warnx("test_nonblocking_one_byte: timed_read: expected to read "
590 "0x%02x, read 0x%02x", 0xfe, ch);
591 cleanfifo2("testfifo", reader_fd, writer_fd);
595 cleanfifo2("testfifo", reader_fd, writer_fd);
599 * First of two test cases involving a 512K buffer: write the buffer into a
600 * blocking file descriptor. We'd like to know it blocks, but the closest we
601 * can get is to see if SIGALRM fired during the I/O resulting in a partial
605 test_blocking_partial_write(void)
607 int reader_fd, ret, timedout, writer_fd;
611 makefifo("testfifo", __func__);
612 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
614 warn("test_blocking_partial_write: openfifo: testfifo");
615 cleanfifo2("testfifo", -1, -1);
619 if (set_blocking(writer_fd, __func__) < 0) {
620 cleanfifo2("testfifo", reader_fd, writer_fd);
624 buffer = malloc(512*1024);
625 if (buffer == NULL) {
626 warn("test_blocking_partial_write: malloc");
627 cleanfifo2("testfifo", reader_fd, writer_fd);
630 bzero(buffer, 512*1024);
632 ret = timed_write(writer_fd, buffer, 512*1024, &len, 5, &timedout,
635 warn("test_blocking_partial_write: timed_write");
637 cleanfifo2("testfifo", reader_fd, writer_fd);
642 warnx("test_blocking_partial_write: timed_write: blocking "
643 "socket didn't time out");
645 cleanfifo2("testfifo", reader_fd, writer_fd);
651 if (drain_fd(reader_fd, __func__) < 0) {
652 cleanfifo2("testfifo", reader_fd, writer_fd);
656 cleanfifo2("testfifo", reader_fd, writer_fd);
660 * Write a 512K buffer to an empty fifo using a non-blocking file descriptor,
661 * and make sure it doesn't block.
664 test_nonblocking_partial_write(void)
666 int reader_fd, ret, timedout, writer_fd;
670 makefifo("testfifo", __func__);
671 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
673 warn("test_blocking_partial_write: openfifo: testfifo");
674 cleanfifo2("testfifo", -1, -1);
678 if (set_nonblocking(writer_fd, __func__) < 0) {
679 cleanfifo2("testfifo", reader_fd, writer_fd);
683 buffer = malloc(512*1024);
684 if (buffer == NULL) {
685 warn("test_blocking_partial_write: malloc");
686 cleanfifo2("testfifo", reader_fd, writer_fd);
689 bzero(buffer, 512*1024);
691 ret = timed_write(writer_fd, buffer, 512*1024, &len, 5, &timedout,
694 warn("test_blocking_partial_write: timed_write");
696 cleanfifo2("testfifo", reader_fd, writer_fd);
701 warnx("test_blocking_partial_write: timed_write: "
702 "non-blocking socket timed out");
704 cleanfifo2("testfifo", reader_fd, writer_fd);
708 if (len == 0 || len >= 512*1024) {
709 warnx("test_blocking_partial_write: timed_write: requested "
710 "%d, sent %zd", 512*1024, len);
712 cleanfifo2("testfifo", reader_fd, writer_fd);
718 if (drain_fd(reader_fd, __func__) < 0) {
719 cleanfifo2("testfifo", reader_fd, writer_fd);
723 cleanfifo2("testfifo", reader_fd, writer_fd);
727 * test_coalesce_big_read() verifies that data mingles in the fifo across
728 * message boundaries by performing two small writes, then a bigger read
729 * that should return data from both writes.
732 test_coalesce_big_read(void)
734 int i, reader_fd, writer_fd;
738 makefifo("testfifo", __func__);
739 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
741 warn("test_coalesce_big_read: openfifo: testfifo");
742 cleanfifo2("testfifo", -1, -1);
746 /* Write five, write five, read ten. */
747 for (i = 0; i < 10; i++)
750 len = write(writer_fd, buffer, 5);
752 warn("test_coalesce_big_read: write 5");
753 cleanfifo2("testfifo", reader_fd, writer_fd);
757 warnx("test_coalesce_big_read: write 5 wrote %zd", len);
758 cleanfifo2("testfifo", reader_fd, writer_fd);
762 len = write(writer_fd, buffer + 5, 5);
764 warn("test_coalesce_big_read: write 5");
765 cleanfifo2("testfifo", reader_fd, writer_fd);
769 warnx("test_coalesce_big_read: write 5 wrote %zd", len);
770 cleanfifo2("testfifo", reader_fd, writer_fd);
774 len = read(reader_fd, buffer, 10);
776 warn("test_coalesce_big_read: read 10");
777 cleanfifo2("testfifo", reader_fd, writer_fd);
781 warnx("test_coalesce_big_read: read 10 read %zd", len);
782 cleanfifo2("testfifo", reader_fd, writer_fd);
786 for (i = 0; i < 10; i++) {
789 warnx("test_coalesce_big_read: expected to read 0x%02x, "
790 "read 0x%02x", i, buffer[i]);
791 cleanfifo2("testfifo", reader_fd, writer_fd);
795 cleanfifo2("testfifo", -1, -1);
799 * test_coalesce_big_write() verifies that data mingles in the fifo across
800 * message boundaries by performing one big write, then two smaller reads
801 * that should return sequential elements of data from the write.
804 test_coalesce_big_write(void)
806 int i, reader_fd, writer_fd;
810 makefifo("testfifo", __func__);
811 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
813 warn("test_coalesce_big_write: openfifo: testfifo");
814 cleanfifo2("testfifo", -1, -1);
818 /* Write ten, read five, read five. */
819 for (i = 0; i < 10; i++)
822 len = write(writer_fd, buffer, 10);
824 warn("test_coalesce_big_write: write 10");
825 cleanfifo2("testfifo", reader_fd, writer_fd);
829 warnx("test_coalesce_big_write: write 10 wrote %zd", len);
830 cleanfifo2("testfifo", reader_fd, writer_fd);
834 len = read(reader_fd, buffer, 5);
836 warn("test_coalesce_big_write: read 5");
837 cleanfifo2("testfifo", reader_fd, writer_fd);
841 warnx("test_coalesce_big_write: read 5 read %zd", len);
842 cleanfifo2("testfifo", reader_fd, writer_fd);
846 len = read(reader_fd, buffer + 5, 5);
848 warn("test_coalesce_big_write: read 5");
849 cleanfifo2("testfifo", reader_fd, writer_fd);
853 warnx("test_coalesce_big_write: read 5 read %zd", len);
854 cleanfifo2("testfifo", reader_fd, writer_fd);
858 for (i = 0; i < 10; i++) {
861 warnx("test_coalesce_big_write: expected to read 0x%02x, "
862 "read 0x%02x", i, buffer[i]);
863 cleanfifo2("testfifo", reader_fd, writer_fd);
867 cleanfifo2("testfifo", -1, -1);
871 poll_status(int fd, int *readable, int *writable, int *exception,
872 const char *testname)
874 struct pollfd fds[1];
877 fds[0].events = POLLIN | POLLOUT | POLLERR;
880 if (poll(fds, 1, 0) < 0) {
881 warn("%s: poll", testname);
884 *readable = (fds[0].revents & POLLIN) ? 1 : 0;
885 *writable = (fds[0].revents & POLLOUT) ? 1 : 0;
886 *exception = (fds[0].revents & POLLERR) ? 1 : 0;
891 select_status(int fd, int *readable, int *writable, int *exception,
892 const char *testname)
894 struct fd_set readfds, writefds, exceptfds;
895 struct timeval timeout;
900 FD_SET(fd, &readfds);
901 FD_SET(fd, &writefds);
902 FD_SET(fd, &exceptfds);
905 if (select(fd+1, &readfds, &writefds, &exceptfds, &timeout) < 0) {
906 warn("%s: select", testname);
909 *readable = FD_ISSET(fd, &readfds) ? 1 : 0;
910 *writable = FD_ISSET(fd, &writefds) ? 1 : 0;
911 *exception = FD_ISSET(fd, &exceptfds) ? 1 : 0;
916 * Given an existing kqueue, set up read and write event filters for the
917 * passed file descriptor. Typically called once for the read endpoint, and
918 * once for the write endpoint.
921 kqueue_setup(int kqueue_fd, int fd, const char *testname)
923 struct kevent kevent_changelist[2];
924 struct kevent kevent_eventlist[KQUEUE_MAX_EVENT], *kp;
925 struct timespec timeout;
931 bzero(&kevent_changelist, sizeof(kevent_changelist));
932 EV_SET(&kevent_changelist[0], fd, EVFILT_READ, EV_ADD, 0, 0, 0);
933 EV_SET(&kevent_changelist[1], fd, EVFILT_WRITE, EV_ADD, 0, 0, 0);
935 bzero(&kevent_eventlist, sizeof(kevent_eventlist));
936 ret = kevent(kqueue_fd, kevent_changelist, 2, kevent_eventlist,
937 KQUEUE_MAX_EVENT, &timeout);
939 warn("%s:%s: kevent initial register", testname, __func__);
944 * Verify that the events registered alright.
946 for (i = 0; i < ret; i++) {
947 kp = &kevent_eventlist[i];
948 if (kp->flags != EV_ERROR)
951 warn("%s:%s: kevent register index %d", testname, __func__,
960 kqueue_status(int kqueue_fd, int fd, int *readable, int *writable,
961 int *exception, const char *testname)
963 struct kevent kevent_eventlist[KQUEUE_MAX_EVENT], *kp;
964 struct timespec timeout;
970 ret = kevent(kqueue_fd, NULL, 0, kevent_eventlist, KQUEUE_MAX_EVENT,
973 warn("%s: %s: kevent", testname, __func__);
977 *readable = *writable = *exception = 0;
978 for (i = 0; i < ret; i++) {
979 kp = &kevent_eventlist[i];
980 if (kp->ident != (u_int)fd)
982 if (kp->filter == EVFILT_READ)
984 if (kp->filter == EVFILT_WRITE)
992 fionread_status(int fd, int *readable, const char *testname)
996 if (ioctl(fd, FIONREAD, &i) < 0) {
997 warn("%s: ioctl(FIONREAD)", testname);
1012 #define NOT_READABLE 0
1013 #define NOT_WRITABLE 0
1014 #define NOT_EXCEPTION 0
1017 assert_status(int fd, int kqueue_fd, int assert_readable,
1018 int assert_writable, int assert_exception, const char *testname,
1019 const char *conditionname, const char *fdname)
1021 int readable, writable, exception;
1023 if (poll_status(fd, &readable, &writable, &exception, testname) < 0)
1026 if (readable != assert_readable || writable != assert_writable ||
1027 exception != assert_exception) {
1028 warnx("%s: %s polls r:%d, w:%d, e:%d on %s", testname,
1029 fdname, readable, writable, exception, conditionname);
1033 if (select_status(fd, &readable, &writable, &exception, testname) < 0)
1036 if (readable != assert_readable || writable != assert_writable ||
1037 exception != assert_exception) {
1038 warnx("%s: %s selects r:%d, w:%d, e:%d on %s", testname,
1039 fdname, readable, writable, exception, conditionname);
1043 if (kqueue_status(kqueue_fd, fd, &readable, &writable, &exception,
1047 if (readable != assert_readable || writable != assert_writable ||
1048 exception != assert_exception) {
1049 warnx("%s: %s kevent r:%d, w:%d, e:%d on %s", testname,
1050 fdname, readable, writable, exception, conditionname);
1054 if (fionread_status(fd, &readable, __func__) < 0)
1057 if (readable != assert_readable) {
1058 warnx("%s: %s fionread r:%d on %s", testname, fdname,
1059 readable, conditionname);
1067 * test_events() uses poll(), select(), and kevent() to query the status of
1068 * fifo file descriptors and determine whether they match expected state
1069 * based on earlier semantic tests: specifically, whether or not poll/select/
1070 * kevent will correctly inform on readable/writable state following I/O.
1072 * It would be nice to also test status changes as a result of closing of one
1073 * or another fifo endpoint.
1076 test_events_outofbox(void)
1078 int kqueue_fd, reader_fd, writer_fd;
1080 makefifo("testfifo", __func__);
1081 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd) < 0) {
1082 warn("test_events_outofbox: openfifo: testfifo");
1083 cleanfifo2("testfifo", -1, -1);
1087 kqueue_fd = kqueue();
1088 if (kqueue_fd < 0) {
1089 warn("%s: kqueue", __func__);
1090 cleanfifo2("testfifo", reader_fd, writer_fd);
1094 if (kqueue_setup(kqueue_fd, reader_fd, __func__) < 0) {
1095 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1099 if (kqueue_setup(kqueue_fd, writer_fd, __func__) < 0) {
1100 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1105 * Make sure that fresh, out-of-the-box fifo file descriptors have
1106 * good initial states. The reader_fd should have no active state,
1107 * since it will not be readable (no data in pipe), writable (it's
1108 * a read-only descriptor), and there's no reason for error yet.
1110 if (assert_status(reader_fd, kqueue_fd, NOT_READABLE, NOT_WRITABLE,
1111 NOT_EXCEPTION, __func__, "create", "reader_fd") < 0) {
1112 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1117 * Make sure that fresh, out-of-the-box fifo file descriptors have
1118 * good initial states. The writer_fd should be ready to write.
1120 if (assert_status(writer_fd, kqueue_fd, NOT_READABLE, WRITABLE,
1121 NOT_EXCEPTION, __func__, "create", "writer_fd") < 0) {
1122 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1126 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1130 test_events_write_read_byte(void)
1132 int kqueue_fd, reader_fd, writer_fd;
1136 makefifo("testfifo", __func__);
1137 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
1139 warn("test_events_write_read_byte: openfifo: testfifo");
1140 cleanfifo2("testfifo", -1, -1);
1144 kqueue_fd = kqueue();
1145 if (kqueue_fd < 0) {
1146 warn("%s: kqueue", __func__);
1147 cleanfifo2("testfifo", reader_fd, writer_fd);
1151 if (kqueue_setup(kqueue_fd, reader_fd, __func__) < 0) {
1152 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1156 if (kqueue_setup(kqueue_fd, writer_fd, __func__) < 0) {
1157 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1162 * Write a byte to the fifo, and make sure that the read end becomes
1163 * readable, and that the write end remains writable (small write).
1166 len = write(writer_fd, &ch, sizeof(ch));
1168 warn("%s: write", __func__);
1169 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1173 if (assert_status(reader_fd, kqueue_fd, READABLE, NOT_WRITABLE,
1174 NOT_EXCEPTION, __func__, "write", "reader_fd") < 0) {
1175 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1180 * the writer_fd should remain writable.
1182 if (assert_status(writer_fd, kqueue_fd, NOT_READABLE, WRITABLE,
1183 NOT_EXCEPTION, __func__, "write", "writer_fd") < 0) {
1184 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1189 * Read the byte from the reader_fd, and now confirm that that fifo
1190 * becomes unreadable.
1192 len = read(reader_fd, &ch, sizeof(ch));
1194 warn("%s: read", __func__);
1195 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1199 if (assert_status(reader_fd, kqueue_fd, NOT_READABLE, NOT_WRITABLE,
1200 NOT_EXCEPTION, __func__, "write+read", "reader_fd") < 0) {
1201 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1206 * The writer_fd should remain writable.
1208 if (assert_status(writer_fd, kqueue_fd, NOT_READABLE, WRITABLE,
1209 NOT_EXCEPTION, __func__, "write+read", "writer_fd") < 0) {
1210 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1214 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1218 * Write a 512k buffer to the fifo in non-blocking mode, and make sure that
1219 * the write end becomes un-writable as a result of a partial write that
1220 * fills the fifo buffer.
1223 test_events_partial_write(void)
1225 int kqueue_fd, reader_fd, writer_fd;
1229 makefifo("testfifo", __func__);
1230 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
1232 warn("test_events_partial_write: openfifo: testfifo");
1233 cleanfifo2("testfifo", -1, -1);
1237 kqueue_fd = kqueue();
1238 if (kqueue_fd < 0) {
1239 warn("%s: kqueue", __func__);
1240 cleanfifo2("testfifo", reader_fd, writer_fd);
1244 if (kqueue_setup(kqueue_fd, reader_fd, __func__) < 0) {
1245 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1249 if (kqueue_setup(kqueue_fd, writer_fd, __func__) < 0) {
1250 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1254 if (set_nonblocking(writer_fd, "test_events") < 0) {
1255 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1259 buffer = malloc(512*1024);
1260 if (buffer == NULL) {
1261 warn("test_events_partial_write: malloc");
1262 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1265 bzero(buffer, 512*1024);
1267 len = write(writer_fd, buffer, 512*1024);
1269 warn("test_events_partial_write: write");
1271 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1277 if (assert_status(writer_fd, kqueue_fd, NOT_READABLE, NOT_WRITABLE,
1278 NOT_EXCEPTION, __func__, "big write", "writer_fd") < 0) {
1279 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1283 if (drain_fd(reader_fd, "test_events") < 0) {
1284 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1289 * Test that the writer_fd has been restored to writable state after
1292 if (assert_status(writer_fd, kqueue_fd, NOT_READABLE, WRITABLE,
1293 NOT_EXCEPTION, __func__, "big write + drain", "writer_fd") < 0) {
1294 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1298 cleanfifo3("testfifo", reader_fd, writer_fd, kqueue_fd);
1302 * We don't comprehensively test O_RDWR file descriptors, but do run a couple
1303 * of event tests to make sure that the fifo implementation doesn't mixed up
1304 * status checks. In particular, at least one past FreeBSD bug exists in
1305 * which the FIONREAD test was performed on the wrong socket implementing the
1306 * fifo, resulting in the fifo never returning readable.
1309 test_events_rdwr(void)
1315 makefifo("testfifo", __func__);
1316 if (openfifo_rw("testfifo", __func__, &fd)
1318 warn("%s: openfifo_rw: testfifo", __func__);
1319 cleanfifo2("testfifo", -1, -1);
1323 kqueue_fd = kqueue();
1324 if (kqueue_fd < 0) {
1325 warn("%s: kqueue", __func__);
1326 cleanfifo2("testifo", fd, -1);
1330 if (kqueue_setup(kqueue_fd, fd, __func__) < 0) {
1331 cleanfifo2("testfifo", fd, kqueue_fd);
1336 * On first creation, the O_RDWR descriptor should be writable but
1339 if (assert_status(fd, kqueue_fd, NOT_READABLE, WRITABLE,
1340 NOT_EXCEPTION, __func__, "create", "fd") < 0) {
1341 cleanfifo2("testfifo", fd, kqueue_fd);
1346 * Write a byte, which should cause the file descriptor to become
1347 * readable and writable.
1350 len = write(fd, &ch, sizeof(ch));
1352 warn("%s: write", __func__);
1353 cleanfifo2("testfifo", fd, kqueue_fd);
1357 if (assert_status(fd, kqueue_fd, READABLE, WRITABLE, NOT_EXCEPTION,
1358 __func__, "write", "fd") < 0) {
1359 cleanfifo2("testfifo", fd, kqueue_fd);
1364 * Read a byte, which should cause the file descriptor to return to
1365 * simply being writable.
1367 len = read(fd, &ch, sizeof(ch));
1369 warn("%s: read", __func__);
1370 cleanfifo2("testfifo", fd, kqueue_fd);
1374 if (assert_status(fd, kqueue_fd, NOT_READABLE, WRITABLE,
1375 NOT_EXCEPTION, __func__, "write+read", "fd") < 0) {
1376 cleanfifo2("testfifo", fd, kqueue_fd);
1380 cleanfifo2("testfifo", fd, kqueue_fd);
1384 main(int argc, char *argv[])
1387 strcpy(temp_dir, "/tmp/fifo_io.XXXXXXXXXXX");
1388 if (mkdtemp(temp_dir) == NULL)
1390 atexit(atexit_temp_dir);
1392 if (chdir(temp_dir) < 0)
1393 err(-1, "chdir %s", temp_dir);
1396 test_blocking_read_empty();
1397 test_blocking_one_byte();
1398 test_nonblocking_one_byte();
1399 test_blocking_partial_write();
1400 test_nonblocking_partial_write();
1401 test_coalesce_big_read();
1402 test_coalesce_big_write();
1403 test_events_outofbox();
1404 test_events_write_read_byte();
1405 test_events_partial_write();