]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkse/thread/thr_sigaltstack.c
This commit was generated by cvs2svn to compensate for changes in r174531,
[FreeBSD/FreeBSD.git] / lib / libkse / thread / thr_sigaltstack.c
1 /*-
2  * Copyright (c) 2003 David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "namespace.h"
31 #include <errno.h>
32 #include <signal.h>
33 #include "un-namespace.h"
34 #include "thr_private.h"
35
36 int     _sigaltstack(stack_t *_ss, stack_t *_oss);
37
38 LT10_COMPAT_PRIVATE(_sigaltstack);
39 LT10_COMPAT_DEFAULT(sigaltstack);
40
41 __weak_reference(_sigaltstack, sigaltstack);
42
43 int
44 _sigaltstack(stack_t *_ss, stack_t *_oss)
45 {
46         struct pthread *curthread = _get_curthread();
47         stack_t ss, oss;
48         int oonstack, errsave, ret;
49         kse_critical_t crit;
50
51         if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
52                 crit = _kse_critical_enter();
53                 ret = __sys_sigaltstack(_ss, _oss);
54                 errsave = errno;
55                 /* Get a copy */
56                 if (ret == 0 && _ss != NULL)
57                         curthread->sigstk = *_ss;
58                 _kse_critical_leave(crit);
59                 errno = errsave;
60                 return (ret);
61         }
62
63         if (_ss)
64                 ss = *_ss;
65         if (_oss)
66                 oss = *_oss;
67
68         /* Should get and set stack in atomic way */
69         crit = _kse_critical_enter();
70         oonstack = _thr_sigonstack(&ss);
71         if (_oss != NULL) {
72                 oss = curthread->sigstk;
73                 oss.ss_flags = (curthread->sigstk.ss_flags & SS_DISABLE)
74                     ? SS_DISABLE : ((oonstack) ? SS_ONSTACK : 0);
75         }
76
77         if (_ss != NULL) {
78                 if (oonstack) {
79                         _kse_critical_leave(crit);
80                         errno = EPERM;
81                         return (-1);
82                 }
83                 if ((ss.ss_flags & ~SS_DISABLE) != 0) {
84                         _kse_critical_leave(crit);
85                         errno = EINVAL;
86                         return (-1);
87                 }
88                 if (!(ss.ss_flags & SS_DISABLE)) {
89                         if (ss.ss_size < MINSIGSTKSZ) {
90                                 _kse_critical_leave(crit);
91                                 errno = ENOMEM;
92                                 return (-1);
93                         }
94                         curthread->sigstk = ss;
95                 } else {
96                         curthread->sigstk.ss_flags |= SS_DISABLE;
97                 }
98         }
99         _kse_critical_leave(crit);
100         if (_oss != NULL)
101                 *_oss = oss;
102         return (0);
103 }
104
105 int
106 _thr_sigonstack(void *sp)
107 {
108         struct pthread *curthread = _get_curthread();
109
110         return ((curthread->sigstk.ss_flags & SS_DISABLE) == 0 ?
111             (((size_t)sp - (size_t)curthread->sigstk.ss_sp) < curthread->sigstk.ss_size)
112             : 0);
113 }
114