3 * $Id: sig.c,v 1.3 2004/04/08 01:36:13 mdw Exp $
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
46 /*----- Data structures ---------------------------------------------------*/
48 typedef struct sigstate {
53 /*----- Static variables --------------------------------------------------*/
55 static sel_file sigsel;
57 static sigstate *sigs;
58 static sigset_t ss_all, ss_caught;
61 /*----- Main code ---------------------------------------------------------*/
63 /* --- @sig_handler@ --- *
65 * Arguments: @int n@ = signal number
69 * Use: Generic signal handler. Writes a single byte to the
70 * signal pipe. The byte contains the signal number. Also
71 * sets the appropriate bit in @ss_caught@ to indicate which
72 * signals are pending.
75 static void sig_handler(int n)
78 unsigned char sch = (unsigned char)n;
79 sigprocmask(SIG_BLOCK, &ss_all, 0);
80 sigaddset(&ss_caught, n);
81 write(sigfd, &sch, 1);
82 /* The system should reset the signal mask here. */
86 /* --- @sig_read@ --- *
88 * Arguments: @int fd@ = file descriptor to read
89 * @unsigned mode@ = thing to do with descriptor
90 * @void *p@ = uninteresting argument
94 * Use: Dispatches signals to their handlers safely.
97 static void sig_read(int fd, unsigned mode, void *p)
101 /* --- Read the currently caught signals --- *
103 * Block signals while the mask is being copied.
108 unsigned char buf[256];
110 sigprocmask(SIG_BLOCK, &ss_all, &oss);
112 sigemptyset(&ss_caught);
113 while (read(fd, buf, sizeof(buf)) > 0)
115 sigprocmask(SIG_SETMASK, &oss, 0);
118 /* --- Process the caught signals --- */
122 for (i = 0; i < nsig; i++) {
124 if (!sigismember(&ss, i))
136 /* --- @sig_add@ --- *
138 * Arguments: @sig *s@ = pointer to signal handler block
139 * @int n@ = number of the signal
140 * @void (*proc)(int n, void *p)@ = signal handler function
141 * @void *p@ = argument to pass to handler
145 * Use: Adds a signal handler.
148 void sig_add(sig *s, int n, void (*proc)(int /*n*/, void */*p*/), void *p)
150 sigstate *ss = &sigs[n];
152 /* --- Initialize the block --- */
160 /* --- Engage a signal handler, maybe --- */
164 sa.sa_handler = sig_handler;
165 sa.sa_flags = SA_NOCLDSTOP;
167 sa.sa_flags |= SA_RESTART;
169 sigemptyset(&sa.sa_mask);
170 sigaddset(&ss_all, n);
171 sigaction(n, &sa, &ss->sa);
174 /* --- Link the block into the list --- */
181 /* --- @sig_remove@ --- *
183 * Arguments: @sig *s@ = pointer to signal handler block
187 * Use: Removes the signal handler from the list.
190 void sig_remove(sig *s)
192 sigstate *ss = &sigs[s->sig];
194 /* --- Unlink the handler block --- */
197 s->next->prev = s->prev;
199 s->prev->next = s->next;
203 /* --- Maybe remove the handler --- */
206 sigaction(s->sig, &ss->sa, 0);
207 sigdelset(&ss_all, s->sig);
211 /* --- @sig_init@ --- *
213 * Arguments: @sel_state *s@ = pointer to select state
217 * Use: Initializes the signal handling system ready for use.
220 void sig_init(sel_state *s)
224 /* --- Work out how many signals there are --- */
228 unsigned min = 0, max = 0;
232 /* --- Get a good upper bound --- *
234 * Cap the search at 256. I'll be sending signal numbers as bytes.
238 while (sigaddset(&ss, nsig) == 0) {
244 /* --- Now binary search until I find the actual limit --- */
249 nsig = (min + max) >> 1;
252 if (sigaddset(&ss, nsig))
260 /* --- Initialize the signal state table --- */
265 sigs = xmalloc(nsig * sizeof(*sigs));
266 for (i = 0; i < nsig; i++)
270 /* --- Create the signal pipe --- */
273 die(1, "couldn't create pipe for signal handling");
275 /* --- Set both ends to nonblocking and close-on-exec --- */
277 fdflags(fd[0], O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
278 fdflags(fd[1], O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
280 /* --- Set everything up for the future --- */
283 sel_initfile(s, &sigsel, fd[0], SEL_READ, sig_read, 0);
284 sel_addfile(&sigsel);
285 sigemptyset(&ss_all);
286 sigemptyset(&ss_caught);
289 /*----- That's all, folks -------------------------------------------------*/