2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
65 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
70 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
71 bss->__bits[1] = lss->__bits[1];
72 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
73 if (LINUX_SIGISMEMBER(*lss, l)) {
74 b = linux_to_bsd_signal[_SIG_IDX(l)];
82 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
86 LINUX_SIGEMPTYSET(*lss);
87 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
88 lss->__bits[1] = bss->__bits[1];
89 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
90 if (SIGISMEMBER(*bss, b)) {
91 l = bsd_to_linux_signal[_SIG_IDX(b)];
93 LINUX_SIGADDSET(*lss, l);
97 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
100 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
103 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
104 bsa->sa_handler = PTRIN(lsa->lsa_handler);
106 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
107 bsa->sa_flags |= SA_NOCLDSTOP;
108 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
109 bsa->sa_flags |= SA_NOCLDWAIT;
110 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
111 bsa->sa_flags |= SA_SIGINFO;
112 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
113 bsa->sa_flags |= SA_ONSTACK;
114 if (lsa->lsa_flags & LINUX_SA_RESTART)
115 bsa->sa_flags |= SA_RESTART;
116 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
117 bsa->sa_flags |= SA_RESETHAND;
118 if (lsa->lsa_flags & LINUX_SA_NOMASK)
119 bsa->sa_flags |= SA_NODEFER;
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);
167 if (linux_sig <= LINUX_SIGTBLSZ)
168 sig = linux_to_bsd_signal[_SIG_IDX(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;
191 printf(ARGS(signal, "%d, %p"),
192 args->sig, (void *)(uintptr_t)args->handler);
195 nsa.lsa_handler = args->handler;
196 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
197 LINUX_SIGEMPTYSET(nsa.lsa_mask);
199 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
200 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
204 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
207 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
209 l_sigaction_t nsa, osa;
213 if (ldebug(rt_sigaction))
214 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
215 (long)args->sig, (void *)args->act,
216 (void *)args->oact, (long)args->sigsetsize);
219 if (args->sigsetsize != sizeof(l_sigset_t))
222 if (args->act != NULL) {
223 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
228 error = linux_do_sigaction(td, args->sig,
229 args->act ? &nsa : NULL,
230 args->oact ? &osa : NULL);
232 if (args->oact != NULL && !error) {
233 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
240 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
243 sigset_t omask, nmask;
247 td->td_retval[0] = 0;
250 case LINUX_SIG_BLOCK:
253 case LINUX_SIG_UNBLOCK:
256 case LINUX_SIG_SETMASK:
263 linux_to_bsd_sigset(new, &nmask);
267 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
268 if (error == 0 && old != NULL)
269 bsd_to_linux_sigset(&omask, old);
274 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
276 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
279 l_sigset_t set, oset;
283 if (ldebug(sigprocmask))
284 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
287 if (args->mask != NULL) {
288 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
291 LINUX_SIGEMPTYSET(set);
292 set.__bits[0] = mask;
295 error = linux_do_sigprocmask(td, args->how,
296 args->mask ? &set : NULL,
297 args->omask ? &oset : NULL);
299 if (args->omask != NULL && !error) {
300 mask = oset.__bits[0];
301 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
306 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
309 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
311 l_sigset_t set, oset;
315 if (ldebug(rt_sigprocmask))
316 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
317 args->how, (void *)args->mask,
318 (void *)args->omask, (long)args->sigsetsize);
321 if (args->sigsetsize != sizeof(l_sigset_t))
324 if (args->mask != NULL) {
325 error = copyin(args->mask, &set, sizeof(l_sigset_t));
330 error = linux_do_sigprocmask(td, args->how,
331 args->mask ? &set : NULL,
332 args->omask ? &oset : NULL);
334 if (args->omask != NULL && !error) {
335 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
341 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
343 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
345 struct proc *p = td->td_proc;
349 if (ldebug(sgetmask))
350 printf(ARGS(sgetmask, ""));
354 bsd_to_linux_sigset(&td->td_sigmask, &mask);
356 td->td_retval[0] = mask.__bits[0];
361 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
363 struct proc *p = td->td_proc;
368 if (ldebug(ssetmask))
369 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
373 bsd_to_linux_sigset(&td->td_sigmask, &lset);
374 td->td_retval[0] = lset.__bits[0];
375 LINUX_SIGEMPTYSET(lset);
376 lset.__bits[0] = args->mask;
377 linux_to_bsd_sigset(&lset, &bset);
378 td->td_sigmask = bset;
379 SIG_CANTMASK(td->td_sigmask);
386 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
388 struct proc *p = td->td_proc;
394 if (ldebug(sigpending))
395 printf(ARGS(sigpending, "*"));
400 SIGSETOR(bset, td->td_siglist);
401 SIGSETAND(bset, td->td_sigmask);
403 bsd_to_linux_sigset(&bset, &lset);
404 mask = lset.__bits[0];
405 return (copyout(&mask, args->mask, sizeof(mask)));
407 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
413 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
415 struct proc *p = td->td_proc;
419 if (args->sigsetsize > sizeof(lset))
424 if (ldebug(rt_sigpending))
425 printf(ARGS(rt_sigpending, "*"));
430 SIGSETOR(bset, td->td_siglist);
431 SIGSETAND(bset, td->td_sigmask);
433 bsd_to_linux_sigset(&bset, &lset);
434 return (copyout(&lset, args->set, args->sigsetsize));
441 linux_rt_sigtimedwait(struct thread *td,
442 struct linux_rt_sigtimedwait_args *args)
447 struct timespec ts, *tsa;
454 if (ldebug(rt_sigtimedwait))
455 printf(ARGS(rt_sigtimedwait, "*"));
457 if (args->sigsetsize != sizeof(l_sigset_t))
460 if ((error = copyin(args->mask, &lset, sizeof(lset))))
462 linux_to_bsd_sigset(&lset, &bset);
466 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
469 if (ldebug(rt_sigtimedwait))
470 printf(LMSG("linux_rt_sigtimedwait: "
471 "incoming timeout (%jd/%jd)\n"),
472 (intmax_t)ltv.tv_sec, (intmax_t)ltv.tv_usec);
474 tv.tv_sec = (long)ltv.tv_sec;
475 tv.tv_usec = (suseconds_t)ltv.tv_usec;
476 if (itimerfix(&tv)) {
478 * The timeout was invalid. Convert it to something
479 * valid that will act as it does under Linux.
481 tv.tv_sec += tv.tv_usec / 1000000;
482 tv.tv_usec %= 1000000;
483 if (tv.tv_usec < 0) {
485 tv.tv_usec += 1000000;
490 if (ldebug(rt_sigtimedwait))
491 printf(LMSG("linux_rt_sigtimedwait: "
492 "converted timeout (%jd/%ld)\n"),
493 (intmax_t)tv.tv_sec, tv.tv_usec);
496 TIMEVAL_TO_TIMESPEC(&tv, &ts);
499 error = kern_sigtimedwait(td, bset, &info, tsa);
501 if (ldebug(rt_sigtimedwait))
502 printf(LMSG("linux_rt_sigtimedwait: "
503 "sigtimedwait returning (%d)\n"), error);
508 sig = BSD_TO_LINUX_SIGNAL(info.ksi_signo);
511 memset(&linfo, 0, sizeof(linfo));
512 ksiginfo_to_lsiginfo(&info, &linfo, sig);
513 error = copyout(&linfo, args->ptr, sizeof(linfo));
516 td->td_retval[0] = sig;
522 linux_kill(struct thread *td, struct linux_kill_args *args)
524 struct kill_args /* {
531 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
535 * Allow signal 0 as a means to check for privileges
537 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
540 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
541 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
543 tmp.signum = args->signum;
546 return (sys_kill(td, &tmp));
550 linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi)
556 AUDIT_ARG_SIGNUM(ksi->ksi_signo);
557 AUDIT_ARG_PID(p->p_pid);
558 AUDIT_ARG_PROCESS(p);
560 error = p_cansignal(td, p, ksi->ksi_signo);
561 if (error != 0 || ksi->ksi_signo == 0)
564 tdksignal(tdt, ksi->ksi_signo, ksi);
572 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
580 printf(ARGS(tgkill, "%d, %d, %d"),
581 args->tgid, args->pid, args->sig);
584 if (args->pid <= 0 || args->tgid <=0)
588 * Allow signal 0 as a means to check for privileges
590 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
593 if (args->sig > 0 && args->sig <= LINUX_SIGTBLSZ)
594 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
598 tdt = linux_tdfind(td, args->pid, args->tgid);
604 ksi.ksi_code = SI_LWP;
606 ksi.ksi_pid = td->td_proc->p_pid;
607 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
608 return (linux_do_tkill(td, tdt, &ksi));
612 * Deprecated since 2.5.75. Replaced by tgkill().
615 linux_tkill(struct thread *td, struct linux_tkill_args *args)
623 printf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
628 if (!LINUX_SIG_VALID(args->sig))
631 if (args->sig > 0 && args->sig <= LINUX_SIGTBLSZ)
632 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
636 tdt = linux_tdfind(td, args->tid, -1);
642 ksi.ksi_code = SI_LWP;
644 ksi.ksi_pid = td->td_proc->p_pid;
645 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
646 return (linux_do_tkill(td, tdt, &ksi));
650 ksiginfo_to_lsiginfo(const ksiginfo_t *ksi, l_siginfo_t *lsi, l_int sig)
653 siginfo_to_lsiginfo(&ksi->ksi_info, lsi, sig);
657 sicode_to_lsicode(int si_code, int *lsi_code)
662 *lsi_code = LINUX_SI_USER;
665 *lsi_code = LINUX_SI_KERNEL;
668 *lsi_code = LINUX_SI_QUEUE;
671 *lsi_code = LINUX_SI_TIMER;
674 *lsi_code = LINUX_SI_MESGQ;
677 *lsi_code = LINUX_SI_ASYNCIO;
680 *lsi_code = LINUX_SI_TKILL;
689 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
692 /* sig alredy converted */
693 lsi->lsi_signo = sig;
694 sicode_to_lsicode(si->si_code, &lsi->lsi_code);
696 switch (si->si_code) {
698 lsi->lsi_pid = si->si_pid;
699 lsi->lsi_uid = si->si_uid;
703 lsi->lsi_int = si->si_value.sival_int;
704 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
705 lsi->lsi_tid = si->si_timerid;
709 lsi->lsi_pid = si->si_pid;
710 lsi->lsi_uid = si->si_uid;
711 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
715 lsi->lsi_int = si->si_value.sival_int;
716 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
723 lsi->lsi_band = si->si_band;
728 lsi->lsi_pid = si->si_pid;
729 lsi->lsi_uid = si->si_uid;
731 if (si->si_code == CLD_STOPPED)
732 lsi->lsi_status = BSD_TO_LINUX_SIGNAL(si->si_status);
733 else if (si->si_code == CLD_CONTINUED)
734 lsi->lsi_status = BSD_TO_LINUX_SIGNAL(SIGCONT);
736 lsi->lsi_status = si->si_status;
743 lsi->lsi_addr = PTROUT(si->si_addr);
747 lsi->lsi_pid = si->si_pid;
748 lsi->lsi_uid = si->si_uid;
749 if (sig >= LINUX_SIGRTMIN) {
750 lsi->lsi_int = si->si_value.sival_int;
751 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
760 lsiginfo_to_ksiginfo(const l_siginfo_t *lsi, ksiginfo_t *ksi, int sig)
763 ksi->ksi_signo = sig;
764 ksi->ksi_code = lsi->lsi_code; /* XXX. Convert. */
765 ksi->ksi_pid = lsi->lsi_pid;
766 ksi->ksi_uid = lsi->lsi_uid;
767 ksi->ksi_status = lsi->lsi_status;
768 ksi->ksi_addr = PTRIN(lsi->lsi_addr);
769 ksi->ksi_info.si_value.sival_int = lsi->lsi_int;
773 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
781 if (!LINUX_SIG_VALID(args->sig))
784 error = copyin(args->info, &linfo, sizeof(linfo));
788 if (linfo.lsi_code >= 0)
791 if (args->sig > 0 && args->sig <= LINUX_SIGTBLSZ)
792 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
797 if ((p = pfind(args->pid)) != NULL ||
798 (p = zpfind(args->pid)) != NULL) {
799 error = p_cansignal(td, p, sig);
806 lsiginfo_to_ksiginfo(&linfo, &ksi, sig);
807 error = tdsendsignal(p, NULL, sig, &ksi);