xref: /netbsd/sys/compat/common/kern_sig_43.c (revision bf9ec67e)
1 /*	$NetBSD: kern_sig_43.c,v 1.16 2001/11/13 02:08:01 lukem 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.16 2001/11/13 02:08:01 lukem 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 
65 #include <sys/mount.h>
66 #include <sys/syscallargs.h>
67 
68 #include <machine/cpu.h>
69 
70 #include <sys/user.h>		/* for coredump */
71 
72 void compat_43_sigmask_to_sigset __P((const int *, sigset_t *));
73 void compat_43_sigset_to_sigmask __P((const sigset_t *, int *));
74 void compat_43_sigvec_to_sigaction __P((const struct sigvec *, struct sigaction *));
75 void compat_43_sigaction_to_sigvec __P((const struct sigaction *, struct sigvec *));
76 void compat_43_sigstack_to_sigaltstack __P((const struct sigstack *, struct sigaltstack *));
77 void compat_43_sigaltstack_to_sigstack __P((const struct sigaltstack *, struct sigstack *));
78 
79 void
80 compat_43_sigmask_to_sigset(sm, ss)
81 	const int *sm;
82 	sigset_t *ss;
83 {
84 
85 	ss->__bits[0] = *sm;
86 	ss->__bits[1] = 0;
87 	ss->__bits[2] = 0;
88 	ss->__bits[3] = 0;
89 }
90 
91 void
92 compat_43_sigset_to_sigmask(ss, sm)
93 	const sigset_t *ss;
94 	int *sm;
95 {
96 
97 	*sm = ss->__bits[0];
98 }
99 
100 void
101 compat_43_sigvec_to_sigaction(sv, sa)
102 	const struct sigvec *sv;
103 	struct sigaction *sa;
104 {
105 	sa->sa_handler = sv->sv_handler;
106 	compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask);
107 	sa->sa_flags = sv->sv_flags ^ SA_RESTART;
108 }
109 
110 void
111 compat_43_sigaction_to_sigvec(sa, sv)
112 	const struct sigaction *sa;
113 	struct sigvec *sv;
114 {
115 	sv->sv_handler = sa->sa_handler;
116 	compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask);
117 	sv->sv_flags = sa->sa_flags ^ SA_RESTART;
118 }
119 
120 void
121 compat_43_sigstack_to_sigaltstack(ss, sa)
122 	const struct sigstack *ss;
123 	struct sigaltstack *sa;
124 {
125 	sa->ss_sp = ss->ss_sp;
126 	sa->ss_size = SIGSTKSZ;	/* Use the recommended size */
127 	sa->ss_flags = 0;
128 	if (ss->ss_onstack)
129 		sa->ss_flags |= SS_ONSTACK;
130 }
131 
132 void
133 compat_43_sigaltstack_to_sigstack(sa, ss)
134 	const struct sigaltstack *sa;
135 	struct sigstack *ss;
136 {
137 	ss->ss_sp = sa->ss_sp;
138 	if (sa->ss_flags & SS_ONSTACK)
139 		ss->ss_onstack = 1;
140 	else
141 		ss->ss_onstack = 0;
142 }
143 
144 int
145 compat_43_sys_sigblock(p, v, retval)
146 	struct proc *p;
147 	void *v;
148 	register_t *retval;
149 {
150 	struct compat_43_sys_sigblock_args /* {
151 		syscallarg(int) mask;
152 	} */ *uap = v;
153 	int nsm, osm;
154 	sigset_t nss, oss;
155 	int error;
156 
157 	nsm = SCARG(uap, mask);
158 	compat_43_sigmask_to_sigset(&nsm, &nss);
159 	error = sigprocmask1(p, SIG_BLOCK, &nss, &oss);
160 	if (error)
161 		return (error);
162 	compat_43_sigset_to_sigmask(&oss, &osm);
163 	*retval = osm;
164 	return (0);
165 }
166 
167 int
168 compat_43_sys_sigsetmask(p, v, retval)
169 	struct proc *p;
170 	void *v;
171 	register_t *retval;
172 {
173 	struct compat_43_sys_sigsetmask_args /* {
174 		syscallarg(int) mask;
175 	} */ *uap = v;
176 	int nsm, osm;
177 	sigset_t nss, oss;
178 	int error;
179 
180 	nsm = SCARG(uap, mask);
181 	compat_43_sigmask_to_sigset(&nsm, &nss);
182 	error = sigprocmask1(p, SIG_SETMASK, &nss, &oss);
183 	if (error)
184 		return (error);
185 	compat_43_sigset_to_sigmask(&oss, &osm);
186 	*retval = osm;
187 	return (0);
188 }
189 
190 /* ARGSUSED */
191 int
192 compat_43_sys_sigstack(p, v, retval)
193 	struct proc *p;
194 	void *v;
195 	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(p,
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(p, v, retval)
230 	struct proc *p;
231 	void *v;
232 	register_t *retval;
233 {
234 	struct compat_43_sys_sigvec_args /* {
235 		syscallarg(int) signum;
236 		syscallarg(const struct sigvec *) nsv;
237 		syscallarg(struct sigvec *) osv;
238 	} */ *uap = v;
239 	struct sigvec nsv, osv;
240 	struct sigaction nsa, osa;
241 	int error;
242 
243 	if (SCARG(uap, nsv)) {
244 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
245 		if (error)
246 			return (error);
247 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
248 	}
249 	error = sigaction1(p, SCARG(uap, signum),
250 	    SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0);
251 	if (error)
252 		return (error);
253 	if (SCARG(uap, osv)) {
254 		compat_43_sigaction_to_sigvec(&osa, &osv);
255 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
256 		if (error)
257 			return (error);
258 	}
259 	return (0);
260 }
261 
262 
263 /* ARGSUSED */
264 int
265 compat_43_sys_killpg(p, v, retval)
266 	struct proc *p;
267 	void *v;
268 	register_t *retval;
269 {
270 	struct compat_43_sys_killpg_args /* {
271 		syscallarg(int) pgid;
272 		syscallarg(int) signum;
273 	} */ *uap = v;
274 
275 #ifdef COMPAT_09
276 	SCARG(uap, pgid) = (short) SCARG(uap, pgid);
277 #endif
278 
279 	if ((u_int)SCARG(uap, signum) >= NSIG)
280 		return (EINVAL);
281 	return (killpg1(p, SCARG(uap, signum), SCARG(uap, pgid), 0));
282 }
283