| 1 |
/* |
| 2 |
* stunnel TLS offloading and load-balancing proxy |
| 3 |
* Copyright (C) 1998-2026 Michal Trojnara <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the |
| 7 |
* Free Software Foundation; either version 2 of the License, or (at your |
| 8 |
* option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, see <http://www.gnu.org/licenses>. |
| 17 |
* |
| 18 |
* Linking stunnel statically or dynamically with other modules is making |
| 19 |
* a combined work based on stunnel. Thus, the terms and conditions of |
| 20 |
* the GNU General Public License cover the whole combination. |
| 21 |
* |
| 22 |
* In addition, as a special exception, the copyright holder of stunnel |
| 23 |
* gives you permission to combine stunnel with free software programs or |
| 24 |
* libraries that are released under the GNU LGPL and with code included |
| 25 |
* in the standard release of OpenSSL under the OpenSSL License (or |
| 26 |
* modified versions of such code, with unchanged license). You may copy |
| 27 |
* and distribute such a system following the terms of the GNU GPL for |
| 28 |
* stunnel and the licenses of the other code concerned. |
| 29 |
* |
| 30 |
* Note that people who make modified versions of stunnel are not obligated |
| 31 |
* to grant this special exception for their modified versions; it is their |
| 32 |
* choice whether to do so. The GNU General Public License gives permission |
| 33 |
* to release a modified version without this exception; this exception |
| 34 |
* also makes it possible to release a modified version which carries |
| 35 |
* forward this exception. |
| 36 |
*/ |
| 37 |
|
| 38 |
#include "prototypes.h" |
| 39 |
|
| 40 |
volatile int tls_initialized=0; |
| 41 |
|
| 42 |
NOEXPORT void tls_platform_init(void); |
| 43 |
|
| 44 |
/**************************************** thread local storage */ |
| 45 |
|
| 46 |
void tls_init(void) { |
| 47 |
tls_platform_init(); |
| 48 |
tls_initialized=1; |
| 49 |
ui_tls=tls_alloc(NULL, NULL, "ui"); |
| 50 |
} |
| 51 |
|
| 52 |
/* this has to be the first function called by a new thread */ |
| 53 |
TLS_DATA *tls_alloc(CLI *c, TLS_DATA *inherited, const char *txt) { |
| 54 |
TLS_DATA *tls_data; |
| 55 |
|
| 56 |
if(inherited) { /* reuse the thread-local storage after fork() */ |
| 57 |
tls_data=inherited; |
| 58 |
str_free_const(tls_data->id); |
| 59 |
} else { |
| 60 |
tls_data=calloc(1, sizeof(TLS_DATA)); |
| 61 |
if(!tls_data) |
| 62 |
fatal("Out of memory"); |
| 63 |
if(c) |
| 64 |
c->tls=tls_data; |
| 65 |
str_thread_init(tls_data); |
| 66 |
tls_data->c=c; |
| 67 |
tls_data->opt=c?c->opt:&service_options; |
| 68 |
} |
| 69 |
tls_data->id="unconfigured"; |
| 70 |
tls_set(tls_data); |
| 71 |
|
| 72 |
/* str.c functions can be used below this point */ |
| 73 |
if(txt) { |
| 74 |
tls_data->id=str_dup(txt); |
| 75 |
str_detach_const(tls_data->id); /* it is deallocated after str_stats() */ |
| 76 |
} else if(c) { |
| 77 |
tls_data->id=log_id(c); |
| 78 |
str_detach_const(tls_data->id); /* it is deallocated after str_stats() */ |
| 79 |
} |
| 80 |
|
| 81 |
return tls_data; |
| 82 |
} |
| 83 |
|
| 84 |
/* per-thread thread-local storage cleanup */ |
| 85 |
void tls_cleanup(void) { |
| 86 |
TLS_DATA *tls_data; |
| 87 |
|
| 88 |
tls_data=tls_get(); |
| 89 |
if(!tls_data) |
| 90 |
return; |
| 91 |
str_thread_cleanup(tls_data); |
| 92 |
str_free_const(tls_data->id); /* detached allocation */ |
| 93 |
tls_set(NULL); |
| 94 |
free(tls_data); |
| 95 |
} |
| 96 |
|
| 97 |
#ifdef USE_UCONTEXT |
| 98 |
|
| 99 |
static TLS_DATA *global_tls=NULL; |
| 100 |
|
| 101 |
NOEXPORT void tls_platform_init(void) { |
| 102 |
} |
| 103 |
|
| 104 |
void tls_set(TLS_DATA *tls_data) { |
| 105 |
if(ready_head) |
| 106 |
ready_head->tls=tls_data; |
| 107 |
else /* ucontext threads not initialized */ |
| 108 |
global_tls=tls_data; |
| 109 |
} |
| 110 |
|
| 111 |
TLS_DATA *tls_get(void) { |
| 112 |
if(ready_head) |
| 113 |
return ready_head->tls; |
| 114 |
else /* ucontext threads not initialized */ |
| 115 |
return global_tls; |
| 116 |
} |
| 117 |
|
| 118 |
#endif /* USE_UCONTEXT */ |
| 119 |
|
| 120 |
#ifdef USE_FORK |
| 121 |
|
| 122 |
static TLS_DATA *global_tls=NULL; |
| 123 |
|
| 124 |
NOEXPORT void tls_platform_init(void) { |
| 125 |
} |
| 126 |
|
| 127 |
void tls_set(TLS_DATA *tls_data) { |
| 128 |
global_tls=tls_data; |
| 129 |
} |
| 130 |
|
| 131 |
TLS_DATA *tls_get(void) { |
| 132 |
return global_tls; |
| 133 |
} |
| 134 |
|
| 135 |
#endif /* USE_FORK */ |
| 136 |
|
| 137 |
#ifdef USE_PTHREAD |
| 138 |
|
| 139 |
static pthread_key_t pthread_key; |
| 140 |
|
| 141 |
NOEXPORT void tls_platform_init(void) { |
| 142 |
pthread_key_create(&pthread_key, NULL); |
| 143 |
} |
| 144 |
|
| 145 |
void tls_set(TLS_DATA *tls_data) { |
| 146 |
pthread_setspecific(pthread_key, tls_data); |
| 147 |
} |
| 148 |
|
| 149 |
TLS_DATA *tls_get(void) { |
| 150 |
return pthread_getspecific(pthread_key); |
| 151 |
} |
| 152 |
|
| 153 |
#endif /* USE_PTHREAD */ |
| 154 |
|
| 155 |
#ifdef USE_WIN32 |
| 156 |
|
| 157 |
static DWORD tls_index; |
| 158 |
|
| 159 |
NOEXPORT void tls_platform_init(void) { |
| 160 |
tls_index=TlsAlloc(); |
| 161 |
} |
| 162 |
|
| 163 |
void tls_set(TLS_DATA *tls_data) { |
| 164 |
TlsSetValue(tls_index, tls_data); |
| 165 |
} |
| 166 |
|
| 167 |
TLS_DATA *tls_get(void) { |
| 168 |
return TlsGetValue(tls_index); |
| 169 |
} |
| 170 |
|
| 171 |
#endif /* USE_WIN32 */ |
| 172 |
|
| 173 |
/* end of tls.c */ |