aio_cancel.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Cancel requests associated with given file descriptor.
  2. Copyright (C) 1997-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. /* We use an UGLY hack to prevent gcc from finding us cheating. The
  16. implementation of aio_cancel and aio_cancel64 are identical and so
  17. we want to avoid code duplication by using aliases. But gcc sees
  18. the different parameter lists and prints a warning. We define here
  19. a function so that aio_cancel64 has no prototype. */
  20. #ifndef aio_cancel
  21. #define aio_cancel64 XXX
  22. #include <aio.h>
  23. /* And undo the hack. */
  24. #undef aio_cancel64
  25. #endif
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <aio_misc.h>
  30. #include <pthreadP.h>
  31. int
  32. __aio_cancel (int fildes, struct aiocb *aiocbp)
  33. {
  34. struct requestlist *req = NULL;
  35. int result = AIO_ALLDONE;
  36. /* If fildes is invalid, error. */
  37. if (__fcntl (fildes, F_GETFL) < 0)
  38. {
  39. __set_errno (EBADF);
  40. return -1;
  41. }
  42. /* Request the mutex. */
  43. __pthread_mutex_lock (&__aio_requests_mutex);
  44. /* We are asked to cancel a specific AIO request. */
  45. if (aiocbp != NULL)
  46. {
  47. /* If the AIO request is not for this descriptor it has no value
  48. to look for the request block. */
  49. if (aiocbp->aio_fildes != fildes)
  50. {
  51. __pthread_mutex_unlock (&__aio_requests_mutex);
  52. __set_errno (EINVAL);
  53. return -1;
  54. }
  55. else if (aiocbp->__error_code == EINPROGRESS)
  56. {
  57. struct requestlist *last = NULL;
  58. req = __aio_find_req_fd (fildes);
  59. if (req == NULL)
  60. {
  61. not_found:
  62. __pthread_mutex_unlock (&__aio_requests_mutex);
  63. __set_errno (EINVAL);
  64. return -1;
  65. }
  66. while (req->aiocbp != (aiocb_union *) aiocbp)
  67. {
  68. last = req;
  69. req = req->next_prio;
  70. if (req == NULL)
  71. goto not_found;
  72. }
  73. /* Don't remove the entry if a thread is already working on it. */
  74. if (req->running == allocated)
  75. {
  76. result = AIO_NOTCANCELED;
  77. req = NULL;
  78. }
  79. else
  80. {
  81. /* We can remove the entry. */
  82. __aio_remove_request (last, req, 0);
  83. result = AIO_CANCELED;
  84. req->next_prio = NULL;
  85. }
  86. }
  87. }
  88. else
  89. {
  90. /* Find the beginning of the list of all requests for this
  91. descriptor. */
  92. req = __aio_find_req_fd (fildes);
  93. /* If any request is worked on by a thread it must be the first.
  94. So either we can delete all requests or all but the first. */
  95. if (req != NULL)
  96. {
  97. if (req->running == allocated)
  98. {
  99. struct requestlist *old = req;
  100. req = req->next_prio;
  101. old->next_prio = NULL;
  102. result = AIO_NOTCANCELED;
  103. if (req != NULL)
  104. __aio_remove_request (old, req, 1);
  105. }
  106. else
  107. {
  108. result = AIO_CANCELED;
  109. /* We can remove the entry. */
  110. __aio_remove_request (NULL, req, 1);
  111. }
  112. }
  113. }
  114. /* Mark requests as canceled and send signal. */
  115. while (req != NULL)
  116. {
  117. struct requestlist *old = req;
  118. assert (req->running == yes || req->running == queued);
  119. req->aiocbp->aiocb.__error_code = ECANCELED;
  120. req->aiocbp->aiocb.__return_value = -1;
  121. __aio_notify (req);
  122. req = req->next_prio;
  123. __aio_free_request (old);
  124. }
  125. /* Release the mutex. */
  126. __pthread_mutex_unlock (&__aio_requests_mutex);
  127. return result;
  128. }
  129. #if PTHREAD_IN_LIBC
  130. # ifndef __aio_cancel
  131. versioned_symbol (libc, __aio_cancel, aio_cancel, GLIBC_2_34);
  132. versioned_symbol (libc, __aio_cancel, aio_cancel64, GLIBC_2_34);
  133. # if OTHER_SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_34)
  134. compat_symbol (librt, __aio_cancel, aio_cancel, GLIBC_2_1);
  135. compat_symbol (librt, __aio_cancel, aio_cancel64, GLIBC_2_1);
  136. # endif
  137. # endif /* __aio_cancel */
  138. #else /* !PTHREAD_IN_LIBC */
  139. strong_alias (__aio_cancel, aio_cancel)
  140. weak_alias (__aio_cancel, aio_cancel64)
  141. #endif