3 * $Id: sig.c,v 1.1 1999/07/26 23:16:26 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/07/26 23:16:26 mdw
34 * Signal handling integrated into I/O system.
38 /*----- Header files ------------------------------------------------------*/
54 /*----- Data structures ---------------------------------------------------*/
56 typedef struct sigstate {
61 /*----- Static variables --------------------------------------------------*/
63 static sel_file sigsel;
65 static sigstate *sigs;
67 /*----- Main code ---------------------------------------------------------*/
69 /* --- @sig_handler@ --- *
71 * Arguments: @int n@ = signal number
75 * Use: Generic signal handler. Just writes a single byte to the
76 * signal pipe. The byte contains the signal number.
79 static void sig_handler(int n)
81 unsigned char sch = (unsigned char)n;
82 write(sigfd, &sch, 1);
85 /* --- @sig_read@ --- *
87 * Arguments: @int fd@ = file descriptor to read
88 * @unsigned mode@ = thing to do with descriptor
89 * @void *p@ = uninteresting argument
93 * Use: Dispatches signals to their handlers safely.
96 static void sig_read(int fd, unsigned mode, void *p)
99 unsigned char buf[256];
103 while ((r = read(fd, buf, sizeof(buf))) > 0) {
104 unsigned char *p, *q;
105 for (p = buf, q = buf + r; p < q; p++) {
108 if (sigismember(&ss, n))
121 /* --- @sig_add@ --- *
123 * Arguments: @sig *s@ = pointer to signal handler block
124 * @int n@ = number of the signal
125 * @void (*proc)(int n, void *p)@ = signal handler function
126 * @void *p@ = argument to pass to handler
130 * Use: Adds a signal handler.
133 void sig_add(sig *s, int n, void (*proc)(int /*n*/, void */*p*/), void *p)
135 sigstate *ss = &sigs[n];
137 /* --- Initialize the block --- */
145 /* --- Engage a signal handler, maybe --- */
149 sa.sa_handler = sig_handler;
150 sa.sa_flags = SA_NOCLDSTOP;
152 sa.sa_flags |= SA_RESTART;
154 sigemptyset(&sa.sa_mask);
155 sigaction(n, &sa, &ss->sa);
158 /* --- Link the block into the list --- */
165 /* --- @sig_remove@ --- *
167 * Arguments: @sig *s@ = pointer to signal handler block
171 * Use: Removes the signal handler from the list.
174 void sig_remove(sig *s)
176 sigstate *ss = &sigs[s->sig];
178 /* --- Unlink the handler block --- */
181 s->next->prev = s->prev;
183 s->prev->next = s->next;
187 /* --- Maybe remove the handler --- */
190 sigaction(s->sig, &ss->sa, 0);
193 /* --- @sig_init@ --- *
195 * Arguments: @sel_state *s@ = pointer to select state
199 * Use: Initializes the signal handling system ready for use.
202 void sig_init(sel_state *s)
207 /* --- Work out how many signals there are --- */
211 unsigned min = 0, max = 0;
215 /* --- Get a good upper bound --- *
217 * Cap the search at 256. I'll be sending signal numbers as bytes.
221 while (sigaddset(&ss, nsig) == 0) {
227 /* --- Now binary search until I find the actual limit --- */
232 nsig = (min + max) >> 1;
235 if (sigaddset(&ss, nsig))
243 /* --- Initialize the signal state table --- */
248 sigs = xmalloc(nsig * sizeof(*sigs));
249 for (i = 0; i < nsig; i++)
253 /* --- Create the signal pipe --- */
256 die(1, "couldn't create pipe for signal handling");
258 /* --- Set both ends to nonblocking and close-on-exec --- */
260 fdflags(fd[0], O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
261 fdflags(fd[1], O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
263 /* --- Set everything up for the future --- */
266 sel_initfile(s, &sigsel, fd[0], SEL_READ, sig_read, 0);
267 sel_addfile(&sigsel);
270 /*----- That's all, folks -------------------------------------------------*/