-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
229 lines (177 loc) · 4.96 KB
/
Copy pathmain.cpp
File metadata and controls
229 lines (177 loc) · 4.96 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
#define _POSIX_SOURCE
#include <iostream>
#include <string>
#include <boost/program_options.hpp>
#include <boost/thread/thread.hpp>
#include <boost/filesystem.hpp>
namespace po = boost::program_options;
using namespace std;
#include "application.h"
#include "logger.h"
#include "general.h"
int redirect_stream(const char* app_name, int stream)
{
std::string fn(app_name);
fn.append(".daemon.");
switch(stream)
{
case 1:
fn += "out";
break;
case 2:
fn += "err";
break;
default:
return 0;
}
std::string old_fn = fn + ".old";
if(boost::filesystem::exists(old_fn))
remove(old_fn.c_str());
if(boost::filesystem::exists(fn))
rename(fn.c_str(), old_fn.c_str());
const int flags = O_WRONLY | O_CREAT | O_APPEND;
const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
if(open(fn.c_str(), flags, mode) < 0)
{
mlogger::error("Unable to open output file %s: %d", fn, stream);
return 1;
}
if (dup(stream) < 0)
{
mlogger::error("Unable to dup output descriptor: %m", stream);
return 1;
}
return 0;
}
int as_daemon(const char* app_name, boost::asio::io_context& ioc)
{
#if ! defined(REDIRECT_OUT) && ! defined(REDIRECT_ERR)
boost::ignore_unused(app_name);
#endif
ioc.notify_fork(boost::asio::io_context::fork_prepare);
if (pid_t pid = fork())
{
if (pid > 0)
{
exit(0);
}
else
{
mlogger::error("First fork failed: %m");
return 1;
}
}
setsid();
chdir("/");
umask(0);
if (pid_t pid = fork())
{
if (pid > 0)
{
exit(0);
}
else
{
mlogger::error("Second fork failed: %m");
return 1;
}
}
close(0);
close(1);
close(2);
if (open("/dev/null", O_RDONLY) < 0)
{
mlogger::error("Unable to open /dev/null: %m");
return 1;
}
#ifdef REDIRECT_OUT
redirect_stream(app_name, 1);
#endif
#ifdef REDIRECT_ERR
redirect_stream(app_name, 2);
#endif
ioc.notify_fork(boost::asio::io_context::fork_child);
return 0;
}
int create_pid_file(const char* app_name)
{
#ifdef LOCAL
boost::ignore_unused(app_name);
return 0;
#else
FILE* fp;
int pid = getpid();
char fpid[128] = DEF_LOCK_DIR;
const char* pos = strrchr(app_name, '/');
if(NULL == pos)
strcat(fpid, app_name);
else
strcat(fpid, pos + 1);
strcat(fpid, ".pid");
fp = fopen(fpid, "w+");
if(NULL == fp)
{
return 1;
}
fprintf(fp, "%d", pid);
fclose(fp);
return 0;
#endif
}
int main(int argc, const char* argv[])
{
int ret;
app_init_t app_data;
app_data.conf_file_path = string(DEF_CONF_DIR) + "bibot.conf";
app_data.log_file_path = string(DEF_LOG_DIR) + "bibot.log";
po::options_description opt_desc{"Разрешенные опции"};
po::variables_map vm; // контейнер для сохранения выбранных опций программы
boost::asio::io_context ioc{THREADS_NUM};
boost::thread_group threadgroup;
asio::signal_set signals(ioc, SIGINT, SIGTERM);
signals.async_wait([&](boost::system::error_code, int)
{
ioc.stop();
});
opt_desc.add_options()
("help", "вызов справки")
("daemon,d", "запуск в режиме демона")
("log,l", po::value<fs::path>(&app_data.log_file_path)->composing(), "log файл")
("conf,c", po::value<fs::path>(&app_data.conf_file_path)->composing(), "файл конфигурации");
po::store(po::parse_command_line(argc, argv, opt_desc), vm); // парсер переданных аргументов
po::notify(vm); // записываем аргументы в переменные
if (vm.count("help"))
{
//выводим описание меню
std::cout << opt_desc << std::endl;
}
if (vm.count("daemon"))
{
ret = as_daemon(argv[0], ioc);
if(0 != ret)
return ret;
ret = create_pid_file(argv[0]);
}
if (vm.count("log"))
app_data.log_file_path = vm["log"].as<fs::path>();
if (vm.count("conf"))
app_data.conf_file_path = vm["conf"].as<fs::path>();
mlogger::init();
mlogger::init_common_log(app_data.log_file_path.c_str());
mlogger::info("Bot backend запущен..");
Application* app = new Application(app_data, ioc);
ret = app->init();
if(0 == ret)
{
for(auto i = THREADS_NUM - 1; i > 0; --i)
{
threadgroup.create_thread(boost::bind(&asio::io_context::run, &ioc));
}
ioc.run();
threadgroup.join_all();
}
delete app;
mlogger::info("Bot backend остановлен..");
mlogger::deinit();
return ret;
}