summaryrefslogtreecommitdiff
path: root/devel/bmake/files/sigcompat.c
blob: 23b6be8890e9d322f629ed8716b94cac837c41c2 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * NAME:
 *      sigcompat - BSD compat signals via POSIX
 *
 * SYNOPSIS:
 *	void (*signal(int  "sig", void (*"handler")(int)))(int);
 *	int sigsetmask(int "mask");
 *	int sigblock(int "mask");
 *	int sigpause(int "mask");
 *	int sigvec(int "signo", struct sigvec *"sv", struct sigvec *"osv");
 *
 * DESCRIPTION:
 *	These implement the old BSD routines via the POSIX equivalents.
 *	This module can be used to provide the missing routines, or if
 *	'FORCE_POSIX_SIGNALS' is defined, force use of these.
 *
 *	Note that signal() is identical to my Signal() routine except
 *	for checking for recursion.  Within libsig, signal() just
 *	calls Signal().
 *
 * BUGS:
 *      This package assumes POSIX signal handling is available and
 *	NOT implemeneted using these routines.  To be safe, we check
 *	for recursion and abort(3) if detected.
 *
 *	Sadly, on some systems, sigset_t is an array, and we cannot
 *	test for this via #if sizeof(sigset_t) ..., so unless
 *	'SIGSET_T_INT' is defined, we have to assume the worst and use
 *	memcpy(3) to handle args and return values.
 *
 * HISTORY:
 *	These routines originate from BSD, and are derrived from the
 *	NetBSD 1.1 implementation.  They have been seriously hacked to
 *	make them portable to other systems.
 *
 * AUTHOR:
 *      Simon J. Gerraty <sjg@crufty.net>
 */
/*
 *      @(#)Copyright (c) 1994, Simon J. Gerraty.
 *
 *      This is free software.  It comes with NO WARRANTY.
 *      Permission to use, modify and distribute this source code
 *      is granted subject to the following conditions.
 *      1/ that the above copyright notice and this notice
 *      are preserved in all copies and that due credit be given
 *      to the author.
 *      2/ that any changes to this code are clearly commented
 *      as such so that the author does not get blamed for bugs
 *      other than his own.
 *
 *      Please send copies of changes and bug-fixes to:
 *      sjg@crufty.net
 */

/*
 * Copyright (c) 1989 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <signal.h>

#if defined(sun) && !(defined(__svr4__) || defined(__SVR4))
# define NO_SIGCOMPAT
#endif

#if !defined(NO_SIGCOMPAT) && (defined(HAVE_SIGACTION) || defined(SA_NOCLDSTOP))

#if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)sigcompat.c	5.3 (Berkeley) 2/24/91";*/
static char *rcsid = "$Id: sigcompat.c,v 1.1 2005/10/31 21:34:25 reed Exp $";
#endif				/* LIBC_SCCS and not lint */

#undef signal
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <sys/cdefs.h>
#include "assert.h"

#ifndef ASSERT
# define ASSERT assert
#endif

#ifdef NDEBUG
# define _DBUG(x)
#else
# define _DBUG(x) x
#endif

#ifndef SA_RESTART
# define SA_RESTART	2
#endif
#ifndef SV_INTERRUPT
# define SV_INTERRUPT SA_RESTART
#endif

#ifndef MASK_T
# ifdef __hpux__
#   define MASK_T long
# else
#   define MASK_T int
# endif
#endif
/* I just hate HPsUX */
#if defined(__HPUX_VERSION) && __HPUX_VERSION > 9
# define  PAUSE_MASK_T int
#else
# define PAUSE_MASK_T MASK_T
#endif

#ifndef SIG_HDLR
# define SIG_HDLR void
#endif

#ifdef FORCE_POSIX_SIGNALS
#if !(defined(libsig) || defined(libsjg))
/*
 * This little block is almost identical to Signal(),
 * and make this module standalone.
 * We don't use it in libsig by default, as some apps might use both
 * and expect _SignalFlags to be used by both.
 */

#ifndef SIGNAL_FLAGS
# define SIGNAL_FLAGS 0		/* no auto-restart */
#endif
int     _signalFlags = SIGNAL_FLAGS;

SIG_HDLR(*signal(sig, handler)) __P((int))
	int     sig;
	SIG_HDLR(*handler) __P((int));
{
	_DBUG(static int depth_signal = 0);
	struct sigaction act, oact;
	int     n;

	_DBUG(++depth_signal);
	ASSERT(depth_signal < 2);
	act.sa_handler = handler;
	sigemptyset(&act.sa_mask);
	act.sa_flags = _signalFlags;
	n = sigaction(sig, &act, &oact);
	_DBUG(--depth_signal);
	if (n < 0)
		return (SIG_ERR);
	return (oact.sa_handler);
}
#else
SIG_HDLR(*signal(sig, handler)) __P((int))
	int     sig;
	SIG_HDLR(*handler) __P((int));
{
	extern  SIG_HDLR(*Signal __P((int, void (*) __P((int))))) __P((int));
	_DBUG(static int depth_signal = 0);
	SIG_HDLR(*old) __P((int));

	_DBUG(++depth_signal);
	ASSERT(depth_signal < 2);
	old = Signal(sig, handler);
	_DBUG(--depth_signal);
	return old;
}
#endif
#endif

/*
 * on some systems, sigset_t is an array...
 * it would be nicer if we could do
 * #if sizeof(sigset_t) > sizeof(MASK_T)
 */
#ifdef SIGSET_T_INT
# define ss2m(ss) (MASK_T) *(ss)
# define m2ss(ss, m)	*ss = (sigset_t) *(m)
#else
static  MASK_T
ss2m(ss)
	sigset_t *ss;
{
	MASK_T  ma[(sizeof(sigset_t) / sizeof(MASK_T)) + 1];

	memcpy((char *) ma, (char *) ss, sizeof(sigset_t));
	return ma[0];
}

static void
m2ss(ss, m)
	sigset_t *ss;
	MASK_T *m;
{
	if (sizeof(sigset_t) > sizeof(MASK_T))
		memset((char *) ss, 0, sizeof(sigset_t));

	memcpy((char *) ss, (char *) m, sizeof(MASK_T));
}
#endif

#if !defined(HAVE_SIGSETMASK) || defined(FORCE_POSIX_SIGNALS)
MASK_T
sigsetmask(mask)
	MASK_T  mask;
{
	_DBUG(static int depth_sigsetmask = 0);
	sigset_t m, omask;
	int     n;

	_DBUG(++depth_sigsetmask);
	ASSERT(depth_sigsetmask < 2);
	m2ss(&m, &mask);
	n = sigprocmask(SIG_SETMASK, (sigset_t *) & m, (sigset_t *) & omask);
	_DBUG(--depth_sigsetmask);
	if (n)
		return (n);

	return ss2m(&omask);
}


MASK_T
sigblock(mask)
	MASK_T  mask;
{
	_DBUG(static int depth_sigblock = 0);
	sigset_t m, omask;
	int     n;

	_DBUG(++depth_sigblock);
	ASSERT(depth_sigblock < 2);
	if (mask)
		m2ss(&m, &mask);
	n = sigprocmask(SIG_BLOCK, (sigset_t *) ((mask) ? &m : 0), (sigset_t *) & omask);
	_DBUG(--depth_sigblock);
	if (n)
		return (n);
	return ss2m(&omask);
}

#undef sigpause				/* Linux at least */

PAUSE_MASK_T
sigpause(mask)
	PAUSE_MASK_T  mask;
{
	_DBUG(static int depth_sigpause = 0);
	sigset_t m;
	PAUSE_MASK_T  n;

	_DBUG(++depth_sigpause);
	ASSERT(depth_sigpause < 2);
	m2ss(&m, &mask);
	n = sigsuspend(&m);
	_DBUG(--depth_sigpause);
	return n;
}
#endif

#if defined(HAVE_SIGVEC) && defined(FORCE_POSIX_SIGNALS)
int
sigvec(signo, sv, osv)
	int     signo;
	struct sigvec *sv, *osv;
{
	_DBUG(static int depth_sigvec = 0);
	int     ret;
	struct sigvec nsv;

	_DBUG(++depth_sigvec);
	ASSERT(depth_sigvec < 2);
	if (sv) {
		nsv = *sv;
		nsv.sv_flags ^= SV_INTERRUPT;	/* !SA_INTERRUPT */
	}
	ret = sigaction(signo, sv ? (struct sigaction *) & nsv : NULL,
	    (struct sigaction *) osv);
	_DBUG(--depth_sigvec);
	if (ret == 0 && osv)
		osv->sv_flags ^= SV_INTERRUPT;	/* !SA_INTERRUPT */
	return (ret);
}
#endif

#ifdef MAIN
# ifndef sigmask
#   define sigmask(n) ((unsigned int)1 << (((n) - 1) & (32 - 1)))
# endif

int
main(argc, argv)
	int     argc;
	char  **argv;
{
	MASK_T  old = 0;

	printf("expect: old=0,old=2\n");
	fflush(stdout);
	signal(SIGQUIT, SIG_IGN);
	old = sigblock(sigmask(SIGINT));
	printf("old=%d,", old);
	old = sigsetmask(sigmask(SIGALRM));
	printf("old=%d\n", old);
}
#endif
#endif