xref: /freebsd/sys/compat/linux/linux_signal.c (revision 489ba2223676ec251ab1bfe2906d2a62959c8ce3)
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 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 void
569 sicode_to_lsicode(int si_code, int *lsi_code)
570 {
571 
572 	switch (si_code) {
573 	case SI_USER:
574 		*lsi_code = LINUX_SI_USER;
575 		break;
576 	case SI_KERNEL:
577 		*lsi_code = LINUX_SI_KERNEL;
578 		break;
579 	case SI_QUEUE:
580 		*lsi_code = LINUX_SI_QUEUE;
581 		break;
582 	case SI_TIMER:
583 		*lsi_code = LINUX_SI_TIMER;
584 		break;
585 	case SI_MESGQ:
586 		*lsi_code = LINUX_SI_MESGQ;
587 		break;
588 	case SI_ASYNCIO:
589 		*lsi_code = LINUX_SI_ASYNCIO;
590 		break;
591 	case SI_LWP:
592 		*lsi_code = LINUX_SI_TKILL;
593 		break;
594 	default:
595 		*lsi_code = si_code;
596 		break;
597 	}
598 }
599 
600 void
601 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
602 {
603 
604 	/* sig alredy converted */
605 	lsi->lsi_signo = sig;
606 	sicode_to_lsicode(si->si_code, &lsi->lsi_code);
607 
608 	switch (si->si_code) {
609 	case SI_LWP:
610 		lsi->lsi_pid = si->si_pid;
611 		lsi->lsi_uid = si->si_uid;
612 		break;
613 
614 	case SI_TIMER:
615 		lsi->lsi_int = si->si_value.sival_int;
616 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
617 		lsi->lsi_tid = si->si_timerid;
618 		break;
619 
620 	case SI_QUEUE:
621 		lsi->lsi_pid = si->si_pid;
622 		lsi->lsi_uid = si->si_uid;
623 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
624 		break;
625 
626 	case SI_ASYNCIO:
627 		lsi->lsi_int = si->si_value.sival_int;
628 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
629 		break;
630 
631 	default:
632 		switch (sig) {
633 		case LINUX_SIGPOLL:
634 			/* XXX si_fd? */
635 			lsi->lsi_band = si->si_band;
636 			break;
637 
638 		case LINUX_SIGCHLD:
639 			lsi->lsi_errno = 0;
640 			lsi->lsi_pid = si->si_pid;
641 			lsi->lsi_uid = si->si_uid;
642 
643 			if (si->si_code == CLD_STOPPED || si->si_code == CLD_KILLED)
644 				lsi->lsi_status = bsd_to_linux_signal(si->si_status);
645 			else if (si->si_code == CLD_CONTINUED)
646 				lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
647 			else
648 				lsi->lsi_status = si->si_status;
649 			break;
650 
651 		case LINUX_SIGBUS:
652 		case LINUX_SIGILL:
653 		case LINUX_SIGFPE:
654 		case LINUX_SIGSEGV:
655 			lsi->lsi_addr = PTROUT(si->si_addr);
656 			break;
657 
658 		default:
659 			lsi->lsi_pid = si->si_pid;
660 			lsi->lsi_uid = si->si_uid;
661 			if (sig >= LINUX_SIGRTMIN) {
662 				lsi->lsi_int = si->si_value.sival_int;
663 				lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
664 			}
665 			break;
666 		}
667 		break;
668 	}
669 }
670 
671 int
672 lsiginfo_to_siginfo(struct thread *td, const l_siginfo_t *lsi,
673     siginfo_t *si, int sig)
674 {
675 
676 	switch (lsi->lsi_code) {
677 	case LINUX_SI_TKILL:
678 		if (linux_kernver(td) >= LINUX_KERNVER_2006039) {
679 			linux_msg(td, "SI_TKILL forbidden since 2.6.39");
680 			return (EPERM);
681 		}
682 		si->si_code = SI_LWP;
683 	case LINUX_SI_QUEUE:
684 		si->si_code = SI_QUEUE;
685 		break;
686 	case LINUX_SI_TIMER:
687 		si->si_code = SI_TIMER;
688 		break;
689 	case LINUX_SI_MESGQ:
690 		si->si_code = SI_MESGQ;
691 		break;
692 	case LINUX_SI_ASYNCIO:
693 		si->si_code = SI_ASYNCIO;
694 		break;
695 	default:
696 		si->si_code = lsi->lsi_code;
697 		break;
698 	}
699 
700 	si->si_signo = sig;
701 	si->si_pid = td->td_proc->p_pid;
702 	si->si_uid = td->td_ucred->cr_ruid;
703 	si->si_value.sival_ptr = PTRIN(lsi->lsi_value.sival_ptr);
704 	return (0);
705 }
706 
707 int
708 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
709 {
710 	l_siginfo_t linfo;
711 	ksiginfo_t ksi;
712 	int error;
713 	int sig;
714 
715 	if (!LINUX_SIG_VALID(args->sig))
716 		return (EINVAL);
717 
718 	error = copyin(args->info, &linfo, sizeof(linfo));
719 	if (error != 0)
720 		return (error);
721 
722 	if (linfo.lsi_code >= 0)
723 		/* SI_USER, SI_KERNEL */
724 		return (EPERM);
725 
726 	sig = linux_to_bsd_signal(args->sig);
727 	ksiginfo_init(&ksi);
728 	error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
729 	if (error != 0)
730 		return (error);
731 
732 	return (linux_pksignal(td, args->pid, sig, &ksi));
733 }
734 
735 int
736 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
737 {
738 	l_siginfo_t linfo;
739 	ksiginfo_t ksi;
740 	int error;
741 	int sig;
742 
743 	if (!LINUX_SIG_VALID(args->sig))
744 		return (EINVAL);
745 
746 	error = copyin(args->uinfo, &linfo, sizeof(linfo));
747 	if (error != 0)
748 		return (error);
749 
750 	if (linfo.lsi_code >= 0)
751 		return (EPERM);
752 
753 	sig = linux_to_bsd_signal(args->sig);
754 	ksiginfo_init(&ksi);
755 	error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
756 	if (error != 0)
757 		return (error);
758 
759 	return (linux_tdksignal(td, args->tid, args->tgid, sig, &ksi));
760 }
761 
762 int
763 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
764 {
765 	sigset_t sigmask;
766 	int error;
767 
768 	error = linux_copyin_sigset(uap->newset, uap->sigsetsize,
769 	    &sigmask, NULL);
770 	if (error != 0)
771 		return (error);
772 
773 	return (kern_sigsuspend(td, sigmask));
774 }
775 
776 static int
777 linux_tdksignal(struct thread *td, lwpid_t tid, int tgid, int sig,
778     ksiginfo_t *ksi)
779 {
780 	struct thread *tdt;
781 	struct proc *p;
782 	int error;
783 
784 	tdt = linux_tdfind(td, tid, tgid);
785 	if (tdt == NULL)
786 		return (ESRCH);
787 
788 	p = tdt->td_proc;
789 	AUDIT_ARG_SIGNUM(sig);
790 	AUDIT_ARG_PID(p->p_pid);
791 	AUDIT_ARG_PROCESS(p);
792 
793 	error = p_cansignal(td, p, sig);
794 	if (error != 0 || sig == 0)
795 		goto out;
796 
797 	tdksignal(tdt, sig, ksi);
798 
799 out:
800 	PROC_UNLOCK(p);
801 	return (error);
802 }
803 
804 static int
805 linux_tdsignal(struct thread *td, lwpid_t tid, int tgid, int sig)
806 {
807 	ksiginfo_t ksi;
808 
809 	ksiginfo_init(&ksi);
810 	ksi.ksi_signo = sig;
811 	ksi.ksi_code = SI_LWP;
812 	ksi.ksi_pid = td->td_proc->p_pid;
813 	ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
814 	return (linux_tdksignal(td, tid, tgid, sig, &ksi));
815 }
816 
817 static int
818 linux_pksignal(struct thread *td, int pid, int sig, ksiginfo_t *ksi)
819 {
820 	struct thread *tdt;
821 	struct proc *p;
822 	int error;
823 
824 	tdt = linux_tdfind(td, pid, -1);
825 	if (tdt == NULL)
826 		return (ESRCH);
827 
828 	p = tdt->td_proc;
829 	AUDIT_ARG_SIGNUM(sig);
830 	AUDIT_ARG_PID(p->p_pid);
831 	AUDIT_ARG_PROCESS(p);
832 
833 	error = p_cansignal(td, p, sig);
834 	if (error != 0 || sig == 0)
835 		goto out;
836 
837 	pksignal(p, sig, ksi);
838 
839 out:
840 	PROC_UNLOCK(p);
841 	return (error);
842 }
843 
844 static int
845 linux_psignal(struct thread *td, int pid, int sig)
846 {
847 	ksiginfo_t ksi;
848 
849 	ksiginfo_init(&ksi);
850 	ksi.ksi_signo = sig;
851 	ksi.ksi_code = SI_LWP;
852 	ksi.ksi_pid = td->td_proc->p_pid;
853 	ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
854 	return (linux_pksignal(td, pid, sig, &ksi));
855 }
856 
857 int
858 linux_copyin_sigset(l_sigset_t *lset, l_size_t sigsetsize, sigset_t *set,
859     sigset_t **pset)
860 {
861 	l_sigset_t lmask;
862 	int error;
863 
864 	if (sigsetsize != sizeof(l_sigset_t))
865 		return (EINVAL);
866 	if (lset != NULL) {
867 		error = copyin(lset, &lmask, sizeof(l_sigset_t));
868 		if (error != 0)
869 			return (error);
870 		linux_to_bsd_sigset(&lmask, set);
871 		if (pset != NULL)
872 			*pset = set;
873 	} else if (pset != NULL)
874 		*pset = NULL;
875 	return (0);
876 }
877