xref: /freebsd/contrib/tcsh/tc.sig.c (revision dc318a4ffabcbfa23bb56a33403aad36e6de30af)
1 /*
2  * tc.sig.c: Signal routine emulations
3  */
4 /*-
5  * Copyright (c) 1980, 1991 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 #include "sh.h"
33 #include "tc.wait.h"
34 
35 void
36 sigset_interrupting(int sig, void (*fn) (int))
37 {
38     struct sigaction act;
39 
40     act.sa_handler = fn;
41     sigemptyset(&act.sa_mask);
42     act.sa_flags = 0;
43     if (sigaction(sig, &act, NULL) == 0) {
44 	sigset_t set;
45 	sigemptyset(&set);
46 	sigaddset(&set, sig);
47 	sigprocmask(SIG_UNBLOCK, &set, NULL);
48     }
49 }
50 
51 static volatile sig_atomic_t alrmcatch_pending; /* = 0; */
52 static volatile sig_atomic_t pchild_pending; /* = 0; */
53 static volatile sig_atomic_t phup_pending; /* = 0; */
54 static volatile sig_atomic_t pintr_pending; /* = 0; */
55 int alrmcatch_disabled; /* = 0; */
56 int phup_disabled; /* = 0; */
57 int pchild_disabled; /* = 0; */
58 int pintr_disabled; /* = 0; */
59 
60 int
61 handle_pending_signals(void)
62 {
63     int rv = 0;
64     if (!phup_disabled && phup_pending) {
65 	phup_pending = 0;
66 	handle_interrupt++;
67 	phup();
68 	handle_interrupt--;
69     }
70     if (!pintr_disabled && pintr_pending) {
71 	pintr_pending = 0;
72 	handle_interrupt++;
73 	pintr();
74 	handle_interrupt--;
75 	rv = 1;
76     }
77     if (!pchild_disabled && pchild_pending) {
78 	pchild_pending = 0;
79 	handle_interrupt++;
80 	pchild();
81 	handle_interrupt--;
82     }
83     if (!alrmcatch_disabled && alrmcatch_pending) {
84 	alrmcatch_pending = 0;
85 	handle_interrupt++;
86 	alrmcatch();
87 	handle_interrupt--;
88     }
89     return rv;
90 }
91 
92 void
93 queue_alrmcatch(int sig)
94 {
95     USE(sig);
96     alrmcatch_pending = 1;
97 }
98 
99 void
100 queue_pchild(int sig)
101 {
102     USE(sig);
103     pchild_pending = 1;
104 }
105 
106 void
107 queue_phup(int sig)
108 {
109     USE(sig);
110     phup_pending = 1;
111 }
112 
113 void
114 queue_pintr(int sig)
115 {
116     USE(sig);
117     pintr_pending = 1;
118 }
119 
120 void
121 disabled_cleanup(void *xdisabled)
122 {
123     int *disabled;
124 
125     disabled = xdisabled;
126     if (--*disabled == 0)
127 	handle_pending_signals();
128 }
129 
130 void
131 pintr_disabled_restore(void *xold)
132 {
133     int *old;
134 
135     old = xold;
136     pintr_disabled = *old;
137 }
138 
139 void
140 pintr_push_enable(int *saved)
141 {
142     *saved = pintr_disabled;
143     pintr_disabled = 0;
144     cleanup_push(saved, pintr_disabled_restore);
145     handle_pending_signals();
146 }
147