2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994 Christos Zoulas
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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/svr4/svr4_signal.c,v 1.9 2000/01/15 15:38:17 newton Exp $
29 * $DragonFly: src/sys/emulation/svr4/Attic/svr4_signal.c,v 1.2 2003/06/17 04:28:57 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/filedesc.h>
36 #include <sys/signal.h>
37 #include <sys/signalvar.h>
38 #include <sys/sysproto.h>
40 #include <svr4/svr4.h>
41 #include <svr4/svr4_types.h>
42 #include <svr4/svr4_signal.h>
43 #include <svr4/svr4_proto.h>
44 #include <svr4/svr4_util.h>
45 #include <svr4/svr4_ucontext.h>
47 #define svr4_sigmask(n) (1 << (((n) - 1) & 31))
48 #define svr4_sigword(n) (((n) - 1) >> 5)
49 #define svr4_sigemptyset(s) memset((s), 0, sizeof(*(s)))
50 #define svr4_sigismember(s, n) ((s)->bits[svr4_sigword(n)] & svr4_sigmask(n))
51 #define svr4_sigaddset(s, n) ((s)->bits[svr4_sigword(n)] |= svr4_sigmask(n))
53 void svr4_to_bsd_sigaction __P((const struct svr4_sigaction *,
55 void bsd_to_svr4_sigaction __P((const struct sigaction *,
56 struct svr4_sigaction *));
57 void svr4_sigfillset __P((svr4_sigset_t *));
59 int bsd_to_svr4_sig[SVR4_SIGTBLSZ] = {
93 int svr4_to_bsd_sig[SVR4_SIGTBLSZ] = {
112 0, /* XXX NetBSD uses SIGPWR here, but we don't seem to have one */
134 for (i = 0; i < SVR4_NSIG; i++)
135 if (svr4_to_bsd_sig[i] != 0)
136 svr4_sigaddset(s, i);
140 svr4_to_bsd_sigset(sss, bss)
141 const svr4_sigset_t *sss;
147 for (i = 0; i < SVR4_NSIG; i++)
148 if (svr4_sigismember(sss, i + 1)) {
149 newsig = svr4_to_bsd_sig[i];
151 SIGADDSET(*bss, newsig);
156 bsd_to_svr4_sigset(bss, sss)
162 svr4_sigemptyset(sss);
163 sss->bits[0] = bss->__bits[0] & ~((1U << SVR4_SIGTBLSZ) - 1);
164 sss->bits[1] = bss->__bits[1];
165 sss->bits[2] = bss->__bits[2];
166 sss->bits[3] = bss->__bits[3];
167 for (i = 1; i <= SVR4_SIGTBLSZ; i++) {
168 if (SIGISMEMBER(*bss, i)) {
169 newsig = bsd_to_svr4_sig[_SIG_IDX(i)];
171 svr4_sigaddset(sss, newsig);
177 * XXX: Only a subset of the flags is currently implemented.
180 svr4_to_bsd_sigaction(ssa, bsa)
181 const struct svr4_sigaction *ssa;
182 struct sigaction *bsa;
185 bsa->sa_handler = (sig_t) ssa->ssa_handler;
186 svr4_to_bsd_sigset(&ssa->ssa_mask, &bsa->sa_mask);
188 if ((ssa->ssa_flags & SVR4_SA_ONSTACK) != 0)
189 bsa->sa_flags |= SA_ONSTACK;
190 if ((ssa->ssa_flags & SVR4_SA_RESETHAND) != 0)
191 bsa->sa_flags |= SA_RESETHAND;
192 if ((ssa->ssa_flags & SVR4_SA_RESTART) != 0)
193 bsa->sa_flags |= SA_RESTART;
194 if ((ssa->ssa_flags & SVR4_SA_SIGINFO) != 0)
195 DPRINTF(("svr4_to_bsd_sigaction: SA_SIGINFO ignored\n"));
196 if ((ssa->ssa_flags & SVR4_SA_NOCLDSTOP) != 0)
197 bsa->sa_flags |= SA_NOCLDSTOP;
198 if ((ssa->ssa_flags & SVR4_SA_NODEFER) != 0)
199 bsa->sa_flags |= SA_NODEFER;
200 if ((ssa->ssa_flags & SVR4_SA_NOCLDWAIT) != 0)
201 bsa->sa_flags |= SA_NOCLDWAIT;
202 if ((ssa->ssa_flags & ~SVR4_SA_ALLBITS) != 0)
203 DPRINTF(("svr4_to_bsd_sigaction: extra bits ignored\n"));
207 bsd_to_svr4_sigaction(bsa, ssa)
208 const struct sigaction *bsa;
209 struct svr4_sigaction *ssa;
212 ssa->ssa_handler = (svr4_sig_t) bsa->sa_handler;
213 bsd_to_svr4_sigset(&bsa->sa_mask, &ssa->ssa_mask);
215 if ((bsa->sa_flags & SA_ONSTACK) != 0)
216 ssa->ssa_flags |= SVR4_SA_ONSTACK;
217 if ((bsa->sa_flags & SA_RESETHAND) != 0)
218 ssa->ssa_flags |= SVR4_SA_RESETHAND;
219 if ((bsa->sa_flags & SA_RESTART) != 0)
220 ssa->ssa_flags |= SVR4_SA_RESTART;
221 if ((bsa->sa_flags & SA_NODEFER) != 0)
222 ssa->ssa_flags |= SVR4_SA_NODEFER;
223 if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
224 ssa->ssa_flags |= SVR4_SA_NOCLDSTOP;
228 svr4_to_bsd_sigaltstack(sss, bss)
229 const struct svr4_sigaltstack *sss;
230 struct sigaltstack *bss;
233 bss->ss_sp = sss->ss_sp;
234 bss->ss_size = sss->ss_size;
236 if ((sss->ss_flags & SVR4_SS_DISABLE) != 0)
237 bss->ss_flags |= SS_DISABLE;
238 if ((sss->ss_flags & SVR4_SS_ONSTACK) != 0)
239 bss->ss_flags |= SS_ONSTACK;
240 if ((sss->ss_flags & ~SVR4_SS_ALLBITS) != 0)
241 /*XXX*/ uprintf("svr4_to_bsd_sigaltstack: extra bits ignored\n");
245 bsd_to_svr4_sigaltstack(bss, sss)
246 const struct sigaltstack *bss;
247 struct svr4_sigaltstack *sss;
250 sss->ss_sp = bss->ss_sp;
251 sss->ss_size = bss->ss_size;
253 if ((bss->ss_flags & SS_DISABLE) != 0)
254 sss->ss_flags |= SVR4_SS_DISABLE;
255 if ((bss->ss_flags & SS_ONSTACK) != 0)
256 sss->ss_flags |= SVR4_SS_ONSTACK;
260 svr4_sys_sigaction(p, uap)
261 register struct proc *p;
262 struct svr4_sys_sigaction_args *uap;
264 struct svr4_sigaction *nisa, *oisa, tmpisa;
265 struct sigaction *nbsa, *obsa, tmpbsa;
266 struct sigaction_args sa;
270 DPRINTF(("@@@ svr4_sys_sigaction(%d, %d, %d)\n", p->p_pid,
272 SVR4_SVR42BSD_SIG(SCARG(uap, signum))));
274 sg = stackgap_init();
275 nisa = SCARG(uap, nsa);
276 oisa = SCARG(uap, osa);
279 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
284 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
285 if ((error = copyin(nisa, &tmpisa, sizeof(tmpisa))) != 0)
287 svr4_to_bsd_sigaction(&tmpisa, &tmpbsa);
288 if ((error = copyout(&tmpbsa, nbsa, sizeof(tmpbsa))) != 0)
293 #if defined(DEBUG_SVR4)
296 for (i = 0; i < 4; i++)
297 DPRINTF(("\tssa_mask[%d] = %lx\n", i,
298 nisa->ssa_mask.bits[i]));
299 DPRINTF(("\tssa_handler = %lx\n", nisa->ssa_handler));
303 SCARG(&sa, sig) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
304 SCARG(&sa, act) = nbsa;
305 SCARG(&sa, oact) = obsa;
307 if ((error = sigaction(p, &sa)) != 0)
311 if ((error = copyin(obsa, &tmpbsa, sizeof(tmpbsa))) != 0)
313 bsd_to_svr4_sigaction(&tmpbsa, &tmpisa);
314 if ((error = copyout(&tmpisa, oisa, sizeof(tmpisa))) != 0)
322 svr4_sys_sigaltstack(p, uap)
323 register struct proc *p;
324 struct svr4_sys_sigaltstack_args *uap;
326 struct svr4_sigaltstack *nsss, *osss, tmpsss;
327 struct sigaltstack *nbss, *obss, tmpbss;
328 struct sigaltstack_args sa;
332 retval = p->p_retval;
333 sg = stackgap_init();
334 nsss = SCARG(uap, nss);
335 osss = SCARG(uap, oss);
338 obss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
343 nbss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
344 if ((error = copyin(nsss, &tmpsss, sizeof(tmpsss))) != 0)
346 svr4_to_bsd_sigaltstack(&tmpsss, &tmpbss);
347 if ((error = copyout(&tmpbss, nbss, sizeof(tmpbss))) != 0)
352 SCARG(&sa, ss) = nbss;
353 SCARG(&sa, oss) = obss;
355 if ((error = sigaltstack(p, &sa)) != 0)
359 if ((error = copyin(obss, &tmpbss, sizeof(tmpbss))) != 0)
361 bsd_to_svr4_sigaltstack(&tmpbss, &tmpsss);
362 if ((error = copyout(&tmpsss, osss, sizeof(tmpsss))) != 0)
370 * Stolen from the ibcs2 one
373 svr4_sys_signal(p, uap)
374 register struct proc *p;
375 struct svr4_sys_signal_args *uap;
378 int error, *retval = p->p_retval;
379 caddr_t sg = stackgap_init();
381 DPRINTF(("@@@ svr4_sys_signal(%d)\n", p->p_pid));
383 signum = SVR4_SVR42BSD_SIG(SVR4_SIGNO(SCARG(uap, signum)));
384 if (signum <= 0 || signum > SVR4_NSIG)
387 switch (SVR4_SIGCALL(SCARG(uap, signum))) {
388 case SVR4_SIGDEFER_MASK:
389 if (SCARG(uap, handler) == SVR4_SIG_HOLD)
393 case SVR4_SIGNAL_MASK:
395 struct sigaction_args sa_args;
396 struct sigaction *nbsa, *obsa, sa;
398 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
399 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
400 SCARG(&sa_args, sig) = signum;
401 SCARG(&sa_args, act) = nbsa;
402 SCARG(&sa_args, oact) = obsa;
404 sa.sa_handler = (sig_t) SCARG(uap, handler);
405 SIGEMPTYSET(sa.sa_mask);
408 if (signum != SIGALRM)
409 sa.sa_flags = SA_RESTART;
411 if ((error = copyout(&sa, nbsa, sizeof(sa))) != 0)
413 if ((error = sigaction(p, &sa_args)) != 0) {
414 DPRINTF(("signal: sigaction failed: %d\n",
416 *retval = (int)SVR4_SIG_ERR;
419 if ((error = copyin(obsa, &sa, sizeof(sa))) != 0)
421 *retval = (int)sa.sa_handler;
425 case SVR4_SIGHOLD_MASK:
428 struct sigprocmask_args sa;
431 set = stackgap_alloc(&sg, sizeof(sigset_t));
433 SIGADDSET(*set, signum);
434 SCARG(&sa, how) = SIG_BLOCK;
435 SCARG(&sa, set) = set;
436 SCARG(&sa, oset) = NULL;
437 return sigprocmask(p, &sa);
440 case SVR4_SIGRELSE_MASK:
442 struct sigprocmask_args sa;
445 set = stackgap_alloc(&sg, sizeof(sigset_t));
447 SIGADDSET(*set, signum);
448 SCARG(&sa, how) = SIG_UNBLOCK;
449 SCARG(&sa, set) = set;
450 SCARG(&sa, oset) = NULL;
451 return sigprocmask(p, &sa);
454 case SVR4_SIGIGNORE_MASK:
456 struct sigaction_args sa_args;
457 struct sigaction *bsa, sa;
459 bsa = stackgap_alloc(&sg, sizeof(struct sigaction));
460 SCARG(&sa_args, sig) = signum;
461 SCARG(&sa_args, act) = bsa;
462 SCARG(&sa_args, oact) = NULL;
464 sa.sa_handler = SIG_IGN;
465 SIGEMPTYSET(sa.sa_mask);
467 if ((error = copyout(&sa, bsa, sizeof(sa))) != 0)
469 if ((error = sigaction(p, &sa_args)) != 0) {
470 DPRINTF(("sigignore: sigaction failed\n"));
476 case SVR4_SIGPAUSE_MASK:
478 struct sigsuspend_args sa;
481 set = stackgap_alloc(&sg, sizeof(sigset_t));
483 SIGDELSET(*set, signum);
484 SCARG(&sa, sigmask) = set;
485 return sigsuspend(p, &sa);
495 svr4_sys_sigprocmask(p, uap)
497 struct svr4_sys_sigprocmask_args *uap;
501 int error = 0, *retval;
503 retval = p->p_retval;
504 if (SCARG(uap, oset) != NULL) {
505 /* Fix the return value first if needed */
506 bsd_to_svr4_sigset(&p->p_sigmask, &sss);
507 if ((error = copyout(&sss, SCARG(uap, oset), sizeof(sss))) != 0)
511 if (SCARG(uap, set) == NULL)
515 if ((error = copyin(SCARG(uap, set), &sss, sizeof(sss))) != 0)
518 svr4_to_bsd_sigset(&sss, &bss);
522 switch (SCARG(uap, how)) {
524 SIGSETOR(p->p_sigmask, bss);
525 SIG_CANTMASK(p->p_sigmask);
528 case SVR4_SIG_UNBLOCK:
529 SIGSETNAND(p->p_sigmask, bss);
532 case SVR4_SIG_SETMASK:
534 SIG_CANTMASK(p->p_sigmask);
548 svr4_sys_sigpending(p, uap)
550 struct svr4_sys_sigpending_args *uap;
556 DPRINTF(("@@@ svr4_sys_sigpending(%d)\n", p->p_pid));
557 retval = p->p_retval;
558 switch (SCARG(uap, what)) {
559 case 1: /* sigpending */
560 if (SCARG(uap, mask) == NULL)
563 SIGSETAND(bss, p->p_sigmask);
564 bsd_to_svr4_sigset(&bss, &sss);
567 case 2: /* sigfillset */
568 svr4_sigfillset(&sss);
569 #if defined(DEBUG_SVR4)
572 for (i = 0; i < 4; i++)
573 DPRINTF(("new sigset[%d] = %lx\n", i, (long)sss.bits[i]));
582 return copyout(&sss, SCARG(uap, mask), sizeof(sss));
586 svr4_sys_sigsuspend(p, uap)
587 register struct proc *p;
588 struct svr4_sys_sigsuspend_args *uap;
592 struct sigsuspend_args sa;
594 caddr_t sg = stackgap_init();
596 if ((error = copyin(SCARG(uap, ss), &sss, sizeof(sss))) != 0)
599 bss = stackgap_alloc(&sg, sizeof(sigset_t));
600 svr4_to_bsd_sigset(&sss, bss);
602 SCARG(&sa, sigmask) = bss;
603 return sigsuspend(p, &sa);
608 svr4_sys_kill(p, uap)
609 register struct proc *p;
610 struct svr4_sys_kill_args *uap;
614 SCARG(&ka, pid) = SCARG(uap, pid);
615 SCARG(&ka, signum) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
621 svr4_sys_context(p, uap)
622 register struct proc *p;
623 struct svr4_sys_context_args *uap;
625 struct svr4_ucontext uc;
630 DPRINTF(("getcontext(%p)\n", uap->uc));
631 svr4_getcontext(p, &uc, &p->p_sigmask,
632 p->p_sigstk.ss_flags & SS_ONSTACK);
633 return copyout(&uc, uap->uc, sizeof(uc));
636 DPRINTF(("setcontext(%p)\n", uap->uc));
637 if ((error = copyin(uap->uc, &uc, sizeof(uc))) != 0)
639 DPRINTF(("uc_flags = %lx\n", uc.uc_flags));
640 #if defined(DEBUG_SVR4)
643 for (i = 0; i < 4; i++)
644 DPRINTF(("uc_sigmask[%d] = %lx\n", i,
645 uc.uc_sigmask.bits[i]));
648 return svr4_setcontext(p, &uc);
651 DPRINTF(("context(%d, %p)\n", uap->func,
659 svr4_sys_pause(p, uap)
660 register struct proc *p;
661 struct svr4_sys_pause_args *uap;
663 struct sigsuspend_args bsa;
665 SCARG(&bsa, sigmask) = &p->p_sigmask;
666 return sigsuspend(p, &bsa);