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)
418 struct kill_args /* {
424 * Allow signal 0 as a means to check for privileges
426 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
429 if (args->signum > 0)
430 tmp.signum = linux_to_bsd_signal(args->signum);
435 return (sys_kill(td, &tmp));
439 linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi)
445 AUDIT_ARG_SIGNUM(ksi->ksi_signo);
446 AUDIT_ARG_PID(p->p_pid);
447 AUDIT_ARG_PROCESS(p);
449 error = p_cansignal(td, p, ksi->ksi_signo);
450 if (error != 0 || ksi->ksi_signo == 0)
453 tdksignal(tdt, ksi->ksi_signo, ksi);
461 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
467 if (args->pid <= 0 || args->tgid <=0)
471 * Allow signal 0 as a means to check for privileges
473 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
477 sig = linux_to_bsd_signal(args->sig);
481 tdt = linux_tdfind(td, args->pid, args->tgid);
487 ksi.ksi_code = SI_LWP;
489 ksi.ksi_pid = td->td_proc->p_pid;
490 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
491 return (linux_do_tkill(td, tdt, &ksi));
495 * Deprecated since 2.5.75. Replaced by tgkill().
498 linux_tkill(struct thread *td, struct linux_tkill_args *args)
507 if (!LINUX_SIG_VALID(args->sig))
510 sig = linux_to_bsd_signal(args->sig);
512 tdt = linux_tdfind(td, args->tid, -1);
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 ksiginfo_to_lsiginfo(const ksiginfo_t *ksi, l_siginfo_t *lsi, l_int sig)
529 siginfo_to_lsiginfo(&ksi->ksi_info, lsi, sig);
533 sicode_to_lsicode(int si_code, int *lsi_code)
538 *lsi_code = LINUX_SI_USER;
541 *lsi_code = LINUX_SI_KERNEL;
544 *lsi_code = LINUX_SI_QUEUE;
547 *lsi_code = LINUX_SI_TIMER;
550 *lsi_code = LINUX_SI_MESGQ;
553 *lsi_code = LINUX_SI_ASYNCIO;
556 *lsi_code = LINUX_SI_TKILL;
565 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
568 /* sig alredy converted */
569 lsi->lsi_signo = sig;
570 sicode_to_lsicode(si->si_code, &lsi->lsi_code);
572 switch (si->si_code) {
574 lsi->lsi_pid = si->si_pid;
575 lsi->lsi_uid = si->si_uid;
579 lsi->lsi_int = si->si_value.sival_int;
580 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
581 lsi->lsi_tid = si->si_timerid;
585 lsi->lsi_pid = si->si_pid;
586 lsi->lsi_uid = si->si_uid;
587 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
591 lsi->lsi_int = si->si_value.sival_int;
592 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
599 lsi->lsi_band = si->si_band;
604 lsi->lsi_pid = si->si_pid;
605 lsi->lsi_uid = si->si_uid;
607 if (si->si_code == CLD_STOPPED)
608 lsi->lsi_status = bsd_to_linux_signal(si->si_status);
609 else if (si->si_code == CLD_CONTINUED)
610 lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
612 lsi->lsi_status = si->si_status;
619 lsi->lsi_addr = PTROUT(si->si_addr);
623 lsi->lsi_pid = si->si_pid;
624 lsi->lsi_uid = si->si_uid;
625 if (sig >= LINUX_SIGRTMIN) {
626 lsi->lsi_int = si->si_value.sival_int;
627 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
636 lsiginfo_to_ksiginfo(const l_siginfo_t *lsi, ksiginfo_t *ksi, int sig)
639 ksi->ksi_signo = sig;
640 ksi->ksi_code = lsi->lsi_code; /* XXX. Convert. */
641 ksi->ksi_pid = lsi->lsi_pid;
642 ksi->ksi_uid = lsi->lsi_uid;
643 ksi->ksi_status = lsi->lsi_status;
644 ksi->ksi_addr = PTRIN(lsi->lsi_addr);
645 ksi->ksi_info.si_value.sival_int = lsi->lsi_int;
649 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
657 if (!LINUX_SIG_VALID(args->sig))
660 error = copyin(args->info, &linfo, sizeof(linfo));
664 if (linfo.lsi_code >= 0)
667 sig = linux_to_bsd_signal(args->sig);
670 if ((p = pfind_any(args->pid)) != NULL) {
671 error = p_cansignal(td, p, sig);
678 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
679 error = tdsendsignal(p, NULL, sig, &ksi);
687 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
695 if (!LINUX_SIG_VALID(args->sig))
698 error = copyin(args->uinfo, &linfo, sizeof(linfo));
702 if (linfo.lsi_code >= 0)
705 tds = linux_tdfind(td, args->tid, args->tgid);
709 sig = linux_to_bsd_signal(args->sig);
711 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
712 return (linux_do_tkill(td, tds, &ksi));