UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / lib / apcsignal.c
blob3ce3d59b5c47d34fca29da96eaa7567ea832caf1
1 /*
2 * apcsignal.c
4 * signal() managing functions
5 */
7 /*
8 * Copyright (C) 1999-2000 Riccardo Facchetti <riccardo@master.oasi.gpa.it>
9 * Copyright (C) 1996-99 Andre M. Hedrick <andre@suse.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General
13 * Public License as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * MA 02111-1307, USA.
26 #include "apc.h"
28 #ifndef HAVE_MINGW
29 static void *terminate(void *arg)
31 // Create signal set containing SIGHUP, SIGINT, and SIGTERM
32 sigset_t sigset;
33 sigemptyset(&sigset);
34 sigaddset(&sigset, SIGHUP);
35 sigaddset(&sigset, SIGINT);
36 sigaddset(&sigset, SIGTERM);
38 // Wait for signal delivery
39 int signum, err;
42 err = sigwait(&sigset, &signum);
44 while(err == EINTR);
46 // Caught a signal; invoke handler
47 void (*handler) (int) = (void (*) (int))arg;
48 handler(signum);
49 return NULL;
51 #endif
53 void init_signals(void (*handler) (int))
55 #ifndef HAVE_MINGW
56 // Block SIGPIPE and termination signals
57 sigset_t sigset;
58 sigemptyset(&sigset);
59 sigaddset(&sigset, SIGPIPE); // Don't care
60 sigaddset(&sigset, SIGHUP); // Will be handled by terminate thread
61 sigaddset(&sigset, SIGINT); // Will be handled by terminate thread
62 sigaddset(&sigset, SIGTERM); // Will be handled by terminate thread
63 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
65 // Launch thread to synchronously wait for termination signals
66 pthread_t tid;
67 pthread_create(&tid, NULL, terminate, (void*)handler);
68 #endif