159243Sobrien/*
259243Sobrien * tc.sig.c: Signal routine emulations
359243Sobrien */
459243Sobrien/*-
559243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
659243Sobrien * All rights reserved.
759243Sobrien *
859243Sobrien * Redistribution and use in source and binary forms, with or without
959243Sobrien * modification, are permitted provided that the following conditions
1059243Sobrien * are met:
1159243Sobrien * 1. Redistributions of source code must retain the above copyright
1259243Sobrien *    notice, this list of conditions and the following disclaimer.
1359243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1459243Sobrien *    notice, this list of conditions and the following disclaimer in the
1559243Sobrien *    documentation and/or other materials provided with the distribution.
16100616Smp * 3. Neither the name of the University nor the names of its contributors
1759243Sobrien *    may be used to endorse or promote products derived from this software
1859243Sobrien *    without specific prior written permission.
1959243Sobrien *
2059243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2159243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2259243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2359243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2459243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2559243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2659243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2759243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2859243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2959243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3059243Sobrien * SUCH DAMAGE.
3159243Sobrien */
3259243Sobrien#include "sh.h"
3359243Sobrien#include "tc.wait.h"
3459243Sobrien
35167465Smpvoid
36167465Smpsigset_interrupting(int sig, void (*fn) (int))
3759243Sobrien{
38167465Smp    struct sigaction act;
3959243Sobrien
40167465Smp    act.sa_handler = fn;
41167465Smp    sigemptyset(&act.sa_mask);
42167465Smp    act.sa_flags = 0;
43167465Smp    if (sigaction(sig, &act, NULL) == 0) {
44167465Smp	sigset_t set;
45167465Smp	sigemptyset(&set);
46167465Smp	sigaddset(&set, sig);
47167465Smp	sigprocmask(SIG_UNBLOCK, &set, NULL);
48167465Smp    }
4959243Sobrien}
5059243Sobrien
51167465Smpstatic volatile sig_atomic_t alrmcatch_pending; /* = 0; */
52167465Smpstatic volatile sig_atomic_t pchild_pending; /* = 0; */
53167465Smpstatic volatile sig_atomic_t phup_pending; /* = 0; */
54167465Smpstatic volatile sig_atomic_t pintr_pending; /* = 0; */
55167465Smpint alrmcatch_disabled; /* = 0; */
56167465Smpint phup_disabled; /* = 0; */
57167465Smpint pchild_disabled; /* = 0; */
58167465Smpint pintr_disabled; /* = 0; */
5959243Sobrien
60231990Smpint
61167465Smphandle_pending_signals(void)
6259243Sobrien{
63231990Smp    int rv = 0;
64167465Smp    if (!phup_disabled && phup_pending) {
65167465Smp	phup_pending = 0;
66231990Smp	handle_interrupt++;
67167465Smp	phup();
68231990Smp	handle_interrupt--;
69167465Smp    }
70167465Smp    if (!pintr_disabled && pintr_pending) {
71167465Smp	pintr_pending = 0;
72231990Smp	handle_interrupt++;
73167465Smp	pintr();
74231990Smp	handle_interrupt--;
75231990Smp	rv = 1;
76167465Smp    }
77167465Smp    if (!pchild_disabled && pchild_pending) {
78167465Smp	pchild_pending = 0;
79231990Smp	handle_interrupt++;
80167465Smp	pchild();
81231990Smp	handle_interrupt--;
82167465Smp    }
83167465Smp    if (!alrmcatch_disabled && alrmcatch_pending) {
84167465Smp	alrmcatch_pending = 0;
85231990Smp	handle_interrupt++;
86167465Smp	alrmcatch();
87231990Smp	handle_interrupt--;
88167465Smp    }
89231990Smp    return rv;
9059243Sobrien}
9159243Sobrien
9259243Sobrienvoid
93167465Smpqueue_alrmcatch(int sig)
9459243Sobrien{
95167465Smp    USE(sig);
96167465Smp    alrmcatch_pending = 1;
9759243Sobrien}
9859243Sobrien
9959243Sobrienvoid
100167465Smpqueue_pchild(int sig)
10159243Sobrien{
102167465Smp    USE(sig);
103167465Smp    pchild_pending = 1;
10459243Sobrien}
10559243Sobrien
10659243Sobrienvoid
107167465Smpqueue_phup(int sig)
10859243Sobrien{
109167465Smp    USE(sig);
110167465Smp    phup_pending = 1;
11159243Sobrien}
11259243Sobrien
11359243Sobrienvoid
114167465Smpqueue_pintr(int sig)
11559243Sobrien{
116167465Smp    USE(sig);
117167465Smp    pintr_pending = 1;
11859243Sobrien}
11959243Sobrien
120167465Smpvoid
121167465Smpdisabled_cleanup(void *xdisabled)
12259243Sobrien{
123167465Smp    int *disabled;
12459243Sobrien
125167465Smp    disabled = xdisabled;
126167465Smp    if (--*disabled == 0)
127167465Smp	handle_pending_signals();
12859243Sobrien}
12959243Sobrien
13059243Sobrienvoid
131167465Smppintr_disabled_restore(void *xold)
13259243Sobrien{
133167465Smp    int *old;
13459243Sobrien
135167465Smp    old = xold;
136167465Smp    pintr_disabled = *old;
13759243Sobrien}
13859243Sobrien
13959243Sobrienvoid
140167465Smppintr_push_enable(int *saved)
14159243Sobrien{
142167465Smp    *saved = pintr_disabled;
143167465Smp    pintr_disabled = 0;
144167465Smp    cleanup_push(saved, pintr_disabled_restore);
145167465Smp    handle_pending_signals();
14659243Sobrien}
147