-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathfmqmain.cpp
More file actions
132 lines (112 loc) · 3.04 KB
/
fmqmain.cpp
File metadata and controls
132 lines (112 loc) · 3.04 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
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#include <iostream>
#include <signal.h>
#include <memory>
#include <string.h>
#include <openssl/ssl.h>
#include <openssl/opensslconf.h>
#include "mainapp.h"
#include "utils.h"
#include "exceptions.h"
std::weak_ptr<MainApp> globalMainApp;
void signal_handler(int signal)
{
std::shared_ptr<MainApp> locked = globalMainApp.lock();
if (!locked)
return;
if (signal == SIGPIPE)
{
return;
}
if (signal == SIGHUP)
{
locked->queueConfigReload();
}
else if (signal == SIGUSR1)
{
locked->queueReopenLogFile();
}
else if (signal == SIGUSR2)
{
locked->queueMemoryTrim();
}
else if (signal == SIGTERM || signal == SIGINT)
{
locked->queueQuit();
}
}
int register_signal_handers()
{
struct sigaction sa;
memset(&sa, 0, sizeof (struct sigaction));
sa.sa_handler = &signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
for (int signal : {SIGHUP, SIGTERM, SIGINT, SIGUSR1, SIGUSR2})
{
if (sigaction(signal, &sa, nullptr) != 0)
{
Logger *logger = Logger::getInstance();
logger->logf(LOG_ERR, "Error registering signal handlers");
return -1;
}
}
sigset_t set;
sigemptyset(&set);
sigaddset(&set,SIGPIPE);
int r;
if ((r = sigprocmask(SIG_BLOCK, &set, NULL) != 0))
{
return r;
}
return 0;
}
int fmqmain(int argc, char *argv[])
{
#ifndef OPENSSL_THREADS
std::cerr << "Error: FlashMQ was compiled with an OpenSSL without thread support." << std::endl;
exit(66);
#endif
Logger *logger = nullptr;
try
{
logger = Logger::getInstance();
std::shared_ptr<MainApp> mainapp = MainApp::initMainApp(argc, argv);
globalMainApp = mainapp;
check<std::runtime_error>(register_signal_handers());
std::string sse = "without SSE support";
#ifdef __SSE4_2__
sse = "with SSE4.2 support";
#endif
#ifdef NDEBUG
logger->logf(LOG_NOTICE, "Starting FlashMQ version %s, release build %s.", FLASHMQ_VERSION, sse.c_str());
#else
logger->logf(LOG_NOTICE, "Starting FlashMQ version %s, debug build %s.", FLASHMQ_VERSION, sse.c_str());
#endif
mainapp->start();
logger->quit();
}
catch (ConfigFileException &ex)
{
if (logger)
logger->quit();
// Not using the logger here, because we may have had all sorts of init errors while setting it up.
std::cerr << ex.what() << std::endl;
return 99;
}
catch (std::exception &ex)
{
if (logger)
logger->quit();
// Not using the logger here, because we may have had all sorts of init errors while setting it up.
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}