]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
a28d06f3 | 2 | * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. |
71a04cfc | 3 | * |
0e9725bc | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
71a04cfc AG |
8 | */ |
9 | ||
9750b4d3 RB |
10 | /* We need to use the OPENSSL_fork_*() deprecated APIs */ |
11 | #define OPENSSL_SUPPRESS_DEPRECATED | |
12 | ||
71a04cfc | 13 | #include <openssl/crypto.h> |
5f8dd0f8 | 14 | #include "internal/cryptlib.h" |
71a04cfc | 15 | |
d6dda392 VK |
16 | #if defined(__sun) |
17 | # include <atomic.h> | |
18 | #endif | |
19 | ||
71a04cfc AG |
20 | #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS) |
21 | ||
84952925 DMSP |
22 | # if defined(OPENSSL_SYS_UNIX) |
23 | # include <sys/types.h> | |
24 | # include <unistd.h> | |
25 | #endif | |
26 | ||
ec93a292 DK |
27 | # ifdef PTHREAD_RWLOCK_INITIALIZER |
28 | # define USE_RWLOCK | |
29 | # endif | |
2accf3f7 | 30 | |
71a04cfc AG |
31 | CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) |
32 | { | |
ec93a292 | 33 | # ifdef USE_RWLOCK |
7de2b9c4 RS |
34 | CRYPTO_RWLOCK *lock; |
35 | ||
36 | if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) { | |
37 | /* Don't set error, to avoid recursion blowup. */ | |
71a04cfc | 38 | return NULL; |
7de2b9c4 | 39 | } |
71a04cfc | 40 | |
0b2fc928 F |
41 | if (pthread_rwlock_init(lock, NULL) != 0) { |
42 | OPENSSL_free(lock); | |
71a04cfc | 43 | return NULL; |
0b2fc928 | 44 | } |
ec93a292 DK |
45 | # else |
46 | pthread_mutexattr_t attr; | |
7de2b9c4 RS |
47 | CRYPTO_RWLOCK *lock; |
48 | ||
49 | if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) { | |
50 | /* Don't set error, to avoid recursion blowup. */ | |
2accf3f7 | 51 | return NULL; |
7de2b9c4 | 52 | } |
2accf3f7 | 53 | |
e60147fe RS |
54 | /* |
55 | * We don't use recursive mutexes, but try to catch errors if we do. | |
56 | */ | |
2accf3f7 | 57 | pthread_mutexattr_init(&attr); |
e60147fe RS |
58 | # if defined(NDEBUG) && defined(PTHREAD_MUTEX_ERRORCHECK) |
59 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | |
60 | # else | |
61 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); | |
62 | # endif | |
5d5eed44 | 63 | |
2accf3f7 DK |
64 | if (pthread_mutex_init(lock, &attr) != 0) { |
65 | pthread_mutexattr_destroy(&attr); | |
66 | OPENSSL_free(lock); | |
67 | return NULL; | |
68 | } | |
5d5eed44 | 69 | |
2accf3f7 | 70 | pthread_mutexattr_destroy(&attr); |
ec93a292 | 71 | # endif |
71a04cfc AG |
72 | |
73 | return lock; | |
74 | } | |
75 | ||
76 | int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) | |
77 | { | |
ec93a292 | 78 | # ifdef USE_RWLOCK |
71a04cfc AG |
79 | if (pthread_rwlock_rdlock(lock) != 0) |
80 | return 0; | |
ec93a292 | 81 | # else |
e60147fe RS |
82 | if (pthread_mutex_lock(lock) != 0) { |
83 | assert(errno != EDEADLK && errno != EBUSY); | |
2accf3f7 | 84 | return 0; |
e60147fe | 85 | } |
ec93a292 | 86 | # endif |
71a04cfc AG |
87 | |
88 | return 1; | |
89 | } | |
90 | ||
91 | int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) | |
92 | { | |
ec93a292 | 93 | # ifdef USE_RWLOCK |
71a04cfc AG |
94 | if (pthread_rwlock_wrlock(lock) != 0) |
95 | return 0; | |
ec93a292 | 96 | # else |
e60147fe RS |
97 | if (pthread_mutex_lock(lock) != 0) { |
98 | assert(errno != EDEADLK && errno != EBUSY); | |
2accf3f7 | 99 | return 0; |
e60147fe | 100 | } |
ec93a292 | 101 | # endif |
71a04cfc AG |
102 | |
103 | return 1; | |
104 | } | |
105 | ||
106 | int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) | |
107 | { | |
ec93a292 | 108 | # ifdef USE_RWLOCK |
71a04cfc AG |
109 | if (pthread_rwlock_unlock(lock) != 0) |
110 | return 0; | |
ec93a292 | 111 | # else |
e60147fe RS |
112 | if (pthread_mutex_unlock(lock) != 0) { |
113 | assert(errno != EPERM); | |
2accf3f7 | 114 | return 0; |
e60147fe | 115 | } |
ec93a292 | 116 | # endif |
71a04cfc AG |
117 | |
118 | return 1; | |
119 | } | |
120 | ||
121 | void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) | |
122 | { | |
123 | if (lock == NULL) | |
124 | return; | |
125 | ||
ec93a292 | 126 | # ifdef USE_RWLOCK |
71a04cfc | 127 | pthread_rwlock_destroy(lock); |
ec93a292 | 128 | # else |
2accf3f7 | 129 | pthread_mutex_destroy(lock); |
ec93a292 | 130 | # endif |
71a04cfc AG |
131 | OPENSSL_free(lock); |
132 | ||
133 | return; | |
134 | } | |
135 | ||
136 | int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void)) | |
137 | { | |
138 | if (pthread_once(once, init) != 0) | |
139 | return 0; | |
140 | ||
141 | return 1; | |
142 | } | |
143 | ||
144 | int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) | |
145 | { | |
146 | if (pthread_key_create(key, cleanup) != 0) | |
147 | return 0; | |
148 | ||
149 | return 1; | |
150 | } | |
151 | ||
152 | void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key) | |
153 | { | |
154 | return pthread_getspecific(*key); | |
155 | } | |
156 | ||
157 | int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val) | |
158 | { | |
159 | if (pthread_setspecific(*key, val) != 0) | |
160 | return 0; | |
161 | ||
162 | return 1; | |
163 | } | |
164 | ||
165 | int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key) | |
166 | { | |
167 | if (pthread_key_delete(*key) != 0) | |
168 | return 0; | |
169 | ||
170 | return 1; | |
171 | } | |
172 | ||
173 | CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void) | |
174 | { | |
175 | return pthread_self(); | |
176 | } | |
177 | ||
178 | int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b) | |
179 | { | |
180 | return pthread_equal(a, b); | |
181 | } | |
182 | ||
183 | int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) | |
184 | { | |
11fc6c76 | 185 | # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) |
1beca676 RL |
186 | if (__atomic_is_lock_free(sizeof(*val), val)) { |
187 | *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL); | |
188 | return 1; | |
189 | } | |
d6dda392 VK |
190 | # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11)) |
191 | /* This will work for all future Solaris versions. */ | |
192 | if (ret != NULL) { | |
193 | *ret = atomic_add_int_nv((volatile unsigned int *)val, amount); | |
194 | return 1; | |
195 | } | |
1beca676 | 196 | # endif |
d5e742de | 197 | if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) |
71a04cfc AG |
198 | return 0; |
199 | ||
200 | *val += amount; | |
201 | *ret = *val; | |
202 | ||
203 | if (!CRYPTO_THREAD_unlock(lock)) | |
204 | return 0; | |
71a04cfc AG |
205 | |
206 | return 1; | |
207 | } | |
208 | ||
d5e742de MC |
209 | int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret, |
210 | CRYPTO_RWLOCK *lock) | |
211 | { | |
212 | # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) | |
213 | if (__atomic_is_lock_free(sizeof(*val), val)) { | |
214 | *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL); | |
215 | return 1; | |
216 | } | |
217 | # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11)) | |
218 | /* This will work for all future Solaris versions. */ | |
219 | if (ret != NULL) { | |
220 | *ret = atomic_or_64_nv(val, op); | |
221 | return 1; | |
222 | } | |
223 | # endif | |
224 | if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) | |
225 | return 0; | |
226 | *val |= op; | |
227 | *ret = *val; | |
228 | ||
229 | if (!CRYPTO_THREAD_unlock(lock)) | |
230 | return 0; | |
231 | ||
232 | return 1; | |
233 | } | |
234 | ||
235 | int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock) | |
236 | { | |
237 | # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) | |
238 | if (__atomic_is_lock_free(sizeof(*val), val)) { | |
239 | __atomic_load(val, ret, __ATOMIC_ACQUIRE); | |
240 | return 1; | |
241 | } | |
242 | # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11)) | |
243 | /* This will work for all future Solaris versions. */ | |
244 | if (ret != NULL) { | |
245 | *ret = atomic_or_64_nv(val, 0); | |
246 | return 1; | |
247 | } | |
248 | # endif | |
249 | if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) | |
250 | return 0; | |
251 | *ret = *val; | |
252 | if (!CRYPTO_THREAD_unlock(lock)) | |
253 | return 0; | |
254 | ||
255 | return 1; | |
256 | } | |
f844f9eb | 257 | # ifndef FIPS_MODULE |
3593266d | 258 | # ifdef OPENSSL_SYS_UNIX |
9750b4d3 | 259 | |
b842fcbb RS |
260 | static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT; |
261 | ||
262 | static void fork_once_func(void) | |
263 | { | |
9750b4d3 | 264 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
b842fcbb RS |
265 | pthread_atfork(OPENSSL_fork_prepare, |
266 | OPENSSL_fork_parent, OPENSSL_fork_child); | |
9750b4d3 | 267 | # endif |
b842fcbb | 268 | } |
3593266d | 269 | # endif |
b842fcbb | 270 | |
2915fe19 RS |
271 | int openssl_init_fork_handlers(void) |
272 | { | |
3593266d | 273 | # ifdef OPENSSL_SYS_UNIX |
b842fcbb | 274 | if (pthread_once(&fork_once_control, fork_once_func) == 0) |
2915fe19 | 275 | return 1; |
3593266d | 276 | # endif |
2915fe19 RS |
277 | return 0; |
278 | } | |
f844f9eb | 279 | # endif /* FIPS_MODULE */ |
84952925 DMSP |
280 | |
281 | int openssl_get_fork_id(void) | |
282 | { | |
283 | return getpid(); | |
284 | } | |
71a04cfc | 285 | #endif |