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