-
Notifications
You must be signed in to change notification settings - Fork 4
/
signals.c
170 lines (152 loc) · 3.59 KB
/
signals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Disktest
* Copyright (c) International Business Machines Corp., 2005
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Please send e-mail to yardleyb@us.ibm.com if you have
* questions or comments.
*
* Project Website: TBD
*
* $Id: signals.c,v 1.4 2007/03/16 01:06:05 yardleyb Exp $
*/
#ifdef WINDOWS
#include <windows.h>
#else
#include <pthread.h>
#endif
#include <signal.h>
#include "threading.h"
#include "signals.h"
/*
* global variable used to indicate what signal
* (if any) has been caught
*/
int handled_signal = -1;
int signal_action = SIGNAL_NONE;
/*
* mutex to be used whenever accessing the above
* global data
*/
#ifdef WINDOWS
HANDLE sig_mutex;
#else
pthread_mutex_t sig_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
#ifdef WINDOWS
void sig_handler( int sig )
#else
void* sig_handler( void* arg )
#endif
{
#ifndef WINDOWS
sigset_t signal_set;
int sig;
int rv;
/* wait for any and all signals */
sigfillset( &signal_set );
#ifdef AIX
/* except in AIX, can't sigwait on this signals */
sigdelset( &signal_set, SIGKILL );
sigdelset( &signal_set, SIGWAITING );
sigdelset( &signal_set, SIGSTOP );
#endif
for(;;) {
rv = sigwait( &signal_set, &sig );
#endif
switch( sig ) {
case SIGQUIT:
LOCK(sig_mutex);
handled_signal = SIGQUIT;
signal_action |= SIGNAL_STOP;
UNLOCK(sig_mutex);
break;
case SIGINT:
LOCK(sig_mutex);
handled_signal = SIGINT;
signal_action |= SIGNAL_STOP;
UNLOCK(sig_mutex);
break;
case SIGTERM:
LOCK(sig_mutex);
handled_signal = SIGTERM;
signal_action |= SIGNAL_STOP;
UNLOCK(sig_mutex);
break;
case SIGHUP:
LOCK(sig_mutex);
handled_signal = SIGHUP;
signal_action |= SIGNAL_STOP;
UNLOCK(sig_mutex);
break;
case SIGUSR1:
LOCK(sig_mutex);
handled_signal = SIGUSR1;
signal_action |= SIGNAL_STAT;
UNLOCK(sig_mutex);
break;
/* whatever you need to do for other signals */
default:
LOCK(sig_mutex);
handled_signal = 0;
UNLOCK(sig_mutex);
break;
}
#ifndef WINDOWS
}
return (void *)0;
#endif
}
void setup_sig_mask( void )
{
#ifndef WINDOWS
sigset_t signal_set;
pthread_t sig_thread;
#endif
#ifdef WINDOWS
if((sig_mutex = CreateMutex(NULL, FALSE, NULL)) == NULL) {
return;
}
#endif
/* block all signals */
#ifdef WINDOWS
signal( SIGINT, sig_handler );
signal( SIGTERM, sig_handler );
signal( SIGUSR1, sig_handler );
#else
sigemptyset( &signal_set );
sigaddset( &signal_set, SIGINT );
sigaddset( &signal_set, SIGHUP );
sigaddset( &signal_set, SIGQUIT );
sigaddset( &signal_set, SIGTERM );
sigaddset( &signal_set, SIGUSR1 );
#ifdef AIX
sigthreadmask(SIG_SETMASK, &signal_set, NULL);
#else
pthread_sigmask( SIG_SETMASK, &signal_set, NULL );
#endif
/* create the signal handling thread */
pthread_create( &sig_thread, NULL, sig_handler, NULL );
#endif
}
void clear_stat_signal( void )
{
if(signal_action & SIGNAL_STAT) {
LOCK(sig_mutex);
signal_action &= ~SIGNAL_STAT;
UNLOCK(sig_mutex);
}
}