summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbc/libc/sys/common/signalmap.c
blob: 7642964fb0513a0f97cf63c72ad18628c4d086d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "signalmap.h"
#include <sys/signal.h>
#include <sys/errno.h>

extern int errno;
void (*handlers[32])();		/* XXX - 32???  NSIG, maybe? */

void
maphandler(int sig, int code, struct sigcontext *scp, char *addr)
{
	switch (sig) {
		case SIGBUS:
		case SIGSEGV:
			switch (FC_CODE(code)) {
			case 3:		/* 5.x value for FC_OBJERR */
				code = FC_MAKE_ERR(FC_ERRNO(code));
				break;
			case 5:		/* 5.x value for FC_NOMAP */
				code = FC_NOMAP;
				break;
			}
			break;
	}
	__sendsig(maptooldsig(sig), code, scp, addr, handlers[sig]);
}
	
void (*
signal(int sig, void (*a)(int)))(int)
{
	int newsig;

	struct sigvec osv, sv;

	sv.sv_handler = a;
	sv.sv_mask = 0;
#ifdef S5EMUL
	sv.sv_flags = SV_INTERRUPT|SV_RESETHAND;
#else
	sv.sv_flags = 0;
#endif
	if (sigvec(sig, &sv, &osv) < 0)
		return (BADSIG);
	return (osv.sv_handler);
}


int
sigvec(int sig, struct sigvec *nvec, struct sigvec *ovec)
{
	int newsig;
	struct sigvec tvec, *tvecp;
	void (*oldhand)(int);

	if ((int)nvec == -1 || (int)ovec == -1) {
		errno = EFAULT;
		return (-1);
	}

	newsig = maptonewsig(sig);
	oldhand = handlers[newsig];

	if ((tvecp = nvec) != 0) {
		tvec = *nvec;
		tvecp = &tvec;
		/*
		 * To be compatible with the behavior of SunOS 4.x:
		 * If the new signal handler is SIG_IGN or SIG_DFL,
		 * do not change the signal's entry in the handler array.
		 * This allows a child of vfork(2) to set signal handlers
		 * to SIG_IGN or SIG_DFL without affecting the parent.
		 */
		if (tvecp->sv_handler != SIG_DFL &&
		    tvecp->sv_handler != SIG_IGN) {
			handlers[newsig] = tvecp->sv_handler;
			tvecp->sv_handler = maphandler;
		}
	}

	if (ucbsigvec(newsig, tvecp, ovec) == -1) {
		handlers[newsig] = oldhand;
		return (-1);
	}

	if (ovec && ovec->sv_handler != SIG_DFL && ovec->sv_handler != SIG_IGN)
		ovec->sv_handler = oldhand;

	return (0);
}

int
sigsetmask(int mask)
{
	int ret;
	ret = ucbsigsetmask(maptonewmask(mask));
	return (maptooldmask(ret));
}

int
sigblock(int mask)
{
	int ret;
	ret = ucbsigblock(maptonewmask(mask));
	return (maptooldmask(ret));
}


int
sigpause(int mask)
{
	int ret;
	return (ucbsigpause(maptonewmask(mask)));
}

int
siginterrupt(int sig, int flag)
{
	return (ucbsiginterrupt(maptonewsig(sig), flag));
}


int
maptonewsig(int sig)
{
	switch (sig) {
	case SIGURG:               /* urgent condition on IO channel */
		return (XSIGURG);
	case SIGSTOP:              /* sendable stop signal not from tty */
		return (XSIGSTOP);
	case SIGTSTP:            /* stop signal from tty */
		return (XSIGTSTP);
	case SIGCONT:            /* continue a stopped process */
		return (XSIGCONT);
	case SIGCLD:          /* System V name for SIGCHLD */
		return (XSIGCLD);
	case SIGTTIN:          /* to readers pgrp upon background tty read */
		return (XSIGTTIN);
	case SIGTTOU:         /* like TTIN for output */
		return (XSIGTTOU);
	case SIGIO:        /* input/output possible signal */
		return (XSIGIO);
	case SIGXCPU:       /* exceeded CPU time limit */
		return (XSIGXCPU);
	case SIGXFSZ:       /* exceeded file size limit */
		return (XSIGXFSZ);
	case SIGVTALRM:      /* virtual time alarm */
		return (XSIGVTALRM);
	case SIGPROF:        /* profiling time alarm */
		return (XSIGPROF);
	case SIGWINCH:       /* window changed */
		return (XSIGWINCH);
	case SIGLOST:	     /* resource lost, not supported */
		return (-1);
	case SIGUSR1:
		return (XSIGUSR1);
	case SIGUSR2:      /* user defined signal 2 */
		return (XSIGUSR2);
	default:
		return (sig);
	}
}

int
maptooldsig(int sig)
{
	switch (sig) {
	case XSIGURG:               /* urgent condition on IO channel */
		return (SIGURG);
	case XSIGSTOP:              /* sendable stop signal not from tty */
		return (SIGSTOP);
	case XSIGTSTP:            /* stop signal from tty */
		return (SIGTSTP);
	case XSIGCONT:            /* continue a stopped process */
		return (SIGCONT);
	case XSIGCLD:          /* System V name for SIGCHLD */
		return (SIGCLD);
	case XSIGTTIN:          /* to readers pgrp upon background tty read */
		return (SIGTTIN);
	case XSIGTTOU:         /* like TTIN for output */
		return (SIGTTOU);
	case XSIGIO:        /* input/output possible signal */
		return (SIGIO);
	case XSIGXCPU:       /* exceeded CPU time limit */
		return (SIGXCPU);
	case XSIGXFSZ:       /* exceeded file size limit */
		return (SIGXFSZ);
	case XSIGVTALRM:      /* virtual time alarm */
		return (SIGVTALRM);
	case XSIGPROF:        /* profiling time alarm */
		return (SIGPROF);
	case XSIGWINCH:       /* window changed */
		return (SIGWINCH);
	case XSIGUSR1:
		return (SIGUSR1);
	case XSIGUSR2:      /* user defined signal 2 */
		return (SIGUSR2);
	case XSIGPWR:      /* user defined signal 2 */
		return (-1);
	default:
		return (sig);
	}
}

int
maptooldmask(int mask)
{
	int omask;

	omask = mask & 0x7FFF;		/* these signo are same */

	if (mask & sigmask(XSIGURG))
		omask |= sigmask(SIGURG);
	if (mask & sigmask(XSIGSTOP))
		omask |= sigmask(SIGSTOP);
	if (mask & sigmask(XSIGTSTP))
		omask |= sigmask(SIGTSTP);
	if (mask & sigmask(XSIGCONT))
		omask |= sigmask(SIGCONT);
	if (mask & sigmask(XSIGCLD))
		omask |= sigmask(SIGCLD);
	if (mask & sigmask(XSIGTTIN))
		omask |= sigmask(SIGTTIN);
	if (mask & sigmask(XSIGTTOU))
		omask |= sigmask(SIGTTOU);
	if (mask & sigmask(XSIGIO))
		omask |= sigmask(SIGIO);
	if (mask & sigmask(XSIGXCPU))
		omask |= sigmask(SIGXCPU);
	if (mask & sigmask(XSIGXFSZ))
		omask |= sigmask(SIGXFSZ);
	if (mask & sigmask(XSIGVTALRM))
		omask |= sigmask(SIGVTALRM);
	if (mask & sigmask(XSIGPROF))
		omask |= sigmask(SIGPROF);
	if (mask & sigmask(XSIGWINCH))
		omask |= sigmask(SIGWINCH);
	if (mask & sigmask(XSIGUSR1))
		omask |= sigmask(SIGUSR1);
	if (mask & sigmask(XSIGUSR2))
		omask |= sigmask(SIGUSR2);
	return (omask);
}	


int
maptonewmask(int omask)
{
	int mask;

	if (omask == -1) {
		return (-1);
	}

	mask = omask & 0x7FFF;		/* these signo are the same */

	if (omask & sigmask(SIGURG))
		mask |= sigmask(XSIGURG);
	if (omask & sigmask(SIGSTOP))
		mask |= sigmask(XSIGSTOP);
	if (omask & sigmask(SIGTSTP))
		mask |= sigmask(XSIGTSTP);
	if (omask & sigmask(SIGCONT))
		mask |= sigmask(XSIGCONT);
	if (omask & sigmask(SIGCLD))
		mask |= sigmask(XSIGCLD);
	if (omask & sigmask(SIGTTIN))
		mask |= sigmask(XSIGTTIN);
	if (omask & sigmask(SIGTTOU))
		mask |= sigmask(XSIGTTOU);
	if (omask & sigmask(SIGIO))
		mask |= sigmask(XSIGIO);
	if (omask & sigmask(SIGXCPU))
		mask |= sigmask(XSIGXCPU);
	if (omask & sigmask(SIGXFSZ))
		mask |= sigmask(XSIGXFSZ);
	if (omask & sigmask(SIGVTALRM))
		mask |= sigmask(XSIGVTALRM);
	if (omask & sigmask(SIGPROF))
		mask |= sigmask(XSIGPROF);
	if (omask & sigmask(SIGWINCH))
		mask |= sigmask(XSIGWINCH);
	if (omask & sigmask(SIGUSR1))
		mask |= sigmask(XSIGUSR1);
	if (omask & sigmask(SIGUSR2))
		mask |= sigmask(XSIGUSR2);
	return (mask);
}