ossl_lib_ctx_get_data.pod 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. =pod
  2. =head1 NAME
  3. ossl_lib_ctx_get_data, ossl_lib_ctx_run_once, ossl_lib_ctx_onfree,
  4. ossl_lib_ctx_is_child
  5. - internal OSSL_LIB_CTX routines
  6. =head1 SYNOPSIS
  7. #include <openssl/types.h>
  8. #include "internal/cryptlib.h"
  9. void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index);
  10. int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
  11. ossl_lib_ctx_run_once_fn run_once_fn);
  12. int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn);
  13. int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx);
  14. =head1 DESCRIPTION
  15. ossl_lib_ctx_run_once() is used to run some initialisation routine I<run_once_fn>
  16. exactly once per library context I<ctx> object. Each initialisation routine
  17. should be allocate a unique run once index in cryptlib.h.
  18. Any resources allocated via a run once initialisation routine can be cleaned up
  19. using ossl_lib_ctx_onfree(). This associates an "on free" routine I<onfreefn> with
  20. the library context I<ctx>. When I<ctx> is freed all associated "on free"
  21. routines are called.
  22. ossl_lib_ctx_is_child() returns 1 if this library context is a child and 0
  23. otherwise.
  24. ossl_lib_ctx_get_data() allows different parts of the library to retrieve
  25. pointers to structures used in diverse parts of the library. The lifetime of
  26. these structures is managed by B<OSSL_LIB_CTX>. The different objects which can
  27. be retrieved are specified with the given argument I<index>. The valid values of
  28. I<index> are specified in cryptlib.h.
  29. =head1 RETURN VALUES
  30. ossl_lib_ctx_get_data() returns a pointer on success, or NULL on
  31. failure.
  32. =head1 EXAMPLES
  33. =head2 Usage
  34. To obtain a pointer for an object managed by the library context, simply do
  35. this:
  36. /*
  37. * ctx is received from a caller,
  38. */
  39. FOO *data = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_FOO_INDEX);
  40. =head2 Run Once
  41. void foo_cleanup(OSSL_LIB_CTX *ctx)
  42. {
  43. /* Free foo resources associated with ctx */
  44. }
  45. static ossl_lib_ctx_run_once_fn do_foo_init;
  46. static int do_foo_init(OSSL_LIB_CTX *ctx)
  47. {
  48. /* Allocate and initialise some foo resources and associated with ctx */
  49. return ossl_lib_ctx_onfree(ctx, &foo_cleanup)
  50. }
  51. int foo_some_function(OSSL_LIB_CTX *ctx)
  52. {
  53. if (!ossl_lib_ctx_run_once(ctx,
  54. OSSL_LIB_CTX_FOO_RUN_ONCE_INDEX,
  55. do_foo_init))
  56. return 0;
  57. /* Do some work using foo resources in ctx */
  58. }
  59. =head1 SEE ALSO
  60. L<OSSL_LIB_CTX(3)>
  61. =head1 COPYRIGHT
  62. Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
  63. Licensed under the Apache License 2.0 (the "License"). You may not use
  64. this file except in compliance with the License. You can obtain a copy
  65. in the file LICENSE in the source distribution or at
  66. L<https://www.openssl.org/source/license.html>.
  67. =cut