jcs's openbsd hax
openbsd
1/* $OpenBSD: rthread_np.c,v 1.26 2025/12/03 17:06:48 kurt 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/param.h> /* for MACHINE_STACK_GROWS_UP */
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
38REDIRECT_SYSCALL(sysctl);
39REDIRECT_SYSCALL(getthrname);
40REDIRECT_SYSCALL(setthrname);
41
42void
43pthread_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
52void
53pthread_get_name_np(pthread_t thread, char *name, size_t len)
54{
55 pid_t tid = 0;
56 int ret;
57
58 if (thread != pthread_self())
59 tid = thread->tib->tib_tid;
60 ret = getthrname(tid, name, len);
61 if (ret == ERANGE && len > 0)
62 name[len-1] = '\0';
63}
64
65int
66pthread_main_np(void)
67{
68 return (!_threads_ready ||
69 (TIB_GET()->tib_thread_flags & TIB_THREAD_INITIAL_STACK) ? 1 : 0);
70}
71
72
73/*
74 * Return stack info from the given thread. Based upon the solaris
75 * thr_stksegment function. Note that the returned ss_sp member is the
76 * *top* of the allocated stack area, unlike in sigaltstack() where
77 * it's the bottom. You'll have to ask Sun what they were thinking...
78 *
79 * This function taken from the uthread library, with the following
80 * license:
81 * PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
82int
83pthread_stackseg_np(pthread_t thread, stack_t *sinfo)
84{
85 if (thread->stack) {
86#ifdef MACHINE_STACK_GROWS_UP
87 sinfo->ss_sp = thread->stack->base;
88#else
89 sinfo->ss_sp = (char *)thread->stack->base +
90 thread->stack->len;
91#endif
92 sinfo->ss_size = thread->stack->len;
93 if (thread->stack->guardsize != 1)
94 sinfo->ss_size -= thread->stack->guardsize;
95 sinfo->ss_flags = 0;
96 return (0);
97 } else if (thread->tib->tib_thread_flags & TIB_THREAD_INITIAL_STACK) {
98 static struct _ps_strings _ps;
99 static struct rlimit rl;
100 static int gotself;
101
102 if (!_threads_ready) /* for ROUND_TO_PAGE */
103 _rthread_init();
104
105 if (gotself == 0) {
106 const int mib[2] = { CTL_VM, VM_PSSTRINGS };
107 size_t len;
108
109 if (getrlimit(RLIMIT_STACK, &rl) != 0)
110 return (EAGAIN);
111
112 len = sizeof(_ps);
113 if (sysctl(mib, 2, &_ps, &len, NULL, 0) != 0)
114 return (EAGAIN);
115 gotself = 1;
116 }
117
118 /*
119 * Provides a rough estimation of stack bounds. Caller
120 * likely wants to know for the purpose of inspecting call
121 * frames, but VM_PSSTRINGS points to process arguments...
122 */
123#ifdef MACHINE_STACK_GROWS_UP
124 sinfo->ss_sp = _ps.val;
125#else
126 sinfo->ss_sp = (void *)ROUND_TO_PAGE((uintptr_t)_ps.val);
127#endif
128 sinfo->ss_size = (size_t)rl.rlim_cur;
129 sinfo->ss_flags = 0;
130 return (0);
131 }
132 return (EAGAIN);
133}