-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.cpp
181 lines (152 loc) · 4.92 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
#include "bridge/Bridge.h"
#include "concurrency/Semaphore.h"
#include "concurrency/ThreadUtils.h"
#include "config/Config.h"
#include "git_version.h"
#include "logger/Logger.h"
#include "utils/Time.h"
#include <execinfo.h>
#include <iostream>
#include <memory>
#include <signal.h>
namespace
{
std::unique_ptr<concurrency::Semaphore> running;
std::unordered_map<int32_t, struct sigaction> oldSignalHandlers;
void fatalSignalHandler(int32_t signalId)
{
if (signalId == SIGPIPE) {
logger::disableStdOut();
logger::disableStdErr();
}
void* array[16];
const auto size = backtrace(array, 16);
logger::errorImmediate("Fatal signal %d, %d stack frames.", "fatalSignalHandler", signalId, size);
logger::logStackImmediate(array, size, "fatalSignalHandler");
logger::error("Fatal signal %d, %d stack frames.", "fatalSignalHandler", signalId, size);
logger::flushLog();
auto oldSignalHandlersItr = oldSignalHandlers.find(signalId);
if (oldSignalHandlersItr != oldSignalHandlers.end())
{
if (sigaction(signalId, &oldSignalHandlersItr->second, nullptr) != 0)
{
exit(signalId);
}
}
}
void intSignalHandler(int32_t)
{
logger::info("SIGINT", "main");
assert(running);
running->post();
}
void hupSignalHandler(int32_t)
{
logger::info("SIGHUP", "main");
logger::reOpenLog();
}
} // namespace
static bool isEqualCaseInsensitive(const std::string& s1, const std::string& s2)
{
if (s1.size() != s2.size())
{
return false;
}
return std::equal(s1.begin(), s1.end(), s2.begin(), [](char a, char b) { return tolower(a) == tolower(b); });
}
static logger::Level parseLogLevel(const std::string& level)
{
if (isEqualCaseInsensitive(level, "error"))
{
return logger::Level::ERROR;
}
if (isEqualCaseInsensitive(level, "warn") || isEqualCaseInsensitive(level, "warning"))
{
return logger::Level::WARN;
}
if (isEqualCaseInsensitive(level, "debug") || isEqualCaseInsensitive(level, "dbg"))
{
return logger::Level::DBG;
}
return logger::Level::INFO;
}
int main(int argc, char** argv)
{
running = std::make_unique<concurrency::Semaphore>();
concurrency::setThreadName("main");
{
struct sigaction sigactionData = {};
sigactionData.sa_handler = intSignalHandler;
sigactionData.sa_flags = 0;
sigemptyset(&sigactionData.sa_mask);
sigaction(SIGINT, &sigactionData, nullptr);
}
{
struct sigaction sigactionData = {};
sigactionData.sa_handler = hupSignalHandler;
sigactionData.sa_flags = 0;
sigemptyset(&sigactionData.sa_mask);
sigaction(SIGUSR1, &sigactionData, nullptr);
sigaction(SIGHUP, &sigactionData, nullptr);
}
{
struct sigaction sigactionData = {};
sigactionData.sa_handler = fatalSignalHandler;
sigactionData.sa_flags = 0;
sigemptyset(&sigactionData.sa_mask);
struct sigaction oldHandler = {};
sigaction(SIGPIPE, &sigactionData, &oldHandler);
oldSignalHandlers.emplace(SIGPIPE, oldHandler);
sigaction(SIGSEGV, &sigactionData, &oldHandler);
oldSignalHandlers.emplace(SIGSEGV, oldHandler);
sigaction(SIGBUS, &sigactionData, &oldHandler);
oldSignalHandlers.emplace(SIGBUS, oldHandler);
sigaction(SIGABRT, &sigactionData, &oldHandler);
oldSignalHandlers.emplace(SIGABRT, oldHandler);
sigaction(SIGILL, &sigactionData, &oldHandler);
oldSignalHandlers.emplace(SIGILL, oldHandler);
sigaction(SIGFPE, &sigactionData, &oldHandler);
oldSignalHandlers.emplace(SIGFPE, oldHandler);
sigaction(SIGSYS, &sigactionData, &oldHandler);
oldSignalHandlers.emplace(SIGSYS, oldHandler);
}
if (argc < 2)
{
std::cout << "Symphony RTC Mixer" << std::endl;
std::cout << "Usage: smb config.json" << std::endl;
return 0;
}
const auto config = std::make_unique<config::Config>();
if (!config->readFromFile(argv[1]))
{
return 1;
}
utils::Time::initialize();
logger::setup(config->logFile.get().c_str(), config->logStdOut, config->logStdErr, parseLogLevel(config->logLevel), 4 * 1024 * 1024);
logger::info("Starting httpd on port %u", "main", config->port.get());
logger::info("Configured udp port range: %s %u - %u",
"main",
config->ip.get().c_str(),
config->ice.udpPortRangeLow.get(),
config->ice.udpPortRangeHigh.get());
logger::logAlways("log level %s", "main", config->logLevel.get().c_str());
{
bridge::Bridge environment(*config);
environment.initialize();
logger::info("Build: %s %s",
"main",
#if DEBUG
"Debug"
#else
"Release"
#endif
,
kGitHash);
if (environment.isInitialized())
{
running->wait();
}
}
logger::stop();
return 0;
}