/src/openssl30/crypto/async/async_wait.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* This must be the first #include file */ |
11 | | #include "async_local.h" |
12 | | |
13 | | #include <openssl/err.h> |
14 | | |
15 | | ASYNC_WAIT_CTX *ASYNC_WAIT_CTX_new(void) |
16 | 0 | { |
17 | 0 | return OPENSSL_zalloc(sizeof(ASYNC_WAIT_CTX)); |
18 | 0 | } |
19 | | |
20 | | void ASYNC_WAIT_CTX_free(ASYNC_WAIT_CTX *ctx) |
21 | 79.2k | { |
22 | 79.2k | struct fd_lookup_st *curr; |
23 | 79.2k | struct fd_lookup_st *next; |
24 | | |
25 | 79.2k | if (ctx == NULL) |
26 | 79.2k | return; |
27 | | |
28 | 0 | curr = ctx->fds; |
29 | 0 | while (curr != NULL) { |
30 | 0 | if (!curr->del) { |
31 | | /* Only try and cleanup if it hasn't been marked deleted */ |
32 | 0 | if (curr->cleanup != NULL) |
33 | 0 | curr->cleanup(ctx, curr->key, curr->fd, curr->custom_data); |
34 | 0 | } |
35 | | /* Always free the fd_lookup_st */ |
36 | 0 | next = curr->next; |
37 | 0 | OPENSSL_free(curr); |
38 | 0 | curr = next; |
39 | 0 | } |
40 | |
|
41 | 0 | OPENSSL_free(ctx); |
42 | 0 | } |
43 | | int ASYNC_WAIT_CTX_set_wait_fd(ASYNC_WAIT_CTX *ctx, const void *key, |
44 | | OSSL_ASYNC_FD fd, void *custom_data, |
45 | | void (*cleanup)(ASYNC_WAIT_CTX *, const void *, |
46 | | OSSL_ASYNC_FD, void *)) |
47 | 0 | { |
48 | 0 | struct fd_lookup_st *fdlookup; |
49 | |
|
50 | 0 | if ((fdlookup = OPENSSL_zalloc(sizeof(*fdlookup))) == NULL) { |
51 | 0 | ERR_raise(ERR_LIB_ASYNC, ERR_R_MALLOC_FAILURE); |
52 | 0 | return 0; |
53 | 0 | } |
54 | | |
55 | 0 | fdlookup->key = key; |
56 | 0 | fdlookup->fd = fd; |
57 | 0 | fdlookup->custom_data = custom_data; |
58 | 0 | fdlookup->cleanup = cleanup; |
59 | 0 | fdlookup->add = 1; |
60 | 0 | fdlookup->next = ctx->fds; |
61 | 0 | ctx->fds = fdlookup; |
62 | 0 | ctx->numadd++; |
63 | 0 | return 1; |
64 | 0 | } |
65 | | |
66 | | int ASYNC_WAIT_CTX_get_fd(ASYNC_WAIT_CTX *ctx, const void *key, |
67 | | OSSL_ASYNC_FD *fd, void **custom_data) |
68 | 0 | { |
69 | 0 | struct fd_lookup_st *curr; |
70 | |
|
71 | 0 | curr = ctx->fds; |
72 | 0 | while (curr != NULL) { |
73 | 0 | if (curr->del) { |
74 | | /* This one has been marked deleted so do nothing */ |
75 | 0 | curr = curr->next; |
76 | 0 | continue; |
77 | 0 | } |
78 | 0 | if (curr->key == key) { |
79 | 0 | *fd = curr->fd; |
80 | 0 | *custom_data = curr->custom_data; |
81 | 0 | return 1; |
82 | 0 | } |
83 | 0 | curr = curr->next; |
84 | 0 | } |
85 | 0 | return 0; |
86 | 0 | } |
87 | | |
88 | | int ASYNC_WAIT_CTX_get_all_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *fd, |
89 | | size_t *numfds) |
90 | 0 | { |
91 | 0 | struct fd_lookup_st *curr; |
92 | |
|
93 | 0 | curr = ctx->fds; |
94 | 0 | *numfds = 0; |
95 | 0 | while (curr != NULL) { |
96 | 0 | if (curr->del) { |
97 | | /* This one has been marked deleted so do nothing */ |
98 | 0 | curr = curr->next; |
99 | 0 | continue; |
100 | 0 | } |
101 | 0 | if (fd != NULL) { |
102 | 0 | *fd = curr->fd; |
103 | 0 | fd++; |
104 | 0 | } |
105 | 0 | (*numfds)++; |
106 | 0 | curr = curr->next; |
107 | 0 | } |
108 | 0 | return 1; |
109 | 0 | } |
110 | | |
111 | | int ASYNC_WAIT_CTX_get_changed_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *addfd, |
112 | | size_t *numaddfds, OSSL_ASYNC_FD *delfd, |
113 | | size_t *numdelfds) |
114 | 0 | { |
115 | 0 | struct fd_lookup_st *curr; |
116 | |
|
117 | 0 | *numaddfds = ctx->numadd; |
118 | 0 | *numdelfds = ctx->numdel; |
119 | 0 | if (addfd == NULL && delfd == NULL) |
120 | 0 | return 1; |
121 | | |
122 | 0 | curr = ctx->fds; |
123 | |
|
124 | 0 | while (curr != NULL) { |
125 | | /* We ignore fds that have been marked as both added and deleted */ |
126 | 0 | if (curr->del && !curr->add && (delfd != NULL)) { |
127 | 0 | *delfd = curr->fd; |
128 | 0 | delfd++; |
129 | 0 | } |
130 | 0 | if (curr->add && !curr->del && (addfd != NULL)) { |
131 | 0 | *addfd = curr->fd; |
132 | 0 | addfd++; |
133 | 0 | } |
134 | 0 | curr = curr->next; |
135 | 0 | } |
136 | |
|
137 | 0 | return 1; |
138 | 0 | } |
139 | | |
140 | | int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key) |
141 | 0 | { |
142 | 0 | struct fd_lookup_st *curr, *prev; |
143 | |
|
144 | 0 | curr = ctx->fds; |
145 | 0 | prev = NULL; |
146 | 0 | while (curr != NULL) { |
147 | 0 | if (curr->del == 1) { |
148 | | /* This one has been marked deleted already so do nothing */ |
149 | 0 | prev = curr; |
150 | 0 | curr = curr->next; |
151 | 0 | continue; |
152 | 0 | } |
153 | 0 | if (curr->key == key) { |
154 | | /* If fd has just been added, remove it from the list */ |
155 | 0 | if (curr->add == 1) { |
156 | 0 | if (ctx->fds == curr) { |
157 | 0 | ctx->fds = curr->next; |
158 | 0 | } else { |
159 | 0 | prev->next = curr->next; |
160 | 0 | } |
161 | | |
162 | | /* It is responsibility of the caller to cleanup before calling |
163 | | * ASYNC_WAIT_CTX_clear_fd |
164 | | */ |
165 | 0 | OPENSSL_free(curr); |
166 | 0 | ctx->numadd--; |
167 | 0 | return 1; |
168 | 0 | } |
169 | | |
170 | | /* |
171 | | * Mark it as deleted. We don't call cleanup if explicitly asked |
172 | | * to clear an fd. We assume the caller is going to do that (if |
173 | | * appropriate). |
174 | | */ |
175 | 0 | curr->del = 1; |
176 | 0 | ctx->numdel++; |
177 | 0 | return 1; |
178 | 0 | } |
179 | 0 | prev = curr; |
180 | 0 | curr = curr->next; |
181 | 0 | } |
182 | 0 | return 0; |
183 | 0 | } |
184 | | |
185 | | int ASYNC_WAIT_CTX_set_callback(ASYNC_WAIT_CTX *ctx, |
186 | | ASYNC_callback_fn callback, |
187 | | void *callback_arg) |
188 | 0 | { |
189 | 0 | if (ctx == NULL) |
190 | 0 | return 0; |
191 | | |
192 | 0 | ctx->callback = callback; |
193 | 0 | ctx->callback_arg = callback_arg; |
194 | 0 | return 1; |
195 | 0 | } |
196 | | |
197 | | int ASYNC_WAIT_CTX_get_callback(ASYNC_WAIT_CTX *ctx, |
198 | | ASYNC_callback_fn *callback, |
199 | | void **callback_arg) |
200 | 0 | { |
201 | 0 | if (ctx->callback == NULL) |
202 | 0 | return 0; |
203 | | |
204 | 0 | *callback = ctx->callback; |
205 | 0 | *callback_arg = ctx->callback_arg; |
206 | 0 | return 1; |
207 | 0 | } |
208 | | |
209 | | int ASYNC_WAIT_CTX_set_status(ASYNC_WAIT_CTX *ctx, int status) |
210 | 0 | { |
211 | 0 | ctx->status = status; |
212 | 0 | return 1; |
213 | 0 | } |
214 | | |
215 | | int ASYNC_WAIT_CTX_get_status(ASYNC_WAIT_CTX *ctx) |
216 | 0 | { |
217 | 0 | return ctx->status; |
218 | 0 | } |
219 | | |
220 | | void async_wait_ctx_reset_counts(ASYNC_WAIT_CTX *ctx) |
221 | 0 | { |
222 | 0 | struct fd_lookup_st *curr, *prev = NULL; |
223 | |
|
224 | 0 | ctx->numadd = 0; |
225 | 0 | ctx->numdel = 0; |
226 | |
|
227 | 0 | curr = ctx->fds; |
228 | |
|
229 | 0 | while (curr != NULL) { |
230 | 0 | if (curr->del) { |
231 | 0 | if (prev == NULL) |
232 | 0 | ctx->fds = curr->next; |
233 | 0 | else |
234 | 0 | prev->next = curr->next; |
235 | 0 | OPENSSL_free(curr); |
236 | 0 | if (prev == NULL) |
237 | 0 | curr = ctx->fds; |
238 | 0 | else |
239 | 0 | curr = prev->next; |
240 | 0 | continue; |
241 | 0 | } |
242 | 0 | if (curr->add) { |
243 | 0 | curr->add = 0; |
244 | 0 | } |
245 | 0 | prev = curr; |
246 | 0 | curr = curr->next; |
247 | 0 | } |
248 | 0 | } |