1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-01-11 17:37:53 +00:00

daemonization.

This commit is contained in:
orignal 2014-07-02 13:48:45 -04:00
parent 3a864cb6c5
commit 334c92bb49
6 changed files with 74 additions and 46 deletions

View File

@ -55,7 +55,17 @@ namespace i2p
i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"), i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"),
i2p::util::config::GetArg("-port", 17007)); i2p::util::config::GetArg("-port", 17007));
if (isLogging == 1) LogPrint("CMD parameters:");
for (int i = 0; i < argc; ++i)
LogPrint(i, " ", argv[i]);
return true;
}
bool Daemon_Singleton::start()
{
// initialize log
if (isLogging)
{ {
std::string logfile_path = i2p::util::filesystem::GetDataDir().string(); std::string logfile_path = i2p::util::filesystem::GetDataDir().string();
#ifndef _WIN32 #ifndef _WIN32
@ -63,18 +73,9 @@ namespace i2p
#else #else
logfile_path.append("\\debug.log"); logfile_path.append("\\debug.log");
#endif #endif
g_Log.SetLogFile (logfile_path); StartLog (logfile_path);
LogPrint("CMD parameters:");
for (int i = 0; i < argc; ++i)
LogPrint(i, " ", argv[i]);
} }
return true;
}
bool Daemon_Singleton::start()
{
d.httpServer = new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpport", 7070)); d.httpServer = new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpport", 7070));
d.httpServer->Start(); d.httpServer->Start();
LogPrint("HTTPServer started"); LogPrint("HTTPServer started");
@ -115,15 +116,11 @@ namespace i2p
LogPrint("NetDB stoped"); LogPrint("NetDB stoped");
d.httpServer->Stop(); d.httpServer->Stop();
LogPrint("HTTPServer stoped"); LogPrint("HTTPServer stoped");
StopLog ();
delete d.httpProxy; d.httpProxy = nullptr; delete d.httpProxy; d.httpProxy = nullptr;
delete d.httpServer; d.httpServer = nullptr; delete d.httpServer; d.httpServer = nullptr;
if (isLogging == 1)
{
fclose(stdout);
}
return true; return true;
} }
} }

View File

@ -48,24 +48,29 @@ namespace i2p
{ {
pid_t pid; pid_t pid;
pid = fork(); pid = fork();
if (pid > 0) if (pid > 0) // parent
{ ::exit (EXIT_SUCCESS);
g_Log.Stop();
return 0;
}
if (pid < 0)
{
return -1;
}
if (pid < 0) // error
return false;
// child
umask(0); umask(0);
int sid = setsid(); int sid = setsid();
if (sid < 0) if (sid < 0)
{ {
LogPrint("Error, could not create process group."); LogPrint("Error, could not create process group.");
return -1; return false;
} }
chdir(i2p::util::filesystem::GetDataDir().string().c_str()); chdir(i2p::util::filesystem::GetDataDir().string().c_str());
// close stdin/stdout/stderr descriptors
::close (0);
::open ("/dev/null", O_RDWR);
::close (1);
::open ("/dev/null", O_RDWR);
::close (2);
::open ("/dev/null", O_RDWR);
} }
// Pidfile // Pidfile
@ -75,12 +80,12 @@ namespace i2p
if (pidFilehandle == -1) if (pidFilehandle == -1)
{ {
LogPrint("Error, could not create pid file (", pidfile, ")\nIs an instance already running?"); LogPrint("Error, could not create pid file (", pidfile, ")\nIs an instance already running?");
return -1; return false;
} }
if (lockf(pidFilehandle, F_TLOCK, 0) == -1) if (lockf(pidFilehandle, F_TLOCK, 0) == -1)
{ {
LogPrint("Error, could not lock pid file (", pidfile, ")\nIs an instance already running?"); LogPrint("Error, could not lock pid file (", pidfile, ")\nIs an instance already running?");
return -1; return false;
} }
char pid[10]; char pid[10];
sprintf(pid, "%d\n", getpid()); sprintf(pid, "%d\n", getpid());
@ -101,12 +106,10 @@ namespace i2p
bool DaemonLinux::stop() bool DaemonLinux::stop()
{ {
Daemon_Singleton::stop();
close(pidFilehandle); close(pidFilehandle);
unlink(pidfile.c_str()); unlink(pidfile.c_str());
return true; return Daemon_Singleton::stop();
} }
} }

View File

@ -1,12 +1,10 @@
#include "Log.h" #include "Log.h"
Log g_Log; Log * g_Log = nullptr;
void LogMsg::Process() void LogMsg::Process()
{ {
output << s.str(); output << s.str();
std::cout << s.str (); // TODO: delete later
} }
void Log::Flush () void Log::Flush ()

30
Log.h
View File

@ -37,7 +37,25 @@ class Log: public i2p::util::MsgQueue<LogMsg>
std::ofstream * m_LogFile; std::ofstream * m_LogFile;
}; };
extern Log g_Log; extern Log * g_Log;
inline void StartLog (const std::string& fullFilePath)
{
if (!g_Log)
{
g_Log = new Log ();
g_Log->SetLogFile (fullFilePath);
}
}
inline void StopLog ()
{
if (g_Log)
{
delete g_Log;
g_Log = nullptr;
}
}
template<typename TValue> template<typename TValue>
void LogPrint (std::stringstream& s, TValue arg) void LogPrint (std::stringstream& s, TValue arg)
@ -55,10 +73,16 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
template<typename... TArgs> template<typename... TArgs>
void LogPrint (TArgs... args) void LogPrint (TArgs... args)
{ {
LogMsg * msg = g_Log.GetLogFile () ? new LogMsg (*g_Log.GetLogFile ()) : new LogMsg (); LogMsg * msg = (g_Log && g_Log->GetLogFile ()) ? new LogMsg (*g_Log->GetLogFile ()) : new LogMsg ();
LogPrint (msg->s, args...); LogPrint (msg->s, args...);
msg->s << std::endl; msg->s << std::endl;
g_Log.Put (msg); if (g_Log)
g_Log->Put (msg);
else
{
msg->Process ();
delete msg;
}
} }
#endif #endif

12
Queue.h
View File

@ -65,7 +65,7 @@ namespace util
return m_Queue.empty (); return m_Queue.empty ();
} }
void WakeUp () { m_NonEmpty.notify_one (); }; void WakeUp () { m_NonEmpty.notify_all (); };
Element * Get () Element * Get ()
{ {
@ -108,11 +108,15 @@ namespace util
typedef std::function<void()> OnEmpty; typedef std::function<void()> OnEmpty;
MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) {}; MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) {};
~MsgQueue () { Stop (); };
void Stop() void Stop()
{ {
m_IsRunning = false; if (m_IsRunning)
Queue<Msg>::WakeUp (); {
m_Thread.join(); m_IsRunning = false;
Queue<Msg>::WakeUp ();
m_Thread.join();
}
} }
void SetOnEmpty (OnEmpty const & e) { m_OnEmpty = e; }; void SetOnEmpty (OnEmpty const & e) { m_OnEmpty = e; };

10
i2p.cpp
View File

@ -5,11 +5,13 @@
int main( int argc, char* argv[] ) int main( int argc, char* argv[] )
{ {
Daemon.init(argc, argv); Daemon.init(argc, argv);
Daemon.start(); if (Daemon.start())
while (Daemon.running)
{ {
//TODO Meeh: Find something better to do here. while (Daemon.running)
std::this_thread::sleep_for (std::chrono::seconds(1)); {
//TODO Meeh: Find something better to do here.
std::this_thread::sleep_for (std::chrono::seconds(1));
}
} }
Daemon.stop(); Daemon.stop();
return EXIT_SUCCESS; return EXIT_SUCCESS;