From 177dca91e2f4ab161afd5135934a9eff437c7f64 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 18 Jan 2019 08:24:33 -0500 Subject: [PATCH 1/2] add pidfile option --- include/llarp.hpp | 11 +++++++++++ llarp/config.cpp | 1 + llarp/context.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/include/llarp.hpp b/include/llarp.hpp index af052b5aa..6b1472d83 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -65,6 +65,16 @@ namespace llarp HandleSignal(int sig); private: + + void + SetPIDFile(const std::string & fname); + + bool + WritePIDFile() const; + + void + RemovePIDFile() const; + bool Configure(); @@ -82,6 +92,7 @@ namespace llarp progress(); std::string configfile; + std::string pidfile; }; } // namespace llarp diff --git a/llarp/config.cpp b/llarp/config.cpp index 7dd5d699b..cbac563b0 100644 --- a/llarp/config.cpp +++ b/llarp/config.cpp @@ -151,6 +151,7 @@ llarp_generic_ensure_config(std::ofstream &f, std::string basepath) f << "# "; #endif f << "group=" << DEFAULT_LOKINET_GROUP << std::endl; + f << "pidfile=" << basepath << "lokinet.pid" << std::endl; f << std::endl << std::endl; f << "# dns provider configuration section" << std::endl; diff --git a/llarp/context.cpp b/llarp/context.cpp index 7c8891342..b00db60db 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -53,6 +53,13 @@ namespace llarp const char *key, const char *val) { Context *ctx = static_cast< Context * >(itr->user); + if(!strcmp(section, "system")) + { + if(!strcmp(key, "pidfile")) + { + ctx->SetPIDFile(val); + } + } if(!strcmp(section, "router")) { if(!strcmp(key, "worker-threads") && !ctx->singleThreaded) @@ -81,6 +88,12 @@ namespace llarp } } + void + Context::SetPIDFile(const std::string & fname) + { + pidfile = fname; + } + int Context::LoadDatabase() { @@ -174,9 +187,12 @@ namespace llarp llarp::LogError("cannot run non configured context"); return 1; } + if(!WritePIDFile()) + return 1; // run if(!router->Run(nodedb)) return 1; + // run net io thread llarp::LogInfo("running mainloop"); llarp_ev_loop_run_single_process(mainloop, worker, logic); @@ -184,6 +200,34 @@ namespace llarp return 0; } + bool + Context::WritePIDFile() const + { + if(pidfile.size()) + { + std::ofstream f(pidfile); + f << std::to_string(getpid()); + return f.good(); + } + else + return true; + } + + void + Context::RemovePIDFile() const + { + if(pidfile.size()) + { + fs::path f = pidfile; + std::error_code ex; + if(fs::exists(f, ex)) + { + if(!ex) + fs::remove(f); + } + } + } + void Context::HandleSignal(int sig) { @@ -277,6 +321,7 @@ namespace llarp delete logic; logic = nullptr; } + RemovePIDFile(); } bool From 9f436174d497d6b416cfe5b5a46048caa012478c Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 18 Jan 2019 08:26:26 -0500 Subject: [PATCH 2/2] add openrc for freebsd --- contrib/freebsd/openrc/lokinet.rc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 contrib/freebsd/openrc/lokinet.rc diff --git a/contrib/freebsd/openrc/lokinet.rc b/contrib/freebsd/openrc/lokinet.rc new file mode 100644 index 000000000..67d8b336d --- /dev/null +++ b/contrib/freebsd/openrc/lokinet.rc @@ -0,0 +1,20 @@ +#!/bin/sh + +. /etc/rc.subr + +name=lokinet +rcvar=lokinet_enable + +command="/usr/local/bin/${name}" +command_args="/usr/local/etc/${name}/daemon.ini > /dev/null 2>&1" + +pidfile="/usr/local/etc/${name}/lokinet.pid" + +required_files="/usr/local/etc/${name}/daemon.ini" + +sig_reload="HUP" + +start_precmd="${command} -g /usr/local/etc/${name}/daemon.ini" + +load_rc_config $name +run_rc_command "$1" \ No newline at end of file