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>
34 #include <sys/filedesc.h>
35 #include <sys/kernel.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_sigfillset(s) memset((s), 0xffffffff, sizeof(*(s)))
51 #define svr4_sigismember(s, n) ((s)->bits[svr4_sigword(n)] & svr4_sigmask(n))
52 #define svr4_sigaddset(s, n) ((s)->bits[svr4_sigword(n)] |= svr4_sigmask(n))
54 void svr4_to_bsd_sigaction __P((const struct svr4_sigaction *,
56 void bsd_to_svr4_sigaction __P((const struct sigaction *,
57 struct svr4_sigaction *));
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 */
128 svr4_to_bsd_sigset(sss, bss)
129 const svr4_sigset_t *sss;
135 bss->__bits[0] = sss->bits[0] & ~((1U << SVR4_SIGTBLSZ) - 1);
136 bss->__bits[1] = sss->bits[1];
137 bss->__bits[2] = sss->bits[2];
138 bss->__bits[3] = sss->bits[3];
139 for (i = 1; i <= SVR4_SIGTBLSZ; i++) {
140 if (svr4_sigismember(sss, i)) {
141 newsig = svr4_to_bsd_sig[_SIG_IDX(i)];
143 SIGADDSET(*bss, newsig);
149 bsd_to_svr4_sigset(bss, sss)
155 svr4_sigemptyset(sss);
156 sss->bits[0] = bss->__bits[0] & ~((1U << SVR4_SIGTBLSZ) - 1);
157 sss->bits[1] = bss->__bits[1];
158 sss->bits[2] = bss->__bits[2];
159 sss->bits[3] = bss->__bits[3];
160 for (i = 1; i <= SVR4_SIGTBLSZ; i++) {
161 if (SIGISMEMBER(*bss, i)) {
162 newsig = bsd_to_svr4_sig[_SIG_IDX(i)];
164 svr4_sigaddset(sss, newsig);
170 * XXX: Only a subset of the flags is currently implemented.
173 svr4_to_bsd_sigaction(ssa, bsa)
174 const struct svr4_sigaction *ssa;
175 struct sigaction *bsa;
178 bsa->sa_handler = (sig_t) ssa->ssa_handler;
179 svr4_to_bsd_sigset(&ssa->ssa_mask, &bsa->sa_mask);
181 if ((ssa->ssa_flags & SVR4_SA_ONSTACK) != 0)
182 bsa->sa_flags |= SA_ONSTACK;
183 if ((ssa->ssa_flags & SVR4_SA_RESETHAND) != 0)
184 bsa->sa_flags |= SA_RESETHAND;
185 if ((ssa->ssa_flags & SVR4_SA_RESTART) != 0)
186 bsa->sa_flags |= SA_RESTART;
187 if ((ssa->ssa_flags & SVR4_SA_SIGINFO) != 0)
188 DPRINTF(("svr4_to_bsd_sigaction: SA_SIGINFO ignored\n"));
189 if ((ssa->ssa_flags & SVR4_SA_NOCLDSTOP) != 0)
190 bsa->sa_flags |= SA_NOCLDSTOP;
191 if ((ssa->ssa_flags & SVR4_SA_NODEFER) != 0)
192 bsa->sa_flags |= SA_NODEFER;
193 if ((ssa->ssa_flags & SVR4_SA_NOCLDWAIT) != 0)
194 bsa->sa_flags |= SA_NOCLDWAIT;
195 if ((ssa->ssa_flags & ~SVR4_SA_ALLBITS) != 0)
196 DPRINTF(("svr4_to_bsd_sigaction: extra bits ignored\n"));
200 bsd_to_svr4_sigaction(bsa, ssa)
201 const struct sigaction *bsa;
202 struct svr4_sigaction *ssa;
205 ssa->ssa_handler = (svr4_sig_t) bsa->sa_handler;
206 bsd_to_svr4_sigset(&bsa->sa_mask, &ssa->ssa_mask);
208 if ((bsa->sa_flags & SA_ONSTACK) != 0)
209 ssa->ssa_flags |= SVR4_SA_ONSTACK;
210 if ((bsa->sa_flags & SA_RESETHAND) != 0)
211 ssa->ssa_flags |= SVR4_SA_RESETHAND;
212 if ((bsa->sa_flags & SA_RESTART) != 0)
213 ssa->ssa_flags |= SVR4_SA_RESTART;
214 if ((bsa->sa_flags & SA_NODEFER) != 0)
215 ssa->ssa_flags |= SVR4_SA_NODEFER;
216 if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
217 ssa->ssa_flags |= SVR4_SA_NOCLDSTOP;
221 svr4_to_bsd_sigaltstack(sss, bss)
222 const struct svr4_sigaltstack *sss;
223 struct sigaltstack *bss;
226 bss->ss_sp = sss->ss_sp;
227 bss->ss_size = sss->ss_size;
229 if ((sss->ss_flags & SVR4_SS_DISABLE) != 0)
230 bss->ss_flags |= SS_DISABLE;
231 if ((sss->ss_flags & SVR4_SS_ONSTACK) != 0)
232 bss->ss_flags |= SS_ONSTACK;
233 if ((sss->ss_flags & ~SVR4_SS_ALLBITS) != 0)
234 /*XXX*/ uprintf("svr4_to_bsd_sigaltstack: extra bits ignored\n");
238 bsd_to_svr4_sigaltstack(bss, sss)
239 const struct sigaltstack *bss;
240 struct svr4_sigaltstack *sss;
243 sss->ss_sp = bss->ss_sp;
244 sss->ss_size = bss->ss_size;
246 if ((bss->ss_flags & SS_DISABLE) != 0)
247 sss->ss_flags |= SVR4_SS_DISABLE;
248 if ((bss->ss_flags & SS_ONSTACK) != 0)
249 sss->ss_flags |= SVR4_SS_ONSTACK;
253 svr4_sys_sigaction(p, uap)
254 register struct proc *p;
255 struct svr4_sys_sigaction_args *uap;
257 struct svr4_sigaction *nisa, *oisa, tmpisa;
258 struct sigaction *nbsa, *obsa, tmpbsa;
259 struct sigaction_args sa;
263 sg = stackgap_init();
264 nisa = SCARG(uap, nsa);
265 oisa = SCARG(uap, osa);
268 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
273 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
274 if ((error = copyin(nisa, &tmpisa, sizeof(tmpisa))) != 0)
276 svr4_to_bsd_sigaction(&tmpisa, &tmpbsa);
277 if ((error = copyout(&tmpbsa, nbsa, sizeof(tmpbsa))) != 0)
282 SCARG(&sa, sig) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
283 SCARG(&sa, act) = nbsa;
284 SCARG(&sa, oact) = obsa;
286 if ((error = sigaction(p, &sa)) != 0)
290 if ((error = copyin(obsa, &tmpbsa, sizeof(tmpbsa))) != 0)
292 bsd_to_svr4_sigaction(&tmpbsa, &tmpisa);
293 if ((error = copyout(&tmpisa, oisa, sizeof(tmpisa))) != 0)
301 svr4_sys_sigaltstack(p, uap)
302 register struct proc *p;
303 struct svr4_sys_sigaltstack_args *uap;
305 struct svr4_sigaltstack *nsss, *osss, tmpsss;
306 struct sigaltstack *nbss, *obss, tmpbss;
307 struct sigaltstack_args sa;
311 retval = p->p_retval;
312 sg = stackgap_init();
313 nsss = SCARG(uap, nss);
314 osss = SCARG(uap, oss);
317 obss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
322 nbss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
323 if ((error = copyin(nsss, &tmpsss, sizeof(tmpsss))) != 0)
325 svr4_to_bsd_sigaltstack(&tmpsss, &tmpbss);
326 if ((error = copyout(&tmpbss, nbss, sizeof(tmpbss))) != 0)
331 SCARG(&sa, ss) = nbss;
332 SCARG(&sa, oss) = obss;
334 if ((error = sigaltstack(p, &sa)) != 0)
338 if ((error = copyin(obss, &tmpbss, sizeof(tmpbss))) != 0)
340 bsd_to_svr4_sigaltstack(&tmpbss, &tmpsss);
341 if ((error = copyout(&tmpsss, osss, sizeof(tmpsss))) != 0)
349 * Stolen from the ibcs2 one
352 svr4_sys_signal(p, uap)
353 register struct proc *p;
354 struct svr4_sys_signal_args *uap;
358 caddr_t sg = stackgap_init();
360 signum = SVR4_SVR42BSD_SIG(SVR4_SIGNO(SCARG(uap, signum)));
361 retval = p->p_retval;
362 if (signum <= 0 || signum > SVR4_NSIG)
365 switch (SVR4_SIGCALL(SCARG(uap, signum))) {
366 case SVR4_SIGDEFER_MASK:
367 if (SCARG(uap, handler) == SVR4_SIG_HOLD)
371 case SVR4_SIGNAL_MASK:
373 struct sigaction_args sa_args;
374 struct sigaction *nbsa, *obsa, sa;
376 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
377 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
378 SCARG(&sa_args, sig) = signum;
379 SCARG(&sa_args, act) = nbsa;
380 SCARG(&sa_args, oact) = obsa;
382 sa.sa_handler = (sig_t) SCARG(uap, handler);
383 SIGEMPTYSET(sa.sa_mask);
386 if (signum != SIGALRM)
387 sa.sa_flags = SA_RESTART;
389 if ((error = copyout(&sa, nbsa, sizeof(sa))) != 0)
391 if ((error = sigaction(p, &sa_args)) != 0) {
392 DPRINTF(("signal: sigaction failed: %d\n",
394 *retval = (int)SVR4_SIG_ERR;
397 if ((error = copyin(obsa, &sa, sizeof(sa))) != 0)
399 *retval = (int)sa.sa_handler;
403 case SVR4_SIGHOLD_MASK:
406 struct sigprocmask_args sa;
409 set = stackgap_alloc(&sg, sizeof(sigset_t));
411 SIGADDSET(*set, signum);
412 SCARG(&sa, how) = SIG_BLOCK;
413 SCARG(&sa, set) = set;
414 SCARG(&sa, oset) = NULL;
415 return sigprocmask(p, &sa);
418 case SVR4_SIGRELSE_MASK:
420 struct sigprocmask_args sa;
423 set = stackgap_alloc(&sg, sizeof(sigset_t));
425 SIGADDSET(*set, signum);
426 SCARG(&sa, how) = SIG_UNBLOCK;
427 SCARG(&sa, set) = set;
428 SCARG(&sa, oset) = NULL;
429 return sigprocmask(p, &sa);
432 case SVR4_SIGIGNORE_MASK:
434 struct sigaction_args sa_args;
435 struct sigaction *bsa, sa;
437 bsa = stackgap_alloc(&sg, sizeof(struct sigaction));
438 SCARG(&sa_args, sig) = signum;
439 SCARG(&sa_args, act) = bsa;
440 SCARG(&sa_args, oact) = NULL;
442 sa.sa_handler = SIG_IGN;
443 SIGEMPTYSET(sa.sa_mask);
445 if ((error = copyout(&sa, bsa, sizeof(sa))) != 0)
447 if ((error = sigaction(p, &sa_args)) != 0) {
448 DPRINTF(("sigignore: sigaction failed\n"));
454 case SVR4_SIGPAUSE_MASK:
456 struct sigsuspend_args sa;
459 set = stackgap_alloc(&sg, sizeof(sigset_t));
461 SIGDELSET(*set, signum);
462 SCARG(&sa, sigmask) = set;
463 return sigsuspend(p, &sa);
473 svr4_sys_sigprocmask(p, uap)
475 struct svr4_sys_sigprocmask_args *uap;
479 int error = 0, *retval;
481 retval = p->p_retval;
482 if (SCARG(uap, oset) != NULL) {
483 /* Fix the return value first if needed */
484 bsd_to_svr4_sigset(&p->p_sigmask, &sss);
485 if ((error = copyout(&sss, SCARG(uap, oset), sizeof(sss))) != 0)
489 if (SCARG(uap, set) == NULL)
493 if ((error = copyin(SCARG(uap, set), &sss, sizeof(sss))) != 0)
496 svr4_to_bsd_sigset(&sss, &bss);
500 switch (SCARG(uap, how)) {
502 SIGSETOR(p->p_sigmask, bss);
503 SIG_CANTMASK(p->p_sigmask);
506 case SVR4_SIG_UNBLOCK:
507 SIGSETNAND(p->p_sigmask, bss);
510 case SVR4_SIG_SETMASK:
512 SIG_CANTMASK(p->p_sigmask);
526 svr4_sys_sigpending(p, uap)
528 struct svr4_sys_sigpending_args *uap;
534 retval = p->p_retval;
535 switch (SCARG(uap, what)) {
536 case 1: /* sigpending */
537 if (SCARG(uap, mask) == NULL)
540 SIGSETAND(bss, p->p_sigmask);
541 bsd_to_svr4_sigset(&bss, &sss);
544 case 2: /* sigfillset */
545 svr4_sigfillset(&sss);
552 return copyout(&sss, SCARG(uap, mask), sizeof(sss));
556 svr4_sys_sigsuspend(p, uap)
557 register struct proc *p;
558 struct svr4_sys_sigsuspend_args *uap;
562 struct sigsuspend_args sa;
564 caddr_t sg = stackgap_init();
566 if ((error = copyin(SCARG(uap, ss), &sss, sizeof(sss))) != 0)
569 bss = stackgap_alloc(&sg, sizeof(sigset_t));
570 svr4_to_bsd_sigset(&sss, bss);
572 SCARG(&sa, sigmask) = bss;
573 return sigsuspend(p, &sa);
578 svr4_sys_kill(p, uap)
579 register struct proc *p;
580 struct svr4_sys_kill_args *uap;
584 SCARG(&ka, pid) = SCARG(uap, pid);
585 SCARG(&ka, signum) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
591 svr4_sys_context(p, uap)
592 register struct proc *p;
593 struct svr4_sys_context_args *uap;
595 struct svr4_ucontext uc;
600 DPRINTF(("getcontext(%p)\n", uap->uc));
601 svr4_getcontext(p, &uc, &p->p_sigmask,
602 p->p_sigstk.ss_flags & SS_ONSTACK);
603 return copyout(&uc, uap->uc, sizeof(uc));
606 DPRINTF(("setcontext(%p)\n", uap->uc));
607 if ((error = copyin(uap->uc, &uc, sizeof(uc))) != 0)
609 DPRINTF(("uc_flags = %x\n", uc.uc_flags));
610 DPRINTF(("uc_sigmask = %x\n", uc.uc_sigmask));
612 return svr4_setcontext(p, &uc);
615 DPRINTF(("context(%d, %p)\n", uap->func,
623 svr4_sys_pause(p, uap)
624 register struct proc *p;
625 struct svr4_sys_pause_args *uap;
627 struct sigsuspend_args bsa;
629 SCARG(&bsa, sigmask) = &p->p_sigmask;
630 return sigsuspend(p, &bsa);