xref: /netbsd-src/sys/compat/common/kern_sig_43.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: kern_sig_43.c,v 1.30 2007/12/20 23:02:44 dsl 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.30 2007/12/20 23:02:44 dsl 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(const int *, sigset_t *);
76 void compat_43_sigset_to_sigmask(const sigset_t *, int *);
77 void compat_43_sigvec_to_sigaction(const struct sigvec *, struct sigaction *);
78 void compat_43_sigaction_to_sigvec(const struct sigaction *, struct sigvec *);
79 void compat_43_sigstack_to_sigaltstack(const struct sigstack *, struct sigaltstack *);
80 void compat_43_sigaltstack_to_sigstack(const struct sigaltstack *, struct sigstack *);
81 
82 void
83 compat_43_sigmask_to_sigset(const int *sm, sigset_t *ss)
84 {
85 
86 	ss->__bits[0] = *sm;
87 	ss->__bits[1] = 0;
88 	ss->__bits[2] = 0;
89 	ss->__bits[3] = 0;
90 }
91 
92 void
93 compat_43_sigset_to_sigmask(const sigset_t *ss, int *sm)
94 {
95 
96 	*sm = ss->__bits[0];
97 }
98 
99 void
100 compat_43_sigvec_to_sigaction(const struct sigvec *sv, struct sigaction *sa)
101 {
102 	sa->sa_handler = sv->sv_handler;
103 	compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask);
104 	sa->sa_flags = sv->sv_flags ^ SA_RESTART;
105 }
106 
107 void
108 compat_43_sigaction_to_sigvec(const struct sigaction *sa, struct sigvec *sv)
109 {
110 	sv->sv_handler = sa->sa_handler;
111 	compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask);
112 	sv->sv_flags = sa->sa_flags ^ SA_RESTART;
113 }
114 
115 void
116 compat_43_sigstack_to_sigaltstack(const struct sigstack *ss, struct sigaltstack *sa)
117 {
118 	sa->ss_sp = ss->ss_sp;
119 	sa->ss_size = SIGSTKSZ;	/* Use the recommended size */
120 	sa->ss_flags = 0;
121 	if (ss->ss_onstack)
122 		sa->ss_flags |= SS_ONSTACK;
123 }
124 
125 void
126 compat_43_sigaltstack_to_sigstack(const struct sigaltstack *sa, struct sigstack *ss)
127 {
128 	ss->ss_sp = sa->ss_sp;
129 	if (sa->ss_flags & SS_ONSTACK)
130 		ss->ss_onstack = 1;
131 	else
132 		ss->ss_onstack = 0;
133 }
134 
135 int
136 compat_43_sys_sigblock(struct lwp *l, const struct compat_43_sys_sigblock_args *uap, register_t *retval)
137 {
138 	/* {
139 		syscallarg(int) mask;
140 	} */
141 	struct proc *p = l->l_proc;
142 	int nsm, osm;
143 	sigset_t nss, oss;
144 	int error;
145 
146 	nsm = SCARG(uap, mask);
147 	compat_43_sigmask_to_sigset(&nsm, &nss);
148 	mutex_enter(&p->p_smutex);
149 	error = sigprocmask1(l, SIG_BLOCK, &nss, &oss);
150 	mutex_exit(&p->p_smutex);
151 	if (error)
152 		return (error);
153 	compat_43_sigset_to_sigmask(&oss, &osm);
154 	*retval = osm;
155 	return (0);
156 }
157 
158 int
159 compat_43_sys_sigsetmask(struct lwp *l, const struct compat_43_sys_sigsetmask_args *uap, register_t *retval)
160 {
161 	/* {
162 		syscallarg(int) mask;
163 	} */
164 	struct proc *p = l->l_proc;
165 	int nsm, osm;
166 	sigset_t nss, oss;
167 	int error;
168 
169 	nsm = SCARG(uap, mask);
170 	compat_43_sigmask_to_sigset(&nsm, &nss);
171 	mutex_enter(&p->p_smutex);
172 	error = sigprocmask1(l, SIG_SETMASK, &nss, &oss);
173 	mutex_exit(&p->p_smutex);
174 	if (error)
175 		return (error);
176 	compat_43_sigset_to_sigmask(&oss, &osm);
177 	*retval = osm;
178 	return (0);
179 }
180 
181 /* ARGSUSED */
182 int
183 compat_43_sys_sigstack(struct lwp *l, const struct compat_43_sys_sigstack_args *uap, register_t *retval)
184 {
185 	/* {
186 		syscallarg(struct sigstack *) nss;
187 		syscallarg(struct sigstack *) oss;
188 	} */
189 	struct sigstack nss, oss;
190 	struct sigaltstack nsa, osa;
191 	int error;
192 
193 	if (SCARG(uap, nss)) {
194 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
195 		if (error)
196 			return (error);
197 		compat_43_sigstack_to_sigaltstack(&nss, &nsa);
198 	}
199 	error = sigaltstack1(l,
200 	    SCARG(uap, nss) ? &nsa : 0, SCARG(uap, oss) ? &osa : 0);
201 	if (error)
202 		return (error);
203 	if (SCARG(uap, oss)) {
204 		compat_43_sigaltstack_to_sigstack(&osa, &oss);
205 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
206 		if (error)
207 			return (error);
208 	}
209 	return (0);
210 }
211 
212 /*
213  * Generalized interface signal handler, 4.3-compatible.
214  */
215 /* ARGSUSED */
216 int
217 compat_43_sys_sigvec(struct lwp *l, const struct compat_43_sys_sigvec_args *uap, register_t *retval)
218 {
219 	/* {
220 		syscallarg(int) signum;
221 		syscallarg(const struct sigvec *) nsv;
222 		syscallarg(struct sigvec *) osv;
223 	} */
224 	struct sigvec nsv, osv;
225 	struct sigaction nsa, osa;
226 	int error;
227 
228 	if (SCARG(uap, nsv)) {
229 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
230 		if (error)
231 			return (error);
232 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
233 	}
234 	error = sigaction1(l, SCARG(uap, signum),
235 	    SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0,
236 	    NULL, 0);
237 	if (error)
238 		return (error);
239 	if (SCARG(uap, osv)) {
240 		compat_43_sigaction_to_sigvec(&osa, &osv);
241 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
242 		if (error)
243 			return (error);
244 	}
245 	return (0);
246 }
247 
248 
249 /* ARGSUSED */
250 int
251 compat_43_sys_killpg(struct lwp *l, const struct compat_43_sys_killpg_args *uap, register_t *retval)
252 {
253 	/* {
254 		syscallarg(int) pgid;
255 		syscallarg(int) signum;
256 	} */
257 	ksiginfo_t ksi;
258 	int pgid = SCARG(uap, pgid);
259 
260 #ifdef COMPAT_09
261 	pgid &= 0xffff;
262 #endif
263 
264 	if ((u_int)SCARG(uap, signum) >= NSIG)
265 		return (EINVAL);
266 	memset(&ksi, 0, sizeof(ksi));
267 	ksi.ksi_signo = SCARG(uap, signum);
268 	ksi.ksi_code = SI_USER;
269 	ksi.ksi_pid = l->l_proc->p_pid;
270 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
271 	return killpg1(l, &ksi, pgid, 0);
272 }
273