2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* This must be the first #include file */
11 #include "async_local.h"
13 #include <openssl/err.h>
15 ASYNC_WAIT_CTX
*ASYNC_WAIT_CTX_new(void)
17 return OPENSSL_zalloc(sizeof(ASYNC_WAIT_CTX
));
20 void ASYNC_WAIT_CTX_free(ASYNC_WAIT_CTX
*ctx
)
22 struct fd_lookup_st
*curr
;
23 struct fd_lookup_st
*next
;
29 while (curr
!= NULL
) {
31 /* Only try and cleanup if it hasn't been marked deleted */
32 if (curr
->cleanup
!= NULL
)
33 curr
->cleanup(ctx
, curr
->key
, curr
->fd
, curr
->custom_data
);
35 /* Always free the fd_lookup_st */
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 *))
48 struct fd_lookup_st
*fdlookup
;
50 if ((fdlookup
= OPENSSL_zalloc(sizeof(*fdlookup
))) == NULL
) {
51 ASYNCerr(ASYNC_F_ASYNC_WAIT_CTX_SET_WAIT_FD
, ERR_R_MALLOC_FAILURE
);
57 fdlookup
->custom_data
= custom_data
;
58 fdlookup
->cleanup
= cleanup
;
60 fdlookup
->next
= ctx
->fds
;
66 int ASYNC_WAIT_CTX_get_fd(ASYNC_WAIT_CTX
*ctx
, const void *key
,
67 OSSL_ASYNC_FD
*fd
, void **custom_data
)
69 struct fd_lookup_st
*curr
;
72 while (curr
!= NULL
) {
74 /* This one has been marked deleted so do nothing */
78 if (curr
->key
== key
) {
80 *custom_data
= curr
->custom_data
;
88 int ASYNC_WAIT_CTX_get_all_fds(ASYNC_WAIT_CTX
*ctx
, OSSL_ASYNC_FD
*fd
,
91 struct fd_lookup_st
*curr
;
95 while (curr
!= NULL
) {
97 /* This one has been marked deleted so do nothing */
111 int ASYNC_WAIT_CTX_get_changed_fds(ASYNC_WAIT_CTX
*ctx
, OSSL_ASYNC_FD
*addfd
,
112 size_t *numaddfds
, OSSL_ASYNC_FD
*delfd
,
115 struct fd_lookup_st
*curr
;
117 *numaddfds
= ctx
->numadd
;
118 *numdelfds
= ctx
->numdel
;
119 if (addfd
== NULL
&& delfd
== NULL
)
124 while (curr
!= NULL
) {
125 /* We ignore fds that have been marked as both added and deleted */
126 if (curr
->del
&& !curr
->add
&& (delfd
!= NULL
)) {
130 if (curr
->add
&& !curr
->del
&& (addfd
!= NULL
)) {
140 int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX
*ctx
, const void *key
)
142 struct fd_lookup_st
*curr
, *prev
;
146 while (curr
!= NULL
) {
147 if (curr
->del
== 1) {
148 /* This one has been marked deleted already so do nothing */
153 if (curr
->key
== key
) {
154 /* If fd has just been added, remove it from the list */
155 if (curr
->add
== 1) {
156 if (ctx
->fds
== curr
) {
157 ctx
->fds
= curr
->next
;
159 prev
->next
= curr
->next
;
162 /* It is responsibility of the caller to cleanup before calling
163 * ASYNC_WAIT_CTX_clear_fd
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
185 int ASYNC_WAIT_CTX_set_callback(ASYNC_WAIT_CTX
*ctx
,
186 ASYNC_callback_fn callback
,
192 ctx
->callback
= callback
;
193 ctx
->callback_arg
= callback_arg
;
197 int ASYNC_WAIT_CTX_get_callback(ASYNC_WAIT_CTX
*ctx
,
198 ASYNC_callback_fn
*callback
,
201 if (ctx
->callback
== NULL
)
204 *callback
= ctx
->callback
;
205 *callback_arg
= ctx
->callback_arg
;
209 int ASYNC_WAIT_CTX_set_status(ASYNC_WAIT_CTX
*ctx
, int status
)
211 ctx
->status
= status
;
215 int ASYNC_WAIT_CTX_get_status(ASYNC_WAIT_CTX
*ctx
)
220 void async_wait_ctx_reset_counts(ASYNC_WAIT_CTX
*ctx
)
222 struct fd_lookup_st
*curr
, *prev
= NULL
;
229 while (curr
!= NULL
) {
232 ctx
->fds
= curr
->next
;
234 prev
->next
= curr
->next
;