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.
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/namei.h>
35 #include <sys/filedesc.h>
36 #include <sys/mount.h>
37 #include <sys/kernel.h>
38 #include <sys/signal.h>
39 #include <sys/signalvar.h>
40 #include <sys/malloc.h>
41 #include <sys/sysproto.h>
43 #include <svr4/svr4.h>
44 #include <svr4/svr4_types.h>
45 #include <svr4/svr4_signal.h>
46 #include <svr4/svr4_proto.h>
47 #include <svr4/svr4_util.h>
48 #include <svr4/svr4_ucontext.h>
50 #define svr4_sigmask(n) (1 << (((n) - 1) & 31))
51 #define svr4_sigword(n) (((n) - 1) >> 5)
52 #define svr4_sigemptyset(s) memset((s), 0, sizeof(*(s)))
53 #define svr4_sigfillset(s) memset((s), 0xffffffff, sizeof(*(s)))
54 #define svr4_sigismember(s, n) ((s)->bits[svr4_sigword(n)] & svr4_sigmask(n))
55 #define svr4_sigaddset(s, n) ((s)->bits[svr4_sigword(n)] |= svr4_sigmask(n))
57 void svr4_to_bsd_sigaction __P((const struct svr4_sigaction *,
59 void bsd_to_svr4_sigaction __P((const struct sigaction *,
60 struct svr4_sigaction *));
62 int bsd_to_svr4_sig[SVR4_SIGTBLSZ] = {
96 int svr4_to_bsd_sig[SVR4_SIGTBLSZ] = {
115 0, /* XXX NetBSD uses SIGPWR here, but we don't seem to have one */
131 svr4_to_bsd_sigset(sss, bss)
132 const svr4_sigset_t *sss;
138 bss->__bits[0] = sss->bits[0] & ~((1U << SVR4_SIGTBLSZ) - 1);
139 bss->__bits[1] = sss->bits[1];
140 bss->__bits[2] = sss->bits[2];
141 bss->__bits[3] = sss->bits[3];
142 for (i = 1; i <= SVR4_SIGTBLSZ; i++) {
143 if (svr4_sigismember(sss, i)) {
144 newsig = svr4_to_bsd_sig[_SIG_IDX(i)];
146 SIGADDSET(*bss, newsig);
152 bsd_to_svr4_sigset(bss, sss)
158 svr4_sigemptyset(sss);
159 sss->bits[0] = bss->__bits[0] & ~((1U << SVR4_SIGTBLSZ) - 1);
160 sss->bits[1] = bss->__bits[1];
161 sss->bits[2] = bss->__bits[2];
162 sss->bits[3] = bss->__bits[3];
163 for (i = 1; i <= SVR4_SIGTBLSZ; i++) {
164 if (SIGISMEMBER(*bss, i)) {
165 newsig = bsd_to_svr4_sig[_SIG_IDX(i)];
167 svr4_sigaddset(sss, newsig);
173 * XXX: Only a subset of the flags is currently implemented.
176 svr4_to_bsd_sigaction(ssa, bsa)
177 const struct svr4_sigaction *ssa;
178 struct sigaction *bsa;
181 bsa->sa_handler = (sig_t) ssa->ssa_handler;
182 svr4_to_bsd_sigset(&ssa->ssa_mask, &bsa->sa_mask);
184 if ((ssa->ssa_flags & SVR4_SA_ONSTACK) != 0)
185 bsa->sa_flags |= SA_ONSTACK;
186 if ((ssa->ssa_flags & SVR4_SA_RESETHAND) != 0)
187 bsa->sa_flags |= SA_RESETHAND;
188 if ((ssa->ssa_flags & SVR4_SA_RESTART) != 0)
189 bsa->sa_flags |= SA_RESTART;
190 if ((ssa->ssa_flags & SVR4_SA_SIGINFO) != 0)
191 DPRINTF(("svr4_to_bsd_sigaction: SA_SIGINFO ignored\n"));
192 if ((ssa->ssa_flags & SVR4_SA_NOCLDSTOP) != 0)
193 bsa->sa_flags |= SA_NOCLDSTOP;
194 if ((ssa->ssa_flags & SVR4_SA_NODEFER) != 0)
195 bsa->sa_flags |= SA_NODEFER;
196 if ((ssa->ssa_flags & SVR4_SA_NOCLDWAIT) != 0)
197 bsa->sa_flags |= SA_NOCLDWAIT;
198 if ((ssa->ssa_flags & ~SVR4_SA_ALLBITS) != 0)
199 DPRINTF(("svr4_to_bsd_sigaction: extra bits ignored\n"));
203 bsd_to_svr4_sigaction(bsa, ssa)
204 const struct sigaction *bsa;
205 struct svr4_sigaction *ssa;
208 ssa->ssa_handler = (svr4_sig_t) bsa->sa_handler;
209 bsd_to_svr4_sigset(&bsa->sa_mask, &ssa->ssa_mask);
211 if ((bsa->sa_flags & SA_ONSTACK) != 0)
212 ssa->ssa_flags |= SVR4_SA_ONSTACK;
213 if ((bsa->sa_flags & SA_RESETHAND) != 0)
214 ssa->ssa_flags |= SVR4_SA_RESETHAND;
215 if ((bsa->sa_flags & SA_RESTART) != 0)
216 ssa->ssa_flags |= SVR4_SA_RESTART;
217 if ((bsa->sa_flags & SA_NODEFER) != 0)
218 ssa->ssa_flags |= SVR4_SA_NODEFER;
219 if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
220 ssa->ssa_flags |= SVR4_SA_NOCLDSTOP;
224 svr4_to_bsd_sigaltstack(sss, bss)
225 const struct svr4_sigaltstack *sss;
226 struct sigaltstack *bss;
229 bss->ss_sp = sss->ss_sp;
230 bss->ss_size = sss->ss_size;
232 if ((sss->ss_flags & SVR4_SS_DISABLE) != 0)
233 bss->ss_flags |= SS_DISABLE;
234 if ((sss->ss_flags & SVR4_SS_ONSTACK) != 0)
235 bss->ss_flags |= SS_ONSTACK;
236 if ((sss->ss_flags & ~SVR4_SS_ALLBITS) != 0)
237 /*XXX*/ uprintf("svr4_to_bsd_sigaltstack: extra bits ignored\n");
241 bsd_to_svr4_sigaltstack(bss, sss)
242 const struct sigaltstack *bss;
243 struct svr4_sigaltstack *sss;
246 sss->ss_sp = bss->ss_sp;
247 sss->ss_size = bss->ss_size;
249 if ((bss->ss_flags & SS_DISABLE) != 0)
250 sss->ss_flags |= SVR4_SS_DISABLE;
251 if ((bss->ss_flags & SS_ONSTACK) != 0)
252 sss->ss_flags |= SVR4_SS_ONSTACK;
256 svr4_sys_sigaction(p, uap)
257 register struct proc *p;
258 struct svr4_sys_sigaction_args *uap;
260 struct svr4_sigaction *nisa, *oisa, tmpisa;
261 struct sigaction *nbsa, *obsa, tmpbsa;
262 struct sigaction_args sa;
266 sg = stackgap_init();
267 nisa = SCARG(uap, nsa);
268 oisa = SCARG(uap, osa);
271 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
276 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
277 if ((error = copyin(nisa, &tmpisa, sizeof(tmpisa))) != 0)
279 svr4_to_bsd_sigaction(&tmpisa, &tmpbsa);
280 if ((error = copyout(&tmpbsa, nbsa, sizeof(tmpbsa))) != 0)
285 SCARG(&sa, sig) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
286 SCARG(&sa, act) = nbsa;
287 SCARG(&sa, oact) = obsa;
289 if ((error = sigaction(p, &sa)) != 0)
293 if ((error = copyin(obsa, &tmpbsa, sizeof(tmpbsa))) != 0)
295 bsd_to_svr4_sigaction(&tmpbsa, &tmpisa);
296 if ((error = copyout(&tmpisa, oisa, sizeof(tmpisa))) != 0)
304 svr4_sys_sigaltstack(p, uap)
305 register struct proc *p;
306 struct svr4_sys_sigaltstack_args *uap;
308 struct svr4_sigaltstack *nsss, *osss, tmpsss;
309 struct sigaltstack *nbss, *obss, tmpbss;
310 struct sigaltstack_args sa;
314 retval = p->p_retval;
315 sg = stackgap_init();
316 nsss = SCARG(uap, nss);
317 osss = SCARG(uap, oss);
320 obss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
325 nbss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
326 if ((error = copyin(nsss, &tmpsss, sizeof(tmpsss))) != 0)
328 svr4_to_bsd_sigaltstack(&tmpsss, &tmpbss);
329 if ((error = copyout(&tmpbss, nbss, sizeof(tmpbss))) != 0)
334 SCARG(&sa, ss) = nbss;
335 SCARG(&sa, oss) = obss;
337 if ((error = sigaltstack(p, &sa)) != 0)
341 if ((error = copyin(obss, &tmpbss, sizeof(tmpbss))) != 0)
343 bsd_to_svr4_sigaltstack(&tmpbss, &tmpsss);
344 if ((error = copyout(&tmpsss, osss, sizeof(tmpsss))) != 0)
352 * Stolen from the ibcs2 one
355 svr4_sys_signal(p, uap)
356 register struct proc *p;
357 struct svr4_sys_signal_args *uap;
361 caddr_t sg = stackgap_init();
363 signum = SVR4_SVR42BSD_SIG(SVR4_SIGNO(SCARG(uap, signum)));
364 retval = p->p_retval;
365 if (signum <= 0 || signum > SVR4_NSIG)
368 switch (SVR4_SIGCALL(SCARG(uap, signum))) {
369 case SVR4_SIGDEFER_MASK:
370 if (SCARG(uap, handler) == SVR4_SIG_HOLD)
374 case SVR4_SIGNAL_MASK:
376 struct sigaction_args sa_args;
377 struct sigaction *nbsa, *obsa, sa;
379 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
380 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
381 SCARG(&sa_args, sig) = signum;
382 SCARG(&sa_args, act) = nbsa;
383 SCARG(&sa_args, oact) = obsa;
385 sa.sa_handler = (sig_t) SCARG(uap, handler);
386 SIGEMPTYSET(sa.sa_mask);
389 if (signum != SIGALRM)
390 sa.sa_flags = SA_RESTART;
392 if ((error = copyout(&sa, nbsa, sizeof(sa))) != 0)
394 if ((error = sigaction(p, &sa_args)) != 0) {
395 DPRINTF(("signal: sigaction failed: %d\n",
397 *retval = (int)SVR4_SIG_ERR;
400 if ((error = copyin(obsa, &sa, sizeof(sa))) != 0)
402 *retval = (int)sa.sa_handler;
406 case SVR4_SIGHOLD_MASK:
409 struct sigprocmask_args sa;
412 set = stackgap_alloc(&sg, sizeof(sigset_t));
414 SIGADDSET(*set, signum);
415 SCARG(&sa, how) = SIG_BLOCK;
416 SCARG(&sa, set) = set;
417 SCARG(&sa, oset) = NULL;
418 return sigprocmask(p, &sa);
421 case SVR4_SIGRELSE_MASK:
423 struct sigprocmask_args sa;
426 set = stackgap_alloc(&sg, sizeof(sigset_t));
428 SIGADDSET(*set, signum);
429 SCARG(&sa, how) = SIG_UNBLOCK;
430 SCARG(&sa, set) = set;
431 SCARG(&sa, oset) = NULL;
432 return sigprocmask(p, &sa);
435 case SVR4_SIGIGNORE_MASK:
437 struct sigaction_args sa_args;
438 struct sigaction *bsa, sa;
440 bsa = stackgap_alloc(&sg, sizeof(struct sigaction));
441 SCARG(&sa_args, sig) = signum;
442 SCARG(&sa_args, act) = bsa;
443 SCARG(&sa_args, oact) = NULL;
445 sa.sa_handler = SIG_IGN;
446 SIGEMPTYSET(sa.sa_mask);
448 if ((error = copyout(&sa, bsa, sizeof(sa))) != 0)
450 if ((error = sigaction(p, &sa_args)) != 0) {
451 DPRINTF(("sigignore: sigaction failed\n"));
457 case SVR4_SIGPAUSE_MASK:
459 struct sigsuspend_args sa;
462 set = stackgap_alloc(&sg, sizeof(sigset_t));
464 SIGDELSET(*set, signum);
465 SCARG(&sa, sigmask) = set;
466 return sigsuspend(p, &sa);
476 svr4_sys_sigprocmask(p, uap)
478 struct svr4_sys_sigprocmask_args *uap;
482 int error = 0, *retval;
484 retval = p->p_retval;
485 if (SCARG(uap, oset) != NULL) {
486 /* Fix the return value first if needed */
487 bsd_to_svr4_sigset(&p->p_sigmask, &sss);
488 if ((error = copyout(&sss, SCARG(uap, oset), sizeof(sss))) != 0)
492 if (SCARG(uap, set) == NULL)
496 if ((error = copyin(SCARG(uap, set), &sss, sizeof(sss))) != 0)
499 svr4_to_bsd_sigset(&sss, &bss);
503 switch (SCARG(uap, how)) {
505 SIGSETOR(p->p_sigmask, bss);
506 SIG_CANTMASK(p->p_sigmask);
509 case SVR4_SIG_UNBLOCK:
510 SIGSETNAND(p->p_sigmask, bss);
513 case SVR4_SIG_SETMASK:
515 SIG_CANTMASK(p->p_sigmask);
529 svr4_sys_sigpending(p, uap)
531 struct svr4_sys_sigpending_args *uap;
537 retval = p->p_retval;
538 switch (SCARG(uap, what)) {
539 case 1: /* sigpending */
540 if (SCARG(uap, mask) == NULL)
543 SIGSETAND(bss, p->p_sigmask);
544 bsd_to_svr4_sigset(&bss, &sss);
547 case 2: /* sigfillset */
548 svr4_sigfillset(&sss);
555 return copyout(&sss, SCARG(uap, mask), sizeof(sss));
559 svr4_sys_sigsuspend(p, uap)
560 register struct proc *p;
561 struct svr4_sys_sigsuspend_args *uap;
565 struct sigsuspend_args sa;
567 caddr_t sg = stackgap_init();
569 if ((error = copyin(SCARG(uap, ss), &sss, sizeof(sss))) != 0)
572 bss = stackgap_alloc(&sg, sizeof(sigset_t));
573 svr4_to_bsd_sigset(&sss, bss);
575 SCARG(&sa, sigmask) = bss;
576 return sigsuspend(p, &sa);
581 svr4_sys_kill(p, uap)
582 register struct proc *p;
583 struct svr4_sys_kill_args *uap;
587 SCARG(&ka, pid) = SCARG(uap, pid);
588 SCARG(&ka, signum) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
594 svr4_sys_context(p, uap)
595 register struct proc *p;
596 struct svr4_sys_context_args *uap;
598 struct svr4_ucontext uc;
603 DPRINTF(("getcontext(%p)\n", uap->uc));
604 svr4_getcontext(p, &uc, &p->p_sigmask,
605 p->p_sigstk.ss_flags & SS_ONSTACK);
606 return copyout(&uc, uap->uc, sizeof(uc));
609 DPRINTF(("setcontext(%p)\n", uap->uc));
610 if ((error = copyin(uap->uc, &uc, sizeof(uc))) != 0)
612 DPRINTF(("uc_flags = %x\n", uc.uc_flags));
613 DPRINTF(("uc_sigmask = %x\n", uc.uc_sigmask));
615 return svr4_setcontext(p, &uc);
618 DPRINTF(("context(%d, %p)\n", uap->func,
626 svr4_sys_pause(p, uap)
627 register struct proc *p;
628 struct svr4_sys_pause_args *uap;
630 struct sigsuspend_args bsa;
632 SCARG(&bsa, sigmask) = &p->p_sigmask;
633 return sigsuspend(p, &bsa);