2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
|
* Copyright (c) 2009-2015 The Bitcoin Core developers
|
2021-02-26 15:14:14 +01:00
|
|
|
* Copyright (C) 2018-2021 Tom Zander <tom@flowee.org>
|
2017-11-09 19:34:51 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2013-05-26 13:48:30 -04:00
|
|
|
|
2015-07-05 14:17:46 +02:00
|
|
|
#include "chainparams.h"
|
2014-10-28 21:33:23 -04:00
|
|
|
#include "clientversion.h"
|
2019-11-13 11:46:09 +01:00
|
|
|
#include <init.h>
|
2026-05-14 13:13:40 +02:00
|
|
|
#include <utiltime.h>
|
2019-11-13 11:46:09 +01:00
|
|
|
#include <server/main.h>
|
2013-04-13 00:13:08 -05:00
|
|
|
#include "noui.h"
|
2015-04-03 11:50:06 -04:00
|
|
|
#include "scheduler.h"
|
2019-08-24 13:20:37 +02:00
|
|
|
#include <serverutil.h>
|
2018-02-13 13:09:33 +01:00
|
|
|
#include "Application.h"
|
2021-03-16 15:14:13 +01:00
|
|
|
#include <HubApiServices.h>
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2013-05-26 13:48:30 -04:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2013-04-13 00:13:08 -05:00
|
|
|
#include <boost/filesystem.hpp>
|
2014-08-21 16:11:09 +02:00
|
|
|
#include <boost/thread.hpp>
|
2013-05-26 13:48:30 -04:00
|
|
|
|
2017-08-17 20:53:23 -06:00
|
|
|
#include <cstdio>
|
2015-01-23 07:53:17 +01:00
|
|
|
|
2013-12-20 11:48:22 +01:00
|
|
|
static bool fDaemon;
|
|
|
|
|
|
2015-01-19 15:33:55 +01:00
|
|
|
void WaitForShutdown(boost::thread_group* threadGroup)
|
2013-05-26 13:48:30 -04:00
|
|
|
{
|
2013-10-06 14:18:55 +02:00
|
|
|
bool fShutdown = ShutdownRequested();
|
2013-05-26 13:48:30 -04:00
|
|
|
// Tell the main threads to shutdown.
|
2013-10-06 14:18:55 +02:00
|
|
|
while (!fShutdown)
|
2013-05-26 13:48:30 -04:00
|
|
|
{
|
|
|
|
|
MilliSleep(200);
|
2013-10-06 14:18:55 +02:00
|
|
|
fShutdown = ShutdownRequested();
|
2013-05-26 13:48:30 -04:00
|
|
|
}
|
|
|
|
|
if (threadGroup)
|
2013-10-06 14:18:55 +02:00
|
|
|
{
|
2015-01-23 07:53:17 +01:00
|
|
|
Interrupt(*threadGroup);
|
2013-10-06 14:18:55 +02:00
|
|
|
threadGroup->join_all();
|
|
|
|
|
}
|
2013-05-26 13:48:30 -04:00
|
|
|
}
|
|
|
|
|
|
2019-05-12 11:04:04 +02:00
|
|
|
static bool SelectChain()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
SelectParams(ChainNameFromCommandLine());
|
|
|
|
|
return true;
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
fprintf(stderr, "Error: %s\n", e.what());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-05-26 13:48:30 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Start
|
|
|
|
|
//
|
|
|
|
|
bool AppInit(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
boost::thread_group threadGroup;
|
2015-04-03 11:50:06 -04:00
|
|
|
CScheduler scheduler;
|
2013-05-26 13:48:30 -04:00
|
|
|
|
|
|
|
|
bool fRet = false;
|
2014-11-22 19:56:25 +01:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Parameters
|
|
|
|
|
//
|
2018-11-21 12:26:17 +01:00
|
|
|
// If Qt is used, parameters/flowee.conf are parsed in qt/bitcoin.cpp's main()
|
2019-04-02 17:45:04 +02:00
|
|
|
Settings::Hub allowedArgs;
|
2017-01-23 01:56:55 -08:00
|
|
|
try {
|
2017-02-14 15:07:49 -08:00
|
|
|
ParseParameters(argc, argv, allowedArgs);
|
2017-01-23 01:56:55 -08:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
fprintf(stderr, "Error parsing program options: %s\n", e.what());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-11-22 19:56:25 +01:00
|
|
|
|
|
|
|
|
// Process help and version before taking care about datadir
|
2015-10-18 21:02:36 +11:00
|
|
|
if (mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help") || mapArgs.count("-version"))
|
2014-11-22 19:56:25 +01:00
|
|
|
{
|
2017-11-13 23:09:33 +01:00
|
|
|
std::string strUsage = _("Flowee the Hub") + " " + _("version") + " " + FormatFullVersion() + "\n";
|
2014-11-22 19:56:25 +01:00
|
|
|
|
2017-11-13 23:09:33 +01:00
|
|
|
if (!mapArgs.count("-version")) {
|
2014-11-22 19:56:25 +01:00
|
|
|
strUsage += "\n" + _("Usage:") + "\n" +
|
2019-04-02 17:45:04 +02:00
|
|
|
" hub [options] " + _("Start Flowee the Hub") + "\n";
|
2014-11-22 19:56:25 +01:00
|
|
|
|
2017-02-14 17:34:40 -08:00
|
|
|
strUsage += "\n" + allowedArgs.helpMessage();
|
2014-11-22 19:56:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf(stdout, "%s", strUsage.c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 15:14:13 +01:00
|
|
|
std::unique_ptr<HubApiServices> hubApiServices;
|
2013-05-26 13:48:30 -04:00
|
|
|
try
|
|
|
|
|
{
|
2019-05-12 11:04:04 +02:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
|
if (!IsSwitchChar(argv[i][0])) {
|
|
|
|
|
fprintf(stderr, "Error: unexpected argument found. Options go in the form of -name=value\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-01 14:10:35 +02:00
|
|
|
std::string dd = GetArg("-datadir", "");
|
|
|
|
|
if (!dd.empty()) {
|
|
|
|
|
auto path = boost::filesystem::system_complete(dd);
|
|
|
|
|
if (!boost::filesystem::is_directory(path)) {
|
|
|
|
|
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", dd.c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-05-26 13:48:30 -04:00
|
|
|
}
|
2019-05-24 22:46:14 +02:00
|
|
|
boost::filesystem::path pathConfigFile = GetArg("-conf", "");
|
2023-02-21 14:16:43 +01:00
|
|
|
const bool confPathSet = pathConfigFile.is_absolute();
|
2019-05-24 22:46:14 +02:00
|
|
|
|
2019-05-12 11:04:04 +02:00
|
|
|
if (!confPathSet) // first select chain, so we read the right conf file.
|
|
|
|
|
if (!SelectChain()) return false;
|
|
|
|
|
|
2018-09-25 16:21:30 +02:00
|
|
|
try {
|
|
|
|
|
ReadConfigFile(mapArgs, mapMultiArgs);
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
fprintf(stderr,"Error reading configuration file: %s\n", e.what());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-12 11:04:04 +02:00
|
|
|
if (confPathSet) // after raeding the user-indicated conf file, select chain (including conf file opts)
|
|
|
|
|
if (!SelectChain()) return false;
|
2013-05-26 13:48:30 -04:00
|
|
|
|
2013-07-24 09:30:09 +02:00
|
|
|
#ifndef WIN32
|
2013-05-26 13:48:30 -04:00
|
|
|
fDaemon = GetBoolArg("-daemon", false);
|
|
|
|
|
if (fDaemon)
|
|
|
|
|
{
|
2019-04-03 14:58:19 +02:00
|
|
|
fprintf(stdout, "Flowee Hub server starting\n");
|
2013-12-20 11:48:22 +01:00
|
|
|
|
2013-05-26 13:48:30 -04:00
|
|
|
// Daemonize
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
|
if (pid < 0)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (pid > 0) // Parent process, pid is child process id
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// Child process falls through to rest of initialization
|
|
|
|
|
|
|
|
|
|
pid_t sid = setsid();
|
|
|
|
|
if (sid < 0)
|
|
|
|
|
fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-12-20 11:48:22 +01:00
|
|
|
SoftSetBoolArg("-server", true);
|
2013-05-26 13:48:30 -04:00
|
|
|
|
2015-11-26 14:03:27 +01:00
|
|
|
// Set this early so that parameter interactions go to console
|
|
|
|
|
InitLogging();
|
2015-10-08 09:58:31 +02:00
|
|
|
InitParameterInteraction();
|
2015-04-03 11:50:06 -04:00
|
|
|
fRet = AppInit2(threadGroup, scheduler);
|
2018-02-13 13:09:33 +01:00
|
|
|
|
2021-03-16 15:14:13 +01:00
|
|
|
if (fRet && GetBoolArg("-api", true))
|
2025-02-08 19:05:26 +01:00
|
|
|
hubApiServices.reset(new HubApiServices(Application::instance()->ioContext()));
|
2013-05-26 13:48:30 -04:00
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const std::exception& e) {
|
2013-05-26 13:48:30 -04:00
|
|
|
PrintExceptionContinue(&e, "AppInit()");
|
|
|
|
|
} catch (...) {
|
2018-06-12 23:07:06 +02:00
|
|
|
PrintExceptionContinue(nullptr, "AppInit()");
|
2013-05-26 13:48:30 -04:00
|
|
|
}
|
2013-10-06 14:18:55 +02:00
|
|
|
|
|
|
|
|
if (!fRet)
|
|
|
|
|
{
|
2015-01-23 07:53:17 +01:00
|
|
|
Interrupt(threadGroup);
|
2013-10-06 14:18:55 +02:00
|
|
|
// threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
|
|
|
|
|
// the startup-failure cases to make sure they don't result in a hang due to some
|
|
|
|
|
// thread-blocking-waiting-for-another-thread-during-startup case
|
2015-01-19 15:33:55 +01:00
|
|
|
} else {
|
|
|
|
|
WaitForShutdown(&threadGroup);
|
2013-05-26 13:48:30 -04:00
|
|
|
}
|
2021-03-16 15:14:13 +01:00
|
|
|
hubApiServices.reset();
|
2013-05-26 13:48:30 -04:00
|
|
|
Shutdown();
|
|
|
|
|
|
|
|
|
|
return fRet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
2014-05-13 10:15:00 +00:00
|
|
|
SetupEnvironment();
|
|
|
|
|
|
2019-04-02 17:45:04 +02:00
|
|
|
// Connect hub signal handlers
|
2013-05-26 13:48:30 -04:00
|
|
|
noui_connect();
|
|
|
|
|
|
2014-05-20 01:22:33 +08:00
|
|
|
return (AppInit(argc, argv) ? 0 : 1);
|
2013-05-26 13:48:30 -04:00
|
|
|
}
|