xref: /dragonfly/contrib/tcsh-6/tc.sig.c (revision 0de61e28)
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 int handle_interrupt; /* = 0; */
60 
61 int
62 handle_pending_signals(void)
63 {
64     int rv = 0;
65     if (!phup_disabled && phup_pending) {
66 	phup_pending = 0;
67 	handle_interrupt++;
68 	phup();
69 	handle_interrupt--;
70     }
71     if (!pintr_disabled && pintr_pending) {
72 	pintr_pending = 0;
73 	handle_interrupt++;
74 	pintr();
75 	handle_interrupt--;
76 	rv = 1;
77     }
78     if (!pchild_disabled && pchild_pending) {
79 	pchild_pending = 0;
80 	handle_interrupt++;
81 	pchild();
82 	handle_interrupt--;
83     }
84     if (!alrmcatch_disabled && alrmcatch_pending) {
85 	alrmcatch_pending = 0;
86 	handle_interrupt++;
87 	alrmcatch();
88 	handle_interrupt--;
89     }
90     return rv;
91 }
92 
93 void
94 queue_alrmcatch(int sig)
95 {
96     USE(sig);
97     alrmcatch_pending = 1;
98 }
99 
100 void
101 queue_pchild(int sig)
102 {
103     USE(sig);
104     pchild_pending = 1;
105 }
106 
107 void
108 queue_phup(int sig)
109 {
110     USE(sig);
111     phup_pending = 1;
112 }
113 
114 void
115 queue_pintr(int sig)
116 {
117     USE(sig);
118     pintr_pending = 1;
119 }
120 
121 void
122 disabled_cleanup(void *xdisabled)
123 {
124     int *disabled;
125 
126     disabled = xdisabled;
127     if (--*disabled == 0)
128 	handle_pending_signals();
129 }
130 
131 void
132 pintr_disabled_restore(void *xold)
133 {
134     int *old;
135 
136     old = xold;
137     pintr_disabled = *old;
138 }
139 
140 void
141 pintr_push_enable(int *saved)
142 {
143     *saved = pintr_disabled;
144     pintr_disabled = 0;
145     cleanup_push(saved, pintr_disabled_restore);
146     handle_pending_signals();
147 }
148