/
sighnd.c
253 lines (213 loc) · 6.32 KB
/
sighnd.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/******************************************************************************
** This file is part of profile-qt
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**
** Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer. Redistributions in
** binary form must reproduce the above copyright notice, this list of
** conditions and the following disclaimer in the documentation and/or
** other materials provided with the distribution.
**
** Neither the name of Nokia Corporation nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#include "profiled_config.h"
#include "sighnd.h"
#include "mainloop.h"
#include "logging.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <glib.h>
/* ========================================================================= *
* SIGNAL HANDING FUNCTIONALITY
* ========================================================================= */
/* list of signals to trap */
static const int sighnd_trap[] =
{
SIGINT,
SIGQUIT,
SIGTERM,
SIGHUP,
SIGUSR1,
SIGUSR2,
-1
};
static int sighnd_exit = 0;
static int sighnd_pipe[2] = {-1, -1};
static guint sighnd_watch = 0;
/* ------------------------------------------------------------------------- *
* sighnd_terminate -- terminate process from signal handler
* ------------------------------------------------------------------------- */
static
void
sighnd_terminate(void)
{
static int done = 0;
switch( ++done )
{
case 1:
mainloop_stop();
break;
case 2:
log_crit("Terminating NOW\n");
// fall through
default:
_exit(1);
break;
}
}
/* ------------------------------------------------------------------------- *
* sighnd_handler -- handle trapped signals
* ------------------------------------------------------------------------- */
static
void
sighnd_handler(int sig)
{
log_warning("Got signal [%d] %s\n", sig, strsignal(sig));
switch( sig )
{
case SIGUSR1:
log_set_level(LOG_DEBUG);
log_emit(LOG_WARNING, "log verbosity -> DEBUG\n");
break;
case SIGUSR2:
log_set_level(LOG_WARNING);
log_emit(LOG_WARNING, "log verbosity -> WARNING\n");
break;
default:
sighnd_terminate();
break;
}
}
/* ------------------------------------------------------------------------- *
* sighnd_pipe_rx_cb
* ------------------------------------------------------------------------- */
static
gboolean
sighnd_pipe_rx_cb(GIOChannel *channel, GIOCondition condition, gpointer data)
{
(void)channel; (void)condition; (void)data;
int sig = 0;
if( read(sighnd_pipe[0], &sig, sizeof sig) == sizeof sig )
{
sighnd_handler(sig);
// keep io watch alive
return TRUE;
}
// disable io watch
return FALSE;
}
/* ------------------------------------------------------------------------- *
* sighnd_pipe_tx_cb -- signal handling callback
* ------------------------------------------------------------------------- */
static void sighnd_pipe_tx_cb(int sig)
{
// restore signal handler
signal(sig, sighnd_pipe_tx_cb);
// make sure that stuck mainloop does not stop
// us from handling terminating signals
switch( sig )
{
case SIGINT:
case SIGQUIT:
case SIGTERM:
if( sighnd_exit != 0 )
{
_exit(1);
}
sighnd_exit = 1;
break;
}
// transfer the signal to mainloop via pipe
if( TEMP_FAILURE_RETRY(write(sighnd_pipe[1], &sig, sizeof sig)) != sizeof sig )
{
abort();
}
}
/* ------------------------------------------------------------------------- *
* sighnd_setup_forwarding
* ------------------------------------------------------------------------- */
static
int
sighnd_setup_forwarding(void)
{
int error = -1;
GIOChannel *channel = 0;
if( pipe(sighnd_pipe) == -1 )
{
log_err("could not create signal pipe: %s\n", strerror(errno));
goto cleanup;
}
if( (channel = g_io_channel_unix_new(sighnd_pipe[0])) == 0 )
{
log_err("could not create signal io channel\n");
goto cleanup;
}
sighnd_watch = g_io_add_watch(channel, G_IO_IN, sighnd_pipe_rx_cb, 0);
if( sighnd_watch == 0 )
{
log_err("could not create signal io watch\n");
goto cleanup;
}
error = 0;
cleanup:
if( channel != 0 )
{
g_io_channel_unref(channel);
}
return error;
}
/* ------------------------------------------------------------------------- *
* sighnd_setup_callbacks
* ------------------------------------------------------------------------- */
static
int
sighnd_setup_callbacks(void)
{
int err = 0;
for( int i = 0; sighnd_trap[i] != -1; ++i )
{
if( signal(sighnd_trap[i], sighnd_pipe_tx_cb) == SIG_ERR )
{
log_warning("could not install handler for signal %d - %s",
sighnd_trap[i], strsignal(sighnd_trap[i]));
err = -1;
}
}
return err;
}
/* ------------------------------------------------------------------------- *
* sighnd_setup -- setup signal trapping
* ------------------------------------------------------------------------- */
int
sighnd_setup(void)
{
int error = -1;
if( sighnd_setup_forwarding() != -1 &&
sighnd_setup_callbacks() != -1 )
{
error = 0;
}
return error;
}