xref: /freebsd/sys/compat/linux/linux_signal.c (revision 690b7ea081790eef2c890f63a4fe7e195cf51df0)
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 	struct l_timespec lts;
440 	int error;
441 
442 	if (args->timeout) {
443 		if ((error = copyin(args->timeout, &lts, sizeof(lts))))
444 			return (error);
445 		error = linux_to_native_timespec(&ts, &lts);
446 		if (error != 0)
447 			return (error);
448 		tsa = &ts;
449 	} else
450 		tsa = NULL;
451 
452 	return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
453 	    args->ptr, args->sigsetsize));
454 }
455 
456 static int
457 linux_common_rt_sigtimedwait(struct thread *td, l_sigset_t *mask,
458     struct timespec *tsa, l_siginfo_t *ptr, l_size_t sigsetsize)
459 {
460 	int error, sig;
461 	sigset_t bset;
462 	l_siginfo_t lsi;
463 	ksiginfo_t ksi;
464 
465 	error = linux_copyin_sigset(mask, sigsetsize, &bset, NULL);
466 	if (error != 0)
467 		return (error);
468 
469 	ksiginfo_init(&ksi);
470 	error = kern_sigtimedwait(td, bset, &ksi, tsa);
471 	if (error)
472 		return (error);
473 
474 	sig = bsd_to_linux_signal(ksi.ksi_signo);
475 
476 	if (ptr) {
477 		memset(&lsi, 0, sizeof(lsi));
478 		siginfo_to_lsiginfo(&ksi.ksi_info, &lsi, sig);
479 		error = copyout(&lsi, ptr, sizeof(lsi));
480 	}
481 	if (error == 0)
482 		td->td_retval[0] = sig;
483 
484 	return (error);
485 }
486 
487 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
488 int
489 linux_rt_sigtimedwait_time64(struct thread *td,
490 	struct linux_rt_sigtimedwait_time64_args *args)
491 {
492 	struct timespec ts, *tsa;
493 	struct l_timespec64 lts;
494 	int error;
495 
496 	if (args->timeout) {
497 		if ((error = copyin(args->timeout, &lts, sizeof(lts))))
498 			return (error);
499 		error = linux_to_native_timespec64(&ts, &lts);
500 		if (error != 0)
501 			return (error);
502 		tsa = &ts;
503 	} else
504 		tsa = NULL;
505 
506 	return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
507 	    args->ptr, args->sigsetsize));
508 }
509 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
510 
511 int
512 linux_kill(struct thread *td, struct linux_kill_args *args)
513 {
514 	int sig;
515 
516 	/*
517 	 * Allow signal 0 as a means to check for privileges
518 	 */
519 	if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
520 		return (EINVAL);
521 
522 	if (args->signum > 0)
523 		sig = linux_to_bsd_signal(args->signum);
524 	else
525 		sig = 0;
526 
527 	if (args->pid > PID_MAX)
528 		return (linux_psignal(td, args->pid, sig));
529 	else
530 		return (kern_kill(td, args->pid, sig));
531 }
532 
533 int
534 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
535 {
536 	int sig;
537 
538 	if (args->pid <= 0 || args->tgid <=0)
539 		return (EINVAL);
540 
541 	/*
542 	 * Allow signal 0 as a means to check for privileges
543 	 */
544 	if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
545 		return (EINVAL);
546 
547 	if (args->sig > 0)
548 		sig = linux_to_bsd_signal(args->sig);
549 	else
550 		sig = 0;
551 
552 	return (linux_tdsignal(td, args->pid, args->tgid, sig));
553 }
554 
555 /*
556  * Deprecated since 2.5.75. Replaced by tgkill().
557  */
558 int
559 linux_tkill(struct thread *td, struct linux_tkill_args *args)
560 {
561 	int sig;
562 
563 	if (args->tid <= 0)
564 		return (EINVAL);
565 
566 	if (!LINUX_SIG_VALID(args->sig))
567 		return (EINVAL);
568 
569 	sig = linux_to_bsd_signal(args->sig);
570 
571 	return (linux_tdsignal(td, args->tid, -1, sig));
572 }
573 
574 static void
575 sicode_to_lsicode(int si_code, int *lsi_code)
576 {
577 
578 	switch (si_code) {
579 	case SI_USER:
580 		*lsi_code = LINUX_SI_USER;
581 		break;
582 	case SI_KERNEL:
583 		*lsi_code = LINUX_SI_KERNEL;
584 		break;
585 	case SI_QUEUE:
586 		*lsi_code = LINUX_SI_QUEUE;
587 		break;
588 	case SI_TIMER:
589 		*lsi_code = LINUX_SI_TIMER;
590 		break;
591 	case SI_MESGQ:
592 		*lsi_code = LINUX_SI_MESGQ;
593 		break;
594 	case SI_ASYNCIO:
595 		*lsi_code = LINUX_SI_ASYNCIO;
596 		break;
597 	case SI_LWP:
598 		*lsi_code = LINUX_SI_TKILL;
599 		break;
600 	default:
601 		*lsi_code = si_code;
602 		break;
603 	}
604 }
605 
606 void
607 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
608 {
609 
610 	/* sig alredy converted */
611 	lsi->lsi_signo = sig;
612 	sicode_to_lsicode(si->si_code, &lsi->lsi_code);
613 
614 	switch (si->si_code) {
615 	case SI_LWP:
616 		lsi->lsi_pid = si->si_pid;
617 		lsi->lsi_uid = si->si_uid;
618 		break;
619 
620 	case SI_TIMER:
621 		lsi->lsi_int = si->si_value.sival_int;
622 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
623 		lsi->lsi_tid = si->si_timerid;
624 		break;
625 
626 	case SI_QUEUE:
627 		lsi->lsi_pid = si->si_pid;
628 		lsi->lsi_uid = si->si_uid;
629 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
630 		break;
631 
632 	case SI_ASYNCIO:
633 		lsi->lsi_int = si->si_value.sival_int;
634 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
635 		break;
636 
637 	default:
638 		switch (sig) {
639 		case LINUX_SIGPOLL:
640 			/* XXX si_fd? */
641 			lsi->lsi_band = si->si_band;
642 			break;
643 
644 		case LINUX_SIGCHLD:
645 			lsi->lsi_errno = 0;
646 			lsi->lsi_pid = si->si_pid;
647 			lsi->lsi_uid = si->si_uid;
648 
649 			if (si->si_code == CLD_STOPPED || si->si_code == CLD_KILLED)
650 				lsi->lsi_status = bsd_to_linux_signal(si->si_status);
651 			else if (si->si_code == CLD_CONTINUED)
652 				lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
653 			else
654 				lsi->lsi_status = si->si_status;
655 			break;
656 
657 		case LINUX_SIGBUS:
658 		case LINUX_SIGILL:
659 		case LINUX_SIGFPE:
660 		case LINUX_SIGSEGV:
661 			lsi->lsi_addr = PTROUT(si->si_addr);
662 			break;
663 
664 		default:
665 			lsi->lsi_pid = si->si_pid;
666 			lsi->lsi_uid = si->si_uid;
667 			if (sig >= LINUX_SIGRTMIN) {
668 				lsi->lsi_int = si->si_value.sival_int;
669 				lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
670 			}
671 			break;
672 		}
673 		break;
674 	}
675 }
676 
677 int
678 lsiginfo_to_siginfo(struct thread *td, const l_siginfo_t *lsi,
679     siginfo_t *si, int sig)
680 {
681 
682 	switch (lsi->lsi_code) {
683 	case LINUX_SI_TKILL:
684 		if (linux_kernver(td) >= LINUX_KERNVER_2006039) {
685 			linux_msg(td, "SI_TKILL forbidden since 2.6.39");
686 			return (EPERM);
687 		}
688 		si->si_code = SI_LWP;
689 	case LINUX_SI_QUEUE:
690 		si->si_code = SI_QUEUE;
691 		break;
692 	case LINUX_SI_TIMER:
693 		si->si_code = SI_TIMER;
694 		break;
695 	case LINUX_SI_MESGQ:
696 		si->si_code = SI_MESGQ;
697 		break;
698 	case LINUX_SI_ASYNCIO:
699 		si->si_code = SI_ASYNCIO;
700 		break;
701 	default:
702 		si->si_code = lsi->lsi_code;
703 		break;
704 	}
705 
706 	si->si_signo = sig;
707 	si->si_pid = td->td_proc->p_pid;
708 	si->si_uid = td->td_ucred->cr_ruid;
709 	si->si_value.sival_ptr = PTRIN(lsi->lsi_value.sival_ptr);
710 	return (0);
711 }
712 
713 int
714 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
715 {
716 	l_siginfo_t linfo;
717 	ksiginfo_t ksi;
718 	int error;
719 	int sig;
720 
721 	if (!LINUX_SIG_VALID(args->sig))
722 		return (EINVAL);
723 
724 	error = copyin(args->info, &linfo, sizeof(linfo));
725 	if (error != 0)
726 		return (error);
727 
728 	if (linfo.lsi_code >= 0)
729 		/* SI_USER, SI_KERNEL */
730 		return (EPERM);
731 
732 	sig = linux_to_bsd_signal(args->sig);
733 	ksiginfo_init(&ksi);
734 	error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
735 	if (error != 0)
736 		return (error);
737 
738 	return (linux_pksignal(td, args->pid, sig, &ksi));
739 }
740 
741 int
742 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
743 {
744 	l_siginfo_t linfo;
745 	ksiginfo_t ksi;
746 	int error;
747 	int sig;
748 
749 	if (!LINUX_SIG_VALID(args->sig))
750 		return (EINVAL);
751 
752 	error = copyin(args->uinfo, &linfo, sizeof(linfo));
753 	if (error != 0)
754 		return (error);
755 
756 	if (linfo.lsi_code >= 0)
757 		return (EPERM);
758 
759 	sig = linux_to_bsd_signal(args->sig);
760 	ksiginfo_init(&ksi);
761 	error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
762 	if (error != 0)
763 		return (error);
764 
765 	return (linux_tdksignal(td, args->tid, args->tgid, sig, &ksi));
766 }
767 
768 int
769 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
770 {
771 	sigset_t sigmask;
772 	int error;
773 
774 	error = linux_copyin_sigset(uap->newset, uap->sigsetsize,
775 	    &sigmask, NULL);
776 	if (error != 0)
777 		return (error);
778 
779 	return (kern_sigsuspend(td, sigmask));
780 }
781 
782 static int
783 linux_tdksignal(struct thread *td, lwpid_t tid, int tgid, int sig,
784     ksiginfo_t *ksi)
785 {
786 	struct thread *tdt;
787 	struct proc *p;
788 	int error;
789 
790 	tdt = linux_tdfind(td, tid, tgid);
791 	if (tdt == NULL)
792 		return (ESRCH);
793 
794 	p = tdt->td_proc;
795 	AUDIT_ARG_SIGNUM(sig);
796 	AUDIT_ARG_PID(p->p_pid);
797 	AUDIT_ARG_PROCESS(p);
798 
799 	error = p_cansignal(td, p, sig);
800 	if (error != 0 || sig == 0)
801 		goto out;
802 
803 	tdksignal(tdt, sig, ksi);
804 
805 out:
806 	PROC_UNLOCK(p);
807 	return (error);
808 }
809 
810 static int
811 linux_tdsignal(struct thread *td, lwpid_t tid, int tgid, int sig)
812 {
813 	ksiginfo_t ksi;
814 
815 	ksiginfo_init(&ksi);
816 	ksi.ksi_signo = sig;
817 	ksi.ksi_code = SI_LWP;
818 	ksi.ksi_pid = td->td_proc->p_pid;
819 	ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
820 	return (linux_tdksignal(td, tid, tgid, sig, &ksi));
821 }
822 
823 static int
824 linux_pksignal(struct thread *td, int pid, int sig, ksiginfo_t *ksi)
825 {
826 	struct thread *tdt;
827 	struct proc *p;
828 	int error;
829 
830 	tdt = linux_tdfind(td, pid, -1);
831 	if (tdt == NULL)
832 		return (ESRCH);
833 
834 	p = tdt->td_proc;
835 	AUDIT_ARG_SIGNUM(sig);
836 	AUDIT_ARG_PID(p->p_pid);
837 	AUDIT_ARG_PROCESS(p);
838 
839 	error = p_cansignal(td, p, sig);
840 	if (error != 0 || sig == 0)
841 		goto out;
842 
843 	pksignal(p, sig, ksi);
844 
845 out:
846 	PROC_UNLOCK(p);
847 	return (error);
848 }
849 
850 static int
851 linux_psignal(struct thread *td, int pid, int sig)
852 {
853 	ksiginfo_t ksi;
854 
855 	ksiginfo_init(&ksi);
856 	ksi.ksi_signo = sig;
857 	ksi.ksi_code = SI_LWP;
858 	ksi.ksi_pid = td->td_proc->p_pid;
859 	ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
860 	return (linux_pksignal(td, pid, sig, &ksi));
861 }
862 
863 int
864 linux_copyin_sigset(l_sigset_t *lset, l_size_t sigsetsize, sigset_t *set,
865     sigset_t **pset)
866 {
867 	l_sigset_t lmask;
868 	int error;
869 
870 	if (sigsetsize != sizeof(l_sigset_t))
871 		return (EINVAL);
872 	if (lset != NULL) {
873 		error = copyin(lset, &lmask, sizeof(l_sigset_t));
874 		if (error != 0)
875 			return (error);
876 		linux_to_bsd_sigset(&lmask, set);
877 		if (pset != NULL)
878 			*pset = set;
879 	} else if (pset != NULL)
880 		*pset = NULL;
881 	return (0);
882 }
883