tst-aio64.c 6.7 KB

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