ibcs2_signal.c revision 76166
1/*
2 * Copyright (c) 1995 Scott Bartram
3 * Copyright (c) 1995 Steven Wallace
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
16 *
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.
27 *
28 * $FreeBSD: head/sys/i386/ibcs2/ibcs2_signal.c 76166 2001-05-01 08:13:21Z markm $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/signalvar.h>
36#include <sys/sysproto.h>
37
38#include <i386/ibcs2/ibcs2_types.h>
39#include <i386/ibcs2/ibcs2_signal.h>
40#include <i386/ibcs2/ibcs2_proto.h>
41#include <i386/ibcs2/ibcs2_xenix.h>
42#include <i386/ibcs2/ibcs2_util.h>
43
44#define sigemptyset(s)		SIGEMPTYSET(*(s))
45#define sigismember(s, n)	SIGISMEMBER(*(s), n)
46#define sigaddset(s, n)		SIGADDSET(*(s), n)
47
48#define	ibcs2_sigmask(n)	(1 << ((n) - 1))
49#define ibcs2_sigemptyset(s)	bzero((s), sizeof(*(s)))
50#define ibcs2_sigismember(s, n)	(*(s) & ibcs2_sigmask(n))
51#define ibcs2_sigaddset(s, n)	(*(s) |= ibcs2_sigmask(n))
52
53static void ibcs2_to_bsd_sigset    __P((const ibcs2_sigset_t *, sigset_t *));
54static void bsd_to_ibcs2_sigset    __P((const sigset_t *, ibcs2_sigset_t *));
55static void ibcs2_to_bsd_sigaction __P((struct ibcs2_sigaction *,
56					struct sigaction *));
57static void bsd_to_ibcs2_sigaction __P((struct sigaction *,
58					struct ibcs2_sigaction *));
59
60int bsd_to_ibcs2_sig[IBCS2_SIGTBLSZ] = {
61	IBCS2_SIGHUP,		/* 1 */
62	IBCS2_SIGINT,		/* 2 */
63	IBCS2_SIGQUIT,		/* 3 */
64	IBCS2_SIGILL,		/* 4 */
65	IBCS2_SIGTRAP,		/* 5 */
66	IBCS2_SIGABRT,		/* 6 */
67	IBCS2_SIGEMT,		/* 7 */
68	IBCS2_SIGFPE,		/* 8 */
69	IBCS2_SIGKILL,		/* 9 */
70	IBCS2_SIGBUS,		/* 10 */
71	IBCS2_SIGSEGV,		/* 11 */
72	IBCS2_SIGSYS,		/* 12 */
73	IBCS2_SIGPIPE,		/* 13 */
74	IBCS2_SIGALRM,		/* 14 */
75	IBCS2_SIGTERM,		/* 15 */
76	0,			/* 16 - SIGURG */
77	IBCS2_SIGSTOP,		/* 17 */
78	IBCS2_SIGTSTP,		/* 18 */
79	IBCS2_SIGCONT,		/* 19 */
80	IBCS2_SIGCLD,		/* 20 */
81	IBCS2_SIGTTIN,		/* 21 */
82	IBCS2_SIGTTOU,		/* 22 */
83	IBCS2_SIGPOLL,		/* 23 */
84	0,			/* 24 - SIGXCPU */
85	0,			/* 25 - SIGXFSZ */
86	IBCS2_SIGVTALRM,	/* 26 */
87	IBCS2_SIGPROF,		/* 27 */
88	IBCS2_SIGWINCH,		/* 28 */
89	0,			/* 29 */
90	IBCS2_SIGUSR1,		/* 30 */
91	IBCS2_SIGUSR2,		/* 31 */
92	0			/* 32 */
93};
94
95static int ibcs2_to_bsd_sig[IBCS2_SIGTBLSZ] = {
96	SIGHUP,			/* 1 */
97	SIGINT,			/* 2 */
98	SIGQUIT,		/* 3 */
99	SIGILL,			/* 4 */
100	SIGTRAP,		/* 5 */
101	SIGABRT,		/* 6 */
102	SIGEMT,			/* 7 */
103	SIGFPE,			/* 8 */
104	SIGKILL,		/* 9 */
105	SIGBUS,			/* 10 */
106	SIGSEGV,		/* 11 */
107	SIGSYS,			/* 12 */
108	SIGPIPE,		/* 13 */
109	SIGALRM,		/* 14 */
110	SIGTERM,		/* 15 */
111	SIGUSR1,		/* 16 */
112	SIGUSR2,		/* 17 */
113	SIGCHLD,		/* 18 */
114	0,			/* 19 - SIGPWR */
115	SIGWINCH,		/* 20 */
116	0,			/* 21 */
117	SIGIO,			/* 22 */
118	SIGSTOP,		/* 23 */
119	SIGTSTP,		/* 24 */
120	SIGCONT,		/* 25 */
121	SIGTTIN,		/* 26 */
122	SIGTTOU,		/* 27 */
123	SIGVTALRM,		/* 28 */
124	SIGPROF,		/* 29 */
125	0,			/* 30 */
126	0,			/* 31 */
127	0			/* 32 */
128};
129
130void
131ibcs2_to_bsd_sigset(iss, bss)
132	const ibcs2_sigset_t *iss;
133	sigset_t *bss;
134{
135	int i, newsig;
136
137	sigemptyset(bss);
138	for (i = 1; i <= IBCS2_SIGTBLSZ; i++) {
139		if (ibcs2_sigismember(iss, i)) {
140			newsig = ibcs2_to_bsd_sig[_SIG_IDX(i)];
141			if (newsig)
142				sigaddset(bss, newsig);
143		}
144	}
145}
146
147static void
148bsd_to_ibcs2_sigset(bss, iss)
149	const sigset_t *bss;
150	ibcs2_sigset_t *iss;
151{
152	int i, newsig;
153
154	ibcs2_sigemptyset(iss);
155	for (i = 1; i <= IBCS2_SIGTBLSZ; i++) {
156		if (sigismember(bss, i)) {
157			newsig = bsd_to_ibcs2_sig[_SIG_IDX(i)];
158			if (newsig)
159				ibcs2_sigaddset(iss, newsig);
160		}
161	}
162}
163
164static void
165ibcs2_to_bsd_sigaction(isa, bsa)
166	struct ibcs2_sigaction *isa;
167	struct sigaction *bsa;
168{
169
170	bsa->sa_handler = isa->isa_handler;
171	ibcs2_to_bsd_sigset(&isa->isa_mask, &bsa->sa_mask);
172	bsa->sa_flags = 0;	/* ??? SA_NODEFER */
173	if ((isa->isa_flags & IBCS2_SA_NOCLDSTOP) != 0)
174		bsa->sa_flags |= SA_NOCLDSTOP;
175}
176
177static void
178bsd_to_ibcs2_sigaction(bsa, isa)
179	struct sigaction *bsa;
180	struct ibcs2_sigaction *isa;
181{
182
183	isa->isa_handler = bsa->sa_handler;
184	bsd_to_ibcs2_sigset(&bsa->sa_mask, &isa->isa_mask);
185	isa->isa_flags = 0;
186	if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
187		isa->isa_flags |= IBCS2_SA_NOCLDSTOP;
188}
189
190int
191ibcs2_sigaction(p, uap)
192	register struct proc *p;
193	struct ibcs2_sigaction_args *uap;
194{
195	struct ibcs2_sigaction *nisa, *oisa, tmpisa;
196	struct sigaction *nbsa, *obsa, tmpbsa;
197	struct sigaction_args sa;
198	caddr_t sg;
199	int error;
200
201	sg = stackgap_init();
202	nisa = SCARG(uap, act);
203	oisa = SCARG(uap, oact);
204
205	if (oisa != NULL)
206		obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
207	else
208		obsa = NULL;
209
210	if (nisa != NULL) {
211		nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
212		if ((error = copyin(nisa, &tmpisa, sizeof(tmpisa))) != 0)
213			return error;
214		ibcs2_to_bsd_sigaction(&tmpisa, &tmpbsa);
215		if ((error = copyout(&tmpbsa, nbsa, sizeof(tmpbsa))) != 0)
216			return error;
217	} else
218		nbsa = NULL;
219
220	SCARG(&sa, sig) = ibcs2_to_bsd_sig[_SIG_IDX(SCARG(uap, sig))];
221	SCARG(&sa, act) = nbsa;
222	SCARG(&sa, oact) = obsa;
223
224	if ((error = sigaction(p, &sa)) != 0)
225		return error;
226
227	if (oisa != NULL) {
228		if ((error = copyin(obsa, &tmpbsa, sizeof(tmpbsa))) != 0)
229			return error;
230		bsd_to_ibcs2_sigaction(&tmpbsa, &tmpisa);
231		if ((error = copyout(&tmpisa, oisa, sizeof(tmpisa))) != 0)
232			return error;
233	}
234
235	return 0;
236}
237
238int
239ibcs2_sigsys(p, uap)
240	register struct proc *p;
241	struct ibcs2_sigsys_args *uap;
242{
243	struct sigaction sa;
244	int signum = ibcs2_to_bsd_sig[_SIG_IDX(IBCS2_SIGNO(SCARG(uap, sig)))];
245	int error;
246	caddr_t sg = stackgap_init();
247
248	if (signum <= 0 || signum >= IBCS2_NSIG) {
249		if (IBCS2_SIGCALL(SCARG(uap, sig)) == IBCS2_SIGNAL_MASK ||
250		    IBCS2_SIGCALL(SCARG(uap, sig)) == IBCS2_SIGSET_MASK)
251			p->p_retval[0] = (int)IBCS2_SIG_ERR;
252		return EINVAL;
253	}
254
255	switch (IBCS2_SIGCALL(SCARG(uap, sig))) {
256	case IBCS2_SIGSET_MASK:
257		/*
258		 * Check for SIG_HOLD action.
259		 * Otherwise, perform signal() except with different sa_flags.
260		 */
261		if (SCARG(uap, fp) != IBCS2_SIG_HOLD) {
262			/* add sig to mask before exececuting signal handler */
263			sa.sa_flags = 0;
264			goto ibcs2_sigset;
265		}
266		/* else fallthrough to sighold */
267
268	case IBCS2_SIGHOLD_MASK:
269		{
270			sigset_t mask;
271			struct sigprocmask_args sa;
272
273			SIGEMPTYSET(mask);
274			SIGADDSET(mask, signum);
275			SCARG(&sa, how) = SIG_BLOCK;
276			SCARG(&sa, set) = &mask;
277			SCARG(&sa, oset) = NULL;
278			return sigprocmask(p, &sa);
279		}
280
281	case IBCS2_SIGNAL_MASK:
282		{
283			struct sigaction_args sa_args;
284			struct sigaction *nbsa, *obsa;
285
286			/* do not automatically block signal */
287			sa.sa_flags = SA_NODEFER;
288#ifdef SA_RESETHAND
289			if((signum != IBCS2_SIGILL) &&
290			   (signum != IBCS2_SIGTRAP) &&
291			   (signum != IBCS2_SIGPWR))
292				/* set to SIG_DFL before executing handler */
293				sa.sa_flags |= SA_RESETHAND;
294#endif
295		ibcs2_sigset:
296			nbsa = stackgap_alloc(&sg, sizeof(struct sigaction));
297			obsa = stackgap_alloc(&sg, sizeof(struct sigaction));
298			SCARG(&sa_args, sig) = signum;
299			SCARG(&sa_args, act) = nbsa;
300			SCARG(&sa_args, oact) = obsa;
301
302			sa.sa_handler = SCARG(uap, fp);
303			sigemptyset(&sa.sa_mask);
304#if 0
305			if (signum != SIGALRM)
306				sa.sa_flags |= SA_RESTART;
307#endif
308			p->p_retval[0] = (int)IBCS2_SIG_ERR; /* init error return */
309
310			/* perform native sigaction() */
311			if ((error = copyout(&sa, nbsa, sizeof(sa))) != 0)
312				return error;
313			if ((error = sigaction(p, &sa_args)) != 0) {
314				DPRINTF(("signal: sigaction failed: %d\n",
315					 error));
316				return error;
317			}
318			if ((error = copyin(obsa, &sa, sizeof(sa))) != 0)
319				return error;
320			p->p_retval[0] = (int)sa.sa_handler;
321
322			/* special sigset() check */
323                        if(IBCS2_SIGCALL(SCARG(uap, sig)) == IBCS2_SIGSET_MASK) {
324				PROC_LOCK(p);
325			        /* check to make sure signal is not blocked */
326                                if(sigismember(&p->p_sigmask, signum)) {
327				        /* return SIG_HOLD and unblock signal*/
328                                        p->p_retval[0] = (int)IBCS2_SIG_HOLD;
329					SIGDELSET(p->p_sigmask, signum);
330				}
331				PROC_UNLOCK(p);
332			}
333
334			return 0;
335		}
336
337	case IBCS2_SIGRELSE_MASK:
338		{
339			sigset_t mask;
340			struct sigprocmask_args sa;
341
342			SIGEMPTYSET(mask);
343			SIGADDSET(mask, signum);
344			SCARG(&sa, how) = SIG_UNBLOCK;
345			SCARG(&sa, set) = &mask;
346			SCARG(&sa, oset) = NULL;
347			return sigprocmask(p, &sa);
348		}
349
350	case IBCS2_SIGIGNORE_MASK:
351		{
352			struct sigaction_args sa_args;
353			struct sigaction *bsa;
354
355			bsa = stackgap_alloc(&sg, sizeof(struct sigaction));
356			SCARG(&sa_args, sig) = signum;
357			SCARG(&sa_args, act) = bsa;
358			SCARG(&sa_args, oact) = NULL;
359
360			sa.sa_handler = SIG_IGN;
361			sigemptyset(&sa.sa_mask);
362			sa.sa_flags = 0;
363			if ((error = copyout(&sa, bsa, sizeof(sa))) != 0)
364				return error;
365			if ((error = sigaction(p, &sa_args)) != 0) {
366				DPRINTF(("sigignore: sigaction failed\n"));
367				return error;
368			}
369			return 0;
370		}
371
372	case IBCS2_SIGPAUSE_MASK:
373		{
374			sigset_t mask;
375			struct sigsuspend_args sa;
376
377			PROC_LOCK(p);
378			mask = p->p_sigmask;
379			PROC_UNLOCK(p);
380			SIGDELSET(mask, signum);
381			SCARG(&sa, sigmask) = &mask;
382			return sigsuspend(p, &sa);
383		}
384
385	default:
386		return ENOSYS;
387	}
388}
389
390int
391ibcs2_sigprocmask(p, uap)
392	register struct proc *p;
393	struct ibcs2_sigprocmask_args *uap;
394{
395	ibcs2_sigset_t iss;
396	sigset_t bss;
397	int error = 0;
398
399	if (SCARG(uap, oset) != NULL) {
400		/* Fix the return value first if needed */
401		PROC_LOCK(p);
402		bsd_to_ibcs2_sigset(&p->p_sigmask, &iss);
403		PROC_UNLOCK(p);
404		if ((error = copyout(&iss, SCARG(uap, oset), sizeof(iss))) != 0)
405			return error;
406	}
407
408	if (SCARG(uap, set) == NULL)
409		/* Just examine */
410		return 0;
411
412	if ((error = copyin(SCARG(uap, set), &iss, sizeof(iss))) != 0)
413		return error;
414
415	ibcs2_to_bsd_sigset(&iss, &bss);
416
417	PROC_LOCK(p);
418
419	switch (SCARG(uap, how)) {
420	case IBCS2_SIG_BLOCK:
421		SIGSETOR(p->p_sigmask, bss);
422		SIG_CANTMASK(p->p_sigmask);
423		break;
424
425	case IBCS2_SIG_UNBLOCK:
426		SIGSETNAND(p->p_sigmask, bss);
427		break;
428
429	case IBCS2_SIG_SETMASK:
430		p->p_sigmask = bss;
431		SIG_CANTMASK(p->p_sigmask);
432		break;
433
434	default:
435		error = EINVAL;
436		break;
437	}
438
439	PROC_UNLOCK(p);
440
441	return error;
442}
443
444int
445ibcs2_sigpending(p, uap)
446	register struct proc *p;
447	struct ibcs2_sigpending_args *uap;
448{
449	sigset_t bss;
450	ibcs2_sigset_t iss;
451
452	PROC_LOCK(p);
453	bss = p->p_siglist;
454	SIGSETAND(bss, p->p_sigmask);
455	PROC_UNLOCK(p);
456	bsd_to_ibcs2_sigset(&bss, &iss);
457
458	return copyout(&iss, SCARG(uap, mask), sizeof(iss));
459}
460
461int
462ibcs2_sigsuspend(p, uap)
463	register struct proc *p;
464	struct ibcs2_sigsuspend_args *uap;
465{
466	ibcs2_sigset_t sss;
467	sigset_t bss;
468	struct sigsuspend_args sa;
469	int error;
470
471	if ((error = copyin(SCARG(uap, mask), &sss, sizeof(sss))) != 0)
472		return error;
473
474	ibcs2_to_bsd_sigset(&sss, &bss);
475	SCARG(&sa, sigmask) = &bss;
476	return sigsuspend(p, &sa);
477}
478
479int
480ibcs2_pause(p, uap)
481	register struct proc *p;
482	struct ibcs2_pause_args *uap;
483{
484	sigset_t mask;
485	struct sigsuspend_args sa;
486
487	PROC_LOCK(p);
488	mask = p->p_sigmask;
489	PROC_UNLOCK(p);
490	SCARG(&sa, sigmask) = &mask;
491	return sigsuspend(p, &sa);
492}
493
494int
495ibcs2_kill(p, uap)
496	register struct proc *p;
497	struct ibcs2_kill_args *uap;
498{
499	struct kill_args ka;
500
501	SCARG(&ka, pid) = SCARG(uap, pid);
502	SCARG(&ka, signum) = ibcs2_to_bsd_sig[_SIG_IDX(SCARG(uap, signo))];
503	return kill(p, &ka);
504}
505