lokinet/daemon/main.cpp

185 lines
3.9 KiB
C++
Raw Normal View History

2018-12-12 02:47:29 +01:00
#include <config.hpp> // for ensure_config
#include <fs.hpp>
#include <getopt.h>
#include <libgen.h>
2018-05-27 20:03:10 +02:00
#include <llarp.h>
2018-12-12 02:47:29 +01:00
#include <logger.hpp>
2018-07-13 15:36:51 +02:00
#include <signal.h>
2018-12-12 02:47:29 +01:00
2018-07-20 06:50:28 +02:00
#include <string>
#include <iostream>
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)
{
2018-11-08 13:31:50 +01:00
std::cout << "usage: " << argv0 << " [-h] [-v] [-g|-c] config.ini"
<< std::endl;
return code;
}
#ifdef _WIN32
int
startWinsock()
{
WSADATA wsockd;
int err;
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
if(err)
{
perror("Failed to start Windows Sockets");
return err;
}
return 0;
}
#endif
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;
}
#ifdef _WIN32
if(startWinsock())
return -1;
#endif
int opt = 0;
bool genconfigOnly = false;
bool asRouter = false;
bool overWrite = false;
2018-11-08 13:31:50 +01:00
while((opt = getopt(argc, argv, "hgcfrv")) != -1)
{
switch(opt)
{
2018-11-08 13:31:50 +01:00
case 'v':
SetLogLevel(llarp::eLogDebug);
llarp::LogDebug("debug logging activated");
break;
case 'h':
return printHelp(argv[0], 0);
case 'g':
genconfigOnly = true;
break;
case 'c':
genconfigOnly = true;
break;
case 'r':
#ifdef _WIN32
llarp::LogWarn(
"please don't try this at home, the relay feature is untested on "
"windows server --R.");
#endif
asRouter = true;
break;
case 'f':
overWrite = true;
break;
default:
return printHelp(argv[0]);
}
}
2018-09-19 15:15:07 +02:00
std::string conffname;
if(optind < argc)
{
// when we have an explicit filepath
fs::path fname = fs::path(argv[optind]);
fs::path basedir = fname.parent_path();
2018-09-19 15:17:15 +02:00
conffname = fname.string();
2018-09-19 15:15:07 +02:00
if(basedir.string().empty())
{
2018-10-04 16:09:36 +02:00
if(!llarp_ensure_config(fname.string().c_str(), nullptr, overWrite,
asRouter))
return 1;
2018-09-19 15:15:07 +02:00
}
else
{
std::error_code ec;
if(!fs::create_directories(basedir, ec))
{
if(ec)
{
llarp::LogError("failed to create '", basedir.string(),
"': ", ec.message());
return 1;
}
}
2018-09-19 15:15:07 +02:00
if(!llarp_ensure_config(fname.string().c_str(), basedir.string().c_str(),
overWrite, asRouter))
2018-09-19 15:15:07 +02:00
return 1;
}
}
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;
// These paths are guaranteed to exist - $APPDATA or $HOME
// so only create .lokinet/*
if(!fs::create_directory(basepath, ec))
{
2018-09-19 15:32:50 +02:00
if(ec)
{
llarp::LogError("failed to create '", basepath.string(),
"': ", ec.message());
return 1;
}
}
if(!llarp_ensure_config(fpath.string().c_str(), basepath.string().c_str(),
overWrite, asRouter))
return 1;
2018-09-19 15:15:07 +02:00
conffname = fpath.string();
}
if(genconfigOnly)
return 0;
2018-07-13 15:36:51 +02:00
2018-09-19 15:15:07 +02:00
ctx = llarp_main_init(conffname.c_str(), 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-11-29 14:12:28 +01:00
signal(SIGTERM, 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-12-03 03:17:36 +01:00
code = llarp_main_setup(ctx);
if(code == 0)
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
}
#ifdef _WIN32
::WSACleanup();
#endif
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
}