new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / shared / sig_op.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 /* Functions to handle signal manipulation */
11
12 #ifndef WIN32
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <signal.h>
17 #include <string.h>
18
19 #include "sig_op.h"
20 #include "file_op.h"
21 #include "debug_op.h"
22 #include "error_messages/error_messages.h"
23
24 static const char *pidfile = NULL;
25
26
27 void HandleSIG(int sig)
28 {
29     merror(SIGNAL_RECV, pidfile, sig, strsignal(sig));
30
31     DeletePID(pidfile);
32
33     exit(1);
34 }
35
36
37 /* To avoid client-server communication problems */
38 void HandleSIGPIPE(__attribute__((unused)) int sig)
39 {
40     return;
41 }
42
43 void StartSIG(const char *process_name)
44 {
45     pidfile = process_name;
46
47     signal(SIGHUP, SIG_IGN);
48     signal(SIGINT, HandleSIG);
49     signal(SIGQUIT, HandleSIG);
50     signal(SIGTERM, HandleSIG);
51     signal(SIGALRM, HandleSIG);
52     signal(SIGPIPE, HandleSIGPIPE);
53 }
54
55 void StartSIG2(const char *process_name, void (*func)(int))
56 {
57     pidfile = process_name;
58
59     signal(SIGHUP, SIG_IGN);
60     signal(SIGINT, func);
61     signal(SIGQUIT, func);
62     signal(SIGTERM, func);
63     signal(SIGALRM, func);
64     signal(SIGPIPE, HandleSIGPIPE);
65 }
66
67 #endif /* !WIN32 */
68