2 signal.c -- signal handling
3 Copyright (C) 2012-2022 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 # define NSIG (_SIGMAX + 1) /* For QNX */
30 static int pipefd[2] = {-1, -1};
31 static signal_t *signal_handle[NSIG + 1] = {NULL};
33 static void signal_handler(int signum) {
34 unsigned char num = signum;
36 if(write(pipefd[1], &num, 1) != 1) {
37 // Pipe full or broken, nothing we can do about it.
41 static void signalio_handler(void *data, int flags) {
46 if(read(pipefd[0], &signum, 1) != 1) {
50 signal_t *sig = signal_handle[signum];
57 static void pipe_init(void) {
59 io_add(&signalio, signalio_handler, NULL, pipefd[0], IO_READ);
63 void signal_add(signal_t *sig, signal_cb_t cb, void *data, int signum) {
76 signal(signum, signal_handler);
78 signal_handle[signum] = sig;
81 void signal_del(signal_t *sig) {
86 signal(sig->signum, SIG_DFL);
88 signal_handle[sig->signum] = NULL;