sig.c revision 1.4 1 1.4 rillig /* $NetBSD: sig.c,v 1.4 2021/11/27 22:16:41 rillig Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1980, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos * 3. Neither the name of the University nor the names of its contributors
16 1.1 christos * may be used to endorse or promote products derived from this software
17 1.1 christos * without specific prior written permission.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 christos * SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/cdefs.h>
33 1.1 christos #ifndef lint
34 1.4 rillig __RCSID("$NetBSD: sig.c,v 1.4 2021/11/27 22:16:41 rillig Exp $");
35 1.1 christos #endif /* not lint */
36 1.1 christos
37 1.1 christos #include <assert.h>
38 1.1 christos #include <util.h>
39 1.1 christos #include <sys/queue.h>
40 1.1 christos
41 1.1 christos #include "rcv.h"
42 1.1 christos #include "extern.h"
43 1.1 christos #include "sig.h"
44 1.1 christos
45 1.1 christos /*
46 1.1 christos * Mail -- a mail program
47 1.1 christos *
48 1.1 christos * Signal routines.
49 1.1 christos */
50 1.1 christos
51 1.1 christos static sig_t sigarray[NSIG];
52 1.1 christos
53 1.1 christos typedef struct q_entry_s {
54 1.1 christos int qe_signo;
55 1.1 christos sig_t qe_handler;
56 1.1 christos struct q_entry_s *qe_next;
57 1.1 christos } q_entry_t;
58 1.1 christos
59 1.1 christos static struct {
60 1.1 christos q_entry_t *qe_first;
61 1.1 christos q_entry_t **qe_last;
62 1.2 dyoung } sigq = { NULL, &sigq.qe_first };
63 1.1 christos #define SIGQUEUE_INIT(p) do {\
64 1.1 christos (p)->qe_first = NULL;\
65 1.1 christos (p)->qe_last = &((p)->qe_first);\
66 1.4 rillig } while (0)
67 1.1 christos
68 1.1 christos /*
69 1.1 christos * The routines alloc_entry() and free_entry() manage the queue
70 1.1 christos * elements.
71 1.1 christos *
72 1.1 christos * Currently, they just assign one element per signo from a fix array
73 1.1 christos * as we don't support POSIX signal queues. We leave them as this may
74 1.1 christos * change in the future and the modifications will be isolated.
75 1.1 christos */
76 1.1 christos static q_entry_t *
77 1.1 christos alloc_entry(int signo)
78 1.1 christos {
79 1.1 christos static q_entry_t entries[NSIG];
80 1.1 christos q_entry_t *e;
81 1.1 christos
82 1.1 christos /*
83 1.1 christos * We currently only post one signal per signal number, so
84 1.1 christos * there is no need to make this complicated.
85 1.1 christos */
86 1.1 christos e = &entries[signo];
87 1.1 christos if (e->qe_signo != 0)
88 1.1 christos return NULL;
89 1.1 christos
90 1.1 christos e->qe_signo = signo;
91 1.1 christos e->qe_handler = sigarray[signo];
92 1.1 christos e->qe_next = NULL;
93 1.1 christos
94 1.1 christos return e;
95 1.1 christos }
96 1.1 christos
97 1.1 christos static void
98 1.1 christos free_entry(q_entry_t *e)
99 1.1 christos {
100 1.1 christos
101 1.1 christos e->qe_signo = 0;
102 1.1 christos e->qe_handler = NULL;
103 1.1 christos e->qe_next = NULL;
104 1.1 christos }
105 1.1 christos
106 1.1 christos /*
107 1.2 dyoung * Attempt to post a signal to the sigq.
108 1.1 christos */
109 1.1 christos static void
110 1.1 christos sig_post(int signo)
111 1.1 christos {
112 1.1 christos q_entry_t *e;
113 1.1 christos
114 1.1 christos if (sigarray[signo] == SIG_DFL || sigarray[signo] == SIG_IGN)
115 1.1 christos return;
116 1.1 christos
117 1.1 christos e = alloc_entry(signo);
118 1.1 christos if (e != NULL) {
119 1.2 dyoung *sigq.qe_last = e;
120 1.2 dyoung sigq.qe_last = &e->qe_next;
121 1.1 christos }
122 1.1 christos }
123 1.1 christos
124 1.1 christos /*
125 1.2 dyoung * Check the sigq for any pending signals. If any are found,
126 1.1 christos * preform the required actions and remove them from the queue.
127 1.1 christos */
128 1.1 christos PUBLIC void
129 1.1 christos sig_check(void)
130 1.1 christos {
131 1.1 christos q_entry_t *e;
132 1.1 christos sigset_t nset;
133 1.1 christos sigset_t oset;
134 1.1 christos void (*handler)(int);
135 1.1 christos int signo;
136 1.1 christos
137 1.1 christos (void)sigfillset(&nset);
138 1.1 christos (void)sigprocmask(SIG_SETMASK, &nset, &oset);
139 1.1 christos
140 1.2 dyoung while ((e = sigq.qe_first) != NULL) {
141 1.1 christos signo = e->qe_signo;
142 1.1 christos handler = e->qe_handler;
143 1.1 christos
144 1.1 christos /*
145 1.1 christos * Remove the entry from the queue and free it.
146 1.1 christos */
147 1.2 dyoung sigq.qe_first = e->qe_next;
148 1.2 dyoung if (sigq.qe_first == NULL)
149 1.2 dyoung sigq.qe_last = &sigq.qe_first;
150 1.1 christos free_entry(e);
151 1.1 christos
152 1.1 christos if (handler == SIG_DFL || handler == SIG_IGN) {
153 1.1 christos assert(/*CONSTCOND*/ 0); /* These should not get posted. */
154 1.1 christos }
155 1.1 christos else {
156 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
157 1.1 christos handler(signo);
158 1.1 christos (void)sigprocmask(SIG_SETMASK, &nset, NULL);
159 1.1 christos }
160 1.1 christos }
161 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
162 1.1 christos }
163 1.1 christos
164 1.1 christos PUBLIC sig_t
165 1.3 christos sig_current(int signo)
166 1.3 christos {
167 1.3 christos assert(signo > 0 && signo < NSIG);
168 1.3 christos return sigarray[signo];
169 1.3 christos }
170 1.3 christos
171 1.3 christos PUBLIC sig_t
172 1.1 christos sig_signal(int signo, sig_t handler)
173 1.1 christos {
174 1.1 christos sig_t old_handler;
175 1.1 christos sigset_t nset;
176 1.1 christos sigset_t oset;
177 1.1 christos
178 1.1 christos assert(signo > 0 && signo < NSIG);
179 1.1 christos
180 1.1 christos (void)sigemptyset(&nset);
181 1.1 christos (void)sigaddset(&nset, signo);
182 1.1 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
183 1.1 christos
184 1.1 christos old_handler = sigarray[signo];
185 1.1 christos sigarray[signo] = handler;
186 1.1 christos
187 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
188 1.1 christos
189 1.1 christos return old_handler;
190 1.1 christos }
191 1.1 christos
192 1.1 christos static void
193 1.1 christos do_default_handler(int signo, int flags)
194 1.1 christos {
195 1.1 christos struct sigaction nsa;
196 1.1 christos struct sigaction osa;
197 1.1 christos sigset_t nset;
198 1.1 christos sigset_t oset;
199 1.1 christos int save_errno;
200 1.1 christos
201 1.1 christos save_errno = errno;
202 1.1 christos (void)sigemptyset(&nsa.sa_mask);
203 1.1 christos nsa.sa_flags = flags;
204 1.1 christos nsa.sa_handler = SIG_DFL;
205 1.1 christos (void)sigaction(signo, &nsa, &osa);
206 1.1 christos
207 1.1 christos (void)sigemptyset(&nset);
208 1.1 christos (void)sigaddset(&nset, signo);
209 1.1 christos (void)sigprocmask(SIG_UNBLOCK, &nset, &oset);
210 1.1 christos
211 1.1 christos (void)kill(0, signo);
212 1.1 christos
213 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
214 1.1 christos (void)sigaction(signo, &osa, NULL);
215 1.1 christos errno = save_errno;
216 1.1 christos }
217 1.1 christos
218 1.1 christos /*
219 1.1 christos * Our generic signal handler.
220 1.1 christos */
221 1.1 christos static void
222 1.1 christos sig_handler(int signo)
223 1.1 christos {
224 1.1 christos sigset_t nset;
225 1.1 christos sigset_t oset;
226 1.1 christos
227 1.1 christos (void)sigfillset(&nset);
228 1.1 christos (void)sigprocmask(SIG_SETMASK, &nset, &oset);
229 1.1 christos
230 1.1 christos assert (signo > 0 && signo < NSIG); /* Should be guaranteed. */
231 1.1 christos
232 1.1 christos sig_post(signo);
233 1.1 christos
234 1.1 christos switch (signo) {
235 1.1 christos case SIGCONT:
236 1.1 christos assert(/*CONSTCOND*/ 0); /* We should not be seeing these. */
237 1.1 christos do_default_handler(signo, 0);
238 1.1 christos break;
239 1.1 christos
240 1.1 christos case SIGTSTP:
241 1.1 christos case SIGTTIN:
242 1.1 christos case SIGTTOU:
243 1.1 christos do_default_handler(signo, 0);
244 1.1 christos break;
245 1.1 christos
246 1.1 christos case SIGINT:
247 1.1 christos case SIGHUP:
248 1.1 christos case SIGQUIT:
249 1.1 christos case SIGPIPE:
250 1.1 christos default:
251 1.1 christos if (sigarray[signo] == SIG_DFL)
252 1.1 christos do_default_handler(signo, SA_RESTART);
253 1.1 christos break;
254 1.1 christos }
255 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
256 1.1 christos }
257 1.1 christos
258 1.1 christos /*
259 1.1 christos * Setup the signal handlers.
260 1.1 christos */
261 1.1 christos PUBLIC void
262 1.1 christos sig_setup(void)
263 1.1 christos {
264 1.1 christos sigset_t nset;
265 1.1 christos sigset_t oset;
266 1.1 christos struct sigaction sa;
267 1.1 christos struct sigaction osa;
268 1.1 christos
269 1.1 christos /* Block all signals while setting things. */
270 1.1 christos (void)sigfillset(&nset);
271 1.1 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
272 1.1 christos
273 1.1 christos /*
274 1.1 christos * Flow Control - SIGTSTP, SIGTTIN, SIGTTOU, SIGCONT:
275 1.1 christos *
276 1.1 christos * We grab SIGTSTP, SIGTTIN, and SIGTTOU so that we post the
277 1.1 christos * signals before suspending so that they are available when
278 1.1 christos * we resume. If we were to use SIGCONT instead, they will
279 1.1 christos * not get posted until SIGCONT is unblocked, even though the
280 1.1 christos * process has resumed.
281 1.1 christos *
282 1.1 christos * NOTE: We default these to SA_RESTART here, but we need to
283 1.1 christos * change this in certain cases, e.g., when reading from a
284 1.1 christos * tty.
285 1.1 christos */
286 1.1 christos (void)sigemptyset(&sa.sa_mask);
287 1.1 christos sa.sa_flags = SA_RESTART;
288 1.1 christos sa.sa_handler = sig_handler;
289 1.1 christos (void)sigaction(SIGTSTP, &sa, NULL);
290 1.1 christos (void)sigaction(SIGTTIN, &sa, NULL);
291 1.1 christos (void)sigaction(SIGTTOU, &sa, NULL);
292 1.1 christos
293 1.1 christos /*
294 1.1 christos * SIGHUP, SIGINT, and SIGQUIT:
295 1.1 christos *
296 1.1 christos * SIGHUP and SIGINT are trapped unless they are being
297 1.1 christos * ignored.
298 1.1 christos *
299 1.1 christos * Currently, we let the default handler deal with SIGQUIT.
300 1.1 christos */
301 1.1 christos (void)sigemptyset(&sa.sa_mask);
302 1.1 christos sa.sa_flags = 0;
303 1.1 christos sa.sa_handler = sig_handler;
304 1.1 christos
305 1.1 christos if (sigaction(SIGHUP, &sa, &osa) != -1 && osa.sa_handler == SIG_IGN)
306 1.1 christos (void)signal(SIGHUP, SIG_IGN);
307 1.1 christos
308 1.1 christos if (sigaction(SIGINT, &sa, &osa) != -1 && osa.sa_handler == SIG_IGN)
309 1.1 christos (void)signal(SIGINT, SIG_IGN);
310 1.1 christos #if 0
311 1.1 christos if (signal(SIGQUIT, SIG_DFL) == SIG_IGN)
312 1.1 christos (void)signal(SIGQUIT, SIG_IGN);
313 1.1 christos #endif
314 1.1 christos /*
315 1.1 christos * SIGCHLD and SIGPIPE:
316 1.1 christos *
317 1.1 christos * SIGCHLD is setup early in main. The handler lives in
318 1.1 christos * popen.c as it uses internals of that module.
319 1.1 christos *
320 1.1 christos * SIGPIPE is grabbed here. It is only used in
321 1.1 christos * lex.c:setup_piping(), cmd1.c:type1(), and cmd1.c:pipecmd().
322 1.1 christos */
323 1.1 christos (void)sigemptyset(&sa.sa_mask);
324 1.1 christos sa.sa_flags = 0;
325 1.1 christos sa.sa_handler = sig_handler;
326 1.1 christos (void)sigaction(SIGPIPE, &sa, NULL);
327 1.1 christos
328 1.1 christos /*
329 1.1 christos * Make sure our structures are initialized.
330 1.1 christos * XXX: This should be unnecessary.
331 1.1 christos */
332 1.1 christos (void)memset(sigarray, 0, sizeof(sigarray));
333 1.2 dyoung SIGQUEUE_INIT(&sigq);
334 1.1 christos
335 1.1 christos /* Restore the signal mask. */
336 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
337 1.1 christos }
338 1.1 christos
339 1.1 christos static struct { /* data shared by sig_hold() and sig_release() */
340 1.1 christos int depth; /* depth of sig_hold() */
341 1.1 christos sigset_t oset; /* old signal mask saved by sig_hold() */
342 1.1 christos } hold;
343 1.1 christos
344 1.1 christos /*
345 1.1 christos * Hold signals SIGHUP, SIGINT, and SIGQUIT.
346 1.1 christos */
347 1.1 christos PUBLIC void
348 1.1 christos sig_hold(void)
349 1.1 christos {
350 1.1 christos sigset_t nset;
351 1.1 christos
352 1.1 christos if (hold.depth++ == 0) {
353 1.1 christos (void)sigemptyset(&nset);
354 1.1 christos (void)sigaddset(&nset, SIGHUP);
355 1.1 christos (void)sigaddset(&nset, SIGINT);
356 1.1 christos (void)sigaddset(&nset, SIGQUIT);
357 1.1 christos (void)sigprocmask(SIG_BLOCK, &nset, &hold.oset);
358 1.1 christos }
359 1.1 christos }
360 1.1 christos
361 1.1 christos /*
362 1.1 christos * Release signals SIGHUP, SIGINT, and SIGQUIT.
363 1.1 christos */
364 1.1 christos PUBLIC void
365 1.1 christos sig_release(void)
366 1.1 christos {
367 1.1 christos
368 1.1 christos if (--hold.depth == 0)
369 1.1 christos (void)sigprocmask(SIG_SETMASK, &hold.oset, NULL);
370 1.1 christos }
371 1.1 christos
372 1.1 christos /*
373 1.1 christos * Unblock and ignore a signal.
374 1.1 christos */
375 1.1 christos PUBLIC int
376 1.1 christos sig_ignore(int sig, struct sigaction *osa, sigset_t *oset)
377 1.1 christos {
378 1.1 christos struct sigaction act;
379 1.1 christos sigset_t nset;
380 1.1 christos int error;
381 1.1 christos
382 1.1 christos (void)sigemptyset(&act.sa_mask);
383 1.1 christos act.sa_flags = SA_RESTART;
384 1.1 christos act.sa_handler = SIG_IGN;
385 1.1 christos error = sigaction(sig, &act, osa);
386 1.1 christos
387 1.1 christos if (error != -1) {
388 1.1 christos (void)sigemptyset(&nset);
389 1.1 christos (void)sigaddset(&nset, sig);
390 1.1 christos (void)sigprocmask(SIG_UNBLOCK, &nset, oset);
391 1.1 christos } else if (oset != NULL)
392 1.1 christos (void)sigprocmask(SIG_UNBLOCK, NULL, oset);
393 1.1 christos
394 1.1 christos return error;
395 1.1 christos }
396 1.1 christos
397 1.1 christos /*
398 1.1 christos * Restore a signal and the current signal mask.
399 1.1 christos */
400 1.1 christos PUBLIC int
401 1.1 christos sig_restore(int sig, struct sigaction *osa, sigset_t *oset)
402 1.1 christos {
403 1.1 christos int error;
404 1.1 christos
405 1.1 christos error = 0;
406 1.1 christos if (oset)
407 1.1 christos error = sigprocmask(SIG_SETMASK, oset, NULL);
408 1.1 christos if (osa)
409 1.1 christos error = sigaction(sig, osa, NULL);
410 1.1 christos
411 1.1 christos return error;
412 1.1 christos }
413 1.1 christos
414 1.1 christos /*
415 1.1 christos * Change the current flags and (optionally) return the old sigaction
416 1.1 christos * structure so we can restore things later. This is used to turn
417 1.1 christos * SA_RESTART on or off.
418 1.1 christos */
419 1.1 christos PUBLIC int
420 1.1 christos sig_setflags(int signo, int flags, struct sigaction *osa)
421 1.1 christos {
422 1.1 christos struct sigaction sa;
423 1.1 christos
424 1.1 christos if (sigaction(signo, NULL, &sa) == -1)
425 1.1 christos return -1;
426 1.1 christos if (osa)
427 1.1 christos *osa = sa;
428 1.1 christos sa.sa_flags = flags;
429 1.1 christos return sigaction(signo, &sa, NULL);
430 1.1 christos }
431 1.1 christos
432