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.9 2003/08/27 06:07:10 rob 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>
41 #include "svr4_types.h"
42 #include "svr4_signal.h"
43 #include "svr4_proto.h"
44 #include "svr4_util.h"
45 #include "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 (const struct svr4_sigaction *,
55 void bsd_to_svr4_sigaction (const struct sigaction *,
56 struct svr4_sigaction *);
57 void svr4_sigfillset (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(struct svr4_sys_sigaction_args *uap)
262 #if defined(DEBUG_SVR4)
263 struct proc *p = curproc;
265 struct svr4_sigaction *nisa, *oisa, tmpisa;
266 struct sigaction *nbsa, *obsa, tmpbsa;
267 struct sigaction_args sa;
271 DPRINTF(("@@@ svr4_sys_sigaction(%d, %d, %d)\n", p->p_pid,
273 SVR4_SVR42BSD_SIG(SCARG(uap, signum))));
275 sg = stackgap_init();
276 nisa = SCARG(uap, nsa);
277 oisa = SCARG(uap, osa);
280 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
285 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
286 if ((error = copyin(nisa, &tmpisa, sizeof(tmpisa))) != 0)
288 svr4_to_bsd_sigaction(&tmpisa, &tmpbsa);
289 if ((error = copyout(&tmpbsa, nbsa, sizeof(tmpbsa))) != 0)
294 #if defined(DEBUG_SVR4)
297 for (i = 0; i < 4; i++)
298 DPRINTF(("\tssa_mask[%d] = %lx\n", i,
299 nisa->ssa_mask.bits[i]));
300 DPRINTF(("\tssa_handler = %p\n", nisa->ssa_handler));
304 SCARG(&sa, sig) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
305 SCARG(&sa, act) = nbsa;
306 SCARG(&sa, oact) = obsa;
308 sa.sysmsg_result = 0;
309 if ((error = sigaction(&sa)) != 0)
311 uap->sysmsg_result = sa.sysmsg_result;
314 if ((error = copyin(obsa, &tmpbsa, sizeof(tmpbsa))) != 0)
316 bsd_to_svr4_sigaction(&tmpbsa, &tmpisa);
317 if ((error = copyout(&tmpisa, oisa, sizeof(tmpisa))) != 0)
325 svr4_sys_sigaltstack(struct svr4_sys_sigaltstack_args *uap)
327 struct svr4_sigaltstack *nsss, *osss, tmpsss;
328 struct sigaltstack *nbss, *obss, tmpbss;
329 struct sigaltstack_args sa;
333 retval = &uap->sysmsg_result;
334 sg = stackgap_init();
335 nsss = SCARG(uap, nss);
336 osss = SCARG(uap, oss);
339 obss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
344 nbss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
345 if ((error = copyin(nsss, &tmpsss, sizeof(tmpsss))) != 0)
347 svr4_to_bsd_sigaltstack(&tmpsss, &tmpbss);
348 if ((error = copyout(&tmpbss, nbss, sizeof(tmpbss))) != 0)
353 SCARG(&sa, ss) = nbss;
354 SCARG(&sa, oss) = obss;
355 sa.sysmsg_result = 0;
357 if ((error = sigaltstack(&sa)) != 0)
360 *retval = sa.sysmsg_result;
363 if ((error = copyin(obss, &tmpbss, sizeof(tmpbss))) != 0)
365 bsd_to_svr4_sigaltstack(&tmpbss, &tmpsss);
366 if ((error = copyout(&tmpsss, osss, sizeof(tmpsss))) != 0)
374 * Stolen from the ibcs2 one
377 svr4_sys_signal(struct svr4_sys_signal_args *uap)
379 struct proc *p = curproc;
381 int error, *retval = &uap->sysmsg_result;
382 caddr_t sg = stackgap_init();
384 DPRINTF(("@@@ svr4_sys_signal(%d)\n", p->p_pid));
386 signum = SVR4_SVR42BSD_SIG(SVR4_SIGNO(SCARG(uap, signum)));
387 if (signum <= 0 || signum > SVR4_NSIG)
390 switch (SVR4_SIGCALL(SCARG(uap, signum))) {
391 case SVR4_SIGDEFER_MASK:
392 if (SCARG(uap, handler) == SVR4_SIG_HOLD)
396 case SVR4_SIGNAL_MASK:
398 struct sigaction_args sa_args;
399 struct sigaction *nbsa, *obsa, sa;
401 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
402 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
403 SCARG(&sa_args, sig) = signum;
404 SCARG(&sa_args, act) = nbsa;
405 SCARG(&sa_args, oact) = obsa;
406 sa_args.sysmsg_result = 0;
408 sa.sa_handler = (sig_t) SCARG(uap, handler);
409 SIGEMPTYSET(sa.sa_mask);
412 if (signum != SIGALRM)
413 sa.sa_flags = SA_RESTART;
415 if ((error = copyout(&sa, nbsa, sizeof(sa))) != 0)
417 if ((error = sigaction(&sa_args)) != 0) {
418 DPRINTF(("signal: sigaction failed: %d\n",
420 *retval = (int)SVR4_SIG_ERR;
423 if ((error = copyin(obsa, &sa, sizeof(sa))) != 0)
425 *retval = (int)sa.sa_handler;
429 case SVR4_SIGHOLD_MASK:
432 struct sigprocmask_args sa;
435 set = stackgap_alloc(&sg, sizeof(sigset_t));
437 SIGADDSET(*set, signum);
438 SCARG(&sa, how) = SIG_BLOCK;
439 SCARG(&sa, set) = set;
440 SCARG(&sa, oset) = NULL;
441 sa.sysmsg_result = 0;
442 error = sigprocmask(&sa);
443 *retval = sa.sysmsg_result;
447 case SVR4_SIGRELSE_MASK:
449 struct sigprocmask_args sa;
452 set = stackgap_alloc(&sg, sizeof(sigset_t));
454 SIGADDSET(*set, signum);
455 SCARG(&sa, how) = SIG_UNBLOCK;
456 SCARG(&sa, set) = set;
457 SCARG(&sa, oset) = NULL;
458 sa.sysmsg_result = 0;
459 error = sigprocmask(&sa);
460 *retval = sa.sysmsg_result;
464 case SVR4_SIGIGNORE_MASK:
466 struct sigaction_args sa_args;
467 struct sigaction *bsa, sa;
469 bsa = stackgap_alloc(&sg, sizeof(struct sigaction));
470 SCARG(&sa_args, sig) = signum;
471 SCARG(&sa_args, act) = bsa;
472 SCARG(&sa_args, oact) = NULL;
474 sa.sa_handler = SIG_IGN;
475 SIGEMPTYSET(sa.sa_mask);
477 if ((error = copyout(&sa, bsa, sizeof(sa))) != 0)
479 sa_args.sysmsg_result = 0;
480 if ((error = sigaction(&sa_args)) != 0) {
481 DPRINTF(("sigignore: sigaction failed\n"));
484 *retval = sa_args.sysmsg_result;
488 case SVR4_SIGPAUSE_MASK:
490 struct sigsuspend_args sa;
493 set = stackgap_alloc(&sg, sizeof(sigset_t));
495 SIGDELSET(*set, signum);
496 SCARG(&sa, sigmask) = set;
497 sa.sysmsg_result = 0;
498 error = sigsuspend(&sa);
499 *retval = sa.sysmsg_result;
510 svr4_sys_sigprocmask(struct svr4_sys_sigprocmask_args *uap)
512 struct proc *p = curproc;
515 int error = 0, *retval;
517 retval = &uap->sysmsg_result;
518 if (SCARG(uap, oset) != NULL) {
519 /* Fix the return value first if needed */
520 bsd_to_svr4_sigset(&p->p_sigmask, &sss);
521 if ((error = copyout(&sss, SCARG(uap, oset), sizeof(sss))) != 0)
525 if (SCARG(uap, set) == NULL)
529 if ((error = copyin(SCARG(uap, set), &sss, sizeof(sss))) != 0)
532 svr4_to_bsd_sigset(&sss, &bss);
536 switch (SCARG(uap, how)) {
538 SIGSETOR(p->p_sigmask, bss);
539 SIG_CANTMASK(p->p_sigmask);
542 case SVR4_SIG_UNBLOCK:
543 SIGSETNAND(p->p_sigmask, bss);
546 case SVR4_SIG_SETMASK:
548 SIG_CANTMASK(p->p_sigmask);
562 svr4_sys_sigpending(struct svr4_sys_sigpending_args *uap)
564 struct proc *p = curproc;
569 DPRINTF(("@@@ svr4_sys_sigpending(%d)\n", p->p_pid));
570 retval = &uap->sysmsg_result;
571 switch (SCARG(uap, what)) {
572 case 1: /* sigpending */
573 if (SCARG(uap, mask) == NULL)
576 SIGSETAND(bss, p->p_sigmask);
577 bsd_to_svr4_sigset(&bss, &sss);
580 case 2: /* sigfillset */
581 svr4_sigfillset(&sss);
582 #if defined(DEBUG_SVR4)
585 for (i = 0; i < 4; i++)
586 DPRINTF(("new sigset[%d] = %lx\n", i, (long)sss.bits[i]));
595 return copyout(&sss, SCARG(uap, mask), sizeof(sss));
599 svr4_sys_sigsuspend(struct svr4_sys_sigsuspend_args *uap)
603 struct sigsuspend_args sa;
605 caddr_t sg = stackgap_init();
607 if ((error = copyin(SCARG(uap, ss), &sss, sizeof(sss))) != 0)
610 bss = stackgap_alloc(&sg, sizeof(sigset_t));
611 svr4_to_bsd_sigset(&sss, bss);
613 SCARG(&sa, sigmask) = bss;
614 sa.sysmsg_result = 0;
615 error = sigsuspend(&sa);
616 uap->sysmsg_result = sa.sysmsg_result;
622 svr4_sys_kill(struct svr4_sys_kill_args *uap)
627 SCARG(&ka, pid) = SCARG(uap, pid);
628 SCARG(&ka, signum) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
629 ka.sysmsg_result = 0;
631 uap->sysmsg_result = ka.sysmsg_result;
637 svr4_sys_context(struct svr4_sys_context_args *uap)
639 struct proc *p = curproc;
640 struct svr4_ucontext uc;
645 DPRINTF(("getcontext(%p)\n", uap->uc));
646 svr4_getcontext(p, &uc, &p->p_sigmask,
647 p->p_sigstk.ss_flags & SS_ONSTACK);
648 return copyout(&uc, uap->uc, sizeof(uc));
651 DPRINTF(("setcontext(%p)\n", uap->uc));
652 if ((error = copyin(uap->uc, &uc, sizeof(uc))) != 0)
654 DPRINTF(("uc_flags = %lx\n", uc.uc_flags));
655 #if defined(DEBUG_SVR4)
658 for (i = 0; i < 4; i++)
659 DPRINTF(("uc_sigmask[%d] = %lx\n", i,
660 uc.uc_sigmask.bits[i]));
663 return svr4_setcontext(p, &uc);
666 DPRINTF(("context(%d, %p)\n", uap->func,
674 svr4_sys_pause(struct svr4_sys_pause_args *uap)
676 struct proc *p = curproc;
677 struct sigsuspend_args bsa;
680 SCARG(&bsa, sigmask) = &p->p_sigmask;
681 bsa.sysmsg_result = 0;
682 error = sigsuspend(&bsa);
683 uap->sysmsg_result = bsa.sysmsg_result;