aboutsummaryrefslogtreecommitdiff
path: root/contrib/bmake/sigaction.c
blob: dc647e7b058f1df4d5fa02c921b4d57394ad68ce (plain) (blame)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* NAME:
 *      sigact.c - fake sigaction(2)
 *
 * SYNOPSIS:
 *      #include "sigact.h"
 *
 *      int sigaction(int sig, struct sigaction *act,
 *                      struct sigaction *oact);
 *      int sigaddset(sigset_t *mask, int sig);
 *      int sigdelset(sigset_t *mask, int sig);
 *      int sigemptyset(sigset_t *mask);
 *      int sigfillset(sigset_t *mask);
 *      int sigismember(sigset_t *mask, int sig);
 *      int sigpending(sigset_t *set);
 *      int sigprocmask(int how, sigset_t *set, sigset_t *oset);
 *      int sigsuspend(sigset_t *mask);
 *
 *      SIG_HDLR (*Signal(int sig, SIG_HDLR (*disp)(int)))(int);
 *
 * DESCRIPTION:
 *      This is a fake sigaction implementation.  It uses
 *      sigsetmask(2) et al or sigset(2) and friends if
 *      available, otherwise it just uses signal(2).  If it
 *      thinks sigaction(2) really exists it compiles to "almost"
 *      nothing.
 *
 *      In any case it provides a Signal() function that is
 *      implemented in terms of sigaction().
 *      If not using signal(2) as part of the underlying
 *      implementation (USE_SIGNAL or USE_SIGMASK), and
 *      NO_SIGNAL is not defined, it also provides a signal()
 *      function that calls Signal().
 *
 *      The need for all this mucking about is the problems
 *      caused by mixing various signal handling mechanisms in
 *      the one process.  This module allows for a consistent
 *      POSIX compliant interface to whatever is actually
 *      available.
 *
 *      sigaction() allows the caller to examine and/or set the
 *      action to be associated with a given signal. "act" and
 *      "oact" are pointers to 'sigaction structs':
 *.nf
 *
 *      struct sigaction
 *      {
 *             SIG_HDLR  (*sa_handler)();
 *             sigset_t  sa_mask;
 *             int       sa_flags;
 *      };
 *.fi
 *
 *      SIG_HDLR is normally 'void' in the POSIX implementation
 *      and for most current systems.  On some older UNIX
 *      systems, signal handlers do not return 'void', so
 *      this implementation keeps 'sa_handler' inline with the
 *      hosts normal signal handling conventions.
 *      'sa_mask' controls which signals will be blocked while
 *      the selected signal handler is active.  It is not used
 *      in this implementation.
 *      'sa_flags' controls various semantics such as whether
 *      system calls should be automagically restarted
 *      (SA_RESTART) etc.  It is not used in this
 *      implementation.
 *      Either "act" or "oact" may be NULL in which case the
 *      appropriate operation is skipped.
 *
 *      sigaddset() adds "sig" to the sigset_t pointed to by "mask".
 *
 *      sigdelset() removes "sig" from the sigset_t pointed to
 *      by "mask".
 *
 *      sigemptyset() makes the sigset_t pointed to by "mask" empty.
 *
 *      sigfillset() makes the sigset_t pointed to by "mask"
 *      full ie. match all signals.
 *
 *      sigismember() returns true if "sig" is found in "*mask".
 *
 *      sigpending() is supposed to return "set" loaded with the
 *      set of signals that are blocked and pending for the
 *      calling process.  It does nothing in this impementation.
 *
 *      sigprocmask() is used to examine and/or change the
 *      signal mask for the calling process.  Either "set" or
 *      "oset" may be NULL in which case the appropriate
 *      operation is skipped.  "how" may be one of SIG_BLOCK,
 *      SIG_UNBLOCK or SIG_SETMASK.  If this package is built
 *      with USE_SIGNAL, then this routine achieves nothing.
 *
 *      sigsuspend() sets the signal mask to "*mask" and waits
 *      for a signal to be delivered after which the previous
 *      mask is restored.
 *
 *
 * RETURN VALUE:
 *      0==success, -1==failure
 *
 * BUGS:
 *      Since we fake most of this, don't expect fancy usage to
 *      work.
 *
 * AUTHOR:
 *      Simon J. Gerraty <sjg@crufty.net>
 */
/* COPYRIGHT:
 *      @(#)Copyright (c) 1992-2021, 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 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 get blamed for bugs
 *      other than his own.
 *
 *      Please send copies of changes and bug-fixes to:
 *      sjg@crufty.net
 *
 */
#ifndef lint
static char *RCSid = "$Id: sigact.c,v 1.8 2021/10/14 19:39:17 sjg Exp $";
#endif

#undef _ANSI_SOURCE		/* causes problems */

#include <signal.h>
#include <sys/cdefs.h>

#ifdef HAVE_CONFIG_H
# include "config.h"
# ifdef NO_SIGSET
#   undef HAVE_SIGSET
# endif
# ifndef HAVE_SIGACTION
#   ifdef HAVE_SIGSETMASK
#     define USE_SIGMASK
#   else
#     ifdef HAVE_SIGSET
#       define USE_SIGSET
#     else
#       define USE_SIGNAL
#     endif
#   endif
# endif
#endif

/*
 * some systems have a faulty sigaction() implementation!
 * Allow us to bypass it.
 * Or they may have installed sigact.h as signal.h which is why
 * we have SA_NOCLDSTOP defined.
 */
#if !defined(SA_NOCLDSTOP) || defined(_SIGACT_H) || defined(USE_SIGNAL) || defined(USE_SIGSET) || defined(USE_SIGMASK)

/*
 * if we haven't been told,
 * try and guess what we should implement with.
 */
#if !defined(USE_SIGSET) && !defined(USE_SIGMASK) && !defined(USE_SIGNAL)
# if defined(sigmask) || defined(BSD) || defined(_BSD) && !defined(BSD41)
#   define USE_SIGMASK
# else
#   ifndef NO_SIGSET
#     define USE_SIGSET
#   else
#     define USE_SIGNAL
#   endif
# endif
#endif
/*
 * if we still don't know, we're in trouble
 */
#if !defined(USE_SIGSET) && !defined(USE_SIGMASK) && !defined(USE_SIGNAL)
error must know what to implement with
#endif

#include "sigact.h"

/*
 * in case signal() has been mapped to our Signal().
 */
#undef signal


int
sigaction(int sig,
    const struct sigaction *act,
    struct sigaction *oact)
{
	SIG_HDLR(*oldh) ();

	if (act) {
#ifdef USE_SIGSET
		oldh = sigset(sig, act->sa_handler);
#else
		oldh = signal(sig, act->sa_handler);
#endif
	} else {
		if (oact) {
#ifdef USE_SIGSET
			oldh = sigset(sig, SIG_IGN);
#else
			oldh = signal(sig, SIG_IGN);
#endif
			if (oldh != SIG_IGN && oldh != SIG_ERR) {
#ifdef USE_SIGSET
				(void) sigset(sig, oldh);
#else
				(void) signal(sig, oldh);
#endif
			}
		}
	}
	if (oact) {
		oact->sa_handler = oldh;
	}
	return 0;		/* hey we're faking it */
}

#ifndef HAVE_SIGADDSET
int
sigaddset(sigset_t *mask, int sig)
{
	*mask |= sigmask(sig);
	return 0;
}


int
sigdelset(sigset_t *mask, int sig)
{
	*mask &= ~(sigmask(sig));
	return 0;
}


int
sigemptyset(sigset_t *mask)
{
	*mask = 0;
	return 0;
}


int
sigfillset(sigset_t *mask)
{
	*mask = ~0;
	return 0;
}


int
sigismember(const sigset_t *mask, int sig)
{
	return ((*mask) & sigmask(sig));
}
#endif

#ifndef HAVE_SIGPENDING
int
sigpending(sigset_t *set)
{
	return 0;		/* faking it! */
}
#endif

#ifndef HAVE_SIGPROCMASK
int
sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
#ifdef USE_SIGSET
	int i;
#endif
	static sigset_t sm;
	static int once = 0;

	if (!once) {
		/*
	         * initally we clear sm,
	         * there after, it represents the last
	         * thing we did.
	         */
		once++;
#ifdef USE_SIGMASK
		sm = sigblock(0);
#else
		sm = 0;
#endif
	}
	if (oset)
		*oset = sm;
	if (set) {
		switch (how) {
		case SIG_BLOCK:
			sm |= *set;
			break;
		case SIG_UNBLOCK:
			sm &= ~(*set);
			break;
		case SIG_SETMASK:
			sm = *set;
			break;
		}
#ifdef USE_SIGMASK
		(void) sigsetmask(sm);
#else
#ifdef USE_SIGSET
		for (i = 1; i < NSIG; i++) {
			if (how == SIG_UNBLOCK) {
				if (*set & sigmask(i))
					sigrelse(i);
			} else
				if (sm & sigmask(i)) {
					sighold(i);
				}
		}
#endif
#endif
	}
	return 0;
}
#endif

#ifndef HAVE_SIGSUSPEND
int
sigsuspend(sigset_t *mask)
{
#ifdef USE_SIGMASK
	sigpause(*mask);
#else
	int i;

#ifdef USE_SIGSET

	for (i = 1; i < NSIG; i++) {
		if (*mask & sigmask(i)) {
			/* not the same sigpause() as above! */
			sigpause(i);
			break;
		}
	}
#else				/* signal(2) only */
	SIG_HDLR(*oldh) ();

	/*
         * make sure that signals in mask will not
         * be ignored.
         */
	for (i = 1; i < NSIG; i++) {
		if (*mask & sigmask(i)) {
			if ((oldh = signal(i, SIG_DFL)) != SIG_ERR &&
			    oldh != SIG_IGN &&
			    oldh != SIG_DFL)
				(void) signal(i, oldh);	/* restore handler */
		}
	}
	pause();		/* wait for a signal */
#endif
#endif
	return 0;
}
#endif
#endif				/* ! SA_NOCLDSTOP */

#if 0
#if !defined(SIG_HDLR)
#define SIG_HDLR void
#endif
#if !defined(SIG_ERR)
#define SIG_ERR	(SIG_HDLR (*)())-1
#endif

#if !defined(USE_SIGNAL) && !defined(USE_SIGMASK) && !defined(NO_SIGNAL)
/*
 * ensure we avoid signal mayhem
 */

extern void (*Signal (int sig, void (*handler) (int)))(int);

SIG_HDLR(*signal(int sig, SIG_HDLR(*handler)(int))
{
	return (Signal(sig, handler));
}
#endif
#endif

/* This lot (for GNU-Emacs) goes at the end of the file. */
/*
 * Local Variables:
 * version-control:t
 * comment-column:40
 * End:
 */