xref: /illumos-gate/usr/src/uts/common/syscall/sigaltstack.c (revision e3ae4b35c024af1196582063ecee3ab79367227d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <sys/systm.h>
33 #include <sys/errno.h>
34 #include <sys/proc.h>
35 #include <sys/fault.h>
36 #include <sys/signal.h>
37 #include <sys/siginfo.h>
38 #include <sys/debug.h>
39 
40 int
41 sigaltstack(struct sigaltstack *ssp, struct sigaltstack *oss)
42 {
43 	klwp_t *lwp = ttolwp(curthread);
44 	struct sigaltstack ss;
45 
46 	/*
47 	 * User's oss and ss might be the same address, so copyin first and
48 	 * save before copying out.
49 	 */
50 	if (ssp) {
51 		if (lwp->lwp_sigaltstack.ss_flags & SS_ONSTACK)
52 			return (set_errno(EPERM));
53 		if (copyin(ssp, &ss, sizeof (ss)))
54 			return (set_errno(EFAULT));
55 		if (ss.ss_flags & ~SS_DISABLE)
56 			return (set_errno(EINVAL));
57 		if (!(ss.ss_flags & SS_DISABLE) && ss.ss_size < MINSIGSTKSZ)
58 			return (set_errno(ENOMEM));
59 	}
60 
61 	if (oss) {
62 		if (copyout(&lwp->lwp_sigaltstack,
63 		    oss, sizeof (struct sigaltstack)))
64 			return (set_errno(EFAULT));
65 	}
66 
67 	if (ssp)
68 		lwp->lwp_sigaltstack = ss;
69 
70 	return (0);
71 }
72 
73 #ifdef _LP64
74 int
75 sigaltstack32(struct sigaltstack32 *ssp, struct sigaltstack32 *oss)
76 {
77 	klwp_t *lwp = ttolwp(curthread);
78 	struct sigaltstack   *ss;
79 	struct sigaltstack32 ss32, oss32;
80 
81 	/*
82 	 * User's oss and ss might be the same address, so copyin first and
83 	 * save before copying out.
84 	 */
85 	if (ssp) {
86 		if (lwp->lwp_sigaltstack.ss_flags & SS_ONSTACK)
87 			return (set_errno(EPERM));
88 		if (copyin(ssp, &ss32, sizeof (ss32)))
89 			return (set_errno(EFAULT));
90 		if (ss32.ss_flags & ~SS_DISABLE)
91 			return (set_errno(EINVAL));
92 		if (!(ss32.ss_flags & SS_DISABLE) && ss32.ss_size < MINSIGSTKSZ)
93 			return (set_errno(ENOMEM));
94 	}
95 
96 	if (oss) {
97 		/*
98 		 * copy to ILP32 struct before copyout.
99 		 */
100 		ss = &lwp->lwp_sigaltstack;
101 		oss32.ss_sp    = (caddr32_t)(uintptr_t)ss->ss_sp;
102 		oss32.ss_size  = (size32_t)ss->ss_size;
103 		oss32.ss_flags = ss->ss_flags;
104 
105 		if (copyout(&oss32, oss, sizeof (oss32)))
106 			return (set_errno(EFAULT));
107 	}
108 
109 	if (ssp) {
110 		ss = &lwp->lwp_sigaltstack;
111 		ss->ss_sp = (void *)(uintptr_t)ss32.ss_sp;
112 		ss->ss_size = (size_t)ss32.ss_size;
113 		ss->ss_flags = ss32.ss_flags;
114 	}
115 
116 	return (0);
117 }
118 #endif /* _LP64 */
119