xref: /netbsd-src/sys/compat/common/kern_sig_43.c (revision 0df165c04d0a9ca1adde9ed2b890344c937954a6)
1 /*	$NetBSD: kern_sig_43.c,v 1.27 2007/10/19 12:16:36 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_sig_43.c,v 1.27 2007/10/19 12:16:36 ad Exp $");
41 
42 #if defined(_KERNEL_OPT)
43 #include "opt_compat_netbsd.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/signalvar.h>
48 #include <sys/resourcevar.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/proc.h>
52 #include <sys/systm.h>
53 #include <sys/timeb.h>
54 #include <sys/times.h>
55 #include <sys/buf.h>
56 #include <sys/acct.h>
57 #include <sys/file.h>
58 #include <sys/kernel.h>
59 #include <sys/wait.h>
60 #include <sys/ktrace.h>
61 #include <sys/syslog.h>
62 #include <sys/stat.h>
63 #include <sys/core.h>
64 #include <sys/kauth.h>
65 
66 #include <sys/mount.h>
67 #include <sys/syscallargs.h>
68 
69 #include <sys/cpu.h>
70 
71 #include <sys/user.h>		/* for coredump */
72 
73 #include <compat/sys/signal.h>
74 
75 void compat_43_sigmask_to_sigset __P((const int *, sigset_t *));
76 void compat_43_sigset_to_sigmask __P((const sigset_t *, int *));
77 void compat_43_sigvec_to_sigaction __P((const struct sigvec *, struct sigaction *));
78 void compat_43_sigaction_to_sigvec __P((const struct sigaction *, struct sigvec *));
79 void compat_43_sigstack_to_sigaltstack __P((const struct sigstack *, struct sigaltstack *));
80 void compat_43_sigaltstack_to_sigstack __P((const struct sigaltstack *, struct sigstack *));
81 
82 void
83 compat_43_sigmask_to_sigset(sm, ss)
84 	const int *sm;
85 	sigset_t *ss;
86 {
87 
88 	ss->__bits[0] = *sm;
89 	ss->__bits[1] = 0;
90 	ss->__bits[2] = 0;
91 	ss->__bits[3] = 0;
92 }
93 
94 void
95 compat_43_sigset_to_sigmask(ss, sm)
96 	const sigset_t *ss;
97 	int *sm;
98 {
99 
100 	*sm = ss->__bits[0];
101 }
102 
103 void
104 compat_43_sigvec_to_sigaction(sv, sa)
105 	const struct sigvec *sv;
106 	struct sigaction *sa;
107 {
108 	sa->sa_handler = sv->sv_handler;
109 	compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask);
110 	sa->sa_flags = sv->sv_flags ^ SA_RESTART;
111 }
112 
113 void
114 compat_43_sigaction_to_sigvec(sa, sv)
115 	const struct sigaction *sa;
116 	struct sigvec *sv;
117 {
118 	sv->sv_handler = sa->sa_handler;
119 	compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask);
120 	sv->sv_flags = sa->sa_flags ^ SA_RESTART;
121 }
122 
123 void
124 compat_43_sigstack_to_sigaltstack(ss, sa)
125 	const struct sigstack *ss;
126 	struct sigaltstack *sa;
127 {
128 	sa->ss_sp = ss->ss_sp;
129 	sa->ss_size = SIGSTKSZ;	/* Use the recommended size */
130 	sa->ss_flags = 0;
131 	if (ss->ss_onstack)
132 		sa->ss_flags |= SS_ONSTACK;
133 }
134 
135 void
136 compat_43_sigaltstack_to_sigstack(sa, ss)
137 	const struct sigaltstack *sa;
138 	struct sigstack *ss;
139 {
140 	ss->ss_sp = sa->ss_sp;
141 	if (sa->ss_flags & SS_ONSTACK)
142 		ss->ss_onstack = 1;
143 	else
144 		ss->ss_onstack = 0;
145 }
146 
147 int
148 compat_43_sys_sigblock(struct lwp *l, void *v, register_t *retval)
149 {
150 	struct compat_43_sys_sigblock_args /* {
151 		syscallarg(int) mask;
152 	} */ *uap = v;
153 	struct proc *p = l->l_proc;
154 	int nsm, osm;
155 	sigset_t nss, oss;
156 	int error;
157 
158 	nsm = SCARG(uap, mask);
159 	compat_43_sigmask_to_sigset(&nsm, &nss);
160 	mutex_enter(&p->p_smutex);
161 	error = sigprocmask1(l, SIG_BLOCK, &nss, &oss);
162 	mutex_exit(&p->p_smutex);
163 	if (error)
164 		return (error);
165 	compat_43_sigset_to_sigmask(&oss, &osm);
166 	*retval = osm;
167 	return (0);
168 }
169 
170 int
171 compat_43_sys_sigsetmask(struct lwp *l, void *v, register_t *retval)
172 {
173 	struct compat_43_sys_sigsetmask_args /* {
174 		syscallarg(int) mask;
175 	} */ *uap = v;
176 	struct proc *p = l->l_proc;
177 	int nsm, osm;
178 	sigset_t nss, oss;
179 	int error;
180 
181 	nsm = SCARG(uap, mask);
182 	compat_43_sigmask_to_sigset(&nsm, &nss);
183 	mutex_enter(&p->p_smutex);
184 	error = sigprocmask1(l, SIG_SETMASK, &nss, &oss);
185 	mutex_exit(&p->p_smutex);
186 	if (error)
187 		return (error);
188 	compat_43_sigset_to_sigmask(&oss, &osm);
189 	*retval = osm;
190 	return (0);
191 }
192 
193 /* ARGSUSED */
194 int
195 compat_43_sys_sigstack(struct lwp *l, void *v, register_t *retval)
196 {
197 	struct compat_43_sys_sigstack_args /* {
198 		syscallarg(struct sigstack *) nss;
199 		syscallarg(struct sigstack *) oss;
200 	} */ *uap = v;
201 	struct sigstack nss, oss;
202 	struct sigaltstack nsa, osa;
203 	int error;
204 
205 	if (SCARG(uap, nss)) {
206 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
207 		if (error)
208 			return (error);
209 		compat_43_sigstack_to_sigaltstack(&nss, &nsa);
210 	}
211 	error = sigaltstack1(l,
212 	    SCARG(uap, nss) ? &nsa : 0, SCARG(uap, oss) ? &osa : 0);
213 	if (error)
214 		return (error);
215 	if (SCARG(uap, oss)) {
216 		compat_43_sigaltstack_to_sigstack(&osa, &oss);
217 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
218 		if (error)
219 			return (error);
220 	}
221 	return (0);
222 }
223 
224 /*
225  * Generalized interface signal handler, 4.3-compatible.
226  */
227 /* ARGSUSED */
228 int
229 compat_43_sys_sigvec(struct lwp *l, void *v, register_t *retval)
230 {
231 	struct compat_43_sys_sigvec_args /* {
232 		syscallarg(int) signum;
233 		syscallarg(const struct sigvec *) nsv;
234 		syscallarg(struct sigvec *) osv;
235 	} */ *uap = v;
236 	struct sigvec nsv, osv;
237 	struct sigaction nsa, osa;
238 	int error;
239 
240 	if (SCARG(uap, nsv)) {
241 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
242 		if (error)
243 			return (error);
244 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
245 	}
246 	error = sigaction1(l, SCARG(uap, signum),
247 	    SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0,
248 	    NULL, 0);
249 	if (error)
250 		return (error);
251 	if (SCARG(uap, osv)) {
252 		compat_43_sigaction_to_sigvec(&osa, &osv);
253 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
254 		if (error)
255 			return (error);
256 	}
257 	return (0);
258 }
259 
260 
261 /* ARGSUSED */
262 int
263 compat_43_sys_killpg(struct lwp *l, void *v, register_t *retval)
264 {
265 	struct compat_43_sys_killpg_args /* {
266 		syscallarg(int) pgid;
267 		syscallarg(int) signum;
268 	} */ *uap = v;
269 	ksiginfo_t ksi;
270 
271 #ifdef COMPAT_09
272 	SCARG(uap, pgid) = (short) SCARG(uap, pgid);
273 #endif
274 
275 	if ((u_int)SCARG(uap, signum) >= NSIG)
276 		return (EINVAL);
277 	memset(&ksi, 0, sizeof(ksi));
278 	ksi.ksi_signo = SCARG(uap, signum);
279 	ksi.ksi_code = SI_USER;
280 	ksi.ksi_pid = l->l_proc->p_pid;
281 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
282 	return (killpg1(l, &ksi, SCARG(uap, pgid), 0));
283 }
284