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,
62 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
67 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
68 bss->__bits[1] = lss->__bits[1];
69 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
70 if (LINUX_SIGISMEMBER(*lss, l)) {
71 b = linux_to_bsd_signal[_SIG_IDX(l)];
79 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
83 LINUX_SIGEMPTYSET(*lss);
84 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
85 lss->__bits[1] = bss->__bits[1];
86 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
87 if (SIGISMEMBER(*bss, b)) {
88 l = bsd_to_linux_signal[_SIG_IDX(b)];
90 LINUX_SIGADDSET(*lss, l);
96 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
99 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
100 bsa->sa_handler = PTRIN(lsa->lsa_handler);
102 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
103 bsa->sa_flags |= SA_NOCLDSTOP;
104 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
105 bsa->sa_flags |= SA_NOCLDWAIT;
106 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
107 bsa->sa_flags |= SA_SIGINFO;
108 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
109 bsa->sa_flags |= SA_ONSTACK;
110 if (lsa->lsa_flags & LINUX_SA_RESTART)
111 bsa->sa_flags |= SA_RESTART;
112 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
113 bsa->sa_flags |= SA_RESETHAND;
114 if (lsa->lsa_flags & LINUX_SA_NOMASK)
115 bsa->sa_flags |= SA_NODEFER;
119 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
122 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
123 #ifdef COMPAT_LINUX32
124 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
126 lsa->lsa_handler = bsa->sa_handler;
128 lsa->lsa_restorer = 0; /* unsupported */
130 if (bsa->sa_flags & SA_NOCLDSTOP)
131 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
132 if (bsa->sa_flags & SA_NOCLDWAIT)
133 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
134 if (bsa->sa_flags & SA_SIGINFO)
135 lsa->lsa_flags |= LINUX_SA_SIGINFO;
136 if (bsa->sa_flags & SA_ONSTACK)
137 lsa->lsa_flags |= LINUX_SA_ONSTACK;
138 if (bsa->sa_flags & SA_RESTART)
139 lsa->lsa_flags |= LINUX_SA_RESTART;
140 if (bsa->sa_flags & SA_RESETHAND)
141 lsa->lsa_flags |= LINUX_SA_ONESHOT;
142 if (bsa->sa_flags & SA_NODEFER)
143 lsa->lsa_flags |= LINUX_SA_NOMASK;
147 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
148 l_sigaction_t *linux_osa)
150 struct sigaction act, oact, *nsa, *osa;
153 if (!LINUX_SIG_VALID(linux_sig))
156 osa = (linux_osa != NULL) ? &oact : NULL;
157 if (linux_nsa != NULL) {
159 linux_to_bsd_sigaction(linux_nsa, nsa);
163 if (linux_sig <= LINUX_SIGTBLSZ)
164 sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
168 error = kern_sigaction(td, sig, nsa, osa, 0);
172 if (linux_osa != NULL)
173 bsd_to_linux_sigaction(osa, linux_osa);
180 linux_signal(struct thread *td, struct linux_signal_args *args)
182 l_sigaction_t nsa, osa;
187 printf(ARGS(signal, "%d, %p"),
188 args->sig, (void *)(uintptr_t)args->handler);
191 nsa.lsa_handler = args->handler;
192 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
193 LINUX_SIGEMPTYSET(nsa.lsa_mask);
195 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
196 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
202 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
204 l_sigaction_t nsa, osa;
208 if (ldebug(rt_sigaction))
209 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
210 (long)args->sig, (void *)args->act,
211 (void *)args->oact, (long)args->sigsetsize);
214 if (args->sigsetsize != sizeof(l_sigset_t))
217 if (args->act != NULL) {
218 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
223 error = linux_do_sigaction(td, args->sig,
224 args->act ? &nsa : NULL,
225 args->oact ? &osa : NULL);
227 if (args->oact != NULL && !error) {
228 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
235 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
238 sigset_t omask, nmask;
242 td->td_retval[0] = 0;
245 case LINUX_SIG_BLOCK:
248 case LINUX_SIG_UNBLOCK:
251 case LINUX_SIG_SETMASK:
258 linux_to_bsd_sigset(new, &nmask);
262 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
263 if (error == 0 && old != NULL)
264 bsd_to_linux_sigset(&omask, old);
270 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
273 l_sigset_t set, oset;
277 if (ldebug(sigprocmask))
278 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
281 if (args->mask != NULL) {
282 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
285 LINUX_SIGEMPTYSET(set);
286 set.__bits[0] = mask;
289 error = linux_do_sigprocmask(td, args->how,
290 args->mask ? &set : NULL,
291 args->omask ? &oset : NULL);
293 if (args->omask != NULL && !error) {
294 mask = oset.__bits[0];
295 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
302 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
304 l_sigset_t set, oset;
308 if (ldebug(rt_sigprocmask))
309 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
310 args->how, (void *)args->mask,
311 (void *)args->omask, (long)args->sigsetsize);
314 if (args->sigsetsize != sizeof(l_sigset_t))
317 if (args->mask != NULL) {
318 error = copyin(args->mask, &set, sizeof(l_sigset_t));
323 error = linux_do_sigprocmask(td, args->how,
324 args->mask ? &set : NULL,
325 args->omask ? &oset : NULL);
327 if (args->omask != NULL && !error) {
328 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
335 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
337 struct proc *p = td->td_proc;
341 if (ldebug(sgetmask))
342 printf(ARGS(sgetmask, ""));
346 bsd_to_linux_sigset(&td->td_sigmask, &mask);
348 td->td_retval[0] = mask.__bits[0];
353 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
355 struct proc *p = td->td_proc;
360 if (ldebug(ssetmask))
361 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
365 bsd_to_linux_sigset(&td->td_sigmask, &lset);
366 td->td_retval[0] = lset.__bits[0];
367 LINUX_SIGEMPTYSET(lset);
368 lset.__bits[0] = args->mask;
369 linux_to_bsd_sigset(&lset, &bset);
370 td->td_sigmask = bset;
371 SIG_CANTMASK(td->td_sigmask);
381 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
383 struct proc *p = td->td_proc;
389 if (ldebug(sigpending))
390 printf(ARGS(sigpending, "*"));
395 SIGSETOR(bset, td->td_siglist);
396 SIGSETAND(bset, td->td_sigmask);
398 bsd_to_linux_sigset(&bset, &lset);
399 mask = lset.__bits[0];
400 return (copyout(&mask, args->mask, sizeof(mask)));
407 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
409 struct proc *p = td->td_proc;
413 if (args->sigsetsize > sizeof(lset))
418 if (ldebug(rt_sigpending))
419 printf(ARGS(rt_sigpending, "*"));
424 SIGSETOR(bset, td->td_siglist);
425 SIGSETAND(bset, td->td_sigmask);
427 bsd_to_linux_sigset(&bset, &lset);
428 return (copyout(&lset, args->set, args->sigsetsize));
435 linux_rt_sigtimedwait(struct thread *td,
436 struct linux_rt_sigtimedwait_args *args)
441 struct timespec ts, *tsa;
448 if (ldebug(rt_sigtimedwait))
449 printf(ARGS(rt_sigtimedwait, "*"));
451 if (args->sigsetsize != sizeof(l_sigset_t))
454 if ((error = copyin(args->mask, &lset, sizeof(lset))))
456 linux_to_bsd_sigset(&lset, &bset);
460 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
463 if (ldebug(rt_sigtimedwait))
464 printf(LMSG("linux_rt_sigtimedwait: "
465 "incoming timeout (%d/%d)\n"),
466 ltv.tv_sec, ltv.tv_usec);
468 tv.tv_sec = (long)ltv.tv_sec;
469 tv.tv_usec = (suseconds_t)ltv.tv_usec;
470 if (itimerfix(&tv)) {
472 * The timeout was invalid. Convert it to something
473 * valid that will act as it does under Linux.
475 tv.tv_sec += tv.tv_usec / 1000000;
476 tv.tv_usec %= 1000000;
477 if (tv.tv_usec < 0) {
479 tv.tv_usec += 1000000;
484 if (ldebug(rt_sigtimedwait))
485 printf(LMSG("linux_rt_sigtimedwait: "
486 "converted timeout (%jd/%ld)\n"),
487 (intmax_t)tv.tv_sec, tv.tv_usec);
490 TIMEVAL_TO_TIMESPEC(&tv, &ts);
493 error = kern_sigtimedwait(td, bset, &info, tsa);
495 if (ldebug(rt_sigtimedwait))
496 printf(LMSG("linux_rt_sigtimedwait: "
497 "sigtimedwait returning (%d)\n"), error);
502 sig = BSD_TO_LINUX_SIGNAL(info.ksi_signo);
505 memset(&linfo, 0, sizeof(linfo));
506 ksiginfo_to_lsiginfo(&info, &linfo, sig);
507 error = copyout(&linfo, args->ptr, sizeof(linfo));
510 td->td_retval[0] = sig;
516 linux_kill(struct thread *td, struct linux_kill_args *args)
518 struct kill_args /* {
525 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
529 * Allow signal 0 as a means to check for privileges
531 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
534 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
535 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
537 tmp.signum = args->signum;
540 return (sys_kill(td, &tmp));
544 linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi)
550 AUDIT_ARG_SIGNUM(ksi->ksi_signo);
551 AUDIT_ARG_PID(p->p_pid);
552 AUDIT_ARG_PROCESS(p);
554 error = p_cansignal(td, p, ksi->ksi_signo);
555 if (error != 0 || ksi->ksi_signo == 0)
558 tdksignal(tdt, ksi->ksi_signo, ksi);
566 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
574 printf(ARGS(tgkill, "%d, %d, %d"),
575 args->tgid, args->pid, args->sig);
578 if (args->pid <= 0 || args->tgid <=0)
582 * Allow signal 0 as a means to check for privileges
584 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
587 if (args->sig > 0 && args->sig <= LINUX_SIGTBLSZ)
588 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
592 tdt = linux_tdfind(td, args->pid, args->tgid);
598 ksi.ksi_code = LINUX_SI_TKILL;
600 ksi.ksi_pid = td->td_proc->p_pid;
601 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
602 return (linux_do_tkill(td, tdt, &ksi));
606 * Deprecated since 2.5.75. Replaced by tgkill().
609 linux_tkill(struct thread *td, struct linux_tkill_args *args)
617 printf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
622 if (!LINUX_SIG_VALID(args->sig))
625 if (args->sig > 0 && args->sig <= LINUX_SIGTBLSZ)
626 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
630 tdt = linux_tdfind(td, args->tid, -1);
636 ksi.ksi_code = LINUX_SI_TKILL;
638 ksi.ksi_pid = td->td_proc->p_pid;
639 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
640 return (linux_do_tkill(td, tdt, &ksi));
644 ksiginfo_to_lsiginfo(ksiginfo_t *ksi, l_siginfo_t *lsi, l_int sig)
647 lsi->lsi_signo = sig;
648 lsi->lsi_code = ksi->ksi_code;
653 lsi->lsi_band = ksi->ksi_band;
656 lsi->lsi_pid = ksi->ksi_pid;
657 lsi->lsi_uid = ksi->ksi_uid;
658 lsi->lsi_status = ksi->ksi_status;
664 lsi->lsi_addr = PTROUT(ksi->ksi_addr);
667 /* XXX SI_TIMER etc... */
668 lsi->lsi_pid = ksi->ksi_pid;
669 lsi->lsi_uid = ksi->ksi_uid;
672 if (sig >= LINUX_SIGRTMIN) {
673 lsi->lsi_int = ksi->ksi_info.si_value.sival_int;
674 lsi->lsi_ptr = PTROUT(ksi->ksi_info.si_value.sival_ptr);