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