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 "opt_compat.h"
45 #include <machine/../linux32/linux.h>
46 #include <machine/../linux32/linux32_proto.h>
48 #include <machine/../linux/linux.h>
49 #include <machine/../linux/linux_proto.h>
51 #include <compat/linux/linux_signal.h>
52 #include <compat/linux/linux_util.h>
53 #include <compat/linux/linux_emul.h>
56 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
61 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
62 bss->__bits[1] = lss->__bits[1];
63 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
64 if (LINUX_SIGISMEMBER(*lss, l)) {
65 b = linux_to_bsd_signal[_SIG_IDX(l)];
73 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
77 LINUX_SIGEMPTYSET(*lss);
78 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
79 lss->__bits[1] = bss->__bits[1];
80 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
81 if (SIGISMEMBER(*bss, b)) {
82 l = bsd_to_linux_signal[_SIG_IDX(b)];
84 LINUX_SIGADDSET(*lss, l);
90 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
93 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
94 bsa->sa_handler = PTRIN(lsa->lsa_handler);
96 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
97 bsa->sa_flags |= SA_NOCLDSTOP;
98 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
99 bsa->sa_flags |= SA_NOCLDWAIT;
100 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
101 bsa->sa_flags |= SA_SIGINFO;
102 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
103 bsa->sa_flags |= SA_ONSTACK;
104 if (lsa->lsa_flags & LINUX_SA_RESTART)
105 bsa->sa_flags |= SA_RESTART;
106 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
107 bsa->sa_flags |= SA_RESETHAND;
108 if (lsa->lsa_flags & LINUX_SA_NOMASK)
109 bsa->sa_flags |= SA_NODEFER;
113 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
116 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
117 #ifdef COMPAT_LINUX32
118 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
120 lsa->lsa_handler = bsa->sa_handler;
122 lsa->lsa_restorer = 0; /* unsupported */
124 if (bsa->sa_flags & SA_NOCLDSTOP)
125 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
126 if (bsa->sa_flags & SA_NOCLDWAIT)
127 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
128 if (bsa->sa_flags & SA_SIGINFO)
129 lsa->lsa_flags |= LINUX_SA_SIGINFO;
130 if (bsa->sa_flags & SA_ONSTACK)
131 lsa->lsa_flags |= LINUX_SA_ONSTACK;
132 if (bsa->sa_flags & SA_RESTART)
133 lsa->lsa_flags |= LINUX_SA_RESTART;
134 if (bsa->sa_flags & SA_RESETHAND)
135 lsa->lsa_flags |= LINUX_SA_ONESHOT;
136 if (bsa->sa_flags & SA_NODEFER)
137 lsa->lsa_flags |= LINUX_SA_NOMASK;
141 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
142 l_sigaction_t *linux_osa)
144 struct sigaction act, oact, *nsa, *osa;
147 if (!LINUX_SIG_VALID(linux_sig))
150 osa = (linux_osa != NULL) ? &oact : NULL;
151 if (linux_nsa != NULL) {
153 linux_to_bsd_sigaction(linux_nsa, nsa);
157 if (linux_sig <= LINUX_SIGTBLSZ)
158 sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
162 error = kern_sigaction(td, sig, nsa, osa, 0);
166 if (linux_osa != NULL)
167 bsd_to_linux_sigaction(osa, linux_osa);
174 linux_signal(struct thread *td, struct linux_signal_args *args)
176 l_sigaction_t nsa, osa;
181 printf(ARGS(signal, "%d, %p"),
182 args->sig, (void *)(uintptr_t)args->handler);
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;
196 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
198 l_sigaction_t nsa, osa;
202 if (ldebug(rt_sigaction))
203 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
204 (long)args->sig, (void *)args->act,
205 (void *)args->oact, (long)args->sigsetsize);
208 if (args->sigsetsize != sizeof(l_sigset_t))
211 if (args->act != NULL) {
212 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
217 error = linux_do_sigaction(td, args->sig,
218 args->act ? &nsa : NULL,
219 args->oact ? &osa : NULL);
221 if (args->oact != NULL && !error) {
222 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
229 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
232 sigset_t omask, nmask;
236 td->td_retval[0] = 0;
239 case LINUX_SIG_BLOCK:
242 case LINUX_SIG_UNBLOCK:
245 case LINUX_SIG_SETMASK:
252 linux_to_bsd_sigset(new, &nmask);
256 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
257 if (error == 0 && old != NULL)
258 bsd_to_linux_sigset(&omask, old);
264 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
267 l_sigset_t set, oset;
271 if (ldebug(sigprocmask))
272 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
275 if (args->mask != NULL) {
276 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
279 LINUX_SIGEMPTYSET(set);
280 set.__bits[0] = mask;
283 error = linux_do_sigprocmask(td, args->how,
284 args->mask ? &set : NULL,
285 args->omask ? &oset : NULL);
287 if (args->omask != NULL && !error) {
288 mask = oset.__bits[0];
289 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
296 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
298 l_sigset_t set, oset;
302 if (ldebug(rt_sigprocmask))
303 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
304 args->how, (void *)args->mask,
305 (void *)args->omask, (long)args->sigsetsize);
308 if (args->sigsetsize != sizeof(l_sigset_t))
311 if (args->mask != NULL) {
312 error = copyin(args->mask, &set, sizeof(l_sigset_t));
317 error = linux_do_sigprocmask(td, args->how,
318 args->mask ? &set : NULL,
319 args->omask ? &oset : NULL);
321 if (args->omask != NULL && !error) {
322 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
329 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
331 struct proc *p = td->td_proc;
335 if (ldebug(sgetmask))
336 printf(ARGS(sgetmask, ""));
340 bsd_to_linux_sigset(&td->td_sigmask, &mask);
342 td->td_retval[0] = mask.__bits[0];
347 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
349 struct proc *p = td->td_proc;
354 if (ldebug(ssetmask))
355 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
359 bsd_to_linux_sigset(&td->td_sigmask, &lset);
360 td->td_retval[0] = lset.__bits[0];
361 LINUX_SIGEMPTYSET(lset);
362 lset.__bits[0] = args->mask;
363 linux_to_bsd_sigset(&lset, &bset);
364 td->td_sigmask = bset;
365 SIG_CANTMASK(td->td_sigmask);
375 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
377 struct proc *p = td->td_proc;
383 if (ldebug(sigpending))
384 printf(ARGS(sigpending, "*"));
389 SIGSETOR(bset, td->td_siglist);
390 SIGSETAND(bset, td->td_sigmask);
392 bsd_to_linux_sigset(&bset, &lset);
393 mask = lset.__bits[0];
394 return (copyout(&mask, args->mask, sizeof(mask)));
401 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
403 struct proc *p = td->td_proc;
407 if (args->sigsetsize > sizeof(lset))
412 if (ldebug(rt_sigpending))
413 printf(ARGS(rt_sigpending, "*"));
418 SIGSETOR(bset, td->td_siglist);
419 SIGSETAND(bset, td->td_sigmask);
421 bsd_to_linux_sigset(&bset, &lset);
422 return (copyout(&lset, args->set, args->sigsetsize));
429 linux_rt_sigtimedwait(struct thread *td,
430 struct linux_rt_sigtimedwait_args *args)
435 struct timespec ts, *tsa;
442 if (ldebug(rt_sigtimedwait))
443 printf(ARGS(rt_sigtimedwait, "*"));
445 if (args->sigsetsize != sizeof(l_sigset_t))
448 if ((error = copyin(args->mask, &lset, sizeof(lset))))
450 linux_to_bsd_sigset(&lset, &bset);
454 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
457 if (ldebug(rt_sigtimedwait))
458 printf(LMSG("linux_rt_sigtimedwait: incoming timeout (%d/%d)\n"),
459 ltv.tv_sec, ltv.tv_usec);
461 tv.tv_sec = (long)ltv.tv_sec;
462 tv.tv_usec = (suseconds_t)ltv.tv_usec;
463 if (itimerfix(&tv)) {
465 * The timeout was invalid. Convert it to something
466 * valid that will act as it does under Linux.
468 tv.tv_sec += tv.tv_usec / 1000000;
469 tv.tv_usec %= 1000000;
470 if (tv.tv_usec < 0) {
472 tv.tv_usec += 1000000;
477 if (ldebug(rt_sigtimedwait))
478 printf(LMSG("linux_rt_sigtimedwait: converted timeout (%jd/%ld)\n"),
479 (intmax_t)tv.tv_sec, tv.tv_usec);
482 TIMEVAL_TO_TIMESPEC(&tv, &ts);
485 error = kern_sigtimedwait(td, bset, &info, tsa);
487 if (ldebug(rt_sigtimedwait))
488 printf(LMSG("linux_rt_sigtimedwait: sigtimedwait returning (%d)\n"), error);
494 memset(&linfo, 0, sizeof(linfo));
495 linfo.lsi_signo = info.ksi_signo;
496 error = copyout(&linfo, args->ptr, sizeof(linfo));
499 /* Repost if we got an error. */
500 if (error && info.ksi_signo) {
501 PROC_LOCK(td->td_proc);
502 tdsignal(td->td_proc, td, info.ksi_signo, &info);
503 PROC_UNLOCK(td->td_proc);
505 td->td_retval[0] = info.ksi_signo;
511 linux_kill(struct thread *td, struct linux_kill_args *args)
513 struct kill_args /* {
520 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
524 * Allow signal 0 as a means to check for privileges
526 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
529 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
530 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
532 tmp.signum = args->signum;
535 return (kill(td, &tmp));
539 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
541 struct linux_emuldata *em;
542 struct linux_kill_args ka;
547 printf(ARGS(tgkill, "%d, %d, %d"), args->tgid, args->pid, args->sig);
551 ka.signum = args->sig;
553 if (args->tgid == -1)
554 return linux_kill(td, &ka);
556 if ((p = pfind(args->pid)) == NULL)
559 if (p->p_sysent != &elf_linux_sysvec)
564 em = em_find(p, EMUL_DONTLOCK);
568 printf("emuldata not found in tgkill.\n");
573 if (em->shared->group_pid != args->tgid)
576 return linux_kill(td, &ka);
580 linux_tkill(struct thread *td, struct linux_tkill_args *args)
584 printf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
587 return (linux_kill(td, (struct linux_kill_args *) args));