1fe38b55cSguenther /*
2fe38b55cSguenther * Copyright (c) 2014 Philip Guenther <guenther@openbsd.org>
3fe38b55cSguenther *
4fe38b55cSguenther * Permission to use, copy, modify, and distribute this software for any
5fe38b55cSguenther * purpose with or without fee is hereby granted, provided that the above
6fe38b55cSguenther * copyright notice and this permission notice appear in all copies.
7fe38b55cSguenther *
8fe38b55cSguenther * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9fe38b55cSguenther * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10fe38b55cSguenther * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11fe38b55cSguenther * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12fe38b55cSguenther * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13fe38b55cSguenther * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14fe38b55cSguenther * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15fe38b55cSguenther */
16fe38b55cSguenther
17*a5511fa9Sguenther #include <pthread.h>
18fe38b55cSguenther #include <signal.h>
195be66c01Sguenther #include <stdio.h>
20fe38b55cSguenther #include <string.h>
21fe38b55cSguenther #include <unistd.h>
225be66c01Sguenther
23fe38b55cSguenther #include "thread_private.h"
24*a5511fa9Sguenther #include "rthread_cb.h"
25*a5511fa9Sguenther
26*a5511fa9Sguenther static __dead void
_thread_canceled(void)27*a5511fa9Sguenther _thread_canceled(void)
28*a5511fa9Sguenther {
29*a5511fa9Sguenther pthread_exit(PTHREAD_CANCELED);
30*a5511fa9Sguenther }
31fe38b55cSguenther
32fe38b55cSguenther void
_thread_set_callbacks(const struct thread_callbacks * cb,size_t len)33fe38b55cSguenther _thread_set_callbacks(const struct thread_callbacks *cb, size_t len)
34fe38b55cSguenther {
35fe38b55cSguenther sigset_t allmask, omask;
36fe38b55cSguenther
37fe38b55cSguenther if (sizeof(*cb) != len) {
38d54a2c0eSchl fprintf(stderr, "library mismatch: libc expected %zu but"
39d54a2c0eSchl " libpthread gave %zu\n", sizeof(*cb), len);
40fe38b55cSguenther fflush(stderr);
41fe38b55cSguenther _exit(44);
42fe38b55cSguenther }
43fe38b55cSguenther
44fe38b55cSguenther sigfillset(&allmask);
45fe38b55cSguenther if (sigprocmask(SIG_BLOCK, &allmask, &omask) == 0) {
46fe38b55cSguenther /* mprotect RW */
47fe38b55cSguenther memcpy(&_thread_cb, cb, sizeof(_thread_cb));
48*a5511fa9Sguenther
49*a5511fa9Sguenther /*
50*a5511fa9Sguenther * These are supplied by libc, but only enabled
51*a5511fa9Sguenther * here when we actually need to prep for doing MT.
52*a5511fa9Sguenther */
53*a5511fa9Sguenther _thread_cb.tc_canceled = _thread_canceled;
54*a5511fa9Sguenther _thread_cb.tc_flockfile = _thread_flockfile;
55*a5511fa9Sguenther _thread_cb.tc_ftrylockfile = _thread_ftrylockfile;
56*a5511fa9Sguenther _thread_cb.tc_funlockfile = _thread_funlockfile;
57*a5511fa9Sguenther _thread_cb.tc_malloc_lock = _thread_malloc_lock;
58*a5511fa9Sguenther _thread_cb.tc_malloc_unlock = _thread_malloc_unlock;
59*a5511fa9Sguenther _thread_cb.tc_atexit_lock = _thread_atexit_lock;
60*a5511fa9Sguenther _thread_cb.tc_atexit_unlock = _thread_atexit_unlock;
61*a5511fa9Sguenther _thread_cb.tc_atfork_lock = _thread_atfork_lock;
62*a5511fa9Sguenther _thread_cb.tc_atfork_unlock = _thread_atfork_unlock;
63*a5511fa9Sguenther _thread_cb.tc_arc4_lock = _thread_arc4_lock;
64*a5511fa9Sguenther _thread_cb.tc_arc4_unlock = _thread_arc4_unlock;
65*a5511fa9Sguenther _thread_cb.tc_mutex_lock = _thread_mutex_lock;
66*a5511fa9Sguenther _thread_cb.tc_mutex_unlock = _thread_mutex_unlock;
67*a5511fa9Sguenther _thread_cb.tc_mutex_destroy = _thread_mutex_destroy;
68*a5511fa9Sguenther _thread_cb.tc_tag_lock = _thread_tag_lock;
69*a5511fa9Sguenther _thread_cb.tc_tag_unlock = _thread_tag_unlock;
70*a5511fa9Sguenther _thread_cb.tc_tag_storage = _thread_tag_storage;
71*a5511fa9Sguenther
72fe38b55cSguenther /* mprotect RO | LOCKPERM | NOUNMAP */
73fe38b55cSguenther sigprocmask(SIG_SETMASK, &omask, NULL);
74fe38b55cSguenther }
75fe38b55cSguenther }
76