2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994-1995 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/mutex.h>
40 #include <sys/signalvar.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysproto.h>
44 #include <security/audit/audit.h>
46 #include "opt_compat.h"
49 #include <machine/../linux32/linux.h>
50 #include <machine/../linux32/linux32_proto.h>
52 #include <machine/../linux/linux.h>
53 #include <machine/../linux/linux_proto.h>
55 #include <compat/linux/linux_signal.h>
56 #include <compat/linux/linux_util.h>
57 #include <compat/linux/linux_emul.h>
58 #include <compat/linux/linux_misc.h>
60 static int linux_do_tkill(struct thread *td, struct thread *tdt,
62 static void sicode_to_lsicode(int si_code, int *lsi_code);
66 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
69 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
70 bsa->sa_handler = PTRIN(lsa->lsa_handler);
72 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
73 bsa->sa_flags |= SA_NOCLDSTOP;
74 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
75 bsa->sa_flags |= SA_NOCLDWAIT;
76 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
77 bsa->sa_flags |= SA_SIGINFO;
78 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
79 bsa->sa_flags |= SA_ONSTACK;
80 if (lsa->lsa_flags & LINUX_SA_RESTART)
81 bsa->sa_flags |= SA_RESTART;
82 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
83 bsa->sa_flags |= SA_RESETHAND;
84 if (lsa->lsa_flags & LINUX_SA_NOMASK)
85 bsa->sa_flags |= SA_NODEFER;
89 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
92 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
94 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
96 lsa->lsa_handler = bsa->sa_handler;
98 lsa->lsa_restorer = 0; /* unsupported */
100 if (bsa->sa_flags & SA_NOCLDSTOP)
101 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
102 if (bsa->sa_flags & SA_NOCLDWAIT)
103 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
104 if (bsa->sa_flags & SA_SIGINFO)
105 lsa->lsa_flags |= LINUX_SA_SIGINFO;
106 if (bsa->sa_flags & SA_ONSTACK)
107 lsa->lsa_flags |= LINUX_SA_ONSTACK;
108 if (bsa->sa_flags & SA_RESTART)
109 lsa->lsa_flags |= LINUX_SA_RESTART;
110 if (bsa->sa_flags & SA_RESETHAND)
111 lsa->lsa_flags |= LINUX_SA_ONESHOT;
112 if (bsa->sa_flags & SA_NODEFER)
113 lsa->lsa_flags |= LINUX_SA_NOMASK;
117 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
118 l_sigaction_t *linux_osa)
120 struct sigaction act, oact, *nsa, *osa;
123 if (!LINUX_SIG_VALID(linux_sig))
126 osa = (linux_osa != NULL) ? &oact : NULL;
127 if (linux_nsa != NULL) {
129 linux_to_bsd_sigaction(linux_nsa, nsa);
132 sig = linux_to_bsd_signal(linux_sig);
134 error = kern_sigaction(td, sig, nsa, osa, 0);
138 if (linux_osa != NULL)
139 bsd_to_linux_sigaction(osa, linux_osa);
144 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
146 linux_signal(struct thread *td, struct linux_signal_args *args)
148 l_sigaction_t nsa, osa;
153 printf(ARGS(signal, "%d, %p"),
154 args->sig, (void *)(uintptr_t)args->handler);
157 nsa.lsa_handler = args->handler;
158 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
159 LINUX_SIGEMPTYSET(nsa.lsa_mask);
161 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
162 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
166 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
169 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
171 l_sigaction_t nsa, osa;
175 if (ldebug(rt_sigaction))
176 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
177 (long)args->sig, (void *)args->act,
178 (void *)args->oact, (long)args->sigsetsize);
181 if (args->sigsetsize != sizeof(l_sigset_t))
184 if (args->act != NULL) {
185 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
190 error = linux_do_sigaction(td, args->sig,
191 args->act ? &nsa : NULL,
192 args->oact ? &osa : NULL);
194 if (args->oact != NULL && !error) {
195 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
202 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
205 sigset_t omask, nmask;
209 td->td_retval[0] = 0;
212 case LINUX_SIG_BLOCK:
215 case LINUX_SIG_UNBLOCK:
218 case LINUX_SIG_SETMASK:
225 linux_to_bsd_sigset(new, &nmask);
229 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
230 if (error == 0 && old != NULL)
231 bsd_to_linux_sigset(&omask, old);
236 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
238 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
241 l_sigset_t set, oset;
245 if (ldebug(sigprocmask))
246 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
249 if (args->mask != NULL) {
250 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
253 LINUX_SIGEMPTYSET(set);
257 error = linux_do_sigprocmask(td, args->how,
258 args->mask ? &set : NULL,
259 args->omask ? &oset : NULL);
261 if (args->omask != NULL && !error) {
263 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
268 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
271 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
273 l_sigset_t set, oset;
277 if (ldebug(rt_sigprocmask))
278 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
279 args->how, (void *)args->mask,
280 (void *)args->omask, (long)args->sigsetsize);
283 if (args->sigsetsize != sizeof(l_sigset_t))
286 if (args->mask != NULL) {
287 error = copyin(args->mask, &set, sizeof(l_sigset_t));
292 error = linux_do_sigprocmask(td, args->how,
293 args->mask ? &set : NULL,
294 args->omask ? &oset : NULL);
296 if (args->omask != NULL && !error) {
297 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
303 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
305 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
307 struct proc *p = td->td_proc;
311 if (ldebug(sgetmask))
312 printf(ARGS(sgetmask, ""));
316 bsd_to_linux_sigset(&td->td_sigmask, &mask);
318 td->td_retval[0] = mask.__mask;
323 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
325 struct proc *p = td->td_proc;
330 if (ldebug(ssetmask))
331 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
335 bsd_to_linux_sigset(&td->td_sigmask, &lset);
336 td->td_retval[0] = lset.__mask;
337 LINUX_SIGEMPTYSET(lset);
338 lset.__mask = args->mask;
339 linux_to_bsd_sigset(&lset, &bset);
340 td->td_sigmask = bset;
341 SIG_CANTMASK(td->td_sigmask);
348 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
350 struct proc *p = td->td_proc;
356 if (ldebug(sigpending))
357 printf(ARGS(sigpending, "*"));
362 SIGSETOR(bset, td->td_siglist);
363 SIGSETAND(bset, td->td_sigmask);
365 bsd_to_linux_sigset(&bset, &lset);
367 return (copyout(&mask, args->mask, sizeof(mask)));
369 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
375 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
377 struct proc *p = td->td_proc;
381 if (args->sigsetsize > sizeof(lset))
386 if (ldebug(rt_sigpending))
387 printf(ARGS(rt_sigpending, "*"));
392 SIGSETOR(bset, td->td_siglist);
393 SIGSETAND(bset, td->td_sigmask);
395 bsd_to_linux_sigset(&bset, &lset);
396 return (copyout(&lset, args->set, args->sigsetsize));
403 linux_rt_sigtimedwait(struct thread *td,
404 struct linux_rt_sigtimedwait_args *args)
409 struct timespec ts, *tsa;
416 if (ldebug(rt_sigtimedwait))
417 printf(ARGS(rt_sigtimedwait, "*"));
419 if (args->sigsetsize != sizeof(l_sigset_t))
422 if ((error = copyin(args->mask, &lset, sizeof(lset))))
424 linux_to_bsd_sigset(&lset, &bset);
428 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
431 if (ldebug(rt_sigtimedwait))
432 printf(LMSG("linux_rt_sigtimedwait: "
433 "incoming timeout (%jd/%jd)\n"),
434 (intmax_t)ltv.tv_sec, (intmax_t)ltv.tv_usec);
436 tv.tv_sec = (long)ltv.tv_sec;
437 tv.tv_usec = (suseconds_t)ltv.tv_usec;
438 if (itimerfix(&tv)) {
440 * The timeout was invalid. Convert it to something
441 * valid that will act as it does under Linux.
443 tv.tv_sec += tv.tv_usec / 1000000;
444 tv.tv_usec %= 1000000;
445 if (tv.tv_usec < 0) {
447 tv.tv_usec += 1000000;
452 if (ldebug(rt_sigtimedwait))
453 printf(LMSG("linux_rt_sigtimedwait: "
454 "converted timeout (%jd/%ld)\n"),
455 (intmax_t)tv.tv_sec, tv.tv_usec);
458 TIMEVAL_TO_TIMESPEC(&tv, &ts);
461 error = kern_sigtimedwait(td, bset, &info, tsa);
463 if (ldebug(rt_sigtimedwait))
464 printf(LMSG("linux_rt_sigtimedwait: "
465 "sigtimedwait returning (%d)\n"), error);
470 sig = bsd_to_linux_signal(info.ksi_signo);
473 memset(&linfo, 0, sizeof(linfo));
474 ksiginfo_to_lsiginfo(&info, &linfo, sig);
475 error = copyout(&linfo, args->ptr, sizeof(linfo));
478 td->td_retval[0] = sig;
484 linux_kill(struct thread *td, struct linux_kill_args *args)
486 struct kill_args /* {
493 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
497 * Allow signal 0 as a means to check for privileges
499 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
502 if (args->signum > 0)
503 tmp.signum = linux_to_bsd_signal(args->signum);
508 return (sys_kill(td, &tmp));
512 linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi)
518 AUDIT_ARG_SIGNUM(ksi->ksi_signo);
519 AUDIT_ARG_PID(p->p_pid);
520 AUDIT_ARG_PROCESS(p);
522 error = p_cansignal(td, p, ksi->ksi_signo);
523 if (error != 0 || ksi->ksi_signo == 0)
526 tdksignal(tdt, ksi->ksi_signo, ksi);
534 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
542 printf(ARGS(tgkill, "%d, %d, %d"),
543 args->tgid, args->pid, args->sig);
546 if (args->pid <= 0 || args->tgid <=0)
550 * Allow signal 0 as a means to check for privileges
552 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
556 sig = linux_to_bsd_signal(args->sig);
560 tdt = linux_tdfind(td, args->pid, args->tgid);
566 ksi.ksi_code = SI_LWP;
568 ksi.ksi_pid = td->td_proc->p_pid;
569 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
570 return (linux_do_tkill(td, tdt, &ksi));
574 * Deprecated since 2.5.75. Replaced by tgkill().
577 linux_tkill(struct thread *td, struct linux_tkill_args *args)
585 printf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
590 if (!LINUX_SIG_VALID(args->sig))
593 sig = linux_to_bsd_signal(args->sig);
595 tdt = linux_tdfind(td, args->tid, -1);
601 ksi.ksi_code = SI_LWP;
603 ksi.ksi_pid = td->td_proc->p_pid;
604 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
605 return (linux_do_tkill(td, tdt, &ksi));
609 ksiginfo_to_lsiginfo(const ksiginfo_t *ksi, l_siginfo_t *lsi, l_int sig)
612 siginfo_to_lsiginfo(&ksi->ksi_info, lsi, sig);
616 sicode_to_lsicode(int si_code, int *lsi_code)
621 *lsi_code = LINUX_SI_USER;
624 *lsi_code = LINUX_SI_KERNEL;
627 *lsi_code = LINUX_SI_QUEUE;
630 *lsi_code = LINUX_SI_TIMER;
633 *lsi_code = LINUX_SI_MESGQ;
636 *lsi_code = LINUX_SI_ASYNCIO;
639 *lsi_code = LINUX_SI_TKILL;
648 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
651 /* sig alredy converted */
652 lsi->lsi_signo = sig;
653 sicode_to_lsicode(si->si_code, &lsi->lsi_code);
655 switch (si->si_code) {
657 lsi->lsi_pid = si->si_pid;
658 lsi->lsi_uid = si->si_uid;
662 lsi->lsi_int = si->si_value.sival_int;
663 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
664 lsi->lsi_tid = si->si_timerid;
668 lsi->lsi_pid = si->si_pid;
669 lsi->lsi_uid = si->si_uid;
670 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
674 lsi->lsi_int = si->si_value.sival_int;
675 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
682 lsi->lsi_band = si->si_band;
687 lsi->lsi_pid = si->si_pid;
688 lsi->lsi_uid = si->si_uid;
690 if (si->si_code == CLD_STOPPED)
691 lsi->lsi_status = bsd_to_linux_signal(si->si_status);
692 else if (si->si_code == CLD_CONTINUED)
693 lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
695 lsi->lsi_status = si->si_status;
702 lsi->lsi_addr = PTROUT(si->si_addr);
706 lsi->lsi_pid = si->si_pid;
707 lsi->lsi_uid = si->si_uid;
708 if (sig >= LINUX_SIGRTMIN) {
709 lsi->lsi_int = si->si_value.sival_int;
710 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
719 lsiginfo_to_ksiginfo(const l_siginfo_t *lsi, ksiginfo_t *ksi, int sig)
722 ksi->ksi_signo = sig;
723 ksi->ksi_code = lsi->lsi_code; /* XXX. Convert. */
724 ksi->ksi_pid = lsi->lsi_pid;
725 ksi->ksi_uid = lsi->lsi_uid;
726 ksi->ksi_status = lsi->lsi_status;
727 ksi->ksi_addr = PTRIN(lsi->lsi_addr);
728 ksi->ksi_info.si_value.sival_int = lsi->lsi_int;
732 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
740 if (!LINUX_SIG_VALID(args->sig))
743 error = copyin(args->info, &linfo, sizeof(linfo));
747 if (linfo.lsi_code >= 0)
750 sig = linux_to_bsd_signal(args->sig);
753 if ((p = pfind_any(args->pid)) != NULL) {
754 error = p_cansignal(td, p, sig);
761 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
762 error = tdsendsignal(p, NULL, sig, &ksi);
770 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
778 if (!LINUX_SIG_VALID(args->sig))
781 error = copyin(args->uinfo, &linfo, sizeof(linfo));
785 if (linfo.lsi_code >= 0)
788 tds = linux_tdfind(td, args->tid, args->tgid);
792 sig = linux_to_bsd_signal(args->sig);
794 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
795 return (linux_do_tkill(td, tds, &ksi));