]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/threads_pthread.c
Don't auto-instantiate a DRBG when trying to use it and it's not
[thirdparty/openssl.git] / crypto / threads_pthread.c
CommitLineData
b1322259
RS
1/*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
71a04cfc 3 *
b1322259
RS
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
71a04cfc
AG
8 */
9
10#include <openssl/crypto.h>
5f8dd0f8 11#include "internal/cryptlib.h"
71a04cfc
AG
12
13#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
14
ec93a292
DK
15# ifdef PTHREAD_RWLOCK_INITIALIZER
16# define USE_RWLOCK
17# endif
2accf3f7 18
71a04cfc
AG
19CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
20{
ec93a292 21# ifdef USE_RWLOCK
71a04cfc
AG
22 CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t));
23 if (lock == NULL)
24 return NULL;
25
0b2fc928
F
26 if (pthread_rwlock_init(lock, NULL) != 0) {
27 OPENSSL_free(lock);
71a04cfc 28 return NULL;
0b2fc928 29 }
ec93a292
DK
30# else
31 pthread_mutexattr_t attr;
2accf3f7
DK
32 CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_mutex_t));
33 if (lock == NULL)
34 return NULL;
35
2accf3f7
DK
36 pthread_mutexattr_init(&attr);
37 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
5d5eed44 38
2accf3f7
DK
39 if (pthread_mutex_init(lock, &attr) != 0) {
40 pthread_mutexattr_destroy(&attr);
41 OPENSSL_free(lock);
42 return NULL;
43 }
5d5eed44 44
2accf3f7 45 pthread_mutexattr_destroy(&attr);
ec93a292 46# endif
71a04cfc
AG
47
48 return lock;
49}
50
51int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
52{
ec93a292 53# ifdef USE_RWLOCK
71a04cfc
AG
54 if (pthread_rwlock_rdlock(lock) != 0)
55 return 0;
ec93a292 56# else
2accf3f7
DK
57 if (pthread_mutex_lock(lock) != 0)
58 return 0;
ec93a292 59# endif
71a04cfc
AG
60
61 return 1;
62}
63
64int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
65{
ec93a292 66# ifdef USE_RWLOCK
71a04cfc
AG
67 if (pthread_rwlock_wrlock(lock) != 0)
68 return 0;
ec93a292 69# else
2accf3f7
DK
70 if (pthread_mutex_lock(lock) != 0)
71 return 0;
ec93a292 72# endif
71a04cfc
AG
73
74 return 1;
75}
76
77int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
78{
ec93a292 79# ifdef USE_RWLOCK
71a04cfc
AG
80 if (pthread_rwlock_unlock(lock) != 0)
81 return 0;
ec93a292 82# else
2accf3f7
DK
83 if (pthread_mutex_unlock(lock) != 0)
84 return 0;
ec93a292 85# endif
71a04cfc
AG
86
87 return 1;
88}
89
90void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
91{
92 if (lock == NULL)
93 return;
94
ec93a292 95# ifdef USE_RWLOCK
71a04cfc 96 pthread_rwlock_destroy(lock);
ec93a292 97# else
2accf3f7 98 pthread_mutex_destroy(lock);
ec93a292 99# endif
71a04cfc
AG
100 OPENSSL_free(lock);
101
102 return;
103}
104
105int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
106{
107 if (pthread_once(once, init) != 0)
108 return 0;
109
110 return 1;
111}
112
113int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
114{
115 if (pthread_key_create(key, cleanup) != 0)
116 return 0;
117
118 return 1;
119}
120
121void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
122{
123 return pthread_getspecific(*key);
124}
125
126int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
127{
128 if (pthread_setspecific(*key, val) != 0)
129 return 0;
130
131 return 1;
132}
133
134int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
135{
136 if (pthread_key_delete(*key) != 0)
137 return 0;
138
139 return 1;
140}
141
142CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
143{
144 return pthread_self();
145}
146
147int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
148{
149 return pthread_equal(a, b);
150}
151
152int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
153{
11fc6c76 154# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
1beca676
RL
155 if (__atomic_is_lock_free(sizeof(*val), val)) {
156 *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
157 return 1;
158 }
159# endif
71a04cfc
AG
160 if (!CRYPTO_THREAD_write_lock(lock))
161 return 0;
162
163 *val += amount;
164 *ret = *val;
165
166 if (!CRYPTO_THREAD_unlock(lock))
167 return 0;
71a04cfc
AG
168
169 return 1;
170}
171
b842fcbb
RS
172# ifdef OPENSSL_SYS_UNIX
173static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
174
175static void fork_once_func(void)
176{
177 pthread_atfork(OPENSSL_fork_prepare,
178 OPENSSL_fork_parent, OPENSSL_fork_child);
179}
180# endif
181
2915fe19
RS
182int openssl_init_fork_handlers(void)
183{
184# ifdef OPENSSL_SYS_UNIX
b842fcbb 185 if (pthread_once(&fork_once_control, fork_once_func) == 0)
2915fe19
RS
186 return 1;
187# endif
188 return 0;
189}
71a04cfc 190#endif