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 $
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/signalvar.h>
36 #include <sys/sysproto.h>
38 #include <machine/../linux/linux.h>
39 #include <machine/../linux/linux_proto.h>
40 #include <compat/linux/linux_signal.h>
41 #include <compat/linux/linux_util.h>
44 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
49 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
50 bss->__bits[1] = lss->__bits[1];
51 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
52 if (LINUX_SIGISMEMBER(*lss, l)) {
56 b = linux_to_bsd_signal[_SIG_IDX(l)];
65 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
69 LINUX_SIGEMPTYSET(*lss);
70 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
71 lss->__bits[1] = bss->__bits[1];
72 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
73 if (SIGISMEMBER(*bss, b)) {
77 l = bsd_to_linux_signal[_SIG_IDX(b)];
80 LINUX_SIGADDSET(*lss, l);
86 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
89 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
90 bsa->sa_handler = lsa->lsa_handler;
92 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
93 bsa->sa_flags |= SA_NOCLDSTOP;
94 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
95 bsa->sa_flags |= SA_NOCLDWAIT;
96 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
97 bsa->sa_flags |= SA_SIGINFO;
98 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
99 bsa->sa_flags |= SA_ONSTACK;
100 if (lsa->lsa_flags & LINUX_SA_RESTART)
101 bsa->sa_flags |= SA_RESTART;
102 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
103 bsa->sa_flags |= SA_RESETHAND;
104 if (lsa->lsa_flags & LINUX_SA_NOMASK)
105 bsa->sa_flags |= SA_NODEFER;
109 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
112 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
113 lsa->lsa_handler = bsa->sa_handler;
114 lsa->lsa_restorer = NULL; /* unsupported */
116 if (bsa->sa_flags & SA_NOCLDSTOP)
117 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
118 if (bsa->sa_flags & SA_NOCLDWAIT)
119 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
120 if (bsa->sa_flags & SA_SIGINFO)
121 lsa->lsa_flags |= LINUX_SA_SIGINFO;
122 if (bsa->sa_flags & SA_ONSTACK)
123 lsa->lsa_flags |= LINUX_SA_ONSTACK;
124 if (bsa->sa_flags & SA_RESTART)
125 lsa->lsa_flags |= LINUX_SA_RESTART;
126 if (bsa->sa_flags & SA_RESETHAND)
127 lsa->lsa_flags |= LINUX_SA_ONESHOT;
128 if (bsa->sa_flags & SA_NODEFER)
129 lsa->lsa_flags |= LINUX_SA_NOMASK;
133 linux_do_sigaction(struct proc *p, int linux_sig, l_sigaction_t *linux_nsa,
134 l_sigaction_t *linux_osa)
136 struct sigaction *nsa, *osa;
137 struct sigaction_args sa_args;
139 caddr_t sg = stackgap_init();
141 if (linux_sig <= 0 || linux_sig > LINUX_NSIG)
144 if (linux_osa != NULL)
145 osa = stackgap_alloc(&sg, sizeof(struct sigaction));
149 if (linux_nsa != NULL) {
150 nsa = stackgap_alloc(&sg, sizeof(struct sigaction));
151 linux_to_bsd_sigaction(linux_nsa, nsa);
157 if (linux_sig <= LINUX_SIGTBLSZ)
158 sa_args.sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
161 sa_args.sig = linux_sig;
165 error = sigaction(p, &sa_args);
169 if (linux_osa != NULL)
170 bsd_to_linux_sigaction(osa, linux_osa);
178 linux_signal(struct proc *p, struct linux_signal_args *args)
180 l_sigaction_t nsa, osa;
185 printf(ARGS(signal, "%d, %p"),
186 args->sig, (void *)args->handler);
189 nsa.lsa_handler = args->handler;
190 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
191 LINUX_SIGEMPTYSET(nsa.lsa_mask);
193 error = linux_do_sigaction(p, args->sig, &nsa, &osa);
194 p->p_retval[0] = (int)osa.lsa_handler;
198 #endif /*!__alpha__*/
201 linux_rt_sigaction(struct proc *p, struct linux_rt_sigaction_args *args)
203 l_sigaction_t nsa, osa;
207 if (ldebug(rt_sigaction))
208 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
209 (long)args->sig, (void *)args->act,
210 (void *)args->oact, (long)args->sigsetsize);
213 if (args->sigsetsize != sizeof(l_sigset_t))
216 if (args->act != NULL) {
217 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
222 error = linux_do_sigaction(p, args->sig,
223 args->act ? &nsa : NULL,
224 args->oact ? &osa : NULL);
226 if (args->oact != NULL && !error) {
227 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
234 linux_do_sigprocmask(struct proc *p, int how, l_sigset_t *new,
244 bsd_to_linux_sigset(&p->p_sigmask, old);
247 linux_to_bsd_sigset(new, &mask);
250 case LINUX_SIG_BLOCK:
251 SIGSETOR(p->p_sigmask, mask);
252 SIG_CANTMASK(p->p_sigmask);
254 case LINUX_SIG_UNBLOCK:
255 SIGSETNAND(p->p_sigmask, mask);
257 case LINUX_SIG_SETMASK:
259 SIG_CANTMASK(p->p_sigmask);
272 linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args)
275 l_sigset_t set, oset;
279 if (ldebug(sigprocmask))
280 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
283 if (args->mask != NULL) {
284 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
287 LINUX_SIGEMPTYSET(set);
288 set.__bits[0] = mask;
291 error = linux_do_sigprocmask(p, args->how,
292 args->mask ? &set : NULL,
293 args->omask ? &oset : NULL);
295 if (args->omask != NULL && !error) {
296 mask = oset.__bits[0];
297 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
302 #endif /*!__alpha__*/
305 linux_rt_sigprocmask(struct proc *p, struct linux_rt_sigprocmask_args *args)
307 l_sigset_t set, oset;
311 if (ldebug(rt_sigprocmask))
312 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
313 args->how, (void *)args->mask,
314 (void *)args->omask, (long)args->sigsetsize);
317 if (args->sigsetsize != sizeof(l_sigset_t))
320 if (args->mask != NULL) {
321 error = copyin(args->mask, &set, sizeof(l_sigset_t));
326 error = linux_do_sigprocmask(p, args->how,
327 args->mask ? &set : NULL,
328 args->omask ? &oset : NULL);
330 if (args->omask != NULL && !error) {
331 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
339 linux_sgetmask(struct proc *p, struct linux_sgetmask_args *args)
344 if (ldebug(sgetmask))
345 printf(ARGS(sgetmask, ""));
348 bsd_to_linux_sigset(&p->p_sigmask, &mask);
349 p->p_retval[0] = mask.__bits[0];
354 linux_ssetmask(struct proc *p, struct linux_ssetmask_args *args)
360 if (ldebug(ssetmask))
361 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
364 bsd_to_linux_sigset(&p->p_sigmask, &lset);
365 p->p_retval[0] = lset.__bits[0];
366 LINUX_SIGEMPTYSET(lset);
367 lset.__bits[0] = args->mask;
368 linux_to_bsd_sigset(&lset, &bset);
370 SIG_CANTMASK(p->p_sigmask);
375 linux_sigpending(struct proc *p, struct linux_sigpending_args *args)
382 if (ldebug(sigpending))
383 printf(ARGS(sigpending, "*"));
387 SIGSETAND(bset, p->p_sigmask);
388 bsd_to_linux_sigset(&bset, &lset);
389 mask = lset.__bits[0];
390 return (copyout(&mask, args->mask, sizeof(mask)));
392 #endif /*!__alpha__*/
395 linux_kill(struct proc *p, struct linux_kill_args *args)
397 struct kill_args /* {
404 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
408 * Allow signal 0 as a means to check for privileges
410 if (args->signum < 0 || args->signum > LINUX_NSIG)
414 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
415 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
418 tmp.signum = args->signum;
421 return (kill(p, &tmp));