Browse Source

daemonization.

pull/73/merge
orignal 11 years ago
parent
commit
334c92bb49
  1. 29
      Daemon.cpp
  2. 33
      DaemonLinux.cpp
  3. 4
      Log.cpp
  4. 30
      Log.h
  5. 6
      Queue.h
  6. 4
      i2p.cpp

29
Daemon.cpp

@ -55,26 +55,27 @@ namespace i2p @@ -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)
{
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]);
}
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
StartLog (logfile_path);
}
d.httpServer = new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpport", 7070));
d.httpServer->Start();
LogPrint("HTTPServer started");
@ -115,15 +116,11 @@ namespace i2p @@ -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;
}
}

33
DaemonLinux.cpp

@ -48,24 +48,29 @@ namespace i2p @@ -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 @@ -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 @@ -101,12 +106,10 @@ namespace i2p
bool DaemonLinux::stop()
{
Daemon_Singleton::stop();
close(pidFilehandle);
unlink(pidfile.c_str());
return true;
return Daemon_Singleton::stop();
}
}

4
Log.cpp

@ -1,12 +1,10 @@ @@ -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> @@ -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) @@ -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

6
Queue.h

@ -65,7 +65,7 @@ namespace util @@ -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,12 +108,16 @@ namespace util @@ -108,12 +108,16 @@ namespace util
typedef std::function<void()> OnEmpty;
MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) {};
~MsgQueue () { Stop (); };
void Stop()
{
if (m_IsRunning)
{
m_IsRunning = false;
Queue<Msg>::WakeUp ();
m_Thread.join();
}
}
void SetOnEmpty (OnEmpty const & e) { m_OnEmpty = e; };

4
i2p.cpp

@ -5,12 +5,14 @@ @@ -5,12 +5,14 @@
int main( int argc, char* argv[] )
{
Daemon.init(argc, argv);
Daemon.start();
if (Daemon.start())
{
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