1 | /*
|
---|
2 | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (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 | #include <openssl/crypto.h>
|
---|
11 |
|
---|
12 | #ifndef VBOX
|
---|
13 |
|
---|
14 | /*
|
---|
15 | * DEFINE_RUN_ONCE: Define an initialiser function that should be run exactly
|
---|
16 | * once. It takes no arguments and returns and int result (1 for success or
|
---|
17 | * 0 for failure). Typical usage might be:
|
---|
18 | *
|
---|
19 | * DEFINE_RUN_ONCE(myinitfunc)
|
---|
20 | * {
|
---|
21 | * do_some_initialisation();
|
---|
22 | * if (init_is_successful())
|
---|
23 | * return 1;
|
---|
24 | *
|
---|
25 | * return 0;
|
---|
26 | * }
|
---|
27 | */
|
---|
28 | #define DEFINE_RUN_ONCE(init) \
|
---|
29 | static int init(void); \
|
---|
30 | int init##_ossl_ret_ = 0; \
|
---|
31 | void init##_ossl_(void) \
|
---|
32 | { \
|
---|
33 | init##_ossl_ret_ = init(); \
|
---|
34 | } \
|
---|
35 | static int init(void)
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * DECLARE_RUN_ONCE: Declare an initialiser function that should be run exactly
|
---|
39 | * once that has been defined in another file via DEFINE_RUN_ONCE().
|
---|
40 | */
|
---|
41 | #define DECLARE_RUN_ONCE(init) \
|
---|
42 | extern int init##_ossl_ret_; \
|
---|
43 | void init##_ossl_(void);
|
---|
44 |
|
---|
45 | /*
|
---|
46 | * DEFINE_RUN_ONCE_STATIC: Define an initialiser function that should be run
|
---|
47 | * exactly once. This function will be declared as static within the file. It
|
---|
48 | * takes no arguments and returns and int result (1 for success or 0 for
|
---|
49 | * failure). Typical usage might be:
|
---|
50 | *
|
---|
51 | * DEFINE_RUN_ONCE_STATIC(myinitfunc)
|
---|
52 | * {
|
---|
53 | * do_some_initialisation();
|
---|
54 | * if (init_is_successful())
|
---|
55 | * return 1;
|
---|
56 | *
|
---|
57 | * return 0;
|
---|
58 | * }
|
---|
59 | */
|
---|
60 | #define DEFINE_RUN_ONCE_STATIC(init) \
|
---|
61 | static int init(void); \
|
---|
62 | static int init##_ossl_ret_ = 0; \
|
---|
63 | static void init##_ossl_(void) \
|
---|
64 | { \
|
---|
65 | init##_ossl_ret_ = init(); \
|
---|
66 | } \
|
---|
67 | static int init(void)
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * DEFINE_RUN_ONCE_STATIC_ALT: Define an alternative initialiser function. This
|
---|
71 | * function will be declared as static within the file. It takes no arguments
|
---|
72 | * and returns an int result (1 for success or 0 for failure). An alternative
|
---|
73 | * initialiser function is expected to be associated with a primary initialiser
|
---|
74 | * function defined via DEFINE_ONCE_STATIC where both functions use the same
|
---|
75 | * CRYPTO_ONCE object to synchronise. Where an alternative initialiser function
|
---|
76 | * is used only one of the primary or the alternative initialiser function will
|
---|
77 | * ever be called - and that function will be called exactly once. Definition
|
---|
78 | * of an alternative initialiser function MUST occur AFTER the definition of the
|
---|
79 | * primary initialiser function.
|
---|
80 | *
|
---|
81 | * Typical usage might be:
|
---|
82 | *
|
---|
83 | * DEFINE_RUN_ONCE_STATIC(myinitfunc)
|
---|
84 | * {
|
---|
85 | * do_some_initialisation();
|
---|
86 | * if (init_is_successful())
|
---|
87 | * return 1;
|
---|
88 | *
|
---|
89 | * return 0;
|
---|
90 | * }
|
---|
91 | *
|
---|
92 | * DEFINE_RUN_ONCE_STATIC_ALT(myaltinitfunc, myinitfunc)
|
---|
93 | * {
|
---|
94 | * do_some_alternative_initialisation();
|
---|
95 | * if (init_is_successful())
|
---|
96 | * return 1;
|
---|
97 | *
|
---|
98 | * return 0;
|
---|
99 | * }
|
---|
100 | */
|
---|
101 | #define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
|
---|
102 | static int initalt(void); \
|
---|
103 | static void initalt##_ossl_(void) \
|
---|
104 | { \
|
---|
105 | init##_ossl_ret_ = initalt(); \
|
---|
106 | } \
|
---|
107 | static int initalt(void)
|
---|
108 |
|
---|
109 | #else /* VBOX */
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * PFNRTONCE returns an IPRT status code but the OpenSSL once functions
|
---|
113 | * return 1 for success and 0 for failure. We need to translate between
|
---|
114 | * these errors back (here) and force (in RUN_ONCE()).
|
---|
115 | */
|
---|
116 | #undef DEFINE_RUN_ONCE /* currently unused */
|
---|
117 | #undef DECLARE_RUN_ONCE /* currently unused */
|
---|
118 | #undef DEFINE_RUN_ONCE_STATIC_ALT /* currently unused */
|
---|
119 |
|
---|
120 | #define DEFINE_RUN_ONCE_STATIC(init) \
|
---|
121 | static int init(void); \
|
---|
122 | static int init##_ossl_ret_ = 0; \
|
---|
123 | static DECLCALLBACK(int) init##_ossl_(void *pvUser) \
|
---|
124 | { \
|
---|
125 | init##_ossl_ret_ = init(); \
|
---|
126 | return init##_ossl_ret_ ? VINF_SUCCESS : VERR_INTERNAL_ERROR; \
|
---|
127 | } \
|
---|
128 | static int init(void)
|
---|
129 |
|
---|
130 | #define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
|
---|
131 | static int initalt(void); \
|
---|
132 | static DECLCALLBACK(int) initalt##_ossl_(void *pvUser) \
|
---|
133 | { \
|
---|
134 | init##_ossl_ret_ = initalt(); \
|
---|
135 | return init##_ossl_ret_ ? VINF_SUCCESS : VERR_INTERNAL_ERROR; \
|
---|
136 | } \
|
---|
137 | static int initalt(void)
|
---|
138 |
|
---|
139 | #endif /* VBOX */
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * RUN_ONCE - use CRYPTO_THREAD_run_once, and check if the init succeeded
|
---|
143 | * @once: pointer to static object of type CRYPTO_ONCE
|
---|
144 | * @init: function name that was previously given to DEFINE_RUN_ONCE,
|
---|
145 | * DEFINE_RUN_ONCE_STATIC or DECLARE_RUN_ONCE. This function
|
---|
146 | * must return 1 for success or 0 for failure.
|
---|
147 | *
|
---|
148 | * The return value is 1 on success (*) or 0 in case of error.
|
---|
149 | *
|
---|
150 | * (*) by convention, since the init function must return 1 on success.
|
---|
151 | */
|
---|
152 | #ifndef VBOX
|
---|
153 | #define RUN_ONCE(once, init) \
|
---|
154 | (CRYPTO_THREAD_run_once(once, init##_ossl_) ? init##_ossl_ret_ : 0)
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * RUN_ONCE_ALT - use CRYPTO_THREAD_run_once, to run an alternative initialiser
|
---|
158 | * function and check if that initialisation succeeded
|
---|
159 | * @once: pointer to static object of type CRYPTO_ONCE
|
---|
160 | * @initalt: alternative initialiser function name that was previously given to
|
---|
161 | * DEFINE_RUN_ONCE_STATIC_ALT. This function must return 1 for
|
---|
162 | * success or 0 for failure.
|
---|
163 | * @init: primary initialiser function name that was previously given to
|
---|
164 | * DEFINE_RUN_ONCE_STATIC. This function must return 1 for success or
|
---|
165 | * 0 for failure.
|
---|
166 | *
|
---|
167 | * The return value is 1 on success (*) or 0 in case of error.
|
---|
168 | *
|
---|
169 | * (*) by convention, since the init function must return 1 on success.
|
---|
170 | */
|
---|
171 | #define RUN_ONCE_ALT(once, initalt, init) \
|
---|
172 | (CRYPTO_THREAD_run_once(once, initalt##_ossl_) ? init##_ossl_ret_ : 0)
|
---|
173 | #else
|
---|
174 | #define RUN_ONCE(once, init) \
|
---|
175 | (RT_SUCCESS_NP(RTOnce(once, init##_ossl_, NULL)) ? init##_ossl_ret_ : 0)
|
---|
176 |
|
---|
177 | #define RUN_ONCE_ALT(once, initalt, init) \
|
---|
178 | (RT_SUCCESS_NP(RTOnce(once, initalt##_ossl_, NULL)) ? init##_ossl_ret_ : 0)
|
---|
179 | #endif
|
---|