|
|
|
@ -15,29 +15,58 @@
|
|
|
|
|
|
|
|
|
|
#include <csignal>
|
|
|
|
|
|
|
|
|
|
#include <cxxopts.hpp>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <future>
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lokinet_main(int, char**);
|
|
|
|
|
#include <CLI/App.hpp>
|
|
|
|
|
#include <CLI/Formatter.hpp>
|
|
|
|
|
#include <CLI/Config.hpp>
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
extern "C" LONG FAR PASCAL
|
|
|
|
|
win32_signal_handler(EXCEPTION_POINTERS*);
|
|
|
|
|
extern "C" VOID FAR PASCAL
|
|
|
|
|
win32_daemon_entry(DWORD, LPTSTR*);
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
struct command_line_options
|
|
|
|
|
{
|
|
|
|
|
// bool options
|
|
|
|
|
bool help = false;
|
|
|
|
|
bool version = false;
|
|
|
|
|
bool generate = false;
|
|
|
|
|
bool router = false;
|
|
|
|
|
bool force = false;
|
|
|
|
|
bool configOnly = false;
|
|
|
|
|
bool overwrite = false;
|
|
|
|
|
|
|
|
|
|
VOID
|
|
|
|
|
insert_description();
|
|
|
|
|
// string options
|
|
|
|
|
std::string configPath;
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
// windows options
|
|
|
|
|
bool win_install = false;
|
|
|
|
|
bool win_remove = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// windows-specific function declarations
|
|
|
|
|
int
|
|
|
|
|
startWinsock();
|
|
|
|
|
void
|
|
|
|
|
install_win32_daemon();
|
|
|
|
|
void
|
|
|
|
|
uninstall_win32_daemon();
|
|
|
|
|
|
|
|
|
|
// operational function definitions
|
|
|
|
|
int
|
|
|
|
|
lokinet_main(int, char**);
|
|
|
|
|
void
|
|
|
|
|
handle_signal(int sig);
|
|
|
|
|
static void
|
|
|
|
|
run_main_context(std::optional<fs::path> confFile, const llarp::RuntimeOptions opts);
|
|
|
|
|
|
|
|
|
|
// variable declarations
|
|
|
|
|
static auto logcat = llarp::log::Cat("main");
|
|
|
|
|
std::shared_ptr<llarp::Context> ctx;
|
|
|
|
|
std::promise<int> exit_code;
|
|
|
|
|
|
|
|
|
|
// operational function definitions
|
|
|
|
|
void
|
|
|
|
|
handle_signal(int sig)
|
|
|
|
|
{
|
|
|
|
@ -48,7 +77,23 @@ handle_signal(int sig)
|
|
|
|
|
std::cerr << "Received signal " << sig << ", but have no context yet. Ignoring!" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Windows specific code
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
extern "C" LONG FAR PASCAL
|
|
|
|
|
win32_signal_handler(EXCEPTION_POINTERS*);
|
|
|
|
|
extern "C" VOID FAR PASCAL
|
|
|
|
|
win32_daemon_entry(DWORD, LPTSTR*);
|
|
|
|
|
VOID
|
|
|
|
|
insert_description();
|
|
|
|
|
|
|
|
|
|
extern "C" BOOL FAR PASCAL
|
|
|
|
|
handle_signal_win32(DWORD fdwCtrlType)
|
|
|
|
|
{
|
|
|
|
|
UNREFERENCED_PARAMETER(fdwCtrlType);
|
|
|
|
|
handle_signal(SIGINT);
|
|
|
|
|
return TRUE; // probably unreachable
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
startWinsock()
|
|
|
|
|
{
|
|
|
|
@ -64,14 +109,6 @@ startWinsock()
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" BOOL FAR PASCAL
|
|
|
|
|
handle_signal_win32(DWORD fdwCtrlType)
|
|
|
|
|
{
|
|
|
|
|
UNREFERENCED_PARAMETER(fdwCtrlType);
|
|
|
|
|
handle_signal(SIGINT);
|
|
|
|
|
return TRUE; // probably unreachable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
install_win32_daemon()
|
|
|
|
|
{
|
|
|
|
@ -221,76 +258,6 @@ uninstall_win32_daemon()
|
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// this sets up, configures and runs the main context
|
|
|
|
|
static void
|
|
|
|
|
run_main_context(std::optional<fs::path> confFile, const llarp::RuntimeOptions opts)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogInfo(fmt::format("starting up {} {}", llarp::VERSION_FULL, llarp::RELEASE_MOTTO));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
std::shared_ptr<llarp::Config> conf;
|
|
|
|
|
if (confFile)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogInfo("Using config file: ", *confFile);
|
|
|
|
|
conf = std::make_shared<llarp::Config>(confFile->parent_path());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
conf = std::make_shared<llarp::Config>(llarp::GetDefaultDataDir());
|
|
|
|
|
}
|
|
|
|
|
if (not conf->Load(confFile, opts.isSNode))
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError("failed to parse configuration");
|
|
|
|
|
exit_code.set_value(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx = std::make_shared<llarp::Context>();
|
|
|
|
|
ctx->Configure(std::move(conf));
|
|
|
|
|
|
|
|
|
|
signal(SIGINT, handle_signal);
|
|
|
|
|
signal(SIGTERM, handle_signal);
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
signal(SIGHUP, handle_signal);
|
|
|
|
|
signal(SIGUSR1, handle_signal);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ctx->Setup(opts);
|
|
|
|
|
}
|
|
|
|
|
catch (llarp::util::bind_socket_error& ex)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError(fmt::format("{}, is lokinet already running? 🤔", ex.what()));
|
|
|
|
|
exit_code.set_value(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception& ex)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError(fmt::format("failed to start up lokinet: {}", ex.what()));
|
|
|
|
|
exit_code.set_value(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
llarp::util::SetThreadName("llarp-mainloop");
|
|
|
|
|
|
|
|
|
|
auto result = ctx->Run(opts);
|
|
|
|
|
exit_code.set_value(result);
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError("Fatal: caught exception while running: ", e.what());
|
|
|
|
|
exit_code.set_exception(std::current_exception());
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError("Fatal: caught non-standard exception while running");
|
|
|
|
|
exit_code.set_exception(std::current_exception());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
|
|
/// minidump generation for windows jizz
|
|
|
|
|
/// will make a coredump when there is an unhandled exception
|
|
|
|
@ -326,46 +293,57 @@ GenerateDump(EXCEPTION_POINTERS* pExceptionPointers)
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
|
VOID FAR PASCAL
|
|
|
|
|
SvcCtrlHandler(DWORD dwCtrl)
|
|
|
|
|
{
|
|
|
|
|
// Set up a default, stderr logging for very early logging; we'll replace this later once we read
|
|
|
|
|
// the desired log info from config.
|
|
|
|
|
llarp::log::add_sink(llarp::log::Type::Print, "stderr");
|
|
|
|
|
llarp::log::reset_level(llarp::log::Level::info);
|
|
|
|
|
// Handle the requested control code.
|
|
|
|
|
|
|
|
|
|
llarp::logRingBuffer = std::make_shared<llarp::log::RingBufferSink>(100);
|
|
|
|
|
llarp::log::add_sink(llarp::logRingBuffer, llarp::log::DEFAULT_PATTERN_MONO);
|
|
|
|
|
switch (dwCtrl)
|
|
|
|
|
{
|
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
|
// tell service we are stopping
|
|
|
|
|
llarp::log::debug(logcat, "Windows service controller gave SERVICE_CONTROL_STOP");
|
|
|
|
|
llarp::sys::service_manager->system_changed_our_state(llarp::sys::ServiceState::Stopping);
|
|
|
|
|
handle_signal(SIGINT);
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
return lokinet_main(argc, argv);
|
|
|
|
|
#else
|
|
|
|
|
SERVICE_TABLE_ENTRY DispatchTable[] = {
|
|
|
|
|
{strdup("lokinet"), (LPSERVICE_MAIN_FUNCTION)win32_daemon_entry}, {NULL, NULL}};
|
|
|
|
|
case SERVICE_CONTROL_INTERROGATE:
|
|
|
|
|
// report status
|
|
|
|
|
llarp::log::debug(logcat, "Got win32 service interrogate signal");
|
|
|
|
|
llarp::sys::service_manager->report_changed_state();
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Try first to run as a service; if this works it fires off to win32_daemon_entry and doesn't
|
|
|
|
|
// return until the service enters STOPPED state.
|
|
|
|
|
if (StartServiceCtrlDispatcher(DispatchTable))
|
|
|
|
|
return 0;
|
|
|
|
|
default:
|
|
|
|
|
llarp::log::debug(logcat, "Got win32 unhandled signal {}", dwCtrl);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto error = GetLastError();
|
|
|
|
|
// The win32 daemon entry point is where we go when invoked as a windows service; we do the
|
|
|
|
|
// required service dance and then pretend we were invoked via main().
|
|
|
|
|
VOID FAR PASCAL
|
|
|
|
|
win32_daemon_entry(DWORD, LPTSTR* argv)
|
|
|
|
|
{
|
|
|
|
|
// Register the handler function for the service
|
|
|
|
|
auto* svc = dynamic_cast<llarp::sys::SVC_Manager*>(llarp::sys::service_manager);
|
|
|
|
|
svc->handle = RegisterServiceCtrlHandler("lokinet", SvcCtrlHandler);
|
|
|
|
|
|
|
|
|
|
// We'll get this error if not invoked as a service, which is fine: we can just run directly
|
|
|
|
|
if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
|
|
|
|
|
if (svc->handle == nullptr)
|
|
|
|
|
{
|
|
|
|
|
llarp::sys::service_manager->disable();
|
|
|
|
|
return lokinet_main(argc, argv);
|
|
|
|
|
llarp::LogError("failed to register daemon control handler");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
llarp::log::critical(
|
|
|
|
|
logcat, "Error launching service: {}", std::system_category().message(error));
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
// we hard code the args to lokinet_main.
|
|
|
|
|
// we yoink argv[0] (lokinet.exe path) and pass in the new args.
|
|
|
|
|
std::array args = {
|
|
|
|
|
reinterpret_cast<char*>(argv[0]),
|
|
|
|
|
reinterpret_cast<char*>(strdup("c:\\programdata\\lokinet\\lokinet.ini")),
|
|
|
|
|
reinterpret_cast<char*>(0)};
|
|
|
|
|
lokinet_main(args.size() - 1, args.data());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lokinet_main(int argc, char** argv)
|
|
|
|
@ -382,98 +360,86 @@ lokinet_main(int argc, char** argv)
|
|
|
|
|
SetConsoleCtrlHandler(handle_signal_win32, TRUE);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
cxxopts::Options options(
|
|
|
|
|
"lokinet",
|
|
|
|
|
"LokiNET is a free, open source, private, "
|
|
|
|
|
"decentralized, \"market based sybil resistant\" "
|
|
|
|
|
"and IP based onion routing network");
|
|
|
|
|
// clang-format off
|
|
|
|
|
options.add_options()
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
("install", "install win32 daemon to SCM", cxxopts::value<bool>())
|
|
|
|
|
("remove", "remove win32 daemon from SCM", cxxopts::value<bool>())
|
|
|
|
|
#endif
|
|
|
|
|
("h,help", "print this help message", cxxopts::value<bool>())
|
|
|
|
|
("version", "print version string", cxxopts::value<bool>())
|
|
|
|
|
("g,generate", "generate default configuration and exit", cxxopts::value<bool>())
|
|
|
|
|
("r,router", "run in routing mode instead of client only mode", cxxopts::value<bool>())
|
|
|
|
|
("f,force", "force writing config even if it already exists", cxxopts::value<bool>())
|
|
|
|
|
("config", "path to lokinet.ini configuration file", cxxopts::value<std::string>())
|
|
|
|
|
;
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
options.parse_positional("config");
|
|
|
|
|
|
|
|
|
|
bool genconfigOnly = false;
|
|
|
|
|
bool overwrite = false;
|
|
|
|
|
std::optional<fs::path> configFile;
|
|
|
|
|
try
|
|
|
|
|
CLI::App cli{
|
|
|
|
|
"LokiNET is a free, open source, private, decentralized, market-based sybil resistant and "
|
|
|
|
|
"IP "
|
|
|
|
|
"based onion routing network",
|
|
|
|
|
"lokinet"};
|
|
|
|
|
command_line_options options{};
|
|
|
|
|
|
|
|
|
|
// flags: boolean values in command_line_options struct
|
|
|
|
|
cli.add_flag("--version", options.version, "Lokinet version");
|
|
|
|
|
cli.add_flag("g,--generate", options.generate, "Generate default configuration and exit");
|
|
|
|
|
cli.add_flag(
|
|
|
|
|
"r,--router", options.router, "Run lokinet in routing mode instead of client-only mode");
|
|
|
|
|
cli.add_flag("f,--force", options.force, "Force writing config even if file exists");
|
|
|
|
|
|
|
|
|
|
// options: string
|
|
|
|
|
cli.add_option("--config", options.configPath, "Path to lokinet.ini configuration file")
|
|
|
|
|
->capture_default_str();
|
|
|
|
|
|
|
|
|
|
if constexpr (llarp::platform::is_windows)
|
|
|
|
|
{
|
|
|
|
|
auto result = options.parse(argc, argv);
|
|
|
|
|
cli.add_flag("--install", options.win_install, "Install win32 daemon to SCM");
|
|
|
|
|
cli.add_flag("--remove", options.win_remove, "Remove win32 daemon from SCM");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.count("help"))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
std::cout << options.help() << std::endl;
|
|
|
|
|
return 0;
|
|
|
|
|
cli.parse(argc, argv);
|
|
|
|
|
}
|
|
|
|
|
if (result.count("version"))
|
|
|
|
|
catch (const CLI::ParseError& e)
|
|
|
|
|
{
|
|
|
|
|
return cli.exit(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<fs::path> configFile;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (options.version)
|
|
|
|
|
{
|
|
|
|
|
std::cout << llarp::VERSION_FULL << std::endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
if (result.count("install"))
|
|
|
|
|
|
|
|
|
|
if constexpr (llarp::platform::is_windows)
|
|
|
|
|
{
|
|
|
|
|
if (options.win_install)
|
|
|
|
|
{
|
|
|
|
|
install_win32_daemon();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.count("remove"))
|
|
|
|
|
if (options.win_remove)
|
|
|
|
|
{
|
|
|
|
|
uninstall_win32_daemon();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (result.count("generate") > 0)
|
|
|
|
|
{
|
|
|
|
|
genconfigOnly = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.count("router") > 0)
|
|
|
|
|
if (options.configOnly)
|
|
|
|
|
{
|
|
|
|
|
opts.isSNode = true;
|
|
|
|
|
configFile = options.configPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.count("force") > 0)
|
|
|
|
|
{
|
|
|
|
|
overwrite = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.count("config") > 0)
|
|
|
|
|
{
|
|
|
|
|
auto arg = result["config"].as<std::string>();
|
|
|
|
|
if (!arg.empty())
|
|
|
|
|
catch (const CLI::OptionNotFound& e)
|
|
|
|
|
{
|
|
|
|
|
configFile = arg;
|
|
|
|
|
}
|
|
|
|
|
cli.exit(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (const cxxopts::option_not_exists_exception& ex)
|
|
|
|
|
catch (const CLI::Error& e)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << ex.what();
|
|
|
|
|
std::cout << options.help() << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
cli.exit(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (configFile.has_value())
|
|
|
|
|
{
|
|
|
|
|
// when we have an explicit filepath
|
|
|
|
|
fs::path basedir = configFile->parent_path();
|
|
|
|
|
if (genconfigOnly)
|
|
|
|
|
if (options.configOnly)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
llarp::ensureConfig(basedir, *configFile, overwrite, opts.isSNode);
|
|
|
|
|
llarp::ensureConfig(basedir, *configFile, options.overwrite, opts.isSNode);
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception& ex)
|
|
|
|
|
{
|
|
|
|
@ -503,7 +469,10 @@ lokinet_main(int argc, char** argv)
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
llarp::ensureConfig(
|
|
|
|
|
llarp::GetDefaultDataDir(), llarp::GetDefaultConfigPath(), overwrite, opts.isSNode);
|
|
|
|
|
llarp::GetDefaultDataDir(),
|
|
|
|
|
llarp::GetDefaultConfigPath(),
|
|
|
|
|
options.overwrite,
|
|
|
|
|
opts.isSNode);
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception& ex)
|
|
|
|
|
{
|
|
|
|
@ -513,10 +482,8 @@ lokinet_main(int argc, char** argv)
|
|
|
|
|
configFile = llarp::GetDefaultConfigPath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (genconfigOnly)
|
|
|
|
|
{
|
|
|
|
|
if (options.configOnly)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
SetUnhandledExceptionFilter(&GenerateDump);
|
|
|
|
@ -588,55 +555,111 @@ lokinet_main(int argc, char** argv)
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
|
|
VOID FAR PASCAL
|
|
|
|
|
SvcCtrlHandler(DWORD dwCtrl)
|
|
|
|
|
// this sets up, configures and runs the main context
|
|
|
|
|
static void
|
|
|
|
|
run_main_context(std::optional<fs::path> confFile, const llarp::RuntimeOptions opts)
|
|
|
|
|
{
|
|
|
|
|
// Handle the requested control code.
|
|
|
|
|
|
|
|
|
|
switch (dwCtrl)
|
|
|
|
|
llarp::LogInfo(fmt::format("starting up {} {}", llarp::VERSION_FULL, llarp::RELEASE_MOTTO));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
|
// tell service we are stopping
|
|
|
|
|
llarp::log::debug(logcat, "Windows service controller gave SERVICE_CONTROL_STOP");
|
|
|
|
|
llarp::sys::service_manager->system_changed_our_state(llarp::sys::ServiceState::Stopping);
|
|
|
|
|
handle_signal(SIGINT);
|
|
|
|
|
std::shared_ptr<llarp::Config> conf;
|
|
|
|
|
if (confFile)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogInfo("Using config file: ", *confFile);
|
|
|
|
|
conf = std::make_shared<llarp::Config>(confFile->parent_path());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
conf = std::make_shared<llarp::Config>(llarp::GetDefaultDataDir());
|
|
|
|
|
}
|
|
|
|
|
if (not conf->Load(confFile, opts.isSNode))
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError("failed to parse configuration");
|
|
|
|
|
exit_code.set_value(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SERVICE_CONTROL_INTERROGATE:
|
|
|
|
|
// report status
|
|
|
|
|
llarp::log::debug(logcat, "Got win32 service interrogate signal");
|
|
|
|
|
llarp::sys::service_manager->report_changed_state();
|
|
|
|
|
ctx = std::make_shared<llarp::Context>();
|
|
|
|
|
ctx->Configure(std::move(conf));
|
|
|
|
|
|
|
|
|
|
signal(SIGINT, handle_signal);
|
|
|
|
|
signal(SIGTERM, handle_signal);
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
signal(SIGHUP, handle_signal);
|
|
|
|
|
signal(SIGUSR1, handle_signal);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ctx->Setup(opts);
|
|
|
|
|
}
|
|
|
|
|
catch (llarp::util::bind_socket_error& ex)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError(fmt::format("{}, is lokinet already running? 🤔", ex.what()));
|
|
|
|
|
exit_code.set_value(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception& ex)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError(fmt::format("failed to start up lokinet: {}", ex.what()));
|
|
|
|
|
exit_code.set_value(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
llarp::util::SetThreadName("llarp-mainloop");
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
llarp::log::debug(logcat, "Got win32 unhandled signal {}", dwCtrl);
|
|
|
|
|
break;
|
|
|
|
|
auto result = ctx->Run(opts);
|
|
|
|
|
exit_code.set_value(result);
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError("Fatal: caught exception while running: ", e.what());
|
|
|
|
|
exit_code.set_exception(std::current_exception());
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError("Fatal: caught non-standard exception while running");
|
|
|
|
|
exit_code.set_exception(std::current_exception());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The win32 daemon entry point is where we go when invoked as a windows service; we do the required
|
|
|
|
|
// service dance and then pretend we were invoked via main().
|
|
|
|
|
VOID FAR PASCAL
|
|
|
|
|
win32_daemon_entry(DWORD, LPTSTR* argv)
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
// Register the handler function for the service
|
|
|
|
|
auto* svc = dynamic_cast<llarp::sys::SVC_Manager*>(llarp::sys::service_manager);
|
|
|
|
|
svc->handle = RegisterServiceCtrlHandler("lokinet", SvcCtrlHandler);
|
|
|
|
|
// Set up a default, stderr logging for very early logging; we'll replace this later once we read
|
|
|
|
|
// the desired log info from config.
|
|
|
|
|
llarp::log::add_sink(llarp::log::Type::Print, "stderr");
|
|
|
|
|
llarp::log::reset_level(llarp::log::Level::info);
|
|
|
|
|
|
|
|
|
|
if (svc->handle == nullptr)
|
|
|
|
|
llarp::logRingBuffer = std::make_shared<llarp::log::RingBufferSink>(100);
|
|
|
|
|
llarp::log::add_sink(llarp::logRingBuffer, llarp::log::DEFAULT_PATTERN_MONO);
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
return lokinet_main(argc, argv);
|
|
|
|
|
#else
|
|
|
|
|
SERVICE_TABLE_ENTRY DispatchTable[] = {
|
|
|
|
|
{strdup("lokinet"), (LPSERVICE_MAIN_FUNCTION)win32_daemon_entry}, {NULL, NULL}};
|
|
|
|
|
|
|
|
|
|
// Try first to run as a service; if this works it fires off to win32_daemon_entry and doesn't
|
|
|
|
|
// return until the service enters STOPPED state.
|
|
|
|
|
if (StartServiceCtrlDispatcher(DispatchTable))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
auto error = GetLastError();
|
|
|
|
|
|
|
|
|
|
// We'll get this error if not invoked as a service, which is fine: we can just run directly
|
|
|
|
|
if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError("failed to register daemon control handler");
|
|
|
|
|
return;
|
|
|
|
|
llarp::sys::service_manager->disable();
|
|
|
|
|
return lokinet_main(argc, argv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we hard code the args to lokinet_main.
|
|
|
|
|
// we yoink argv[0] (lokinet.exe path) and pass in the new args.
|
|
|
|
|
std::array args = {
|
|
|
|
|
reinterpret_cast<char*>(argv[0]),
|
|
|
|
|
reinterpret_cast<char*>(strdup("c:\\programdata\\lokinet\\lokinet.ini")),
|
|
|
|
|
reinterpret_cast<char*>(0)};
|
|
|
|
|
lokinet_main(args.size() - 1, args.data());
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
llarp::log::critical(
|
|
|
|
|
logcat, "Error launching service: {}", std::system_category().message(error));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|