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.9 2003/11/16 01:50:54 daver Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/signalvar.h>
37 #include <sys/sysproto.h>
38 #include <sys/kern_syscall.h>
40 #include <arch_linux/linux.h>
41 #include <arch_linux/linux_proto.h>
42 #include "linux_signal.h"
43 #include "linux_util.h"
46 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
51 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
52 bss->__bits[1] = lss->__bits[1];
53 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
54 if (LINUX_SIGISMEMBER(*lss, l)) {
58 b = linux_to_bsd_signal[_SIG_IDX(l)];
67 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
71 LINUX_SIGEMPTYSET(*lss);
72 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
73 lss->__bits[1] = bss->__bits[1];
74 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
75 if (SIGISMEMBER(*bss, b)) {
79 l = bsd_to_linux_signal[_SIG_IDX(b)];
82 LINUX_SIGADDSET(*lss, l);
88 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
91 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
92 bsa->sa_handler = lsa->lsa_handler;
94 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
95 bsa->sa_flags |= SA_NOCLDSTOP;
96 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
97 bsa->sa_flags |= SA_NOCLDWAIT;
98 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
99 bsa->sa_flags |= SA_SIGINFO;
100 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
101 bsa->sa_flags |= SA_ONSTACK;
102 if (lsa->lsa_flags & LINUX_SA_RESTART)
103 bsa->sa_flags |= SA_RESTART;
104 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
105 bsa->sa_flags |= SA_RESETHAND;
106 if (lsa->lsa_flags & LINUX_SA_NOMASK)
107 bsa->sa_flags |= SA_NODEFER;
111 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
114 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
115 lsa->lsa_handler = bsa->sa_handler;
116 lsa->lsa_restorer = NULL; /* unsupported */
118 if (bsa->sa_flags & SA_NOCLDSTOP)
119 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
120 if (bsa->sa_flags & SA_NOCLDWAIT)
121 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
122 if (bsa->sa_flags & SA_SIGINFO)
123 lsa->lsa_flags |= LINUX_SA_SIGINFO;
124 if (bsa->sa_flags & SA_ONSTACK)
125 lsa->lsa_flags |= LINUX_SA_ONSTACK;
126 if (bsa->sa_flags & SA_RESTART)
127 lsa->lsa_flags |= LINUX_SA_RESTART;
128 if (bsa->sa_flags & SA_RESETHAND)
129 lsa->lsa_flags |= LINUX_SA_ONESHOT;
130 if (bsa->sa_flags & SA_NODEFER)
131 lsa->lsa_flags |= LINUX_SA_NOMASK;
136 linux_signal(struct linux_signal_args *args)
138 l_sigaction_t linux_nsa, linux_osa;
139 struct sigaction nsa, osa;
144 printf(ARGS(signal, "%d, %p"),
145 args->sig, (void *)args->handler);
147 linux_nsa.lsa_handler = args->handler;
148 linux_nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
149 LINUX_SIGEMPTYSET(linux_nsa.lsa_mask);
150 linux_to_bsd_sigaction(&linux_nsa, &nsa);
151 if (args->sig <= LINUX_SIGTBLSZ) {
152 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
157 error = kern_sigaction(sig, &nsa, &osa);
159 bsd_to_linux_sigaction(&osa, &linux_osa);
160 args->sysmsg_result = (int) linux_osa.lsa_handler;
163 #endif /*!__alpha__*/
166 linux_rt_sigaction(struct linux_rt_sigaction_args *args)
168 l_sigaction_t linux_nsa, linux_osa;
169 struct sigaction nsa, osa;
173 if (ldebug(rt_sigaction))
174 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
175 (long)args->sig, (void *)args->act,
176 (void *)args->oact, (long)args->sigsetsize);
178 if (args->sigsetsize != sizeof(l_sigset_t))
182 error = copyin(args->act, &linux_nsa, sizeof(linux_nsa));
185 linux_to_bsd_sigaction(&linux_nsa, &nsa);
187 if (args->sig <= LINUX_SIGTBLSZ) {
188 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
193 error = kern_sigaction(sig, args->act ? &nsa : NULL,
194 args->oact ? &osa : NULL);
196 if (error == 0 && args->oact) {
197 bsd_to_linux_sigaction(&osa, &linux_osa);
198 error = copyout(&linux_osa, args->oact, sizeof(linux_osa));
205 linux_to_bsd_sigprocmask(int how)
208 case LINUX_SIG_BLOCK:
210 case LINUX_SIG_UNBLOCK:
212 case LINUX_SIG_SETMASK:
221 linux_sigprocmask(struct linux_sigprocmask_args *args)
224 l_sigset_t linux_set, linux_oset;
229 if (ldebug(sigprocmask))
230 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
234 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
237 LINUX_SIGEMPTYSET(linux_set);
238 linux_set.__bits[0] = mask;
239 linux_to_bsd_sigset(&linux_set, &set);
241 how = linux_to_bsd_sigprocmask(args->how);
243 error = kern_sigprocmask(how, args->mask ? &set : NULL,
244 args->omask ? &oset : NULL);
246 if (error == 0 && args->omask) {
247 bsd_to_linux_sigset(&oset, &linux_oset);
248 mask = linux_oset.__bits[0];
249 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
253 #endif /*!__alpha__*/
256 linux_rt_sigprocmask(struct linux_rt_sigprocmask_args *args)
258 l_sigset_t linux_set, linux_oset;
263 if (ldebug(rt_sigprocmask))
264 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
265 args->how, (void *)args->mask,
266 (void *)args->omask, (long)args->sigsetsize);
269 if (args->sigsetsize != sizeof(l_sigset_t))
273 error = copyin(args->mask, &linux_set, sizeof(l_sigset_t));
276 linux_to_bsd_sigset(&linux_set, &set);
278 how = linux_to_bsd_sigprocmask(args->how);
280 error = kern_sigprocmask(how, args->mask ? &set : NULL,
281 args->omask ? &oset : NULL);
283 if (error == 0 && args->omask) {
284 bsd_to_linux_sigset(&oset, &linux_oset);
285 error = copyout(&linux_oset, args->omask, sizeof(l_sigset_t));
293 linux_sgetmask(struct linux_sgetmask_args *args)
295 struct proc *p = curproc;
299 if (ldebug(sgetmask))
300 printf(ARGS(sgetmask, ""));
303 bsd_to_linux_sigset(&p->p_sigmask, &mask);
304 args->sysmsg_result = mask.__bits[0];
309 linux_ssetmask(struct linux_ssetmask_args *args)
311 struct proc *p = curproc;
316 if (ldebug(ssetmask))
317 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
320 bsd_to_linux_sigset(&p->p_sigmask, &lset);
321 args->sysmsg_result = lset.__bits[0];
322 LINUX_SIGEMPTYSET(lset);
323 lset.__bits[0] = args->mask;
324 linux_to_bsd_sigset(&lset, &bset);
326 SIG_CANTMASK(p->p_sigmask);
331 linux_sigpending(struct linux_sigpending_args *args)
333 struct thread *td = curthread;
334 struct proc *p = td->td_proc;
336 l_sigset_t linux_set;
341 if (ldebug(sigpending))
342 printf(ARGS(sigpending, "*"));
345 error = kern_sigpending(&set);
348 SIGSETAND(set, p->p_sigmask);
349 bsd_to_linux_sigset(&set, &linux_set);
350 mask = linux_set.__bits[0];
351 error = copyout(&mask, args->mask, sizeof(mask));
355 #endif /*!__alpha__*/
358 linux_kill(struct linux_kill_args *args)
364 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
368 * Allow signal 0 as a means to check for privileges
370 if (args->signum < 0 || args->signum > LINUX_NSIG)
374 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
375 sig = linux_to_bsd_signal[_SIG_IDX(args->signum)];
380 error = kern_kill(sig, args->pid);