Deleted Added
full compact
fifo_io.c (281414) fifo_io.c (281450)
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 unchanged lines hidden (view full) ---

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 *
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 unchanged lines hidden (view full) ---

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: user/ngie/more-tests/tools/regression/fifo/fifo_io/fifo_io.c 228975 2011-12-30 00:04:11Z uqs $
26 * $FreeBSD: user/ngie/more-tests/tests/sys/fifo/fifo_io.c 281450 2015-04-12 06:18:24Z ngie $
27 */
28
29#include <sys/types.h>
30#include <sys/event.h>
31#include <sys/ioctl.h>
32#include <sys/select.h>
33#include <sys/stat.h>
34#include <sys/time.h>

--- 48 unchanged lines hidden (view full) ---

83 */
84
85#define KQUEUE_MAX_EVENT 8
86
87/*
88 * All activity occurs within a temporary directory created early in the
89 * test.
90 */
27 */
28
29#include <sys/types.h>
30#include <sys/event.h>
31#include <sys/ioctl.h>
32#include <sys/select.h>
33#include <sys/stat.h>
34#include <sys/time.h>

--- 48 unchanged lines hidden (view full) ---

83 */
84
85#define KQUEUE_MAX_EVENT 8
86
87/*
88 * All activity occurs within a temporary directory created early in the
89 * test.
90 */
91char temp_dir[PATH_MAX];
91static char temp_dir[PATH_MAX];
92
93static void __unused
94atexit_temp_dir(void)
95{
96
97 rmdir(temp_dir);
98}
99

--- 25 unchanged lines hidden (view full) ---

125 cleanfifo2(fifoname, fd1, fd2);
126}
127
128/*
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.
131 */
132static int
92
93static void __unused
94atexit_temp_dir(void)
95{
96
97 rmdir(temp_dir);
98}
99

--- 25 unchanged lines hidden (view full) ---

125 cleanfifo2(fifoname, fd1, fd2);
126}
127
128/*
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.
131 */
132static int
133openfifo(const char *fifoname, const char *testname, int *reader_fdp,
134 int *writer_fdp)
133openfifo(const char *fifoname, int *reader_fdp, int *writer_fdp)
135{
136 int error, fd1, fd2;
137
138 fd1 = open(fifoname, O_RDONLY | O_NONBLOCK);
139 if (fd1 < 0)
140 return (-1);
141 fd2 = open(fifoname, O_WRONLY | O_NONBLOCK);
142 if (fd2 < 0) {

--- 7 unchanged lines hidden (view full) ---

150
151 return (0);
152}
153
154/*
155 * Open one file descriptor for the fifo, supporting both read and write.
156 */
157static int
134{
135 int error, fd1, fd2;
136
137 fd1 = open(fifoname, O_RDONLY | O_NONBLOCK);
138 if (fd1 < 0)
139 return (-1);
140 fd2 = open(fifoname, O_WRONLY | O_NONBLOCK);
141 if (fd2 < 0) {

--- 7 unchanged lines hidden (view full) ---

149
150 return (0);
151}
152
153/*
154 * Open one file descriptor for the fifo, supporting both read and write.
155 */
156static int
158openfifo_rw(const char *fifoname, const char *testname, int *fdp)
157openfifo_rw(const char *fifoname, int *fdp)
159{
160 int fd;
161
162 fd = open(fifoname, O_RDWR);
163 if (fd < 0)
164 return (-1);
165 *fdp = fd;
166

--- 77 unchanged lines hidden (view full) ---

244static void
245test_simpleio(void)
246{
247 int i, reader_fd, writer_fd;
248 u_char buffer[10];
249 ssize_t len;
250
251 makefifo("testfifo", __func__);
158{
159 int fd;
160
161 fd = open(fifoname, O_RDWR);
162 if (fd < 0)
163 return (-1);
164 *fdp = fd;
165

--- 77 unchanged lines hidden (view full) ---

243static void
244test_simpleio(void)
245{
246 int i, reader_fd, writer_fd;
247 u_char buffer[10];
248 ssize_t len;
249
250 makefifo("testfifo", __func__);
252 if (openfifo("testfifo", "test_simpleio", &reader_fd, &writer_fd)
251 if (openfifo("testfifo", &reader_fd, &writer_fd)
253 < 0) {
254 warn("test_simpleio: openfifo: testfifo");
255 cleanfifo2("testfifo", -1, -1);
256 exit(-1);
257 }
258
259 for (i = 0; i < 10; i++)
260 buffer[i] = i;

--- 30 unchanged lines hidden (view full) ---

291 "0x%02x", i, i, buffer[i]);
292 cleanfifo2("testfifo", reader_fd, writer_fd);
293 exit(-1);
294 }
295
296 cleanfifo2("testfifo", reader_fd, writer_fd);
297}
298
252 < 0) {
253 warn("test_simpleio: openfifo: testfifo");
254 cleanfifo2("testfifo", -1, -1);
255 exit(-1);
256 }
257
258 for (i = 0; i < 10; i++)
259 buffer[i] = i;

--- 30 unchanged lines hidden (view full) ---

290 "0x%02x", i, i, buffer[i]);
291 cleanfifo2("testfifo", reader_fd, writer_fd);
292 exit(-1);
293 }
294
295 cleanfifo2("testfifo", reader_fd, writer_fd);
296}
297
299static int alarm_fired;
298static volatile int alarm_fired;
300/*
301 * Non-destructive SIGALRM handler.
302 */
303static void
299/*
300 * Non-destructive SIGALRM handler.
301 */
302static void
304sigalarm(int signum)
303sigalarm(int signum __unused)
305{
306
307 alarm_fired = 1;
308}
309
310/*
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

--- 90 unchanged lines hidden (view full) ---

403static void
404test_blocking_read_empty(void)
405{
406 int reader_fd, ret, timedout, writer_fd;
407 ssize_t len;
408 u_char ch;
409
410 makefifo("testfifo", __func__);
304{
305
306 alarm_fired = 1;
307}
308
309/*
310 * Wrapper function for write, which uses a timer to interrupt any blocking.
311 * Because we can't reliably detect EINTR for blocking I/O, we also track

--- 90 unchanged lines hidden (view full) ---

402static void
403test_blocking_read_empty(void)
404{
405 int reader_fd, ret, timedout, writer_fd;
406 ssize_t len;
407 u_char ch;
408
409 makefifo("testfifo", __func__);
411 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
410 if (openfifo("testfifo", &reader_fd, &writer_fd)
412 < 0) {
413 warn("test_blocking_read_empty: openfifo: testfifo");
414 cleanfifo2("testfifo", -1, -1);
415 exit(-1);
416 }
417
418 /*
419 * Read one byte from an empty blocking fifo, block as there is no

--- 52 unchanged lines hidden (view full) ---

472static void
473test_blocking_one_byte(void)
474{
475 int reader_fd, ret, timedout, writer_fd;
476 ssize_t len;
477 u_char ch;
478
479 makefifo("testfifo", __func__);
411 < 0) {
412 warn("test_blocking_read_empty: openfifo: testfifo");
413 cleanfifo2("testfifo", -1, -1);
414 exit(-1);
415 }
416
417 /*
418 * Read one byte from an empty blocking fifo, block as there is no

--- 52 unchanged lines hidden (view full) ---

471static void
472test_blocking_one_byte(void)
473{
474 int reader_fd, ret, timedout, writer_fd;
475 ssize_t len;
476 u_char ch;
477
478 makefifo("testfifo", __func__);
480 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
481 < 0) {
479 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
482 warn("test_blocking: openfifo: testfifo");
483 cleanfifo2("testfifo", -1, -1);
484 exit(-1);
485 }
486
487 if (set_blocking(writer_fd, __func__) < 0) {
488 cleanfifo2("testfifo", reader_fd, writer_fd);
489 exit(-1);

--- 49 unchanged lines hidden (view full) ---

539static void
540test_nonblocking_one_byte(void)
541{
542 int reader_fd, ret, timedout, writer_fd;
543 ssize_t len;
544 u_char ch;
545
546 makefifo("testfifo", __func__);
480 warn("test_blocking: openfifo: testfifo");
481 cleanfifo2("testfifo", -1, -1);
482 exit(-1);
483 }
484
485 if (set_blocking(writer_fd, __func__) < 0) {
486 cleanfifo2("testfifo", reader_fd, writer_fd);
487 exit(-1);

--- 49 unchanged lines hidden (view full) ---

537static void
538test_nonblocking_one_byte(void)
539{
540 int reader_fd, ret, timedout, writer_fd;
541 ssize_t len;
542 u_char ch;
543
544 makefifo("testfifo", __func__);
547 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
548 < 0) {
545 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
549 warn("test_nonblocking: openfifo: testfifo");
550 cleanfifo2("testfifo", -1, -1);
551 exit(-1);
552 }
553
554 if (set_nonblocking(reader_fd, __func__) < 0) {
555 cleanfifo2("testfifo", reader_fd, writer_fd);
556 exit(-1);

--- 47 unchanged lines hidden (view full) ---

604static void
605test_blocking_partial_write(void)
606{
607 int reader_fd, ret, timedout, writer_fd;
608 u_char *buffer;
609 ssize_t len;
610
611 makefifo("testfifo", __func__);
546 warn("test_nonblocking: openfifo: testfifo");
547 cleanfifo2("testfifo", -1, -1);
548 exit(-1);
549 }
550
551 if (set_nonblocking(reader_fd, __func__) < 0) {
552 cleanfifo2("testfifo", reader_fd, writer_fd);
553 exit(-1);

--- 47 unchanged lines hidden (view full) ---

601static void
602test_blocking_partial_write(void)
603{
604 int reader_fd, ret, timedout, writer_fd;
605 u_char *buffer;
606 ssize_t len;
607
608 makefifo("testfifo", __func__);
612 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
613 < 0) {
609 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
614 warn("test_blocking_partial_write: openfifo: testfifo");
615 cleanfifo2("testfifo", -1, -1);
616 exit(-1);
617 }
618
619 if (set_blocking(writer_fd, __func__) < 0) {
620 cleanfifo2("testfifo", reader_fd, writer_fd);
621 exit(-1);

--- 41 unchanged lines hidden (view full) ---

663static void
664test_nonblocking_partial_write(void)
665{
666 int reader_fd, ret, timedout, writer_fd;
667 u_char *buffer;
668 ssize_t len;
669
670 makefifo("testfifo", __func__);
610 warn("test_blocking_partial_write: openfifo: testfifo");
611 cleanfifo2("testfifo", -1, -1);
612 exit(-1);
613 }
614
615 if (set_blocking(writer_fd, __func__) < 0) {
616 cleanfifo2("testfifo", reader_fd, writer_fd);
617 exit(-1);

--- 41 unchanged lines hidden (view full) ---

659static void
660test_nonblocking_partial_write(void)
661{
662 int reader_fd, ret, timedout, writer_fd;
663 u_char *buffer;
664 ssize_t len;
665
666 makefifo("testfifo", __func__);
671 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
672 < 0) {
667 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
673 warn("test_blocking_partial_write: openfifo: testfifo");
674 cleanfifo2("testfifo", -1, -1);
675 exit(-1);
676 }
677
678 if (set_nonblocking(writer_fd, __func__) < 0) {
679 cleanfifo2("testfifo", reader_fd, writer_fd);
680 exit(-1);

--- 50 unchanged lines hidden (view full) ---

731static void
732test_coalesce_big_read(void)
733{
734 int i, reader_fd, writer_fd;
735 u_char buffer[10];
736 ssize_t len;
737
738 makefifo("testfifo", __func__);
668 warn("test_blocking_partial_write: openfifo: testfifo");
669 cleanfifo2("testfifo", -1, -1);
670 exit(-1);
671 }
672
673 if (set_nonblocking(writer_fd, __func__) < 0) {
674 cleanfifo2("testfifo", reader_fd, writer_fd);
675 exit(-1);

--- 50 unchanged lines hidden (view full) ---

726static void
727test_coalesce_big_read(void)
728{
729 int i, reader_fd, writer_fd;
730 u_char buffer[10];
731 ssize_t len;
732
733 makefifo("testfifo", __func__);
739 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
740 < 0) {
734 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
741 warn("test_coalesce_big_read: openfifo: testfifo");
742 cleanfifo2("testfifo", -1, -1);
743 exit(-1);
744 }
745
746 /* Write five, write five, read ten. */
747 for (i = 0; i < 10; i++)
748 buffer[i] = i;

--- 54 unchanged lines hidden (view full) ---

803static void
804test_coalesce_big_write(void)
805{
806 int i, reader_fd, writer_fd;
807 u_char buffer[10];
808 ssize_t len;
809
810 makefifo("testfifo", __func__);
735 warn("test_coalesce_big_read: openfifo: testfifo");
736 cleanfifo2("testfifo", -1, -1);
737 exit(-1);
738 }
739
740 /* Write five, write five, read ten. */
741 for (i = 0; i < 10; i++)
742 buffer[i] = i;

--- 54 unchanged lines hidden (view full) ---

797static void
798test_coalesce_big_write(void)
799{
800 int i, reader_fd, writer_fd;
801 u_char buffer[10];
802 ssize_t len;
803
804 makefifo("testfifo", __func__);
811 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
812 < 0) {
805 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
813 warn("test_coalesce_big_write: openfifo: testfifo");
814 cleanfifo2("testfifo", -1, -1);
815 exit(-1);
816 }
817
818 /* Write ten, read five, read five. */
819 for (i = 0; i < 10; i++)
820 buffer[i] = i;

--- 252 unchanged lines hidden (view full) ---

1073 * or another fifo endpoint.
1074 */
1075static void
1076test_events_outofbox(void)
1077{
1078 int kqueue_fd, reader_fd, writer_fd;
1079
1080 makefifo("testfifo", __func__);
806 warn("test_coalesce_big_write: openfifo: testfifo");
807 cleanfifo2("testfifo", -1, -1);
808 exit(-1);
809 }
810
811 /* Write ten, read five, read five. */
812 for (i = 0; i < 10; i++)
813 buffer[i] = i;

--- 252 unchanged lines hidden (view full) ---

1066 * or another fifo endpoint.
1067 */
1068static void
1069test_events_outofbox(void)
1070{
1071 int kqueue_fd, reader_fd, writer_fd;
1072
1073 makefifo("testfifo", __func__);
1081 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd) < 0) {
1074 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
1082 warn("test_events_outofbox: openfifo: testfifo");
1083 cleanfifo2("testfifo", -1, -1);
1084 exit(-1);
1085 }
1086
1087 kqueue_fd = kqueue();
1088 if (kqueue_fd < 0) {
1089 warn("%s: kqueue", __func__);

--- 39 unchanged lines hidden (view full) ---

1129static void
1130test_events_write_read_byte(void)
1131{
1132 int kqueue_fd, reader_fd, writer_fd;
1133 ssize_t len;
1134 u_char ch;
1135
1136 makefifo("testfifo", __func__);
1075 warn("test_events_outofbox: openfifo: testfifo");
1076 cleanfifo2("testfifo", -1, -1);
1077 exit(-1);
1078 }
1079
1080 kqueue_fd = kqueue();
1081 if (kqueue_fd < 0) {
1082 warn("%s: kqueue", __func__);

--- 39 unchanged lines hidden (view full) ---

1122static void
1123test_events_write_read_byte(void)
1124{
1125 int kqueue_fd, reader_fd, writer_fd;
1126 ssize_t len;
1127 u_char ch;
1128
1129 makefifo("testfifo", __func__);
1137 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
1138 < 0) {
1130 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
1139 warn("test_events_write_read_byte: openfifo: testfifo");
1140 cleanfifo2("testfifo", -1, -1);
1141 exit(-1);
1142 }
1143
1144 kqueue_fd = kqueue();
1145 if (kqueue_fd < 0) {
1146 warn("%s: kqueue", __func__);

--- 75 unchanged lines hidden (view full) ---

1222static void
1223test_events_partial_write(void)
1224{
1225 int kqueue_fd, reader_fd, writer_fd;
1226 u_char *buffer;
1227 ssize_t len;
1228
1229 makefifo("testfifo", __func__);
1131 warn("test_events_write_read_byte: openfifo: testfifo");
1132 cleanfifo2("testfifo", -1, -1);
1133 exit(-1);
1134 }
1135
1136 kqueue_fd = kqueue();
1137 if (kqueue_fd < 0) {
1138 warn("%s: kqueue", __func__);

--- 75 unchanged lines hidden (view full) ---

1214static void
1215test_events_partial_write(void)
1216{
1217 int kqueue_fd, reader_fd, writer_fd;
1218 u_char *buffer;
1219 ssize_t len;
1220
1221 makefifo("testfifo", __func__);
1230 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd)
1231 < 0) {
1222 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
1232 warn("test_events_partial_write: openfifo: testfifo");
1233 cleanfifo2("testfifo", -1, -1);
1234 exit(-1);
1235 }
1236
1237 kqueue_fd = kqueue();
1238 if (kqueue_fd < 0) {
1239 warn("%s: kqueue", __func__);

--- 68 unchanged lines hidden (view full) ---

1308static void
1309test_events_rdwr(void)
1310{
1311 int fd, kqueue_fd;
1312 ssize_t len;
1313 char ch;
1314
1315 makefifo("testfifo", __func__);
1223 warn("test_events_partial_write: openfifo: testfifo");
1224 cleanfifo2("testfifo", -1, -1);
1225 exit(-1);
1226 }
1227
1228 kqueue_fd = kqueue();
1229 if (kqueue_fd < 0) {
1230 warn("%s: kqueue", __func__);

--- 68 unchanged lines hidden (view full) ---

1299static void
1300test_events_rdwr(void)
1301{
1302 int fd, kqueue_fd;
1303 ssize_t len;
1304 char ch;
1305
1306 makefifo("testfifo", __func__);
1316 if (openfifo_rw("testfifo", __func__, &fd)
1317 < 0) {
1307 if (openfifo_rw("testfifo", &fd) < 0) {
1318 warn("%s: openfifo_rw: testfifo", __func__);
1319 cleanfifo2("testfifo", -1, -1);
1320 exit(-1);
1321 }
1322
1323 kqueue_fd = kqueue();
1324 if (kqueue_fd < 0) {
1325 warn("%s: kqueue", __func__);

--- 50 unchanged lines hidden (view full) ---

1376 cleanfifo2("testfifo", fd, kqueue_fd);
1377 exit(-1);
1378 }
1379
1380 cleanfifo2("testfifo", fd, kqueue_fd);
1381}
1382
1383int
1308 warn("%s: openfifo_rw: testfifo", __func__);
1309 cleanfifo2("testfifo", -1, -1);
1310 exit(-1);
1311 }
1312
1313 kqueue_fd = kqueue();
1314 if (kqueue_fd < 0) {
1315 warn("%s: kqueue", __func__);

--- 50 unchanged lines hidden (view full) ---

1366 cleanfifo2("testfifo", fd, kqueue_fd);
1367 exit(-1);
1368 }
1369
1370 cleanfifo2("testfifo", fd, kqueue_fd);
1371}
1372
1373int
1384main(int argc, char *argv[])
1374main(void)
1385{
1386
1375{
1376
1387 strcpy(temp_dir, "/tmp/fifo_io.XXXXXXXXXXX");
1377 strcpy(temp_dir, "fifo_io.XXXXXXXXXXX");
1388 if (mkdtemp(temp_dir) == NULL)
1389 err(-1, "mkdtemp");
1390 atexit(atexit_temp_dir);
1391
1392 if (chdir(temp_dir) < 0)
1393 err(-1, "chdir %s", temp_dir);
1394
1395 test_simpleio();

--- 14 unchanged lines hidden ---
1378 if (mkdtemp(temp_dir) == NULL)
1379 err(-1, "mkdtemp");
1380 atexit(atexit_temp_dir);
1381
1382 if (chdir(temp_dir) < 0)
1383 err(-1, "chdir %s", temp_dir);
1384
1385 test_simpleio();

--- 14 unchanged lines hidden ---