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