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