From d0418fb869e5a95022888b44fe67a31f08986d61 Mon Sep 17 00:00:00 2001 From: Ben Summers Date: Mon, 30 Jan 2006 20:04:53 +0000 Subject: Merge chris/bb-save-state, resolving conflicts --- lib/server/Daemon.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++------- lib/server/Daemon.h | 13 +++++++- 2 files changed, 88 insertions(+), 13 deletions(-) (limited to 'lib/server') diff --git a/lib/server/Daemon.cpp b/lib/server/Daemon.cpp index a4dfdaec..9c820b22 100644 --- a/lib/server/Daemon.cpp +++ b/lib/server/Daemon.cpp @@ -9,14 +9,15 @@ #include "Box.h" +#include #include #include #include #include #include -#ifndef WIN32 -#include +#ifdef HAVE_SYSLOG_H + #include #endif #include "Daemon.h" @@ -24,6 +25,7 @@ #include "ServerException.h" #include "Guards.h" #include "UnixUser.h" +#include "FileModificationTime.h" #include "MemLeakFindOn.h" @@ -92,22 +94,21 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) } std::string pidFileName; - const char *configfile = 0; try { // Find filename of config file - configfile = DefaultConfigFile; + mConfigFileName = DefaultConfigFile; if(argc >= 2) { // First argument is config file, or it's -c and the next arg is the config file if(::strcmp(argv[1], "-c") == 0 && argc >= 3) { - configfile = argv[2]; + mConfigFileName = argv[2]; } else { - configfile = argv[1]; + mConfigFileName = argv[1]; } } @@ -123,19 +124,25 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) // Load the configuration file. std::string errors; - std::auto_ptr pconfig = Configuration::LoadAndVerify(configfile, GetConfigVerify(), errors); + std::auto_ptr pconfig = + Configuration::LoadAndVerify( + mConfigFileName.c_str(), + GetConfigVerify(), errors); // Got errors? if(pconfig.get() == 0 || !errors.empty()) { // Tell user about errors - fprintf(stderr, "%s: Errors in config file %s:\n%s", DaemonName(), configfile, errors.c_str()); + fprintf(stderr, "%s: Errors in config file %s:\n%s", + DaemonName(), mConfigFileName.c_str(), + errors.c_str()); // And give up return 1; } // Store configuration mpConfiguration = pconfig.release(); + mLoadedConfigModifiedTime = GetConfigFileModifiedTime(); // Server configuration const Configuration &serverConfig(mpConfiguration->GetSubConfiguration("Server")); @@ -228,7 +235,8 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) // open the log ::openlog(DaemonName(), LOG_PID, LOG_LOCAL6); // Log the start message - ::syslog(LOG_INFO, "Starting daemon (config: %s) (version " BOX_VERSION ")", configfile); + ::syslog(LOG_INFO, "Starting daemon (config: %s) (version " + BOX_VERSION ")", mConfigFileName.c_str()); #ifndef WIN32 // Write PID to file @@ -306,15 +314,23 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) if(mReloadConfigWanted && !mTerminateWanted) { // Need to reload that config file... - ::syslog(LOG_INFO, "Reloading configuration (config: %s)", configfile); + ::syslog(LOG_INFO, "Reloading configuration " + "(config: %s)", + mConfigFileName.c_str()); std::string errors; - std::auto_ptr pconfig = Configuration::LoadAndVerify(configfile, GetConfigVerify(), errors); + std::auto_ptr pconfig = + Configuration::LoadAndVerify( + mConfigFileName.c_str(), + GetConfigVerify(), errors); // Got errors? if(pconfig.get() == 0 || !errors.empty()) { // Tell user about errors - ::syslog(LOG_ERR, "Errors in config file %s:\n%s", configfile, errors.c_str()); + ::syslog(LOG_ERR, "Errors in config " + "file %s:\n%s", + mConfigFileName.c_str(), + errors.c_str()); // And give up return 1; } @@ -325,6 +341,8 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) // Store configuration mpConfiguration = pconfig.release(); + mLoadedConfigModifiedTime = + GetConfigFileModifiedTime(); // Stop being marked for loading config again mReloadConfigWanted = false; @@ -547,3 +565,49 @@ void Daemon::SetProcessTitle(const char *format, ...) #endif // HAVE_SETPROCTITLE } + + +// -------------------------------------------------------------------------- +// +// Function +// Name: Daemon::GetConfigFileModifiedTime() +// Purpose: Returns the timestamp when the configuration file +// was last modified +// +// Created: 2006/01/29 +// +// -------------------------------------------------------------------------- + +box_time_t Daemon::GetConfigFileModifiedTime() const +{ + struct stat st; + + if(::stat(GetConfigFileName().c_str(), &st) != 0) + { + if (errno == ENOENT) + { + return 0; + } + THROW_EXCEPTION(CommonException, OSFileError) + } + + return FileModificationTime(st); +} + +// -------------------------------------------------------------------------- +// +// Function +// Name: Daemon::GetLoadedConfigModifiedTime() +// Purpose: Returns the timestamp when the configuration file +// had been last modified, at the time when it was +// loaded +// +// Created: 2006/01/29 +// +// -------------------------------------------------------------------------- + +box_time_t Daemon::GetLoadedConfigModifiedTime() const +{ + return mLoadedConfigModifiedTime; +} + diff --git a/lib/server/Daemon.h b/lib/server/Daemon.h index a7b9488b..dbc83e98 100644 --- a/lib/server/Daemon.h +++ b/lib/server/Daemon.h @@ -16,6 +16,10 @@ #ifndef DAEMON__H #define DAEMON__H +#include + +#include "BoxTime.h" + class Configuration; class ConfigurationVerify; @@ -40,7 +44,8 @@ public: virtual void Run(); const Configuration &GetConfiguration() const; - + const std::string &GetConfigFileName() const {return mConfigFileName;} + virtual const char *DaemonName() const; virtual const char *DaemonBanner() const; virtual const ConfigurationVerify *GetConfigVerify() const; @@ -57,12 +62,18 @@ public: virtual void EnterChild(); static void SetProcessTitle(const char *format, ...); + +protected: + box_time_t GetLoadedConfigModifiedTime() const; private: static void SignalHandler(int sigraised); + box_time_t GetConfigFileModifiedTime() const; private: + std::string mConfigFileName; Configuration *mpConfiguration; + box_time_t mLoadedConfigModifiedTime; bool mReloadConfigWanted; bool mTerminateWanted; static Daemon *spDaemon; -- cgit v1.2.3