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 withough 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 $
29 * $DragonFly: src/sys/emulation/linux/linux_signal.c,v 1.3 2003/06/23 17:55:26 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/signalvar.h>
37 #include <sys/sysproto.h>
39 #include <machine/../linux/linux.h>
40 #include <machine/../linux/linux_proto.h>
41 #include <compat/linux/linux_signal.h>
42 #include <compat/linux/linux_util.h>
45 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
50 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
51 bss->__bits[1] = lss->__bits[1];
52 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
53 if (LINUX_SIGISMEMBER(*lss, l)) {
57 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)) {
78 l = bsd_to_linux_signal[_SIG_IDX(b)];
81 LINUX_SIGADDSET(*lss, l);
87 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
90 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
91 bsa->sa_handler = lsa->lsa_handler;
93 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
94 bsa->sa_flags |= SA_NOCLDSTOP;
95 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
96 bsa->sa_flags |= SA_NOCLDWAIT;
97 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
98 bsa->sa_flags |= SA_SIGINFO;
99 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
100 bsa->sa_flags |= SA_ONSTACK;
101 if (lsa->lsa_flags & LINUX_SA_RESTART)
102 bsa->sa_flags |= SA_RESTART;
103 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
104 bsa->sa_flags |= SA_RESETHAND;
105 if (lsa->lsa_flags & LINUX_SA_NOMASK)
106 bsa->sa_flags |= SA_NODEFER;
110 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
113 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
114 lsa->lsa_handler = bsa->sa_handler;
115 lsa->lsa_restorer = NULL; /* unsupported */
117 if (bsa->sa_flags & SA_NOCLDSTOP)
118 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
119 if (bsa->sa_flags & SA_NOCLDWAIT)
120 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
121 if (bsa->sa_flags & SA_SIGINFO)
122 lsa->lsa_flags |= LINUX_SA_SIGINFO;
123 if (bsa->sa_flags & SA_ONSTACK)
124 lsa->lsa_flags |= LINUX_SA_ONSTACK;
125 if (bsa->sa_flags & SA_RESTART)
126 lsa->lsa_flags |= LINUX_SA_RESTART;
127 if (bsa->sa_flags & SA_RESETHAND)
128 lsa->lsa_flags |= LINUX_SA_ONESHOT;
129 if (bsa->sa_flags & SA_NODEFER)
130 lsa->lsa_flags |= LINUX_SA_NOMASK;
134 linux_do_sigaction(int linux_sig, l_sigaction_t *linux_nsa,
135 l_sigaction_t *linux_osa)
137 struct sigaction *nsa, *osa;
138 struct sigaction_args sa_args;
140 caddr_t sg = stackgap_init();
142 if (linux_sig <= 0 || linux_sig > LINUX_NSIG)
145 if (linux_osa != NULL)
146 osa = stackgap_alloc(&sg, sizeof(struct sigaction));
150 if (linux_nsa != NULL) {
151 nsa = stackgap_alloc(&sg, sizeof(struct sigaction));
152 linux_to_bsd_sigaction(linux_nsa, nsa);
158 if (linux_sig <= LINUX_SIGTBLSZ)
159 sa_args.sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
162 sa_args.sig = linux_sig;
166 error = sigaction(&sa_args);
170 if (linux_osa != NULL)
171 bsd_to_linux_sigaction(osa, linux_osa);
179 linux_signal(struct linux_signal_args *args)
181 struct proc *p = curproc;
182 l_sigaction_t nsa, osa;
187 printf(ARGS(signal, "%d, %p"),
188 args->sig, (void *)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(args->sig, &nsa, &osa);
196 p->p_retval[0] = (int)osa.lsa_handler;
200 #endif /*!__alpha__*/
203 linux_rt_sigaction(struct linux_rt_sigaction_args *args)
205 l_sigaction_t nsa, osa;
209 if (ldebug(rt_sigaction))
210 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
211 (long)args->sig, (void *)args->act,
212 (void *)args->oact, (long)args->sigsetsize);
215 if (args->sigsetsize != sizeof(l_sigset_t))
218 if (args->act != NULL) {
219 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
224 error = linux_do_sigaction(args->sig,
225 args->act ? &nsa : NULL,
226 args->oact ? &osa : NULL);
228 if (args->oact != NULL && !error) {
229 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
236 linux_do_sigprocmask(int how, l_sigset_t *new, l_sigset_t *old)
238 struct proc *p = curproc;
246 bsd_to_linux_sigset(&p->p_sigmask, old);
249 linux_to_bsd_sigset(new, &mask);
252 case LINUX_SIG_BLOCK:
253 SIGSETOR(p->p_sigmask, mask);
254 SIG_CANTMASK(p->p_sigmask);
256 case LINUX_SIG_UNBLOCK:
257 SIGSETNAND(p->p_sigmask, mask);
259 case LINUX_SIG_SETMASK:
261 SIG_CANTMASK(p->p_sigmask);
274 linux_sigprocmask(struct linux_sigprocmask_args *args)
277 l_sigset_t set, oset;
281 if (ldebug(sigprocmask))
282 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
285 if (args->mask != NULL) {
286 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
289 LINUX_SIGEMPTYSET(set);
290 set.__bits[0] = mask;
293 error = linux_do_sigprocmask(args->how,
294 args->mask ? &set : NULL,
295 args->omask ? &oset : NULL);
297 if (args->omask != NULL && !error) {
298 mask = oset.__bits[0];
299 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
304 #endif /*!__alpha__*/
307 linux_rt_sigprocmask(struct linux_rt_sigprocmask_args *args)
309 l_sigset_t set, oset;
313 if (ldebug(rt_sigprocmask))
314 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
315 args->how, (void *)args->mask,
316 (void *)args->omask, (long)args->sigsetsize);
319 if (args->sigsetsize != sizeof(l_sigset_t))
322 if (args->mask != NULL) {
323 error = copyin(args->mask, &set, sizeof(l_sigset_t));
328 error = linux_do_sigprocmask(args->how,
329 args->mask ? &set : NULL,
330 args->omask ? &oset : NULL);
332 if (args->omask != NULL && !error) {
333 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
341 linux_sgetmask(struct linux_sgetmask_args *args)
343 struct proc *p = curproc;
347 if (ldebug(sgetmask))
348 printf(ARGS(sgetmask, ""));
351 bsd_to_linux_sigset(&p->p_sigmask, &mask);
352 p->p_retval[0] = mask.__bits[0];
357 linux_ssetmask(struct linux_ssetmask_args *args)
359 struct proc *p = curproc;
364 if (ldebug(ssetmask))
365 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
368 bsd_to_linux_sigset(&p->p_sigmask, &lset);
369 p->p_retval[0] = lset.__bits[0];
370 LINUX_SIGEMPTYSET(lset);
371 lset.__bits[0] = args->mask;
372 linux_to_bsd_sigset(&lset, &bset);
374 SIG_CANTMASK(p->p_sigmask);
379 linux_sigpending(struct linux_sigpending_args *args)
381 struct proc *p = curproc;
387 if (ldebug(sigpending))
388 printf(ARGS(sigpending, "*"));
392 SIGSETAND(bset, p->p_sigmask);
393 bsd_to_linux_sigset(&bset, &lset);
394 mask = lset.__bits[0];
395 return (copyout(&mask, args->mask, sizeof(mask)));
397 #endif /*!__alpha__*/
400 linux_kill(struct linux_kill_args *args)
402 struct kill_args /* {
409 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
413 * Allow signal 0 as a means to check for privileges
415 if (args->signum < 0 || args->signum > LINUX_NSIG)
419 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
420 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
423 tmp.signum = args->signum;