xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Test for AIO POSIX compliance. |
| 2 | Copyright (C) 2001-2016 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <aio.h> |
| 20 | #include <error.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | |
| 28 | /* We might wait for 3 seconds, so increase timeout to 10 seconds. */ |
| 29 | #define TIMEOUT 10 |
| 30 | |
| 31 | |
| 32 | #define TEST_FUNCTION do_test () |
| 33 | static int |
| 34 | do_test (void) |
| 35 | { |
| 36 | int result = 0; |
| 37 | int piped[2]; |
| 38 | |
| 39 | /* Make a pipe that we will never write to, so we can block reading it. */ |
| 40 | if (pipe (piped) < 0) |
| 41 | { |
| 42 | perror ("pipe"); |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | /* Test for aio_cancel() detecting invalid file descriptor. */ |
| 47 | { |
| 48 | struct aiocb cb; |
| 49 | int fd = -1; |
| 50 | |
| 51 | cb.aio_fildes = fd; |
| 52 | cb.aio_offset = 0; |
| 53 | cb.aio_buf = NULL; |
| 54 | cb.aio_nbytes = 0; |
| 55 | cb.aio_reqprio = 0; |
| 56 | cb.aio_sigevent.sigev_notify = SIGEV_NONE; |
| 57 | |
| 58 | errno = 0; |
| 59 | |
| 60 | /* Case one: invalid fds that match. */ |
| 61 | if (aio_cancel (fd, &cb) != -1 || errno != EBADF) |
| 62 | { |
| 63 | if (errno == ENOSYS) |
| 64 | { |
| 65 | puts ("no aio support in this configuration"); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | puts ("aio_cancel( -1, {-1..} ) did not return -1 or errno != EBADF"); |
| 70 | ++result; |
| 71 | } |
| 72 | |
| 73 | cb.aio_fildes = -2; |
| 74 | errno = 0; |
| 75 | |
| 76 | /* Case two: invalid fds that do not match; just print warning. */ |
| 77 | if (aio_cancel (fd, &cb) != -1 || errno != EBADF) |
| 78 | puts ("aio_cancel( -1, {-2..} ) did not return -1 or errno != EBADF"); |
| 79 | } |
| 80 | |
| 81 | /* Test for aio_fsync() detecting bad fd. */ |
| 82 | { |
| 83 | struct aiocb cb; |
| 84 | int fd = -1; |
| 85 | |
| 86 | cb.aio_fildes = fd; |
| 87 | cb.aio_offset = 0; |
| 88 | cb.aio_buf = NULL; |
| 89 | cb.aio_nbytes = 0; |
| 90 | cb.aio_reqprio = 0; |
| 91 | cb.aio_sigevent.sigev_notify = SIGEV_NONE; |
| 92 | |
| 93 | errno = 0; |
| 94 | |
| 95 | /* Case one: invalid fd. */ |
| 96 | if (aio_fsync (O_SYNC, &cb) != -1 || errno != EBADF) |
| 97 | { |
| 98 | puts ("aio_fsync( op, {-1..} ) did not return -1 or errno != EBADF"); |
| 99 | ++result; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* Test for aio_suspend() suspending even if completed elements in list. */ |
| 104 | { |
| 105 | #define BYTES 8 |
| 106 | const int ELEMS = 2; |
| 107 | int i, r, fd; |
| 108 | static char buff[BYTES]; |
| 109 | char name[] = "/tmp/aio7.XXXXXX"; |
| 110 | struct timespec timeout; |
| 111 | static struct aiocb cb0, cb1; |
| 112 | struct aiocb *list[ELEMS]; |
| 113 | |
| 114 | fd = mkstemp (name); |
| 115 | if (fd < 0) |
| 116 | error (1, errno, "creating temp file"); |
| 117 | |
| 118 | if (unlink (name)) |
| 119 | error (1, errno, "unlinking temp file"); |
| 120 | |
| 121 | if (write (fd, "01234567", BYTES) != BYTES) |
| 122 | error (1, errno, "writing to temp file"); |
| 123 | |
| 124 | cb0.aio_fildes = fd; |
| 125 | cb0.aio_offset = 0; |
| 126 | cb0.aio_buf = buff; |
| 127 | cb0.aio_nbytes = BYTES; |
| 128 | cb0.aio_reqprio = 0; |
| 129 | cb0.aio_sigevent.sigev_notify = SIGEV_NONE; |
| 130 | |
| 131 | r = aio_read (&cb0); |
| 132 | if (r != 0) |
| 133 | error (1, errno, "reading from file"); |
| 134 | |
| 135 | while (aio_error (&(cb0)) == EINPROGRESS) |
| 136 | usleep (10); |
| 137 | |
| 138 | for (i = 0; i < BYTES; i++) |
| 139 | printf ("%c ", buff[i]); |
| 140 | printf ("\n"); |
| 141 | |
| 142 | /* At this point, the first read is completed, so start another one on |
| 143 | the read half of a pipe on which nothing will be written. */ |
| 144 | cb1.aio_fildes = piped[0]; |
| 145 | cb1.aio_offset = 0; |
| 146 | cb1.aio_buf = buff; |
| 147 | cb1.aio_nbytes = BYTES; |
| 148 | cb1.aio_reqprio = 0; |
| 149 | cb1.aio_sigevent.sigev_notify = SIGEV_NONE; |
| 150 | |
| 151 | r = aio_read (&cb1); |
| 152 | if (r != 0) |
| 153 | error (1, errno, "reading from file"); |
| 154 | |
| 155 | /* Now call aio_suspend() with the two reads. It should return |
| 156 | * immediately according to the POSIX spec. |
| 157 | */ |
| 158 | list[0] = &cb0; |
| 159 | list[1] = &cb1; |
| 160 | timeout.tv_sec = 3; |
| 161 | timeout.tv_nsec = 0; |
| 162 | r = aio_suspend ((const struct aiocb * const *) list, ELEMS, &timeout); |
| 163 | |
| 164 | if (r == -1 && errno == EAGAIN) |
| 165 | { |
| 166 | puts ("aio_suspend([done,blocked],2,3) suspended thread"); |
| 167 | ++result; |
| 168 | } |
| 169 | |
| 170 | /* Note that CB1 is still pending, and so cannot be an auto variable. |
| 171 | Thus we also test that exiting with an outstanding request works. */ |
| 172 | } |
| 173 | |
| 174 | return result; |
| 175 | } |
| 176 | |
| 177 | #include "../test-skeleton.c" |