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