Skip to content

Commit

Permalink
daemonization.
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jul 2, 2014
1 parent 3a864cb commit 334c92b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 46 deletions.
31 changes: 14 additions & 17 deletions Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,27 @@ 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
logfile_path.append("/debug.log");
#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");
Expand Down Expand Up @@ -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;
}
}
Expand Down
33 changes: 18 additions & 15 deletions DaemonLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
Expand All @@ -101,12 +106,10 @@ namespace i2p

bool DaemonLinux::stop()
{
Daemon_Singleton::stop();

close(pidFilehandle);
unlink(pidfile.c_str());

return true;
return Daemon_Singleton::stop();
}

}
Expand Down
4 changes: 1 addition & 3 deletions Log.cpp
Original file line number Diff line number Diff line change
@@ -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 ()
Expand Down
30 changes: 27 additions & 3 deletions Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
12 changes: 8 additions & 4 deletions Queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace util
return m_Queue.empty ();
}

void WakeUp () { m_NonEmpty.notify_one (); };
void WakeUp () { m_NonEmpty.notify_all (); };

Element * Get ()
{
Expand Down Expand Up @@ -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; };
Expand Down
10 changes: 6 additions & 4 deletions i2p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 334c92b

Please sign in to comment.