xref: /freebsd/sys/compat/linux/linux_signal.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*-
2  * Copyright (c) 1994-1995 S�ren Schmidt
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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software withough specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/signalvar.h>
37 #include <sys/sysproto.h>
38 
39 #include <machine/../linux/linux.h>
40 #include <machine/../linux/linux_proto.h>
41 #include <compat/linux/linux_signal.h>
42 #include <compat/linux/linux_util.h>
43 
44 void
45 linux_to_bsd_sigset(linux_sigset_t *lss, sigset_t *bss)
46 {
47 	int b, l;
48 
49 	SIGEMPTYSET(*bss);
50 	bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
51 	bss->__bits[1] = lss->__bits[1];
52 	for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
53 		if (LINUX_SIGISMEMBER(*lss, l)) {
54 #ifdef __alpha__
55 			b = _SIG_IDX(l);
56 #else
57 			b = linux_to_bsd_signal[_SIG_IDX(l)];
58 #endif
59 			if (b)
60 				SIGADDSET(*bss, b);
61 		}
62 	}
63 }
64 
65 void
66 bsd_to_linux_sigset(sigset_t *bss, linux_sigset_t *lss)
67 {
68 	int b, l;
69 
70 	LINUX_SIGEMPTYSET(*lss);
71 	lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
72 	lss->__bits[1] = bss->__bits[1];
73 	for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
74 		if (SIGISMEMBER(*bss, b)) {
75 #if __alpha__
76 			l = _SIG_IDX(b);
77 #else
78 			l = bsd_to_linux_signal[_SIG_IDX(b)];
79 #endif
80 			if (l)
81 				LINUX_SIGADDSET(*lss, l);
82 		}
83 	}
84 }
85 
86 static void
87 linux_to_bsd_sigaction(linux_sigaction_t *lsa, struct sigaction *bsa)
88 {
89 
90 	linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
91 	bsa->sa_handler = lsa->lsa_handler;
92 	bsa->sa_flags = 0;
93 	if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
94 		bsa->sa_flags |= SA_NOCLDSTOP;
95 	if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
96 		bsa->sa_flags |= SA_NOCLDWAIT;
97 	if (lsa->lsa_flags & LINUX_SA_SIGINFO)
98 		bsa->sa_flags |= SA_SIGINFO;
99 	if (lsa->lsa_flags & LINUX_SA_ONSTACK)
100 		bsa->sa_flags |= SA_ONSTACK;
101 	if (lsa->lsa_flags & LINUX_SA_RESTART)
102 		bsa->sa_flags |= SA_RESTART;
103 	if (lsa->lsa_flags & LINUX_SA_ONESHOT)
104 		bsa->sa_flags |= SA_RESETHAND;
105 	if (lsa->lsa_flags & LINUX_SA_NOMASK)
106 		bsa->sa_flags |= SA_NODEFER;
107 }
108 
109 static void
110 bsd_to_linux_sigaction(struct sigaction *bsa, linux_sigaction_t *lsa)
111 {
112 
113 	bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
114 	lsa->lsa_handler = bsa->sa_handler;
115 	lsa->lsa_restorer = NULL;	/* unsupported */
116 	lsa->lsa_flags = 0;
117 	if (bsa->sa_flags & SA_NOCLDSTOP)
118 		lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
119 	if (bsa->sa_flags & SA_NOCLDWAIT)
120 		lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
121 	if (bsa->sa_flags & SA_SIGINFO)
122 		lsa->lsa_flags |= LINUX_SA_SIGINFO;
123 	if (bsa->sa_flags & SA_ONSTACK)
124 		lsa->lsa_flags |= LINUX_SA_ONSTACK;
125 	if (bsa->sa_flags & SA_RESTART)
126 		lsa->lsa_flags |= LINUX_SA_RESTART;
127 	if (bsa->sa_flags & SA_RESETHAND)
128 		lsa->lsa_flags |= LINUX_SA_ONESHOT;
129 	if (bsa->sa_flags & SA_NODEFER)
130 		lsa->lsa_flags |= LINUX_SA_NOMASK;
131 }
132 
133 int
134 linux_do_sigaction(struct proc *p, int linux_sig, linux_sigaction_t *linux_nsa,
135 		   linux_sigaction_t *linux_osa)
136 {
137 	struct sigaction *nsa, *osa;
138 	struct sigaction_args sa_args;
139 	int error;
140 	caddr_t sg = stackgap_init();
141 
142 	if (linux_sig <= 0 || linux_sig > LINUX_NSIG)
143 		return (EINVAL);
144 
145 	if (linux_osa != NULL)
146 		osa = stackgap_alloc(&sg, sizeof(struct sigaction));
147 	else
148 		osa = NULL;
149 
150 	if (linux_nsa != NULL) {
151 		nsa = stackgap_alloc(&sg, sizeof(struct sigaction));
152 		linux_to_bsd_sigaction(linux_nsa, nsa);
153 	}
154 	else
155 		nsa = NULL;
156 
157 #ifndef __alpha__
158 	if (linux_sig <= LINUX_SIGTBLSZ)
159 		sa_args.sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
160 	else
161 #endif
162 		sa_args.sig = linux_sig;
163 
164 	sa_args.act = nsa;
165 	sa_args.oact = osa;
166 	error = sigaction(p, &sa_args);
167 	if (error)
168 		return (error);
169 
170 	if (linux_osa != NULL)
171 		bsd_to_linux_sigaction(osa, linux_osa);
172 
173 	return (0);
174 }
175 
176 
177 #ifndef __alpha__
178 int
179 linux_signal(struct proc *p, struct linux_signal_args *args)
180 {
181 	linux_sigaction_t nsa, osa;
182 	int error;
183 
184 #ifdef DEBUG
185 	if (ldebug(signal))
186 		printf(ARGS(signal, "%d, %p"),
187 		    args->sig, (void *)args->handler);
188 #endif
189 
190 	nsa.lsa_handler = args->handler;
191 	nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
192 	LINUX_SIGEMPTYSET(nsa.lsa_mask);
193 
194 	error = linux_do_sigaction(p, args->sig, &nsa, &osa);
195 	p->p_retval[0] = (int)osa.lsa_handler;
196 
197 	return (error);
198 }
199 #endif	/*!__alpha__*/
200 
201 int
202 linux_rt_sigaction(struct proc *p, struct linux_rt_sigaction_args *args)
203 {
204 	linux_sigaction_t nsa, osa;
205 	int error;
206 
207 #ifdef DEBUG
208 	if (ldebug(rt_sigaction))
209 		printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
210 		    (long)args->sig, (void *)args->act,
211 		    (void *)args->oact, (long)args->sigsetsize);
212 #endif
213 
214 	if (args->sigsetsize != sizeof(linux_sigset_t))
215 		return (EINVAL);
216 
217 	if (args->act != NULL) {
218 		error = copyin(args->act, &nsa, sizeof(linux_sigaction_t));
219 		if (error)
220 			return (error);
221 	}
222 
223 	error = linux_do_sigaction(p, args->sig,
224 				   args->act ? &nsa : NULL,
225 				   args->oact ? &osa : NULL);
226 
227 	if (args->oact != NULL && !error) {
228 		error = copyout(&osa, args->oact, sizeof(linux_sigaction_t));
229 	}
230 
231 	return (error);
232 }
233 
234 static int
235 linux_do_sigprocmask(struct proc *p, int how, linux_sigset_t *new,
236 		     linux_sigset_t *old)
237 {
238 	int error;
239 	sigset_t mask;
240 
241 	error = 0;
242 	p->p_retval[0] = 0;
243 
244 	PROC_LOCK(p);
245 	if (old != NULL)
246 		bsd_to_linux_sigset(&p->p_sigmask, old);
247 
248 	if (new != NULL) {
249 		linux_to_bsd_sigset(new, &mask);
250 
251 		switch (how) {
252 		case LINUX_SIG_BLOCK:
253 			SIGSETOR(p->p_sigmask, mask);
254 			SIG_CANTMASK(p->p_sigmask);
255 			break;
256 		case LINUX_SIG_UNBLOCK:
257 			SIGSETNAND(p->p_sigmask, mask);
258 			break;
259 		case LINUX_SIG_SETMASK:
260 			p->p_sigmask = mask;
261 			SIG_CANTMASK(p->p_sigmask);
262 			break;
263 		default:
264 			error = EINVAL;
265 			break;
266 		}
267 	}
268 	PROC_UNLOCK(p);
269 
270 	return (error);
271 }
272 
273 #ifndef __alpha__
274 int
275 linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args)
276 {
277 	linux_osigset_t mask;
278 	linux_sigset_t set, oset;
279 	int error;
280 
281 #ifdef DEBUG
282 	if (ldebug(sigprocmask))
283 		printf(ARGS(sigprocmask, "%d, *, *"), args->how);
284 #endif
285 
286 	if (args->mask != NULL) {
287 		error = copyin(args->mask, &mask, sizeof(linux_osigset_t));
288 		if (error)
289 			return (error);
290 		LINUX_SIGEMPTYSET(set);
291 		set.__bits[0] = mask;
292 	}
293 
294 	error = linux_do_sigprocmask(p, args->how,
295 				     args->mask ? &set : NULL,
296 				     args->omask ? &oset : NULL);
297 
298 	if (args->omask != NULL && !error) {
299 		mask = oset.__bits[0];
300 		error = copyout(&mask, args->omask, sizeof(linux_osigset_t));
301 	}
302 
303 	return (error);
304 }
305 #endif	/*!__alpha__*/
306 
307 int
308 linux_rt_sigprocmask(struct proc *p, struct linux_rt_sigprocmask_args *args)
309 {
310 	linux_sigset_t set, oset;
311 	int error;
312 
313 #ifdef DEBUG
314 	if (ldebug(rt_sigprocmask))
315 		printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
316 		    args->how, (void *)args->mask,
317 		    (void *)args->omask, (long)args->sigsetsize);
318 #endif
319 
320 	if (args->sigsetsize != sizeof(linux_sigset_t))
321 		return EINVAL;
322 
323 	if (args->mask != NULL) {
324 		error = copyin(args->mask, &set, sizeof(linux_sigset_t));
325 		if (error)
326 			return (error);
327 	}
328 
329 	error = linux_do_sigprocmask(p, args->how,
330 				     args->mask ? &set : NULL,
331 				     args->omask ? &oset : NULL);
332 
333 	if (args->omask != NULL && !error) {
334 		error = copyout(&oset, args->omask, sizeof(linux_sigset_t));
335 	}
336 
337 	return (error);
338 }
339 
340 #ifndef __alpha__
341 int
342 linux_siggetmask(struct proc *p, struct linux_siggetmask_args *args)
343 {
344 	linux_sigset_t mask;
345 
346 #ifdef DEBUG
347 	if (ldebug(siggetmask))
348 		printf(ARGS(siggetmask, ""));
349 #endif
350 
351 	PROC_LOCK(p);
352 	bsd_to_linux_sigset(&p->p_sigmask, &mask);
353 	PROC_UNLOCK(p);
354 	p->p_retval[0] = mask.__bits[0];
355 	return (0);
356 }
357 
358 int
359 linux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args)
360 {
361 	linux_sigset_t lset;
362 	sigset_t bset;
363 
364 #ifdef DEBUG
365 	if (ldebug(sigsetmask))
366 		printf(ARGS(sigsetmask, "%08lx"), (unsigned long)args->mask);
367 #endif
368 
369 	PROC_LOCK(p);
370 	bsd_to_linux_sigset(&p->p_sigmask, &lset);
371 	p->p_retval[0] = lset.__bits[0];
372 	LINUX_SIGEMPTYSET(lset);
373 	lset.__bits[0] = args->mask;
374 	linux_to_bsd_sigset(&lset, &bset);
375 	p->p_sigmask = bset;
376 	SIG_CANTMASK(p->p_sigmask);
377 	PROC_UNLOCK(p);
378 	return (0);
379 }
380 
381 int
382 linux_sigpending(struct proc *p, struct linux_sigpending_args *args)
383 {
384 	sigset_t bset;
385 	linux_sigset_t lset;
386 	linux_osigset_t mask;
387 
388 #ifdef DEBUG
389 	if (ldebug(sigpending))
390 		printf(ARGS(sigpending, "*"));
391 #endif
392 
393 	PROC_LOCK(p);
394 	bset = p->p_siglist;
395 	SIGSETAND(bset, p->p_sigmask);
396 	bsd_to_linux_sigset(&bset, &lset);
397 	PROC_UNLOCK(p);
398 	mask = lset.__bits[0];
399 	return (copyout(&mask, args->mask, sizeof(mask)));
400 }
401 #endif	/*!__alpha__*/
402 
403 int
404 linux_kill(struct proc *p, struct linux_kill_args *args)
405 {
406 	struct kill_args /* {
407 	    int pid;
408 	    int signum;
409 	} */ tmp;
410 
411 #ifdef DEBUG
412 	if (ldebug(kill))
413 		printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
414 #endif
415 
416 	/*
417 	 * Allow signal 0 as a means to check for privileges
418 	 */
419 	if (args->signum < 0 || args->signum > LINUX_NSIG)
420 		return EINVAL;
421 
422 #ifndef __alpha__
423 	if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
424 		tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
425 	else
426 #endif
427 		tmp.signum = args->signum;
428 
429 	tmp.pid = args->pid;
430 	return (kill(p, &tmp));
431 }
432