lokinet/daemon/main.cpp

540 lines
15 KiB
C++
Raw Normal View History

#include <config/config.hpp> // for ensure_config
#include <constants/version.hpp>
#include <llarp.hpp>
#include <util/lokinet_init.h>
2019-01-11 02:59:44 +01:00
#include <util/fs.hpp>
2019-09-01 14:10:49 +02:00
#include <util/logging/logger.hpp>
2019-09-16 21:40:31 +02:00
#include <util/logging/ostream_logger.hpp>
#include <util/str.hpp>
2020-06-29 22:09:59 +02:00
#include <util/thread/logic.hpp>
2019-01-11 02:59:44 +01:00
#include <csignal>
#include <cxxopts.hpp>
2018-07-20 06:50:28 +02:00
#include <string>
#include <iostream>
#include <future>
2018-05-20 18:15:16 +02:00
2020-05-21 16:18:23 +02:00
#ifdef USE_JEMALLOC
#include <new>
#include <jemalloc/jemalloc.h>
void*
operator new(std::size_t sz)
{
void* ptr = malloc(sz);
if (ptr)
return ptr;
else
throw std::bad_alloc{};
}
void
operator delete(void* ptr) noexcept
{
free(ptr);
}
void
operator delete(void* ptr, size_t) noexcept
{
free(ptr);
}
#endif
#ifdef _WIN32
#include <setjmp.h>
2020-08-12 22:27:39 +02:00
#include <strsafe.h>
2019-08-02 05:25:48 +02:00
extern "C" LONG FAR PASCAL
win32_signal_handler(EXCEPTION_POINTERS*);
extern "C" VOID FAR PASCAL
win32_daemon_entry(DWORD, LPTSTR*);
2020-08-12 21:53:29 +02:00
VOID ReportSvcStatus(DWORD,DWORD,DWORD);
jmp_buf svc_entry;
2020-08-12 21:53:29 +02:00
SERVICE_STATUS SvcStatus;
2020-08-12 22:27:39 +02:00
SERVICE_STATUS_HANDLE SvcStatusHandle;
bool start_as_daemon = false;
#endif
2020-06-29 21:55:59 +02:00
std::shared_ptr<llarp::Context> ctx;
std::promise<int> exit_code;
2018-04-30 18:14:20 +02:00
void
handle_signal(int sig)
2018-05-18 19:50:21 +02:00
{
2020-06-29 22:09:59 +02:00
if (ctx)
LogicCall(ctx->logic, std::bind(&llarp::Context::HandleSignal, ctx.get(), sig));
else
std::cerr << "Received signal " << sig << ", but have no context yet. Ignoring!" << std::endl;
2018-05-18 19:50:21 +02:00
}
#ifdef _WIN32
int
startWinsock()
{
WSADATA wsockd;
int err;
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
if (err)
{
perror("Failed to start Windows Sockets");
return err;
}
::CreateMutex(nullptr, FALSE, "lokinet_win32_daemon");
return 0;
}
2018-12-14 13:50:45 +01:00
extern "C" BOOL FAR PASCAL
handle_signal_win32(DWORD fdwCtrlType)
{
UNREFERENCED_PARAMETER(fdwCtrlType);
handle_signal(SIGINT);
2018-12-23 14:29:11 +01:00
return TRUE; // probably unreachable
2018-12-14 13:50:45 +01:00
}
2020-08-12 20:00:51 +02:00
void install_win32_daemon()
{
2020-08-12 22:27:39 +02:00
SC_HANDLE schSCManager;
SC_HANDLE schService;
std::array<char, 1024> szPath{};
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
if( !GetModuleFileName( nullptr, szPath.data(), MAX_PATH ) )
{
llarp::LogError("Cannot install service ", GetLastError());
return;
}
StringCchCat(szPath.data(), 1024, " --win32-daemon");
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
// Get a handle to the SCM database.
schSCManager = OpenSCManager(
nullptr, // local computer
nullptr, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
if (nullptr == schSCManager)
{
llarp::LogError("OpenSCManager failed ", GetLastError());
return;
}
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
// Create the service
schService = CreateService(
schSCManager, // SCM database
"lokinet", // name of service
"Lokinet for Windows", // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szPath.data(), // path to service's binary
nullptr, // no load ordering group
nullptr, // no tag identifier
nullptr, // no dependencies
nullptr, // LocalSystem account
nullptr); // no password
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
if (schService == nullptr)
{
llarp::LogError("CreateService failed ", GetLastError());
2020-08-12 20:00:51 +02:00
CloseServiceHandle(schSCManager);
2020-08-12 22:27:39 +02:00
return;
}
else llarp::LogInfo("Service installed successfully");
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
2020-08-12 20:00:51 +02:00
}
void uninstall_win32_daemon()
{
2020-08-12 22:27:39 +02:00
SC_HANDLE schSCManager;
SC_HANDLE schService;
// Get a handle to the SCM database.
schSCManager = OpenSCManager(
nullptr, // local computer
nullptr, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
if (nullptr == schSCManager)
{
llarp::LogError("OpenSCManager failed ", GetLastError());
return;
}
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
// Get a handle to the service.
schService = OpenService(
schSCManager, // SCM database
"lokinet", // name of service
0x10000); // need delete access
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
if (schService == nullptr)
{
llarp::LogError("OpenService failed ", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
// Delete the service.
if (! DeleteService(schService) )
{
llarp::LogError("DeleteService failed ", GetLastError());
}
else llarp::LogInfo("Service deleted successfully\n");
2020-08-12 20:00:51 +02:00
2020-08-12 22:27:39 +02:00
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
2020-08-12 20:00:51 +02:00
}
#endif
/// this sets up, configures and runs the main context
static void
run_main_context(const fs::path confFile, const llarp::RuntimeOptions opts)
{
2020-06-29 21:55:59 +02:00
try
{
2020-06-29 21:55:59 +02:00
// this is important, can downgrade from Info though
llarp::LogDebug("Running from: ", fs::current_path().string());
llarp::LogInfo("Using config file: ", confFile);
llarp::Config conf;
conf.Load(confFile, opts.isRouter, confFile.parent_path());
2020-07-02 17:40:08 +02:00
ctx = std::make_shared<llarp::Context>();
ctx->Configure(conf);
2020-06-29 21:55:59 +02:00
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
#ifndef _WIN32
signal(SIGHUP, handle_signal);
#endif
2020-06-29 21:55:59 +02:00
ctx->Setup(opts);
2020-06-29 21:55:59 +02:00
llarp::util::SetThreadName("llarp-mainloop");
2020-06-29 21:55:59 +02:00
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());
2020-06-29 21:55:59 +02:00
}
catch (...)
{
llarp::LogError("Fatal: caught non-standard exception while running");
exit_code.set_exception(std::current_exception());
}
}
int
main(int argc, char* argv[])
{
auto result = Lokinet_INIT();
if (result)
{
return result;
}
2020-06-29 21:55:59 +02:00
llarp::RuntimeOptions opts;
#ifdef _WIN32
if (startWinsock())
return -1;
2018-12-14 13:50:45 +01:00
SetConsoleCtrlHandler(handle_signal_win32, TRUE);
2019-08-02 05:25:48 +02:00
// SetUnhandledExceptionFilter(win32_signal_handler);
2020-08-12 22:27:39 +02:00
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ "lokinet", (LPSERVICE_MAIN_FUNCTION) win32_daemon_entry },
{ NULL, NULL }
};
#endif
cxxopts::Options options(
"lokinet",
"LokiNET is a free, open source, private, "
"decentralized, \"market based sybil resistant\" "
"and IP based onion routing network");
options.add_options()("v,verbose", "Verbose", cxxopts::value<bool>())(
2020-08-12 20:00:51 +02:00
"h,help", "help", cxxopts::value<bool>())("version", "version", cxxopts::value<bool>())
#ifdef _WIN32
("install", "install win32 daemon to SCM", cxxopts::value<bool>())
("remove", "remove win32 daemon from SCM", cxxopts::value<bool>())
2020-08-12 22:27:39 +02:00
("win32-daemon", "do not use interactively", cxxopts::value<bool>())
2020-08-12 20:00:51 +02:00
#endif
("g,generate", "generate client config", cxxopts::value<bool>())(
"r,relay", "run as relay instead of client", cxxopts::value<bool>())(
"f,force", "overwrite", cxxopts::value<bool>())(
"c,colour", "colour output", cxxopts::value<bool>()->default_value("true"))(
"b,background",
"background mode (start, but do not connect to the network)",
cxxopts::value<bool>())(
"config", "path to configuration file", cxxopts::value<std::string>());
2019-04-21 23:48:28 +02:00
options.parse_positional("config");
2019-10-08 16:52:01 +02:00
bool genconfigOnly = false;
bool overwrite = false;
2020-06-29 21:55:59 +02:00
fs::path configFile;
try
{
auto result = options.parse(argc, argv);
if (result.count("verbose") > 0)
{
SetLogLevel(llarp::eLogDebug);
llarp::LogDebug("debug logging activated");
}
if (!result["colour"].as<bool>())
2019-09-16 21:40:31 +02:00
{
llarp::LogContext::Instance().logStream =
std::make_unique<llarp::OStreamLogStream>(false, std::cerr);
2019-09-16 21:40:31 +02:00
}
if (result.count("help"))
{
2019-04-23 22:52:13 +02:00
std::cout << options.help() << std::endl;
return 0;
}
if (result.count("version"))
{
2020-06-29 21:55:59 +02:00
std::cout << llarp::VERSION_FULL << std::endl;
return 0;
}
2020-08-12 20:00:51 +02:00
#ifdef _WIN32
if (result.count("install"))
{
2020-08-12 22:27:39 +02:00
install_win32_daemon();
2020-08-12 20:00:51 +02:00
return 0;
}
2020-08-12 20:00:51 +02:00
if (result.count("remove"))
{
2020-08-12 22:27:39 +02:00
uninstall_win32_daemon();
2020-08-12 20:00:51 +02:00
return 0;
}
2020-08-12 22:27:39 +02:00
if (result.count("win32-daemon"))
{
start_as_daemon = true;
}
2020-08-12 20:00:51 +02:00
#endif
if (result.count("generate") > 0)
{
genconfigOnly = true;
}
if (result.count("background") > 0)
2019-05-28 02:19:25 +02:00
{
opts.background = true;
2019-05-28 02:19:25 +02:00
}
2020-06-29 21:55:59 +02:00
if (result.count("router") > 0)
{
2020-06-29 21:55:59 +02:00
opts.isRouter = true;
}
if (result.count("force") > 0)
{
overwrite = true;
}
if (result.count("config") > 0)
{
auto arg = result["config"].as<std::string>();
if (!arg.empty())
{
2020-06-29 21:55:59 +02:00
configFile = arg;
}
}
}
catch (const cxxopts::option_not_exists_exception& ex)
{
std::cerr << ex.what();
2019-04-23 22:52:13 +02:00
std::cout << options.help() << std::endl;
return 1;
}
#ifdef _WIN32
2020-08-12 22:27:39 +02:00
if (start_as_daemon)
{
setjmp(svc_entry);
// calling this twice returns a harmless error (daemon already running)
StartServiceCtrlDispatcher(DispatchTable);
ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);
}
#endif
2020-06-29 21:55:59 +02:00
if (!configFile.empty())
{
// when we have an explicit filepath
2020-06-29 21:55:59 +02:00
fs::path basedir = configFile.parent_path();
2019-01-29 12:23:40 +01:00
if (genconfigOnly)
{
2020-06-29 21:55:59 +02:00
llarp::ensureConfig(basedir, configFile, overwrite, opts.isRouter);
}
else
{
std::error_code ec;
2020-06-29 21:55:59 +02:00
if (!fs::exists(configFile, ec))
2019-01-29 12:23:40 +01:00
{
2020-06-29 21:55:59 +02:00
llarp::LogError("Config file not found ", configFile);
return 1;
2019-01-29 12:23:40 +01:00
}
if (ec)
throw std::runtime_error(llarp::stringify("filesystem error: ", ec));
}
}
else
{
llarp::ensureConfig(
2020-06-29 21:55:59 +02:00
llarp::GetDefaultDataDir(), llarp::GetDefaultConfigPath(), overwrite, opts.isRouter);
configFile = llarp::GetDefaultConfigPath();
}
if (genconfigOnly)
{
return 0;
}
2018-07-13 15:36:51 +02:00
2020-06-29 21:55:59 +02:00
std::thread main_thread{std::bind(&run_main_context, configFile, opts)};
auto ftr = exit_code.get_future();
do
2018-05-27 20:03:10 +02:00
{
// do periodic non lokinet related tasks here
2020-06-29 21:55:59 +02:00
if (ctx and ctx->IsUp() and not ctx->LooksAlive())
{
2020-06-29 21:55:59 +02:00
for (const auto& wtf : {"you have been visited by the mascott of the "
"deadlocked router.",
"⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣀⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠄⠄⠄⠄",
"⠄⠄⠄⠄⠄⢀⣀⣀⡀⠄⠄⠄⡠⢲⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠄⠄",
"⠄⠄⠄⠔⣈⣀⠄⢔⡒⠳⡴⠊⠄⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⣿⣿⣧⠄⠄",
"⠄⢜⡴⢑⠖⠊⢐⣤⠞⣩⡇⠄⠄⠄⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⠄⠝⠛⠋⠐",
"⢸⠏⣷⠈⠄⣱⠃⠄⢠⠃⠐⡀⠄⠄⠄⠄⠙⠻⢿⣿⣿⣿⣿⣿⣿⣿⡿⠛⠸⠄⠄⠄⠄",
"⠈⣅⠞⢁⣿⢸⠘⡄⡆⠄⠄⠈⠢⡀⠄⠄⠄⠄⠄⠄⠉⠙⠛⠛⠛⠉⠉⡀⠄⠡⢀⠄⣀",
"⠄⠙⡎⣹⢸⠄⠆⢘⠁⠄⠄⠄⢸⠈⠢⢄⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠃⠄⠄⠄⠄⠄",
"⠄⠄⠑⢿⠈⢆⠘⢼⠄⠄⠄⠄⠸⢐⢾⠄⡘⡏⠲⠆⠠⣤⢤⢤⡤⠄⣖⡇⠄⠄⠄⠄⠄",
"⣴⣶⣿⣿⣣⣈⣢⣸⠄⠄⠄⠄⡾⣷⣾⣮⣤⡏⠁⠘⠊⢠⣷⣾⡛⡟⠈⠄⠄⠄⠄⠄⠄",
"⣿⣿⣿⣿⣿⠉⠒⢽⠄⠄⠄⠄⡇⣿⣟⣿⡇⠄⠄⠄⠄⢸⣻⡿⡇⡇⠄⠄⠄⠄⠄⠄⠄",
"⠻⣿⣿⣿⣿⣄⠰⢼⠄⠄⠄⡄⠁⢻⣍⣯⠃⠄⠄⠄⠄⠈⢿⣻⠃⠈⡆⡄⠄⠄⠄⠄⠄",
"⠄⠙⠿⠿⠛⣿⣶⣤⡇⠄⠄⢣⠄⠄⠈⠄⢠⠂⠄⠁⠄⡀⠄⠄⣀⠔⢁⠃⠄⠄⠄⠄⠄",
"⠄⠄⠄⠄⠄⣿⣿⣿⣿⣾⠢⣖⣶⣦⣤⣤⣬⣤⣤⣤⣴⣶⣶⡏⠠⢃⠌⠄⠄⠄⠄⠄⠄",
"⠄⠄⠄⠄⠄⠿⠿⠟⠛⡹⠉⠛⠛⠿⠿⣿⣿⣿⣿⣿⡿⠂⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄",
"⠠⠤⠤⠄⠄⣀⠄⠄⠄⠑⠠⣤⣀⣀⣀⡘⣿⠿⠙⠻⡍⢀⡈⠂⠄⠄⠄⠄⠄⠄⠄⠄⠄",
"⠄⠄⠄⠄⠄⠄⠑⠠⣠⣴⣾⣿⣿⣿⣿⣿⣿⣇⠉⠄⠻⣿⣷⣄⡀⠄⠄⠄⠄⠄⠄⠄⠄",
"file a bug report now or be cursed with this "
"annoying image in your syslog for all time."})
{
2020-06-29 21:55:59 +02:00
LogError(wtf);
llarp::LogContext::Instance().ImmediateFlush();
}
2020-06-29 21:55:59 +02:00
std::abort();
}
} while (ftr.wait_for(std::chrono::seconds(1)) != std::future_status::ready);
main_thread.join();
2020-07-02 17:40:08 +02:00
int code = 0;
try
{
code = ftr.get();
}
catch (const std::exception& e)
{
std::cerr << "main thread threw exception: " << e.what() << std::endl;
code = 1;
}
catch (...)
{
std::cerr << "main thread threw non-standard exception" << std::endl;
code = 2;
}
llarp::LogContext::Instance().ImmediateFlush();
#ifdef _WIN32
::WSACleanup();
#endif
if (ctx)
{
2020-06-29 21:55:59 +02:00
ctx.reset();
}
2018-05-27 21:13:25 +02:00
return code;
2017-09-28 19:02:05 +02:00
}
#ifdef _WIN32
2020-08-12 21:53:29 +02:00
VOID ReportSvcStatus( DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwWaitHint)
{
static DWORD dwCheckPoint = 1;
// Fill in the SERVICE_STATUS structure.
SvcStatus.dwCurrentState = dwCurrentState;
SvcStatus.dwWin32ExitCode = dwWin32ExitCode;
SvcStatus.dwWaitHint = dwWaitHint;
if (dwCurrentState == SERVICE_START_PENDING)
SvcStatus.dwControlsAccepted = 0;
else SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
if ( (dwCurrentState == SERVICE_RUNNING) ||
(dwCurrentState == SERVICE_STOPPED) )
SvcStatus.dwCheckPoint = 0;
else SvcStatus.dwCheckPoint = dwCheckPoint++;
// Report the status of the service to the SCM.
SetServiceStatus( SvcStatusHandle, &SvcStatus );
}
VOID FAR PASCAL SvcCtrlHandler(DWORD dwCtrl)
{
// Handle the requested control code.
switch(dwCtrl)
{
case SERVICE_CONTROL_STOP:
ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
// Signal the service to stop.
handle_signal(SIGINT);
ReportSvcStatus(SvcStatus.dwCurrentState, NO_ERROR, 0);
return;
case SERVICE_CONTROL_INTERROGATE:
break;
default:
break;
}
}
// The win32 daemon entry point is just a trampoline that returns control
// to the original lokinet entry
2020-08-12 22:27:39 +02:00
// and only gets called if we get --win32-daemon in the command line
VOID FAR PASCAL win32_daemon_entry(DWORD largc, LPTSTR* largv)
{
UNREFERENCED_PARAMETER(largc);
UNREFERENCED_PARAMETER(largv);
2020-08-12 21:53:29 +02:00
// Register the handler function for the service
SvcStatusHandle = RegisterServiceCtrlHandler(
"lokinet",
SvcCtrlHandler);
if( !SvcStatusHandle )
{
llarp::LogError("failed to register daemon control handler");
return;
}
// These SERVICE_STATUS members remain as set here
SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
SvcStatus.dwServiceSpecificExitCode = 0;
// Report initial status to the SCM
ReportSvcStatus( SERVICE_START_PENDING, NO_ERROR, 3000 );
longjmp(svc_entry,0);
}
#endif