1232633Smp/* $Header: /p/tcsh/cvsroot/tcsh/tc.sig.c,v 3.40 2012/01/25 15:34:41 christos Exp $ */
259243Sobrien/*
359243Sobrien * tc.sig.c: Signal routine emulations
459243Sobrien */
559243Sobrien/*-
659243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
759243Sobrien * All rights reserved.
859243Sobrien *
959243Sobrien * Redistribution and use in source and binary forms, with or without
1059243Sobrien * modification, are permitted provided that the following conditions
1159243Sobrien * are met:
1259243Sobrien * 1. Redistributions of source code must retain the above copyright
1359243Sobrien *    notice, this list of conditions and the following disclaimer.
1459243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1559243Sobrien *    notice, this list of conditions and the following disclaimer in the
1659243Sobrien *    documentation and/or other materials provided with the distribution.
17100616Smp * 3. Neither the name of the University nor the names of its contributors
1859243Sobrien *    may be used to endorse or promote products derived from this software
1959243Sobrien *    without specific prior written permission.
2059243Sobrien *
2159243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2259243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2359243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2459243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2559243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2659243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2759243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2859243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2959243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3059243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3159243Sobrien * SUCH DAMAGE.
3259243Sobrien */
3359243Sobrien#include "sh.h"
3459243Sobrien
35232633SmpRCSID("$tcsh: tc.sig.c,v 3.40 2012/01/25 15:34:41 christos Exp $")
3659243Sobrien
3759243Sobrien#include "tc.wait.h"
3859243Sobrien
39167465Smpvoid
40167465Smpsigset_interrupting(int sig, void (*fn) (int))
4159243Sobrien{
42167465Smp    struct sigaction act;
4359243Sobrien
44167465Smp    act.sa_handler = fn;
45167465Smp    sigemptyset(&act.sa_mask);
46167465Smp    act.sa_flags = 0;
47167465Smp    if (sigaction(sig, &act, NULL) == 0) {
48167465Smp	sigset_t set;
49167465Smp	sigemptyset(&set);
50167465Smp	sigaddset(&set, sig);
51167465Smp	sigprocmask(SIG_UNBLOCK, &set, NULL);
52167465Smp    }
5359243Sobrien}
5459243Sobrien
55167465Smpstatic volatile sig_atomic_t alrmcatch_pending; /* = 0; */
56167465Smpstatic volatile sig_atomic_t pchild_pending; /* = 0; */
57167465Smpstatic volatile sig_atomic_t phup_pending; /* = 0; */
58167465Smpstatic volatile sig_atomic_t pintr_pending; /* = 0; */
59167465Smpint alrmcatch_disabled; /* = 0; */
60167465Smpint phup_disabled; /* = 0; */
61167465Smpint pchild_disabled; /* = 0; */
62167465Smpint pintr_disabled; /* = 0; */
63232633Smpint handle_interrupt; /* = 0; */
6459243Sobrien
65232633Smpint
66167465Smphandle_pending_signals(void)
6759243Sobrien{
68232633Smp    int rv = 0;
69167465Smp    if (!phup_disabled && phup_pending) {
70167465Smp	phup_pending = 0;
71232633Smp	handle_interrupt++;
72167465Smp	phup();
73232633Smp	handle_interrupt--;
74167465Smp    }
75167465Smp    if (!pintr_disabled && pintr_pending) {
76167465Smp	pintr_pending = 0;
77232633Smp	handle_interrupt++;
78167465Smp	pintr();
79232633Smp	handle_interrupt--;
80232633Smp	rv = 1;
81167465Smp    }
82167465Smp    if (!pchild_disabled && pchild_pending) {
83167465Smp	pchild_pending = 0;
84232633Smp	handle_interrupt++;
85167465Smp	pchild();
86232633Smp	handle_interrupt--;
87167465Smp    }
88167465Smp    if (!alrmcatch_disabled && alrmcatch_pending) {
89167465Smp	alrmcatch_pending = 0;
90232633Smp	handle_interrupt++;
91167465Smp	alrmcatch();
92232633Smp	handle_interrupt--;
93167465Smp    }
94232633Smp    return rv;
9559243Sobrien}
9659243Sobrien
9759243Sobrienvoid
98167465Smpqueue_alrmcatch(int sig)
9959243Sobrien{
100167465Smp    USE(sig);
101167465Smp    alrmcatch_pending = 1;
10259243Sobrien}
10359243Sobrien
10459243Sobrienvoid
105167465Smpqueue_pchild(int sig)
10659243Sobrien{
107167465Smp    USE(sig);
108167465Smp    pchild_pending = 1;
10959243Sobrien}
11059243Sobrien
11159243Sobrienvoid
112167465Smpqueue_phup(int sig)
11359243Sobrien{
114167465Smp    USE(sig);
115167465Smp    phup_pending = 1;
11659243Sobrien}
11759243Sobrien
11859243Sobrienvoid
119167465Smpqueue_pintr(int sig)
12059243Sobrien{
121167465Smp    USE(sig);
122167465Smp    pintr_pending = 1;
12359243Sobrien}
12459243Sobrien
125167465Smpvoid
126167465Smpdisabled_cleanup(void *xdisabled)
12759243Sobrien{
128167465Smp    int *disabled;
12959243Sobrien
130167465Smp    disabled = xdisabled;
131167465Smp    if (--*disabled == 0)
132167465Smp	handle_pending_signals();
13359243Sobrien}
13459243Sobrien
13559243Sobrienvoid
136167465Smppintr_disabled_restore(void *xold)
13759243Sobrien{
138167465Smp    int *old;
13959243Sobrien
140167465Smp    old = xold;
141167465Smp    pintr_disabled = *old;
14259243Sobrien}
14359243Sobrien
14459243Sobrienvoid
145167465Smppintr_push_enable(int *saved)
14659243Sobrien{
147167465Smp    *saved = pintr_disabled;
148167465Smp    pintr_disabled = 0;
149167465Smp    cleanup_push(saved, pintr_disabled_restore);
150167465Smp    handle_pending_signals();
15159243Sobrien}
152