tst-aio5.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Test for completion thread 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 <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #define MY_SIVAL 27
  22. volatile sig_atomic_t flag;
  23. static void
  24. callback (sigval_t s)
  25. {
  26. flag = s.sival_int;
  27. }
  28. static int
  29. wait_flag (void)
  30. {
  31. while (flag == 0)
  32. {
  33. puts ("Sleeping...");
  34. sleep (1);
  35. }
  36. if (flag != MY_SIVAL)
  37. {
  38. printf ("signal handler received wrong signal, flag is %d\n", flag);
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. static int
  44. do_test (int argc, char *argv[])
  45. {
  46. char name[] = "/tmp/aio5.XXXXXX";
  47. int fd;
  48. struct aiocb *arr[1];
  49. struct aiocb cb;
  50. static const char buf[] = "Hello World\n";
  51. struct sigevent ev;
  52. fd = mkstemp (name);
  53. if (fd == -1)
  54. {
  55. printf ("cannot open temp name: %m\n");
  56. return 1;
  57. }
  58. unlink (name);
  59. arr[0] = &cb;
  60. cb.aio_fildes = fd;
  61. cb.aio_lio_opcode = LIO_WRITE;
  62. cb.aio_reqprio = 0;
  63. cb.aio_buf = (void *) buf;
  64. cb.aio_nbytes = sizeof (buf) - 1;
  65. cb.aio_offset = 0;
  66. cb.aio_sigevent.sigev_notify = SIGEV_THREAD;
  67. cb.aio_sigevent.sigev_notify_function = callback;
  68. cb.aio_sigevent.sigev_notify_attributes = NULL;
  69. cb.aio_sigevent.sigev_value.sival_int = MY_SIVAL;
  70. ev.sigev_notify = SIGEV_THREAD;
  71. ev.sigev_notify_function = callback;
  72. ev.sigev_notify_attributes = NULL;
  73. ev.sigev_value.sival_int = MY_SIVAL;
  74. /* First use aio_write. */
  75. if (aio_write (arr[0]) < 0)
  76. {
  77. if (errno == ENOSYS)
  78. {
  79. puts ("no aio support in this configuration");
  80. return 0;
  81. }
  82. printf ("aio_write failed: %m\n");
  83. return 1;
  84. }
  85. if (wait_flag ())
  86. return 1;
  87. puts ("aio_write OK");
  88. flag = 0;
  89. /* Again with lio_listio. */
  90. if (lio_listio (LIO_NOWAIT, arr, 1, &ev) < 0)
  91. {
  92. printf ("lio_listio failed: %m\n");
  93. return 1;
  94. }
  95. if (wait_flag ())
  96. return 1;
  97. puts ("all OK");
  98. return 0;
  99. }
  100. #include "../test-skeleton.c"