-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalistener.cc
57 lines (47 loc) · 1.12 KB
/
signalistener.cc
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
// Copyright: (C) 2016 ESIMTEK ALL RIGHTS RESERVED
// author: Yonghua Zheng
#include <stdio.h>
#include <signal.h>
#include "log.h"
#include "signalistener.h"
#include "dispatchmanager.h"
SignalListener::SignalListener()
{
signals = new SignalContainers();
}
SignalListener::~SignalListener()
{
clean();
delete signals;
}
void SignalListener::clean()
{
while (!signals->empty()) {
int s = signals->front();
signals->pop_front();
signal(s, SIG_DFL);
}
}
void SignalListener::watch(int sig)
{
SignalContainers::iterator it;
for (it = signals->begin(); it != signals->end(); it++) {
int s = *it;
if (s == sig)
return;
}
signals->push_back(sig);
signal(sig, SignalListener::signal_handler);
}
// @static
void SignalListener::signal_handler(int sig)
{
char message[64];
Log::Info("signal recevied: %d", sig);
if (sig == SIGCHLD) {
snprintf(message, sizeof(message), "ACTION=waitpid");
} else {
snprintf(message, sizeof(message), "ACTION=exit");
}
DispatchManager::Post(DispatchManager::main_id, message);
}