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/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_sigfillset(s) memset((s), 0xffffffff, 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 __P((const struct svr4_sigaction *,
55 void bsd_to_svr4_sigaction __P((const struct sigaction *,
56 struct svr4_sigaction *));
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 */
127 svr4_to_bsd_sigset(sss, bss)
128 const svr4_sigset_t *sss;
134 bss->__bits[0] = sss->bits[0] & ~((1U << SVR4_SIGTBLSZ) - 1);
135 bss->__bits[1] = sss->bits[1];
136 bss->__bits[2] = sss->bits[2];
137 bss->__bits[3] = sss->bits[3];
138 for (i = 1; i <= SVR4_SIGTBLSZ; i++) {
139 if (svr4_sigismember(sss, i)) {
140 newsig = svr4_to_bsd_sig[_SIG_IDX(i)];
142 SIGADDSET(*bss, newsig);
148 bsd_to_svr4_sigset(bss, sss)
154 svr4_sigemptyset(sss);
155 sss->bits[0] = bss->__bits[0] & ~((1U << SVR4_SIGTBLSZ) - 1);
156 sss->bits[1] = bss->__bits[1];
157 sss->bits[2] = bss->__bits[2];
158 sss->bits[3] = bss->__bits[3];
159 for (i = 1; i <= SVR4_SIGTBLSZ; i++) {
160 if (SIGISMEMBER(*bss, i)) {
161 newsig = bsd_to_svr4_sig[_SIG_IDX(i)];
163 svr4_sigaddset(sss, newsig);
169 * XXX: Only a subset of the flags is currently implemented.
172 svr4_to_bsd_sigaction(ssa, bsa)
173 const struct svr4_sigaction *ssa;
174 struct sigaction *bsa;
177 bsa->sa_handler = (sig_t) ssa->ssa_handler;
178 svr4_to_bsd_sigset(&ssa->ssa_mask, &bsa->sa_mask);
180 if ((ssa->ssa_flags & SVR4_SA_ONSTACK) != 0)
181 bsa->sa_flags |= SA_ONSTACK;
182 if ((ssa->ssa_flags & SVR4_SA_RESETHAND) != 0)
183 bsa->sa_flags |= SA_RESETHAND;
184 if ((ssa->ssa_flags & SVR4_SA_RESTART) != 0)
185 bsa->sa_flags |= SA_RESTART;
186 if ((ssa->ssa_flags & SVR4_SA_SIGINFO) != 0)
187 DPRINTF(("svr4_to_bsd_sigaction: SA_SIGINFO ignored\n"));
188 if ((ssa->ssa_flags & SVR4_SA_NOCLDSTOP) != 0)
189 bsa->sa_flags |= SA_NOCLDSTOP;
190 if ((ssa->ssa_flags & SVR4_SA_NODEFER) != 0)
191 bsa->sa_flags |= SA_NODEFER;
192 if ((ssa->ssa_flags & SVR4_SA_NOCLDWAIT) != 0)
193 bsa->sa_flags |= SA_NOCLDWAIT;
194 if ((ssa->ssa_flags & ~SVR4_SA_ALLBITS) != 0)
195 DPRINTF(("svr4_to_bsd_sigaction: extra bits ignored\n"));
199 bsd_to_svr4_sigaction(bsa, ssa)
200 const struct sigaction *bsa;
201 struct svr4_sigaction *ssa;
204 ssa->ssa_handler = (svr4_sig_t) bsa->sa_handler;
205 bsd_to_svr4_sigset(&bsa->sa_mask, &ssa->ssa_mask);
207 if ((bsa->sa_flags & SA_ONSTACK) != 0)
208 ssa->ssa_flags |= SVR4_SA_ONSTACK;
209 if ((bsa->sa_flags & SA_RESETHAND) != 0)
210 ssa->ssa_flags |= SVR4_SA_RESETHAND;
211 if ((bsa->sa_flags & SA_RESTART) != 0)
212 ssa->ssa_flags |= SVR4_SA_RESTART;
213 if ((bsa->sa_flags & SA_NODEFER) != 0)
214 ssa->ssa_flags |= SVR4_SA_NODEFER;
215 if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
216 ssa->ssa_flags |= SVR4_SA_NOCLDSTOP;
220 svr4_to_bsd_sigaltstack(sss, bss)
221 const struct svr4_sigaltstack *sss;
222 struct sigaltstack *bss;
225 bss->ss_sp = sss->ss_sp;
226 bss->ss_size = sss->ss_size;
228 if ((sss->ss_flags & SVR4_SS_DISABLE) != 0)
229 bss->ss_flags |= SS_DISABLE;
230 if ((sss->ss_flags & SVR4_SS_ONSTACK) != 0)
231 bss->ss_flags |= SS_ONSTACK;
232 if ((sss->ss_flags & ~SVR4_SS_ALLBITS) != 0)
233 /*XXX*/ uprintf("svr4_to_bsd_sigaltstack: extra bits ignored\n");
237 bsd_to_svr4_sigaltstack(bss, sss)
238 const struct sigaltstack *bss;
239 struct svr4_sigaltstack *sss;
242 sss->ss_sp = bss->ss_sp;
243 sss->ss_size = bss->ss_size;
245 if ((bss->ss_flags & SS_DISABLE) != 0)
246 sss->ss_flags |= SVR4_SS_DISABLE;
247 if ((bss->ss_flags & SS_ONSTACK) != 0)
248 sss->ss_flags |= SVR4_SS_ONSTACK;
252 svr4_sys_sigaction(p, uap)
253 register struct proc *p;
254 struct svr4_sys_sigaction_args *uap;
256 struct svr4_sigaction *nisa, *oisa, tmpisa;
257 struct sigaction *nbsa, *obsa, tmpbsa;
258 struct sigaction_args sa;
262 sg = stackgap_init();
263 nisa = SCARG(uap, nsa);
264 oisa = SCARG(uap, osa);
267 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
272 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
273 if ((error = copyin(nisa, &tmpisa, sizeof(tmpisa))) != 0)
275 svr4_to_bsd_sigaction(&tmpisa, &tmpbsa);
276 if ((error = copyout(&tmpbsa, nbsa, sizeof(tmpbsa))) != 0)
281 SCARG(&sa, sig) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
282 SCARG(&sa, act) = nbsa;
283 SCARG(&sa, oact) = obsa;
285 if ((error = sigaction(p, &sa)) != 0)
289 if ((error = copyin(obsa, &tmpbsa, sizeof(tmpbsa))) != 0)
291 bsd_to_svr4_sigaction(&tmpbsa, &tmpisa);
292 if ((error = copyout(&tmpisa, oisa, sizeof(tmpisa))) != 0)
300 svr4_sys_sigaltstack(p, uap)
301 register struct proc *p;
302 struct svr4_sys_sigaltstack_args *uap;
304 struct svr4_sigaltstack *nsss, *osss, tmpsss;
305 struct sigaltstack *nbss, *obss, tmpbss;
306 struct sigaltstack_args sa;
310 retval = p->p_retval;
311 sg = stackgap_init();
312 nsss = SCARG(uap, nss);
313 osss = SCARG(uap, oss);
316 obss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
321 nbss = stackgap_alloc(&sg, sizeof(struct sigaltstack));
322 if ((error = copyin(nsss, &tmpsss, sizeof(tmpsss))) != 0)
324 svr4_to_bsd_sigaltstack(&tmpsss, &tmpbss);
325 if ((error = copyout(&tmpbss, nbss, sizeof(tmpbss))) != 0)
330 SCARG(&sa, ss) = nbss;
331 SCARG(&sa, oss) = obss;
333 if ((error = sigaltstack(p, &sa)) != 0)
337 if ((error = copyin(obss, &tmpbss, sizeof(tmpbss))) != 0)
339 bsd_to_svr4_sigaltstack(&tmpbss, &tmpsss);
340 if ((error = copyout(&tmpsss, osss, sizeof(tmpsss))) != 0)
348 * Stolen from the ibcs2 one
351 svr4_sys_signal(p, uap)
352 register struct proc *p;
353 struct svr4_sys_signal_args *uap;
357 caddr_t sg = stackgap_init();
359 signum = SVR4_SVR42BSD_SIG(SVR4_SIGNO(SCARG(uap, signum)));
360 retval = p->p_retval;
361 if (signum <= 0 || signum > SVR4_NSIG)
364 switch (SVR4_SIGCALL(SCARG(uap, signum))) {
365 case SVR4_SIGDEFER_MASK:
366 if (SCARG(uap, handler) == SVR4_SIG_HOLD)
370 case SVR4_SIGNAL_MASK:
372 struct sigaction_args sa_args;
373 struct sigaction *nbsa, *obsa, sa;
375 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
376 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
377 SCARG(&sa_args, sig) = signum;
378 SCARG(&sa_args, act) = nbsa;
379 SCARG(&sa_args, oact) = obsa;
381 sa.sa_handler = (sig_t) SCARG(uap, handler);
382 SIGEMPTYSET(sa.sa_mask);
385 if (signum != SIGALRM)
386 sa.sa_flags = SA_RESTART;
388 if ((error = copyout(&sa, nbsa, sizeof(sa))) != 0)
390 if ((error = sigaction(p, &sa_args)) != 0) {
391 DPRINTF(("signal: sigaction failed: %d\n",
393 *retval = (int)SVR4_SIG_ERR;
396 if ((error = copyin(obsa, &sa, sizeof(sa))) != 0)
398 *retval = (int)sa.sa_handler;
402 case SVR4_SIGHOLD_MASK:
405 struct sigprocmask_args sa;
408 set = stackgap_alloc(&sg, sizeof(sigset_t));
410 SIGADDSET(*set, signum);
411 SCARG(&sa, how) = SIG_BLOCK;
412 SCARG(&sa, set) = set;
413 SCARG(&sa, oset) = NULL;
414 return sigprocmask(p, &sa);
417 case SVR4_SIGRELSE_MASK:
419 struct sigprocmask_args sa;
422 set = stackgap_alloc(&sg, sizeof(sigset_t));
424 SIGADDSET(*set, signum);
425 SCARG(&sa, how) = SIG_UNBLOCK;
426 SCARG(&sa, set) = set;
427 SCARG(&sa, oset) = NULL;
428 return sigprocmask(p, &sa);
431 case SVR4_SIGIGNORE_MASK:
433 struct sigaction_args sa_args;
434 struct sigaction *bsa, sa;
436 bsa = stackgap_alloc(&sg, sizeof(struct sigaction));
437 SCARG(&sa_args, sig) = signum;
438 SCARG(&sa_args, act) = bsa;
439 SCARG(&sa_args, oact) = NULL;
441 sa.sa_handler = SIG_IGN;
442 SIGEMPTYSET(sa.sa_mask);
444 if ((error = copyout(&sa, bsa, sizeof(sa))) != 0)
446 if ((error = sigaction(p, &sa_args)) != 0) {
447 DPRINTF(("sigignore: sigaction failed\n"));
453 case SVR4_SIGPAUSE_MASK:
455 struct sigsuspend_args sa;
458 set = stackgap_alloc(&sg, sizeof(sigset_t));
460 SIGDELSET(*set, signum);
461 SCARG(&sa, sigmask) = set;
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 SIGSETOR(p->p_sigmask, bss);
502 SIG_CANTMASK(p->p_sigmask);
505 case SVR4_SIG_UNBLOCK:
506 SIGSETNAND(p->p_sigmask, bss);
509 case SVR4_SIG_SETMASK:
511 SIG_CANTMASK(p->p_sigmask);
525 svr4_sys_sigpending(p, uap)
527 struct svr4_sys_sigpending_args *uap;
533 retval = p->p_retval;
534 switch (SCARG(uap, what)) {
535 case 1: /* sigpending */
536 if (SCARG(uap, mask) == NULL)
539 SIGSETAND(bss, p->p_sigmask);
540 bsd_to_svr4_sigset(&bss, &sss);
543 case 2: /* sigfillset */
544 svr4_sigfillset(&sss);
551 return copyout(&sss, SCARG(uap, mask), sizeof(sss));
555 svr4_sys_sigsuspend(p, uap)
556 register struct proc *p;
557 struct svr4_sys_sigsuspend_args *uap;
561 struct sigsuspend_args sa;
563 caddr_t sg = stackgap_init();
565 if ((error = copyin(SCARG(uap, ss), &sss, sizeof(sss))) != 0)
568 bss = stackgap_alloc(&sg, sizeof(sigset_t));
569 svr4_to_bsd_sigset(&sss, bss);
571 SCARG(&sa, sigmask) = bss;
572 return sigsuspend(p, &sa);
577 svr4_sys_kill(p, uap)
578 register struct proc *p;
579 struct svr4_sys_kill_args *uap;
583 SCARG(&ka, pid) = SCARG(uap, pid);
584 SCARG(&ka, signum) = SVR4_SVR42BSD_SIG(SCARG(uap, signum));
590 svr4_sys_context(p, uap)
591 register struct proc *p;
592 struct svr4_sys_context_args *uap;
594 struct svr4_ucontext uc;
599 DPRINTF(("getcontext(%p)\n", uap->uc));
600 svr4_getcontext(p, &uc, &p->p_sigmask,
601 p->p_sigstk.ss_flags & SS_ONSTACK);
602 return copyout(&uc, uap->uc, sizeof(uc));
605 DPRINTF(("setcontext(%p)\n", uap->uc));
606 if ((error = copyin(uap->uc, &uc, sizeof(uc))) != 0)
608 DPRINTF(("uc_flags = %x\n", uc.uc_flags));
609 DPRINTF(("uc_sigmask = %x\n", uc.uc_sigmask));
611 return svr4_setcontext(p, &uc);
614 DPRINTF(("context(%d, %p)\n", uap->func,
622 svr4_sys_pause(p, uap)
623 register struct proc *p;
624 struct svr4_sys_pause_args *uap;
626 struct sigsuspend_args bsa;
628 SCARG(&bsa, sigmask) = &p->p_sigmask;
629 return sigsuspend(p, &bsa);