tst-aio4.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Test for completion signal 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. int my_signo;
  22. volatile sig_atomic_t flag;
  23. static void
  24. sighandler (const int signo)
  25. {
  26. flag = signo;
  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_signo)
  37. {
  38. printf ("signal handler received wrong signal, flag is %d\n", flag);
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. #ifndef SIGRTMIN
  44. # define SIGRTMIN -1
  45. # define SIGRTMAX -1
  46. #endif
  47. static int
  48. do_test (int argc, char *argv[])
  49. {
  50. char name[] = "/tmp/aio4.XXXXXX";
  51. int fd;
  52. struct aiocb *arr[1];
  53. struct aiocb cb;
  54. static const char buf[] = "Hello World\n";
  55. struct aioinit init = {10, 20, 0};
  56. struct sigaction sa;
  57. struct sigevent ev;
  58. if (SIGRTMIN == -1)
  59. {
  60. printf ("RT signals not supported.\n");
  61. return 0;
  62. }
  63. /* Select a signal from the middle of the available choices... */
  64. my_signo = (SIGRTMAX + SIGRTMIN) / 2;
  65. fd = mkstemp (name);
  66. if (fd == -1)
  67. {
  68. printf ("cannot open temp name: %m\n");
  69. return 1;
  70. }
  71. unlink (name);
  72. /* Test also aio_init. */
  73. aio_init (&init);
  74. arr[0] = &cb;
  75. cb.aio_fildes = fd;
  76. cb.aio_lio_opcode = LIO_WRITE;
  77. cb.aio_reqprio = 0;
  78. cb.aio_buf = (void *) buf;
  79. cb.aio_nbytes = sizeof (buf) - 1;
  80. cb.aio_offset = 0;
  81. cb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
  82. cb.aio_sigevent.sigev_notify_function = NULL;
  83. cb.aio_sigevent.sigev_notify_attributes = NULL;
  84. cb.aio_sigevent.sigev_signo = my_signo;
  85. cb.aio_sigevent.sigev_value.sival_ptr = NULL;
  86. ev.sigev_notify = SIGEV_SIGNAL;
  87. ev.sigev_notify_function = NULL;
  88. ev.sigev_notify_attributes = NULL;
  89. ev.sigev_signo = my_signo;
  90. sa.sa_handler = sighandler;
  91. sigemptyset (&sa.sa_mask);
  92. sa.sa_flags = SA_RESTART;
  93. if (sigaction (my_signo, &sa, NULL) < 0)
  94. {
  95. printf ("sigaction failed: %m\n");
  96. return 1;
  97. }
  98. flag = 0;
  99. /* First use aio_write. */
  100. if (aio_write (arr[0]) < 0)
  101. {
  102. if (errno == ENOSYS)
  103. {
  104. puts ("no aio support in this configuration");
  105. return 0;
  106. }
  107. printf ("aio_write failed: %m\n");
  108. return 1;
  109. }
  110. if (wait_flag ())
  111. return 1;
  112. puts ("aio_write OK");
  113. flag = 0;
  114. /* Again with lio_listio. */
  115. if (lio_listio (LIO_NOWAIT, arr, 1, &ev) < 0)
  116. {
  117. printf ("lio_listio failed: %m\n");
  118. return 1;
  119. }
  120. if (wait_flag ())
  121. return 1;
  122. puts ("all OK");
  123. return 0;
  124. }
  125. #include "../test-skeleton.c"