async_wait.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2016-2018 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. /* This must be the first #include file */
  10. #include "async_locl.h"
  11. #include <openssl/err.h>
  12. ASYNC_WAIT_CTX *ASYNC_WAIT_CTX_new(void)
  13. {
  14. return OPENSSL_zalloc(sizeof(ASYNC_WAIT_CTX));
  15. }
  16. void ASYNC_WAIT_CTX_free(ASYNC_WAIT_CTX *ctx)
  17. {
  18. struct fd_lookup_st *curr;
  19. struct fd_lookup_st *next;
  20. if (ctx == NULL)
  21. return;
  22. curr = ctx->fds;
  23. while (curr != NULL) {
  24. if (!curr->del) {
  25. /* Only try and cleanup if it hasn't been marked deleted */
  26. if (curr->cleanup != NULL)
  27. curr->cleanup(ctx, curr->key, curr->fd, curr->custom_data);
  28. }
  29. /* Always free the fd_lookup_st */
  30. next = curr->next;
  31. OPENSSL_free(curr);
  32. curr = next;
  33. }
  34. OPENSSL_free(ctx);
  35. }
  36. int ASYNC_WAIT_CTX_set_wait_fd(ASYNC_WAIT_CTX *ctx, const void *key,
  37. OSSL_ASYNC_FD fd, void *custom_data,
  38. void (*cleanup)(ASYNC_WAIT_CTX *, const void *,
  39. OSSL_ASYNC_FD, void *))
  40. {
  41. struct fd_lookup_st *fdlookup;
  42. if ((fdlookup = OPENSSL_zalloc(sizeof(*fdlookup))) == NULL) {
  43. ASYNCerr(ASYNC_F_ASYNC_WAIT_CTX_SET_WAIT_FD, ERR_R_MALLOC_FAILURE);
  44. return 0;
  45. }
  46. fdlookup->key = key;
  47. fdlookup->fd = fd;
  48. fdlookup->custom_data = custom_data;
  49. fdlookup->cleanup = cleanup;
  50. fdlookup->add = 1;
  51. fdlookup->next = ctx->fds;
  52. ctx->fds = fdlookup;
  53. ctx->numadd++;
  54. return 1;
  55. }
  56. int ASYNC_WAIT_CTX_get_fd(ASYNC_WAIT_CTX *ctx, const void *key,
  57. OSSL_ASYNC_FD *fd, void **custom_data)
  58. {
  59. struct fd_lookup_st *curr;
  60. curr = ctx->fds;
  61. while (curr != NULL) {
  62. if (curr->del) {
  63. /* This one has been marked deleted so do nothing */
  64. curr = curr->next;
  65. continue;
  66. }
  67. if (curr->key == key) {
  68. *fd = curr->fd;
  69. *custom_data = curr->custom_data;
  70. return 1;
  71. }
  72. curr = curr->next;
  73. }
  74. return 0;
  75. }
  76. int ASYNC_WAIT_CTX_get_all_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *fd,
  77. size_t *numfds)
  78. {
  79. struct fd_lookup_st *curr;
  80. curr = ctx->fds;
  81. *numfds = 0;
  82. while (curr != NULL) {
  83. if (curr->del) {
  84. /* This one has been marked deleted so do nothing */
  85. curr = curr->next;
  86. continue;
  87. }
  88. if (fd != NULL) {
  89. *fd = curr->fd;
  90. fd++;
  91. }
  92. (*numfds)++;
  93. curr = curr->next;
  94. }
  95. return 1;
  96. }
  97. int ASYNC_WAIT_CTX_get_changed_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *addfd,
  98. size_t *numaddfds, OSSL_ASYNC_FD *delfd,
  99. size_t *numdelfds)
  100. {
  101. struct fd_lookup_st *curr;
  102. *numaddfds = ctx->numadd;
  103. *numdelfds = ctx->numdel;
  104. if (addfd == NULL && delfd == NULL)
  105. return 1;
  106. curr = ctx->fds;
  107. while (curr != NULL) {
  108. /* We ignore fds that have been marked as both added and deleted */
  109. if (curr->del && !curr->add && (delfd != NULL)) {
  110. *delfd = curr->fd;
  111. delfd++;
  112. }
  113. if (curr->add && !curr->del && (addfd != NULL)) {
  114. *addfd = curr->fd;
  115. addfd++;
  116. }
  117. curr = curr->next;
  118. }
  119. return 1;
  120. }
  121. int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key)
  122. {
  123. struct fd_lookup_st *curr, *prev;
  124. curr = ctx->fds;
  125. prev = NULL;
  126. while (curr != NULL) {
  127. if (curr->del == 1) {
  128. /* This one has been marked deleted already so do nothing */
  129. prev = curr;
  130. curr = curr->next;
  131. continue;
  132. }
  133. if (curr->key == key) {
  134. /* If fd has just been added, remove it from the list */
  135. if (curr->add == 1) {
  136. if (ctx->fds == curr) {
  137. ctx->fds = curr->next;
  138. } else {
  139. prev->next = curr->next;
  140. }
  141. /* It is responsibility of the caller to cleanup before calling
  142. * ASYNC_WAIT_CTX_clear_fd
  143. */
  144. OPENSSL_free(curr);
  145. ctx->numadd--;
  146. return 1;
  147. }
  148. /*
  149. * Mark it as deleted. We don't call cleanup if explicitly asked
  150. * to clear an fd. We assume the caller is going to do that (if
  151. * appropriate).
  152. */
  153. curr->del = 1;
  154. ctx->numdel++;
  155. return 1;
  156. }
  157. prev = curr;
  158. curr = curr->next;
  159. }
  160. return 0;
  161. }
  162. int ASYNC_WAIT_CTX_set_callback(ASYNC_WAIT_CTX *ctx,
  163. ASYNC_callback_fn callback,
  164. void *callback_arg)
  165. {
  166. if (ctx == NULL)
  167. return 0;
  168. ctx->callback = callback;
  169. ctx->callback_arg = callback_arg;
  170. return 1;
  171. }
  172. int ASYNC_WAIT_CTX_get_callback(ASYNC_WAIT_CTX *ctx,
  173. ASYNC_callback_fn *callback,
  174. void **callback_arg)
  175. {
  176. if (ctx->callback == NULL)
  177. return 0;
  178. *callback = ctx->callback;
  179. *callback_arg = ctx->callback_arg;
  180. return 1;
  181. }
  182. int ASYNC_WAIT_CTX_set_status(ASYNC_WAIT_CTX *ctx, int status)
  183. {
  184. ctx->status = status;
  185. return 1;
  186. }
  187. int ASYNC_WAIT_CTX_get_status(ASYNC_WAIT_CTX *ctx)
  188. {
  189. return ctx->status;
  190. }
  191. void async_wait_ctx_reset_counts(ASYNC_WAIT_CTX *ctx)
  192. {
  193. struct fd_lookup_st *curr, *prev = NULL;
  194. ctx->numadd = 0;
  195. ctx->numdel = 0;
  196. curr = ctx->fds;
  197. while (curr != NULL) {
  198. if (curr->del) {
  199. if (prev == NULL)
  200. ctx->fds = curr->next;
  201. else
  202. prev->next = curr->next;
  203. OPENSSL_free(curr);
  204. if (prev == NULL)
  205. curr = ctx->fds;
  206. else
  207. curr = prev->next;
  208. continue;
  209. }
  210. if (curr->add) {
  211. curr->add = 0;
  212. }
  213. prev = curr;
  214. curr = curr->next;
  215. }
  216. }