2 * Copyright (c) 1995 Scott Bartram
3 * Copyright (c) 1995 Steven Wallace
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/i386/ibcs2/ibcs2_signal.c,v 1.16 1999/10/10 09:14:31 marcel Exp $
29 * $DragonFly: src/sys/emulation/ibcs2/i386/Attic/ibcs2_signal.c,v 1.3 2003/06/23 17:55:38 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/signalvar.h>
35 #include <sys/sysproto.h>
37 #include <i386/ibcs2/ibcs2_types.h>
38 #include <i386/ibcs2/ibcs2_signal.h>
39 #include <i386/ibcs2/ibcs2_proto.h>
40 #include <i386/ibcs2/ibcs2_xenix.h>
41 #include <i386/ibcs2/ibcs2_util.h>
43 #define sigemptyset(s) SIGEMPTYSET(*(s))
44 #define sigismember(s, n) SIGISMEMBER(*(s), n)
45 #define sigaddset(s, n) SIGADDSET(*(s), n)
47 #define ibcs2_sigmask(n) (1 << ((n) - 1))
48 #define ibcs2_sigemptyset(s) bzero((s), sizeof(*(s)))
49 #define ibcs2_sigismember(s, n) (*(s) & ibcs2_sigmask(n))
50 #define ibcs2_sigaddset(s, n) (*(s) |= ibcs2_sigmask(n))
52 static void ibcs2_to_bsd_sigset __P((const ibcs2_sigset_t *, sigset_t *));
53 static void bsd_to_ibcs2_sigset __P((const sigset_t *, ibcs2_sigset_t *));
54 static void ibcs2_to_bsd_sigaction __P((struct ibcs2_sigaction *,
56 static void bsd_to_ibcs2_sigaction __P((struct sigaction *,
57 struct ibcs2_sigaction *));
59 int bsd_to_ibcs2_sig[IBCS2_SIGTBLSZ] = {
62 IBCS2_SIGQUIT, /* 3 */
64 IBCS2_SIGTRAP, /* 5 */
65 IBCS2_SIGABRT, /* 6 */
68 IBCS2_SIGKILL, /* 9 */
69 IBCS2_SIGBUS, /* 10 */
70 IBCS2_SIGSEGV, /* 11 */
71 IBCS2_SIGSYS, /* 12 */
72 IBCS2_SIGPIPE, /* 13 */
73 IBCS2_SIGALRM, /* 14 */
74 IBCS2_SIGTERM, /* 15 */
76 IBCS2_SIGSTOP, /* 17 */
77 IBCS2_SIGTSTP, /* 18 */
78 IBCS2_SIGCONT, /* 19 */
79 IBCS2_SIGCLD, /* 20 */
80 IBCS2_SIGTTIN, /* 21 */
81 IBCS2_SIGTTOU, /* 22 */
82 IBCS2_SIGPOLL, /* 23 */
85 IBCS2_SIGVTALRM, /* 26 */
86 IBCS2_SIGPROF, /* 27 */
87 IBCS2_SIGWINCH, /* 28 */
89 IBCS2_SIGUSR1, /* 30 */
90 IBCS2_SIGUSR2, /* 31 */
94 static int ibcs2_to_bsd_sig[IBCS2_SIGTBLSZ] = {
130 ibcs2_to_bsd_sigset(iss, bss)
131 const ibcs2_sigset_t *iss;
137 for (i = 1; i <= IBCS2_SIGTBLSZ; i++) {
138 if (ibcs2_sigismember(iss, i)) {
139 newsig = ibcs2_to_bsd_sig[_SIG_IDX(i)];
141 sigaddset(bss, newsig);
147 bsd_to_ibcs2_sigset(bss, iss)
153 ibcs2_sigemptyset(iss);
154 for (i = 1; i <= IBCS2_SIGTBLSZ; i++) {
155 if (sigismember(bss, i)) {
156 newsig = bsd_to_ibcs2_sig[_SIG_IDX(i)];
158 ibcs2_sigaddset(iss, newsig);
164 ibcs2_to_bsd_sigaction(isa, bsa)
165 struct ibcs2_sigaction *isa;
166 struct sigaction *bsa;
169 bsa->sa_handler = isa->isa_handler;
170 ibcs2_to_bsd_sigset(&isa->isa_mask, &bsa->sa_mask);
171 bsa->sa_flags = 0; /* ??? SA_NODEFER */
172 if ((isa->isa_flags & IBCS2_SA_NOCLDSTOP) != 0)
173 bsa->sa_flags |= SA_NOCLDSTOP;
177 bsd_to_ibcs2_sigaction(bsa, isa)
178 struct sigaction *bsa;
179 struct ibcs2_sigaction *isa;
182 isa->isa_handler = bsa->sa_handler;
183 bsd_to_ibcs2_sigset(&bsa->sa_mask, &isa->isa_mask);
185 if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
186 isa->isa_flags |= IBCS2_SA_NOCLDSTOP;
190 ibcs2_sigaction(struct ibcs2_sigaction_args *uap)
192 struct ibcs2_sigaction *nisa, *oisa, tmpisa;
193 struct sigaction *nbsa, *obsa, tmpbsa;
194 struct sigaction_args sa;
198 sg = stackgap_init();
199 nisa = SCARG(uap, act);
200 oisa = SCARG(uap, oact);
203 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
208 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
209 if ((error = copyin(nisa, &tmpisa, sizeof(tmpisa))) != 0)
211 ibcs2_to_bsd_sigaction(&tmpisa, &tmpbsa);
212 if ((error = copyout(&tmpbsa, nbsa, sizeof(tmpbsa))) != 0)
217 SCARG(&sa, sig) = ibcs2_to_bsd_sig[_SIG_IDX(SCARG(uap, sig))];
218 SCARG(&sa, act) = nbsa;
219 SCARG(&sa, oact) = obsa;
221 if ((error = sigaction(&sa)) != 0)
225 if ((error = copyin(obsa, &tmpbsa, sizeof(tmpbsa))) != 0)
227 bsd_to_ibcs2_sigaction(&tmpbsa, &tmpisa);
228 if ((error = copyout(&tmpisa, oisa, sizeof(tmpisa))) != 0)
236 ibcs2_sigsys(struct ibcs2_sigsys_args *uap)
238 struct proc *p = curproc;
240 int signum = ibcs2_to_bsd_sig[_SIG_IDX(IBCS2_SIGNO(SCARG(uap, sig)))];
242 caddr_t sg = stackgap_init();
244 if (signum <= 0 || signum >= IBCS2_NSIG) {
245 if (IBCS2_SIGCALL(SCARG(uap, sig)) == IBCS2_SIGNAL_MASK ||
246 IBCS2_SIGCALL(SCARG(uap, sig)) == IBCS2_SIGSET_MASK)
247 p->p_retval[0] = (int)IBCS2_SIG_ERR;
251 switch (IBCS2_SIGCALL(SCARG(uap, sig))) {
252 case IBCS2_SIGSET_MASK:
254 * Check for SIG_HOLD action.
255 * Otherwise, perform signal() except with different sa_flags.
257 if (SCARG(uap, fp) != IBCS2_SIG_HOLD) {
258 /* add sig to mask before exececuting signal handler */
262 /* else fallthrough to sighold */
264 case IBCS2_SIGHOLD_MASK:
267 struct sigprocmask_args sa;
270 SIGADDSET(mask, signum);
271 SCARG(&sa, how) = SIG_BLOCK;
272 SCARG(&sa, set) = &mask;
273 SCARG(&sa, oset) = NULL;
274 return sigprocmask(&sa);
277 case IBCS2_SIGNAL_MASK:
279 struct sigaction_args sa_args;
280 struct sigaction *nbsa, *obsa;
282 /* do not automatically block signal */
283 sa.sa_flags = SA_NODEFER;
285 if((signum != IBCS2_SIGILL) &&
286 (signum != IBCS2_SIGTRAP) &&
287 (signum != IBCS2_SIGPWR))
288 /* set to SIG_DFL before executing handler */
289 sa.sa_flags |= SA_RESETHAND;
292 nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
293 obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
294 SCARG(&sa_args, sig) = signum;
295 SCARG(&sa_args, act) = nbsa;
296 SCARG(&sa_args, oact) = obsa;
298 sa.sa_handler = SCARG(uap, fp);
299 sigemptyset(&sa.sa_mask);
301 if (signum != SIGALRM)
302 sa.sa_flags |= SA_RESTART;
304 p->p_retval[0] = (int)IBCS2_SIG_ERR; /* init error return */
306 /* perform native sigaction() */
307 if ((error = copyout(&sa, nbsa, sizeof(sa))) != 0)
309 if ((error = sigaction(&sa_args)) != 0) {
310 DPRINTF(("signal: sigaction failed: %d\n",
314 if ((error = copyin(obsa, &sa, sizeof(sa))) != 0)
316 p->p_retval[0] = (int)sa.sa_handler;
318 /* special sigset() check */
319 if(IBCS2_SIGCALL(SCARG(uap, sig)) == IBCS2_SIGSET_MASK)
320 /* check to make sure signal is not blocked */
321 if(sigismember(&p->p_sigmask, signum)) {
322 /* return SIG_HOLD and unblock signal*/
323 p->p_retval[0] = (int)IBCS2_SIG_HOLD;
324 SIGDELSET(p->p_sigmask, signum);
330 case IBCS2_SIGRELSE_MASK:
333 struct sigprocmask_args sa;
336 SIGADDSET(mask, signum);
337 SCARG(&sa, how) = SIG_UNBLOCK;
338 SCARG(&sa, set) = &mask;
339 SCARG(&sa, oset) = NULL;
340 return sigprocmask(&sa);
343 case IBCS2_SIGIGNORE_MASK:
345 struct sigaction_args sa_args;
346 struct sigaction *bsa;
348 bsa = stackgap_alloc(&sg, sizeof(struct sigaction));
349 SCARG(&sa_args, sig) = signum;
350 SCARG(&sa_args, act) = bsa;
351 SCARG(&sa_args, oact) = NULL;
353 sa.sa_handler = SIG_IGN;
354 sigemptyset(&sa.sa_mask);
356 if ((error = copyout(&sa, bsa, sizeof(sa))) != 0)
358 if ((error = sigaction(&sa_args)) != 0) {
359 DPRINTF(("sigignore: sigaction failed\n"));
365 case IBCS2_SIGPAUSE_MASK:
368 struct sigsuspend_args sa;
371 SIGDELSET(mask, signum);
372 SCARG(&sa, sigmask) = &mask;
373 return sigsuspend(&sa);
382 ibcs2_sigprocmask(struct ibcs2_sigprocmask_args *uap)
384 struct proc *p = curproc;
389 if (SCARG(uap, oset) != NULL) {
390 /* Fix the return value first if needed */
391 bsd_to_ibcs2_sigset(&p->p_sigmask, &iss);
392 if ((error = copyout(&iss, SCARG(uap, oset), sizeof(iss))) != 0)
396 if (SCARG(uap, set) == NULL)
400 if ((error = copyin(SCARG(uap, set), &iss, sizeof(iss))) != 0)
403 ibcs2_to_bsd_sigset(&iss, &bss);
407 switch (SCARG(uap, how)) {
408 case IBCS2_SIG_BLOCK:
409 SIGSETOR(p->p_sigmask, bss);
410 SIG_CANTMASK(p->p_sigmask);
413 case IBCS2_SIG_UNBLOCK:
414 SIGSETNAND(p->p_sigmask, bss);
417 case IBCS2_SIG_SETMASK:
419 SIG_CANTMASK(p->p_sigmask);
433 ibcs2_sigpending(struct ibcs2_sigpending_args *uap)
435 struct proc *p = curproc;
440 SIGSETAND(bss, p->p_sigmask);
441 bsd_to_ibcs2_sigset(&bss, &iss);
443 return copyout(&iss, SCARG(uap, mask), sizeof(iss));
447 ibcs2_sigsuspend(struct ibcs2_sigsuspend_args *uap)
451 struct sigsuspend_args sa;
454 if ((error = copyin(SCARG(uap, mask), &sss, sizeof(sss))) != 0)
457 ibcs2_to_bsd_sigset(&sss, &bss);
458 SCARG(&sa, sigmask) = &bss;
459 return sigsuspend(&sa);
463 ibcs2_pause(struct ibcs2_pause_args *uap)
465 struct proc *p = curproc;
467 struct sigsuspend_args sa;
470 SCARG(&sa, sigmask) = &mask;
471 return sigsuspend(&sa);
475 ibcs2_kill(struct ibcs2_kill_args *uap)
479 SCARG(&ka, pid) = SCARG(uap, pid);
480 SCARG(&ka, signum) = ibcs2_to_bsd_sig[_SIG_IDX(SCARG(uap, signo))];