/src/suricata7/src/util-signal.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-2012 Open Information Security Foundation |
2 | | * |
3 | | * You can copy, redistribute or modify this Program under the terms of |
4 | | * the GNU General Public License version 2 as published by the Free |
5 | | * Software Foundation. |
6 | | * |
7 | | * This program is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | | * GNU General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU General Public License |
13 | | * version 2 along with this program; if not, write to the Free Software |
14 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
15 | | * 02110-1301, USA. |
16 | | */ |
17 | | |
18 | | /** |
19 | | * \file |
20 | | * |
21 | | * \author Anoop Saldanha <anoopsaldanha@gmail.com> |
22 | | */ |
23 | | |
24 | | #include "suricata-common.h" |
25 | | #include "suricata.h" |
26 | | #include "util-debug.h" |
27 | | #include "util-signal.h" |
28 | | |
29 | | int UtilSignalBlock(int signum) |
30 | 80 | { |
31 | 80 | #ifndef OS_WIN32 |
32 | 80 | sigset_t x; |
33 | 80 | if (sigemptyset(&x) < 0) |
34 | 0 | return -1; |
35 | 80 | if (sigaddset(&x, signum) < 0) |
36 | 0 | return -1; |
37 | | /* don't use sigprocmask(), as it's undefined for |
38 | | * multithreaded programs. Use phtread_sigmask(). |
39 | | */ |
40 | 80 | if (pthread_sigmask(SIG_BLOCK, &x, NULL) != 0) |
41 | 0 | return -1; |
42 | 80 | #endif |
43 | 80 | return 0; |
44 | 80 | } |
45 | | |
46 | | int UtilSignalUnblock(int signum) |
47 | 0 | { |
48 | 0 | #ifndef OS_WIN32 |
49 | 0 | sigset_t x; |
50 | 0 | if (sigemptyset(&x) < 0) |
51 | 0 | return -1; |
52 | 0 | if (sigaddset(&x, signum) < 0) |
53 | 0 | return -1; |
54 | 0 | if (pthread_sigmask(SIG_UNBLOCK, &x, NULL) != 0) |
55 | 0 | return -1; |
56 | 0 | #endif |
57 | 0 | return 0; |
58 | 0 | } |
59 | | |
60 | | void UtilSignalHandlerSetup(int sig, void (*handler)(int)) |
61 | 136 | { |
62 | | #ifdef OS_WIN32 |
63 | | signal(sig, handler); |
64 | | #else |
65 | 136 | struct sigaction action; |
66 | 136 | memset(&action, 0x00, sizeof(struct sigaction)); |
67 | | |
68 | 136 | action.sa_handler = handler; |
69 | 136 | sigemptyset(&(action.sa_mask)); |
70 | 136 | sigaddset(&(action.sa_mask),sig); |
71 | 136 | action.sa_flags = 0; |
72 | 136 | sigaction(sig, &action, 0); |
73 | 136 | #endif /* OS_WIN32 */ |
74 | | |
75 | 136 | return; |
76 | 136 | } |
77 | | |
78 | | #if 0 |
79 | | int UtilSignalIsHandler(int sig, void (*handler)(int)) |
80 | | { |
81 | | struct sigaction action; |
82 | | memset(&action, 0x00, sizeof(struct sigaction)); |
83 | | |
84 | | sigaction(sig, NULL, &action); |
85 | | |
86 | | return (action.sa_handler == handler); |
87 | | } |
88 | | #endif |