From 6614919c13235410865495fd85b4ce70cb05fca4 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Mon, 4 Aug 2014 21:11:14 +0000 Subject: [PATCH] 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 --- core/config.cpp | 1 + core/config.h | 4 ++++ main/main.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/core/config.cpp b/core/config.cpp index 9f37010..1a797c4 100755 --- a/core/config.cpp +++ b/core/config.cpp @@ -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""); } diff --git a/core/config.h b/core/config.h index 8cbb7f8..69b91ef 100755 --- a/core/config.h +++ b/core/config.h @@ -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; diff --git a/main/main.cpp b/main/main.cpp index e83bd33..c13f2f6 100755 --- a/main/main.cpp +++ b/main/main.cpp @@ -10,14 +10,17 @@ #include #include #include +#include #include #include - +#include +#include #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; }