lokinet/daemon/main.cpp

108 lines
2.2 KiB
C++
Raw Normal View History

2018-05-27 20:03:10 +02:00
#include <llarp.h>
#include <llarp/logger.hpp>
2018-07-13 15:36:51 +02:00
#include <signal.h>
#include <getopt.h>
2018-07-20 06:50:28 +02:00
#include <string>
#include <iostream>
#include <libgen.h>
#include "fs.hpp"
2018-05-20 18:15:16 +02:00
#ifdef _WIN32
#define wmin(x, y) (((x) < (y)) ? (x) : (y))
#define MIN wmin
#endif
2018-05-27 20:03:10 +02:00
struct llarp_main *ctx = 0;
2018-04-30 18:14:20 +02:00
void
handle_signal(int sig)
2018-05-18 19:50:21 +02:00
{
if(ctx)
2018-05-27 20:03:10 +02:00
llarp_main_signal(ctx, sig);
2018-05-18 19:50:21 +02:00
}
int
printHelp(const char *argv0, int code = 1)
{
std::cout << "usage: " << argv0 << " [-h] [-g] config.ini" << std::endl;
return code;
}
int
main(int argc, char *argv[])
{
2018-07-20 06:50:28 +02:00
bool multiThreaded = true;
const char *singleThreadVar = getenv("LLARP_SHADOW");
if(singleThreadVar && std::string(singleThreadVar) == "1")
{
multiThreaded = false;
}
int opt = 0;
bool genconfigOnly = false;
while((opt = getopt(argc, argv, "hg")) != -1)
{
switch(opt)
{
case 'h':
return printHelp(argv[0], 0);
case 'g':
genconfigOnly = true;
break;
default:
return printHelp(argv[0]);
}
}
const char *conffname = nullptr;
if(optind < argc)
{
// when we have an explicit filepath
if(!llarp_ensure_config(argv[optind], dirname(argv[optind]), genconfigOnly))
return 1;
conffname = argv[optind];
}
else
{
// no explicit config file provided
#ifdef _WIN32
fs::path homedir = fs::path(getenv("APPDATA"));
#else
fs::path homedir = fs::path(getenv("HOME"));
#endif
fs::path basepath = homedir / fs::path(".lokinet");
fs::path fpath = basepath / "lokinet.ini";
std::error_code ec;
if(!fs::create_directories(basepath, ec))
{
llarp::LogError("failed to create '", basepath.string(),
"': ", ec.message());
return 1;
}
if(!llarp_ensure_config(fpath.string().c_str(), basepath.string().c_str(),
genconfigOnly))
return 1;
conffname = fpath.string().c_str();
}
if(genconfigOnly)
return 0;
2018-07-13 15:36:51 +02:00
2018-07-20 06:50:28 +02:00
ctx = llarp_main_init(conffname, multiThreaded);
2018-05-27 21:13:25 +02:00
int code = 1;
if(ctx)
2018-05-27 20:03:10 +02:00
{
signal(SIGINT, handle_signal);
2018-09-19 13:57:07 +02:00
#ifndef _WIN32
2018-09-17 13:47:34 +02:00
signal(SIGHUP, handle_signal);
2018-09-19 13:57:07 +02:00
#endif
2018-05-27 21:13:25 +02:00
code = llarp_main_run(ctx);
2018-07-20 06:50:28 +02:00
llarp_main_free(ctx);
2018-05-27 20:03:10 +02:00
}
2018-08-12 19:22:29 +02:00
exit(code);
2018-05-27 21:13:25 +02:00
return code;
2017-09-28 19:02:05 +02:00
}