2 * SPDX-License-Identifier: BSD-2-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 * 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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/mutex.h>
38 #include <sys/signalvar.h>
39 #include <sys/syscallsubr.h>
40 #include <sys/sysproto.h>
42 #include <security/audit/audit.h>
44 #include "opt_compat.h"
47 #include <machine/../linux32/linux.h>
48 #include <machine/../linux32/linux32_proto.h>
50 #include <machine/../linux/linux.h>
51 #include <machine/../linux/linux_proto.h>
53 #include <compat/linux/linux_signal.h>
54 #include <compat/linux/linux_util.h>
55 #include <compat/linux/linux_emul.h>
56 #include <compat/linux/linux_misc.h>
58 static int linux_do_tkill(struct thread *td, struct thread *tdt,
60 static void sicode_to_lsicode(int si_code, int *lsi_code);
64 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
67 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
68 bsa->sa_handler = PTRIN(lsa->lsa_handler);
70 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
71 bsa->sa_flags |= SA_NOCLDSTOP;
72 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
73 bsa->sa_flags |= SA_NOCLDWAIT;
74 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
75 bsa->sa_flags |= SA_SIGINFO;
76 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
77 bsa->sa_flags |= SA_ONSTACK;
78 if (lsa->lsa_flags & LINUX_SA_RESTART)
79 bsa->sa_flags |= SA_RESTART;
80 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
81 bsa->sa_flags |= SA_RESETHAND;
82 if (lsa->lsa_flags & LINUX_SA_NOMASK)
83 bsa->sa_flags |= SA_NODEFER;
87 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
90 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
92 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
94 lsa->lsa_handler = bsa->sa_handler;
96 lsa->lsa_restorer = 0; /* unsupported */
98 if (bsa->sa_flags & SA_NOCLDSTOP)
99 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
100 if (bsa->sa_flags & SA_NOCLDWAIT)
101 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
102 if (bsa->sa_flags & SA_SIGINFO)
103 lsa->lsa_flags |= LINUX_SA_SIGINFO;
104 if (bsa->sa_flags & SA_ONSTACK)
105 lsa->lsa_flags |= LINUX_SA_ONSTACK;
106 if (bsa->sa_flags & SA_RESTART)
107 lsa->lsa_flags |= LINUX_SA_RESTART;
108 if (bsa->sa_flags & SA_RESETHAND)
109 lsa->lsa_flags |= LINUX_SA_ONESHOT;
110 if (bsa->sa_flags & SA_NODEFER)
111 lsa->lsa_flags |= LINUX_SA_NOMASK;
115 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
116 l_sigaction_t *linux_osa)
118 struct sigaction act, oact, *nsa, *osa;
121 if (!LINUX_SIG_VALID(linux_sig))
124 osa = (linux_osa != NULL) ? &oact : NULL;
125 if (linux_nsa != NULL) {
127 linux_to_bsd_sigaction(linux_nsa, nsa);
130 sig = linux_to_bsd_signal(linux_sig);
132 error = kern_sigaction(td, sig, nsa, osa, 0);
136 if (linux_osa != NULL)
137 bsd_to_linux_sigaction(osa, linux_osa);
142 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
144 linux_signal(struct thread *td, struct linux_signal_args *args)
146 l_sigaction_t nsa, osa;
151 printf(ARGS(signal, "%d, %p"),
152 args->sig, (void *)(uintptr_t)args->handler);
155 nsa.lsa_handler = args->handler;
156 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
157 LINUX_SIGEMPTYSET(nsa.lsa_mask);
159 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
160 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
164 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
167 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
169 l_sigaction_t nsa, osa;
173 if (ldebug(rt_sigaction))
174 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
175 (long)args->sig, (void *)args->act,
176 (void *)args->oact, (long)args->sigsetsize);
179 if (args->sigsetsize != sizeof(l_sigset_t))
182 if (args->act != NULL) {
183 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
188 error = linux_do_sigaction(td, args->sig,
189 args->act ? &nsa : NULL,
190 args->oact ? &osa : NULL);
192 if (args->oact != NULL && !error) {
193 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
200 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
203 sigset_t omask, nmask;
207 td->td_retval[0] = 0;
210 case LINUX_SIG_BLOCK:
213 case LINUX_SIG_UNBLOCK:
216 case LINUX_SIG_SETMASK:
223 linux_to_bsd_sigset(new, &nmask);
227 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
228 if (error == 0 && old != NULL)
229 bsd_to_linux_sigset(&omask, old);
234 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
236 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
239 l_sigset_t set, oset;
243 if (ldebug(sigprocmask))
244 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
247 if (args->mask != NULL) {
248 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
251 LINUX_SIGEMPTYSET(set);
255 error = linux_do_sigprocmask(td, args->how,
256 args->mask ? &set : NULL,
257 args->omask ? &oset : NULL);
259 if (args->omask != NULL && !error) {
261 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
266 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
269 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
271 l_sigset_t set, oset;
275 if (ldebug(rt_sigprocmask))
276 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
277 args->how, (void *)args->mask,
278 (void *)args->omask, (long)args->sigsetsize);
281 if (args->sigsetsize != sizeof(l_sigset_t))
284 if (args->mask != NULL) {
285 error = copyin(args->mask, &set, sizeof(l_sigset_t));
290 error = linux_do_sigprocmask(td, args->how,
291 args->mask ? &set : NULL,
292 args->omask ? &oset : NULL);
294 if (args->omask != NULL && !error) {
295 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
301 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
303 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
305 struct proc *p = td->td_proc;
309 if (ldebug(sgetmask))
310 printf(ARGS(sgetmask, ""));
314 bsd_to_linux_sigset(&td->td_sigmask, &mask);
316 td->td_retval[0] = mask.__mask;
321 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
323 struct proc *p = td->td_proc;
328 if (ldebug(ssetmask))
329 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
333 bsd_to_linux_sigset(&td->td_sigmask, &lset);
334 td->td_retval[0] = lset.__mask;
335 LINUX_SIGEMPTYSET(lset);
336 lset.__mask = args->mask;
337 linux_to_bsd_sigset(&lset, &bset);
338 td->td_sigmask = bset;
339 SIG_CANTMASK(td->td_sigmask);
346 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
348 struct proc *p = td->td_proc;
354 if (ldebug(sigpending))
355 printf(ARGS(sigpending, "*"));
360 SIGSETOR(bset, td->td_siglist);
361 SIGSETAND(bset, td->td_sigmask);
363 bsd_to_linux_sigset(&bset, &lset);
365 return (copyout(&mask, args->mask, sizeof(mask)));
367 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
373 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
375 struct proc *p = td->td_proc;
379 if (args->sigsetsize > sizeof(lset))
384 if (ldebug(rt_sigpending))
385 printf(ARGS(rt_sigpending, "*"));
390 SIGSETOR(bset, td->td_siglist);
391 SIGSETAND(bset, td->td_sigmask);
393 bsd_to_linux_sigset(&bset, &lset);
394 return (copyout(&lset, args->set, args->sigsetsize));
401 linux_rt_sigtimedwait(struct thread *td,
402 struct linux_rt_sigtimedwait_args *args)
407 struct timespec ts, *tsa;
414 if (ldebug(rt_sigtimedwait))
415 printf(ARGS(rt_sigtimedwait, "*"));
417 if (args->sigsetsize != sizeof(l_sigset_t))
420 if ((error = copyin(args->mask, &lset, sizeof(lset))))
422 linux_to_bsd_sigset(&lset, &bset);
426 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
429 if (ldebug(rt_sigtimedwait))
430 printf(LMSG("linux_rt_sigtimedwait: "
431 "incoming timeout (%jd/%jd)\n"),
432 (intmax_t)ltv.tv_sec, (intmax_t)ltv.tv_usec);
434 tv.tv_sec = (long)ltv.tv_sec;
435 tv.tv_usec = (suseconds_t)ltv.tv_usec;
436 if (itimerfix(&tv)) {
438 * The timeout was invalid. Convert it to something
439 * valid that will act as it does under Linux.
441 tv.tv_sec += tv.tv_usec / 1000000;
442 tv.tv_usec %= 1000000;
443 if (tv.tv_usec < 0) {
445 tv.tv_usec += 1000000;
450 if (ldebug(rt_sigtimedwait))
451 printf(LMSG("linux_rt_sigtimedwait: "
452 "converted timeout (%jd/%ld)\n"),
453 (intmax_t)tv.tv_sec, tv.tv_usec);
456 TIMEVAL_TO_TIMESPEC(&tv, &ts);
459 error = kern_sigtimedwait(td, bset, &info, tsa);
461 if (ldebug(rt_sigtimedwait))
462 printf(LMSG("linux_rt_sigtimedwait: "
463 "sigtimedwait returning (%d)\n"), error);
468 sig = bsd_to_linux_signal(info.ksi_signo);
471 memset(&linfo, 0, sizeof(linfo));
472 ksiginfo_to_lsiginfo(&info, &linfo, sig);
473 error = copyout(&linfo, args->ptr, sizeof(linfo));
476 td->td_retval[0] = sig;
482 linux_kill(struct thread *td, struct linux_kill_args *args)
484 struct kill_args /* {
491 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
495 * Allow signal 0 as a means to check for privileges
497 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
500 if (args->signum > 0)
501 tmp.signum = linux_to_bsd_signal(args->signum);
506 return (sys_kill(td, &tmp));
510 linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi)
516 AUDIT_ARG_SIGNUM(ksi->ksi_signo);
517 AUDIT_ARG_PID(p->p_pid);
518 AUDIT_ARG_PROCESS(p);
520 error = p_cansignal(td, p, ksi->ksi_signo);
521 if (error != 0 || ksi->ksi_signo == 0)
524 tdksignal(tdt, ksi->ksi_signo, ksi);
532 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
540 printf(ARGS(tgkill, "%d, %d, %d"),
541 args->tgid, args->pid, args->sig);
544 if (args->pid <= 0 || args->tgid <=0)
548 * Allow signal 0 as a means to check for privileges
550 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
554 sig = linux_to_bsd_signal(args->sig);
558 tdt = linux_tdfind(td, args->pid, args->tgid);
564 ksi.ksi_code = SI_LWP;
566 ksi.ksi_pid = td->td_proc->p_pid;
567 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
568 return (linux_do_tkill(td, tdt, &ksi));
572 * Deprecated since 2.5.75. Replaced by tgkill().
575 linux_tkill(struct thread *td, struct linux_tkill_args *args)
583 printf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
588 if (!LINUX_SIG_VALID(args->sig))
591 sig = linux_to_bsd_signal(args->sig);
593 tdt = linux_tdfind(td, args->tid, -1);
599 ksi.ksi_code = SI_LWP;
601 ksi.ksi_pid = td->td_proc->p_pid;
602 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
603 return (linux_do_tkill(td, tdt, &ksi));
607 ksiginfo_to_lsiginfo(const ksiginfo_t *ksi, l_siginfo_t *lsi, l_int sig)
610 siginfo_to_lsiginfo(&ksi->ksi_info, lsi, sig);
614 sicode_to_lsicode(int si_code, int *lsi_code)
619 *lsi_code = LINUX_SI_USER;
622 *lsi_code = LINUX_SI_KERNEL;
625 *lsi_code = LINUX_SI_QUEUE;
628 *lsi_code = LINUX_SI_TIMER;
631 *lsi_code = LINUX_SI_MESGQ;
634 *lsi_code = LINUX_SI_ASYNCIO;
637 *lsi_code = LINUX_SI_TKILL;
646 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
649 /* sig alredy converted */
650 lsi->lsi_signo = sig;
651 sicode_to_lsicode(si->si_code, &lsi->lsi_code);
653 switch (si->si_code) {
655 lsi->lsi_pid = si->si_pid;
656 lsi->lsi_uid = si->si_uid;
660 lsi->lsi_int = si->si_value.sival_int;
661 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
662 lsi->lsi_tid = si->si_timerid;
666 lsi->lsi_pid = si->si_pid;
667 lsi->lsi_uid = si->si_uid;
668 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
672 lsi->lsi_int = si->si_value.sival_int;
673 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
680 lsi->lsi_band = si->si_band;
685 lsi->lsi_pid = si->si_pid;
686 lsi->lsi_uid = si->si_uid;
688 if (si->si_code == CLD_STOPPED)
689 lsi->lsi_status = bsd_to_linux_signal(si->si_status);
690 else if (si->si_code == CLD_CONTINUED)
691 lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
693 lsi->lsi_status = si->si_status;
700 lsi->lsi_addr = PTROUT(si->si_addr);
704 lsi->lsi_pid = si->si_pid;
705 lsi->lsi_uid = si->si_uid;
706 if (sig >= LINUX_SIGRTMIN) {
707 lsi->lsi_int = si->si_value.sival_int;
708 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
717 lsiginfo_to_ksiginfo(const l_siginfo_t *lsi, ksiginfo_t *ksi, int sig)
720 ksi->ksi_signo = sig;
721 ksi->ksi_code = lsi->lsi_code; /* XXX. Convert. */
722 ksi->ksi_pid = lsi->lsi_pid;
723 ksi->ksi_uid = lsi->lsi_uid;
724 ksi->ksi_status = lsi->lsi_status;
725 ksi->ksi_addr = PTRIN(lsi->lsi_addr);
726 ksi->ksi_info.si_value.sival_int = lsi->lsi_int;
730 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
738 if (!LINUX_SIG_VALID(args->sig))
741 error = copyin(args->info, &linfo, sizeof(linfo));
745 if (linfo.lsi_code >= 0)
748 sig = linux_to_bsd_signal(args->sig);
751 if ((p = pfind_any(args->pid)) != NULL) {
752 error = p_cansignal(td, p, sig);
759 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
760 error = tdsendsignal(p, NULL, sig, &ksi);
768 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
776 if (!LINUX_SIG_VALID(args->sig))
779 error = copyin(args->uinfo, &linfo, sizeof(linfo));
783 if (linfo.lsi_code >= 0)
786 tds = linux_tdfind(td, args->tid, args->tgid);
790 sig = linux_to_bsd_signal(args->sig);
792 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
793 return (linux_do_tkill(td, tds, &ksi));