daemonization.

pull/73/merge
orignal 10 years ago
parent 3a864cb6c5
commit 334c92bb49

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

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

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

30
Log.h

@ -37,7 +37,25 @@ class Log: public i2p::util::MsgQueue<LogMsg>
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>
void LogPrint (std::stringstream& s, TValue arg)
@ -55,10 +73,16 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
template<typename... TArgs>
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...);
msg->s << std::endl;
g_Log.Put (msg);
if (g_Log)
g_Log->Put (msg);
else
{
msg->Process ();
delete msg;
}
}
#endif

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

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

Loading…
Cancel
Save