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)
68 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
69 bsa->sa_handler = PTRIN(lsa->lsa_handler);
72 flags = lsa->lsa_flags;
73 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP) {
74 flags &= ~LINUX_SA_NOCLDSTOP;
75 bsa->sa_flags |= SA_NOCLDSTOP;
77 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT) {
78 flags &= ~LINUX_SA_NOCLDWAIT;
79 bsa->sa_flags |= SA_NOCLDWAIT;
81 if (lsa->lsa_flags & LINUX_SA_SIGINFO) {
82 flags &= ~LINUX_SA_SIGINFO;
83 bsa->sa_flags |= SA_SIGINFO;
86 * XXX: We seem to be missing code to convert
87 * some of the fields in ucontext_t.
90 "partially unsupported sigaction flag SA_SIGINFO");
93 if (lsa->lsa_flags & LINUX_SA_RESTORER) {
94 flags &= ~LINUX_SA_RESTORER;
95 /* XXX: We might want to handle it; see Linux sigreturn(2). */
97 if (lsa->lsa_flags & LINUX_SA_ONSTACK) {
98 flags &= ~LINUX_SA_ONSTACK;
99 bsa->sa_flags |= SA_ONSTACK;
101 if (lsa->lsa_flags & LINUX_SA_RESTART) {
102 flags &= ~LINUX_SA_RESTART;
103 bsa->sa_flags |= SA_RESTART;
105 if (lsa->lsa_flags & LINUX_SA_INTERRUPT) {
106 flags &= ~LINUX_SA_INTERRUPT;
107 /* Documented to be a "historical no-op". */
109 if (lsa->lsa_flags & LINUX_SA_ONESHOT) {
110 flags &= ~LINUX_SA_ONESHOT;
111 bsa->sa_flags |= SA_RESETHAND;
113 if (lsa->lsa_flags & LINUX_SA_NOMASK) {
114 flags &= ~LINUX_SA_NOMASK;
115 bsa->sa_flags |= SA_NODEFER;
119 linux_msg(curthread, "unsupported sigaction flag %#lx", flags);
123 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
126 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
127 #ifdef COMPAT_LINUX32
128 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
130 lsa->lsa_handler = bsa->sa_handler;
132 lsa->lsa_restorer = 0; /* unsupported */
134 if (bsa->sa_flags & SA_NOCLDSTOP)
135 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
136 if (bsa->sa_flags & SA_NOCLDWAIT)
137 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
138 if (bsa->sa_flags & SA_SIGINFO)
139 lsa->lsa_flags |= LINUX_SA_SIGINFO;
140 if (bsa->sa_flags & SA_ONSTACK)
141 lsa->lsa_flags |= LINUX_SA_ONSTACK;
142 if (bsa->sa_flags & SA_RESTART)
143 lsa->lsa_flags |= LINUX_SA_RESTART;
144 if (bsa->sa_flags & SA_RESETHAND)
145 lsa->lsa_flags |= LINUX_SA_ONESHOT;
146 if (bsa->sa_flags & SA_NODEFER)
147 lsa->lsa_flags |= LINUX_SA_NOMASK;
151 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
152 l_sigaction_t *linux_osa)
154 struct sigaction act, oact, *nsa, *osa;
157 if (!LINUX_SIG_VALID(linux_sig))
160 osa = (linux_osa != NULL) ? &oact : NULL;
161 if (linux_nsa != NULL) {
163 linux_to_bsd_sigaction(linux_nsa, nsa);
166 sig = linux_to_bsd_signal(linux_sig);
168 error = kern_sigaction(td, sig, nsa, osa, 0);
172 if (linux_osa != NULL)
173 bsd_to_linux_sigaction(osa, linux_osa);
178 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
180 linux_signal(struct thread *td, struct linux_signal_args *args)
182 l_sigaction_t nsa, osa;
185 nsa.lsa_handler = args->handler;
186 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
187 LINUX_SIGEMPTYSET(nsa.lsa_mask);
189 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
190 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
194 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
197 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
199 l_sigaction_t nsa, osa;
202 if (args->sigsetsize != sizeof(l_sigset_t))
205 if (args->act != NULL) {
206 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
211 error = linux_do_sigaction(td, args->sig,
212 args->act ? &nsa : NULL,
213 args->oact ? &osa : NULL);
215 if (args->oact != NULL && !error) {
216 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
223 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
226 sigset_t omask, nmask;
230 td->td_retval[0] = 0;
233 case LINUX_SIG_BLOCK:
236 case LINUX_SIG_UNBLOCK:
239 case LINUX_SIG_SETMASK:
246 linux_to_bsd_sigset(new, &nmask);
250 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
251 if (error == 0 && old != NULL)
252 bsd_to_linux_sigset(&omask, old);
257 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
259 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
262 l_sigset_t set, oset;
265 if (args->mask != NULL) {
266 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
269 LINUX_SIGEMPTYSET(set);
273 error = linux_do_sigprocmask(td, args->how,
274 args->mask ? &set : NULL,
275 args->omask ? &oset : NULL);
277 if (args->omask != NULL && !error) {
279 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
284 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
287 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
289 l_sigset_t set, oset;
292 if (args->sigsetsize != sizeof(l_sigset_t))
295 if (args->mask != NULL) {
296 error = copyin(args->mask, &set, sizeof(l_sigset_t));
301 error = linux_do_sigprocmask(td, args->how,
302 args->mask ? &set : NULL,
303 args->omask ? &oset : NULL);
305 if (args->omask != NULL && !error) {
306 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
312 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
314 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
316 struct proc *p = td->td_proc;
320 bsd_to_linux_sigset(&td->td_sigmask, &mask);
322 td->td_retval[0] = mask.__mask;
327 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
329 struct proc *p = td->td_proc;
334 bsd_to_linux_sigset(&td->td_sigmask, &lset);
335 td->td_retval[0] = lset.__mask;
336 LINUX_SIGEMPTYSET(lset);
337 lset.__mask = args->mask;
338 linux_to_bsd_sigset(&lset, &bset);
339 td->td_sigmask = bset;
340 SIG_CANTMASK(td->td_sigmask);
347 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
349 struct proc *p = td->td_proc;
356 SIGSETOR(bset, td->td_siglist);
357 SIGSETAND(bset, td->td_sigmask);
359 bsd_to_linux_sigset(&bset, &lset);
361 return (copyout(&mask, args->mask, sizeof(mask)));
363 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
369 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
371 struct proc *p = td->td_proc;
375 if (args->sigsetsize > sizeof(lset))
381 SIGSETOR(bset, td->td_siglist);
382 SIGSETAND(bset, td->td_sigmask);
384 bsd_to_linux_sigset(&bset, &lset);
385 return (copyout(&lset, args->set, args->sigsetsize));
392 linux_rt_sigtimedwait(struct thread *td,
393 struct linux_rt_sigtimedwait_args *args)
398 struct timespec ts, *tsa;
404 if (args->sigsetsize != sizeof(l_sigset_t))
407 if ((error = copyin(args->mask, &lset, sizeof(lset))))
409 linux_to_bsd_sigset(&lset, &bset);
413 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
415 tv.tv_sec = (long)ltv.tv_sec;
416 tv.tv_usec = (suseconds_t)ltv.tv_usec;
417 if (itimerfix(&tv)) {
419 * The timeout was invalid. Convert it to something
420 * valid that will act as it does under Linux.
422 tv.tv_sec += tv.tv_usec / 1000000;
423 tv.tv_usec %= 1000000;
424 if (tv.tv_usec < 0) {
426 tv.tv_usec += 1000000;
431 TIMEVAL_TO_TIMESPEC(&tv, &ts);
434 error = kern_sigtimedwait(td, bset, &info, tsa);
438 sig = bsd_to_linux_signal(info.ksi_signo);
441 memset(&linfo, 0, sizeof(linfo));
442 ksiginfo_to_lsiginfo(&info, &linfo, sig);
443 error = copyout(&linfo, args->ptr, sizeof(linfo));
446 td->td_retval[0] = sig;
452 linux_kill(struct thread *td, struct linux_kill_args *args)
457 * Allow signal 0 as a means to check for privileges
459 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
462 if (args->signum > 0)
463 l_signum = linux_to_bsd_signal(args->signum);
467 return (kern_kill(td, args->pid, l_signum));
471 linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi)
477 AUDIT_ARG_SIGNUM(ksi->ksi_signo);
478 AUDIT_ARG_PID(p->p_pid);
479 AUDIT_ARG_PROCESS(p);
481 error = p_cansignal(td, p, ksi->ksi_signo);
482 if (error != 0 || ksi->ksi_signo == 0)
485 tdksignal(tdt, ksi->ksi_signo, ksi);
493 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
499 if (args->pid <= 0 || args->tgid <=0)
503 * Allow signal 0 as a means to check for privileges
505 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
509 sig = linux_to_bsd_signal(args->sig);
513 tdt = linux_tdfind(td, args->pid, args->tgid);
519 ksi.ksi_code = SI_LWP;
521 ksi.ksi_pid = td->td_proc->p_pid;
522 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
523 return (linux_do_tkill(td, tdt, &ksi));
527 * Deprecated since 2.5.75. Replaced by tgkill().
530 linux_tkill(struct thread *td, struct linux_tkill_args *args)
539 if (!LINUX_SIG_VALID(args->sig))
542 sig = linux_to_bsd_signal(args->sig);
544 tdt = linux_tdfind(td, args->tid, -1);
550 ksi.ksi_code = SI_LWP;
552 ksi.ksi_pid = td->td_proc->p_pid;
553 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
554 return (linux_do_tkill(td, tdt, &ksi));
558 ksiginfo_to_lsiginfo(const ksiginfo_t *ksi, l_siginfo_t *lsi, l_int sig)
561 siginfo_to_lsiginfo(&ksi->ksi_info, lsi, sig);
565 sicode_to_lsicode(int si_code, int *lsi_code)
570 *lsi_code = LINUX_SI_USER;
573 *lsi_code = LINUX_SI_KERNEL;
576 *lsi_code = LINUX_SI_QUEUE;
579 *lsi_code = LINUX_SI_TIMER;
582 *lsi_code = LINUX_SI_MESGQ;
585 *lsi_code = LINUX_SI_ASYNCIO;
588 *lsi_code = LINUX_SI_TKILL;
597 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
600 /* sig alredy converted */
601 lsi->lsi_signo = sig;
602 sicode_to_lsicode(si->si_code, &lsi->lsi_code);
604 switch (si->si_code) {
606 lsi->lsi_pid = si->si_pid;
607 lsi->lsi_uid = si->si_uid;
611 lsi->lsi_int = si->si_value.sival_int;
612 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
613 lsi->lsi_tid = si->si_timerid;
617 lsi->lsi_pid = si->si_pid;
618 lsi->lsi_uid = si->si_uid;
619 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
623 lsi->lsi_int = si->si_value.sival_int;
624 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
631 lsi->lsi_band = si->si_band;
636 lsi->lsi_pid = si->si_pid;
637 lsi->lsi_uid = si->si_uid;
639 if (si->si_code == CLD_STOPPED)
640 lsi->lsi_status = bsd_to_linux_signal(si->si_status);
641 else if (si->si_code == CLD_CONTINUED)
642 lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
644 lsi->lsi_status = si->si_status;
651 lsi->lsi_addr = PTROUT(si->si_addr);
655 lsi->lsi_pid = si->si_pid;
656 lsi->lsi_uid = si->si_uid;
657 if (sig >= LINUX_SIGRTMIN) {
658 lsi->lsi_int = si->si_value.sival_int;
659 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
668 lsiginfo_to_ksiginfo(const l_siginfo_t *lsi, ksiginfo_t *ksi, int sig)
671 ksi->ksi_signo = sig;
672 ksi->ksi_code = lsi->lsi_code; /* XXX. Convert. */
673 ksi->ksi_pid = lsi->lsi_pid;
674 ksi->ksi_uid = lsi->lsi_uid;
675 ksi->ksi_status = lsi->lsi_status;
676 ksi->ksi_addr = PTRIN(lsi->lsi_addr);
677 ksi->ksi_info.si_value.sival_int = lsi->lsi_int;
681 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
689 if (!LINUX_SIG_VALID(args->sig))
692 error = copyin(args->info, &linfo, sizeof(linfo));
696 if (linfo.lsi_code >= 0)
699 sig = linux_to_bsd_signal(args->sig);
702 if ((p = pfind_any(args->pid)) != NULL) {
703 error = p_cansignal(td, p, sig);
710 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
711 error = tdsendsignal(p, NULL, sig, &ksi);
719 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
727 if (!LINUX_SIG_VALID(args->sig))
730 error = copyin(args->uinfo, &linfo, sizeof(linfo));
734 if (linfo.lsi_code >= 0)
737 tds = linux_tdfind(td, args->tid, args->tgid);
741 sig = linux_to_bsd_signal(args->sig);
743 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
744 return (linux_do_tkill(td, tds, &ksi));