-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
261 lines (232 loc) · 5.01 KB
/
main.cpp
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
/**
***All Rights Reserved
*
*author frankiezhu
*date 2013-03-22
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sstream>
#include "log.h"
#include "config.h"
#include "server.h"
#define SVR_LOCK_FILE "/var/run/simplesvr.pid"
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#define SVR_CONF_FILE "/etc/simplesvr_conf.xml"
static int daemonize()
{
return daemon(0, 0);
}
static void set_core_limit(void)
{
struct rlimit rlim, rlim_new;
if (getrlimit(RLIMIT_CORE, &rlim) == 0)
{
rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &rlim_new) != 0)
{
/* failed. try raising just to the old max */
rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max;
setrlimit(RLIMIT_CORE, &rlim_new);
}
}
}
int lockfile (int fd)
{
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_start = 0;
fl.l_whence = SEEK_SET;
fl.l_len = 0;
return fcntl(fd, F_SETLK, &fl);
}
static int already_running(const char *lock_file_name)
{
int fd;
char buf[32];
fd = open(lock_file_name, O_RDWR | O_CREAT, LOCKMODE);
if (fd < 0)
{
logerr("failed to open:%s\n", lock_file_name);
return -1;
}
if (lockfile(fd) < 0)
{
logerr("proc is already running\n");
close(fd);
return -1;
}
ftruncate(fd, 0);
sprintf(buf, "%ld", (long)getpid());
write(fd, buf, strlen(buf) + 1);
return 0;
}
static void sig_hup(int signo)
{
int ret;
ret = ::glb_config()->read_config(SVR_CONF_FILE);
if (ret)
{
logerr("reload config:%s failed.\n", SVR_CONF_FILE);
}
else
{
logerr("reload config:%s success.\n", SVR_CONF_FILE);
}
return;
}
static void sig_child(int signo)
{
pid_t pid = waitpid(-1, NULL, WNOHANG);
if (pid > 0)
{
logerr("SIGCHLD, pid:%d\n", pid);
}
return;
}
static int setup_signal()
{
struct sigaction sa;
int sig_ignore_array[] = {SIGQUIT, SIGPIPE, SIGINT};
unsigned int i;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = &sig_child;
if (sigaction(SIGCHLD, &sa, NULL) < 0)
{
return -1;
}
sa.sa_handler = &sig_hup;
if (sigaction(SIGHUP, &sa, NULL) < 0)
{
return -1;
}
sa.sa_handler = SIG_IGN;
for (i = 0; i < sizeof(sig_ignore_array) / sizeof(int); i++)
{
if (sigaction(sig_ignore_array[i], &sa, NULL) < 0)
{
return -1;
}
}
return 0;
}
static void print_usage_and_exit(void)
{
fprintf(stderr, "usage: -f no daemon -h print this\n");
exit(-1);
return ;
}
static int run_main()
{
int ret;
ret = ::glb_config()->read_config(SVR_CONF_FILE);
if (ret < 0)
{
logerr("read server config file:%s failed!\n", SVR_CONF_FILE);
return -1;
}
Server *svr = new(std::nothrow) Server(::glb_config()->get_listen_port(),::glb_config()->get_lifetime());
if (!svr)
{
logerr("alloc svr failed!\n");
return -1;
}
svr->set_worker_num(::glb_config()->get_worker_num());
ret = svr->init_server();
if (ret < 0)
{
logerr("init_server failed!\n");
delete svr;
return -1;
}
svr->run();
return 0;
}
static int mkdirs()
{
int count = sizeof(g_msg_file_name) / sizeof(const char *);
if (count != MSG_MAX)
{
logerr("MSG not fit file_name array!%d->%d\n", MSG_MAX, count);
return -1;
}
stringstream ss;
ss<<"mkdir -p "<<FILE_MSG_DIR<<" && cd "<<FILE_MSG_DIR <<" && mkdir -p ";
for (int i = 0; i < MSG_MAX; i++)
{
ss<<g_msg_file_name[i]<<" ";
}
int rc = system(ss.str().c_str());
if (rc != 0)
{
logerr("system cmd:%s failed!\n", ss.str().c_str());
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
int ret;
int opt;
int be_daemon = 0;
while ((opt = getopt(argc, argv, "dh:")) != -1)
{
switch (opt)
{
case 'd':
be_daemon = 1;
break;
case 'h':
print_usage_and_exit();
break;
default:
print_usage_and_exit();
break;
}
}
set_core_limit();
if (be_daemon)
{
ret = daemonize();
if (ret < 0)
{
logerr("daemonize failed!\n");
return -1;
}
}
openlog("Server2", LOG_ODELAY, LOG_USER);
ret = already_running(SVR_LOCK_FILE);
if (ret != 0)
{
return -1;
}
if (be_daemon)
{
ret = setup_signal();
if (ret < 0)
{
logerr("setup signal failed\n");
return -1;
}
}
ret = mkdirs();
if (ret < 0)
{
return -1;
}
ret = run_main();
if (ret < 0)
{
logerr("run main ret error!\n");
return -1;
}
return 0;
}