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.
28 * $FreeBSD: src/sys/compat/linux/linux_signal.c,v 1.23.2.3 2001/11/05 19:08:23 marcel Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/signalvar.h>
36 #include <sys/sysproto.h>
37 #include <sys/kern_syscall.h>
38 #include <sys/thread.h>
40 #include <sys/thread2.h>
42 #include <arch_linux/linux.h>
43 #include <arch_linux/linux_proto.h>
44 #include "linux_emuldata.h"
45 #include "linux_signal.h"
46 #include "linux_util.h"
49 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
54 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
55 bss->__bits[1] = lss->__bits[1];
56 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
57 if (LINUX_SIGISMEMBER(*lss, l)) {
58 b = linux_to_bsd_signal[_SIG_IDX(l)];
66 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
70 LINUX_SIGEMPTYSET(*lss);
71 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
72 lss->__bits[1] = bss->__bits[1];
73 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
74 if (SIGISMEMBER(*bss, b)) {
75 l = bsd_to_linux_signal[_SIG_IDX(b)];
77 LINUX_SIGADDSET(*lss, l);
83 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
86 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
87 bsa->sa_handler = lsa->lsa_handler;
89 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
90 bsa->sa_flags |= SA_NOCLDSTOP;
91 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
92 bsa->sa_flags |= SA_NOCLDWAIT;
93 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
94 bsa->sa_flags |= SA_SIGINFO;
95 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
96 bsa->sa_flags |= SA_ONSTACK;
97 if (lsa->lsa_flags & LINUX_SA_RESTART)
98 bsa->sa_flags |= SA_RESTART;
99 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
100 bsa->sa_flags |= SA_RESETHAND;
101 if (lsa->lsa_flags & LINUX_SA_NOMASK)
102 bsa->sa_flags |= SA_NODEFER;
106 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
109 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
110 lsa->lsa_handler = bsa->sa_handler;
111 lsa->lsa_restorer = NULL; /* unsupported */
113 if (bsa->sa_flags & SA_NOCLDSTOP)
114 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
115 if (bsa->sa_flags & SA_NOCLDWAIT)
116 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
117 if (bsa->sa_flags & SA_SIGINFO)
118 lsa->lsa_flags |= LINUX_SA_SIGINFO;
119 if (bsa->sa_flags & SA_ONSTACK)
120 lsa->lsa_flags |= LINUX_SA_ONSTACK;
121 if (bsa->sa_flags & SA_RESTART)
122 lsa->lsa_flags |= LINUX_SA_RESTART;
123 if (bsa->sa_flags & SA_RESETHAND)
124 lsa->lsa_flags |= LINUX_SA_ONESHOT;
125 if (bsa->sa_flags & SA_NODEFER)
126 lsa->lsa_flags |= LINUX_SA_NOMASK;
133 sys_linux_signal(struct linux_signal_args *args)
135 l_sigaction_t linux_nsa, linux_osa;
136 struct sigaction nsa, osa;
141 kprintf(ARGS(signal, "%d, %p"),
142 args->sig, (void *)args->handler);
144 linux_nsa.lsa_handler = args->handler;
145 linux_nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
146 LINUX_SIGEMPTYSET(linux_nsa.lsa_mask);
147 linux_to_bsd_sigaction(&linux_nsa, &nsa);
148 if (args->sig <= LINUX_SIGTBLSZ) {
149 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
154 error = kern_sigaction(sig, &nsa, &osa);
156 bsd_to_linux_sigaction(&osa, &linux_osa);
157 args->sysmsg_result = (int) linux_osa.lsa_handler;
165 sys_linux_rt_sigaction(struct linux_rt_sigaction_args *args)
167 l_sigaction_t linux_nsa, linux_osa;
168 struct sigaction nsa, osa;
172 if (ldebug(rt_sigaction))
173 kprintf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
174 (long)args->sig, (void *)args->act,
175 (void *)args->oact, (long)args->sigsetsize);
177 if (args->sigsetsize != sizeof(l_sigset_t))
181 error = copyin(args->act, &linux_nsa, sizeof(linux_nsa));
184 linux_to_bsd_sigaction(&linux_nsa, &nsa);
186 if (args->sig <= LINUX_SIGTBLSZ) {
187 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
192 error = kern_sigaction(sig, args->act ? &nsa : NULL,
193 args->oact ? &osa : NULL);
195 if (error == 0 && args->oact) {
196 bsd_to_linux_sigaction(&osa, &linux_osa);
197 error = copyout(&linux_osa, args->oact, sizeof(linux_osa));
204 linux_to_bsd_sigprocmask(int how)
207 case LINUX_SIG_BLOCK:
209 case LINUX_SIG_UNBLOCK:
211 case LINUX_SIG_SETMASK:
222 sys_linux_sigprocmask(struct linux_sigprocmask_args *args)
225 l_sigset_t linux_set, linux_oset;
230 if (ldebug(sigprocmask))
231 kprintf(ARGS(sigprocmask, "%d, *, *"), args->how);
235 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
238 LINUX_SIGEMPTYSET(linux_set);
239 linux_set.__bits[0] = mask;
240 linux_to_bsd_sigset(&linux_set, &set);
242 how = linux_to_bsd_sigprocmask(args->how);
244 error = kern_sigprocmask(how, args->mask ? &set : NULL,
245 args->omask ? &oset : NULL);
247 if (error == 0 && args->omask) {
248 bsd_to_linux_sigset(&oset, &linux_oset);
249 mask = linux_oset.__bits[0];
250 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
259 sys_linux_rt_sigprocmask(struct linux_rt_sigprocmask_args *args)
261 l_sigset_t linux_set, linux_oset;
266 if (ldebug(rt_sigprocmask))
267 kprintf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
268 args->how, (void *)args->mask,
269 (void *)args->omask, (long)args->sigsetsize);
272 if (args->sigsetsize != sizeof(l_sigset_t))
276 error = copyin(args->mask, &linux_set, sizeof(l_sigset_t));
279 linux_to_bsd_sigset(&linux_set, &set);
281 how = linux_to_bsd_sigprocmask(args->how);
283 error = kern_sigprocmask(how, args->mask ? &set : NULL,
284 args->omask ? &oset : NULL);
286 if (error == 0 && args->omask) {
287 bsd_to_linux_sigset(&oset, &linux_oset);
288 error = copyout(&linux_oset, args->omask, sizeof(l_sigset_t));
298 sys_linux_sgetmask(struct linux_sgetmask_args *args)
300 struct lwp *lp = curthread->td_lwp;
304 if (ldebug(sgetmask))
305 kprintf(ARGS(sgetmask, ""));
308 bsd_to_linux_sigset(&lp->lwp_sigmask, &mask);
309 args->sysmsg_result = mask.__bits[0];
317 sys_linux_ssetmask(struct linux_ssetmask_args *args)
319 struct lwp *lp = curthread->td_lwp;
324 if (ldebug(ssetmask))
325 kprintf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
328 bsd_to_linux_sigset(&lp->lwp_sigmask, &lset);
329 args->sysmsg_result = lset.__bits[0];
330 LINUX_SIGEMPTYSET(lset);
331 lset.__bits[0] = args->mask;
332 linux_to_bsd_sigset(&lset, &bset);
334 lp->lwp_sigmask = bset;
335 SIG_CANTMASK(lp->lwp_sigmask);
344 sys_linux_sigpending(struct linux_sigpending_args *args)
346 struct thread *td = curthread;
347 struct lwp *lp = td->td_lwp;
349 l_sigset_t linux_set;
354 if (ldebug(sigpending))
355 kprintf(ARGS(sigpending, "*"));
358 error = kern_sigpending(&set);
361 SIGSETAND(set, lp->lwp_sigmask);
362 bsd_to_linux_sigset(&set, &linux_set);
363 mask = linux_set.__bits[0];
364 error = copyout(&mask, args->mask, sizeof(mask));
370 sys_linux_kill(struct linux_kill_args *args)
376 kprintf(ARGS(kill, "%d, %d"), args->pid, args->signum);
379 * Allow signal 0 as a means to check for privileges
381 if (args->signum < 0 || args->signum > LINUX_NSIG)
384 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
385 sig = linux_to_bsd_signal[_SIG_IDX(args->signum)];
389 error = kern_kill(sig, args->pid, -1);
396 linux_do_tkill(l_int tgid, l_int pid, l_int sig)
398 struct linux_emuldata *em;
403 * Allow signal 0 as a means to check for privileges
405 if (sig < 0 || sig > LINUX_NSIG)
408 if (sig > 0 && sig <= LINUX_SIGTBLSZ)
409 sig = linux_to_bsd_signal[_SIG_IDX(sig)];
411 lwkt_gettoken(&proc_token);
412 if ((p = pfind(pid)) == NULL) {
413 if ((p = zpfind(pid)) == NULL) {
421 em = emuldata_get(p);
429 if (tgid > 0 && em->s->group_pid != tgid) {
436 error = kern_kill(sig, pid, -1);
441 lwkt_reltoken(&proc_token);
447 sys_linux_tgkill(struct linux_tgkill_args *args)
452 kprintf(ARGS(tgkill, "%d, %d, %d"), args->tgid, args->pid, args->sig);
454 if (args->pid <= 0 || args->tgid <= 0)
457 return (linux_do_tkill(args->tgid, args->pid, args->sig));
461 sys_linux_tkill(struct linux_tkill_args *args)
465 kprintf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
470 return (linux_do_tkill(0, args->tid, args->sig));