1 /* $OpenBSD: rthread_np.c,v 1.24 2023/01/07 05:24:58 guenther Exp $ */
2 /*
3 * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
4 * Copyright (c) 2005 Otto Moerbeek <otto@openbsd.org>
5 * All Rights Reserved.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <sys/types.h>
21 #include <sys/time.h>
22 #include <sys/lock.h>
23 #include <sys/resource.h>
24 #include <sys/queue.h>
25 #include <sys/sysctl.h>
26
27 #include <errno.h>
28 #include <pthread.h>
29 #include <pthread_np.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <tib.h>
34 #include <unistd.h>
35
36 #include "rthread.h"
37
38 REDIRECT_SYSCALL(sysctl);
39 REDIRECT_SYSCALL(getthrname);
40 REDIRECT_SYSCALL(setthrname);
41
42 void
pthread_set_name_np(pthread_t thread,const char * name)43 pthread_set_name_np(pthread_t thread, const char *name)
44 {
45 pid_t tid = 0;
46
47 if (thread != pthread_self())
48 tid = thread->tib->tib_tid;
49 setthrname(tid, name);
50 }
51
52 void
pthread_get_name_np(pthread_t thread,char * name,size_t len)53 pthread_get_name_np(pthread_t thread, char *name, size_t len)
54 {
55 pid_t tid = 0;
56
57 if (thread != pthread_self())
58 tid = thread->tib->tib_tid;
59 getthrname(tid, name, len);
60 }
61
62 int
pthread_main_np(void)63 pthread_main_np(void)
64 {
65 return (!_threads_ready ||
66 (TIB_GET()->tib_thread_flags & TIB_THREAD_INITIAL_STACK) ? 1 : 0);
67 }
68
69
70 /*
71 * Return stack info from the given thread. Based upon the solaris
72 * thr_stksegment function. Note that the returned ss_sp member is the
73 * *top* of the allocated stack area, unlike in sigaltstack() where
74 * it's the bottom. You'll have to ask Sun what they were thinking...
75 *
76 * This function taken from the uthread library, with the following
77 * license:
78 * PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
79 int
pthread_stackseg_np(pthread_t thread,stack_t * sinfo)80 pthread_stackseg_np(pthread_t thread, stack_t *sinfo)
81 {
82 if (thread->stack) {
83 #ifdef MACHINE_STACK_GROWS_UP
84 sinfo->ss_sp = thread->stack->base;
85 #else
86 sinfo->ss_sp = (char *)thread->stack->base +
87 thread->stack->len;
88 #endif
89 sinfo->ss_size = thread->stack->len;
90 if (thread->stack->guardsize != 1)
91 sinfo->ss_size -= thread->stack->guardsize;
92 sinfo->ss_flags = 0;
93 return (0);
94 } else if (thread->tib->tib_thread_flags & TIB_THREAD_INITIAL_STACK) {
95 static struct _ps_strings _ps;
96 static struct rlimit rl;
97 static int gotself;
98
99 if (!_threads_ready) /* for ROUND_TO_PAGE */
100 _rthread_init();
101
102 if (gotself == 0) {
103 const int mib[2] = { CTL_VM, VM_PSSTRINGS };
104 size_t len;
105
106 if (getrlimit(RLIMIT_STACK, &rl) != 0)
107 return (EAGAIN);
108
109 len = sizeof(_ps);
110 if (sysctl(mib, 2, &_ps, &len, NULL, 0) != 0)
111 return (EAGAIN);
112 gotself = 1;
113 }
114
115 /*
116 * Provides a rough estimation of stack bounds. Caller
117 * likely wants to know for the purpose of inspecting call
118 * frames, but VM_PSSTRINGS points to process arguments...
119 */
120 #ifdef MACHINE_STACK_GROWS_UP
121 sinfo->ss_sp = _ps.val;
122 #else
123 sinfo->ss_sp = (void *)ROUND_TO_PAGE((uintptr_t)_ps.val);
124 #endif
125 sinfo->ss_size = (size_t)rl.rlim_cur;
126 sinfo->ss_flags = 0;
127 return (0);
128 }
129 return (EAGAIN);
130 }
131