]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/aio/aio_test.c
Remove spurious newline
[FreeBSD/FreeBSD.git] / tests / sys / aio / aio_test.c
1 /*-
2  * Copyright (c) 2004 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 /*
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
39  * types.
40  */
41
42 #include <sys/param.h>
43 #include <sys/module.h>
44 #include <sys/resource.h>
45 #include <sys/socket.h>
46 #include <sys/stat.h>
47 #include <sys/mdioctl.h>
48
49 #include <aio.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <libutil.h>
54 #include <limits.h>
55 #include <semaphore.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <termios.h>
61 #include <unistd.h>
62
63 #include <atf-c.h>
64
65 #include "freebsd_test_suite/macros.h"
66 #include "local.h"
67
68 /*
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).
73  */
74 #define GLOBAL_MAX      16384
75
76 #define BUFFER_MAX      GLOBAL_MAX
77
78 /*
79  * A completion function will block until the aio has completed, then return
80  * the result of the aio.  errno will be set appropriately.
81  */
82 typedef ssize_t (*completion)(struct aiocb*);
83
84 struct aio_context {
85         int              ac_read_fd, ac_write_fd;
86         long             ac_seed;
87         char             ac_buffer[GLOBAL_MAX];
88         int              ac_buflen;
89         int              ac_seconds;
90 };
91
92 static sem_t            completions;
93
94
95 /*
96  * Fill a buffer given a seed that can be fed into srandom() to initialize
97  * the PRNG in a repeatable manner.
98  */
99 static void
100 aio_fill_buffer(char *buffer, int len, long seed)
101 {
102         char ch;
103         int i;
104
105         srandom(seed);
106         for (i = 0; i < len; i++) {
107                 ch = random() & 0xff;
108                 buffer[i] = ch;
109         }
110 }
111
112 /*
113  * Test that a buffer matches a given seed.  See aio_fill_buffer().  Return
114  * (1) on a match, (0) on a mismatch.
115  */
116 static int
117 aio_test_buffer(char *buffer, int len, long seed)
118 {
119         char ch;
120         int i;
121
122         srandom(seed);
123         for (i = 0; i < len; i++) {
124                 ch = random() & 0xff;
125                 if (buffer[i] != ch)
126                         return (0);
127         }
128         return (1);
129 }
130
131 /*
132  * Initialize a testing context given the file descriptors provided by the
133  * test setup.
134  */
135 static void
136 aio_context_init(struct aio_context *ac, int read_fd,
137     int write_fd, int buflen)
138 {
139
140         ATF_REQUIRE_MSG(buflen <= BUFFER_MAX,
141             "aio_context_init: buffer too large (%d > %d)",
142             buflen, BUFFER_MAX);
143         bzero(ac, sizeof(*ac));
144         ac->ac_read_fd = read_fd;
145         ac->ac_write_fd = write_fd;
146         ac->ac_buflen = buflen;
147         srandomdev();
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");
152 }
153
154 static ssize_t
155 poll(struct aiocb *aio)
156 {
157         int error;
158
159         while ((error = aio_error(aio)) == EINPROGRESS)
160                 usleep(25000);
161         if (error)
162                 return (error);
163         else
164                 return (aio_return(aio));
165 }
166
167 static void
168 sigusr1_handler(int sig __unused)
169 {
170         ATF_REQUIRE_EQ(0, sem_post(&completions));
171 }
172
173 static void
174 thr_handler(union sigval sv __unused)
175 {
176         ATF_REQUIRE_EQ(0, sem_post(&completions));
177 }
178
179 static ssize_t
180 poll_signaled(struct aiocb *aio)
181 {
182         int error;
183
184         ATF_REQUIRE_EQ(0, sem_wait(&completions));
185         error = aio_error(aio);
186         switch (error) {
187                 case EINPROGRESS:
188                         errno = EINTR;
189                         return (-1);
190                 case 0:
191                         return (aio_return(aio));
192                 default:
193                         return (error);
194         }
195 }
196
197 /*
198  * Setup a signal handler for signal delivery tests
199  * This isn't thread safe, but it's ok since ATF runs each testcase in a
200  * separate process
201  */
202 static struct sigevent*
203 setup_signal(void)
204 {
205         static struct sigevent sev;
206
207         ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
208         sev.sigev_notify = SIGEV_SIGNAL;
209         sev.sigev_signo = SIGUSR1;
210         ATF_REQUIRE(SIG_ERR != signal(SIGUSR1, sigusr1_handler));
211         return (&sev);
212 }
213
214 /*
215  * Setup a thread for thread delivery tests
216  * This isn't thread safe, but it's ok since ATF runs each testcase in a
217  * separate process
218  */
219 static struct sigevent*
220 setup_thread(void)
221 {
222         static struct sigevent sev;
223
224         ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
225         sev.sigev_notify = SIGEV_THREAD;
226         sev.sigev_notify_function = thr_handler;
227         sev.sigev_notify_attributes = NULL;
228         return (&sev);
229 }
230
231 static ssize_t
232 suspend(struct aiocb *aio)
233 {
234         const struct aiocb *const iocbs[] = {aio};
235         int error;
236
237         error = aio_suspend(iocbs, 1, NULL);
238         if (error == 0)
239                 return (aio_return(aio));
240         else
241                 return (error);
242 }
243
244 static ssize_t
245 waitcomplete(struct aiocb *aio)
246 {
247         struct aiocb *aiop;
248         ssize_t ret;
249
250         ret = aio_waitcomplete(&aiop, NULL);
251         ATF_REQUIRE_EQ(aio, aiop);
252         return (ret);
253 }
254
255 /*
256  * Perform a simple write test of our initialized data buffer to the provided
257  * file descriptor.
258  */
259 static void
260 aio_write_test(struct aio_context *ac, completion comp, struct sigevent *sev)
261 {
262         struct aiocb aio;
263         ssize_t len;
264
265         bzero(&aio, sizeof(aio));
266         aio.aio_buf = ac->ac_buffer;
267         aio.aio_nbytes = ac->ac_buflen;
268         aio.aio_fildes = ac->ac_write_fd;
269         aio.aio_offset = 0;
270         if (sev)
271                 aio.aio_sigevent = *sev;
272
273         if (aio_write(&aio) < 0)
274                 atf_tc_fail("aio_write failed: %s", strerror(errno));
275
276         len = comp(&aio);
277         if (len < 0)
278                 atf_tc_fail("aio failed: %s", strerror(errno));
279
280         if (len != ac->ac_buflen)
281                 atf_tc_fail("aio short write (%jd)", (intmax_t)len);
282 }
283
284 /*
285  * Perform a simple read test of our initialized data buffer from the
286  * provided file descriptor.
287  */
288 static void
289 aio_read_test(struct aio_context *ac, completion comp, struct sigevent *sev)
290 {
291         struct aiocb aio;
292         ssize_t len;
293
294         bzero(ac->ac_buffer, ac->ac_buflen);
295         bzero(&aio, sizeof(aio));
296         aio.aio_buf = ac->ac_buffer;
297         aio.aio_nbytes = ac->ac_buflen;
298         aio.aio_fildes = ac->ac_read_fd;
299         aio.aio_offset = 0;
300         if (sev)
301                 aio.aio_sigevent = *sev;
302
303         if (aio_read(&aio) < 0)
304                 atf_tc_fail("aio_read failed: %s", strerror(errno));
305
306         len = comp(&aio);
307         if (len < 0)
308                 atf_tc_fail("aio failed: %s", strerror(errno));
309
310         ATF_REQUIRE_EQ_MSG(len, ac->ac_buflen,
311             "aio short read (%jd)", (intmax_t)len);
312
313         if (aio_test_buffer(ac->ac_buffer, ac->ac_buflen, ac->ac_seed) == 0)
314                 atf_tc_fail("buffer mismatched");
315 }
316
317 /*
318  * Series of type-specific tests for AIO.  For now, we just make sure we can
319  * issue a write and then a read to each type.  We assume that once a write
320  * is issued, a read can follow.
321  */
322
323 /*
324  * Test with a classic file.  Assumes we can create a moderate size temporary
325  * file.
326  */
327 #define FILE_LEN        GLOBAL_MAX
328 #define FILE_PATHNAME   "testfile"
329
330 static void
331 aio_file_test(completion comp, struct sigevent *sev)
332 {
333         struct aio_context ac;
334         int fd;
335
336         ATF_REQUIRE_KERNEL_MODULE("aio");
337         ATF_REQUIRE_UNSAFE_AIO();
338
339         fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
340         ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
341
342         aio_context_init(&ac, fd, fd, FILE_LEN);
343         aio_write_test(&ac, comp, sev);
344         aio_read_test(&ac, comp, sev);
345         close(fd);
346 }
347
348 ATF_TC_WITHOUT_HEAD(file_poll);
349 ATF_TC_BODY(file_poll, tc)
350 {
351         aio_file_test(poll, NULL);
352 }
353
354 ATF_TC_WITHOUT_HEAD(file_signal);
355 ATF_TC_BODY(file_signal, tc)
356 {
357         aio_file_test(poll_signaled, setup_signal());
358 }
359
360 ATF_TC_WITHOUT_HEAD(file_suspend);
361 ATF_TC_BODY(file_suspend, tc)
362 {
363         aio_file_test(suspend, NULL);
364 }
365
366 ATF_TC_WITHOUT_HEAD(file_thread);
367 ATF_TC_BODY(file_thread, tc)
368 {
369         aio_file_test(poll_signaled, setup_thread());
370 }
371
372 ATF_TC_WITHOUT_HEAD(file_waitcomplete);
373 ATF_TC_BODY(file_waitcomplete, tc)
374 {
375         aio_file_test(waitcomplete, NULL);
376 }
377
378 #define FIFO_LEN        256
379 #define FIFO_PATHNAME   "testfifo"
380
381 static void
382 aio_fifo_test(completion comp, struct sigevent *sev)
383 {
384         int error, read_fd = -1, write_fd = -1;
385         struct aio_context ac;
386
387         ATF_REQUIRE_KERNEL_MODULE("aio");
388         ATF_REQUIRE_UNSAFE_AIO();
389
390         ATF_REQUIRE_MSG(mkfifo(FIFO_PATHNAME, 0600) != -1,
391             "mkfifo failed: %s", strerror(errno));
392
393         read_fd = open(FIFO_PATHNAME, O_RDONLY | O_NONBLOCK);
394         if (read_fd == -1) {
395                 error = errno;
396                 errno = error;
397                 atf_tc_fail("read_fd open failed: %s",
398                     strerror(errno));
399         }
400
401         write_fd = open(FIFO_PATHNAME, O_WRONLY);
402         if (write_fd == -1) {
403                 error = errno;
404                 errno = error;
405                 atf_tc_fail("write_fd open failed: %s",
406                     strerror(errno));
407         }
408
409         aio_context_init(&ac, read_fd, write_fd, FIFO_LEN);
410         aio_write_test(&ac, comp, sev);
411         aio_read_test(&ac, comp, sev);
412
413         close(read_fd);
414         close(write_fd);
415 }
416
417 ATF_TC_WITHOUT_HEAD(fifo_poll);
418 ATF_TC_BODY(fifo_poll, tc)
419 {
420         aio_fifo_test(poll, NULL);
421 }
422
423 ATF_TC_WITHOUT_HEAD(fifo_signal);
424 ATF_TC_BODY(fifo_signal, tc)
425 {
426         aio_fifo_test(poll_signaled, setup_signal());
427 }
428
429 ATF_TC_WITHOUT_HEAD(fifo_suspend);
430 ATF_TC_BODY(fifo_suspend, tc)
431 {
432         aio_fifo_test(suspend, NULL);
433 }
434
435 ATF_TC_WITHOUT_HEAD(fifo_thread);
436 ATF_TC_BODY(fifo_thread, tc)
437 {
438         aio_fifo_test(poll_signaled, setup_thread());
439 }
440
441 ATF_TC_WITHOUT_HEAD(fifo_waitcomplete);
442 ATF_TC_BODY(fifo_waitcomplete, tc)
443 {
444         aio_fifo_test(waitcomplete, NULL);
445 }
446
447 #define UNIX_SOCKETPAIR_LEN     256
448 static void
449 aio_unix_socketpair_test(completion comp, struct sigevent *sev)
450 {
451         struct aio_context ac;
452         struct rusage ru_before, ru_after;
453         int sockets[2];
454
455         ATF_REQUIRE_KERNEL_MODULE("aio");
456
457         ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != -1,
458             "socketpair failed: %s", strerror(errno));
459
460         aio_context_init(&ac, sockets[0], sockets[1], UNIX_SOCKETPAIR_LEN);
461         ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_before) != -1,
462             "getrusage failed: %s", strerror(errno));
463         aio_write_test(&ac, comp, sev);
464         ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_after) != -1,
465             "getrusage failed: %s", strerror(errno));
466         ATF_REQUIRE(ru_after.ru_msgsnd == ru_before.ru_msgsnd + 1);
467         ru_before = ru_after;
468         aio_read_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_msgrcv == ru_before.ru_msgrcv + 1);
472
473         close(sockets[0]);
474         close(sockets[1]);
475 }
476
477 ATF_TC_WITHOUT_HEAD(socket_poll);
478 ATF_TC_BODY(socket_poll, tc)
479 {
480         aio_unix_socketpair_test(poll, NULL);
481 }
482
483 ATF_TC_WITHOUT_HEAD(socket_signal);
484 ATF_TC_BODY(socket_signal, tc)
485 {
486         aio_unix_socketpair_test(poll_signaled, setup_signal());
487 }
488
489 ATF_TC_WITHOUT_HEAD(socket_suspend);
490 ATF_TC_BODY(socket_suspend, tc)
491 {
492         aio_unix_socketpair_test(suspend, NULL);
493 }
494
495 ATF_TC_WITHOUT_HEAD(socket_thread);
496 ATF_TC_BODY(socket_thread, tc)
497 {
498         aio_unix_socketpair_test(poll_signaled, setup_thread());
499 }
500
501 ATF_TC_WITHOUT_HEAD(socket_waitcomplete);
502 ATF_TC_BODY(socket_waitcomplete, tc)
503 {
504         aio_unix_socketpair_test(waitcomplete, NULL);
505 }
506
507 struct aio_pty_arg {
508         int     apa_read_fd;
509         int     apa_write_fd;
510 };
511
512 #define PTY_LEN         256
513 static void
514 aio_pty_test(completion comp, struct sigevent *sev)
515 {
516         struct aio_context ac;
517         int read_fd, write_fd;
518         struct termios ts;
519         int error;
520
521         ATF_REQUIRE_KERNEL_MODULE("aio");
522         ATF_REQUIRE_UNSAFE_AIO();
523
524         ATF_REQUIRE_MSG(openpty(&read_fd, &write_fd, NULL, NULL, NULL) == 0,
525             "openpty failed: %s", strerror(errno));
526
527
528         if (tcgetattr(write_fd, &ts) < 0) {
529                 error = errno;
530                 errno = error;
531                 atf_tc_fail("tcgetattr failed: %s", strerror(errno));
532         }
533         cfmakeraw(&ts);
534         if (tcsetattr(write_fd, TCSANOW, &ts) < 0) {
535                 error = errno;
536                 errno = error;
537                 atf_tc_fail("tcsetattr failed: %s", strerror(errno));
538         }
539         aio_context_init(&ac, read_fd, write_fd, PTY_LEN);
540
541         aio_write_test(&ac, comp, sev);
542         aio_read_test(&ac, comp, sev);
543
544         close(read_fd);
545         close(write_fd);
546 }
547
548 ATF_TC_WITHOUT_HEAD(pty_poll);
549 ATF_TC_BODY(pty_poll, tc)
550 {
551         aio_pty_test(poll, NULL);
552 }
553
554 ATF_TC_WITHOUT_HEAD(pty_signal);
555 ATF_TC_BODY(pty_signal, tc)
556 {
557         aio_pty_test(poll_signaled, setup_signal());
558 }
559
560 ATF_TC_WITHOUT_HEAD(pty_suspend);
561 ATF_TC_BODY(pty_suspend, tc)
562 {
563         aio_pty_test(suspend, NULL);
564 }
565
566 ATF_TC_WITHOUT_HEAD(pty_thread);
567 ATF_TC_BODY(pty_thread, tc)
568 {
569         aio_pty_test(poll_signaled, setup_thread());
570 }
571
572 ATF_TC_WITHOUT_HEAD(pty_waitcomplete);
573 ATF_TC_BODY(pty_waitcomplete, tc)
574 {
575         aio_pty_test(waitcomplete, NULL);
576 }
577
578 #define PIPE_LEN        256
579 static void
580 aio_pipe_test(completion comp, struct sigevent *sev)
581 {
582         struct aio_context ac;
583         int pipes[2];
584
585         ATF_REQUIRE_KERNEL_MODULE("aio");
586         ATF_REQUIRE_UNSAFE_AIO();
587
588         ATF_REQUIRE_MSG(pipe(pipes) != -1,
589             "pipe failed: %s", strerror(errno));
590
591         aio_context_init(&ac, pipes[0], pipes[1], PIPE_LEN);
592         aio_write_test(&ac, comp, sev);
593         aio_read_test(&ac, comp, sev);
594
595         close(pipes[0]);
596         close(pipes[1]);
597 }
598
599 ATF_TC_WITHOUT_HEAD(pipe_poll);
600 ATF_TC_BODY(pipe_poll, tc)
601 {
602         aio_pipe_test(poll, NULL);
603 }
604
605 ATF_TC_WITHOUT_HEAD(pipe_signal);
606 ATF_TC_BODY(pipe_signal, tc)
607 {
608         aio_pipe_test(poll_signaled, setup_signal());
609 }
610
611 ATF_TC_WITHOUT_HEAD(pipe_suspend);
612 ATF_TC_BODY(pipe_suspend, tc)
613 {
614         aio_pipe_test(suspend, NULL);
615 }
616
617 ATF_TC_WITHOUT_HEAD(pipe_thread);
618 ATF_TC_BODY(pipe_thread, tc)
619 {
620         aio_pipe_test(poll_signaled, setup_thread());
621 }
622
623 ATF_TC_WITHOUT_HEAD(pipe_waitcomplete);
624 ATF_TC_BODY(pipe_waitcomplete, tc)
625 {
626         aio_pipe_test(waitcomplete, NULL);
627 }
628
629 #define MD_LEN          GLOBAL_MAX
630 #define MDUNIT_LINK     "mdunit_link"
631
632 static void
633 aio_md_cleanup(void)
634 {
635         struct md_ioctl mdio;
636         int mdctl_fd, error, n, unit;
637         char buf[80];
638
639         mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
640         ATF_REQUIRE(mdctl_fd >= 0);
641         n = readlink(MDUNIT_LINK, buf, sizeof(buf));
642         if (n > 0) {
643                 if (sscanf(buf, "%d", &unit) == 1 && unit >= 0) {
644                         bzero(&mdio, sizeof(mdio));
645                         mdio.md_version = MDIOVERSION;
646                         mdio.md_unit = unit;
647                         if (ioctl(mdctl_fd, MDIOCDETACH, &mdio) == -1) {
648                                 error = errno;
649                                 close(mdctl_fd);
650                                 errno = error;
651                                 atf_tc_fail("ioctl MDIOCDETACH failed: %s",
652                                     strerror(errno));
653                         }
654                 }
655         }
656                 
657         close(mdctl_fd);
658 }
659
660 static void
661 aio_md_test(completion comp, struct sigevent *sev)
662 {
663         int error, fd, mdctl_fd, unit;
664         char pathname[PATH_MAX];
665         struct aio_context ac;
666         struct md_ioctl mdio;
667         char buf[80];
668
669         ATF_REQUIRE_KERNEL_MODULE("aio");
670
671         mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
672         ATF_REQUIRE_MSG(mdctl_fd != -1,
673             "opening /dev/%s failed: %s", MDCTL_NAME, strerror(errno));
674
675         bzero(&mdio, sizeof(mdio));
676         mdio.md_version = MDIOVERSION;
677         mdio.md_type = MD_MALLOC;
678         mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
679         mdio.md_mediasize = GLOBAL_MAX;
680         mdio.md_sectorsize = 512;
681
682         if (ioctl(mdctl_fd, MDIOCATTACH, &mdio) < 0) {
683                 error = errno;
684                 errno = error;
685                 atf_tc_fail("ioctl MDIOCATTACH failed: %s", strerror(errno));
686         }
687         close(mdctl_fd);
688
689         /* Store the md unit number in a symlink for future cleanup */
690         unit = mdio.md_unit;
691         snprintf(buf, sizeof(buf), "%d", unit);
692         ATF_REQUIRE_EQ(0, symlink(buf, MDUNIT_LINK));
693         snprintf(pathname, PATH_MAX, "/dev/md%d", unit);
694         fd = open(pathname, O_RDWR);
695         ATF_REQUIRE_MSG(fd != -1,
696             "opening %s failed: %s", pathname, strerror(errno));
697
698         aio_context_init(&ac, fd, fd, MD_LEN);
699         aio_write_test(&ac, comp, sev);
700         aio_read_test(&ac, comp, sev);
701         
702         close(fd);
703 }
704
705 ATF_TC_WITH_CLEANUP(md_poll);
706 ATF_TC_HEAD(md_poll, tc)
707 {
708
709         atf_tc_set_md_var(tc, "require.user", "root");
710 }
711 ATF_TC_BODY(md_poll, tc)
712 {
713         aio_md_test(poll, NULL);
714 }
715 ATF_TC_CLEANUP(md_poll, tc)
716 {
717         aio_md_cleanup();
718 }
719
720 ATF_TC_WITH_CLEANUP(md_signal);
721 ATF_TC_HEAD(md_signal, tc)
722 {
723
724         atf_tc_set_md_var(tc, "require.user", "root");
725 }
726 ATF_TC_BODY(md_signal, tc)
727 {
728         aio_md_test(poll_signaled, setup_signal());
729 }
730 ATF_TC_CLEANUP(md_signal, tc)
731 {
732         aio_md_cleanup();
733 }
734
735 ATF_TC_WITH_CLEANUP(md_suspend);
736 ATF_TC_HEAD(md_suspend, tc)
737 {
738
739         atf_tc_set_md_var(tc, "require.user", "root");
740 }
741 ATF_TC_BODY(md_suspend, tc)
742 {
743         aio_md_test(suspend, NULL);
744 }
745 ATF_TC_CLEANUP(md_suspend, tc)
746 {
747         aio_md_cleanup();
748 }
749
750 ATF_TC_WITH_CLEANUP(md_thread);
751 ATF_TC_HEAD(md_thread, tc)
752 {
753
754         atf_tc_set_md_var(tc, "require.user", "root");
755 }
756 ATF_TC_BODY(md_thread, tc)
757 {
758         aio_md_test(poll_signaled, setup_thread());
759 }
760 ATF_TC_CLEANUP(md_thread, tc)
761 {
762         aio_md_cleanup();
763 }
764
765 ATF_TC_WITH_CLEANUP(md_waitcomplete);
766 ATF_TC_HEAD(md_waitcomplete, tc)
767 {
768
769         atf_tc_set_md_var(tc, "require.user", "root");
770 }
771 ATF_TC_BODY(md_waitcomplete, tc)
772 {
773         aio_md_test(waitcomplete, NULL);
774 }
775 ATF_TC_CLEANUP(md_waitcomplete, tc)
776 {
777         aio_md_cleanup();
778 }
779
780 ATF_TC_WITHOUT_HEAD(aio_large_read_test);
781 ATF_TC_BODY(aio_large_read_test, tc)
782 {
783         struct aiocb cb, *cbp;
784         ssize_t nread;
785         size_t len;
786         int fd;
787 #ifdef __LP64__
788         int clamped;
789 #endif
790
791         ATF_REQUIRE_KERNEL_MODULE("aio");
792         ATF_REQUIRE_UNSAFE_AIO();
793
794 #ifdef __LP64__
795         len = sizeof(clamped);
796         if (sysctlbyname("debug.iosize_max_clamp", &clamped, &len, NULL, 0) ==
797             -1)
798                 atf_libc_error(errno, "Failed to read debug.iosize_max_clamp");
799 #endif
800
801         /* Determine the maximum supported read(2) size. */
802         len = SSIZE_MAX;
803 #ifdef __LP64__
804         if (clamped)
805                 len = INT_MAX;
806 #endif
807
808         fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
809         ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
810
811         unlink(FILE_PATHNAME);
812
813         memset(&cb, 0, sizeof(cb));
814         cb.aio_nbytes = len;
815         cb.aio_fildes = fd;
816         cb.aio_buf = NULL;
817         if (aio_read(&cb) == -1)
818                 atf_tc_fail("aio_read() of maximum read size failed: %s",
819                     strerror(errno));
820
821         nread = aio_waitcomplete(&cbp, NULL);
822         if (nread == -1)
823                 atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
824         if (nread != 0)
825                 atf_tc_fail("aio_read() from empty file returned data: %zd",
826                     nread);
827
828         memset(&cb, 0, sizeof(cb));
829         cb.aio_nbytes = len + 1;
830         cb.aio_fildes = fd;
831         cb.aio_buf = NULL;
832         if (aio_read(&cb) == -1) {
833                 if (errno == EINVAL)
834                         goto finished;
835                 atf_tc_fail("aio_read() of too large read size failed: %s",
836                     strerror(errno));
837         }
838
839         nread = aio_waitcomplete(&cbp, NULL);
840         if (nread == -1) {
841                 if (errno == EINVAL)
842                         goto finished;
843                 atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
844         }
845         atf_tc_fail("aio_read() of too large read size returned: %zd", nread);
846
847 finished:
848         close(fd);
849 }
850
851 /*
852  * This tests for a bug where arriving socket data can wakeup multiple
853  * AIO read requests resulting in an uncancellable request.
854  */
855 ATF_TC_WITHOUT_HEAD(aio_socket_two_reads);
856 ATF_TC_BODY(aio_socket_two_reads, tc)
857 {
858         struct ioreq {
859                 struct aiocb iocb;
860                 char buffer[1024];
861         } ioreq[2];
862         struct aiocb *iocb;
863         unsigned i;
864         int s[2];
865         char c;
866
867         ATF_REQUIRE_KERNEL_MODULE("aio");
868 #if __FreeBSD_version < 1100101
869         aft_tc_skip("kernel version %d is too old (%d required)",
870             __FreeBSD_version, 1100101);
871 #endif
872
873         ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
874
875         /* Queue two read requests. */
876         memset(&ioreq, 0, sizeof(ioreq));
877         for (i = 0; i < nitems(ioreq); i++) {
878                 ioreq[i].iocb.aio_nbytes = sizeof(ioreq[i].buffer);
879                 ioreq[i].iocb.aio_fildes = s[0];
880                 ioreq[i].iocb.aio_buf = ioreq[i].buffer;
881                 ATF_REQUIRE(aio_read(&ioreq[i].iocb) == 0);
882         }
883
884         /* Send a single byte.  This should complete one request. */
885         c = 0xc3;
886         ATF_REQUIRE(write(s[1], &c, sizeof(c)) == 1);
887
888         ATF_REQUIRE(aio_waitcomplete(&iocb, NULL) == 1);
889
890         /* Determine which request completed and verify the data was read. */
891         if (iocb == &ioreq[0].iocb)
892                 i = 0;
893         else
894                 i = 1;
895         ATF_REQUIRE(ioreq[i].buffer[0] == c);
896
897         i ^= 1;
898
899         /*
900          * Try to cancel the other request.  On broken systems this
901          * will fail and the process will hang on exit.
902          */
903         ATF_REQUIRE(aio_error(&ioreq[i].iocb) == EINPROGRESS);
904         ATF_REQUIRE(aio_cancel(s[0], &ioreq[i].iocb) == AIO_CANCELED);
905
906         close(s[1]);
907         close(s[0]);
908 }
909
910 /*
911  * This test ensures that aio_write() on a blocking socket of a "large"
912  * buffer does not return a short completion.
913  */
914 ATF_TC_WITHOUT_HEAD(aio_socket_blocking_short_write);
915 ATF_TC_BODY(aio_socket_blocking_short_write, tc)
916 {
917         struct aiocb iocb, *iocbp;
918         char *buffer[2];
919         ssize_t done;
920         int buffer_size, sb_size;
921         socklen_t len;
922         int s[2];
923
924         ATF_REQUIRE_KERNEL_MODULE("aio");
925
926         ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
927
928         len = sizeof(sb_size);
929         ATF_REQUIRE(getsockopt(s[0], SOL_SOCKET, SO_RCVBUF, &sb_size, &len) !=
930             -1);
931         ATF_REQUIRE(len == sizeof(sb_size));
932         buffer_size = sb_size;
933
934         ATF_REQUIRE(getsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &sb_size, &len) !=
935             -1);
936         ATF_REQUIRE(len == sizeof(sb_size));
937         if (sb_size > buffer_size)
938                 buffer_size = sb_size;
939
940         /*
941          * Use twice the size of the MAX(receive buffer, send buffer)
942          * to ensure that the write is split up into multiple writes
943          * internally.
944          */
945         buffer_size *= 2;
946
947         buffer[0] = malloc(buffer_size);
948         ATF_REQUIRE(buffer[0] != NULL);
949         buffer[1] = malloc(buffer_size);
950         ATF_REQUIRE(buffer[1] != NULL);
951
952         srandomdev();
953         aio_fill_buffer(buffer[1], buffer_size, random());
954
955         memset(&iocb, 0, sizeof(iocb));
956         iocb.aio_fildes = s[1];
957         iocb.aio_buf = buffer[1];
958         iocb.aio_nbytes = buffer_size;
959         ATF_REQUIRE(aio_write(&iocb) == 0);
960
961         done = recv(s[0], buffer[0], buffer_size, MSG_WAITALL);
962         ATF_REQUIRE(done == buffer_size);
963
964         done = aio_waitcomplete(&iocbp, NULL);
965         ATF_REQUIRE(iocbp == &iocb);
966         ATF_REQUIRE(done == buffer_size);
967
968         ATF_REQUIRE(memcmp(buffer[0], buffer[1], buffer_size) == 0);
969
970         close(s[1]);
971         close(s[0]);
972 }
973
974 /*
975  * This test verifies that cancelling a partially completed socket write
976  * returns a short write rather than ECANCELED.
977  */
978 ATF_TC_WITHOUT_HEAD(aio_socket_short_write_cancel);
979 ATF_TC_BODY(aio_socket_short_write_cancel, tc)
980 {
981         struct aiocb iocb, *iocbp;
982         char *buffer[2];
983         ssize_t done;
984         int buffer_size, sb_size;
985         socklen_t len;
986         int s[2];
987
988         ATF_REQUIRE_KERNEL_MODULE("aio");
989
990         ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
991
992         len = sizeof(sb_size);
993         ATF_REQUIRE(getsockopt(s[0], SOL_SOCKET, SO_RCVBUF, &sb_size, &len) !=
994             -1);
995         ATF_REQUIRE(len == sizeof(sb_size));
996         buffer_size = sb_size;
997
998         ATF_REQUIRE(getsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &sb_size, &len) !=
999             -1);
1000         ATF_REQUIRE(len == sizeof(sb_size));
1001         if (sb_size > buffer_size)
1002                 buffer_size = sb_size;
1003
1004         /*
1005          * Use three times the size of the MAX(receive buffer, send
1006          * buffer) for the write to ensure that the write is split up
1007          * into multiple writes internally.  The recv() ensures that
1008          * the write has partially completed, but a remaining size of
1009          * two buffers should ensure that the write has not completed
1010          * fully when it is cancelled.
1011          */
1012         buffer[0] = malloc(buffer_size);
1013         ATF_REQUIRE(buffer[0] != NULL);
1014         buffer[1] = malloc(buffer_size * 3);
1015         ATF_REQUIRE(buffer[1] != NULL);
1016
1017         srandomdev();
1018         aio_fill_buffer(buffer[1], buffer_size * 3, random());
1019
1020         memset(&iocb, 0, sizeof(iocb));
1021         iocb.aio_fildes = s[1];
1022         iocb.aio_buf = buffer[1];
1023         iocb.aio_nbytes = buffer_size * 3;
1024         ATF_REQUIRE(aio_write(&iocb) == 0);
1025
1026         done = recv(s[0], buffer[0], buffer_size, MSG_WAITALL);
1027         ATF_REQUIRE(done == buffer_size);
1028
1029         ATF_REQUIRE(aio_error(&iocb) == EINPROGRESS);
1030         ATF_REQUIRE(aio_cancel(s[1], &iocb) == AIO_NOTCANCELED);
1031
1032         done = aio_waitcomplete(&iocbp, NULL);
1033         ATF_REQUIRE(iocbp == &iocb);
1034         ATF_REQUIRE(done >= buffer_size && done <= buffer_size * 2);
1035
1036         ATF_REQUIRE(memcmp(buffer[0], buffer[1], buffer_size) == 0);
1037
1038         close(s[1]);
1039         close(s[0]);
1040 }
1041
1042 /* 
1043  * test aio_fsync's behavior with bad inputs 
1044  */
1045 ATF_TC_WITHOUT_HEAD(aio_fsync_errors);
1046 ATF_TC_BODY(aio_fsync_errors, tc)
1047 {
1048         int fd;
1049         struct aiocb iocb;
1050
1051         ATF_REQUIRE_KERNEL_MODULE("aio");
1052         ATF_REQUIRE_UNSAFE_AIO();
1053
1054         fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
1055         ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1056         unlink(FILE_PATHNAME);
1057
1058         /* aio_fsync should return EINVAL unless op is O_SYNC */
1059         memset(&iocb, 0, sizeof(iocb));
1060         iocb.aio_fildes = fd;
1061         ATF_CHECK_EQ(-1, aio_fsync(666, &iocb));
1062         ATF_CHECK_EQ(EINVAL, errno);
1063
1064         /* aio_fsync should return EBADF if fd is not a valid descriptor */
1065         memset(&iocb, 0, sizeof(iocb));
1066         iocb.aio_fildes = 666;
1067         ATF_CHECK_EQ(-1, aio_fsync(O_SYNC, &iocb));
1068         ATF_CHECK_EQ(EBADF, errno);
1069
1070         /* aio_fsync should return EINVAL if sigev_notify is invalid */
1071         memset(&iocb, 0, sizeof(iocb));
1072         iocb.aio_fildes = fd;
1073         iocb.aio_sigevent.sigev_notify = 666;
1074         ATF_CHECK_EQ(-1, aio_fsync(666, &iocb));
1075         ATF_CHECK_EQ(EINVAL, errno);
1076 }
1077
1078 /*
1079  * This test just performs a basic test of aio_fsync().
1080  */
1081 ATF_TC_WITHOUT_HEAD(aio_fsync_test);
1082 ATF_TC_BODY(aio_fsync_test, tc)
1083 {
1084         struct aiocb synccb, *iocbp;
1085         struct {
1086                 struct aiocb iocb;
1087                 bool done;
1088                 char *buffer;
1089         } buffers[16];
1090         struct stat sb;
1091         ssize_t rval;
1092         unsigned i;
1093         int fd;
1094
1095         ATF_REQUIRE_KERNEL_MODULE("aio");
1096         ATF_REQUIRE_UNSAFE_AIO();
1097
1098         fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
1099         ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1100         unlink(FILE_PATHNAME);
1101
1102         ATF_REQUIRE(fstat(fd, &sb) == 0);
1103         ATF_REQUIRE(sb.st_blksize != 0);
1104         ATF_REQUIRE(ftruncate(fd, sb.st_blksize * nitems(buffers)) == 0);
1105
1106         /*
1107          * Queue several asynchronous write requests.  Hopefully this
1108          * forces the aio_fsync() request to be deferred.  There is no
1109          * reliable way to guarantee that however.
1110          */
1111         srandomdev();
1112         for (i = 0; i < nitems(buffers); i++) {
1113                 buffers[i].done = false;
1114                 memset(&buffers[i].iocb, 0, sizeof(buffers[i].iocb));
1115                 buffers[i].buffer = malloc(sb.st_blksize);
1116                 aio_fill_buffer(buffers[i].buffer, sb.st_blksize, random());
1117                 buffers[i].iocb.aio_fildes = fd;
1118                 buffers[i].iocb.aio_buf = buffers[i].buffer;
1119                 buffers[i].iocb.aio_nbytes = sb.st_blksize;
1120                 buffers[i].iocb.aio_offset = sb.st_blksize * i;
1121                 ATF_REQUIRE(aio_write(&buffers[i].iocb) == 0);
1122         }
1123
1124         /* Queue the aio_fsync request. */
1125         memset(&synccb, 0, sizeof(synccb));
1126         synccb.aio_fildes = fd;
1127         ATF_REQUIRE(aio_fsync(O_SYNC, &synccb) == 0);
1128
1129         /* Wait for requests to complete. */
1130         for (;;) {
1131         next:
1132                 rval = aio_waitcomplete(&iocbp, NULL);
1133                 ATF_REQUIRE(iocbp != NULL);
1134                 if (iocbp == &synccb) {
1135                         ATF_REQUIRE(rval == 0);
1136                         break;
1137                 }
1138
1139                 for (i = 0; i < nitems(buffers); i++) {
1140                         if (iocbp == &buffers[i].iocb) {
1141                                 ATF_REQUIRE(buffers[i].done == false);
1142                                 ATF_REQUIRE(rval == sb.st_blksize);
1143                                 buffers[i].done = true;
1144                                 goto next;
1145                         }
1146                 }
1147
1148                 ATF_REQUIRE_MSG(false, "unmatched AIO request");
1149         }
1150
1151         for (i = 0; i < nitems(buffers); i++)
1152                 ATF_REQUIRE_MSG(buffers[i].done,
1153                     "AIO request %u did not complete", i);
1154
1155         close(fd);
1156 }
1157
1158 ATF_TP_ADD_TCS(tp)
1159 {
1160
1161         ATF_TP_ADD_TC(tp, file_poll);
1162         ATF_TP_ADD_TC(tp, file_signal);
1163         ATF_TP_ADD_TC(tp, file_suspend);
1164         ATF_TP_ADD_TC(tp, file_thread);
1165         ATF_TP_ADD_TC(tp, file_waitcomplete);
1166         ATF_TP_ADD_TC(tp, fifo_poll);
1167         ATF_TP_ADD_TC(tp, fifo_signal);
1168         ATF_TP_ADD_TC(tp, fifo_suspend);
1169         ATF_TP_ADD_TC(tp, fifo_thread);
1170         ATF_TP_ADD_TC(tp, fifo_waitcomplete);
1171         ATF_TP_ADD_TC(tp, socket_poll);
1172         ATF_TP_ADD_TC(tp, socket_signal);
1173         ATF_TP_ADD_TC(tp, socket_suspend);
1174         ATF_TP_ADD_TC(tp, socket_thread);
1175         ATF_TP_ADD_TC(tp, socket_waitcomplete);
1176         ATF_TP_ADD_TC(tp, pty_poll);
1177         ATF_TP_ADD_TC(tp, pty_signal);
1178         ATF_TP_ADD_TC(tp, pty_suspend);
1179         ATF_TP_ADD_TC(tp, pty_thread);
1180         ATF_TP_ADD_TC(tp, pty_waitcomplete);
1181         ATF_TP_ADD_TC(tp, pipe_poll);
1182         ATF_TP_ADD_TC(tp, pipe_signal);
1183         ATF_TP_ADD_TC(tp, pipe_suspend);
1184         ATF_TP_ADD_TC(tp, pipe_thread);
1185         ATF_TP_ADD_TC(tp, pipe_waitcomplete);
1186         ATF_TP_ADD_TC(tp, md_poll);
1187         ATF_TP_ADD_TC(tp, md_signal);
1188         ATF_TP_ADD_TC(tp, md_suspend);
1189         ATF_TP_ADD_TC(tp, md_thread);
1190         ATF_TP_ADD_TC(tp, md_waitcomplete);
1191         ATF_TP_ADD_TC(tp, aio_fsync_errors);
1192         ATF_TP_ADD_TC(tp, aio_fsync_test);
1193         ATF_TP_ADD_TC(tp, aio_large_read_test);
1194         ATF_TP_ADD_TC(tp, aio_socket_two_reads);
1195         ATF_TP_ADD_TC(tp, aio_socket_blocking_short_write);
1196         ATF_TP_ADD_TC(tp, aio_socket_short_write_cancel);
1197
1198         return (atf_no_error());
1199 }