| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Tests for 64bit AIO in librt. | 
 | 2 |    Copyright (C) 1998-2016 Free Software Foundation, Inc. | 
 | 3 |    This file is part of the GNU C Library. | 
 | 4 |    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. | 
 | 5 |  | 
 | 6 |    The GNU C Library is free software; you can redistribute it and/or | 
 | 7 |    modify it under the terms of the GNU Lesser General Public | 
 | 8 |    License as published by the Free Software Foundation; either | 
 | 9 |    version 2.1 of the License, or (at your option) any later version. | 
 | 10 |  | 
 | 11 |    The GNU C Library is distributed in the hope that it will be useful, | 
 | 12 |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 13 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
 | 14 |    Lesser General Public License for more details. | 
 | 15 |  | 
 | 16 |    You should have received a copy of the GNU Lesser General Public | 
 | 17 |    License along with the GNU C Library; if not, see | 
 | 18 |    <http://www.gnu.org/licenses/>.  */ | 
 | 19 |  | 
 | 20 | #define _LARGEFILE_SOURCE 1 | 
 | 21 | #include <aio.h> | 
 | 22 | #include <errno.h> | 
 | 23 | #include <error.h> | 
 | 24 | #include <fcntl.h> | 
 | 25 | #include <stdlib.h> | 
 | 26 | #include <string.h> | 
 | 27 | #include <unistd.h> | 
 | 28 | #include <sys/stat.h> | 
 | 29 |  | 
 | 30 |  | 
 | 31 | /* Prototype for our test function.  */ | 
 | 32 | extern void do_prepare (int argc, char *argv[]); | 
 | 33 | extern int do_test (int argc, char *argv[]); | 
 | 34 |  | 
 | 35 | /* We have a preparation function.  */ | 
 | 36 | #define PREPARE do_prepare | 
 | 37 |  | 
 | 38 | /* We might need a bit longer timeout.  */ | 
 | 39 | #define TIMEOUT 20 /* sec */ | 
 | 40 |  | 
 | 41 | /* This defines the `main' function and some more.  */ | 
 | 42 | #include <test-skeleton.c> | 
 | 43 |  | 
 | 44 |  | 
 | 45 | /* These are for the temporary file we generate.  */ | 
 | 46 | char *name; | 
 | 47 | int fd; | 
 | 48 |  | 
 | 49 | void | 
 | 50 | do_prepare (int argc, char *argv[]) | 
 | 51 | { | 
 | 52 |   size_t name_len; | 
 | 53 |  | 
 | 54 |   name_len = strlen (test_dir); | 
 | 55 |   name = malloc (name_len + sizeof ("/aioXXXXXX")); | 
 | 56 |   mempcpy (mempcpy (name, test_dir, name_len), | 
 | 57 | 	   "/aioXXXXXX", sizeof ("/aioXXXXXX")); | 
 | 58 |  | 
 | 59 |   /* Open our test file.   */ | 
 | 60 |   fd = mkstemp (name); | 
 | 61 |   if (fd == -1) | 
 | 62 |     error (EXIT_FAILURE, errno, "cannot open test file `%s'", name); | 
 | 63 |   add_temp_file (name); | 
 | 64 | } | 
 | 65 |  | 
 | 66 |  | 
 | 67 | static int | 
 | 68 | test_file (const void *buf, size_t size, int fd, const char *msg) | 
 | 69 | { | 
 | 70 |   struct stat st; | 
 | 71 |   char tmp[size]; | 
 | 72 |  | 
 | 73 |   errno = 0; | 
 | 74 |   if (fstat (fd, &st) < 0) | 
 | 75 |     { | 
 | 76 |       error (0, errno, "%s: failed stat", msg); | 
 | 77 |       return 1; | 
 | 78 |     } | 
 | 79 |  | 
 | 80 |   if (st.st_size != (off_t) size) | 
 | 81 |     { | 
 | 82 |       error (0, errno, "%s: wrong size: %lu, should be %lu", | 
 | 83 | 	     msg, (unsigned long int) st.st_size, (unsigned long int) size); | 
 | 84 |       return 1; | 
 | 85 |     } | 
 | 86 |  | 
 | 87 |   if (pread (fd, tmp, size, 0) != (ssize_t) size) | 
 | 88 |     { | 
 | 89 |       error (0, errno, "%s: failed pread", msg); | 
 | 90 |       return 1; | 
 | 91 |     } | 
 | 92 |  | 
 | 93 |   if (memcmp (buf, tmp, size) != 0) | 
 | 94 |     { | 
 | 95 |       error (0, errno, "%s: failed comparison", msg); | 
 | 96 |       return 1; | 
 | 97 |     } | 
 | 98 |  | 
 | 99 |   printf ("%s test ok\n", msg); | 
 | 100 |  | 
 | 101 |   return 0; | 
 | 102 | } | 
 | 103 |  | 
 | 104 |  | 
 | 105 | static int | 
 | 106 | do_wait (struct aiocb64 **cbp, size_t nent, int allowed_err) | 
 | 107 | { | 
 | 108 |   int go_on; | 
 | 109 |   size_t cnt; | 
 | 110 |   int result = 0; | 
 | 111 |  | 
 | 112 |   do | 
 | 113 |     { | 
 | 114 |       aio_suspend64 ((const struct aiocb64 *const *) cbp, nent, NULL); | 
 | 115 |       go_on = 0; | 
 | 116 |       for (cnt = 0; cnt < nent; ++cnt) | 
 | 117 | 	if (cbp[cnt] != NULL) | 
 | 118 | 	  { | 
 | 119 | 	    if (aio_error64 (cbp[cnt]) == EINPROGRESS) | 
 | 120 | 	      go_on = 1; | 
 | 121 | 	    else | 
 | 122 | 	      { | 
 | 123 | 		if (aio_return64 (cbp[cnt]) == -1 | 
 | 124 | 		    && (allowed_err == 0 | 
 | 125 | 			|| aio_error64 (cbp[cnt]) != allowed_err)) | 
 | 126 | 		  { | 
 | 127 | 		    error (0, aio_error64 (cbp[cnt]), "Operation failed\n"); | 
 | 128 | 		    result = 1; | 
 | 129 | 		  } | 
 | 130 | 		cbp[cnt] = NULL; | 
 | 131 | 	      } | 
 | 132 | 	  } | 
 | 133 |     } | 
 | 134 |   while (go_on); | 
 | 135 |  | 
 | 136 |   return result; | 
 | 137 | } | 
 | 138 |  | 
 | 139 |  | 
 | 140 | int | 
 | 141 | do_test (int argc, char *argv[]) | 
 | 142 | { | 
 | 143 |   struct aiocb64 cbs[10]; | 
 | 144 |   struct aiocb64 cbs_fsync; | 
 | 145 |   struct aiocb64 *cbp[10]; | 
 | 146 |   struct aiocb64 *cbp_fsync[1]; | 
 | 147 |   char buf[1000]; | 
 | 148 |   size_t cnt; | 
 | 149 |   int result = 0; | 
 | 150 |  | 
 | 151 |   /* Preparation.  */ | 
 | 152 |   for (cnt = 0; cnt < 10; ++cnt) | 
 | 153 |     { | 
 | 154 |       cbs[cnt].aio_fildes = fd; | 
 | 155 |       cbs[cnt].aio_reqprio = 0; | 
 | 156 |       cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100); | 
 | 157 |       cbs[cnt].aio_nbytes = 100; | 
 | 158 |       cbs[cnt].aio_offset = cnt * 100; | 
 | 159 |       cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE; | 
 | 160 |  | 
 | 161 |       cbp[cnt] = &cbs[cnt]; | 
 | 162 |     } | 
 | 163 |  | 
 | 164 |   /* First a simple test.  */ | 
 | 165 |   for (cnt = 10; cnt > 0; ) | 
 | 166 |     if (aio_write64 (cbp[--cnt]) < 0 && errno == ENOSYS) | 
 | 167 |       { | 
 | 168 | 	error (0, 0, "no aio support in this configuration"); | 
 | 169 | 	return 0; | 
 | 170 |       } | 
 | 171 |   /* Wait 'til the results are there.  */ | 
 | 172 |   result |= do_wait (cbp, 10, 0); | 
 | 173 |   /* Test this.  */ | 
 | 174 |   result |= test_file (buf, sizeof (buf), fd, "aio_write"); | 
 | 175 |  | 
 | 176 |   /* Read now as we've written it.  */ | 
 | 177 |   memset (buf, '\0', sizeof (buf)); | 
 | 178 |   /* Issue the commands.  */ | 
 | 179 |   for (cnt = 10; cnt > 0; ) | 
 | 180 |     { | 
 | 181 |       --cnt; | 
 | 182 |       cbp[cnt] = &cbs[cnt]; | 
 | 183 |       aio_read64 (cbp[cnt]); | 
 | 184 |     } | 
 | 185 |   /* Wait 'til the results are there.  */ | 
 | 186 |   result |= do_wait (cbp, 10, 0); | 
 | 187 |   /* Test this.  */ | 
 | 188 |   for (cnt = 0; cnt < 1000; ++cnt) | 
 | 189 |     if (buf[cnt] != '0' + (cnt / 100)) | 
 | 190 |       { | 
 | 191 | 	result = 1; | 
 | 192 | 	error (0, 0, "comparison failed for aio_read test"); | 
 | 193 | 	break; | 
 | 194 |       } | 
 | 195 |  | 
 | 196 |   if (cnt == 1000) | 
 | 197 |     puts ("aio_read test ok"); | 
 | 198 |  | 
 | 199 |   /* Remove the test file contents.  */ | 
 | 200 |   if (ftruncate64 (fd, 0) < 0) | 
 | 201 |     { | 
 | 202 |       error (0, errno, "ftruncate failed\n"); | 
 | 203 |       result = 1; | 
 | 204 |     } | 
 | 205 |  | 
 | 206 |   /* Test lio_listio.  */ | 
 | 207 |   for (cnt = 0; cnt < 10; ++cnt) | 
 | 208 |     { | 
 | 209 |       cbs[cnt].aio_lio_opcode = LIO_WRITE; | 
 | 210 |       cbp[cnt] = &cbs[cnt]; | 
 | 211 |     } | 
 | 212 |   /* Issue the command.  */ | 
 | 213 |   lio_listio64 (LIO_WAIT, cbp, 10, NULL); | 
 | 214 |   /* ...and immediately test it since we started it in wait mode.  */ | 
 | 215 |   result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)"); | 
 | 216 |  | 
 | 217 |   /* Test aio_fsync.  */ | 
 | 218 |   cbs_fsync.aio_fildes = fd; | 
 | 219 |   cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE; | 
 | 220 |   cbp_fsync[0] = &cbs_fsync; | 
 | 221 |  | 
 | 222 |   /* Remove the test file contents first.  */ | 
 | 223 |   if (ftruncate64 (fd, 0) < 0) | 
 | 224 |     { | 
 | 225 |       error (0, errno, "ftruncate failed\n"); | 
 | 226 |       result = 1; | 
 | 227 |     } | 
 | 228 |  | 
 | 229 |   /* Write again.  */ | 
 | 230 |   for (cnt = 10; cnt > 0; ) | 
 | 231 |     aio_write64 (cbp[--cnt]); | 
 | 232 |  | 
 | 233 |   if (aio_fsync64 (O_SYNC, &cbs_fsync) < 0) | 
 | 234 |     { | 
 | 235 |       error (0, errno, "aio_fsync failed\n"); | 
 | 236 |       result = 1; | 
 | 237 |     } | 
 | 238 |   result |= do_wait (cbp_fsync, 1, 0); | 
 | 239 |  | 
 | 240 |   /* ...and test since all data should be on disk now.  */ | 
 | 241 |   result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)"); | 
 | 242 |  | 
 | 243 |   /* Test aio_cancel.  */ | 
 | 244 |   /* Remove the test file contents first.  */ | 
 | 245 |   if (ftruncate64 (fd, 0) < 0) | 
 | 246 |     { | 
 | 247 |       error (0, errno, "ftruncate failed\n"); | 
 | 248 |       result = 1; | 
 | 249 |     } | 
 | 250 |  | 
 | 251 |   /* Write again.  */ | 
 | 252 |   for (cnt = 10; cnt > 0; ) | 
 | 253 |     aio_write64 (cbp[--cnt]); | 
 | 254 |  | 
 | 255 |   /* Cancel all requests.  */ | 
 | 256 |   if (aio_cancel64 (fd, NULL) == -1) | 
 | 257 |     printf ("aio_cancel64 (fd, NULL) cannot cancel anything\n"); | 
 | 258 |  | 
 | 259 |   result |= do_wait (cbp, 10, ECANCELED); | 
 | 260 |  | 
 | 261 |   /* Another test for aio_cancel.  */ | 
 | 262 |   /* Remove the test file contents first.  */ | 
 | 263 |   if (ftruncate64 (fd, 0) < 0) | 
 | 264 |     { | 
 | 265 |       error (0, errno, "ftruncate failed\n"); | 
 | 266 |       result = 1; | 
 | 267 |     } | 
 | 268 |  | 
 | 269 |   /* Write again.  */ | 
 | 270 |   for (cnt = 10; cnt > 0; ) | 
 | 271 |     { | 
 | 272 |       --cnt; | 
 | 273 |       cbp[cnt] = &cbs[cnt]; | 
 | 274 |       aio_write64 (cbp[cnt]); | 
 | 275 |     } | 
 | 276 |   puts ("finished3"); | 
 | 277 |  | 
 | 278 |   /* Cancel all requests.  */ | 
 | 279 |   for (cnt = 10; cnt > 0; ) | 
 | 280 |     if (aio_cancel64 (fd, cbp[--cnt]) == -1) | 
 | 281 |       /* This is not an error.  The request can simply be finished.  */ | 
 | 282 |       printf ("aio_cancel64 (fd, cbp[%Zd]) cannot be canceled\n", cnt); | 
 | 283 |   puts ("finished2"); | 
 | 284 |  | 
 | 285 |   result |= do_wait (cbp, 10, ECANCELED); | 
 | 286 |  | 
 | 287 |   puts ("finished"); | 
 | 288 |  | 
 | 289 |   return result; | 
 | 290 | } |