1/* Cancel requests associated with given file descriptor.
2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20
21/* We use an UGLY hack to prevent gcc from finding us cheating. The
22 implementation of aio_cancel and aio_cancel64 are identical and so
23 we want to avoid code duplication by using aliases. But gcc sees
24 the different parameter lists and prints a warning. We define here
25 a function so that aio_cancel64 has no prototype. */
26#ifndef aio_cancel
27#define aio_cancel64 XXX
28#include <aio.h>
29/* And undo the hack. */
30#undef aio_cancel64
31#endif
32
33#include <assert.h>
34#include <errno.h>
35#include <fcntl.h>
36
37#include <aio_misc.h>
38#include <pthreadP.h>
39
40
41int
42__aio_cancel (int fildes, struct aiocb *aiocbp)
43{
44 struct requestlist *req = NULL;
45 int result = AIO_ALLDONE;
46
47 /* If fildes is invalid, error. */
48 if (__fcntl (fildes, F_GETFL) < 0)
49 {
50 __set_errno (EBADF);
51 return -1;
52 }
53
54 /* Request the mutex. */
55 __pthread_mutex_lock (&__aio_requests_mutex);
56
57 /* We are asked to cancel a specific AIO request. */
58 if (aiocbp != NULL)
59 {
60 /* If the AIO request is not for this descriptor it has no value
61 to look for the request block. */
62 if (aiocbp->aio_fildes != fildes)
63 {
64 __pthread_mutex_unlock (&__aio_requests_mutex);
65 __set_errno (EINVAL);
66 return -1;
67 }
68 else if (aiocbp->__error_code == EINPROGRESS)
69 {
70 struct requestlist *last = NULL;
71
72 req = __aio_find_req_fd (fildes);
73
74 if (req == NULL)
75 {
76 not_found:
77 __pthread_mutex_unlock (&__aio_requests_mutex);
78 __set_errno (EINVAL);
79 return -1;
80 }
81
82 while (req->aiocbp != (aiocb_union *) aiocbp)
83 {
84 last = req;
85 req = req->next_prio;
86 if (req == NULL)
87 goto not_found;
88 }
89
90 /* Don't remove the entry if a thread is already working on it. */
91 if (req->running == allocated)
92 {
93 result = AIO_NOTCANCELED;
94 req = NULL;
95 }
96 else
97 {
98 /* We can remove the entry. */
99 __aio_remove_request (last, req, 0);
100
101 result = AIO_CANCELED;
102
103 req->next_prio = NULL;
104 }
105 }
106 }
107 else
108 {
109 /* Find the beginning of the list of all requests for this
110 desriptor. */
111 req = __aio_find_req_fd (fildes);
112
113 /* If any request is worked on by a thread it must be the first.
114 So either we can delete all requests or all but the first. */
115 if (req != NULL)
116 {
117 if (req->running == allocated)
118 {
119 struct requestlist *old = req;
120 req = req->next_prio;
121 old->next_prio = NULL;
122
123 result = AIO_NOTCANCELED;
124
125 if (req != NULL)
126 __aio_remove_request (old, req, 1);
127 }
128 else
129 {
130 result = AIO_CANCELED;
131
132 /* We can remove the entry. */
133 __aio_remove_request (NULL, req, 1);
134 }
135 }
136 }
137
138 /* Mark requests as canceled and send signal. */
139 while (req != NULL)
140 {
141 struct requestlist *old = req;
142 assert (req->running == yes || req->running == queued);
143 req->aiocbp->aiocb.__error_code = ECANCELED;
144 req->aiocbp->aiocb.__return_value = -1;
145 __aio_notify (req);
146 req = req->next_prio;
147 __aio_free_request (old);
148 }
149
150 /* Release the mutex. */
151 __pthread_mutex_unlock (&__aio_requests_mutex);
152
153 return result;
154}
155#if PTHREAD_IN_LIBC
156# ifndef __aio_cancel
157versioned_symbol (libc, __aio_cancel, aio_cancel, GLIBC_2_34);
158versioned_symbol (libc, __aio_cancel, aio_cancel64, GLIBC_2_34);
159# if OTHER_SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_34)
160compat_symbol (librt, __aio_cancel, aio_cancel, GLIBC_2_1);
161compat_symbol (librt, __aio_cancel, aio_cancel64, GLIBC_2_1);
162# endif
163# endif /* __aio_cancel */
164#else /* !PTHREAD_IN_LIBC */
165strong_alias (__aio_cancel, aio_cancel)
166weak_alias (__aio_cancel, aio_cancel64)
167#endif
168