tst-aio7.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Test for AIO POSIX compliance.
  2. Copyright (C) 2001-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 <error.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #define TEST_FUNCTION do_test ()
  23. static int
  24. do_test (void)
  25. {
  26. int result = 0;
  27. int piped[2];
  28. /* Make a pipe that we will never write to, so we can block reading it. */
  29. if (pipe (piped) < 0)
  30. {
  31. perror ("pipe");
  32. return 1;
  33. }
  34. /* Test for aio_cancel() detecting invalid file descriptor. */
  35. {
  36. struct aiocb cb;
  37. int fd = -1;
  38. cb.aio_fildes = fd;
  39. cb.aio_offset = 0;
  40. cb.aio_buf = NULL;
  41. cb.aio_nbytes = 0;
  42. cb.aio_reqprio = 0;
  43. cb.aio_sigevent.sigev_notify = SIGEV_NONE;
  44. errno = 0;
  45. /* Case one: invalid fds that match. */
  46. if (aio_cancel (fd, &cb) != -1 || errno != EBADF)
  47. {
  48. if (errno == ENOSYS)
  49. {
  50. puts ("no aio support in this configuration");
  51. return 0;
  52. }
  53. puts ("aio_cancel( -1, {-1..} ) did not return -1 or errno != EBADF");
  54. ++result;
  55. }
  56. cb.aio_fildes = -2;
  57. errno = 0;
  58. /* Case two: invalid fds that do not match; just print warning. */
  59. if (aio_cancel (fd, &cb) != -1 || errno != EBADF)
  60. puts ("aio_cancel( -1, {-2..} ) did not return -1 or errno != EBADF");
  61. }
  62. /* Test for aio_fsync() detecting bad fd. */
  63. {
  64. struct aiocb cb;
  65. int fd = -1;
  66. cb.aio_fildes = fd;
  67. cb.aio_offset = 0;
  68. cb.aio_buf = NULL;
  69. cb.aio_nbytes = 0;
  70. cb.aio_reqprio = 0;
  71. cb.aio_sigevent.sigev_notify = SIGEV_NONE;
  72. errno = 0;
  73. /* Case one: invalid fd. */
  74. if (aio_fsync (O_SYNC, &cb) != -1 || errno != EBADF)
  75. {
  76. puts ("aio_fsync( op, {-1..} ) did not return -1 or errno != EBADF");
  77. ++result;
  78. }
  79. }
  80. /* Test for aio_suspend() suspending even if completed elements in list. */
  81. {
  82. #define BYTES 8
  83. const int ELEMS = 2;
  84. int i, r, fd;
  85. static char buff[BYTES];
  86. char name[] = "/tmp/aio7.XXXXXX";
  87. struct timespec timeout;
  88. static struct aiocb cb0, cb1;
  89. struct aiocb *list[ELEMS];
  90. fd = mkstemp (name);
  91. if (fd < 0)
  92. error (1, errno, "creating temp file");
  93. if (unlink (name))
  94. error (1, errno, "unlinking temp file");
  95. if (write (fd, "01234567", BYTES) != BYTES)
  96. error (1, errno, "writing to temp file");
  97. cb0.aio_fildes = fd;
  98. cb0.aio_offset = 0;
  99. cb0.aio_buf = buff;
  100. cb0.aio_nbytes = BYTES;
  101. cb0.aio_reqprio = 0;
  102. cb0.aio_sigevent.sigev_notify = SIGEV_NONE;
  103. r = aio_read (&cb0);
  104. if (r != 0)
  105. error (1, errno, "reading from file");
  106. while (aio_error (&(cb0)) == EINPROGRESS)
  107. usleep (10);
  108. for (i = 0; i < BYTES; i++)
  109. printf ("%c ", buff[i]);
  110. printf ("\n");
  111. /* At this point, the first read is completed, so start another one on
  112. the read half of a pipe on which nothing will be written. */
  113. cb1.aio_fildes = piped[0];
  114. cb1.aio_offset = 0;
  115. cb1.aio_buf = buff;
  116. cb1.aio_nbytes = BYTES;
  117. cb1.aio_reqprio = 0;
  118. cb1.aio_sigevent.sigev_notify = SIGEV_NONE;
  119. r = aio_read (&cb1);
  120. if (r != 0)
  121. error (1, errno, "reading from file");
  122. /* Now call aio_suspend() with the two reads. It should return
  123. * immediately according to the POSIX spec.
  124. */
  125. list[0] = &cb0;
  126. list[1] = &cb1;
  127. timeout.tv_sec = 3;
  128. timeout.tv_nsec = 0;
  129. r = aio_suspend ((const struct aiocb * const *) list, ELEMS, &timeout);
  130. if (r == -1 && errno == EAGAIN)
  131. {
  132. puts ("aio_suspend([done,blocked],2,3) suspended thread");
  133. ++result;
  134. }
  135. /* Note that CB1 is still pending, and so cannot be an auto variable.
  136. Thus we also test that exiting with an outstanding request works. */
  137. }
  138. return result;
  139. }
  140. #include "../test-skeleton.c"