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 sigemptyset(s) memset((s), 0, sizeof(*(s)))
51 #define sigismember(s, n) (*(s) & sigmask(n))
52 #define sigaddset(s, n) (*(s) |= sigmask(n))
54 #define svr4_sigmask(n) (1 << (((n) - 1) & 31))
55 #define svr4_sigword(n) (((n) - 1) >> 5)
56 #define svr4_sigemptyset(s) memset((s), 0, sizeof(*(s)))
57 #define svr4_sigismember(s, n) ((s)->bits[svr4_sigword(n)] & svr4_sigmask(n))
58 #define svr4_sigaddset(s, n) ((s)->bits[svr4_sigword(n)] |= svr4_sigmask(n))
60 static __inline void svr4_sigfillset __P((svr4_sigset_t *));
61 void svr4_to_bsd_sigaction __P((const struct svr4_sigaction *,
63 void bsd_to_svr4_sigaction __P((const struct sigaction *,
64 struct svr4_sigaction *));
66 int bsd_to_svr4_sig[] = {
101 int svr4_to_bsd_sig[] = {
121 0, /* XXX NetBSD uses SIGPWR here, but we don't seem to have one */
143 for (i = 1; i < SVR4_NSIG; i++)
144 svr4_sigaddset(s, i);
148 svr4_to_bsd_sigset(sss, bss)
149 const svr4_sigset_t *sss;
155 for (i = 1; i < SVR4_NSIG; i++) {
156 if (svr4_sigismember(sss, i)) {
157 newsig = svr4_to_bsd_sig[i];
159 sigaddset(bss, newsig);
166 bsd_to_svr4_sigset(bss, sss)
172 svr4_sigemptyset(sss);
173 for (i = 1; i < NSIG; i++) {
174 if (sigismember(bss, i)) {
175 newsig = bsd_to_svr4_sig[i];
177 svr4_sigaddset(sss, newsig);
184 * XXX: Only a subset of the flags is currently implemented.
187 svr4_to_bsd_sigaction(ssa, bsa)
188 const struct svr4_sigaction *ssa;
189 struct sigaction *bsa;
192 bsa->sa_handler = (sig_t) ssa->ssa_handler;
193 svr4_to_bsd_sigset(&ssa->ssa_mask, &bsa->sa_mask);
195 if ((ssa->ssa_flags & SVR4_SA_ONSTACK) != 0)
196 bsa->sa_flags |= SA_ONSTACK;
197 if ((ssa->ssa_flags & SVR4_SA_RESETHAND) != 0)
198 bsa->sa_flags |= SA_RESETHAND;
199 if ((ssa->ssa_flags & SVR4_SA_RESTART) != 0)
200 bsa->sa_flags |= SA_RESTART;
201 if ((ssa->ssa_flags & SVR4_SA_SIGINFO) != 0)
202 DPRINTF(("svr4_to_bsd_sigaction: SA_SIGINFO ignored\n"));
203 if ((ssa->ssa_flags & SVR4_SA_NOCLDSTOP) != 0)
204 bsa->sa_flags |= SA_NOCLDSTOP;
205 if ((ssa->ssa_flags & SVR4_SA_NODEFER) != 0)
206 bsa->sa_flags |= SA_NODEFER;
207 if ((ssa->ssa_flags & SVR4_SA_NOCLDWAIT) != 0)
208 bsa->sa_flags |= SA_NOCLDWAIT;
209 if ((ssa->ssa_flags & ~SVR4_SA_ALLBITS) != 0)
210 DPRINTF(("svr4_to_bsd_sigaction: extra bits ignored\n"));
214 bsd_to_svr4_sigaction(bsa, ssa)
215 const struct sigaction *bsa;
216 struct svr4_sigaction *ssa;
219 ssa->ssa_handler = (svr4_sig_t) bsa->sa_handler;
220 bsd_to_svr4_sigset(&bsa->sa_mask, &ssa->ssa_mask);
222 if ((bsa->sa_flags & SA_ONSTACK) != 0)
223 ssa->ssa_flags |= SVR4_SA_ONSTACK;
224 if ((bsa->sa_flags & SA_RESETHAND) != 0)
225 ssa->ssa_flags |= SVR4_SA_RESETHAND;
226 if ((bsa->sa_flags & SA_RESTART) != 0)
227 ssa->ssa_flags |= SVR4_SA_RESTART;
228 if ((bsa->sa_flags & SA_NODEFER) != 0)
229 ssa->ssa_flags |= SVR4_SA_NODEFER;
230 if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
231 ssa->ssa_flags |= SVR4_SA_NOCLDSTOP;
235 svr4_to_bsd_sigaltstack(sss, bss)
236 const struct svr4_sigaltstack *sss;
237 struct sigaltstack *bss;
240 bss->ss_sp = sss->ss_sp;
241 bss->ss_size = sss->ss_size;
243 if ((sss->ss_flags & SVR4_SS_DISABLE) != 0)
244 bss->ss_flags |= SS_DISABLE;
245 if ((sss->ss_flags & SVR4_SS_ONSTACK) != 0)
246 bss->ss_flags |= SS_ONSTACK;
247 if ((sss->ss_flags & ~SVR4_SS_ALLBITS) != 0)
248 /*XXX*/ uprintf("svr4_to_bsd_sigaltstack: extra bits ignored\n");
252 bsd_to_svr4_sigaltstack(bss, sss)
253 const struct sigaltstack *bss;
254 struct svr4_sigaltstack *sss;
257 sss->ss_sp = bss->ss_sp;
258 sss->ss_size = bss->ss_size;
260 if ((bss->ss_flags & SS_DISABLE) != 0)
261 sss->ss_flags |= SVR4_SS_DISABLE;
262 if ((bss->ss_flags & SS_ONSTACK) != 0)
263 sss->ss_flags |= SVR4_SS_ONSTACK;
267 svr4_sys_sigaction(p, uap)
268 register struct proc *p;
269 struct svr4_sys_sigaction_args *uap;
271 struct svr4_sigaction *nisa, *oisa, tmpisa;
272 struct sigaction *nbsa, *obsa, tmpbsa;
273 struct sigaction_args sa;
277 sg = stackgap_init();
278 nisa = SCARG(uap, nsa);
279 oisa = SCARG(uap, osa);
282 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
287 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
288 if ((error = copyin(nisa, &tmpisa, sizeof(tmpisa))) != 0)
290 svr4_to_bsd_sigaction(&tmpisa, &tmpbsa);
291 if ((error = copyout(&tmpbsa, nbsa, sizeof(tmpbsa))) != 0)
296 SCARG(&sa, signum) = svr4_to_bsd_sig[SCARG(uap, signum)];
297 SCARG(&sa, nsa) = nbsa;
298 SCARG(&sa, osa) = obsa;
300 if ((error = sigaction(p, &sa)) != 0)
304 if ((error = copyin(obsa, &tmpbsa, sizeof(tmpbsa))) != 0)
306 bsd_to_svr4_sigaction(&tmpbsa, &tmpisa);
307 if ((error = copyout(&tmpisa, oisa, sizeof(tmpisa))) != 0)
315 svr4_sys_sigaltstack(p, uap)
316 register struct proc *p;
317 struct svr4_sys_sigaltstack_args *uap;
319 struct svr4_sigaltstack *nsss, *osss, tmpsss;
320 struct sigaltstack *nbss, *obss, tmpbss;
321 struct sigaltstack_args sa;
325 retval = p->p_retval;
326 sg = stackgap_init();
327 nsss = SCARG(uap, nss);
328 osss = SCARG(uap, oss);
331 obss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
336 nbss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
337 if ((error = copyin(nsss, &tmpsss, sizeof(tmpsss))) != 0)
339 svr4_to_bsd_sigaltstack(&tmpsss, &tmpbss);
340 if ((error = copyout(&tmpbss, nbss, sizeof(tmpbss))) != 0)
345 SCARG(&sa, nss) = nbss;
346 SCARG(&sa, oss) = obss;
348 if ((error = sigaltstack(p, &sa)) != 0)
352 if ((error = copyin(obss, &tmpbss, sizeof(tmpbss))) != 0)
354 bsd_to_svr4_sigaltstack(&tmpbss, &tmpsss);
355 if ((error = copyout(&tmpsss, osss, sizeof(tmpsss))) != 0)
363 * Stolen from the ibcs2 one
366 svr4_sys_signal(p, uap)
367 register struct proc *p;
368 struct svr4_sys_signal_args *uap;
370 int signum = svr4_to_bsd_sig[SVR4_SIGNO(SCARG(uap, signum))];
372 caddr_t sg = stackgap_init();
374 retval = p->p_retval;
375 if (signum <= 0 || signum >= SVR4_NSIG)
378 switch (SVR4_SIGCALL(SCARG(uap, signum))) {
379 case SVR4_SIGDEFER_MASK:
380 if (SCARG(uap, handler) == SVR4_SIG_HOLD)
384 case SVR4_SIGNAL_MASK:
386 struct sigaction_args sa_args;
387 struct sigaction *nbsa, *obsa, sa;
389 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
390 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
391 SCARG(&sa_args, signum) = signum;
392 SCARG(&sa_args, nsa) = nbsa;
393 SCARG(&sa_args, osa) = obsa;
395 sa.sa_handler = (sig_t) SCARG(uap, handler);
396 sigemptyset(&sa.sa_mask);
399 if (signum != SIGALRM)
400 sa.sa_flags = SA_RESTART;
402 if ((error = copyout(&sa, nbsa, sizeof(sa))) != 0)
404 if ((error = sigaction(p, &sa_args)) != 0) {
405 DPRINTF(("signal: sigaction failed: %d\n",
407 *retval = (int)SVR4_SIG_ERR;
410 if ((error = copyin(obsa, &sa, sizeof(sa))) != 0)
412 *retval = (int)sa.sa_handler;
416 case SVR4_SIGHOLD_MASK:
419 struct sigprocmask_args sa;
421 SCARG(&sa, how) = SIG_BLOCK;
422 SCARG(&sa, mask) = sigmask(signum);
423 return sigprocmask(p, &sa);
426 case SVR4_SIGRELSE_MASK:
428 struct sigprocmask_args sa;
430 SCARG(&sa, how) = SIG_UNBLOCK;
431 SCARG(&sa, mask) = sigmask(signum);
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, signum) = signum;
442 SCARG(&sa_args, nsa) = bsa;
443 SCARG(&sa_args, osa) = 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;
461 SCARG(&sa, mask) = p->p_sigmask & ~sigmask(signum);
462 return sigsuspend(p, &sa);
472 svr4_sys_sigprocmask(p, uap)
474 struct svr4_sys_sigprocmask_args *uap;
478 int error = 0, *retval;
480 retval = p->p_retval;
481 if (SCARG(uap, oset) != NULL) {
482 /* Fix the return value first if needed */
483 bsd_to_svr4_sigset(&p->p_sigmask, &sss);
484 if ((error = copyout(&sss, SCARG(uap, oset), sizeof(sss))) != 0)
488 if (SCARG(uap, set) == NULL)
492 if ((error = copyin(SCARG(uap, set), &sss, sizeof(sss))) != 0)
495 svr4_to_bsd_sigset(&sss, &bss);
499 switch (SCARG(uap, how)) {
501 p->p_sigmask |= bss & ~sigcantmask;
504 case SVR4_SIG_UNBLOCK:
505 p->p_sigmask &= ~bss;
508 case SVR4_SIG_SETMASK:
509 p->p_sigmask = bss & ~sigcantmask;
523 svr4_sys_sigpending(p, uap)
525 struct svr4_sys_sigpending_args *uap;
531 retval = p->p_retval;
532 switch (SCARG(uap, what)) {
533 case 1: /* sigpending */
534 if (SCARG(uap, mask) == NULL)
536 bss = p->p_siglist & p->p_sigmask;
537 bsd_to_svr4_sigset(&bss, &sss);
540 case 2: /* sigfillset */
541 svr4_sigfillset(&sss);
548 return copyout(&sss, SCARG(uap, mask), sizeof(sss));
552 svr4_sys_sigsuspend(p, uap)
553 register struct proc *p;
554 struct svr4_sys_sigsuspend_args *uap;
558 struct sigsuspend_args sa;
561 if ((error = copyin(SCARG(uap, ss), &sss, sizeof(sss))) != 0)
564 svr4_to_bsd_sigset(&sss, &bss);
566 SCARG(&sa, mask) = bss;
567 return sigsuspend(p, &sa);
572 svr4_sys_kill(p, uap)
573 register struct proc *p;
574 struct svr4_sys_kill_args *uap;
578 SCARG(&ka, pid) = SCARG(uap, pid);
579 SCARG(&ka, signum) = svr4_to_bsd_sig[SCARG(uap, signum)];
585 svr4_sys_context(p, uap)
586 register struct proc *p;
587 struct svr4_sys_context_args *uap;
589 struct svr4_ucontext uc;
594 DPRINTF(("getcontext(%p)\n", uap->uc));
595 svr4_getcontext(p, &uc, p->p_sigmask,
596 p->p_sigacts->ps_sigstk.ss_flags & SS_ONSTACK);
597 return copyout(&uc, uap->uc, sizeof(uc));
600 DPRINTF(("setcontext(%p)\n", uap->uc));
601 if ((error = copyin(uap->uc, &uc, sizeof(uc))) != 0)
603 DPRINTF(("uc_flags = %x\n", uc.uc_flags));
604 DPRINTF(("uc_sigmask = %x\n", uc.uc_sigmask));
606 return svr4_setcontext(p, &uc);
609 DPRINTF(("context(%d, %p)\n", uap->func,
617 svr4_sys_pause(p, uap)
618 register struct proc *p;
619 struct svr4_sys_pause_args *uap;
621 struct sigsuspend_args bsa;
623 SCARG(&bsa, mask) = p->p_sigmask;
624 return sigsuspend(p, &bsa);