1 /*
2  * Copyright 2016-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 #include "internal/cryptlib.h"
12 
13 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
14 
15 # if defined(OPENSSL_SYS_UNIX)
16 #  include <sys/types.h>
17 #  include <unistd.h>
18 #endif
19 
20 # ifdef PTHREAD_RWLOCK_INITIALIZER
21 #  define USE_RWLOCK
22 # endif
23 
24 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
25 {
26 # ifdef USE_RWLOCK
27     CRYPTO_RWLOCK *lock;
28 
29     if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
30         /* Don't set error, to avoid recursion blowup. */
31         return NULL;
32     }
33 
34     if (pthread_rwlock_init(lock, NULL) != 0) {
35         OPENSSL_free(lock);
36         return NULL;
37     }
38 # else
39     pthread_mutexattr_t attr;
40     CRYPTO_RWLOCK *lock;
41 
42     if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
43         /* Don't set error, to avoid recursion blowup. */
44         return NULL;
45     }
46 
47     pthread_mutexattr_init(&attr);
48     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
49 
50     if (pthread_mutex_init(lock, &attr) != 0) {
51         pthread_mutexattr_destroy(&attr);
52         OPENSSL_free(lock);
53         return NULL;
54     }
55 
56     pthread_mutexattr_destroy(&attr);
57 # endif
58 
59     return lock;
60 }
61 
62 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
63 {
64 # ifdef USE_RWLOCK
65     if (pthread_rwlock_rdlock(lock) != 0)
66         return 0;
67 # else
68     if (pthread_mutex_lock(lock) != 0)
69         return 0;
70 # endif
71 
72     return 1;
73 }
74 
75 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
76 {
77 # ifdef USE_RWLOCK
78     if (pthread_rwlock_wrlock(lock) != 0)
79         return 0;
80 # else
81     if (pthread_mutex_lock(lock) != 0)
82         return 0;
83 # endif
84 
85     return 1;
86 }
87 
88 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
89 {
90 # ifdef USE_RWLOCK
91     if (pthread_rwlock_unlock(lock) != 0)
92         return 0;
93 # else
94     if (pthread_mutex_unlock(lock) != 0)
95         return 0;
96 # endif
97 
98     return 1;
99 }
100 
101 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
102 {
103     if (lock == NULL)
104         return;
105 
106 # ifdef USE_RWLOCK
107     pthread_rwlock_destroy(lock);
108 # else
109     pthread_mutex_destroy(lock);
110 # endif
111     OPENSSL_free(lock);
112 
113     return;
114 }
115 
116 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
117 {
118     if (pthread_once(once, init) != 0)
119         return 0;
120 
121     return 1;
122 }
123 
124 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
125 {
126     if (pthread_key_create(key, cleanup) != 0)
127         return 0;
128 
129     return 1;
130 }
131 
132 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
133 {
134     return pthread_getspecific(*key);
135 }
136 
137 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
138 {
139     if (pthread_setspecific(*key, val) != 0)
140         return 0;
141 
142     return 1;
143 }
144 
145 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
146 {
147     if (pthread_key_delete(*key) != 0)
148         return 0;
149 
150     return 1;
151 }
152 
153 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
154 {
155     return pthread_self();
156 }
157 
158 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
159 {
160     return pthread_equal(a, b);
161 }
162 
163 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
164 {
165 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && \
166      !(defined(__arm__) && __ARM_ARCH < 6)
167     if (__atomic_is_lock_free(sizeof(*val), val)) {
168         *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
169         return 1;
170     }
171 # endif
172     if (!CRYPTO_THREAD_write_lock(lock))
173         return 0;
174 
175     *val += amount;
176     *ret  = *val;
177 
178     if (!CRYPTO_THREAD_unlock(lock))
179         return 0;
180 
181     return 1;
182 }
183 
184 # ifdef OPENSSL_SYS_UNIX
185 static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
186 
187 static void fork_once_func(void)
188 {
189     pthread_atfork(OPENSSL_fork_prepare,
190                    OPENSSL_fork_parent, OPENSSL_fork_child);
191 }
192 # endif
193 
194 int openssl_init_fork_handlers(void)
195 {
196 # ifdef OPENSSL_SYS_UNIX
197     if (pthread_once(&fork_once_control, fork_once_func) == 0)
198         return 1;
199 # endif
200     return 0;
201 }
202 
203 int openssl_get_fork_id(void)
204 {
205     return getpid();
206 }
207 #endif
208