added: possibility to save a pid file

new config option: pid_file (a full path to a pid file)



git-svn-id: svn://ttmath.org/publicrep/winix/trunk@957 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2014-08-04 21:11:14 +00:00
parent 8379acdb7f
commit 6614919c13
3 changed files with 48 additions and 2 deletions

View File

@ -267,6 +267,7 @@ void Config::AssignValues(bool stdout_is_closed)
incorrect_login_cannot_login_treshold = Size(L"incorrect_login_cannot_login_treshold", 20);
incorrect_login_cannot_login_delay = Size(L"incorrect_login_cannot_login_delay", 1800);
pid_file = Text(L"pid_file", L"");
}

View File

@ -620,6 +620,10 @@ public:
// will be taken accordingly
size_t incorrect_login_cannot_login_delay;
// pid file (a full path to a pid file)
// default: empty which means there is not a pid file used
// pid file is saved after winix has dropped privileges
std::wstring pid_file;

View File

@ -10,14 +10,17 @@
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <sys/param.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include "core/log.h"
#include "core/slog.h"
#include "core/app.h"
#include "core/plugin.h"
#include "core/version.h"
#include "utf8/utf8.h"
@ -94,6 +97,40 @@ void LogInfo(LogManipulators log_level, const char * msg, bool put_version, cons
}
void SavePidFile()
{
if( !app.config.pid_file.empty() )
{
std::string file_name;
PT::WideToUTF8(app.config.pid_file, file_name);
std::ofstream file(file_name);
if( !file )
{
log << log1 << "I cannot save the pid to a file: " << app.config.pid_file << logend;
}
else
{
file << getpid() << "\n";
file.close();
log << log3 << "Process pid saved to: " << app.config.pid_file << logend;
}
}
}
void RemovePidFile()
{
if( !app.config.pid_file.empty() )
{
std::string file_name;
PT::WideToUTF8(app.config.pid_file, file_name);
unlink(file_name.c_str());
}
}
} // namespace Winix
@ -140,8 +177,8 @@ using Winix::app;
return 3;
app.LogUserGroups();
Winix::log << Winix::log3 << "base_url: " << app.config.base_url << Winix::logend;
Winix::SavePidFile();
// load plugins before loading sessions - session_manager.LoadSessions()
// because some of the plugins can init its own sessions dates
@ -149,7 +186,10 @@ using Winix::app;
// app.Init() starts other threads as well (they will be waiting on the lock)
if( !app.Init() )
{
Winix::RemovePidFile();
return 1;
}
app.StartThreads();
// now we have more threads, we should use Lock() and Unlock()
@ -173,6 +213,7 @@ using Winix::app;
// now all others threads are terminated
Winix::LogInfo(Winix::log1, "Winix", true, "stopped");
Winix::RemovePidFile();
return 0;
}