Adjust PID file management

refs #5230
This commit is contained in:
Alexander A. Klimov 2019-07-15 16:08:08 +02:00
parent 368ebf4fbf
commit 06b504f291
3 changed files with 31 additions and 1 deletions

View File

@ -80,7 +80,9 @@ void Application::Stop(bool runtimeRemoved)
WSACleanup();
#endif /* _WIN32 */
#ifdef _WIN32
ClosePidFile(true);
#endif /* _WIN32 */
ObjectImpl<Application>::Stop(runtimeRemoved);
}
@ -964,6 +966,7 @@ int Application::Run()
SetConsoleCtrlHandler(&Application::CtrlHandler, TRUE);
#endif /* _WIN32 */
#ifdef _WIN32
try {
UpdatePidFile(Configuration::PidPath);
} catch (const std::exception&) {
@ -971,6 +974,7 @@ int Application::Run()
<< "Cannot update PID file '" << Configuration::PidPath << "'. Aborting.";
return EXIT_FAILURE;
}
#endif /* _WIN32 */
SetMainTime(Utility::GetTime());

View File

@ -132,7 +132,7 @@ private:
static int m_ArgC; /**< The number of command-line arguments. */
static char **m_ArgV; /**< Command-line arguments. */
FILE *m_PidFile; /**< The PID file */
FILE *m_PidFile = nullptr; /**< The PID file */
static bool m_Debugging; /**< Whether debugging is enabled. */
static LogSeverity m_DebuggingSeverity; /**< Whether debugging severity is set. */
static double m_StartTime;

View File

@ -7,6 +7,7 @@
#include "config/configcompiler.hpp"
#include "config/configcompilercontext.hpp"
#include "config/configitembuilder.hpp"
#include "base/defer.hpp"
#include "base/logger.hpp"
#include "base/application.hpp"
#include "base/timer.hpp"
@ -412,6 +413,15 @@ static pid_t StartUnixWorker(const std::vector<std::string>& configs)
return pid;
}
class PidFileManagementApp : public Application
{
public:
inline int Main() override
{
return EXIT_FAILURE;
}
};
#endif /* _WIN32 */
/**
@ -466,6 +476,22 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
Daemonize();
}
#ifndef _WIN32
PidFileManagementApp app;
try {
app.UpdatePidFile(Configuration::PidPath);
} catch (const std::exception&) {
Log(LogCritical, "Application")
<< "Cannot update PID file '" << Configuration::PidPath << "'. Aborting.";
return EXIT_FAILURE;
}
Defer closePidFile ([&app]() {
app.ClosePidFile(true);
});
#endif /* _WIN32 */
if (vm.count("daemonize") || vm.count("close-stdio")) {
// After disabling the console log, any further errors will go to the configured log only.
// Let's try to make this clear and say good bye.