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_mib.h>
54 #include <compat/linux/linux_signal.h>
55 #include <compat/linux/linux_timer.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);
63 static int linux_common_rt_sigtimedwait(struct thread *,
64 l_sigset_t *, struct timespec *, l_siginfo_t *,
68 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
72 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
73 bsa->sa_handler = PTRIN(lsa->lsa_handler);
76 flags = lsa->lsa_flags;
77 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP) {
78 flags &= ~LINUX_SA_NOCLDSTOP;
79 bsa->sa_flags |= SA_NOCLDSTOP;
81 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT) {
82 flags &= ~LINUX_SA_NOCLDWAIT;
83 bsa->sa_flags |= SA_NOCLDWAIT;
85 if (lsa->lsa_flags & LINUX_SA_SIGINFO) {
86 flags &= ~LINUX_SA_SIGINFO;
87 bsa->sa_flags |= SA_SIGINFO;
90 * XXX: We seem to be missing code to convert
91 * some of the fields in ucontext_t.
94 "partially unsupported sigaction flag SA_SIGINFO");
97 if (lsa->lsa_flags & LINUX_SA_RESTORER) {
98 flags &= ~LINUX_SA_RESTORER;
99 /* XXX: We might want to handle it; see Linux sigreturn(2). */
101 if (lsa->lsa_flags & LINUX_SA_ONSTACK) {
102 flags &= ~LINUX_SA_ONSTACK;
103 bsa->sa_flags |= SA_ONSTACK;
105 if (lsa->lsa_flags & LINUX_SA_RESTART) {
106 flags &= ~LINUX_SA_RESTART;
107 bsa->sa_flags |= SA_RESTART;
109 if (lsa->lsa_flags & LINUX_SA_INTERRUPT) {
110 flags &= ~LINUX_SA_INTERRUPT;
111 /* Documented to be a "historical no-op". */
113 if (lsa->lsa_flags & LINUX_SA_ONESHOT) {
114 flags &= ~LINUX_SA_ONESHOT;
115 bsa->sa_flags |= SA_RESETHAND;
117 if (lsa->lsa_flags & LINUX_SA_NOMASK) {
118 flags &= ~LINUX_SA_NOMASK;
119 bsa->sa_flags |= SA_NODEFER;
123 linux_msg(curthread, "unsupported sigaction flag %#lx", flags);
127 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
130 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
131 #ifdef COMPAT_LINUX32
132 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
134 lsa->lsa_handler = bsa->sa_handler;
136 lsa->lsa_restorer = 0; /* unsupported */
138 if (bsa->sa_flags & SA_NOCLDSTOP)
139 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
140 if (bsa->sa_flags & SA_NOCLDWAIT)
141 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
142 if (bsa->sa_flags & SA_SIGINFO)
143 lsa->lsa_flags |= LINUX_SA_SIGINFO;
144 if (bsa->sa_flags & SA_ONSTACK)
145 lsa->lsa_flags |= LINUX_SA_ONSTACK;
146 if (bsa->sa_flags & SA_RESTART)
147 lsa->lsa_flags |= LINUX_SA_RESTART;
148 if (bsa->sa_flags & SA_RESETHAND)
149 lsa->lsa_flags |= LINUX_SA_ONESHOT;
150 if (bsa->sa_flags & SA_NODEFER)
151 lsa->lsa_flags |= LINUX_SA_NOMASK;
155 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
156 l_sigaction_t *linux_osa)
158 struct sigaction act, oact, *nsa, *osa;
161 if (!LINUX_SIG_VALID(linux_sig))
164 osa = (linux_osa != NULL) ? &oact : NULL;
165 if (linux_nsa != NULL) {
167 linux_to_bsd_sigaction(linux_nsa, nsa);
170 sig = linux_to_bsd_signal(linux_sig);
172 error = kern_sigaction(td, sig, nsa, osa, 0);
176 if (linux_osa != NULL)
177 bsd_to_linux_sigaction(osa, linux_osa);
182 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
184 linux_signal(struct thread *td, struct linux_signal_args *args)
186 l_sigaction_t nsa, osa;
189 nsa.lsa_handler = args->handler;
190 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
191 LINUX_SIGEMPTYSET(nsa.lsa_mask);
193 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
194 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
198 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
201 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
203 l_sigaction_t nsa, osa;
206 if (args->sigsetsize != sizeof(l_sigset_t))
209 if (args->act != NULL) {
210 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
215 error = linux_do_sigaction(td, args->sig,
216 args->act ? &nsa : NULL,
217 args->oact ? &osa : NULL);
219 if (args->oact != NULL && !error) {
220 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
227 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
230 sigset_t omask, nmask;
234 td->td_retval[0] = 0;
237 case LINUX_SIG_BLOCK:
240 case LINUX_SIG_UNBLOCK:
243 case LINUX_SIG_SETMASK:
250 linux_to_bsd_sigset(new, &nmask);
254 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
255 if (error == 0 && old != NULL)
256 bsd_to_linux_sigset(&omask, old);
261 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
263 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
266 l_sigset_t set, oset;
269 if (args->mask != NULL) {
270 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
273 LINUX_SIGEMPTYSET(set);
277 error = linux_do_sigprocmask(td, args->how,
278 args->mask ? &set : NULL,
279 args->omask ? &oset : NULL);
281 if (args->omask != NULL && !error) {
283 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
288 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
291 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
293 l_sigset_t set, oset;
296 if (args->sigsetsize != sizeof(l_sigset_t))
299 if (args->mask != NULL) {
300 error = copyin(args->mask, &set, sizeof(l_sigset_t));
305 error = linux_do_sigprocmask(td, args->how,
306 args->mask ? &set : NULL,
307 args->omask ? &oset : NULL);
309 if (args->omask != NULL && !error) {
310 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
316 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
318 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
320 struct proc *p = td->td_proc;
324 bsd_to_linux_sigset(&td->td_sigmask, &mask);
326 td->td_retval[0] = mask.__mask;
331 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
333 struct proc *p = td->td_proc;
338 bsd_to_linux_sigset(&td->td_sigmask, &lset);
339 td->td_retval[0] = lset.__mask;
340 LINUX_SIGEMPTYSET(lset);
341 lset.__mask = args->mask;
342 linux_to_bsd_sigset(&lset, &bset);
343 td->td_sigmask = bset;
344 SIG_CANTMASK(td->td_sigmask);
351 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
353 struct proc *p = td->td_proc;
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))
385 SIGSETOR(bset, td->td_siglist);
386 SIGSETAND(bset, td->td_sigmask);
388 bsd_to_linux_sigset(&bset, &lset);
389 return (copyout(&lset, args->set, args->sigsetsize));
393 linux_rt_sigtimedwait(struct thread *td,
394 struct linux_rt_sigtimedwait_args *args)
396 struct timespec ts, *tsa;
397 struct l_timespec lts;
401 if ((error = copyin(args->timeout, <s, sizeof(lts))))
403 error = linux_to_native_timespec(&ts, <s);
410 return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
411 args->ptr, args->sigsetsize));
415 linux_common_rt_sigtimedwait(struct thread *td, l_sigset_t *mask,
416 struct timespec *tsa, l_siginfo_t *ptr, l_size_t sigsetsize)
424 if (sigsetsize != sizeof(l_sigset_t))
427 if ((error = copyin(mask, &lset, sizeof(lset))))
429 linux_to_bsd_sigset(&lset, &bset);
432 error = kern_sigtimedwait(td, bset, &ksi, tsa);
436 sig = bsd_to_linux_signal(ksi.ksi_signo);
439 memset(&lsi, 0, sizeof(lsi));
440 siginfo_to_lsiginfo(&ksi.ksi_info, &lsi, sig);
441 error = copyout(&lsi, ptr, sizeof(lsi));
444 td->td_retval[0] = sig;
449 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
451 linux_rt_sigtimedwait_time64(struct thread *td,
452 struct linux_rt_sigtimedwait_time64_args *args)
454 struct timespec ts, *tsa;
455 struct l_timespec64 lts;
459 if ((error = copyin(args->timeout, <s, sizeof(lts))))
461 error = linux_to_native_timespec64(&ts, <s);
468 return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
469 args->ptr, args->sigsetsize));
471 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
474 linux_kill(struct thread *td, struct linux_kill_args *args)
479 * Allow signal 0 as a means to check for privileges
481 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
484 if (args->signum > 0)
485 l_signum = linux_to_bsd_signal(args->signum);
489 return (kern_kill(td, args->pid, l_signum));
493 linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi)
499 AUDIT_ARG_SIGNUM(ksi->ksi_signo);
500 AUDIT_ARG_PID(p->p_pid);
501 AUDIT_ARG_PROCESS(p);
503 error = p_cansignal(td, p, ksi->ksi_signo);
504 if (error != 0 || ksi->ksi_signo == 0)
507 tdksignal(tdt, ksi->ksi_signo, ksi);
515 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
521 if (args->pid <= 0 || args->tgid <=0)
525 * Allow signal 0 as a means to check for privileges
527 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
531 sig = linux_to_bsd_signal(args->sig);
535 tdt = linux_tdfind(td, args->pid, args->tgid);
541 ksi.ksi_code = SI_LWP;
543 ksi.ksi_pid = td->td_proc->p_pid;
544 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
545 return (linux_do_tkill(td, tdt, &ksi));
549 * Deprecated since 2.5.75. Replaced by tgkill().
552 linux_tkill(struct thread *td, struct linux_tkill_args *args)
561 if (!LINUX_SIG_VALID(args->sig))
564 sig = linux_to_bsd_signal(args->sig);
566 tdt = linux_tdfind(td, args->tid, -1);
572 ksi.ksi_code = SI_LWP;
574 ksi.ksi_pid = td->td_proc->p_pid;
575 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
576 return (linux_do_tkill(td, tdt, &ksi));
580 sicode_to_lsicode(int si_code, int *lsi_code)
585 *lsi_code = LINUX_SI_USER;
588 *lsi_code = LINUX_SI_KERNEL;
591 *lsi_code = LINUX_SI_QUEUE;
594 *lsi_code = LINUX_SI_TIMER;
597 *lsi_code = LINUX_SI_MESGQ;
600 *lsi_code = LINUX_SI_ASYNCIO;
603 *lsi_code = LINUX_SI_TKILL;
612 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
615 /* sig alredy converted */
616 lsi->lsi_signo = sig;
617 sicode_to_lsicode(si->si_code, &lsi->lsi_code);
619 switch (si->si_code) {
621 lsi->lsi_pid = si->si_pid;
622 lsi->lsi_uid = si->si_uid;
626 lsi->lsi_int = si->si_value.sival_int;
627 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
628 lsi->lsi_tid = si->si_timerid;
632 lsi->lsi_pid = si->si_pid;
633 lsi->lsi_uid = si->si_uid;
634 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
638 lsi->lsi_int = si->si_value.sival_int;
639 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
646 lsi->lsi_band = si->si_band;
651 lsi->lsi_pid = si->si_pid;
652 lsi->lsi_uid = si->si_uid;
654 if (si->si_code == CLD_STOPPED)
655 lsi->lsi_status = bsd_to_linux_signal(si->si_status);
656 else if (si->si_code == CLD_CONTINUED)
657 lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
659 lsi->lsi_status = si->si_status;
666 lsi->lsi_addr = PTROUT(si->si_addr);
670 lsi->lsi_pid = si->si_pid;
671 lsi->lsi_uid = si->si_uid;
672 if (sig >= LINUX_SIGRTMIN) {
673 lsi->lsi_int = si->si_value.sival_int;
674 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
683 lsiginfo_to_siginfo(struct thread *td, const l_siginfo_t *lsi,
684 siginfo_t *si, int sig)
687 switch (lsi->lsi_code) {
689 if (linux_kernver(td) >= LINUX_KERNVER_2006039) {
690 linux_msg(td, "SI_TKILL forbidden since 2.6.39");
693 si->si_code = SI_LWP;
695 si->si_code = SI_QUEUE;
698 si->si_code = SI_TIMER;
701 si->si_code = SI_MESGQ;
703 case LINUX_SI_ASYNCIO:
704 si->si_code = SI_ASYNCIO;
707 si->si_code = lsi->lsi_code;
712 si->si_pid = td->td_proc->p_pid;
713 si->si_uid = td->td_ucred->cr_ruid;
714 si->si_value.sival_ptr = PTRIN(lsi->lsi_value.sival_ptr);
719 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
727 if (!LINUX_SIG_VALID(args->sig))
730 error = copyin(args->info, &linfo, sizeof(linfo));
734 if (linfo.lsi_code >= 0)
735 /* SI_USER, SI_KERNEL */
738 sig = linux_to_bsd_signal(args->sig);
740 error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
745 if ((p = pfind_any(args->pid)) != NULL) {
746 error = p_cansignal(td, p, sig);
751 error = tdsendsignal(p, NULL, sig, &ksi);
759 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
767 if (!LINUX_SIG_VALID(args->sig))
770 error = copyin(args->uinfo, &linfo, sizeof(linfo));
774 if (linfo.lsi_code >= 0)
777 sig = linux_to_bsd_signal(args->sig);
779 error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
783 tds = linux_tdfind(td, args->tid, args->tgid);
787 return (linux_do_tkill(td, tds, &ksi));