2 * Copyright (c) 2004 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
30 * Regression test to do some very basic AIO exercising on several types of
31 * file descriptors. Currently, the tests consist of initializing a fixed
32 * size buffer with pseudo-random data, writing it to one fd using AIO, then
33 * reading it from a second descriptor using AIO. For some targets, the same
34 * fd is used for write and read (i.e., file, md device), but for others the
35 * operation is performed on a peer (pty, socket, fifo, etc). For each file
36 * descriptor type, several completion methods are tested. This test program
37 * does not attempt to exercise error cases or more subtle asynchronous
38 * behavior, just make sure that the basic operations work on some basic object
42 #include <sys/param.h>
43 #include <sys/module.h>
44 #include <sys/resource.h>
45 #include <sys/socket.h>
47 #include <sys/mdioctl.h>
55 #include <semaphore.h>
65 #include "freebsd_test_suite/macros.h"
69 * GLOBAL_MAX sets the largest usable buffer size to be read and written, as
70 * it sizes ac_buffer in the aio_context structure. It is also the default
71 * size for file I/O. For other types, we use smaller blocks or we risk
72 * blocking (and we run in a single process/thread so that would be bad).
74 #define GLOBAL_MAX 16384
76 #define BUFFER_MAX GLOBAL_MAX
79 * A completion function will block until the aio has completed, then return
80 * the result of the aio. errno will be set appropriately.
82 typedef ssize_t (*completion)(struct aiocb*);
85 int ac_read_fd, ac_write_fd;
87 char ac_buffer[GLOBAL_MAX];
92 static sem_t completions;
96 * Fill a buffer given a seed that can be fed into srandom() to initialize
97 * the PRNG in a repeatable manner.
100 aio_fill_buffer(char *buffer, int len, long seed)
106 for (i = 0; i < len; i++) {
107 ch = random() & 0xff;
113 * Test that a buffer matches a given seed. See aio_fill_buffer(). Return
114 * (1) on a match, (0) on a mismatch.
117 aio_test_buffer(char *buffer, int len, long seed)
123 for (i = 0; i < len; i++) {
124 ch = random() & 0xff;
132 * Initialize a testing context given the file descriptors provided by the
136 aio_context_init(struct aio_context *ac, int read_fd,
137 int write_fd, int buflen)
140 ATF_REQUIRE_MSG(buflen <= BUFFER_MAX,
141 "aio_context_init: buffer too large (%d > %d)",
143 bzero(ac, sizeof(*ac));
144 ac->ac_read_fd = read_fd;
145 ac->ac_write_fd = write_fd;
146 ac->ac_buflen = buflen;
148 ac->ac_seed = random();
149 aio_fill_buffer(ac->ac_buffer, buflen, ac->ac_seed);
150 ATF_REQUIRE_MSG(aio_test_buffer(ac->ac_buffer, buflen,
151 ac->ac_seed) != 0, "aio_test_buffer: internal error");
155 poll(struct aiocb *aio)
159 while ((error = aio_error(aio)) == EINPROGRESS)
166 return (aio_return(aio));
173 sigusr1_handler(int sig __unused)
175 ATF_REQUIRE_EQ(0, sem_post(&completions));
179 thr_handler(union sigval sv __unused)
181 ATF_REQUIRE_EQ(0, sem_post(&completions));
185 poll_signaled(struct aiocb *aio)
189 ATF_REQUIRE_EQ(0, sem_wait(&completions));
190 error = aio_error(aio);
196 return (aio_return(aio));
203 * Setup a signal handler for signal delivery tests
204 * This isn't thread safe, but it's ok since ATF runs each testcase in a
207 static struct sigevent*
210 static struct sigevent sev;
212 ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
213 sev.sigev_notify = SIGEV_SIGNAL;
214 sev.sigev_signo = SIGUSR1;
215 ATF_REQUIRE(SIG_ERR != signal(SIGUSR1, sigusr1_handler));
220 * Setup a thread for thread delivery tests
221 * This isn't thread safe, but it's ok since ATF runs each testcase in a
224 static struct sigevent*
227 static struct sigevent sev;
229 ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
230 sev.sigev_notify = SIGEV_THREAD;
231 sev.sigev_notify_function = thr_handler;
232 sev.sigev_notify_attributes = NULL;
237 suspend(struct aiocb *aio)
239 const struct aiocb *const iocbs[] = {aio};
242 error = aio_suspend(iocbs, 1, NULL);
244 return (aio_return(aio));
250 waitcomplete(struct aiocb *aio)
255 ret = aio_waitcomplete(&aiop, NULL);
256 ATF_REQUIRE_EQ(aio, aiop);
261 * Perform a simple write test of our initialized data buffer to the provided
265 aio_write_test(struct aio_context *ac, completion comp, struct sigevent *sev)
270 bzero(&aio, sizeof(aio));
271 aio.aio_buf = ac->ac_buffer;
272 aio.aio_nbytes = ac->ac_buflen;
273 aio.aio_fildes = ac->ac_write_fd;
276 aio.aio_sigevent = *sev;
278 if (aio_write(&aio) < 0)
279 atf_tc_fail("aio_write failed: %s", strerror(errno));
283 atf_tc_fail("aio failed: %s", strerror(errno));
285 if (len != ac->ac_buflen)
286 atf_tc_fail("aio short write (%jd)", (intmax_t)len);
290 * Perform a simple read test of our initialized data buffer from the
291 * provided file descriptor.
294 aio_read_test(struct aio_context *ac, completion comp, struct sigevent *sev)
299 bzero(ac->ac_buffer, ac->ac_buflen);
300 bzero(&aio, sizeof(aio));
301 aio.aio_buf = ac->ac_buffer;
302 aio.aio_nbytes = ac->ac_buflen;
303 aio.aio_fildes = ac->ac_read_fd;
306 aio.aio_sigevent = *sev;
308 if (aio_read(&aio) < 0)
309 atf_tc_fail("aio_read failed: %s", strerror(errno));
313 atf_tc_fail("aio failed: %s", strerror(errno));
315 ATF_REQUIRE_EQ_MSG(len, ac->ac_buflen,
316 "aio short read (%jd)", (intmax_t)len);
318 if (aio_test_buffer(ac->ac_buffer, ac->ac_buflen, ac->ac_seed) == 0)
319 atf_tc_fail("buffer mismatched");
323 * Series of type-specific tests for AIO. For now, we just make sure we can
324 * issue a write and then a read to each type. We assume that once a write
325 * is issued, a read can follow.
329 * Test with a classic file. Assumes we can create a moderate size temporary
332 #define FILE_LEN GLOBAL_MAX
333 #define FILE_PATHNAME "testfile"
336 aio_file_test(completion comp, struct sigevent *sev)
338 struct aio_context ac;
341 ATF_REQUIRE_KERNEL_MODULE("aio");
342 ATF_REQUIRE_UNSAFE_AIO();
344 fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
345 ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
347 aio_context_init(&ac, fd, fd, FILE_LEN);
348 aio_write_test(&ac, comp, sev);
349 aio_read_test(&ac, comp, sev);
353 ATF_TC_WITHOUT_HEAD(file_poll);
354 ATF_TC_BODY(file_poll, tc)
356 aio_file_test(poll, NULL);
359 ATF_TC_WITHOUT_HEAD(file_signal);
360 ATF_TC_BODY(file_signal, tc)
362 aio_file_test(poll_signaled, setup_signal());
365 ATF_TC_WITHOUT_HEAD(file_suspend);
366 ATF_TC_BODY(file_suspend, tc)
368 aio_file_test(suspend, NULL);
371 ATF_TC_WITHOUT_HEAD(file_thread);
372 ATF_TC_BODY(file_thread, tc)
374 aio_file_test(poll_signaled, setup_thread());
377 ATF_TC_WITHOUT_HEAD(file_waitcomplete);
378 ATF_TC_BODY(file_waitcomplete, tc)
380 aio_file_test(waitcomplete, NULL);
384 #define FIFO_PATHNAME "testfifo"
387 aio_fifo_test(completion comp, struct sigevent *sev)
389 int error, read_fd = -1, write_fd = -1;
390 struct aio_context ac;
392 ATF_REQUIRE_KERNEL_MODULE("aio");
393 ATF_REQUIRE_UNSAFE_AIO();
395 ATF_REQUIRE_MSG(mkfifo(FIFO_PATHNAME, 0600) != -1,
396 "mkfifo failed: %s", strerror(errno));
398 read_fd = open(FIFO_PATHNAME, O_RDONLY | O_NONBLOCK);
402 atf_tc_fail("read_fd open failed: %s",
406 write_fd = open(FIFO_PATHNAME, O_WRONLY);
407 if (write_fd == -1) {
410 atf_tc_fail("write_fd open failed: %s",
414 aio_context_init(&ac, read_fd, write_fd, FIFO_LEN);
415 aio_write_test(&ac, comp, sev);
416 aio_read_test(&ac, comp, sev);
422 ATF_TC_WITHOUT_HEAD(fifo_poll);
423 ATF_TC_BODY(fifo_poll, tc)
425 aio_fifo_test(poll, NULL);
428 ATF_TC_WITHOUT_HEAD(fifo_signal);
429 ATF_TC_BODY(fifo_signal, tc)
431 aio_fifo_test(poll_signaled, setup_signal());
434 ATF_TC_WITHOUT_HEAD(fifo_suspend);
435 ATF_TC_BODY(fifo_suspend, tc)
437 aio_fifo_test(suspend, NULL);
440 ATF_TC_WITHOUT_HEAD(fifo_thread);
441 ATF_TC_BODY(fifo_thread, tc)
443 aio_fifo_test(poll_signaled, setup_thread());
446 ATF_TC_WITHOUT_HEAD(fifo_waitcomplete);
447 ATF_TC_BODY(fifo_waitcomplete, tc)
449 aio_fifo_test(waitcomplete, NULL);
452 #define UNIX_SOCKETPAIR_LEN 256
454 aio_unix_socketpair_test(completion comp, struct sigevent *sev)
456 struct aio_context ac;
457 struct rusage ru_before, ru_after;
460 ATF_REQUIRE_KERNEL_MODULE("aio");
462 ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != -1,
463 "socketpair failed: %s", strerror(errno));
465 aio_context_init(&ac, sockets[0], sockets[1], UNIX_SOCKETPAIR_LEN);
466 ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_before) != -1,
467 "getrusage failed: %s", strerror(errno));
468 aio_write_test(&ac, comp, sev);
469 ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_after) != -1,
470 "getrusage failed: %s", strerror(errno));
471 ATF_REQUIRE(ru_after.ru_msgsnd == ru_before.ru_msgsnd + 1);
472 ru_before = ru_after;
473 aio_read_test(&ac, comp, sev);
474 ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_after) != -1,
475 "getrusage failed: %s", strerror(errno));
476 ATF_REQUIRE(ru_after.ru_msgrcv == ru_before.ru_msgrcv + 1);
482 ATF_TC_WITHOUT_HEAD(socket_poll);
483 ATF_TC_BODY(socket_poll, tc)
485 aio_unix_socketpair_test(poll, NULL);
488 ATF_TC_WITHOUT_HEAD(socket_signal);
489 ATF_TC_BODY(socket_signal, tc)
491 aio_unix_socketpair_test(poll_signaled, setup_signal());
494 ATF_TC_WITHOUT_HEAD(socket_suspend);
495 ATF_TC_BODY(socket_suspend, tc)
497 aio_unix_socketpair_test(suspend, NULL);
500 ATF_TC_WITHOUT_HEAD(socket_thread);
501 ATF_TC_BODY(socket_thread, tc)
503 aio_unix_socketpair_test(poll_signaled, setup_thread());
506 ATF_TC_WITHOUT_HEAD(socket_waitcomplete);
507 ATF_TC_BODY(socket_waitcomplete, tc)
509 aio_unix_socketpair_test(waitcomplete, NULL);
519 aio_pty_test(completion comp, struct sigevent *sev)
521 struct aio_context ac;
522 int read_fd, write_fd;
526 ATF_REQUIRE_KERNEL_MODULE("aio");
527 ATF_REQUIRE_UNSAFE_AIO();
529 ATF_REQUIRE_MSG(openpty(&read_fd, &write_fd, NULL, NULL, NULL) == 0,
530 "openpty failed: %s", strerror(errno));
533 if (tcgetattr(write_fd, &ts) < 0) {
536 atf_tc_fail("tcgetattr failed: %s", strerror(errno));
539 if (tcsetattr(write_fd, TCSANOW, &ts) < 0) {
542 atf_tc_fail("tcsetattr failed: %s", strerror(errno));
544 aio_context_init(&ac, read_fd, write_fd, PTY_LEN);
546 aio_write_test(&ac, comp, sev);
547 aio_read_test(&ac, comp, sev);
553 ATF_TC_WITHOUT_HEAD(pty_poll);
554 ATF_TC_BODY(pty_poll, tc)
556 aio_pty_test(poll, NULL);
559 ATF_TC_WITHOUT_HEAD(pty_signal);
560 ATF_TC_BODY(pty_signal, tc)
562 aio_pty_test(poll_signaled, setup_signal());
565 ATF_TC_WITHOUT_HEAD(pty_suspend);
566 ATF_TC_BODY(pty_suspend, tc)
568 aio_pty_test(suspend, NULL);
571 ATF_TC_WITHOUT_HEAD(pty_thread);
572 ATF_TC_BODY(pty_thread, tc)
574 aio_pty_test(poll_signaled, setup_thread());
577 ATF_TC_WITHOUT_HEAD(pty_waitcomplete);
578 ATF_TC_BODY(pty_waitcomplete, tc)
580 aio_pty_test(waitcomplete, NULL);
585 aio_pipe_test(completion comp, struct sigevent *sev)
587 struct aio_context ac;
590 ATF_REQUIRE_KERNEL_MODULE("aio");
591 ATF_REQUIRE_UNSAFE_AIO();
593 ATF_REQUIRE_MSG(pipe(pipes) != -1,
594 "pipe failed: %s", strerror(errno));
596 aio_context_init(&ac, pipes[0], pipes[1], PIPE_LEN);
597 aio_write_test(&ac, comp, sev);
598 aio_read_test(&ac, comp, sev);
604 ATF_TC_WITHOUT_HEAD(pipe_poll);
605 ATF_TC_BODY(pipe_poll, tc)
607 aio_pipe_test(poll, NULL);
610 ATF_TC_WITHOUT_HEAD(pipe_signal);
611 ATF_TC_BODY(pipe_signal, tc)
613 aio_pipe_test(poll_signaled, setup_signal());
616 ATF_TC_WITHOUT_HEAD(pipe_suspend);
617 ATF_TC_BODY(pipe_suspend, tc)
619 aio_pipe_test(suspend, NULL);
622 ATF_TC_WITHOUT_HEAD(pipe_thread);
623 ATF_TC_BODY(pipe_thread, tc)
625 aio_pipe_test(poll_signaled, setup_thread());
628 ATF_TC_WITHOUT_HEAD(pipe_waitcomplete);
629 ATF_TC_BODY(pipe_waitcomplete, tc)
631 aio_pipe_test(waitcomplete, NULL);
634 #define MD_LEN GLOBAL_MAX
635 #define MDUNIT_LINK "mdunit_link"
640 struct md_ioctl mdio;
641 int mdctl_fd, error, n, unit;
644 mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
645 ATF_REQUIRE(mdctl_fd >= 0);
646 n = readlink(MDUNIT_LINK, buf, sizeof(buf));
648 if (sscanf(buf, "%d", &unit) == 1 && unit >= 0) {
649 bzero(&mdio, sizeof(mdio));
650 mdio.md_version = MDIOVERSION;
652 if (ioctl(mdctl_fd, MDIOCDETACH, &mdio) == -1) {
656 atf_tc_fail("ioctl MDIOCDETACH failed: %s",
666 aio_md_test(completion comp, struct sigevent *sev)
668 int error, fd, mdctl_fd, unit;
669 char pathname[PATH_MAX];
670 struct aio_context ac;
671 struct md_ioctl mdio;
674 ATF_REQUIRE_KERNEL_MODULE("aio");
675 ATF_REQUIRE_UNSAFE_AIO();
677 mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
678 ATF_REQUIRE_MSG(mdctl_fd != -1,
679 "opening /dev/%s failed: %s", MDCTL_NAME, strerror(errno));
681 bzero(&mdio, sizeof(mdio));
682 mdio.md_version = MDIOVERSION;
683 mdio.md_type = MD_MALLOC;
684 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
685 mdio.md_mediasize = GLOBAL_MAX;
686 mdio.md_sectorsize = 512;
688 if (ioctl(mdctl_fd, MDIOCATTACH, &mdio) < 0) {
691 atf_tc_fail("ioctl MDIOCATTACH failed: %s", strerror(errno));
695 /* Store the md unit number in a symlink for future cleanup */
697 snprintf(buf, sizeof(buf), "%d", unit);
698 ATF_REQUIRE_EQ(0, symlink(buf, MDUNIT_LINK));
699 snprintf(pathname, PATH_MAX, "/dev/md%d", unit);
700 fd = open(pathname, O_RDWR);
701 ATF_REQUIRE_MSG(fd != -1,
702 "opening %s failed: %s", pathname, strerror(errno));
704 aio_context_init(&ac, fd, fd, MD_LEN);
705 aio_write_test(&ac, comp, sev);
706 aio_read_test(&ac, comp, sev);
711 ATF_TC_WITH_CLEANUP(md_poll);
712 ATF_TC_HEAD(md_poll, tc)
715 atf_tc_set_md_var(tc, "require.user", "root");
717 ATF_TC_BODY(md_poll, tc)
719 aio_md_test(poll, NULL);
721 ATF_TC_CLEANUP(md_poll, tc)
726 ATF_TC_WITH_CLEANUP(md_signal);
727 ATF_TC_HEAD(md_signal, tc)
730 atf_tc_set_md_var(tc, "require.user", "root");
732 ATF_TC_BODY(md_signal, tc)
734 aio_md_test(poll_signaled, setup_signal());
736 ATF_TC_CLEANUP(md_signal, tc)
741 ATF_TC_WITH_CLEANUP(md_suspend);
742 ATF_TC_HEAD(md_suspend, tc)
745 atf_tc_set_md_var(tc, "require.user", "root");
747 ATF_TC_BODY(md_suspend, tc)
749 aio_md_test(suspend, NULL);
751 ATF_TC_CLEANUP(md_suspend, tc)
756 ATF_TC_WITH_CLEANUP(md_thread);
757 ATF_TC_HEAD(md_thread, tc)
760 atf_tc_set_md_var(tc, "require.user", "root");
762 ATF_TC_BODY(md_thread, tc)
764 aio_md_test(poll_signaled, setup_thread());
766 ATF_TC_CLEANUP(md_thread, tc)
771 ATF_TC_WITH_CLEANUP(md_waitcomplete);
772 ATF_TC_HEAD(md_waitcomplete, tc)
775 atf_tc_set_md_var(tc, "require.user", "root");
777 ATF_TC_BODY(md_waitcomplete, tc)
779 aio_md_test(waitcomplete, NULL);
781 ATF_TC_CLEANUP(md_waitcomplete, tc)
786 ATF_TC_WITHOUT_HEAD(aio_large_read_test);
787 ATF_TC_BODY(aio_large_read_test, tc)
789 struct aiocb cb, *cbp;
797 ATF_REQUIRE_KERNEL_MODULE("aio");
798 ATF_REQUIRE_UNSAFE_AIO();
801 len = sizeof(clamped);
802 if (sysctlbyname("debug.iosize_max_clamp", &clamped, &len, NULL, 0) ==
804 atf_libc_error(errno, "Failed to read debug.iosize_max_clamp");
807 /* Determine the maximum supported read(2) size. */
814 fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
815 ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
817 unlink(FILE_PATHNAME);
819 memset(&cb, 0, sizeof(cb));
823 if (aio_read(&cb) == -1)
824 atf_tc_fail("aio_read() of maximum read size failed: %s",
827 nread = aio_waitcomplete(&cbp, NULL);
829 atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
831 atf_tc_fail("aio_read() from empty file returned data: %zd",
834 memset(&cb, 0, sizeof(cb));
835 cb.aio_nbytes = len + 1;
838 if (aio_read(&cb) == -1) {
841 atf_tc_fail("aio_read() of too large read size failed: %s",
845 nread = aio_waitcomplete(&cbp, NULL);
849 atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
851 atf_tc_fail("aio_read() of too large read size returned: %zd", nread);
858 * This tests for a bug where arriving socket data can wakeup multiple
859 * AIO read requests resulting in an uncancellable request.
861 ATF_TC_WITHOUT_HEAD(aio_socket_two_reads);
862 ATF_TC_BODY(aio_socket_two_reads, tc)
873 ATF_REQUIRE_KERNEL_MODULE("aio");
874 #if __FreeBSD_version < 1100101
875 aft_tc_skip("kernel version %d is too old (%d required)",
876 __FreeBSD_version, 1100101);
879 ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
881 /* Queue two read requests. */
882 memset(&ioreq, 0, sizeof(ioreq));
883 for (i = 0; i < nitems(ioreq); i++) {
884 ioreq[i].iocb.aio_nbytes = sizeof(ioreq[i].buffer);
885 ioreq[i].iocb.aio_fildes = s[0];
886 ioreq[i].iocb.aio_buf = ioreq[i].buffer;
887 ATF_REQUIRE(aio_read(&ioreq[i].iocb) == 0);
890 /* Send a single byte. This should complete one request. */
892 ATF_REQUIRE(write(s[1], &c, sizeof(c)) == 1);
894 ATF_REQUIRE(aio_waitcomplete(&iocb, NULL) == 1);
896 /* Determine which request completed and verify the data was read. */
897 if (iocb == &ioreq[0].iocb)
901 ATF_REQUIRE(ioreq[i].buffer[0] == c);
906 * Try to cancel the other request. On broken systems this
907 * will fail and the process will hang on exit.
909 ATF_REQUIRE(aio_error(&ioreq[i].iocb) == EINPROGRESS);
910 ATF_REQUIRE(aio_cancel(s[0], &ioreq[i].iocb) == AIO_CANCELED);
917 * This test ensures that aio_write() on a blocking socket of a "large"
918 * buffer does not return a short completion.
920 ATF_TC_WITHOUT_HEAD(aio_socket_blocking_short_write);
921 ATF_TC_BODY(aio_socket_blocking_short_write, tc)
923 struct aiocb iocb, *iocbp;
926 int buffer_size, sb_size;
930 ATF_REQUIRE_KERNEL_MODULE("aio");
932 ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
934 len = sizeof(sb_size);
935 ATF_REQUIRE(getsockopt(s[0], SOL_SOCKET, SO_RCVBUF, &sb_size, &len) !=
937 ATF_REQUIRE(len == sizeof(sb_size));
938 buffer_size = sb_size;
940 ATF_REQUIRE(getsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &sb_size, &len) !=
942 ATF_REQUIRE(len == sizeof(sb_size));
943 if (sb_size > buffer_size)
944 buffer_size = sb_size;
947 * Use twice the size of the MAX(receive buffer, send buffer)
948 * to ensure that the write is split up into multiple writes
953 buffer[0] = malloc(buffer_size);
954 ATF_REQUIRE(buffer[0] != NULL);
955 buffer[1] = malloc(buffer_size);
956 ATF_REQUIRE(buffer[1] != NULL);
959 aio_fill_buffer(buffer[1], buffer_size, random());
961 memset(&iocb, 0, sizeof(iocb));
962 iocb.aio_fildes = s[1];
963 iocb.aio_buf = buffer[1];
964 iocb.aio_nbytes = buffer_size;
965 ATF_REQUIRE(aio_write(&iocb) == 0);
967 done = recv(s[0], buffer[0], buffer_size, MSG_WAITALL);
968 ATF_REQUIRE(done == buffer_size);
970 done = aio_waitcomplete(&iocbp, NULL);
971 ATF_REQUIRE(iocbp == &iocb);
972 ATF_REQUIRE(done == buffer_size);
974 ATF_REQUIRE(memcmp(buffer[0], buffer[1], buffer_size) == 0);
981 * This test verifies that cancelling a partially completed socket write
982 * returns a short write rather than ECANCELED.
984 ATF_TC_WITHOUT_HEAD(aio_socket_short_write_cancel);
985 ATF_TC_BODY(aio_socket_short_write_cancel, tc)
987 struct aiocb iocb, *iocbp;
990 int buffer_size, sb_size;
994 ATF_REQUIRE_KERNEL_MODULE("aio");
996 ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
998 len = sizeof(sb_size);
999 ATF_REQUIRE(getsockopt(s[0], SOL_SOCKET, SO_RCVBUF, &sb_size, &len) !=
1001 ATF_REQUIRE(len == sizeof(sb_size));
1002 buffer_size = sb_size;
1004 ATF_REQUIRE(getsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &sb_size, &len) !=
1006 ATF_REQUIRE(len == sizeof(sb_size));
1007 if (sb_size > buffer_size)
1008 buffer_size = sb_size;
1011 * Use three times the size of the MAX(receive buffer, send
1012 * buffer) for the write to ensure that the write is split up
1013 * into multiple writes internally. The recv() ensures that
1014 * the write has partially completed, but a remaining size of
1015 * two buffers should ensure that the write has not completed
1016 * fully when it is cancelled.
1018 buffer[0] = malloc(buffer_size);
1019 ATF_REQUIRE(buffer[0] != NULL);
1020 buffer[1] = malloc(buffer_size * 3);
1021 ATF_REQUIRE(buffer[1] != NULL);
1024 aio_fill_buffer(buffer[1], buffer_size * 3, random());
1026 memset(&iocb, 0, sizeof(iocb));
1027 iocb.aio_fildes = s[1];
1028 iocb.aio_buf = buffer[1];
1029 iocb.aio_nbytes = buffer_size * 3;
1030 ATF_REQUIRE(aio_write(&iocb) == 0);
1032 done = recv(s[0], buffer[0], buffer_size, MSG_WAITALL);
1033 ATF_REQUIRE(done == buffer_size);
1035 ATF_REQUIRE(aio_error(&iocb) == EINPROGRESS);
1036 ATF_REQUIRE(aio_cancel(s[1], &iocb) == AIO_NOTCANCELED);
1038 done = aio_waitcomplete(&iocbp, NULL);
1039 ATF_REQUIRE(iocbp == &iocb);
1040 ATF_REQUIRE(done >= buffer_size && done <= buffer_size * 2);
1042 ATF_REQUIRE(memcmp(buffer[0], buffer[1], buffer_size) == 0);
1049 * This test just performs a basic test of aio_fsync().
1051 ATF_TC_WITHOUT_HEAD(aio_fsync_test);
1052 ATF_TC_BODY(aio_fsync_test, tc)
1054 struct aiocb synccb, *iocbp;
1065 ATF_REQUIRE_KERNEL_MODULE("aio");
1066 ATF_REQUIRE_UNSAFE_AIO();
1068 fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
1069 ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1070 unlink(FILE_PATHNAME);
1072 ATF_REQUIRE(fstat(fd, &sb) == 0);
1073 ATF_REQUIRE(sb.st_blksize != 0);
1074 ATF_REQUIRE(ftruncate(fd, sb.st_blksize * nitems(buffers)) == 0);
1077 * Queue several asynchronous write requests. Hopefully this
1078 * forces the aio_fsync() request to be deferred. There is no
1079 * reliable way to guarantee that however.
1082 for (i = 0; i < nitems(buffers); i++) {
1083 buffers[i].done = false;
1084 memset(&buffers[i].iocb, 0, sizeof(buffers[i].iocb));
1085 buffers[i].buffer = malloc(sb.st_blksize);
1086 aio_fill_buffer(buffers[i].buffer, sb.st_blksize, random());
1087 buffers[i].iocb.aio_fildes = fd;
1088 buffers[i].iocb.aio_buf = buffers[i].buffer;
1089 buffers[i].iocb.aio_nbytes = sb.st_blksize;
1090 buffers[i].iocb.aio_offset = sb.st_blksize * i;
1091 ATF_REQUIRE(aio_write(&buffers[i].iocb) == 0);
1094 /* Queue the aio_fsync request. */
1095 memset(&synccb, 0, sizeof(synccb));
1096 synccb.aio_fildes = fd;
1097 ATF_REQUIRE(aio_fsync(O_SYNC, &synccb) == 0);
1099 /* Wait for requests to complete. */
1102 rval = aio_waitcomplete(&iocbp, NULL);
1103 ATF_REQUIRE(iocbp != NULL);
1104 if (iocbp == &synccb) {
1105 ATF_REQUIRE(rval == 0);
1109 for (i = 0; i < nitems(buffers); i++) {
1110 if (iocbp == &buffers[i].iocb) {
1111 ATF_REQUIRE(buffers[i].done == false);
1112 ATF_REQUIRE(rval == sb.st_blksize);
1113 buffers[i].done = true;
1118 ATF_REQUIRE_MSG(false, "unmatched AIO request");
1121 for (i = 0; i < nitems(buffers); i++)
1122 ATF_REQUIRE_MSG(buffers[i].done,
1123 "AIO request %u did not complete", i);
1131 ATF_TP_ADD_TC(tp, file_poll);
1132 ATF_TP_ADD_TC(tp, file_signal);
1133 ATF_TP_ADD_TC(tp, file_suspend);
1134 ATF_TP_ADD_TC(tp, file_thread);
1135 ATF_TP_ADD_TC(tp, file_waitcomplete);
1136 ATF_TP_ADD_TC(tp, fifo_poll);
1137 ATF_TP_ADD_TC(tp, fifo_signal);
1138 ATF_TP_ADD_TC(tp, fifo_suspend);
1139 ATF_TP_ADD_TC(tp, fifo_thread);
1140 ATF_TP_ADD_TC(tp, fifo_waitcomplete);
1141 ATF_TP_ADD_TC(tp, socket_poll);
1142 ATF_TP_ADD_TC(tp, socket_signal);
1143 ATF_TP_ADD_TC(tp, socket_suspend);
1144 ATF_TP_ADD_TC(tp, socket_thread);
1145 ATF_TP_ADD_TC(tp, socket_waitcomplete);
1146 ATF_TP_ADD_TC(tp, pty_poll);
1147 ATF_TP_ADD_TC(tp, pty_signal);
1148 ATF_TP_ADD_TC(tp, pty_suspend);
1149 ATF_TP_ADD_TC(tp, pty_thread);
1150 ATF_TP_ADD_TC(tp, pty_waitcomplete);
1151 ATF_TP_ADD_TC(tp, pipe_poll);
1152 ATF_TP_ADD_TC(tp, pipe_signal);
1153 ATF_TP_ADD_TC(tp, pipe_suspend);
1154 ATF_TP_ADD_TC(tp, pipe_thread);
1155 ATF_TP_ADD_TC(tp, pipe_waitcomplete);
1156 ATF_TP_ADD_TC(tp, md_poll);
1157 ATF_TP_ADD_TC(tp, md_signal);
1158 ATF_TP_ADD_TC(tp, md_suspend);
1159 ATF_TP_ADD_TC(tp, md_thread);
1160 ATF_TP_ADD_TC(tp, md_waitcomplete);
1161 ATF_TP_ADD_TC(tp, aio_fsync_test);
1162 ATF_TP_ADD_TC(tp, aio_large_read_test);
1163 ATF_TP_ADD_TC(tp, aio_socket_two_reads);
1164 ATF_TP_ADD_TC(tp, aio_socket_blocking_short_write);
1165 ATF_TP_ADD_TC(tp, aio_socket_short_write_cancel);
1167 return (atf_no_error());