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