xref: /titanic_50/usr/src/lib/libbc/libc/sys/common/sigaction.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <errno.h>
31*7c478bd9Sstevel@tonic-gate #include <signal.h>
32*7c478bd9Sstevel@tonic-gate #include "signalmap.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate static void signal_init(void);
35*7c478bd9Sstevel@tonic-gate #pragma init(signal_init)
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate extern void (*handlers[])();
38*7c478bd9Sstevel@tonic-gate extern void maphandler(int, int, struct sigcontext *, char *);
39*7c478bd9Sstevel@tonic-gate extern void (*_siguhandler[])();		/* libucb */
40*7c478bd9Sstevel@tonic-gate extern void _sigvechandler(int, void*, void*);	/* libucb */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate extern int maptonewsig();
43*7c478bd9Sstevel@tonic-gate extern int _sigaction();
44*7c478bd9Sstevel@tonic-gate extern int maptonewmask();
45*7c478bd9Sstevel@tonic-gate extern int maptooldmask();
46*7c478bd9Sstevel@tonic-gate extern int _signal();
47*7c478bd9Sstevel@tonic-gate extern int _sigprocmask();
48*7c478bd9Sstevel@tonic-gate extern char *memset();
49*7c478bd9Sstevel@tonic-gate extern int _sigpending();
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate typedef struct {
52*7c478bd9Sstevel@tonic-gate 	unsigned long	__sigbits[4];
53*7c478bd9Sstevel@tonic-gate } S5_sigset_t;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate typedef struct {
56*7c478bd9Sstevel@tonic-gate 	int		sa_flags;
57*7c478bd9Sstevel@tonic-gate 	void		(*sa_handler)();
58*7c478bd9Sstevel@tonic-gate 	S5_sigset_t	sa_mask;
59*7c478bd9Sstevel@tonic-gate 	int		sa_resv[2];
60*7c478bd9Sstevel@tonic-gate } S5_sigaction;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #define	S5_SA_ONSTACK	0x00000001
63*7c478bd9Sstevel@tonic-gate #define	S5_SA_RESETHAND	0x00000002
64*7c478bd9Sstevel@tonic-gate #define	S5_SA_RESTART	0x00000004
65*7c478bd9Sstevel@tonic-gate #define	S5_SA_NOCLDSTOP	0x00020000
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate int
sigaction(sig,act,oact)68*7c478bd9Sstevel@tonic-gate sigaction(sig, act, oact)
69*7c478bd9Sstevel@tonic-gate int sig;
70*7c478bd9Sstevel@tonic-gate struct sigaction *act, *oact;
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	S5_sigaction	S5_act;
73*7c478bd9Sstevel@tonic-gate 	S5_sigaction	S5_oact;
74*7c478bd9Sstevel@tonic-gate 	int		ret;
75*7c478bd9Sstevel@tonic-gate 	int		newsig;
76*7c478bd9Sstevel@tonic-gate 	void		(*oldhand)();
77*7c478bd9Sstevel@tonic-gate 	void		(*oldsiguhand)();
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	newsig = maptonewsig(sig);
80*7c478bd9Sstevel@tonic-gate 	oldhand = handlers[newsig];
81*7c478bd9Sstevel@tonic-gate 	oldsiguhand = _siguhandler[newsig];
82*7c478bd9Sstevel@tonic-gate 	if (act == NULL) {
83*7c478bd9Sstevel@tonic-gate 		ret = _sigaction(newsig, (S5_sigaction *)NULL, &S5_oact);
84*7c478bd9Sstevel@tonic-gate 	} else {
85*7c478bd9Sstevel@tonic-gate 		S5_act.sa_flags = 0;
86*7c478bd9Sstevel@tonic-gate 		if (act->sa_flags & SA_ONSTACK)
87*7c478bd9Sstevel@tonic-gate 			S5_act.sa_flags |= S5_SA_ONSTACK;
88*7c478bd9Sstevel@tonic-gate 		if (act->sa_flags & SA_RESETHAND)
89*7c478bd9Sstevel@tonic-gate 			S5_act.sa_flags |= S5_SA_RESETHAND;
90*7c478bd9Sstevel@tonic-gate 		if (act->sa_flags & SA_NOCLDSTOP)
91*7c478bd9Sstevel@tonic-gate 			S5_act.sa_flags |= S5_SA_NOCLDSTOP;
92*7c478bd9Sstevel@tonic-gate 		if (!(act->sa_flags & SA_INTERRUPT))
93*7c478bd9Sstevel@tonic-gate 			S5_act.sa_flags |= S5_SA_RESTART;
94*7c478bd9Sstevel@tonic-gate 		/*
95*7c478bd9Sstevel@tonic-gate 		 * _sigvechandler() receives control from the OS.
96*7c478bd9Sstevel@tonic-gate 		 * It calls through _siguhandler[] to maphandler(),
97*7c478bd9Sstevel@tonic-gate 		 * which maps the signal number new-to-old, and
98*7c478bd9Sstevel@tonic-gate 		 * calls the user's handler through handlers[].
99*7c478bd9Sstevel@tonic-gate 		 */
100*7c478bd9Sstevel@tonic-gate 		handlers[newsig] = act->sa_handler;
101*7c478bd9Sstevel@tonic-gate 		_siguhandler[newsig] = maphandler;
102*7c478bd9Sstevel@tonic-gate 		if ((act->sa_handler == SIG_DFL) ||
103*7c478bd9Sstevel@tonic-gate 		    (act->sa_handler == SIG_IGN))
104*7c478bd9Sstevel@tonic-gate 			S5_act.sa_handler = act->sa_handler;
105*7c478bd9Sstevel@tonic-gate 		else
106*7c478bd9Sstevel@tonic-gate 			S5_act.sa_handler = _sigvechandler;
107*7c478bd9Sstevel@tonic-gate 		S5_act.sa_mask.__sigbits[0] = maptonewmask(act->sa_mask);
108*7c478bd9Sstevel@tonic-gate 		S5_act.sa_mask.__sigbits[1] = 0;
109*7c478bd9Sstevel@tonic-gate 		S5_act.sa_mask.__sigbits[2] = 0;
110*7c478bd9Sstevel@tonic-gate 		S5_act.sa_mask.__sigbits[3] = 0;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 		ret = _sigaction(newsig, &S5_act, &S5_oact);
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	if ((oact != NULL) && (ret != -1)) {
116*7c478bd9Sstevel@tonic-gate 		oact->sa_flags = 0;
117*7c478bd9Sstevel@tonic-gate 		if (S5_oact.sa_flags & S5_SA_ONSTACK)
118*7c478bd9Sstevel@tonic-gate 			oact->sa_flags |= SA_ONSTACK;
119*7c478bd9Sstevel@tonic-gate 		if (S5_oact.sa_flags & S5_SA_RESETHAND)
120*7c478bd9Sstevel@tonic-gate 			oact->sa_flags |= SA_RESETHAND;
121*7c478bd9Sstevel@tonic-gate 		if (S5_oact.sa_flags & S5_SA_NOCLDSTOP)
122*7c478bd9Sstevel@tonic-gate 			oact->sa_flags |= SA_NOCLDSTOP;
123*7c478bd9Sstevel@tonic-gate 		if (!(S5_oact.sa_flags & S5_SA_RESTART))
124*7c478bd9Sstevel@tonic-gate 			oact->sa_flags |= SA_INTERRUPT;
125*7c478bd9Sstevel@tonic-gate 		if ((S5_oact.sa_handler == SIG_DFL) ||
126*7c478bd9Sstevel@tonic-gate 		    (S5_oact.sa_handler == SIG_IGN))
127*7c478bd9Sstevel@tonic-gate 			oact->sa_handler = S5_oact.sa_handler;
128*7c478bd9Sstevel@tonic-gate 		else
129*7c478bd9Sstevel@tonic-gate 			oact->sa_handler = oldhand;
130*7c478bd9Sstevel@tonic-gate 		oact->sa_mask = maptooldmask(S5_oact.sa_mask.__sigbits[0]);
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 	if (ret == -1) {
133*7c478bd9Sstevel@tonic-gate 		handlers[newsig] = oldhand;
134*7c478bd9Sstevel@tonic-gate 		_siguhandler[newsig] = oldsiguhand;
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 	return (ret);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate static void
signal_init()140*7c478bd9Sstevel@tonic-gate signal_init() {
141*7c478bd9Sstevel@tonic-gate #define	S5_SIGPOLL	22
142*7c478bd9Sstevel@tonic-gate 	_signal(S5_SIGPOLL, SIG_IGN);
143*7c478bd9Sstevel@tonic-gate #undef S5_SIGPOLL
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate int
sigprocmask(how,set,oset)147*7c478bd9Sstevel@tonic-gate sigprocmask(how, set, oset)
148*7c478bd9Sstevel@tonic-gate int how;
149*7c478bd9Sstevel@tonic-gate sigset_t *set, *oset;
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	int		how_map[] = {0, 1, 2, 0, 3};
152*7c478bd9Sstevel@tonic-gate 	int		ret;
153*7c478bd9Sstevel@tonic-gate 	S5_sigset_t	s5_set, s5_oset;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	if (set == NULL) /* query */
156*7c478bd9Sstevel@tonic-gate 		ret = _sigprocmask(how_map[how], NULL, &s5_oset);
157*7c478bd9Sstevel@tonic-gate 	else {
158*7c478bd9Sstevel@tonic-gate 		memset(&s5_set, 0, sizeof (S5_sigset_t));
159*7c478bd9Sstevel@tonic-gate 		s5_set.__sigbits[0] = maptonewmask(*set);
160*7c478bd9Sstevel@tonic-gate 		ret = _sigprocmask(how_map[how], &s5_set, &s5_oset);
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 	if ((oset != NULL) && (ret == 0))
163*7c478bd9Sstevel@tonic-gate 		*oset = maptooldmask(s5_oset.__sigbits[0]);
164*7c478bd9Sstevel@tonic-gate 	return (ret);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate int
sigpending(set)168*7c478bd9Sstevel@tonic-gate sigpending(set)
169*7c478bd9Sstevel@tonic-gate sigset_t *set;
170*7c478bd9Sstevel@tonic-gate {
171*7c478bd9Sstevel@tonic-gate 	S5_sigset_t	s5_set;
172*7c478bd9Sstevel@tonic-gate 	int		ret;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	ret = _sigpending(&s5_set);
175*7c478bd9Sstevel@tonic-gate 	*set = maptooldmask(s5_set.__sigbits[0]);
176*7c478bd9Sstevel@tonic-gate 	return (ret);
177*7c478bd9Sstevel@tonic-gate }
178