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;
149 nsa.lsa_handler = args->handler;
150 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
151 LINUX_SIGEMPTYSET(nsa.lsa_mask);
153 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
154 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
158 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
161 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
163 l_sigaction_t nsa, osa;
166 if (args->sigsetsize != sizeof(l_sigset_t))
169 if (args->act != NULL) {
170 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
175 error = linux_do_sigaction(td, args->sig,
176 args->act ? &nsa : NULL,
177 args->oact ? &osa : NULL);
179 if (args->oact != NULL && !error) {
180 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
187 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
190 sigset_t omask, nmask;
194 td->td_retval[0] = 0;
197 case LINUX_SIG_BLOCK:
200 case LINUX_SIG_UNBLOCK:
203 case LINUX_SIG_SETMASK:
210 linux_to_bsd_sigset(new, &nmask);
214 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
215 if (error == 0 && old != NULL)
216 bsd_to_linux_sigset(&omask, old);
221 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
223 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
226 l_sigset_t set, oset;
229 if (args->mask != NULL) {
230 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
233 LINUX_SIGEMPTYSET(set);
237 error = linux_do_sigprocmask(td, args->how,
238 args->mask ? &set : NULL,
239 args->omask ? &oset : NULL);
241 if (args->omask != NULL && !error) {
243 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
248 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
251 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
253 l_sigset_t set, oset;
256 if (args->sigsetsize != sizeof(l_sigset_t))
259 if (args->mask != NULL) {
260 error = copyin(args->mask, &set, sizeof(l_sigset_t));
265 error = linux_do_sigprocmask(td, args->how,
266 args->mask ? &set : NULL,
267 args->omask ? &oset : NULL);
269 if (args->omask != NULL && !error) {
270 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
276 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
278 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
280 struct proc *p = td->td_proc;
284 bsd_to_linux_sigset(&td->td_sigmask, &mask);
286 td->td_retval[0] = mask.__mask;
291 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
293 struct proc *p = td->td_proc;
298 bsd_to_linux_sigset(&td->td_sigmask, &lset);
299 td->td_retval[0] = lset.__mask;
300 LINUX_SIGEMPTYSET(lset);
301 lset.__mask = args->mask;
302 linux_to_bsd_sigset(&lset, &bset);
303 td->td_sigmask = bset;
304 SIG_CANTMASK(td->td_sigmask);
311 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
313 struct proc *p = td->td_proc;
320 SIGSETOR(bset, td->td_siglist);
321 SIGSETAND(bset, td->td_sigmask);
323 bsd_to_linux_sigset(&bset, &lset);
325 return (copyout(&mask, args->mask, sizeof(mask)));
327 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
333 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
335 struct proc *p = td->td_proc;
339 if (args->sigsetsize > sizeof(lset))
345 SIGSETOR(bset, td->td_siglist);
346 SIGSETAND(bset, td->td_sigmask);
348 bsd_to_linux_sigset(&bset, &lset);
349 return (copyout(&lset, args->set, args->sigsetsize));
356 linux_rt_sigtimedwait(struct thread *td,
357 struct linux_rt_sigtimedwait_args *args)
362 struct timespec ts, *tsa;
368 if (args->sigsetsize != sizeof(l_sigset_t))
371 if ((error = copyin(args->mask, &lset, sizeof(lset))))
373 linux_to_bsd_sigset(&lset, &bset);
377 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
379 tv.tv_sec = (long)ltv.tv_sec;
380 tv.tv_usec = (suseconds_t)ltv.tv_usec;
381 if (itimerfix(&tv)) {
383 * The timeout was invalid. Convert it to something
384 * valid that will act as it does under Linux.
386 tv.tv_sec += tv.tv_usec / 1000000;
387 tv.tv_usec %= 1000000;
388 if (tv.tv_usec < 0) {
390 tv.tv_usec += 1000000;
395 TIMEVAL_TO_TIMESPEC(&tv, &ts);
398 error = kern_sigtimedwait(td, bset, &info, tsa);
402 sig = bsd_to_linux_signal(info.ksi_signo);
405 memset(&linfo, 0, sizeof(linfo));
406 ksiginfo_to_lsiginfo(&info, &linfo, sig);
407 error = copyout(&linfo, args->ptr, sizeof(linfo));
410 td->td_retval[0] = sig;
416 linux_kill(struct thread *td, struct linux_kill_args *args)
421 * Allow signal 0 as a means to check for privileges
423 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
426 if (args->signum > 0)
427 l_signum = linux_to_bsd_signal(args->signum);
431 return (kern_kill(td, args->pid, l_signum));
435 linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi)
441 AUDIT_ARG_SIGNUM(ksi->ksi_signo);
442 AUDIT_ARG_PID(p->p_pid);
443 AUDIT_ARG_PROCESS(p);
445 error = p_cansignal(td, p, ksi->ksi_signo);
446 if (error != 0 || ksi->ksi_signo == 0)
449 tdksignal(tdt, ksi->ksi_signo, ksi);
457 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
463 if (args->pid <= 0 || args->tgid <=0)
467 * Allow signal 0 as a means to check for privileges
469 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
473 sig = linux_to_bsd_signal(args->sig);
477 tdt = linux_tdfind(td, args->pid, args->tgid);
483 ksi.ksi_code = SI_LWP;
485 ksi.ksi_pid = td->td_proc->p_pid;
486 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
487 return (linux_do_tkill(td, tdt, &ksi));
491 * Deprecated since 2.5.75. Replaced by tgkill().
494 linux_tkill(struct thread *td, struct linux_tkill_args *args)
503 if (!LINUX_SIG_VALID(args->sig))
506 sig = linux_to_bsd_signal(args->sig);
508 tdt = linux_tdfind(td, args->tid, -1);
514 ksi.ksi_code = SI_LWP;
516 ksi.ksi_pid = td->td_proc->p_pid;
517 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
518 return (linux_do_tkill(td, tdt, &ksi));
522 ksiginfo_to_lsiginfo(const ksiginfo_t *ksi, l_siginfo_t *lsi, l_int sig)
525 siginfo_to_lsiginfo(&ksi->ksi_info, lsi, sig);
529 sicode_to_lsicode(int si_code, int *lsi_code)
534 *lsi_code = LINUX_SI_USER;
537 *lsi_code = LINUX_SI_KERNEL;
540 *lsi_code = LINUX_SI_QUEUE;
543 *lsi_code = LINUX_SI_TIMER;
546 *lsi_code = LINUX_SI_MESGQ;
549 *lsi_code = LINUX_SI_ASYNCIO;
552 *lsi_code = LINUX_SI_TKILL;
561 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
564 /* sig alredy converted */
565 lsi->lsi_signo = sig;
566 sicode_to_lsicode(si->si_code, &lsi->lsi_code);
568 switch (si->si_code) {
570 lsi->lsi_pid = si->si_pid;
571 lsi->lsi_uid = si->si_uid;
575 lsi->lsi_int = si->si_value.sival_int;
576 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
577 lsi->lsi_tid = si->si_timerid;
581 lsi->lsi_pid = si->si_pid;
582 lsi->lsi_uid = si->si_uid;
583 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
587 lsi->lsi_int = si->si_value.sival_int;
588 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
595 lsi->lsi_band = si->si_band;
600 lsi->lsi_pid = si->si_pid;
601 lsi->lsi_uid = si->si_uid;
603 if (si->si_code == CLD_STOPPED)
604 lsi->lsi_status = bsd_to_linux_signal(si->si_status);
605 else if (si->si_code == CLD_CONTINUED)
606 lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
608 lsi->lsi_status = si->si_status;
615 lsi->lsi_addr = PTROUT(si->si_addr);
619 lsi->lsi_pid = si->si_pid;
620 lsi->lsi_uid = si->si_uid;
621 if (sig >= LINUX_SIGRTMIN) {
622 lsi->lsi_int = si->si_value.sival_int;
623 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
632 lsiginfo_to_ksiginfo(const l_siginfo_t *lsi, ksiginfo_t *ksi, int sig)
635 ksi->ksi_signo = sig;
636 ksi->ksi_code = lsi->lsi_code; /* XXX. Convert. */
637 ksi->ksi_pid = lsi->lsi_pid;
638 ksi->ksi_uid = lsi->lsi_uid;
639 ksi->ksi_status = lsi->lsi_status;
640 ksi->ksi_addr = PTRIN(lsi->lsi_addr);
641 ksi->ksi_info.si_value.sival_int = lsi->lsi_int;
645 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
653 if (!LINUX_SIG_VALID(args->sig))
656 error = copyin(args->info, &linfo, sizeof(linfo));
660 if (linfo.lsi_code >= 0)
663 sig = linux_to_bsd_signal(args->sig);
666 if ((p = pfind_any(args->pid)) != NULL) {
667 error = p_cansignal(td, p, sig);
674 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
675 error = tdsendsignal(p, NULL, sig, &ksi);
683 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
691 if (!LINUX_SIG_VALID(args->sig))
694 error = copyin(args->uinfo, &linfo, sizeof(linfo));
698 if (linfo.lsi_code >= 0)
701 tds = linux_tdfind(td, args->tid, args->tgid);
705 sig = linux_to_bsd_signal(args->sig);
707 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
708 return (linux_do_tkill(td, tds, &ksi));