1 /* $OpenBSD: uthread_sigaltstack.c,v 1.3 2003/01/20 19:24:24 marc Exp $ */
2 /* PUBLIC DOMAIN <marc@snafu.org */
3 
4 #include <signal.h>
5 #include <errno.h>
6 #ifdef _THREAD_SAFE
7 #include <pthread.h>
8 #include "pthread_private.h"
9 
10 /*
11  * IEEE Std 1003.1-2001 says:
12  *
13  * "Use of this function by library threads that are not bound to
14  * kernel-scheduled entities results in undefined behavior."
15  *
16  * There exists code (e.g. alpha setjmp) that uses this function
17  * to get information about the current stack.
18  *
19  * The "undefined behaviour" in this implementation is thus:
20  * o if ss is *not* null return -1 with errno set to EINVAL
21  * o if oss is *not* null fill it in with information about the
22  *   current stack and return 0.
23  *
24  * This lets things like alpha setjmp work in threaded applications.
25  */
26 
27 int
sigaltstack(const struct sigaltstack * ss,struct sigaltstack * oss)28 sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss)
29 {
30 	struct pthread *curthread = _get_curthread();
31 
32 	int ret = 0;
33 	if (ss != NULL) {
34 		errno = EINVAL;
35 		ret = -1;
36 	} else if (oss != NULL) {
37 		/*
38 		 * get the requested info from the kernel if there is no
39 		 * thread or if the main thread (no thread stack).
40 		 */
41 		if (curthread == NULL || curthread->stack == NULL)
42 			_thread_sys_sigaltstack(ss, oss);
43 		else {
44 			oss->ss_sp = curthread->stack->base;
45 			oss->ss_size = curthread->stack->size;
46 			oss->ss_flags = SS_DISABLE;
47 		}
48 	}
49 	return (ret);
50 }
51 #endif /* _THREAD_SAFE */
52