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);
63 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);
71 flags = lsa->lsa_flags;
72 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP) {
73 flags &= ~LINUX_SA_NOCLDSTOP;
74 bsa->sa_flags |= SA_NOCLDSTOP;
76 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT) {
77 flags &= ~LINUX_SA_NOCLDWAIT;
78 bsa->sa_flags |= SA_NOCLDWAIT;
80 if (lsa->lsa_flags & LINUX_SA_SIGINFO) {
81 flags &= ~LINUX_SA_SIGINFO;
82 bsa->sa_flags |= SA_SIGINFO;
85 * XXX: We seem to be missing code to convert
86 * some of the fields in ucontext_t.
89 "partially unsupported sigaction flag SA_SIGINFO");
92 if (lsa->lsa_flags & LINUX_SA_RESTORER) {
93 flags &= ~LINUX_SA_RESTORER;
94 /* XXX: We might want to handle it; see Linux sigreturn(2). */
96 if (lsa->lsa_flags & LINUX_SA_ONSTACK) {
97 flags &= ~LINUX_SA_ONSTACK;
98 bsa->sa_flags |= SA_ONSTACK;
100 if (lsa->lsa_flags & LINUX_SA_RESTART) {
101 flags &= ~LINUX_SA_RESTART;
102 bsa->sa_flags |= SA_RESTART;
104 if (lsa->lsa_flags & LINUX_SA_INTERRUPT) {
105 flags &= ~LINUX_SA_INTERRUPT;
106 /* Documented to be a "historical no-op". */
108 if (lsa->lsa_flags & LINUX_SA_ONESHOT) {
109 flags &= ~LINUX_SA_ONESHOT;
110 bsa->sa_flags |= SA_RESETHAND;
112 if (lsa->lsa_flags & LINUX_SA_NOMASK) {
113 flags &= ~LINUX_SA_NOMASK;
114 bsa->sa_flags |= SA_NODEFER;
118 linux_msg(curthread, "unsupported sigaction flag %#lx", flags);
122 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
125 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
126 #ifdef COMPAT_LINUX32
127 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
129 lsa->lsa_handler = bsa->sa_handler;
131 lsa->lsa_restorer = 0; /* unsupported */
133 if (bsa->sa_flags & SA_NOCLDSTOP)
134 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
135 if (bsa->sa_flags & SA_NOCLDWAIT)
136 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
137 if (bsa->sa_flags & SA_SIGINFO)
138 lsa->lsa_flags |= LINUX_SA_SIGINFO;
139 if (bsa->sa_flags & SA_ONSTACK)
140 lsa->lsa_flags |= LINUX_SA_ONSTACK;
141 if (bsa->sa_flags & SA_RESTART)
142 lsa->lsa_flags |= LINUX_SA_RESTART;
143 if (bsa->sa_flags & SA_RESETHAND)
144 lsa->lsa_flags |= LINUX_SA_ONESHOT;
145 if (bsa->sa_flags & SA_NODEFER)
146 lsa->lsa_flags |= LINUX_SA_NOMASK;
150 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
151 l_sigaction_t *linux_osa)
153 struct sigaction act, oact, *nsa, *osa;
156 if (!LINUX_SIG_VALID(linux_sig))
159 osa = (linux_osa != NULL) ? &oact : NULL;
160 if (linux_nsa != NULL) {
162 linux_to_bsd_sigaction(linux_nsa, nsa);
165 sig = linux_to_bsd_signal(linux_sig);
167 error = kern_sigaction(td, sig, nsa, osa, 0);
171 if (linux_osa != NULL)
172 bsd_to_linux_sigaction(osa, linux_osa);
177 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
179 linux_signal(struct thread *td, struct linux_signal_args *args)
181 l_sigaction_t nsa, osa;
184 nsa.lsa_handler = args->handler;
185 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
186 LINUX_SIGEMPTYSET(nsa.lsa_mask);
188 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
189 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
193 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
196 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
198 l_sigaction_t nsa, osa;
201 if (args->sigsetsize != sizeof(l_sigset_t))
204 if (args->act != NULL) {
205 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
210 error = linux_do_sigaction(td, args->sig,
211 args->act ? &nsa : NULL,
212 args->oact ? &osa : NULL);
214 if (args->oact != NULL && !error) {
215 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
222 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
225 sigset_t omask, nmask;
229 td->td_retval[0] = 0;
232 case LINUX_SIG_BLOCK:
235 case LINUX_SIG_UNBLOCK:
238 case LINUX_SIG_SETMASK:
245 linux_to_bsd_sigset(new, &nmask);
249 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
250 if (error == 0 && old != NULL)
251 bsd_to_linux_sigset(&omask, old);
256 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
258 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
261 l_sigset_t set, oset;
264 if (args->mask != NULL) {
265 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
268 LINUX_SIGEMPTYSET(set);
272 error = linux_do_sigprocmask(td, args->how,
273 args->mask ? &set : NULL,
274 args->omask ? &oset : NULL);
276 if (args->omask != NULL && !error) {
278 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
283 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
286 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
288 l_sigset_t set, oset;
291 if (args->sigsetsize != sizeof(l_sigset_t))
294 if (args->mask != NULL) {
295 error = copyin(args->mask, &set, sizeof(l_sigset_t));
300 error = linux_do_sigprocmask(td, args->how,
301 args->mask ? &set : NULL,
302 args->omask ? &oset : NULL);
304 if (args->omask != NULL && !error) {
305 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
311 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
313 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
315 struct proc *p = td->td_proc;
319 bsd_to_linux_sigset(&td->td_sigmask, &mask);
321 td->td_retval[0] = mask.__mask;
326 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
328 struct proc *p = td->td_proc;
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;
355 SIGSETOR(bset, td->td_siglist);
356 SIGSETAND(bset, td->td_sigmask);
358 bsd_to_linux_sigset(&bset, &lset);
360 return (copyout(&mask, args->mask, sizeof(mask)));
362 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
368 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
370 struct proc *p = td->td_proc;
374 if (args->sigsetsize > sizeof(lset))
380 SIGSETOR(bset, td->td_siglist);
381 SIGSETAND(bset, td->td_sigmask);
383 bsd_to_linux_sigset(&bset, &lset);
384 return (copyout(&lset, args->set, args->sigsetsize));
391 linux_rt_sigtimedwait(struct thread *td,
392 struct linux_rt_sigtimedwait_args *args)
397 struct timespec ts, *tsa;
403 if (args->sigsetsize != sizeof(l_sigset_t))
406 if ((error = copyin(args->mask, &lset, sizeof(lset))))
408 linux_to_bsd_sigset(&lset, &bset);
412 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
414 tv.tv_sec = (long)ltv.tv_sec;
415 tv.tv_usec = (suseconds_t)ltv.tv_usec;
416 if (itimerfix(&tv)) {
418 * The timeout was invalid. Convert it to something
419 * valid that will act as it does under Linux.
421 tv.tv_sec += tv.tv_usec / 1000000;
422 tv.tv_usec %= 1000000;
423 if (tv.tv_usec < 0) {
425 tv.tv_usec += 1000000;
430 TIMEVAL_TO_TIMESPEC(&tv, &ts);
433 error = kern_sigtimedwait(td, bset, &info, tsa);
437 sig = bsd_to_linux_signal(info.ksi_signo);
440 memset(&linfo, 0, sizeof(linfo));
441 ksiginfo_to_lsiginfo(&info, &linfo, sig);
442 error = copyout(&linfo, args->ptr, sizeof(linfo));
445 td->td_retval[0] = sig;
451 linux_kill(struct thread *td, struct linux_kill_args *args)
456 * Allow signal 0 as a means to check for privileges
458 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
461 if (args->signum > 0)
462 l_signum = linux_to_bsd_signal(args->signum);
466 return (kern_kill(td, args->pid, l_signum));
470 linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi)
476 AUDIT_ARG_SIGNUM(ksi->ksi_signo);
477 AUDIT_ARG_PID(p->p_pid);
478 AUDIT_ARG_PROCESS(p);
480 error = p_cansignal(td, p, ksi->ksi_signo);
481 if (error != 0 || ksi->ksi_signo == 0)
484 tdksignal(tdt, ksi->ksi_signo, ksi);
492 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
498 if (args->pid <= 0 || args->tgid <=0)
502 * Allow signal 0 as a means to check for privileges
504 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
508 sig = linux_to_bsd_signal(args->sig);
512 tdt = linux_tdfind(td, args->pid, args->tgid);
518 ksi.ksi_code = SI_LWP;
520 ksi.ksi_pid = td->td_proc->p_pid;
521 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
522 return (linux_do_tkill(td, tdt, &ksi));
526 * Deprecated since 2.5.75. Replaced by tgkill().
529 linux_tkill(struct thread *td, struct linux_tkill_args *args)
538 if (!LINUX_SIG_VALID(args->sig))
541 sig = linux_to_bsd_signal(args->sig);
543 tdt = linux_tdfind(td, args->tid, -1);
549 ksi.ksi_code = SI_LWP;
551 ksi.ksi_pid = td->td_proc->p_pid;
552 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
553 return (linux_do_tkill(td, tdt, &ksi));
557 ksiginfo_to_lsiginfo(const ksiginfo_t *ksi, l_siginfo_t *lsi, l_int sig)
560 siginfo_to_lsiginfo(&ksi->ksi_info, lsi, sig);
564 sicode_to_lsicode(int si_code, int *lsi_code)
569 *lsi_code = LINUX_SI_USER;
572 *lsi_code = LINUX_SI_KERNEL;
575 *lsi_code = LINUX_SI_QUEUE;
578 *lsi_code = LINUX_SI_TIMER;
581 *lsi_code = LINUX_SI_MESGQ;
584 *lsi_code = LINUX_SI_ASYNCIO;
587 *lsi_code = LINUX_SI_TKILL;
596 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
599 /* sig alredy converted */
600 lsi->lsi_signo = sig;
601 sicode_to_lsicode(si->si_code, &lsi->lsi_code);
603 switch (si->si_code) {
605 lsi->lsi_pid = si->si_pid;
606 lsi->lsi_uid = si->si_uid;
610 lsi->lsi_int = si->si_value.sival_int;
611 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
612 lsi->lsi_tid = si->si_timerid;
616 lsi->lsi_pid = si->si_pid;
617 lsi->lsi_uid = si->si_uid;
618 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
622 lsi->lsi_int = si->si_value.sival_int;
623 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
630 lsi->lsi_band = si->si_band;
635 lsi->lsi_pid = si->si_pid;
636 lsi->lsi_uid = si->si_uid;
638 if (si->si_code == CLD_STOPPED)
639 lsi->lsi_status = bsd_to_linux_signal(si->si_status);
640 else if (si->si_code == CLD_CONTINUED)
641 lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
643 lsi->lsi_status = si->si_status;
650 lsi->lsi_addr = PTROUT(si->si_addr);
654 lsi->lsi_pid = si->si_pid;
655 lsi->lsi_uid = si->si_uid;
656 if (sig >= LINUX_SIGRTMIN) {
657 lsi->lsi_int = si->si_value.sival_int;
658 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
667 lsiginfo_to_ksiginfo(const l_siginfo_t *lsi, ksiginfo_t *ksi, int sig)
670 ksi->ksi_signo = sig;
671 ksi->ksi_code = lsi->lsi_code; /* XXX. Convert. */
672 ksi->ksi_pid = lsi->lsi_pid;
673 ksi->ksi_uid = lsi->lsi_uid;
674 ksi->ksi_status = lsi->lsi_status;
675 ksi->ksi_addr = PTRIN(lsi->lsi_addr);
676 ksi->ksi_info.si_value.sival_int = lsi->lsi_int;
680 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
688 if (!LINUX_SIG_VALID(args->sig))
691 error = copyin(args->info, &linfo, sizeof(linfo));
695 if (linfo.lsi_code >= 0)
698 sig = linux_to_bsd_signal(args->sig);
701 if ((p = pfind_any(args->pid)) != NULL) {
702 error = p_cansignal(td, p, sig);
709 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
710 error = tdsendsignal(p, NULL, sig, &ksi);
718 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
726 if (!LINUX_SIG_VALID(args->sig))
729 error = copyin(args->uinfo, &linfo, sizeof(linfo));
733 if (linfo.lsi_code >= 0)
736 tds = linux_tdfind(td, args->tid, args->tgid);
740 sig = linux_to_bsd_signal(args->sig);
742 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
743 return (linux_do_tkill(td, tds, &ksi));