tst-aio.c 6.6 KB

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