sig.c revision 1.1 1 1.1 christos /* $NetBSD: sig.c,v 1.1 2009/04/10 13:08:25 christos 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.1 christos __RCSID("$NetBSD: sig.c,v 1.1 2009/04/10 13:08:25 christos 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.1 christos } sigqueue = { NULL, &sigqueue.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.1 christos } while (/*CONSTCOND*/ 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.1 christos * Attempt to post a signal to the sigqueue.
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.1 christos *sigqueue.qe_last = e;
120 1.1 christos sigqueue.qe_last = &e->qe_next;
121 1.1 christos }
122 1.1 christos }
123 1.1 christos
124 1.1 christos /*
125 1.1 christos * Check the sigqueue 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.1 christos while ((e = sigqueue.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.1 christos sigqueue.qe_first = e->qe_next;
148 1.1 christos if (sigqueue.qe_first == NULL)
149 1.1 christos sigqueue.qe_last = &sigqueue.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.1 christos sig_signal(int signo, sig_t handler)
166 1.1 christos {
167 1.1 christos sig_t old_handler;
168 1.1 christos sigset_t nset;
169 1.1 christos sigset_t oset;
170 1.1 christos
171 1.1 christos assert(signo > 0 && signo < NSIG);
172 1.1 christos
173 1.1 christos (void)sigemptyset(&nset);
174 1.1 christos (void)sigaddset(&nset, signo);
175 1.1 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
176 1.1 christos
177 1.1 christos old_handler = sigarray[signo];
178 1.1 christos sigarray[signo] = handler;
179 1.1 christos
180 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
181 1.1 christos
182 1.1 christos return old_handler;
183 1.1 christos }
184 1.1 christos
185 1.1 christos static void
186 1.1 christos do_default_handler(int signo, int flags)
187 1.1 christos {
188 1.1 christos struct sigaction nsa;
189 1.1 christos struct sigaction osa;
190 1.1 christos sigset_t nset;
191 1.1 christos sigset_t oset;
192 1.1 christos int save_errno;
193 1.1 christos
194 1.1 christos save_errno = errno;
195 1.1 christos (void)sigemptyset(&nsa.sa_mask);
196 1.1 christos nsa.sa_flags = flags;
197 1.1 christos nsa.sa_handler = SIG_DFL;
198 1.1 christos (void)sigaction(signo, &nsa, &osa);
199 1.1 christos
200 1.1 christos (void)sigemptyset(&nset);
201 1.1 christos (void)sigaddset(&nset, signo);
202 1.1 christos (void)sigprocmask(SIG_UNBLOCK, &nset, &oset);
203 1.1 christos
204 1.1 christos (void)kill(0, signo);
205 1.1 christos
206 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
207 1.1 christos (void)sigaction(signo, &osa, NULL);
208 1.1 christos errno = save_errno;
209 1.1 christos }
210 1.1 christos
211 1.1 christos /*
212 1.1 christos * Our generic signal handler.
213 1.1 christos */
214 1.1 christos static void
215 1.1 christos sig_handler(int signo)
216 1.1 christos {
217 1.1 christos sigset_t nset;
218 1.1 christos sigset_t oset;
219 1.1 christos
220 1.1 christos (void)sigfillset(&nset);
221 1.1 christos (void)sigprocmask(SIG_SETMASK, &nset, &oset);
222 1.1 christos
223 1.1 christos assert (signo > 0 && signo < NSIG); /* Should be guaranteed. */
224 1.1 christos
225 1.1 christos sig_post(signo);
226 1.1 christos
227 1.1 christos switch (signo) {
228 1.1 christos case SIGCONT:
229 1.1 christos assert(/*CONSTCOND*/ 0); /* We should not be seeing these. */
230 1.1 christos do_default_handler(signo, 0);
231 1.1 christos break;
232 1.1 christos
233 1.1 christos case SIGTSTP:
234 1.1 christos case SIGTTIN:
235 1.1 christos case SIGTTOU:
236 1.1 christos do_default_handler(signo, 0);
237 1.1 christos break;
238 1.1 christos
239 1.1 christos case SIGINT:
240 1.1 christos case SIGHUP:
241 1.1 christos case SIGQUIT:
242 1.1 christos case SIGPIPE:
243 1.1 christos default:
244 1.1 christos if (sigarray[signo] == SIG_DFL)
245 1.1 christos do_default_handler(signo, SA_RESTART);
246 1.1 christos break;
247 1.1 christos }
248 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
249 1.1 christos }
250 1.1 christos
251 1.1 christos /*
252 1.1 christos * Setup the signal handlers.
253 1.1 christos */
254 1.1 christos PUBLIC void
255 1.1 christos sig_setup(void)
256 1.1 christos {
257 1.1 christos sigset_t nset;
258 1.1 christos sigset_t oset;
259 1.1 christos struct sigaction sa;
260 1.1 christos struct sigaction osa;
261 1.1 christos
262 1.1 christos /* Block all signals while setting things. */
263 1.1 christos (void)sigfillset(&nset);
264 1.1 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
265 1.1 christos
266 1.1 christos /*
267 1.1 christos * Flow Control - SIGTSTP, SIGTTIN, SIGTTOU, SIGCONT:
268 1.1 christos *
269 1.1 christos * We grab SIGTSTP, SIGTTIN, and SIGTTOU so that we post the
270 1.1 christos * signals before suspending so that they are available when
271 1.1 christos * we resume. If we were to use SIGCONT instead, they will
272 1.1 christos * not get posted until SIGCONT is unblocked, even though the
273 1.1 christos * process has resumed.
274 1.1 christos *
275 1.1 christos * NOTE: We default these to SA_RESTART here, but we need to
276 1.1 christos * change this in certain cases, e.g., when reading from a
277 1.1 christos * tty.
278 1.1 christos */
279 1.1 christos (void)sigemptyset(&sa.sa_mask);
280 1.1 christos sa.sa_flags = SA_RESTART;
281 1.1 christos sa.sa_handler = sig_handler;
282 1.1 christos (void)sigaction(SIGTSTP, &sa, NULL);
283 1.1 christos (void)sigaction(SIGTTIN, &sa, NULL);
284 1.1 christos (void)sigaction(SIGTTOU, &sa, NULL);
285 1.1 christos
286 1.1 christos /*
287 1.1 christos * SIGHUP, SIGINT, and SIGQUIT:
288 1.1 christos *
289 1.1 christos * SIGHUP and SIGINT are trapped unless they are being
290 1.1 christos * ignored.
291 1.1 christos *
292 1.1 christos * Currently, we let the default handler deal with SIGQUIT.
293 1.1 christos */
294 1.1 christos (void)sigemptyset(&sa.sa_mask);
295 1.1 christos sa.sa_flags = 0;
296 1.1 christos sa.sa_handler = sig_handler;
297 1.1 christos
298 1.1 christos if (sigaction(SIGHUP, &sa, &osa) != -1 && osa.sa_handler == SIG_IGN)
299 1.1 christos (void)signal(SIGHUP, SIG_IGN);
300 1.1 christos
301 1.1 christos if (sigaction(SIGINT, &sa, &osa) != -1 && osa.sa_handler == SIG_IGN)
302 1.1 christos (void)signal(SIGINT, SIG_IGN);
303 1.1 christos #if 0
304 1.1 christos if (signal(SIGQUIT, SIG_DFL) == SIG_IGN)
305 1.1 christos (void)signal(SIGQUIT, SIG_IGN);
306 1.1 christos #endif
307 1.1 christos /*
308 1.1 christos * SIGCHLD and SIGPIPE:
309 1.1 christos *
310 1.1 christos * SIGCHLD is setup early in main. The handler lives in
311 1.1 christos * popen.c as it uses internals of that module.
312 1.1 christos *
313 1.1 christos * SIGPIPE is grabbed here. It is only used in
314 1.1 christos * lex.c:setup_piping(), cmd1.c:type1(), and cmd1.c:pipecmd().
315 1.1 christos */
316 1.1 christos (void)sigemptyset(&sa.sa_mask);
317 1.1 christos sa.sa_flags = 0;
318 1.1 christos sa.sa_handler = sig_handler;
319 1.1 christos (void)sigaction(SIGPIPE, &sa, NULL);
320 1.1 christos
321 1.1 christos /*
322 1.1 christos * Make sure our structures are initialized.
323 1.1 christos * XXX: This should be unnecessary.
324 1.1 christos */
325 1.1 christos (void)memset(sigarray, 0, sizeof(sigarray));
326 1.1 christos SIGQUEUE_INIT(&sigqueue);
327 1.1 christos
328 1.1 christos /* Restore the signal mask. */
329 1.1 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
330 1.1 christos }
331 1.1 christos
332 1.1 christos static struct { /* data shared by sig_hold() and sig_release() */
333 1.1 christos int depth; /* depth of sig_hold() */
334 1.1 christos sigset_t oset; /* old signal mask saved by sig_hold() */
335 1.1 christos } hold;
336 1.1 christos
337 1.1 christos /*
338 1.1 christos * Hold signals SIGHUP, SIGINT, and SIGQUIT.
339 1.1 christos */
340 1.1 christos PUBLIC void
341 1.1 christos sig_hold(void)
342 1.1 christos {
343 1.1 christos sigset_t nset;
344 1.1 christos
345 1.1 christos if (hold.depth++ == 0) {
346 1.1 christos (void)sigemptyset(&nset);
347 1.1 christos (void)sigaddset(&nset, SIGHUP);
348 1.1 christos (void)sigaddset(&nset, SIGINT);
349 1.1 christos (void)sigaddset(&nset, SIGQUIT);
350 1.1 christos (void)sigprocmask(SIG_BLOCK, &nset, &hold.oset);
351 1.1 christos }
352 1.1 christos }
353 1.1 christos
354 1.1 christos /*
355 1.1 christos * Release signals SIGHUP, SIGINT, and SIGQUIT.
356 1.1 christos */
357 1.1 christos PUBLIC void
358 1.1 christos sig_release(void)
359 1.1 christos {
360 1.1 christos
361 1.1 christos if (--hold.depth == 0)
362 1.1 christos (void)sigprocmask(SIG_SETMASK, &hold.oset, NULL);
363 1.1 christos }
364 1.1 christos
365 1.1 christos /*
366 1.1 christos * Unblock and ignore a signal.
367 1.1 christos */
368 1.1 christos PUBLIC int
369 1.1 christos sig_ignore(int sig, struct sigaction *osa, sigset_t *oset)
370 1.1 christos {
371 1.1 christos struct sigaction act;
372 1.1 christos sigset_t nset;
373 1.1 christos int error;
374 1.1 christos
375 1.1 christos (void)sigemptyset(&act.sa_mask);
376 1.1 christos act.sa_flags = SA_RESTART;
377 1.1 christos act.sa_handler = SIG_IGN;
378 1.1 christos error = sigaction(sig, &act, osa);
379 1.1 christos
380 1.1 christos if (error != -1) {
381 1.1 christos (void)sigemptyset(&nset);
382 1.1 christos (void)sigaddset(&nset, sig);
383 1.1 christos (void)sigprocmask(SIG_UNBLOCK, &nset, oset);
384 1.1 christos } else if (oset != NULL)
385 1.1 christos (void)sigprocmask(SIG_UNBLOCK, NULL, oset);
386 1.1 christos
387 1.1 christos return error;
388 1.1 christos }
389 1.1 christos
390 1.1 christos /*
391 1.1 christos * Restore a signal and the current signal mask.
392 1.1 christos */
393 1.1 christos PUBLIC int
394 1.1 christos sig_restore(int sig, struct sigaction *osa, sigset_t *oset)
395 1.1 christos {
396 1.1 christos int error;
397 1.1 christos
398 1.1 christos error = 0;
399 1.1 christos if (oset)
400 1.1 christos error = sigprocmask(SIG_SETMASK, oset, NULL);
401 1.1 christos if (osa)
402 1.1 christos error = sigaction(sig, osa, NULL);
403 1.1 christos
404 1.1 christos return error;
405 1.1 christos }
406 1.1 christos
407 1.1 christos /*
408 1.1 christos * Change the current flags and (optionally) return the old sigaction
409 1.1 christos * structure so we can restore things later. This is used to turn
410 1.1 christos * SA_RESTART on or off.
411 1.1 christos */
412 1.1 christos PUBLIC int
413 1.1 christos sig_setflags(int signo, int flags, struct sigaction *osa)
414 1.1 christos {
415 1.1 christos struct sigaction sa;
416 1.1 christos
417 1.1 christos if (sigaction(signo, NULL, &sa) == -1)
418 1.1 christos return -1;
419 1.1 christos if (osa)
420 1.1 christos *osa = sa;
421 1.1 christos sa.sa_flags = flags;
422 1.1 christos return sigaction(signo, &sa, NULL);
423 1.1 christos }
424 1.1 christos
425