summaryrefslogtreecommitdiffstats
path: root/contrib/tcsh/tc.sig.c
blob: 29bbded90004eb214e40c976b74d29eaeced9dff (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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* $Header: /src/pub/tcsh/tc.sig.c,v 3.25 2000/07/04 19:46:24 christos Exp $ */
/*
 * tc.sig.c: Signal routine emulations
 */
/*-
 * Copyright (c) 1980, 1991 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.
 */
#include "sh.h"

RCSID("$Id: tc.sig.c,v 3.25 2000/07/04 19:46:24 christos Exp $")

#include "tc.wait.h"

#ifndef BSDSIGS

/* this stack is used to queue signals
 * we can handle up to MAX_CHLD outstanding children now;
 */
#define MAX_CHLD 50

# ifdef UNRELSIGS
static struct mysigstack {
    int     s_w;		/* wait report			 */
    int     s_errno;		/* errno returned;		 */
    pid_t   s_pid;		/* pid returned			 */
}       stk[MAX_CHLD];
static int stk_ptr = -1;


/* queue child signals
 */
static sigret_t
sig_ch_queue()
{
#  ifdef JOBDEBUG
    xprintf("queue SIGCHLD\n");
    flush();
#  endif /* JOBDEBUG */
    stk_ptr++;
    stk[stk_ptr].s_pid = (pid_t) wait(&stk[stk_ptr].s_w);
    stk[stk_ptr].s_errno = errno;
    (void) signal(SIGCHLD, sig_ch_queue);
#  ifndef SIGVOID
    return(0);
#  endif /* SIGVOID */
}

/* process all awaiting child signals
 */
static sigret_t
sig_ch_rel()
{
    while (stk_ptr > -1)
	pchild(SIGCHLD);
#  ifdef JOBDEBUG
    xprintf("signal(SIGCHLD, pchild);\n");
#  endif /* JOBDEBUG */
    (void) signal(SIGCHLD, pchild);
#  ifndef SIGVOID
    return(0);
#  endif /* SIGVOID */
}


/* libc.a contains these functions in SYSVREL >= 3. */
sigret_t
(*xsigset(a, b)) ()
    int     a;
    signalfun_t  b;
{
    return (signal(a, b));
}

/* release signal
 *	release all queued signals and
 *	set the default signal handler
 */
void
sigrelse(what)
    int     what;
{
    if (what == SIGCHLD)
	sig_ch_rel();

#  ifdef COHERENT
    (void) signal(what, what == SIGINT ? pintr : SIG_DFL);
#  endif /* COHERENT */
}

/* hold signal
 * only works with child and interrupt
 */
void
xsighold(what)
    int     what;
{
    if (what == SIGCHLD)
	(void) signal(SIGCHLD, sig_ch_queue);

#  ifdef COHERENT
    (void) signal(what, SIG_IGN);
#  endif /* COHERENT */
}

/* ignore signal
 */
void
xsigignore(a)
    int     a;
{
    (void) signal(a, SIG_IGN);
}

/* atomically release one signal
 */
void
xsigpause(what)
    int     what;
{
    /* From: Jim Mattson <mattson%cs@ucsd.edu> */
    if (what == SIGCHLD)
	pchild(SIGCHLD);
}


/* return either awaiting processes or do a wait now
 */
pid_t
ourwait(w)
    int    *w;
{
    pid_t pid;

#  ifdef JOBDEBUG
    xprintf(CGETS(25, 1, "our wait %d\n"), stk_ptr);
    flush();
#  endif /* JOBDEBUG */

    if (stk_ptr == -1) {
	/* stack empty return signal from stack */
	pid = (pid_t) wait(w);
#  ifdef JOBDEBUG
	xprintf("signal(SIGCHLD, pchild);\n");
#  endif /* JOBDEBUG */
	(void) signal(SIGCHLD, pchild);
	return (pid);
    }
    else {
	/* return signal from stack */
	errno = stk[stk_ptr].s_errno;
	*w = stk[stk_ptr].s_w;
	stk_ptr--;
	return (stk[stk_ptr + 1].s_pid);
    }
} /* end ourwait */

#  ifdef COHERENT
#   undef signal
sigret_t
(*xsignal(a, b)) ()
    int     a;
    signalfun_t  b;
{
    if (a == SIGCHLD)
	return SIG_DFL;
    else
	return (signal(a, b));
}
#  endif /* COHERENT */

# endif /* UNRELSIGS */

# ifdef SXA
/*
 * SX/A is SYSVREL3 but does not have sys5-sigpause().
 * I've heard that sigpause() is not defined in SYSVREL3.
 */
/* This is not need if you make tcsh by BSD option's cc. */
void
sigpause(what)
{
    if (what == SIGCHLD) {
	(void) bsd_sigpause(bsd_sigblock((sigmask_t) 0) & ~sigmask(SIGBSDCHLD));
    }
    else if (what == 0) {
	pause();
    }
    else {
	xprintf("sigpause(%d)\n", what);
	pause();
    }
}
# endif /* SXA */

#endif /* !BSDSIGS */

#ifdef NEEDsignal
/* turn into bsd signals */
sigret_t
(*xsignal(s, a)) ()
    int     s;
    signalfun_t a;
{
    sigvec_t osv, sv;

    (void) mysigvec(s, NULL, &osv);
    sv = osv;
    sv.sv_handler = a;
#ifdef SIG_STK
    sv.sv_onstack = SIG_STK;
#endif /* SIG_STK */
#ifdef SV_BSDSIG
    sv.sv_flags = SV_BSDSIG;
#endif /* SV_BSDSIG */

    if (mysigvec(s, &sv, NULL) < 0)
	return (BADSIG);
    return (osv.sv_handler);
}

#endif /* NEEDsignal */

#ifdef POSIXSIGS
/*
 * Support for signals.
 */

extern int errno;

/* Set and test a bit.  Bits numbered 1 to 32 */

#define SETBIT(x, y)	x |= sigmask(y)
#define ISSET(x, y)	((x & sigmask(y)) != 0)

#ifdef DEBUG
# define SHOW_SIGNALS	1	/* to assist in debugging signals */
#endif /* DEBUG */

#ifdef SHOW_SIGNALS
char   *show_sig_mask();
#endif /* SHOW_SIGNALS */

#ifndef __PARAGON__
/*
 * sigsetmask(mask)
 *
 * Set a new signal mask.  Return old mask.
 */
sigmask_t
sigsetmask(mask)
    sigmask_t     mask;
{
    sigset_t set, oset;
    int     m;
    register int i;

    (void) sigemptyset(&set);
    (void) sigemptyset(&oset);

    for (i = 1; i <= MAXSIG; i++)
	if (ISSET(mask, i))
	    (void) sigaddset(&set, i);

    if ((sigprocmask(SIG_SETMASK, &set, &oset)) == -1) {
	xprintf("sigsetmask(0x%x) - sigprocmask failed, errno %d",
		mask, errno);
    }

    m = 0;
    for (i = 1; i <= MAXSIG; i++)
	if (sigismember(&oset, i))
	    SETBIT(m, i);

    return (m);
}
#endif /* __PARAGON__ */

#ifndef __DGUX__
/*
 * sigblock(mask)
 *
 * Add "mask" set of signals to the present signal mask.
 * Return old mask.
 */
sigmask_t
sigblock(mask)
    sigmask_t     mask;
{
    sigset_t set, oset;
    int     m;
    register int i;

    (void) sigemptyset(&set);
    (void) sigemptyset(&oset);

    /* Get present set of signals. */
    if ((sigprocmask(SIG_SETMASK, NULL, &set)) == -1)
	stderror(ERR_SYSTEM, "sigprocmask", strerror(errno));

    /* Add in signals from mask. */
    for (i = 1; i <= MAXSIG; i++)
	if (ISSET(mask, i))
	    (void) sigaddset(&set, i);

    if ((sigprocmask(SIG_SETMASK, &set, &oset)) == -1)
	stderror(ERR_SYSTEM, "sigprocmask", strerror(errno));

    /* Return old mask to user. */
    m = 0;
    for (i = 1; i <= MAXSIG; i++)
	if (sigismember(&oset, i))
	    SETBIT(m, i);

    return (m);
}
#endif /* __DGUX__ */


/*
 * bsd_sigpause(mask)
 *
 * Set new signal mask and wait for signal;
 * Old mask is restored on signal.
 */
void
bsd_sigpause(mask)
    sigmask_t     mask;
{
    sigset_t set;
    register int i;

    (void) sigemptyset(&set);

    for (i = 1; i <= MAXSIG; i++)
	if (ISSET(mask, i))
	    (void) sigaddset(&set, i);
    (void) sigsuspend(&set);
}

/*
 * bsd_signal(sig, func)
 *
 * Emulate bsd style signal()
 */
sigret_t (*bsd_signal(sig, func)) ()
        int sig;
        signalfun_t func;
{
        struct sigaction act, oact;
        sigset_t set;
        signalfun_t r_func;

        if (sig < 0 || sig > MAXSIG) {
                xprintf(CGETS(25, 2,
			"error: bsd_signal(%d) signal out of range\n"), sig);
                return((signalfun_t) SIG_IGN);
        }

        (void) sigemptyset(&set);

        act.sa_handler = (signalfun_t) func; /* user function */
        act.sa_mask = set;                      /* signal mask */
        act.sa_flags = 0;                       /* no special actions */

        if (sigaction(sig, &act, &oact)) {
                xprintf(CGETS(25, 3,
			"error: bsd_signal(%d) - sigaction failed, errno %d\n"),
			sig, errno);
                return((signalfun_t) SIG_IGN);
        }

        r_func = (signalfun_t) oact.sa_handler;
        return(r_func);
}
#endif /* POSIXSIG */


#ifdef SIGSYNCH
static long Synch_Cnt = 0;

sigret_t
synch_handler(sno)
int sno;
{
    if (sno != SIGSYNCH)
	abort();
    Synch_Cnt++;
}
#endif /* SIGSYNCH */
OpenPOWER on IntegriCloud