forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainapp.cpp
1269 lines (1047 loc) · 40.3 KB
/
mainapp.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 "mainapp.h"
#include <cassert>
#include "exceptions.h"
#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <arpa/inet.h>
#include <memory>
#include <malloc.h>
#include <netinet/tcp.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "logger.h"
#include "threadglobals.h"
#include "threadloop.h"
#include "plugin.h"
#include "threadglobals.h"
#include "globalstats.h"
#include "utils.h"
#include "bridgeconfig.h"
#include "bridgeinfodb.h"
MainApp *MainApp::instance = nullptr;
MainApp::MainApp(const std::string &configFilePath) :
subscriptionStore(std::make_shared<SubscriptionStore>())
{
epollFdAccept = check<std::runtime_error>(epoll_create(999));
taskEventFd = eventfd(0, EFD_NONBLOCK);
confFileParser = std::make_unique<ConfigFileParser>(configFilePath);
loadConfig(false);
this->num_threads = get_nprocs();
if (settings.threadCount > 0)
{
this->num_threads = settings.threadCount;
logger->logf(LOG_NOTICE, "%d threads specified by 'thread_count'.", num_threads);
}
else
{
logger->logf(LOG_NOTICE, "%d CPUs are detected, making as many threads. Use 'thread_count' setting to override.", num_threads);
}
if (num_threads <= 0)
throw std::runtime_error("Invalid number of CPUs: " + std::to_string(num_threads));
{
auto f = std::bind(&MainApp::queueCleanup, this);
//const uint64_t derrivedSessionCheckInterval = std::max<uint64_t>((settings.expireSessionsAfterSeconds)*1000*2, 600000);
//const uint64_t sessionCheckInterval = std::min<uint64_t>(derrivedSessionCheckInterval, 86400000);
uint64_t interval = 10000;
#ifdef TESTING
interval = 1000;
#endif
timer.addCallback(f, interval, "session expiration");
}
{
uint64_t interval = 1846849; // prime
auto f = std::bind(&MainApp::queuePurgeSubscriptionTree, this);
timer.addCallback(f, interval, "Rebuild subscription tree");
}
{
uint64_t interval = 3949193; // prime
#ifdef TESTING
interval = 500;
#endif
auto f = std::bind(&MainApp::queueRetainedMessageExpiration, this);
timer.addCallback(f, interval, "Purge expired retained messages.");
}
auto fKeepAlive = std::bind(&MainApp::queueKeepAliveCheckAtAllThreads, this);
timer.addCallback(fKeepAlive, 5000, "keep-alive check");
auto fPasswordFileReload = std::bind(&MainApp::queuePasswordFileReloadAllThreads, this);
timer.addCallback(fPasswordFileReload, 2000, "Password file reload.");
auto fPublishStats = std::bind(&MainApp::queuePublishStatsOnDollarTopic, this);
timer.addCallback(fPublishStats, 10000, "Publish stats on $SYS");
if (settings.pluginTimerPeriod > 0)
{
auto fpluginPeriodicEvent = std::bind(&MainApp::queuepluginPeriodicEventAllThreads, this);
timer.addCallback(fpluginPeriodicEvent, settings.pluginTimerPeriod*1000, "Auth plugin periodic event.");
}
if (!settings.storageDir.empty())
{
const std::string retainedDbPath = settings.getRetainedMessagesDBFile();
if (settings.retainedMessagesMode == RetainedMessagesMode::Enabled)
subscriptionStore->loadRetainedMessages(settings.getRetainedMessagesDBFile());
else
logger->logf(LOG_INFO, "Not loading '%s', because 'retained_messages_mode' is not 'enabled'.", retainedDbPath.c_str());
subscriptionStore->loadSessionsAndSubscriptions(settings.getSessionsDBFile());
}
auto fSaveState = std::bind(&MainApp::queueSaveStateInThread, this);
timer.addCallback(fSaveState, 900000, "Save state.");
auto fSendPendingWills = std::bind(&MainApp::queueSendQueuedWills, this);
timer.addCallback(fSendPendingWills, 2000, "Publish pending wills.");
auto fInternalHeartbeat = std::bind(&MainApp::queueInternalHeartbeat, this);
timer.addCallback(fInternalHeartbeat, HEARTBEAT_INTERVAL, "Internal heartbeat.");
}
MainApp::~MainApp()
{
if (taskEventFd >= 0)
close(taskEventFd);
if (epollFdAccept >= 0)
close(epollFdAccept);
}
void MainApp::doHelp(const char *arg)
{
puts("FlashMQ - the scalable light-weight MQTT broker");
puts("");
printf("Usage: %s [options]\n", arg);
puts("");
puts(" -h, --help Print help");
puts(" -c, --config-file <flashmq.conf> Configuration file. Default '/etc/flashmq/flashmq.conf'.");
puts(" -t, --test-config Test configuration file.");
#ifndef NDEBUG
puts(" -z, --fuzz-file <inputdata.dat> For fuzzing, provides the bytes that would be sent by a client.");
puts(" If the name contains 'web' it will activate websocket mode.");
puts(" If the name also contains 'upgrade', it will assume the websocket");
puts(" client is upgrade, and bypass the cryptograhically secured websocket");
puts(" handshake.");
#endif
puts(" -V, --version Show version");
puts(" -l, --license Show license");
}
void MainApp::showLicense()
{
std::string sse = "without SSE support";
#ifdef __SSE4_2__
sse = "with SSE4.2 support";
#endif
printf("FlashMQ Version %s %s\n", FLASHMQ_VERSION, sse.c_str());
puts("Copyright (C) 2021-2024 Wiebe Cazemier.");
puts("License OSL3: Open Software License 3.0 <https://opensource.org/license/osl-3-0-php/>.");
puts("");
puts("Author: Wiebe Cazemier <[email protected]>");
}
std::list<ScopedSocket> MainApp::createListenSocket(const std::shared_ptr<Listener> &listener)
{
std::list<ScopedSocket> result;
if (listener->port <= 0)
return result;
bool error = false;
for (ListenerProtocol p : std::list<ListenerProtocol>({ ListenerProtocol::IPv4, ListenerProtocol::IPv6}))
{
std::string pname = p == ListenerProtocol::IPv4 ? "IPv4" : "IPv6";
int family = p == ListenerProtocol::IPv4 ? AF_INET : AF_INET6;
if (!(listener->protocol == ListenerProtocol::IPv46 || listener->protocol == p))
continue;
try
{
std::string haproxy = "";
if (listener->isHaProxy())
haproxy = "haproxy ";
logger->logf(LOG_NOTICE, "Creating %s %s %slistener on [%s]:%d", pname.c_str(), listener->getProtocolName().c_str(),
haproxy.c_str(), listener->getBindAddress(p).c_str(), listener->port);
BindAddr bindAddr = getBindAddr(family, listener->getBindAddress(p), listener->port);
ScopedSocket uniqueListenFd(check<std::runtime_error>(socket(family, SOCK_STREAM, 0)), listener);
// Not needed for now. Maybe I will make multiple accept threads later, with SO_REUSEPORT.
int optval = 1;
check<std::runtime_error>(setsockopt(uniqueListenFd.get(), SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &optval, sizeof(optval)));
if (listener->isTcpNoDelay())
{
int tcp_nodelay_optval = 1;
check<std::runtime_error>(setsockopt(uniqueListenFd.get(), IPPROTO_TCP, TCP_NODELAY, &tcp_nodelay_optval, sizeof(tcp_nodelay_optval)));
}
int flags = fcntl(uniqueListenFd.get(), F_GETFL);
check<std::runtime_error>(fcntl(uniqueListenFd.get(), F_SETFL, flags | O_NONBLOCK ));
check<std::runtime_error>(bind(uniqueListenFd.get(), bindAddr.p.get(), bindAddr.len));
check<std::runtime_error>(listen(uniqueListenFd.get(), 32768));
struct epoll_event ev;
memset(&ev, 0, sizeof (struct epoll_event));
ev.data.fd = uniqueListenFd.get();
ev.events = EPOLLIN;
check<std::runtime_error>(epoll_ctl(this->epollFdAccept, EPOLL_CTL_ADD, uniqueListenFd.get(), &ev));
result.push_back(std::move(uniqueListenFd));
}
catch (std::exception &ex)
{
logger->logf(LOG_ERR, "Creating %s %s listener on [%s]:%d failed: %s", pname.c_str(), listener->getProtocolName().c_str(),
listener->getBindAddress(p).c_str(), listener->port, ex.what());
error = true;
}
}
if (error)
return std::list<ScopedSocket>();
return result;
}
void MainApp::wakeUpThread()
{
uint64_t one = 1;
check<std::runtime_error>(write(taskEventFd, &one, sizeof(uint64_t)));
}
void MainApp::queueKeepAliveCheckAtAllThreads()
{
for (std::shared_ptr<ThreadData> &thread : threads)
{
thread->queueDoKeepAliveCheck();
}
}
void MainApp::queuePasswordFileReloadAllThreads()
{
for (std::shared_ptr<ThreadData> &thread : threads)
{
thread->queuePasswdFileReload();
}
}
void MainApp::queuepluginPeriodicEventAllThreads()
{
for (std::shared_ptr<ThreadData> &thread : threads)
{
thread->queuepluginPeriodicEvent();
}
}
void MainApp::setFuzzFile(const std::string &fuzzFilePath)
{
this->fuzzFilePath = fuzzFilePath;
}
/**
* @brief MainApp::queuePublishStatsOnDollarTopic publishes the dollar topics, on a thread that has thread local authentication.
*/
void MainApp::queuePublishStatsOnDollarTopic()
{
std::lock_guard<std::mutex> locker(eventMutex);
if (!threads.empty())
{
auto f = std::bind(&ThreadData::queuePublishStatsOnDollarTopic, threads.front().get(), threads);
taskQueue.push_back(f);
wakeUpThread();
}
}
/**
* @brief MainApp::saveStateInThread starts a thread for disk IO, because file IO is not async.
*/
void MainApp::saveStateInThread()
{
std::list<BridgeInfoForSerializing> bridgeInfos = BridgeInfoForSerializing::getBridgeInfosForSerializing(this->bridgeConfigs);
auto f = std::bind(&MainApp::saveState, this->settings, bridgeInfos, true);
this->bgWorker.addTask(f);
}
/**
* @brief MainApp::queueSaveStateInThread is a wrapper to be called from another thread, to make sure saveStateInThread() is called
* on the main loop.
*/
void MainApp::queueSaveStateInThread()
{
std::lock_guard<std::mutex> locker(eventMutex);
auto f = std::bind(&MainApp::saveStateInThread, this);
taskQueue.push_back(f);
wakeUpThread();
}
void MainApp::queueSendQueuedWills()
{
if (!threads.empty())
{
int threadnr = rand() % threads.size();
std::shared_ptr<ThreadData> t = threads[threadnr];
t->queueSendingQueuedWills();
}
}
void MainApp::waitForWillsQueued()
{
int i = 0;
while(std::any_of(threads.begin(), threads.end(), [](std::shared_ptr<ThreadData> t){ return !t->allWillsQueued && t->running; }) && i++ < 5000)
{
usleep(1000);
}
}
void MainApp::waitForDisconnectsInitiated()
{
int i = 0;
while(std::any_of(threads.begin(), threads.end(), [](std::shared_ptr<ThreadData> t){ return !t->allDisconnectsSent && t->running; }) && i++ < 5000)
{
usleep(1000);
}
}
void MainApp::queueRetainedMessageExpiration()
{
if (!threads.empty())
{
int threadnr = rand() % threads.size();
std::shared_ptr<ThreadData> t = threads[threadnr];
t->queueRemoveExpiredRetainedMessages();
}
}
void MainApp::sendBridgesToThreads()
{
if (threads.empty())
return;
int i = 0;
auto bridge_pos = this->bridgeConfigs.begin();
while (bridge_pos != this->bridgeConfigs.end())
{
auto cur = bridge_pos;
bridge_pos++;
std::shared_ptr<BridgeConfig> bridge = cur->second;
if (!bridge)
continue;
std::shared_ptr<ThreadData> owner = bridge->owner.lock();
if (!owner)
{
owner = threads.at(i++ % threads.size());
bridge->owner = owner;
}
if (bridge->queueForDelete)
{
owner->removeBridgeQueued(bridge, "Bridge disappeared from config");
this->bridgeConfigs.erase(cur);
}
else
{
std::shared_ptr<BridgeState> bridgeState = std::make_shared<BridgeState>(*bridge);
bridgeState->threadData = owner;
owner->giveBridge(bridgeState);
}
}
}
void MainApp::queueSendBridgesToThreads()
{
{
std::lock_guard<std::mutex> locker(eventMutex);
auto f = std::bind(&MainApp::sendBridgesToThreads, this);
taskQueue.push_back(f);
}
wakeUpThread();
}
void MainApp::queueBridgeReconnectAllThreads(bool alsoQueueNexts)
{
try
{
for (std::shared_ptr<ThreadData> &thread : threads)
{
thread->queueBridgeReconnect();
}
}
catch (std::exception &ex)
{
Logger *logger = Logger::getInstance();
logger->logf(LOG_ERR, ex.what());
}
if (alsoQueueNexts)
{
auto fReconnectBridges = std::bind(&MainApp::queueBridgeReconnectAllThreads, this, false);
timer.addCallback(fReconnectBridges, 5000, "Reconnect bridges.");
}
}
void MainApp::queueInternalHeartbeat()
{
if (threads.empty())
return;
auto set_drift = [this](std::chrono::time_point<std::chrono::steady_clock> queue_time) {
const std::chrono::milliseconds main_loop_drift = drift.getDrift();
if (main_loop_drift > settings.maxEventLoopDrift)
{
Logger::getInstance()->log(LOG_WARNING) << "Main loop thread drift is " << main_loop_drift.count() << " ms.";
}
if (this->medianThreadDrift > settings.maxEventLoopDrift)
{
Logger::getInstance()->log(LOG_WARNING) << "Median thread drift is " << this->medianThreadDrift.count() << " ms.";
}
drift.update(queue_time);
std::vector<std::chrono::milliseconds> drifts(threads.size());
std::transform(threads.begin(), threads.end(), drifts.begin(), [] (const std::shared_ptr<const ThreadData> &t) {
return t->driftCounter.getDrift();
});
const size_t n = drifts.size() / 2;
std::nth_element(drifts.begin(), drifts.begin() + n, drifts.end());
this->medianThreadDrift = drifts.at(n);
};
{
auto call_set_drift = std::bind(set_drift, std::chrono::steady_clock::now());
std::lock_guard<std::mutex> locker(eventMutex);
taskQueue.push_back(call_set_drift);
}
wakeUpThread();
for (std::shared_ptr<ThreadData> &thread : threads)
{
thread->queueInternalHeartbeat();
}
}
/**
* @brief MainApp::saveState saves sessions and such to files. It's run in the main thread, but also dedicated threads. For that,
* reason, it's a static method to reduce the risk of accidental use of data without locks.
* @param settings A local settings, copied from a std::bind copy when running in a thread, because of thread safety.
* @param bridgeInfos is a list of objects already prepared from the original bridge configs, to avoid concurrent access.
*/
void MainApp::saveState(const Settings &settings, const std::list<BridgeInfoForSerializing> &bridgeInfos, bool sleep_after_limit)
{
Logger *logger = Logger::getInstance();
try
{
if (!settings.storageDir.empty())
{
std::shared_ptr<SubscriptionStore> subscriptionStore = MainApp::getMainApp()->getSubscriptionStore();
const std::string retainedDBPath = settings.getRetainedMessagesDBFile();
if (settings.retainedMessagesMode == RetainedMessagesMode::Enabled)
subscriptionStore->saveRetainedMessages(retainedDBPath, sleep_after_limit);
else
logger->logf(LOG_INFO, "Not saving '%s', because 'retained_messages_mode' is not 'enabled'.", retainedDBPath.c_str());
const std::string sessionsDBPath = settings.getSessionsDBFile();
subscriptionStore->saveSessionsAndSubscriptions(sessionsDBPath);
MainApp::saveBridgeInfo(settings.getBridgeNamesDBFile(), bridgeInfos);
logger->logf(LOG_NOTICE, "Saving states done");
}
}
catch(std::exception &ex)
{
logger->logf(LOG_ERR, "Error saving state: %s", ex.what());
}
}
void MainApp::saveBridgeInfo(const std::string &filePath, const std::list<BridgeInfoForSerializing> &bridgeInfos)
{
Logger *logger = Logger::getInstance();
logger->logf(LOG_NOTICE, "Saving bridge info in '%s'", filePath.c_str());
BridgeInfoDb bridgeInfoDb(filePath);
bridgeInfoDb.openWrite();
bridgeInfoDb.saveInfo(bridgeInfos);
}
std::list<std::shared_ptr<BridgeConfig>> MainApp::loadBridgeInfo(Settings &settings)
{
Logger *logger = Logger::getInstance();
std::list<std::shared_ptr<BridgeConfig>> bridges = settings.stealBridges();
if (settings.storageDir.empty())
return bridges;
const std::string filePath = settings.getBridgeNamesDBFile();
try
{
logger->logf(LOG_NOTICE, "Loading '%s'", filePath.c_str());
BridgeInfoDb dbfile(filePath);
dbfile.openRead();
std::list<BridgeInfoForSerializing> bridgeInfos = dbfile.readInfo();
for(const BridgeInfoForSerializing &info : bridgeInfos)
{
for(std::shared_ptr<BridgeConfig> &bridgeConfig : bridges)
{
if (!bridgeConfig->useSavedClientId)
continue;
if (bridgeConfig->clientidPrefix == info.prefix)
{
logger->log(LOG_INFO) << "Assigning stored bridge clientid '" << info.clientId << "' to bridge '" << info.prefix << "'.";
bridgeConfig->setClientId(info.prefix, info.clientId);
break;
}
}
}
}
catch (PersistenceFileCantBeOpened &ex)
{
logger->logf(LOG_WARNING, "File '%s' is not there (yet)", filePath.c_str());
}
return bridges;
}
void MainApp::initMainApp(int argc, char *argv[])
{
if (instance != nullptr)
throw std::runtime_error("App was already initialized.");
static struct option long_options[] =
{
{"help", no_argument, nullptr, 'h'},
{"config-file", required_argument, nullptr, 'c'},
{"test-config", no_argument, nullptr, 't'},
{"fuzz-file", required_argument, nullptr, 'z'},
{"version", no_argument, nullptr, 'V'},
{"license", no_argument, nullptr, 'l'},
{nullptr, 0, nullptr, 0}
};
#ifdef TESTING
const std::string defaultConfigFile = "/dummy/flashmq.org";
#else
const std::string defaultConfigFile = "/etc/flashmq/flashmq.conf";
#endif
std::string configFile;
if (access(defaultConfigFile.c_str(), R_OK) == 0)
{
configFile = defaultConfigFile;
}
std::string fuzzFile;
int option_index = 0;
int opt;
bool testConfig = false;
optind = 1; // allow repeated calls to getopt_long.
while((opt = getopt_long(argc, argv, "hc:Vltz:", long_options, &option_index)) != -1)
{
switch(opt)
{
case 'c':
configFile = optarg;
break;
case 'l':
MainApp::showLicense();
exit(0);
case 'V':
MainApp::showLicense();
exit(0);
case 'z':
fuzzFile = optarg;
break;
case 'h':
MainApp::doHelp(argv[0]);
exit(16);
case 't':
testConfig = true;
break;
case '?':
MainApp::doHelp(argv[0]);
exit(16);
}
}
if (testConfig)
{
try
{
if (configFile.empty())
{
std::cerr << "No config specified (with -c) and the default " << defaultConfigFile << " not found." << std::endl << std::endl;
MainApp::doHelp(argv[0]);
exit(1);
}
ConfigFileParser c(configFile);
c.loadFile(true);
printf("Config '%s' OK\n", configFile.c_str());
exit(0);
}
catch (ConfigFileException &ex)
{
std::cerr << ex.what() << std::endl;
exit(1);
}
}
instance = new MainApp(configFile);
instance->setFuzzFile(fuzzFile);
}
MainApp *MainApp::getMainApp()
{
if (!instance)
throw std::runtime_error("You haven't initialized the app yet.");
return instance;
}
void MainApp::start()
{
#ifndef NDEBUG
#ifndef TESTING
if (!getFuzzMode())
{
oneInstanceLock.lock();
}
#endif
#endif
#ifdef NDEBUG
logger->noLongerLogToStd();
#endif
struct epoll_event ev;
memset(&ev, 0, sizeof (struct epoll_event));
ev.data.fd = taskEventFd;
ev.events = EPOLLIN;
check<std::runtime_error>(epoll_ctl(this->epollFdAccept, EPOLL_CTL_ADD, taskEventFd, &ev));
#ifndef NDEBUG
// I fuzzed using afl-fuzz. You need to compile it with their compiler.
if (getFuzzMode())
{
// No threads for execution stability/determinism.
num_threads = 0;
settings.allowAnonymous = true;
int fd = open(fuzzFilePath.c_str(), O_RDONLY);
assert(fd > 0);
int fdnull = open("/dev/null", O_RDWR);
assert(fdnull > 0);
int fdnull2 = open("/dev/null", O_RDWR);
assert(fdnull2 > 0);
// TODO: matching for filename patterns doesn't work, because AFL fuzz changes the name.
const std::string fuzzFilePathLower = str_tolower(fuzzFilePath);
bool fuzzWebsockets = strContains(fuzzFilePathLower, "web");
try
{
const std::string empty;
Authentication auth(settings);
ThreadGlobals::assign(&auth);
std::vector<MqttPacket> packetQueueIn;
std::vector<std::string> subtopics;
PluginLoader pluginLoader;
std::shared_ptr<ThreadData> threaddata = std::make_shared<ThreadData>(0, settings, pluginLoader);
ThreadGlobals::assignThreadData(threaddata.get());
std::shared_ptr<Client> client = std::make_shared<Client>(fd, threaddata, nullptr, fuzzWebsockets, false, nullptr, settings, true);
std::shared_ptr<Client> subscriber = std::make_shared<Client>(fdnull, threaddata, nullptr, fuzzWebsockets, false, nullptr, settings, true);
subscriber->setClientProperties(ProtocolVersion::Mqtt311, "subscriber", "subuser", true, 60);
subscriber->setAuthenticated(true);
std::shared_ptr<Client> websocketsubscriber = std::make_shared<Client>(fdnull2, threaddata, nullptr, true, false, nullptr, settings, true);
websocketsubscriber->setClientProperties(ProtocolVersion::Mqtt311, "websocketsubscriber", "websocksubuser", true, 60);
websocketsubscriber->setAuthenticated(true);
websocketsubscriber->setFakeUpgraded();
subscriptionStore->registerClientAndKickExistingOne(websocketsubscriber);
subtopics = splitTopic("#");
subscriptionStore->addSubscription(websocketsubscriber, subtopics, 0, false, false, empty, AuthResult::success);
subscriptionStore->registerClientAndKickExistingOne(subscriber);
subscriptionStore->addSubscription(subscriber, subtopics, 0, false, false, empty, AuthResult::success);
if (fuzzWebsockets && strContains(fuzzFilePathLower, "upgrade"))
{
client->setFakeUpgraded();
subscriber->setFakeUpgraded();
}
{
VectorClearGuard vectorClearGuard(packetQueueIn);
client->readFdIntoBuffer();
client->bufferToMqttPackets(packetQueueIn, client);
for (MqttPacket &packet : packetQueueIn)
{
packet.handle();
}
subscriber->writeBufIntoFd();
websocketsubscriber->writeBufIntoFd();
}
}
catch (ProtocolError &ex)
{
logger->logf(LOG_ERR, "Expected MqttPacket handling error: %s", ex.what());
}
running = false;
}
#endif
GlobalStats *globalStats = GlobalStats::getInstance();
PluginLoader pluginLoader;
pluginLoader.loadPlugin(settings.pluginPath);
std::unordered_map<std::string, std::string> &authOpts = settings.getFlashmqpluginOpts();
pluginLoader.mainInit(authOpts);
for (int i = 0; i < num_threads; i++)
{
std::shared_ptr<ThreadData> t = std::make_shared<ThreadData>(i, settings, pluginLoader);
t->start(&do_thread_work);
threads.push_back(t);
}
// Populate the $SYS topics, otherwise you have to wait until the timer expires.
if (!threads.empty())
threads.front()->queuePublishStatsOnDollarTopic(threads);
timer.start();
sendBridgesToThreads();
queueBridgeReconnectAllThreads(true);
uint next_thread_index = 0;
this->bgWorker.start();
struct epoll_event events[MAX_EVENTS];
memset(&events, 0, sizeof (struct epoll_event)*MAX_EVENTS);
started = true;
while (running)
{
int num_fds = epoll_wait(this->epollFdAccept, events, MAX_EVENTS, 100);
if (num_fds < 0)
{
if (errno == EINTR)
continue;
logger->logf(LOG_ERR, "Waiting for listening socket error: %s", strerror(errno));
}
for (int i = 0; i < num_fds; i++)
{
int cur_fd = events[i].data.fd;
try
{
if (cur_fd != taskEventFd)
{
std::shared_ptr<Listener> listener = activeListenSockets[cur_fd].getListener();
if (!listener)
continue;
std::shared_ptr<ThreadData> thread_data = threads[next_thread_index++ % num_threads];
logger->logf(LOG_DEBUG, "Accepting connection on thread %d on %s", thread_data->threadnr, listener->getProtocolName().c_str());
struct sockaddr_in6 addrBiggest;
struct sockaddr *addr = reinterpret_cast<sockaddr*>(&addrBiggest);
socklen_t len = sizeof(struct sockaddr_in6);
memset(addr, 0, len);
int fd = check<std::runtime_error>(accept(cur_fd, addr, &len));
/*
* I decided to not use a delayed close mechanism. It has been observed that under overload and clients in a reconnect loop,
* you can collect open files up to (a) million(s). By accepting and closing, the hope is we can keep clients at bay from
* the thread loops well enough.
*/
if (this->medianThreadDrift > settings.maxEventLoopDrift || this->drift.getDrift() > settings.maxEventLoopDrift)
{
const std::string addr_s = sockaddrToString(addr);
bool do_close = false;
if (settings.overloadMode == OverloadMode::CloseNewClients)
{
if (overloadLogCounter <= OVERLOAD_LOGS_MUTE_AFTER_LINES)
{
overloadLogCounter++;
logger->log(LOG_ERROR) << "[OVERLOAD] FlashMQ seems to be overloaded while accepting new connection(s) from '"
<< addr_s << ". Closing socket. See 'overload_mode' and 'max_event_loop_drift'.";
}
do_close = true;
}
else if (settings.overloadMode == OverloadMode::Log)
{
if (overloadLogCounter <= OVERLOAD_LOGS_MUTE_AFTER_LINES)
{
overloadLogCounter++;
logger->log(LOG_WARNING) << "[OVERLOAD] FlashMQ seems to be overloaded while accepting new connection(s) from '"
<< addr_s << ". See 'overload_mode' and 'max_event_loop_drift'.";
}
}
else
{
throw std::runtime_error("Unimplemented OverloadMode");
}
if (overloadLogCounter > OVERLOAD_LOGS_MUTE_AFTER_LINES && overloadLogCounter < OVERLOAD_LOGS_MUTE_AFTER_LINES * 2)
{
overloadLogCounter = OVERLOAD_LOGS_MUTE_AFTER_LINES * 5;
logger->log(LOG_WARNING) << "[OVERLOAD] Muting overload logging until it recovers, to avoid log spam and extra load.";
}
if (do_close)
{
close(fd);
continue;
}
}
else
{
overloadLogCounter = 0;
}
SSL *clientSSL = nullptr;
if (listener->isSsl())
{
if (!listener->sslctx)
{
logger->log(LOG_ERR) << "Listener is SSL but SSL context is null. Application bug.";
close(fd);
continue;
}
clientSSL = SSL_new(listener->sslctx->get());
if (clientSSL == NULL)
{
logger->logf(LOG_ERR, "Problem creating SSL object. Closing client.");
close(fd);
continue;
}
SSL_set_fd(clientSSL, fd);
}
// Don't use std::make_shared to avoid the weak pointers keeping the control block in memory.
std::shared_ptr<Client> client = std::shared_ptr<Client>(new Client(fd, thread_data, clientSSL, listener->websocket, listener->isHaProxy(), addr, settings));
if (listener->getX509ClientVerficationMode() != X509ClientVerification::None)
{
client->setSslVerify(listener->getX509ClientVerficationMode());
}
client->setAllowAnonymousOverride(listener->allowAnonymous);
thread_data->giveClient(std::move(client));
globalStats->socketConnects.inc();
}
else
{
uint64_t eventfd_value = 0;
check<std::runtime_error>(read(cur_fd, &eventfd_value, sizeof(uint64_t)));
if (doConfigReload)
{
reloadConfig();
}
if (doLogFileReOpen)
{
reopenLogfile();
}
if (doQuitAction)
{
quit();
}
if (doMemoryTrim)
{
memoryTrim();
}
std::list<std::function<void()>> tasks;
{
std::lock_guard<std::mutex> locker(eventMutex);
tasks = std::move(taskQueue);
taskQueue.clear();
}
for(auto &f : tasks)
{
f();
}
}
}
catch (std::exception &ex)
{
logger->logf(LOG_ERR, "Problem in main thread: %s", ex.what());
}
}
}
this->bgWorker.stop();
if (settings.willsEnabled)
{
logger->logf(LOG_DEBUG, "Having all client in all threads send or queue their will.");
for(std::shared_ptr<ThreadData> &thread : threads)
{
thread->queueSendWills();
}
waitForWillsQueued();
}
logger->logf(LOG_DEBUG, "Having all client in all threads send a disconnect packet.");
for(std::shared_ptr<ThreadData> &thread : threads)
{
thread->queueSendDisconnects();
}
waitForDisconnectsInitiated();
oneInstanceLock.unlock();
logger->logf(LOG_DEBUG, "Signaling threads to finish.");
for(std::shared_ptr<ThreadData> &thread : threads)
{
thread->queueQuit();
}
logger->logf(LOG_DEBUG, "Waiting for threads to finish.");
int count = 0;
bool waitTimeExpired = false;
while(std::any_of(threads.begin(), threads.end(), [](std::shared_ptr<ThreadData> t){ return !t->finished; }))
{
if (count++ >= 10000)
{
waitTimeExpired = true;
break;
}
usleep(1000);
}
if (waitTimeExpired)
{
logger->logf(LOG_WARNING, "(Some) threads failed to terminate. Program will exit uncleanly. If you're using a plugin, it may not be thread-safe.");
}
else
{
for(std::shared_ptr<ThreadData> &thread : threads)
{
logger->logf(LOG_DEBUG, "Waiting for thread %d to join.", thread->threadnr);
thread->waitForQuit();
}
}
pluginLoader.mainDeinit(settings.getFlashmqpluginOpts());