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