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