xref: /openbsd-src/lib/librthread/rthread_np.c (revision cef5a146e600a27064f0ea2aa25fc5f8663cb9b7)
1*cef5a146Sguenther /*	$OpenBSD: rthread_np.c,v 1.24 2023/01/07 05:24:58 guenther Exp $	*/
2c1289a39Sotto /*
3c1289a39Sotto  * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
4c1289a39Sotto  * Copyright (c) 2005 Otto Moerbeek <otto@openbsd.org>
5c1289a39Sotto  * All Rights Reserved.
6c1289a39Sotto  *
7c1289a39Sotto  * Permission to use, copy, modify, and distribute this software for any
8c1289a39Sotto  * purpose with or without fee is hereby granted, provided that the above
9c1289a39Sotto  * copyright notice and this permission notice appear in all copies.
10c1289a39Sotto  *
11c1289a39Sotto  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12c1289a39Sotto  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13c1289a39Sotto  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14c1289a39Sotto  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15c1289a39Sotto  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16c1289a39Sotto  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17c1289a39Sotto  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18c1289a39Sotto  */
19c1289a39Sotto 
204e44baf3Sderaadt #include <sys/types.h>
2164d5a1e1Skurt #include <sys/time.h>
22c1289a39Sotto #include <sys/lock.h>
2364d5a1e1Skurt #include <sys/resource.h>
24c1289a39Sotto #include <sys/queue.h>
255f5b45c4Sderaadt #include <sys/sysctl.h>
26c1289a39Sotto 
27c1289a39Sotto #include <errno.h>
28c1289a39Sotto #include <pthread.h>
29c1289a39Sotto #include <pthread_np.h>
3064d5a1e1Skurt #include <stddef.h>
31fe38b55cSguenther #include <stdint.h>
32c1289a39Sotto #include <string.h>
33fe38b55cSguenther #include <tib.h>
34c1289a39Sotto #include <unistd.h>
35c1289a39Sotto 
36c1289a39Sotto #include "rthread.h"
37c1289a39Sotto 
3829088be8Sguenther REDIRECT_SYSCALL(sysctl);
39*cef5a146Sguenther REDIRECT_SYSCALL(getthrname);
40*cef5a146Sguenther REDIRECT_SYSCALL(setthrname);
4129088be8Sguenther 
42c1289a39Sotto void
pthread_set_name_np(pthread_t thread,const char * name)43138597f3Shenning pthread_set_name_np(pthread_t thread, const char *name)
44c1289a39Sotto {
45*cef5a146Sguenther 	pid_t tid = 0;
46*cef5a146Sguenther 
47*cef5a146Sguenther 	if (thread != pthread_self())
48*cef5a146Sguenther 		tid = thread->tib->tib_tid;
49*cef5a146Sguenther 	setthrname(tid, name);
50c1289a39Sotto }
51c1289a39Sotto 
5229511769Stedu void
pthread_get_name_np(pthread_t thread,char * name,size_t len)5329511769Stedu pthread_get_name_np(pthread_t thread, char *name, size_t len)
5429511769Stedu {
55*cef5a146Sguenther 	pid_t tid = 0;
56*cef5a146Sguenther 
57*cef5a146Sguenther 	if (thread != pthread_self())
58*cef5a146Sguenther 		tid = thread->tib->tib_tid;
59*cef5a146Sguenther 	getthrname(tid, name, len);
6029511769Stedu }
6129511769Stedu 
62c1289a39Sotto int
pthread_main_np(void)63c1289a39Sotto pthread_main_np(void)
64c1289a39Sotto {
65fe38b55cSguenther 	return (!_threads_ready ||
66fe38b55cSguenther 	    (TIB_GET()->tib_thread_flags & TIB_THREAD_INITIAL_STACK) ? 1 : 0);
67c1289a39Sotto }
68c1289a39Sotto 
69c1289a39Sotto 
70c1289a39Sotto /*
71c1289a39Sotto  * Return stack info from the given thread.  Based upon the solaris
7258dbb15cSguenther  * thr_stksegment function.  Note that the returned ss_sp member is the
7358dbb15cSguenther  * *top* of the allocated stack area, unlike in sigaltstack() where
7458dbb15cSguenther  * it's the bottom.  You'll have to ask Sun what they were thinking...
75c1289a39Sotto  *
76c1289a39Sotto  * This function taken from the uthread library, with the following
77c1289a39Sotto  * license:
78c1289a39Sotto  * PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
79c1289a39Sotto int
pthread_stackseg_np(pthread_t thread,stack_t * sinfo)80c1289a39Sotto pthread_stackseg_np(pthread_t thread, stack_t *sinfo)
81c1289a39Sotto {
82c1289a39Sotto 	if (thread->stack) {
837e9a0975Skurt #ifdef MACHINE_STACK_GROWS_UP
847e9a0975Skurt 		sinfo->ss_sp = thread->stack->base;
857e9a0975Skurt #else
86896c2100Stedu 		sinfo->ss_sp = (char *)thread->stack->base +
87896c2100Stedu 		    thread->stack->len;
887e9a0975Skurt #endif
89c1289a39Sotto 		sinfo->ss_size = thread->stack->len;
9045b73353Skurt 		if (thread->stack->guardsize != 1)
9145b73353Skurt 			sinfo->ss_size -= thread->stack->guardsize;
92c1289a39Sotto 		sinfo->ss_flags = 0;
935f5b45c4Sderaadt 		return (0);
94fe38b55cSguenther 	} else if (thread->tib->tib_thread_flags & TIB_THREAD_INITIAL_STACK) {
955f5b45c4Sderaadt 		static struct _ps_strings _ps;
965f5b45c4Sderaadt 		static struct rlimit rl;
975f5b45c4Sderaadt 		static int gotself;
985f5b45c4Sderaadt 
99a5511fa9Sguenther 		if (!_threads_ready)		/* for ROUND_TO_PAGE */
100a5511fa9Sguenther 			_rthread_init();
101a5511fa9Sguenther 
1025f5b45c4Sderaadt 		if (gotself == 0) {
10387afc19eSderaadt 			const int mib[2] = { CTL_VM, VM_PSSTRINGS };
1045f5b45c4Sderaadt 			size_t len;
1055f5b45c4Sderaadt 
10664d5a1e1Skurt 			if (getrlimit(RLIMIT_STACK, &rl) != 0)
10764d5a1e1Skurt 				return (EAGAIN);
10858dbb15cSguenther 
1095f5b45c4Sderaadt 			len = sizeof(_ps);
1105f5b45c4Sderaadt 			if (sysctl(mib, 2, &_ps, &len, NULL, 0) != 0)
1115f5b45c4Sderaadt 				return (EAGAIN);
1125f5b45c4Sderaadt 			gotself = 1;
1135f5b45c4Sderaadt 		}
1145f5b45c4Sderaadt 
11564d5a1e1Skurt 		/*
1165f5b45c4Sderaadt 		 * Provides a rough estimation of stack bounds.   Caller
1175f5b45c4Sderaadt 		 * likely wants to know for the purpose of inspecting call
1185f5b45c4Sderaadt 		 * frames, but VM_PSSTRINGS points to process arguments...
11964d5a1e1Skurt 		 */
1207e9a0975Skurt #ifdef MACHINE_STACK_GROWS_UP
1215f5b45c4Sderaadt 		sinfo->ss_sp = _ps.val;
1227e9a0975Skurt #else
1237e9a0975Skurt 		sinfo->ss_sp = (void *)ROUND_TO_PAGE((uintptr_t)_ps.val);
1247e9a0975Skurt #endif
1255f5b45c4Sderaadt 		sinfo->ss_size = (size_t)rl.rlim_cur;
126c1289a39Sotto 		sinfo->ss_flags = 0;
1275f5b45c4Sderaadt 		return (0);
1285f5b45c4Sderaadt 	}
1295f5b45c4Sderaadt 	return (EAGAIN);
130c1289a39Sotto }
131