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