add pidfile option

pull/236/head
Jeff Becker 5 years ago
parent ed297f68d9
commit 177dca91e2
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -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

@ -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;

@ -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

Loading…
Cancel
Save