Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
387 lines (336 loc) · 11.4 KB
/
Copy pathmain.cpp
File metadata and controls
387 lines (336 loc) · 11.4 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "NxCache.hpp"
#include <boost/program_options.hpp>
#include <csignal>
#include <future>
#if defined(_MSC_VER)
#include "winsvc.hpp"
#include <strsafe.h>
#include <shlobj.h>
#else
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#endif
namespace bpo = boost::program_options;
static std::promise<int> exitPromise;
static std::once_flag exitFlag;
#if defined(_MSC_VER)
static SERVICE_STATUS svcStatus;
static SERVICE_STATUS_HANDLE svcStatusHandle;
void StopWinService()
{
std::call_once(exitFlag, []() { exitPromise.set_value(SIGTERM); });
}
bool IsRunningAsSystemService()
{
DWORD sessionId = 0;
ProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
return sessionId == 0;
}
void SvcReportEvent(LPTSTR szFunction)
{
HANDLE hEventSource = RegisterEventSource(NULL, SVCNAME);
if (NULL != hEventSource) {
char Buffer[80];
StringCchPrintf(Buffer, 80, "%s failed with %d", szFunction, GetLastError());
LPCSTR lpszStrings[2];
lpszStrings[0] = SVCNAME;
lpszStrings[1] = Buffer;
ReportEvent(hEventSource, // event log handle
EVENTLOG_ERROR_TYPE, // event type
0, // event category
0, // event identifier
NULL, // no security identifier
2, // size of lpszStrings array
0, // no binary data
lpszStrings, // array of strings
NULL); // no binary data
DeregisterEventSource(hEventSource);
}
}
void ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
{
static DWORD dwCheckPoint = 1;
// Report the status of the service to the SCM.
svcStatus.dwCurrentState = dwCurrentState;
svcStatus.dwWin32ExitCode = dwWin32ExitCode;
svcStatus.dwWaitHint = dwWaitHint;
svcStatus.dwControlsAccepted = dwCurrentState == SERVICE_START_PENDING || dwCurrentState == SERVICE_STOP_PENDING ? 0 : SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN;
svcStatus.dwCheckPoint = dwCurrentState == SERVICE_RUNNING || dwCurrentState == SERVICE_STOPPED ? 0 : dwCheckPoint++;
SetServiceStatus(svcStatusHandle, &svcStatus);
}
DWORD WINAPI SvcCtrlHandler(DWORD dwCtrl, DWORD, LPVOID, LPVOID)
{
switch (dwCtrl) {
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_PRESHUTDOWN:
ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
StopWinService();
[[fallthrough]];
case SERVICE_CONTROL_INTERROGATE:
return NO_ERROR;
default:
break;
}
return ERROR_CALL_NOT_IMPLEMENTED;
}
BOOL WINAPI HandlerRoutine(_In_ DWORD dwCtrlType)
{
switch (dwCtrlType) {
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
std::call_once(exitFlag, []() { exitPromise.set_value(SIGTERM); });
return TRUE;
default:
return FALSE;
}
}
#else
static const char* pid_file_path = "/run/" SVCNAME ".pid";
static void signal_handler(int signal)
{
std::call_once(exitFlag, [signal]() { exitPromise.set_value(signal); });
}
static int fork_to_background()
{
// Fork off the parent process
pid_t pid = fork();
if (pid < 0) {
return -1;
}
// If we got a good PID, then we can exit the parent process
if (pid > 0) {
return 1;
}
// Change the file mode mask and open any logs here
umask(022);
// Create a new SID for the child process
pid_t sid = setsid();
if (sid < 0) {
return -1;
}
// Change the current working directory
if ((chdir("/")) < 0) {
return -1;
}
// Redirect the standard file descriptors
int dev_null = open("/dev/null", O_RDWR);
if (dev_null != -1) {
dup2(dev_null, STDIN_FILENO);
dup2(dev_null, STDOUT_FILENO);
dup2(dev_null, STDERR_FILENO);
if (dev_null > STDERR_FILENO) {
close(dev_null);
}
}
std::ofstream pid_file(pid_file_path, std::ios::out | std::ios::trunc);
if (pid_file) {
pid_file << getpid() << std::endl;
pid_file.close();
}
// Only fail hard when running as root under systemd(service mode expects PIDFile = ...)
if (!pid_file && geteuid() == 0 && std::filesystem::exists("/run/systemd/system")) {
std::error_code ec;
std::filesystem::remove(pid_file_path, ec);
return -1;
}
return 0;
}
#endif
static int main_internal(int argc, char** argv, bool detached = false)
{
try {
std::filesystem::path cache_dir;
#if defined(_MSC_VER)
if (FAILED(SHGetFolderPath(NULL, detached ? CSIDL_COMMON_APPDATA : CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, szPath))) {
return EXIT_FAILURE;
}
cache_dir = szPath;
cache_dir /= SVCNAME;
#else
cache_dir = "/var/lib/" SVCNAME;
#endif
bpo::variables_map options;
bpo::options_description app_options(SVCDISPLAYNAME);
app_options.add_options()
("cache-dir,c", bpo::value<std::filesystem::path>()->default_value(cache_dir), "Directory containing cache files (absolute or relative to current working directory)")
("port,p", bpo::value<uint16_t>()->default_value(8080u), "Port to listen on")
("token,t", bpo::value<std::string>()->default_value(TOKEN), "Authentication token");
if (!detached) {
app_options.add_options()
#if defined(_MSC_VER)
("register-service", "Register itself as Windows service")
("unregister-service", "Unregister itself as Windows service")
#else
("detach,d", "Run in background")
#endif
("help,h", "Print this help message and exit");
try {
bpo::store(bpo::command_line_parser(argc, argv).options(app_options).run(), options);
bpo::notify(options);
}
catch (const bpo::error& e) {
std::cerr << "Error parsing command line: " << e.what() << std::endl;
return EXIT_FAILURE;
}
if (options.count("help")) {
std::cout << app_options << std::endl;
return EXIT_SUCCESS;
}
#if defined(_MSC_VER)
else if (options.count("register-service"))
{
return install_win_service();
}
else if (options.count("unregister-service"))
{
return remove_win_service();
}
#else
detached = options.count("detach");
#endif
}
if (detached) {
#if defined(_MSC_VER)
DWORD len = GetModuleFileName(NULL, szPath, MAX_PATH);
if (len == 0 || len == MAX_PATH) {
return EXIT_FAILURE;
}
std::filesystem::path config_file = szPath;
config_file = config_file.parent_path() / "config.ini";
#else
int ret = fork_to_background();
if (ret < 0) {
std::cerr << "Failed to fork to background." << std::endl;
return EXIT_FAILURE;
}
else if (ret == 1) {
return EXIT_SUCCESS;
}
std::filesystem::path config_file = "/etc/" SVCNAME;
#endif
try {
#if defined(_MSC_VER)
if (argc > 1) {
bpo::store(bpo::command_line_parser(argc, argv).options(app_options).run(), options);
}
else
#endif
if (std::filesystem::exists(config_file)) {
std::ifstream cfg_stream(config_file);
bpo::store(bpo::parse_config_file<char>(cfg_stream, app_options, true), options);
}
bpo::notify(options);
}
catch (const bpo::error& e) {
std::cerr << e.what() << std::endl;
#if !defined(_MSC_VER)
std::error_code ec;
std::filesystem::remove(pid_file_path, ec);
#endif
return EXIT_FAILURE;
}
}
cache_dir = options["cache-dir"].as<std::filesystem::path>();
if (cache_dir.is_relative())
cache_dir = std::filesystem::current_path() / cache_dir;
std::filesystem::create_directories(cache_dir);
std::string url = std::format("http://127.0.0.1:{}", options["port"].as<uint16_t>());
NxCacheService service(utility::conversions::to_string_t(url), options["token"].as<std::string>(), cache_dir);
service.open();
if (detached) {
#if defined(_MSC_VER)
ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);
#endif
}
else {
std::cout << "Started " << SVCDISPLAYNAME << " at " << url << std::endl;
}
#if !defined(_MSC_VER)
std::signal(SIGINT, signal_handler);
std::signal(SIGTERM, signal_handler);
std::signal(SIGHUP, signal_handler);
std::signal(SIGQUIT, signal_handler);
#endif
int signal = exitPromise.get_future().get();
if (detached) {
#if defined(_MSC_VER)
ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
#endif
}
else {
std::cout << "Exiting with signal " << signal << std::endl;
}
service.close();
#if !defined(_MSC_VER)
if (detached) {
std::error_code ec;
std::filesystem::remove(pid_file_path, ec);
}
#endif
return EXIT_SUCCESS;
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
catch (...) {
std::cerr << "Unknown exception caught" << std::endl;
}
#if !defined(_MSC_VER)
if (detached) {
std::error_code ec;
std::filesystem::remove(pid_file_path, ec);
}
#endif
return EXIT_FAILURE;
}
#if defined(_MSC_VER)
char cRegisterServiceCtrlHandlerEx[] = "cRegisterServiceCtrlHandlerEx";
char cStartServiceCtrlDispatcher[] = "StartServiceCtrlDispatcher";
void WINAPI ServiceMain(DWORD argc, LPTSTR* argv)
{
bool is_win_service = IsRunningAsSystemService();
if (is_win_service) {
svcStatusHandle = RegisterServiceCtrlHandlerEx(SVCNAME, SvcCtrlHandler, NULL);
if (!svcStatusHandle) {
SvcReportEvent(cRegisterServiceCtrlHandlerEx);
return;
}
// These SERVICE_STATUS members remain as set here
svcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
svcStatus.dwServiceSpecificExitCode = 0;
// Report initial status to the SCM
ReportSvcStatus(SERVICE_START_PENDING, NO_ERROR, 3000);
}
int exit_code = main_internal(static_cast<int>(argc), reinterpret_cast<char**>(argv), is_win_service);
if (is_win_service && exit_code != EXIT_SUCCESS) {
ReportSvcStatus(SERVICE_STOPPED, static_cast<DWORD>(exit_code), 0);
}
}
SERVICE_TABLE_ENTRY DispatchTable[] = {
{ (LPSTR)SVCNAME, ServiceMain },
{ NULL, NULL }
};
#endif
int main(int argc, char** argv)
{
#if defined(_MSC_VER)
bool is_win_service = IsRunningAsSystemService();
if (is_win_service) {
if (!StartServiceCtrlDispatcher(DispatchTable)) {
SvcReportEvent(cStartServiceCtrlDispatcher);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
else {
SetConsoleCtrlHandler(HandlerRoutine, TRUE);
}
return main_internal(argc, argv, is_win_service);
#else
return main_internal(argc, argv);
#endif
}