1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include "namespace.h"
36#include <sys/mman.h>
37#include <signal.h>
38#include <stdlib.h>
39#include <string.h>
40#include <errno.h>
41#include <pthread.h>
42#include "un-namespace.h"
43#include "libc_private.h"
44
45#include "thr_private.h"
46
47/* Used in symbol lookup of libthread_db */
48struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
49
50__weak_reference(_thr_key_create, pthread_key_create);
51__weak_reference(_thr_key_create, _pthread_key_create);
52__weak_reference(_thr_key_delete, pthread_key_delete);
53__weak_reference(_thr_key_delete, _pthread_key_delete);
54__weak_reference(_thr_getspecific, pthread_getspecific);
55__weak_reference(_thr_getspecific, _pthread_getspecific);
56__weak_reference(_thr_setspecific, pthread_setspecific);
57__weak_reference(_thr_setspecific, _pthread_setspecific);
58
59int
60_thr_key_create(pthread_key_t *key, void (*destructor)(void *))
61{
62	struct pthread *curthread;
63	int i;
64
65	_thr_check_init();
66
67	curthread = _get_curthread();
68
69	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
70	for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
71
72		if (_thread_keytable[i].allocated == 0) {
73			_thread_keytable[i].allocated = 1;
74			_thread_keytable[i].destructor = destructor;
75			_thread_keytable[i].seqno++;
76
77			THR_LOCK_RELEASE(curthread, &_keytable_lock);
78			*key = i + 1;
79			return (0);
80		}
81
82	}
83	THR_LOCK_RELEASE(curthread, &_keytable_lock);
84	return (EAGAIN);
85}
86
87int
88_thr_key_delete(pthread_key_t userkey)
89{
90	struct pthread *curthread;
91	int key, ret;
92
93	key = userkey - 1;
94	if ((unsigned int)key >= PTHREAD_KEYS_MAX)
95		return (EINVAL);
96	curthread = _get_curthread();
97	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
98	if (_thread_keytable[key].allocated) {
99		_thread_keytable[key].allocated = 0;
100		ret = 0;
101	} else {
102		ret = EINVAL;
103	}
104	THR_LOCK_RELEASE(curthread, &_keytable_lock);
105	return (ret);
106}
107
108void
109_thread_cleanupspecific(void)
110{
111	struct pthread *curthread;
112	void (*destructor)(void *);
113	const void *data;
114	int i, key;
115
116	curthread = _get_curthread();
117	if (curthread->specific == NULL)
118		return;
119	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
120	for (i = 0; i < PTHREAD_DESTRUCTOR_ITERATIONS &&
121	    curthread->specific_data_count > 0; i++) {
122		for (key = 0; key < PTHREAD_KEYS_MAX &&
123		    curthread->specific_data_count > 0; key++) {
124			destructor = NULL;
125
126			if (_thread_keytable[key].allocated &&
127			    (curthread->specific[key].data != NULL)) {
128				if (curthread->specific[key].seqno ==
129				    _thread_keytable[key].seqno) {
130					data = curthread->specific[key].data;
131					destructor = _thread_keytable[key].
132					    destructor;
133				}
134				curthread->specific[key].data = NULL;
135				curthread->specific_data_count--;
136			} else if (curthread->specific[key].data != NULL) {
137				/*
138				 * This can happen if the key is
139				 * deleted via pthread_key_delete
140				 * without first setting the value to
141				 * NULL in all threads.  POSIX says
142				 * that the destructor is not invoked
143				 * in this case.
144				 */
145				curthread->specific[key].data = NULL;
146				curthread->specific_data_count--;
147			}
148
149			/*
150			 * If there is a destructor, call it with the
151			 * key table entry unlocked.
152			 */
153			if (destructor != NULL) {
154				THR_LOCK_RELEASE(curthread, &_keytable_lock);
155				destructor(__DECONST(void *, data));
156				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
157			}
158		}
159	}
160	THR_LOCK_RELEASE(curthread, &_keytable_lock);
161	__thr_free(curthread->specific);
162	curthread->specific = NULL;
163	if (curthread->specific_data_count > 0) {
164		stderr_debug("Thread %p has exited with leftover "
165		    "thread-specific data after %d destructor iterations\n",
166		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
167	}
168}
169
170int
171_thr_setspecific(pthread_key_t userkey, const void *value)
172{
173	struct pthread *pthread;
174	void *tmp;
175	pthread_key_t key;
176
177	key = userkey - 1;
178	if ((unsigned int)key >= PTHREAD_KEYS_MAX ||
179	    !_thread_keytable[key].allocated)
180		return (EINVAL);
181
182	pthread = _get_curthread();
183	if (pthread->specific == NULL) {
184		tmp = __thr_calloc(PTHREAD_KEYS_MAX,
185		    sizeof(struct pthread_specific_elem));
186		if (tmp == NULL)
187			return (ENOMEM);
188		pthread->specific = tmp;
189	}
190	if (pthread->specific[key].data == NULL) {
191		if (value != NULL)
192			pthread->specific_data_count++;
193	} else if (value == NULL)
194		pthread->specific_data_count--;
195	pthread->specific[key].data = value;
196	pthread->specific[key].seqno = _thread_keytable[key].seqno;
197	return (0);
198}
199
200void *
201_thr_getspecific(pthread_key_t userkey)
202{
203	struct pthread *pthread;
204	const void *data;
205	pthread_key_t key;
206
207	/* Check if there is specific data. */
208	key = userkey - 1;
209	if ((unsigned int)key >= PTHREAD_KEYS_MAX)
210		return (NULL);
211
212	pthread = _get_curthread();
213	/* Check if this key has been used before. */
214	if (_thread_keytable[key].allocated && pthread->specific != NULL &&
215	    pthread->specific[key].seqno == _thread_keytable[key].seqno) {
216		/* Return the value: */
217		data = pthread->specific[key].data;
218	} else {
219		/*
220		 * This key has not been used before, so return NULL
221		 * instead.
222		 */
223		data = NULL;
224	}
225	return (__DECONST(void *, data));
226}
227
228void
229_thr_tsd_unload(struct dl_phdr_info *phdr_info)
230{
231	struct pthread *curthread;
232	void (*destructor)(void *);
233	int key;
234
235	curthread = _get_curthread();
236	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
237	for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
238		if (!_thread_keytable[key].allocated)
239			continue;
240		destructor = _thread_keytable[key].destructor;
241		if (destructor == NULL)
242			continue;
243		if (__elf_phdr_match_addr(phdr_info, destructor))
244			_thread_keytable[key].destructor = NULL;
245	}
246	THR_LOCK_RELEASE(curthread, &_keytable_lock);
247}
248