tst-aio6.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Test for timeout handling.
  2. Copyright (C) 2000-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 <stdio.h>
  18. #include <unistd.h>
  19. #include <sys/time.h>
  20. #define TEST_FUNCTION do_test ()
  21. static int
  22. do_test (void)
  23. {
  24. struct aiocb *arr[1];
  25. struct aiocb cb;
  26. char buf[100];
  27. struct timeval before;
  28. struct timeval after;
  29. struct timespec timeout;
  30. int fd[2];
  31. int result = 0;
  32. if (pipe (fd) != 0)
  33. {
  34. printf ("cannot create pipe: %m\n");
  35. return 1;
  36. }
  37. arr[0] = &cb;
  38. cb.aio_fildes = fd[0];
  39. cb.aio_lio_opcode = LIO_WRITE;
  40. cb.aio_reqprio = 0;
  41. cb.aio_buf = (void *) buf;
  42. cb.aio_nbytes = sizeof (buf) - 1;
  43. cb.aio_offset = 0;
  44. cb.aio_sigevent.sigev_notify = SIGEV_NONE;
  45. /* Try to read from stdin where nothing will be available. */
  46. if (aio_read (arr[0]) < 0)
  47. {
  48. if (errno == ENOSYS)
  49. {
  50. puts ("no aio support in this configuration");
  51. return 0;
  52. }
  53. printf ("aio_read failed: %m\n");
  54. return 1;
  55. }
  56. /* Get the current time. */
  57. gettimeofday (&before, NULL);
  58. /* Wait for input which is unsuccessful and therefore the function will
  59. time out. */
  60. timeout.tv_sec = 3;
  61. timeout.tv_nsec = 0;
  62. if (aio_suspend ((const struct aiocb *const*) arr, 1, &timeout) != -1)
  63. {
  64. puts ("aio_suspend() didn't return -1");
  65. result = 1;
  66. }
  67. else if (errno != EAGAIN)
  68. {
  69. puts ("error not set to EAGAIN");
  70. result = 1;
  71. }
  72. else
  73. {
  74. gettimeofday (&after, NULL);
  75. if (after.tv_sec < before.tv_sec + 1)
  76. {
  77. puts ("timeout came too early");
  78. result = 1;
  79. }
  80. }
  81. return result;
  82. }
  83. #include "../test-skeleton.c"