tc.sig.c revision 167465
1167465Smp/* $Header: /p/tcsh/cvsroot/tcsh/tc.sig.c,v 3.36 2006/08/24 20:56:31 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
35167465SmpRCSID("$tcsh: tc.sig.c,v 3.36 2006/08/24 20:56:31 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; */
6359243Sobrien
64167465Smpvoid
65167465Smphandle_pending_signals(void)
6659243Sobrien{
67167465Smp    if (!phup_disabled && phup_pending) {
68167465Smp	phup_pending = 0;
69167465Smp	phup();
70167465Smp    }
71167465Smp    if (!pintr_disabled && pintr_pending) {
72167465Smp	pintr_pending = 0;
73167465Smp	pintr();
74167465Smp    }
75167465Smp    if (!pchild_disabled && pchild_pending) {
76167465Smp	pchild_pending = 0;
77167465Smp	pchild();
78167465Smp    }
79167465Smp    if (!alrmcatch_disabled && alrmcatch_pending) {
80167465Smp	alrmcatch_pending = 0;
81167465Smp	alrmcatch();
82167465Smp    }
8359243Sobrien}
8459243Sobrien
8559243Sobrienvoid
86167465Smpqueue_alrmcatch(int sig)
8759243Sobrien{
88167465Smp    USE(sig);
89167465Smp    alrmcatch_pending = 1;
9059243Sobrien}
9159243Sobrien
9259243Sobrienvoid
93167465Smpqueue_pchild(int sig)
9459243Sobrien{
95167465Smp    USE(sig);
96167465Smp    pchild_pending = 1;
9759243Sobrien}
9859243Sobrien
9959243Sobrienvoid
100167465Smpqueue_phup(int sig)
10159243Sobrien{
102167465Smp    USE(sig);
103167465Smp    phup_pending = 1;
10459243Sobrien}
10559243Sobrien
10659243Sobrienvoid
107167465Smpqueue_pintr(int sig)
10859243Sobrien{
109167465Smp    USE(sig);
110167465Smp    pintr_pending = 1;
11159243Sobrien}
11259243Sobrien
113167465Smpvoid
114167465Smpdisabled_cleanup(void *xdisabled)
11559243Sobrien{
116167465Smp    int *disabled;
11759243Sobrien
118167465Smp    disabled = xdisabled;
119167465Smp    if (--*disabled == 0)
120167465Smp	handle_pending_signals();
12159243Sobrien}
12259243Sobrien
12359243Sobrienvoid
124167465Smppintr_disabled_restore(void *xold)
12559243Sobrien{
126167465Smp    int *old;
12759243Sobrien
128167465Smp    old = xold;
129167465Smp    pintr_disabled = *old;
13059243Sobrien}
13159243Sobrien
13259243Sobrienvoid
133167465Smppintr_push_enable(int *saved)
13459243Sobrien{
135167465Smp    *saved = pintr_disabled;
136167465Smp    pintr_disabled = 0;
137167465Smp    cleanup_push(saved, pintr_disabled_restore);
138167465Smp    handle_pending_signals();
13959243Sobrien}
140