1 /*
2  * lftp - file transfer program
3  *
4  * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef SIGNALHOOK_H
21 #define SIGNALHOOK_H
22 
23 #include <signal.h>
24 
25 typedef void (*signal_handler)(int);
26 
27 class SignalHook
28 {
29    static int *counts;
30    static struct sigaction *old_handlers;
31    static bool *old_saved;
32 
33    static void cnt_handler(int sig);
34    static void set_signal(int sig,signal_handler handler);
35 public:
DoCount(int sig)36    static void DoCount(int sig) { set_signal(sig,&SignalHook::cnt_handler); }
GetCount(int sig)37    static int GetCount(int sig) { return counts[sig]; }
ResetCount(int sig)38    static void ResetCount(int sig) { counts[sig]=0; }
IncreaseCount(int sig)39    static void IncreaseCount(int sig) { counts[sig]++; }
Handle(int sig,void (* h)(int))40    static void Handle(int sig,void (*h)(int)) { set_signal(sig,h); }
Ignore(int sig)41    static void Ignore(int sig)  { set_signal(sig,(signal_handler)SIG_IGN); }
Default(int sig)42    static void Default(int sig) { set_signal(sig,(signal_handler)SIG_DFL); }
43    static void Restore(int sig);
44    static void Block(int sig);
45    static void Unblock(int sig);
46    static void RestoreAll();
47 
48    static void ClassInit();
49    static void Cleanup();
50 };
51 
52 #endif//SIGNALHOOK_H
53