1 /*
2  * Copyright (C) Tildeslash Ltd. All rights reserved.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License version 3.
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 Affero General Public License
13  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14  *
15  * In addition, as a special exception, the copyright holders give
16  * permission to link the code of portions of this program with the
17  * OpenSSL library under certain conditions as described in each
18  * individual source file, and distribute linked combinations
19  * including the two.
20  *
21  * You must obey the GNU Affero General Public License in all respects
22  * for all of the code used other than OpenSSL.
23  */
24 
25 #include "config.h"
26 
27 #ifdef HAVE_STDIO_H
28 #include <stdio.h>
29 #endif
30 
31 #ifdef HAVE_SIGNAL_H
32 #include <signal.h>
33 #endif
34 
35 #include "monit.h"
36 
37 /**
38  *  Signal handling routines.
39  *
40  *  @file
41  */
42 
43 
44 /* ------------------------------------------------------------------ Public */
45 
46 
47 #if ! defined HAVE_ASAN && ! defined FREEBSD
48 /**
49  * Replace the standard signal() function, with a more reliable
50  * using sigaction. From W. Richard Stevens' "Advanced Programming
51  * in the UNIX Environment"
52  */
signal(int signo,Sigfunc * func)53 Sigfunc *signal(int signo, Sigfunc *func) {
54         struct sigaction act, oact;
55 
56         act.sa_handler = func;
57         sigemptyset(&act.sa_mask);
58         act.sa_flags = 0;
59         if (signo == SIGALRM) {
60 #ifdef  SA_INTERRUPT
61                 act.sa_flags |= SA_INTERRUPT;   /* SunOS */
62 #endif
63         } else {
64 #ifdef  SA_RESTART
65                 act.sa_flags |= SA_RESTART;             /* SVR4, 44BSD */
66 #endif
67         }
68         if (sigaction(signo, &act, &oact) < 0)
69                 return(SIG_ERR);
70 
71         return(oact.sa_handler);
72 }
73 #endif
74 
75 
76 /**
77  * Set a collective thread signal block for signals honored by monit
78  */
set_signal_block()79 void set_signal_block() {
80         sigset_t mask;
81         sigemptyset(&mask);
82         sigaddset(&mask, SIGHUP);
83         sigaddset(&mask, SIGINT);
84         sigaddset(&mask, SIGUSR1);
85         sigaddset(&mask, SIGTERM);
86         pthread_sigmask(SIG_BLOCK, &mask, NULL);
87 }
88 
89