xref: /freebsd/sys/compat/linux/linux_signal.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994-1995 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_ktrace.h"
31 
32 #include <sys/param.h>
33 #include <sys/ktr.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/signalvar.h>
38 #include <sys/sx.h>
39 #include <sys/syscallsubr.h>
40 #include <sys/sysproto.h>
41 #ifdef KTRACE
42 #include <sys/ktrace.h>
43 #endif
44 
45 #include <security/audit/audit.h>
46 
47 #ifdef COMPAT_LINUX32
48 #include <machine/../linux32/linux.h>
49 #include <machine/../linux32/linux32_proto.h>
50 #else
51 #include <machine/../linux/linux.h>
52 #include <machine/../linux/linux_proto.h>
53 #endif
54 #include <compat/linux/linux_mib.h>
55 #include <compat/linux/linux_signal.h>
56 #include <compat/linux/linux_time.h>
57 #include <compat/linux/linux_util.h>
58 #include <compat/linux/linux_emul.h>
59 #include <compat/linux/linux_misc.h>
60 
61 static int	linux_pksignal(struct thread *td, int pid, int sig,
62 		    ksiginfo_t *ksi);
63 static int	linux_psignal(struct thread *td, int pid, int sig);
64 static int	linux_tdksignal(struct thread *td, lwpid_t tid,
65 		    int tgid, int sig, ksiginfo_t *ksi);
66 static int	linux_tdsignal(struct thread *td, lwpid_t tid,
67 		    int tgid, int sig);
68 static void	sicode_to_lsicode(int sig, int si_code, int *lsi_code);
69 static int	linux_common_rt_sigtimedwait(struct thread *,
70 		    l_sigset_t *, struct timespec *, l_siginfo_t *,
71 		    l_size_t);
72 
73 static void
74 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
75 {
76 	unsigned long flags;
77 
78 	linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
79 	bsa->sa_handler = PTRIN(lsa->lsa_handler);
80 	bsa->sa_flags = 0;
81 
82 	flags = lsa->lsa_flags;
83 	if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP) {
84 		flags &= ~LINUX_SA_NOCLDSTOP;
85 		bsa->sa_flags |= SA_NOCLDSTOP;
86 	}
87 	if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT) {
88 		flags &= ~LINUX_SA_NOCLDWAIT;
89 		bsa->sa_flags |= SA_NOCLDWAIT;
90 	}
91 	if (lsa->lsa_flags & LINUX_SA_SIGINFO) {
92 		flags &= ~LINUX_SA_SIGINFO;
93 		bsa->sa_flags |= SA_SIGINFO;
94 #ifdef notyet
95 		/*
96 		 * XXX: We seem to be missing code to convert
97 		 *      some of the fields in ucontext_t.
98 		 */
99 		linux_msg(curthread,
100 		    "partially unsupported sigaction flag SA_SIGINFO");
101 #endif
102 	}
103 	if (lsa->lsa_flags & LINUX_SA_RESTORER) {
104 		flags &= ~LINUX_SA_RESTORER;
105 		/*
106 		 * We ignore the lsa_restorer and always use our own signal
107 		 * trampoline instead.  It looks like SA_RESTORER is obsolete
108 		 * in Linux too - it doesn't seem to be used at all on arm64.
109 		 * In any case: see Linux sigreturn(2).
110 		 */
111 	}
112 	if (lsa->lsa_flags & LINUX_SA_ONSTACK) {
113 		flags &= ~LINUX_SA_ONSTACK;
114 		bsa->sa_flags |= SA_ONSTACK;
115 	}
116 	if (lsa->lsa_flags & LINUX_SA_RESTART) {
117 		flags &= ~LINUX_SA_RESTART;
118 		bsa->sa_flags |= SA_RESTART;
119 	}
120 	if (lsa->lsa_flags & LINUX_SA_INTERRUPT) {
121 		flags &= ~LINUX_SA_INTERRUPT;
122 		/* Documented to be a "historical no-op". */
123 	}
124 	if (lsa->lsa_flags & LINUX_SA_ONESHOT) {
125 		flags &= ~LINUX_SA_ONESHOT;
126 		bsa->sa_flags |= SA_RESETHAND;
127 	}
128 	if (lsa->lsa_flags & LINUX_SA_NOMASK) {
129 		flags &= ~LINUX_SA_NOMASK;
130 		bsa->sa_flags |= SA_NODEFER;
131 	}
132 
133 	if (flags != 0)
134 		linux_msg(curthread, "unsupported sigaction flag %#lx", flags);
135 }
136 
137 static void
138 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
139 {
140 
141 	bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
142 #ifdef COMPAT_LINUX32
143 	lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
144 #else
145 	lsa->lsa_handler = bsa->sa_handler;
146 #endif
147 	lsa->lsa_restorer = 0;		/* unsupported */
148 	lsa->lsa_flags = 0;
149 	if (bsa->sa_flags & SA_NOCLDSTOP)
150 		lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
151 	if (bsa->sa_flags & SA_NOCLDWAIT)
152 		lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
153 	if (bsa->sa_flags & SA_SIGINFO)
154 		lsa->lsa_flags |= LINUX_SA_SIGINFO;
155 	if (bsa->sa_flags & SA_ONSTACK)
156 		lsa->lsa_flags |= LINUX_SA_ONSTACK;
157 	if (bsa->sa_flags & SA_RESTART)
158 		lsa->lsa_flags |= LINUX_SA_RESTART;
159 	if (bsa->sa_flags & SA_RESETHAND)
160 		lsa->lsa_flags |= LINUX_SA_ONESHOT;
161 	if (bsa->sa_flags & SA_NODEFER)
162 		lsa->lsa_flags |= LINUX_SA_NOMASK;
163 }
164 
165 int
166 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
167 		   l_sigaction_t *linux_osa)
168 {
169 	struct sigaction act, oact, *nsa, *osa;
170 	int error, sig;
171 
172 	if (!LINUX_SIG_VALID(linux_sig))
173 		return (EINVAL);
174 
175 	osa = (linux_osa != NULL) ? &oact : NULL;
176 	if (linux_nsa != NULL) {
177 		nsa = &act;
178 		linux_to_bsd_sigaction(linux_nsa, nsa);
179 #ifdef KTRACE
180 		if (KTRPOINT(td, KTR_STRUCT))
181 			linux_ktrsigset(&linux_nsa->lsa_mask,
182 			    sizeof(linux_nsa->lsa_mask));
183 #endif
184 	} else
185 		nsa = NULL;
186 	sig = linux_to_bsd_signal(linux_sig);
187 
188 	error = kern_sigaction(td, sig, nsa, osa, 0);
189 	if (error != 0)
190 		return (error);
191 
192 	if (linux_osa != NULL) {
193 		bsd_to_linux_sigaction(osa, linux_osa);
194 #ifdef KTRACE
195 		if (KTRPOINT(td, KTR_STRUCT))
196 			linux_ktrsigset(&linux_osa->lsa_mask,
197 			    sizeof(linux_osa->lsa_mask));
198 #endif
199 	}
200 	return (0);
201 }
202 
203 int
204 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
205 {
206 	stack_t ss, oss;
207 	l_stack_t lss;
208 	int error;
209 
210 	memset(&lss, 0, sizeof(lss));
211 	LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss);
212 
213 	if (uap->uss != NULL) {
214 		error = copyin(uap->uss, &lss, sizeof(lss));
215 		if (error != 0)
216 			return (error);
217 
218 		ss.ss_sp = PTRIN(lss.ss_sp);
219 		ss.ss_size = lss.ss_size;
220 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
221 	}
222 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
223 	    (uap->uoss != NULL) ? &oss : NULL);
224 	if (error == 0 && uap->uoss != NULL) {
225 		lss.ss_sp = PTROUT(oss.ss_sp);
226 		lss.ss_size = oss.ss_size;
227 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
228 		error = copyout(&lss, uap->uoss, sizeof(lss));
229 	}
230 
231 	return (error);
232 }
233 
234 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
235 int
236 linux_signal(struct thread *td, struct linux_signal_args *args)
237 {
238 	l_sigaction_t nsa, osa;
239 	int error;
240 
241 	nsa.lsa_handler = args->handler;
242 	nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
243 	LINUX_SIGEMPTYSET(nsa.lsa_mask);
244 
245 	error = linux_do_sigaction(td, args->sig, &nsa, &osa);
246 	td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
247 
248 	return (error);
249 }
250 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
251 
252 int
253 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
254 {
255 	l_sigaction_t nsa, osa;
256 	int error;
257 
258 	if (args->sigsetsize != sizeof(l_sigset_t))
259 		return (EINVAL);
260 
261 	if (args->act != NULL) {
262 		error = copyin(args->act, &nsa, sizeof(nsa));
263 		if (error != 0)
264 			return (error);
265 	}
266 
267 	error = linux_do_sigaction(td, args->sig,
268 				   args->act ? &nsa : NULL,
269 				   args->oact ? &osa : NULL);
270 
271 	if (args->oact != NULL && error == 0)
272 		error = copyout(&osa, args->oact, sizeof(osa));
273 
274 	return (error);
275 }
276 
277 static int
278 linux_do_sigprocmask(struct thread *td, int how, sigset_t *new,
279 		     l_sigset_t *old)
280 {
281 	sigset_t omask;
282 	int error;
283 
284 	td->td_retval[0] = 0;
285 
286 	switch (how) {
287 	case LINUX_SIG_BLOCK:
288 		how = SIG_BLOCK;
289 		break;
290 	case LINUX_SIG_UNBLOCK:
291 		how = SIG_UNBLOCK;
292 		break;
293 	case LINUX_SIG_SETMASK:
294 		how = SIG_SETMASK;
295 		break;
296 	default:
297 		return (EINVAL);
298 	}
299 	error = kern_sigprocmask(td, how, new, &omask, 0);
300 	if (error == 0 && old != NULL)
301 		bsd_to_linux_sigset(&omask, old);
302 
303 	return (error);
304 }
305 
306 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
307 int
308 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
309 {
310 	l_osigset_t mask;
311 	l_sigset_t lset, oset;
312 	sigset_t set;
313 	int error;
314 
315 	if (args->mask != NULL) {
316 		error = copyin(args->mask, &mask, sizeof(mask));
317 		if (error != 0)
318 			return (error);
319 		LINUX_SIGEMPTYSET(lset);
320 		lset.__mask = mask;
321 #ifdef KTRACE
322 		if (KTRPOINT(td, KTR_STRUCT))
323 			linux_ktrsigset(&lset, sizeof(lset));
324 #endif
325 		linux_to_bsd_sigset(&lset, &set);
326 	}
327 
328 	error = linux_do_sigprocmask(td, args->how,
329 				     args->mask ? &set : NULL,
330 				     args->omask ? &oset : NULL);
331 
332 	if (args->omask != NULL && error == 0) {
333 #ifdef KTRACE
334 		if (KTRPOINT(td, KTR_STRUCT))
335 			linux_ktrsigset(&oset, sizeof(oset));
336 #endif
337 		mask = oset.__mask;
338 		error = copyout(&mask, args->omask, sizeof(mask));
339 	}
340 
341 	return (error);
342 }
343 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
344 
345 int
346 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
347 {
348 	l_sigset_t oset;
349 	sigset_t set, *pset;
350 	int error;
351 
352 	error = linux_copyin_sigset(td, args->mask, args->sigsetsize,
353 	    &set, &pset);
354 	if (error != 0)
355 		return (EINVAL);
356 
357 	error = linux_do_sigprocmask(td, args->how, pset,
358 				     args->omask ? &oset : NULL);
359 
360 	if (args->omask != NULL && error == 0) {
361 #ifdef KTRACE
362 		if (KTRPOINT(td, KTR_STRUCT))
363 			linux_ktrsigset(&oset, sizeof(oset));
364 #endif
365 		error = copyout(&oset, args->omask, sizeof(oset));
366 	}
367 
368 	return (error);
369 }
370 
371 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
372 int
373 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
374 {
375 	struct proc *p = td->td_proc;
376 	l_sigset_t mask;
377 
378 	PROC_LOCK(p);
379 	bsd_to_linux_sigset(&td->td_sigmask, &mask);
380 	PROC_UNLOCK(p);
381 	td->td_retval[0] = mask.__mask;
382 #ifdef KTRACE
383 	if (KTRPOINT(td, KTR_STRUCT))
384 		linux_ktrsigset(&mask, sizeof(mask));
385 #endif
386 	return (0);
387 }
388 
389 int
390 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
391 {
392 	struct proc *p = td->td_proc;
393 	l_sigset_t lset;
394 	sigset_t bset;
395 
396 	PROC_LOCK(p);
397 	bsd_to_linux_sigset(&td->td_sigmask, &lset);
398 	td->td_retval[0] = lset.__mask;
399 	LINUX_SIGEMPTYSET(lset);
400 	lset.__mask = args->mask;
401 	linux_to_bsd_sigset(&lset, &bset);
402 #ifdef KTRACE
403 	if (KTRPOINT(td, KTR_STRUCT))
404 		linux_ktrsigset(&lset, sizeof(lset));
405 #endif
406 	td->td_sigmask = bset;
407 	SIG_CANTMASK(td->td_sigmask);
408 	signotify(td);
409 	PROC_UNLOCK(p);
410 	return (0);
411 }
412 
413 int
414 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
415 {
416 	struct proc *p = td->td_proc;
417 	sigset_t bset;
418 	l_sigset_t lset;
419 	l_osigset_t mask;
420 
421 	PROC_LOCK(p);
422 	bset = p->p_siglist;
423 	SIGSETOR(bset, td->td_siglist);
424 	SIGSETAND(bset, td->td_sigmask);
425 	PROC_UNLOCK(p);
426 	bsd_to_linux_sigset(&bset, &lset);
427 #ifdef KTRACE
428 	if (KTRPOINT(td, KTR_STRUCT))
429 		linux_ktrsigset(&lset, sizeof(lset));
430 #endif
431 	mask = lset.__mask;
432 	return (copyout(&mask, args->mask, sizeof(mask)));
433 }
434 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
435 
436 /*
437  * MPSAFE
438  */
439 int
440 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
441 {
442 	struct proc *p = td->td_proc;
443 	sigset_t bset;
444 	l_sigset_t lset;
445 
446 	if (args->sigsetsize > sizeof(lset))
447 		return (EINVAL);
448 		/* NOT REACHED */
449 
450 	PROC_LOCK(p);
451 	bset = p->p_siglist;
452 	SIGSETOR(bset, td->td_siglist);
453 	SIGSETAND(bset, td->td_sigmask);
454 	PROC_UNLOCK(p);
455 	bsd_to_linux_sigset(&bset, &lset);
456 #ifdef KTRACE
457 	if (KTRPOINT(td, KTR_STRUCT))
458 		linux_ktrsigset(&lset, sizeof(lset));
459 #endif
460 	return (copyout(&lset, args->set, args->sigsetsize));
461 }
462 
463 int
464 linux_rt_sigtimedwait(struct thread *td,
465 	struct linux_rt_sigtimedwait_args *args)
466 {
467 	struct timespec ts, *tsa;
468 	int error;
469 
470 	if (args->timeout) {
471 		error = linux_get_timespec(&ts, args->timeout);
472 		if (error != 0)
473 			return (error);
474 		tsa = &ts;
475 	} else
476 		tsa = NULL;
477 
478 	return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
479 	    args->ptr, args->sigsetsize));
480 }
481 
482 static int
483 linux_common_rt_sigtimedwait(struct thread *td, l_sigset_t *mask,
484     struct timespec *tsa, l_siginfo_t *ptr, l_size_t sigsetsize)
485 {
486 	int error, sig;
487 	sigset_t bset;
488 	l_siginfo_t lsi;
489 	ksiginfo_t ksi;
490 
491 	error = linux_copyin_sigset(td, mask, sigsetsize, &bset, NULL);
492 	if (error != 0)
493 		return (error);
494 
495 	ksiginfo_init(&ksi);
496 	error = kern_sigtimedwait(td, bset, &ksi, tsa);
497 	if (error != 0)
498 		return (error);
499 
500 	sig = bsd_to_linux_signal(ksi.ksi_signo);
501 
502 	if (ptr) {
503 		memset(&lsi, 0, sizeof(lsi));
504 		siginfo_to_lsiginfo(&ksi.ksi_info, &lsi, sig);
505 		error = copyout(&lsi, ptr, sizeof(lsi));
506 	}
507 	if (error == 0)
508 		td->td_retval[0] = sig;
509 
510 	return (error);
511 }
512 
513 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
514 int
515 linux_rt_sigtimedwait_time64(struct thread *td,
516 	struct linux_rt_sigtimedwait_time64_args *args)
517 {
518 	struct timespec ts, *tsa;
519 	int error;
520 
521 	if (args->timeout) {
522 		error = linux_get_timespec64(&ts, args->timeout);
523 		if (error != 0)
524 			return (error);
525 		tsa = &ts;
526 	} else
527 		tsa = NULL;
528 
529 	return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
530 	    args->ptr, args->sigsetsize));
531 }
532 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
533 
534 int
535 linux_kill(struct thread *td, struct linux_kill_args *args)
536 {
537 	int sig;
538 
539 	/*
540 	 * Allow signal 0 as a means to check for privileges
541 	 */
542 	if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
543 		return (EINVAL);
544 
545 	if (args->signum > 0)
546 		sig = linux_to_bsd_signal(args->signum);
547 	else
548 		sig = 0;
549 
550 	if (args->pid > PID_MAX)
551 		return (linux_psignal(td, args->pid, sig));
552 	else
553 		return (kern_kill(td, args->pid, sig));
554 }
555 
556 int
557 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
558 {
559 	int sig;
560 
561 	if (args->pid <= 0 || args->tgid <=0)
562 		return (EINVAL);
563 
564 	/*
565 	 * Allow signal 0 as a means to check for privileges
566 	 */
567 	if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
568 		return (EINVAL);
569 
570 	if (args->sig > 0)
571 		sig = linux_to_bsd_signal(args->sig);
572 	else
573 		sig = 0;
574 
575 	return (linux_tdsignal(td, args->pid, args->tgid, sig));
576 }
577 
578 /*
579  * Deprecated since 2.5.75. Replaced by tgkill().
580  */
581 int
582 linux_tkill(struct thread *td, struct linux_tkill_args *args)
583 {
584 	int sig;
585 
586 	if (args->tid <= 0)
587 		return (EINVAL);
588 
589 	if (!LINUX_SIG_VALID(args->sig))
590 		return (EINVAL);
591 
592 	sig = linux_to_bsd_signal(args->sig);
593 
594 	return (linux_tdsignal(td, args->tid, -1, sig));
595 }
596 
597 static int
598 sigfpe_sicode2lsicode(int si_code)
599 {
600 
601 	switch (si_code) {
602 	case FPE_INTOVF:
603 		return (LINUX_FPE_INTOVF);
604 	case FPE_INTDIV:
605 		return (LINUX_FPE_INTDIV);
606 	case FPE_FLTIDO:
607 		return (LINUX_FPE_FLTUNK);
608 	default:
609 		return (si_code);
610 	}
611 }
612 
613 static int
614 sigbus_sicode2lsicode(int si_code)
615 {
616 
617 	switch (si_code) {
618 	case BUS_OOMERR:
619 		return (LINUX_BUS_MCEERR_AR);
620 	default:
621 		return (si_code);
622 	}
623 }
624 
625 static int
626 sigsegv_sicode2lsicode(int si_code)
627 {
628 
629 	switch (si_code) {
630 	case SEGV_PKUERR:
631 		return (LINUX_SEGV_PKUERR);
632 	default:
633 		return (si_code);
634 	}
635 }
636 
637 static int
638 sigtrap_sicode2lsicode(int si_code)
639 {
640 
641 	switch (si_code) {
642 	case TRAP_DTRACE:
643 		return (LINUX_TRAP_TRACE);
644 	case TRAP_CAP:
645 		return (LINUX_TRAP_UNK);
646 	default:
647 		return (si_code);
648 	}
649 }
650 
651 static void
652 sicode_to_lsicode(int sig, int si_code, int *lsi_code)
653 {
654 
655 	switch (si_code) {
656 	case SI_USER:
657 		*lsi_code = LINUX_SI_USER;
658 		break;
659 	case SI_KERNEL:
660 		*lsi_code = LINUX_SI_KERNEL;
661 		break;
662 	case SI_QUEUE:
663 		*lsi_code = LINUX_SI_QUEUE;
664 		break;
665 	case SI_TIMER:
666 		*lsi_code = LINUX_SI_TIMER;
667 		break;
668 	case SI_MESGQ:
669 		*lsi_code = LINUX_SI_MESGQ;
670 		break;
671 	case SI_ASYNCIO:
672 		*lsi_code = LINUX_SI_ASYNCIO;
673 		break;
674 	case SI_LWP:
675 		*lsi_code = LINUX_SI_TKILL;
676 		break;
677 	default:
678 		switch (sig) {
679 		case LINUX_SIGFPE:
680 			*lsi_code = sigfpe_sicode2lsicode(si_code);
681 			break;
682 		case LINUX_SIGBUS:
683 			*lsi_code = sigbus_sicode2lsicode(si_code);
684 			break;
685 		case LINUX_SIGSEGV:
686 			*lsi_code = sigsegv_sicode2lsicode(si_code);
687 			break;
688 		case LINUX_SIGTRAP:
689 			*lsi_code = sigtrap_sicode2lsicode(si_code);
690 			break;
691 		default:
692 			*lsi_code = si_code;
693 			break;
694 		}
695 		break;
696 	}
697 }
698 
699 void
700 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
701 {
702 
703 	/* sig already converted */
704 	lsi->lsi_signo = sig;
705 	sicode_to_lsicode(sig, si->si_code, &lsi->lsi_code);
706 
707 	switch (si->si_code) {
708 	case SI_LWP:
709 		lsi->lsi_pid = si->si_pid;
710 		lsi->lsi_uid = si->si_uid;
711 		break;
712 
713 	case SI_TIMER:
714 		lsi->lsi_int = si->si_value.sival_int;
715 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
716 		lsi->lsi_tid = si->si_timerid;
717 		break;
718 
719 	case SI_QUEUE:
720 		lsi->lsi_pid = si->si_pid;
721 		lsi->lsi_uid = si->si_uid;
722 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
723 		break;
724 
725 	case SI_ASYNCIO:
726 		lsi->lsi_int = si->si_value.sival_int;
727 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
728 		break;
729 
730 	default:
731 		switch (sig) {
732 		case LINUX_SIGPOLL:
733 			/* XXX si_fd? */
734 			lsi->lsi_band = si->si_band;
735 			break;
736 
737 		case LINUX_SIGCHLD:
738 			lsi->lsi_errno = 0;
739 			lsi->lsi_pid = si->si_pid;
740 			lsi->lsi_uid = si->si_uid;
741 
742 			if (si->si_code == CLD_STOPPED || si->si_code == CLD_KILLED)
743 				lsi->lsi_status = bsd_to_linux_signal(si->si_status);
744 			else if (si->si_code == CLD_CONTINUED)
745 				lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
746 			else
747 				lsi->lsi_status = si->si_status;
748 			break;
749 
750 		case LINUX_SIGBUS:
751 		case LINUX_SIGILL:
752 		case LINUX_SIGFPE:
753 		case LINUX_SIGSEGV:
754 			lsi->lsi_addr = PTROUT(si->si_addr);
755 			break;
756 
757 		default:
758 			lsi->lsi_pid = si->si_pid;
759 			lsi->lsi_uid = si->si_uid;
760 			if (sig >= LINUX_SIGRTMIN) {
761 				lsi->lsi_int = si->si_value.sival_int;
762 				lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
763 			}
764 			break;
765 		}
766 		break;
767 	}
768 }
769 
770 int
771 lsiginfo_to_siginfo(struct thread *td, const l_siginfo_t *lsi,
772     siginfo_t *si, int sig)
773 {
774 
775 	switch (lsi->lsi_code) {
776 	case LINUX_SI_TKILL:
777 		if (linux_kernver(td) >= LINUX_KERNVER(2,6,39)) {
778 			linux_msg(td, "SI_TKILL forbidden since 2.6.39");
779 			return (EPERM);
780 		}
781 		si->si_code = SI_LWP;
782 	case LINUX_SI_QUEUE:
783 		si->si_code = SI_QUEUE;
784 		break;
785 	case LINUX_SI_TIMER:
786 		si->si_code = SI_TIMER;
787 		break;
788 	case LINUX_SI_MESGQ:
789 		si->si_code = SI_MESGQ;
790 		break;
791 	case LINUX_SI_ASYNCIO:
792 		si->si_code = SI_ASYNCIO;
793 		break;
794 	default:
795 		si->si_code = lsi->lsi_code;
796 		break;
797 	}
798 
799 	si->si_signo = sig;
800 	si->si_pid = td->td_proc->p_pid;
801 	si->si_uid = td->td_ucred->cr_ruid;
802 	si->si_value.sival_ptr = PTRIN(lsi->lsi_value.sival_ptr);
803 	return (0);
804 }
805 
806 int
807 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
808 {
809 	l_siginfo_t linfo;
810 	ksiginfo_t ksi;
811 	int error;
812 	int sig;
813 
814 	if (!LINUX_SIG_VALID(args->sig))
815 		return (EINVAL);
816 
817 	error = copyin(args->info, &linfo, sizeof(linfo));
818 	if (error != 0)
819 		return (error);
820 
821 	if (linfo.lsi_code >= 0)
822 		/* SI_USER, SI_KERNEL */
823 		return (EPERM);
824 
825 	sig = linux_to_bsd_signal(args->sig);
826 	ksiginfo_init(&ksi);
827 	error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
828 	if (error != 0)
829 		return (error);
830 
831 	return (linux_pksignal(td, args->pid, sig, &ksi));
832 }
833 
834 int
835 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
836 {
837 	l_siginfo_t linfo;
838 	ksiginfo_t ksi;
839 	int error;
840 	int sig;
841 
842 	if (!LINUX_SIG_VALID(args->sig))
843 		return (EINVAL);
844 
845 	error = copyin(args->uinfo, &linfo, sizeof(linfo));
846 	if (error != 0)
847 		return (error);
848 
849 	if (linfo.lsi_code >= 0)
850 		return (EPERM);
851 
852 	sig = linux_to_bsd_signal(args->sig);
853 	ksiginfo_init(&ksi);
854 	error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
855 	if (error != 0)
856 		return (error);
857 
858 	return (linux_tdksignal(td, args->tid, args->tgid, sig, &ksi));
859 }
860 
861 int
862 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
863 {
864 	sigset_t sigmask;
865 	int error;
866 
867 	error = linux_copyin_sigset(td, uap->newset, uap->sigsetsize,
868 	    &sigmask, NULL);
869 	if (error != 0)
870 		return (error);
871 
872 	return (kern_sigsuspend(td, sigmask));
873 }
874 
875 static int
876 linux_tdksignal(struct thread *td, lwpid_t tid, int tgid, int sig,
877     ksiginfo_t *ksi)
878 {
879 	struct thread *tdt;
880 	struct proc *p;
881 	int error;
882 
883 	tdt = linux_tdfind(td, tid, tgid);
884 	if (tdt == NULL)
885 		return (ESRCH);
886 
887 	p = tdt->td_proc;
888 	AUDIT_ARG_SIGNUM(sig);
889 	AUDIT_ARG_PID(p->p_pid);
890 	AUDIT_ARG_PROCESS(p);
891 
892 	error = p_cansignal(td, p, sig);
893 	if (error != 0 || sig == 0)
894 		goto out;
895 
896 	tdksignal(tdt, sig, ksi);
897 
898 out:
899 	PROC_UNLOCK(p);
900 	return (error);
901 }
902 
903 static int
904 linux_tdsignal(struct thread *td, lwpid_t tid, int tgid, int sig)
905 {
906 	ksiginfo_t ksi;
907 
908 	ksiginfo_init(&ksi);
909 	ksi.ksi_signo = sig;
910 	ksi.ksi_code = SI_LWP;
911 	ksi.ksi_pid = td->td_proc->p_pid;
912 	ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
913 	return (linux_tdksignal(td, tid, tgid, sig, &ksi));
914 }
915 
916 static int
917 linux_pksignal(struct thread *td, int pid, int sig, ksiginfo_t *ksi)
918 {
919 	struct thread *tdt;
920 	struct proc *p;
921 	int error;
922 
923 	tdt = linux_tdfind(td, pid, -1);
924 	if (tdt == NULL)
925 		return (ESRCH);
926 
927 	p = tdt->td_proc;
928 	AUDIT_ARG_SIGNUM(sig);
929 	AUDIT_ARG_PID(p->p_pid);
930 	AUDIT_ARG_PROCESS(p);
931 
932 	error = p_cansignal(td, p, sig);
933 	if (error != 0 || sig == 0)
934 		goto out;
935 
936 	pksignal(p, sig, ksi);
937 
938 out:
939 	PROC_UNLOCK(p);
940 	return (error);
941 }
942 
943 static int
944 linux_psignal(struct thread *td, int pid, int sig)
945 {
946 	ksiginfo_t ksi;
947 
948 	ksiginfo_init(&ksi);
949 	ksi.ksi_signo = sig;
950 	ksi.ksi_code = SI_LWP;
951 	ksi.ksi_pid = td->td_proc->p_pid;
952 	ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
953 	return (linux_pksignal(td, pid, sig, &ksi));
954 }
955 
956 int
957 linux_copyin_sigset(struct thread *td, l_sigset_t *lset,
958     l_size_t sigsetsize, sigset_t *set, sigset_t **pset)
959 {
960 	l_sigset_t lmask;
961 	int error;
962 
963 	if (sigsetsize != sizeof(l_sigset_t))
964 		return (EINVAL);
965 	if (lset != NULL) {
966 		error = copyin(lset, &lmask, sizeof(lmask));
967 		if (error != 0)
968 			return (error);
969 		linux_to_bsd_sigset(&lmask, set);
970 		if (pset != NULL)
971 			*pset = set;
972 #ifdef KTRACE
973 		if (KTRPOINT(td, KTR_STRUCT))
974 			linux_ktrsigset(&lmask, sizeof(lmask));
975 #endif
976 	} else if (pset != NULL)
977 		*pset = NULL;
978 	return (0);
979 }
980