[292045]: server / signals.cpp Maximize Restore History

Download this file

signals.cpp    348 lines (288 with data), 7.7 kB

  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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright (C) 2010 David Sugar, Tycho Softworks.
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
#include "server.h"
#ifdef HAVE_SYS_INOTIFY_H
#include <sys/inotify.h>
#include <sys/poll.h>
#endif
NAMESPACE_SIPWITCH
#ifdef HAVE_SIGWAIT
signals signals::thread;
signals::signals() :
JoinableThread()
{
shutdown = started = false;
}
signals::~signals()
{
if(!shutdown)
cancel();
}
void signals::cancel(void)
{
if(started) {
shutdown = true;
#ifdef __FreeBSD__
raise(SIGINT);
#endif
pthread_kill(tid, SIGALRM);
join();
}
}
void signals::run(void)
{
int signo;
unsigned period = 900;
started = true;
shell::log(DEBUG1, "starting signal handler");
for(;;) {
alarm(period);
#ifdef HAVE_SIGWAIT2
int result = sigwait(&sigs, &signo);
if(result) {
alarm(0);
shell::log(shell::ERR, "signal handler error %d", errno);
Thread::sleep(1000);
continue;
}
#else
signo = sigwait(&sigs);
if(signo < 0) {
alarm(0);
shell::log(shell::ERR, "signal handler error %d", errno);
Thread:sleep(1000);
continue;
}
#endif
alarm(0);
if(shutdown)
break;
shell::log(DEBUG1, "received signal %d", signo);
switch(signo) {
case SIGALRM:
shell::log(shell::INFO, "system housekeeping");
registry::cleanup(period);
events::sync(period);
cache::cleanup();
break;
case SIGINT:
case SIGTERM:
control::send("down");
break;
case SIGUSR1:
control::send("snapshot");
break;
case SIGHUP:
control::send("reload");
break;
default:
break;
}
}
shell::log(DEBUG1, "stopping signal handler");
}
void signals::service(const char *name)
{
}
void signals::setup(void)
{
sigemptyset(&thread.sigs);
sigaddset(&thread.sigs, SIGALRM);
sigaddset(&thread.sigs, SIGHUP);
sigaddset(&thread.sigs, SIGINT);
sigaddset(&thread.sigs, SIGTERM);
sigaddset(&thread.sigs, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &thread.sigs, NULL);
signal(SIGPIPE, SIG_IGN);
}
void signals::start(void)
{
thread.background();
}
void signals::stop(void)
{
thread.cancel();
}
#elif defined(WIN32)
static SERVICE_STATUS_HANDLE hStatus = 0;
static SERVICE_STATUS status;
static void WINAPI handler(DWORD sigint)
{
switch(sigint) {
case 128:
// control::request("reload");
return;
case 129:
// control::request("snapshot");
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
status.dwCurrentState = SERVICE_STOP_PENDING;
status.dwWin32ExitCode = 0;
status.dwCheckPoint = 0;
status.dwWaitHint = 6000;
SetServiceStatus(hStatus, &status);
// control::request("down");
break;
default:
break;
}
}
void signals::service(const char *name)
{
memset(&status, 0, sizeof(SERVICE_STATUS));
status.dwServiceType = SERVICE_WIN32;
status.dwCurrentState = SERVICE_START_PENDING;
status.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
hStatus = ::RegisterServiceCtrlHandler(name, &handler);
}
void signals::setup(void)
{
}
void signals::start(void)
{
if(!hStatus)
return;
status.dwCurrentState = SERVICE_RUNNING;
::SetServiceStatus(hStatus, &status);
}
void signals::stop(void)
{
if(!hStatus)
return;
status.dwCurrentState = SERVICE_STOPPED;
::SetServiceStatus(hStatus, &status);
}
#else
void signals::service(const char *name)
{
}
void signals::setup(void)
{
}
void signals::start(void)
{
}
void signals::stop(void)
{
}
#endif
#ifdef HAVE_SYS_INOTIFY_H
static const char *dirpath;
static fd_t watcher = -1;
static uint32_t dirnode;
notify notify::thread;
notify::notify() : JoinableThread()
{
}
notify::~notify()
{
notify::stop();
}
void notify::start(void)
{
dirpath = control::env("users");
if(!dirpath)
dirpath = control::env("prefix");
if(fsys::is_dir(dirpath))
thread.background();
else
shell::log(shell::ERR, "notify failed; %s missing", dirpath);
}
void notify::run(void)
{
static bool logged = false;
timeout_t timeout = -1;
unsigned updates = 0;
struct pollfd pfd;
shell::log(DEBUG1, "notify watching %s", dirpath);
watcher = inotify_init();
dirnode = inotify_add_watch(watcher, dirpath, IN_CLOSE_WRITE|IN_MOVED_TO|IN_MOVED_FROM|IN_DELETE|IN_DONT_FOLLOW);
while(watcher != -1) {
// we want 500 ms of inactivity before actual updating...
if(updates)
timeout = 500;
else
timeout = 1000;
pfd.fd = watcher;
pfd.events = POLLIN | POLLNVAL | POLLERR;
pfd.revents = 0;
int result = poll(&pfd, 1, timeout);
// if error, we sleep....we should also not get errors...
if(result < 0) {
if(!logged) {
shell::log(shell::ERR, "notify error %d", errno);
logged = true;
}
// throttle on repeated errors...
Thread::sleep(1000);
continue;
}
if(!result) {
if(!updates)
continue;
// clear updates and process config on timeout
control::send("reload");
updates = 0;
continue;
}
if(pfd.revents & (POLLNVAL|POLLERR))
break;
if(pfd.revents & POLLIN) {
char buffer[512];
size_t offset = 0;
size_t len = ::read(watcher, &buffer, sizeof(buffer));
if(len < sizeof(struct inotify_event)) {
shell::log(shell::ERR, "notify failed to read inotify");
continue;
}
while(offset < len) {
struct inotify_event *event = (struct inotify_event *)&buffer[offset];
if(!event->len)
break;
// only if xml files updated do we care...
const char *ext = strrchr(event->name, '.');
if(ext && eq_case(ext, ".xml")) {
shell::log(DEBUG2, "%s updated", event->name);
++updates;
}
else if(eq(event->name, "reload")) {
shell::log(DEBUG2, "reload requested");
control::send("reload");
updates = 0;
}
offset += sizeof(struct inotify_event) + event->len;
}
}
}
shell::log(DEBUG1, "notify terminating");
}
void notify::stop(void)
{
if(watcher != -1) {
fd_t fd = watcher;
watcher = -1;
::close(fd);
thread.join();
}
}
#else
void notify::start(void)
{
}
void notify::stop(void)
{
}
#endif
END_NAMESPACE