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 $
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/filedesc.h>
35 #include <sys/signal.h>
36 #include <sys/signalvar.h>
37 #include <sys/sysproto.h>
39 #include <svr4/svr4.h>
40 #include <svr4/svr4_types.h>
41 #include <svr4/svr4_signal.h>
42 #include <svr4/svr4_proto.h>
43 #include <svr4/svr4_util.h>
44 #include <svr4/svr4_ucontext.h>
46 #define svr4_sigmask(n) (1 << (((n) - 1) & 31))
47 #define svr4_sigword(n) (((n) - 1) >> 5)
48 #define svr4_sigemptyset(s) memset((s), 0, sizeof(*(s)))
49 #define svr4_sigismember(s, n) ((s)->bits[svr4_sigword(n)] & svr4_sigmask(n))
50 #define svr4_sigaddset(s, n) ((s)->bits[svr4_sigword(n)] |= svr4_sigmask(n))
52 void svr4_to_bsd_sigaction __P((const struct svr4_sigaction *,
54 void bsd_to_svr4_sigaction __P((const struct sigaction *,
55 struct svr4_sigaction *));
56 void svr4_sigfillset __P((svr4_sigset_t *));
58 int bsd_to_svr4_sig[SVR4_SIGTBLSZ] = {
92 int svr4_to_bsd_sig[SVR4_SIGTBLSZ] = {
111 0, /* XXX NetBSD uses SIGPWR here, but we don't seem to have one */
133 for (i = 0; i < SVR4_NSIG; i++)
134 if (svr4_to_bsd_sig[i] != 0)
135 svr4_sigaddset(s, i);
139 svr4_to_bsd_sigset(sss, bss)
140 const svr4_sigset_t *sss;
146 for (i = 0; i < SVR4_NSIG; i++)
147 if (svr4_sigismember(sss, i + 1)) {
148 newsig = svr4_to_bsd_sig[i];
150 SIGADDSET(*bss, newsig);
155 bsd_to_svr4_sigset(bss, sss)
161 svr4_sigemptyset(sss);
162 sss->bits[0] = bss->__bits[0] & ~((1U << SVR4_SIGTBLSZ) - 1);
163 sss->bits[1] = bss->__bits[1];
164 sss->bits[2] = bss->__bits[2];
165 sss->bits[3] = bss->__bits[3];
166 for (i = 1; i <= SVR4_SIGTBLSZ; i++) {
167 if (SIGISMEMBER(*bss, i)) {
168 newsig = bsd_to_svr4_sig[_SIG_IDX(i)];
170 svr4_sigaddset(sss, newsig);
176 * XXX: Only a subset of the flags is currently implemented.
179 svr4_to_bsd_sigaction(ssa, bsa)
180 const struct svr4_sigaction *ssa;
181 struct sigaction *bsa;
184 bsa->sa_handler = (sig_t) ssa->ssa_handler;
185 svr4_to_bsd_sigset(&ssa->ssa_mask, &bsa->sa_mask);
187 if ((ssa->ssa_flags & SVR4_SA_ONSTACK) != 0)
188 bsa->sa_flags |= SA_ONSTACK;
189 if ((ssa->ssa_flags & SVR4_SA_RESETHAND) != 0)
190 bsa->sa_flags |= SA_RESETHAND;
191 if ((ssa->ssa_flags & SVR4_SA_RESTART) != 0)
192 bsa->sa_flags |= SA_RESTART;
193 if ((ssa->ssa_flags & SVR4_SA_SIGINFO) != 0)
194 DPRINTF(("svr4_to_bsd_sigaction: SA_SIGINFO ignored\n"));
195 if ((ssa->ssa_flags & SVR4_SA_NOCLDSTOP) != 0)
196 bsa->sa_flags |= SA_NOCLDSTOP;
197 if ((ssa->ssa_flags & SVR4_SA_NODEFER) != 0)
198 bsa->sa_flags |= SA_NODEFER;
199 if ((ssa->ssa_flags & SVR4_SA_NOCLDWAIT) != 0)
200 bsa->sa_flags |= SA_NOCLDWAIT;
201 if ((ssa->ssa_flags & ~SVR4_SA_ALLBITS) != 0)
202 DPRINTF(("svr4_to_bsd_sigaction: extra bits ignored\n"));
206 bsd_to_svr4_sigaction(bsa, ssa)
207 const struct sigaction *bsa;
208 struct svr4_sigaction *ssa;
211 ssa->ssa_handler = (svr4_sig_t) bsa->sa_handler;
212 bsd_to_svr4_sigset(&bsa->sa_mask, &ssa->ssa_mask);
214 if ((bsa->sa_flags & SA_ONSTACK) != 0)
215 ssa->ssa_flags |= SVR4_SA_ONSTACK;
216 if ((bsa->sa_flags & SA_RESETHAND) != 0)
217 ssa->ssa_flags |= SVR4_SA_RESETHAND;
218 if ((bsa->sa_flags & SA_RESTART) != 0)
219 ssa->ssa_flags |= SVR4_SA_RESTART;
220 if ((bsa->sa_flags & SA_NODEFER) != 0)
221 ssa->ssa_flags |= SVR4_SA_NODEFER;
222 if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
223 ssa->ssa_flags |= SVR4_SA_NOCLDSTOP;
227 svr4_to_bsd_sigaltstack(sss, bss)
228 const struct svr4_sigaltstack *sss;
229 struct sigaltstack *bss;
232 bss->ss_sp = sss->ss_sp;
233 bss->ss_size = sss->ss_size;
235 if ((sss->ss_flags & SVR4_SS_DISABLE) != 0)
236 bss->ss_flags |= SS_DISABLE;
237 if ((sss->ss_flags & SVR4_SS_ONSTACK) != 0)
238 bss->ss_flags |= SS_ONSTACK;
239 if ((sss->ss_flags & ~SVR4_SS_ALLBITS) != 0)
240 /*XXX*/ uprintf("svr4_to_bsd_sigaltstack: extra bits ignored\n");
244 bsd_to_svr4_sigaltstack(bss, sss)
245 const struct sigaltstack *bss;
246 struct svr4_sigaltstack *sss;
249 sss->ss_sp = bss->ss_sp;
250 sss->ss_size = bss->ss_size;
252 if ((bss->ss_flags & SS_DISABLE) != 0)
253 sss->ss_flags |= SVR4_SS_DISABLE;
254 if ((bss->ss_flags & SS_ONSTACK) != 0)
255 sss->ss_flags |= SVR4_SS_ONSTACK;
259 svr4_sys_sigaction(p, uap)
260 register struct proc *p;
261 struct svr4_sys_sigaction_args *uap;
263 struct svr4_sigaction *nisa, *oisa, tmpisa;
264 struct sigaction *nbsa, *obsa, tmpbsa;
265 struct sigaction_args sa;
269 DPRINTF(("@@@ svr4_sys_sigaction(%d, %d, %d)\n", p->p_pid,
271 SVR4_SVR42BSD_SIG(SCARG(uap, signum))));
273 sg = stackgap_init();
274 nisa = SCARG(uap, nsa);
275 oisa = SCARG(uap, osa);
278 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
283 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
284 if ((error = copyin(nisa, &tmpisa, sizeof(tmpisa))) != 0)
286 svr4_to_bsd_sigaction(&tmpisa, &tmpbsa);
287 if ((error = copyout(&tmpbsa, nbsa, sizeof(tmpbsa))) != 0)
292 #if defined(DEBUG_SVR4)
295 for (i = 0; i < 4; i++)
296 DPRINTF(("\tssa_mask[%d] = %lx\n", i,
297 nisa->ssa_mask.bits[i]));
298 DPRINTF(("\tssa_handler = %lx\n", nisa->ssa_handler));
302 SCARG(&sa, sig) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
303 SCARG(&sa, act) = nbsa;
304 SCARG(&sa, oact) = obsa;
306 if ((error = sigaction(p, &sa)) != 0)
310 if ((error = copyin(obsa, &tmpbsa, sizeof(tmpbsa))) != 0)
312 bsd_to_svr4_sigaction(&tmpbsa, &tmpisa);
313 if ((error = copyout(&tmpisa, oisa, sizeof(tmpisa))) != 0)
321 svr4_sys_sigaltstack(p, uap)
322 register struct proc *p;
323 struct svr4_sys_sigaltstack_args *uap;
325 struct svr4_sigaltstack *nsss, *osss, tmpsss;
326 struct sigaltstack *nbss, *obss, tmpbss;
327 struct sigaltstack_args sa;
331 retval = p->p_retval;
332 sg = stackgap_init();
333 nsss = SCARG(uap, nss);
334 osss = SCARG(uap, oss);
337 obss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
342 nbss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
343 if ((error = copyin(nsss, &tmpsss, sizeof(tmpsss))) != 0)
345 svr4_to_bsd_sigaltstack(&tmpsss, &tmpbss);
346 if ((error = copyout(&tmpbss, nbss, sizeof(tmpbss))) != 0)
351 SCARG(&sa, ss) = nbss;
352 SCARG(&sa, oss) = obss;
354 if ((error = sigaltstack(p, &sa)) != 0)
358 if ((error = copyin(obss, &tmpbss, sizeof(tmpbss))) != 0)
360 bsd_to_svr4_sigaltstack(&tmpbss, &tmpsss);
361 if ((error = copyout(&tmpsss, osss, sizeof(tmpsss))) != 0)
369 * Stolen from the ibcs2 one
372 svr4_sys_signal(p, uap)
373 register struct proc *p;
374 struct svr4_sys_signal_args *uap;
377 int error, *retval = p->p_retval;
378 caddr_t sg = stackgap_init();
380 DPRINTF(("@@@ svr4_sys_signal(%d)\n", p->p_pid));
382 signum = SVR4_SVR42BSD_SIG(SVR4_SIGNO(SCARG(uap, signum)));
383 if (signum <= 0 || signum > SVR4_NSIG)
386 switch (SVR4_SIGCALL(SCARG(uap, signum))) {
387 case SVR4_SIGDEFER_MASK:
388 if (SCARG(uap, handler) == SVR4_SIG_HOLD)
392 case SVR4_SIGNAL_MASK:
394 struct sigaction_args sa_args;
395 struct sigaction *nbsa, *obsa, sa;
397 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
398 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
399 SCARG(&sa_args, sig) = signum;
400 SCARG(&sa_args, act) = nbsa;
401 SCARG(&sa_args, oact) = obsa;
403 sa.sa_handler = (sig_t) SCARG(uap, handler);
404 SIGEMPTYSET(sa.sa_mask);
407 if (signum != SIGALRM)
408 sa.sa_flags = SA_RESTART;
410 if ((error = copyout(&sa, nbsa, sizeof(sa))) != 0)
412 if ((error = sigaction(p, &sa_args)) != 0) {
413 DPRINTF(("signal: sigaction failed: %d\n",
415 *retval = (int)SVR4_SIG_ERR;
418 if ((error = copyin(obsa, &sa, sizeof(sa))) != 0)
420 *retval = (int)sa.sa_handler;
424 case SVR4_SIGHOLD_MASK:
427 struct sigprocmask_args sa;
430 set = stackgap_alloc(&sg, sizeof(sigset_t));
432 SIGADDSET(*set, signum);
433 SCARG(&sa, how) = SIG_BLOCK;
434 SCARG(&sa, set) = set;
435 SCARG(&sa, oset) = NULL;
436 return sigprocmask(p, &sa);
439 case SVR4_SIGRELSE_MASK:
441 struct sigprocmask_args sa;
444 set = stackgap_alloc(&sg, sizeof(sigset_t));
446 SIGADDSET(*set, signum);
447 SCARG(&sa, how) = SIG_UNBLOCK;
448 SCARG(&sa, set) = set;
449 SCARG(&sa, oset) = NULL;
450 return sigprocmask(p, &sa);
453 case SVR4_SIGIGNORE_MASK:
455 struct sigaction_args sa_args;
456 struct sigaction *bsa, sa;
458 bsa = stackgap_alloc(&sg, sizeof(struct sigaction));
459 SCARG(&sa_args, sig) = signum;
460 SCARG(&sa_args, act) = bsa;
461 SCARG(&sa_args, oact) = NULL;
463 sa.sa_handler = SIG_IGN;
464 SIGEMPTYSET(sa.sa_mask);
466 if ((error = copyout(&sa, bsa, sizeof(sa))) != 0)
468 if ((error = sigaction(p, &sa_args)) != 0) {
469 DPRINTF(("sigignore: sigaction failed\n"));
475 case SVR4_SIGPAUSE_MASK:
477 struct sigsuspend_args sa;
480 set = stackgap_alloc(&sg, sizeof(sigset_t));
482 SIGDELSET(*set, signum);
483 SCARG(&sa, sigmask) = set;
484 return sigsuspend(p, &sa);
494 svr4_sys_sigprocmask(p, uap)
496 struct svr4_sys_sigprocmask_args *uap;
500 int error = 0, *retval;
502 retval = p->p_retval;
503 if (SCARG(uap, oset) != NULL) {
504 /* Fix the return value first if needed */
505 bsd_to_svr4_sigset(&p->p_sigmask, &sss);
506 if ((error = copyout(&sss, SCARG(uap, oset), sizeof(sss))) != 0)
510 if (SCARG(uap, set) == NULL)
514 if ((error = copyin(SCARG(uap, set), &sss, sizeof(sss))) != 0)
517 svr4_to_bsd_sigset(&sss, &bss);
521 switch (SCARG(uap, how)) {
523 SIGSETOR(p->p_sigmask, bss);
524 SIG_CANTMASK(p->p_sigmask);
527 case SVR4_SIG_UNBLOCK:
528 SIGSETNAND(p->p_sigmask, bss);
531 case SVR4_SIG_SETMASK:
533 SIG_CANTMASK(p->p_sigmask);
547 svr4_sys_sigpending(p, uap)
549 struct svr4_sys_sigpending_args *uap;
555 DPRINTF(("@@@ svr4_sys_sigpending(%d)\n", p->p_pid));
556 retval = p->p_retval;
557 switch (SCARG(uap, what)) {
558 case 1: /* sigpending */
559 if (SCARG(uap, mask) == NULL)
562 SIGSETAND(bss, p->p_sigmask);
563 bsd_to_svr4_sigset(&bss, &sss);
566 case 2: /* sigfillset */
567 svr4_sigfillset(&sss);
568 #if defined(DEBUG_SVR4)
571 for (i = 0; i < 4; i++)
572 DPRINTF(("new sigset[%d] = %lx\n", i, (long)sss.bits[i]));
581 return copyout(&sss, SCARG(uap, mask), sizeof(sss));
585 svr4_sys_sigsuspend(p, uap)
586 register struct proc *p;
587 struct svr4_sys_sigsuspend_args *uap;
591 struct sigsuspend_args sa;
593 caddr_t sg = stackgap_init();
595 if ((error = copyin(SCARG(uap, ss), &sss, sizeof(sss))) != 0)
598 bss = stackgap_alloc(&sg, sizeof(sigset_t));
599 svr4_to_bsd_sigset(&sss, bss);
601 SCARG(&sa, sigmask) = bss;
602 return sigsuspend(p, &sa);
607 svr4_sys_kill(p, uap)
608 register struct proc *p;
609 struct svr4_sys_kill_args *uap;
613 SCARG(&ka, pid) = SCARG(uap, pid);
614 SCARG(&ka, signum) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
620 svr4_sys_context(p, uap)
621 register struct proc *p;
622 struct svr4_sys_context_args *uap;
624 struct svr4_ucontext uc;
629 DPRINTF(("getcontext(%p)\n", uap->uc));
630 svr4_getcontext(p, &uc, &p->p_sigmask,
631 p->p_sigstk.ss_flags & SS_ONSTACK);
632 return copyout(&uc, uap->uc, sizeof(uc));
635 DPRINTF(("setcontext(%p)\n", uap->uc));
636 if ((error = copyin(uap->uc, &uc, sizeof(uc))) != 0)
638 DPRINTF(("uc_flags = %lx\n", uc.uc_flags));
639 #if defined(DEBUG_SVR4)
642 for (i = 0; i < 4; i++)
643 DPRINTF(("uc_sigmask[%d] = %lx\n", i,
644 uc.uc_sigmask.bits[i]));
647 return svr4_setcontext(p, &uc);
650 DPRINTF(("context(%d, %p)\n", uap->func,
658 svr4_sys_pause(p, uap)
659 register struct proc *p;
660 struct svr4_sys_pause_args *uap;
662 struct sigsuspend_args bsa;
664 SCARG(&bsa, sigmask) = &p->p_sigmask;
665 return sigsuspend(p, &bsa);