-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.cpp
More file actions
254 lines (218 loc) · 6.08 KB
/
application.cpp
File metadata and controls
254 lines (218 loc) · 6.08 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
#include "application.h"
extern "C" {
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <memory.h>
};
#ifdef _MSC_VER
#include "winbase.h"
#endif // _MSC_VER
//////////////////////////////////////////////////////////////////////////
#include <utility>
#include "color_print.h"
#ifdef _DEBUG
#include "macro.h"
#endif // _DEBUG
using namespace std;
bool Application::_IsSigOutput = true;
Application::SignalMap_t Application::_SignalsMap;
Application::Application()
: _argc(0), _argv(NULL) {}
Application::~Application() {}
bool Application::init(int argc, char* argv[]) {
_argc = argc;
_argv = argv;
return true;
}
int Application::getpid() { return ::getpid(); }
int Application::getppid(void) { return 0; }
// for debug
#include "macro.h"
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#ifndef STDIN_FILENO
# define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
# define STDOUT_FILENO 1
#endif
#ifndef STDOUT_FILENO
# define STDERR_FILENO 2
#endif
#else
#include <unistd.h>
#endif
void Application::daemon() {
switch (fork()) {
case -1:
perror("fork()");
return;
case 0:
cout << "fork() return 0, child process(id:"<< getpid() << ") begin." << endl;
break;
default:
cout << "father process(id:" << getpid() << ") _exit." << endl;
_exit(EXIT_SUCCESS);
}
if (setsid() == -1) {
perror("setsid()");
return;
}
int fd = 0;
if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
if(dup2(fd, STDIN_FILENO) < 0) {
perror("dup2 stdin");
return;
}
if(dup2(fd, STDOUT_FILENO) < 0) {
perror("dup2 stdout");
return;
}
if(dup2(fd, STDERR_FILENO) < 0) {
perror("dup2 stderr");
return;
}
if (fd > STDERR_FILENO) {
if(close(fd) < 0) {
perror("close");
return;
}
}
}
return;
}
bool Application::reg_sig(int sig, SigHandler_t func, bool repeat /* = false */) {
#ifdef _DEBUG
cout << "in Application::reg_sig() parameters as follow:" << endl;
COUT(sig);
SEE_ADDRESS_64_PTR(func);
COUT(repeat);
#endif // _DEBUG
SignalMap_t::iterator iter = _SignalsMap.find(sig);
if (_SignalsMap.find(sig) == _SignalsMap.end()) {
#ifdef _DEBUG
cout << "first time to regisetr signal(" << sig << ")." << endl;
#endif // _DEBUG
register_signal_flag(sig, repeat);
}
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = func;
size_t actions_size = iter->second.actions.size();
size_t i = 0;
for (;i < actions_size; ++i) {
if ((iter->second.actions)[i].sa_handler == func)
break;
}
if (i == actions_size) {
iter->second.actions.push_back(act);
}
return true;
}
bool Application::reg_sig(int sig, const struct sigaction& act, struct sigaction* old_act /* = NULL */) {
if (sigaction(sig, &act, NULL) < 0) {
perror("install signal error");
return false;
}
return true;
}
long Application::gettid() {
return static_cast<long> (syscall(SYS_gettid));
}
unsigned int Application::sleep( unsigned int seconds )
{
#ifdef _MSC_VER
Sleep(seconds);
return 0;
#else
return ::sleep(seconds);
#endif // _MSC_VER
}
std::string Application::my_getline()
{
std::string result;
char buf[8];
while (1==scanf("%c", buf)) {
if (buf[0] == '\n') {
break;
}
result.push_back( buf[0] );
}
return result;
}
void Application::g_signal_handle(int sig_no) {
if (_IsSigOutput)
printf(START_BLUE "process(id=%ld): g_signal_handle(%d) is touched." END_COLOR "\n",
Application::gettid(), sig_no);
Application::SignalMap_t::iterator iter = _SignalsMap.find(sig_no);
if (iter != _SignalsMap.end()) {
size_t size = iter->second.actions.size();
for (size_t i = 0; i < size; ++i) {
struct sigaction& old_act = iter->second.actions[i];
if (SIG_DFL != old_act.sa_handler
&& SIG_ERR != old_act.sa_handler
&& SIG_IGN != old_act.sa_handler) {
if (g_signal_handle != old_act.sa_handler)
(*old_act.sa_handler)(sig_no);
}
}
iter->second.flag = true;
if (iter->second.repeat)
Application::reg_sig(sig_no, g_signal_handle, true);
}
}
void Application::register_signal_flag(int sig_no, bool repeat /* = false */) {
if (_SignalsMap.find(sig_no) != _SignalsMap.end())
return;
struct sigaction act, old_act;
memset(&act, 0, sizeof(struct sigaction));
memset(&old_act, 0, sizeof(struct sigaction));
act.sa_handler = g_signal_handle;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
if (sigaction(sig_no, &act, &old_act) < 0) {
perror("install signal (sa_handler) error");
return;
}
struct SignalInfo_t si;
si.flag = false;
si.repeat = repeat;
si.actions.push_back(old_act);
si.actions.push_back(act);
_SignalsMap.insert(make_pair(sig_no, si));
#ifdef _DEBUG
SignalMap_t::iterator iter = _SignalsMap.find(sig_no);
if (iter != _SignalsMap.end()) {
cout << START_BLUE;
COUT(sig_no);
COUT(iter->second.actions.size());
for (size_t i = 0; i < iter->second.actions.size(); ++i) {
SEE_ADDRESS_64_PTR(iter->second.actions[i].sa_handler);
}
cout << END_COLOR;
}
#endif // _DEBUG
}
bool Application::check_signal_flag(int sig_no) {
SignalMap_t::iterator iter = _SignalsMap.find(sig_no);
if (iter == _SignalsMap.end()) {
return false;
}
return iter->second.flag;
}
void Application::reset_signal_flag(int sig_no, bool value /* = false */) {
SignalMap_t::iterator iter = _SignalsMap.find(sig_no);
if (iter != _SignalsMap.end()) {
iter->second.flag = value;
}
}
void Application::set_is_output( bool b ) {
_IsSigOutput = b;
}