Introduced logging facility.
Improved option handling when reloading through SIGHUP.
Improved signal handling.
Sorry for big check in ;-). Next time there will be smaller ones.
* @license GPLv2
*/
-
#include "config.h"
+namespace po = boost::program_options;
+namespace fs = boost::filesystem;
+
+using namespace std;
/**
* Default Constructor. Available command line and config file options with their default values are defined here.
*/
Config::Config(LoggerPtr _log)
: Daemon_mode(false)
+ , Logfile("/var/log/bpdyndns.log")
, Loglevel(0)
, Syslog(false)
+ , Config_path("/etc/bpdyndnsd")
{
// initialize Logger
Log = _log;
}
Services.clear();
}
+
+
+/**
+ * Deletes the map with the previously parsed options.
+ * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
+ */
+void Config::delete_variables_map()
+{
+ Variables_map.clear();
+
+ po::variables_map _variables_map;
+ Variables_map = _variables_map;
+}
+
+
+/**
+ * Getter for member Syslog.
+ * @return True if logging through syslog is enabled, false otherwise.
+ */
+bool Config::get_syslog()
+{
+ return Syslog;
+}
#include "dhs.h"
#include "ods.h"
-namespace po = boost::program_options;
-namespace fs = boost::filesystem;
-
-using namespace std;
-
typedef boost::shared_ptr<Service> ServicePtr;
typedef boost::shared_ptr<Logger> LoggerPtr;
private:
- po::options_description *Opt_desc_cmd;
- po::options_description *Opt_desc_conf_main;
- po::options_description *Opt_desc_conf_service;
- po::variables_map Variables_map;
+ boost::program_options::options_description *Opt_desc_cmd;
+ boost::program_options::options_description *Opt_desc_conf_main;
+ boost::program_options::options_description *Opt_desc_conf_service;
+ boost::program_options::variables_map Variables_map;
- list<ServicePtr> Services;
+ std::list<ServicePtr> Services;
LoggerPtr Log;
bool Daemon_mode;
- string Logfile;
+ std::string Logfile;
int Loglevel;
bool Syslog;
- string Config_path;
+ std::string Config_path;
- ServicePtr create_service(const string&,const string&,const string&,const string&);
- int load_main_config_file(const string&);
- int load_service_config_file(const string&);
+ ServicePtr create_service(const std::string&,const std::string&,const std::string&,const std::string&);
+ int load_main_config_file(const std::string&);
+ int load_service_config_file(const std::string&);
public:
int load_config_from_files();
- list<ServicePtr> get_services();
+ std::list<ServicePtr> get_services();
- po::options_description* get_opt_desc_cmd();
+ boost::program_options::options_description* get_opt_desc_cmd();
- po::options_description* get_opt_desc_conf_main();
+ boost::program_options::options_description* get_opt_desc_conf_main();
- po::options_description* get_opt_desc_conf_service();
-
- int get_loglevel();
+ boost::program_options::options_description* get_opt_desc_conf_service();
bool get_daemon_mode();
void delete_services();
+ int get_loglevel();
+
+ bool get_syslog();
+
+ void delete_variables_map();
+
};
#endif
#include "dhs.h"
+using namespace std;
/**
* Constructor.
#include "service.h"
#include "logger.h"
-using namespace std;
-
typedef boost::shared_ptr<Logger> LoggerPtr;
class DHS : public Service
private:
- string Hostname;
- string Login;
- string Password;
+ std::string Hostname;
+ std::string Login;
+ std::string Password;
LoggerPtr Log;
public:
- DHS(const LoggerPtr&, const string&, const string&, const string&);
+ DHS(const LoggerPtr&, const std::string&, const std::string&, const std::string&);
~DHS();
- void update(const string&);
+ void update(const std::string&);
};
#include "logger.h"
+namespace po = boost::program_options;
+
+using namespace std;
/**
* Default Constructor
*/
Logger::Logger()
+ : Loglevel(0)
+ , Syslog(1)
{
print_constructor_call("Logger");
}
/**
+ * Setter for member Loglevel.
+ * @param _loglevel Value to set Loglevel to.
+ */
+void Logger::set_loglevel(const int _loglevel)
+{
+ Loglevel = _loglevel;
+}
+
+
+/**
+ * Getter for member Loglevel.
+ * @return Loglevel.
+ */
+int Logger::get_loglevel()
+{
+ return Loglevel;
+}
+
+
+/**
+ * Setter for member Syslog.
+ * @param _syslog Wether to log through syslog or not.
+ */
+void Logger::set_syslog(const bool _syslog)
+{
+ Syslog = _syslog;
+}
+
+
+/**
+ * Getter for member Syslog.
+ * @return True if logging through syslog is enabled, false otherwise.
+ */
+bool Logger::get_syslog()
+{
+ return Syslog;
+}
+
+
+/**
+ * Initialize the logging facility.
+ */
+void Logger::set_log_facility(const int _loglevel, const bool _syslog)
+{
+ Loglevel = _loglevel;
+ Syslog = _syslog;
+
+ if ( Syslog )
+ openlog("bpdyndnsd",LOG_PID,LOG_DAEMON);
+}
+
+
+/**
* Prints out the usage to the command line.
*/
void Logger::print_usage(const po::options_description* opt_desc)
{
cout << "Caught SIGHUP. Reloading config and switching to online mode..." << endl;
}
+
+
+/**
+ * Error while setting signal handler.
+ */
+void Logger::print_error_setting_signal()
+{
+ cerr << "Error while setting signal handler." << endl;
+}
+
+
+/**
+ * Error while setting signal handler.
+ */
+void Logger::print_init_log_facility()
+{
+ cout << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << endl;
+}
+
+/**
+ * Be verbose. Currently we are in offline mode.
+ */
+void Logger::print_offline_mode()
+{
+ cout << "Offline mode..." << endl;
+}
#ifndef LOGGER_H
#define LOGGER_H
-namespace po = boost::program_options;
+#include <syslog.h>
-using namespace std;
class Logger
{
+private:
+ int Loglevel;
+ bool Syslog;
public:
~Logger();
- void print_usage(const po::options_description*);
+ void set_loglevel(const int);
+
+ int get_loglevel();
+
+ void set_syslog(const bool);
+
+ bool get_syslog();
+
+ void set_log_facility(const int, const bool);
+
+ void print_usage(const boost::program_options::options_description*);
void print_version();
void print_cmd_parsed();
- void print_destructor_call(const string&);
+ void print_destructor_call(const std::string&);
- void print_constructor_call(const string&);
+ void print_constructor_call(const std::string&);
- void print_update_service(const string&);
+ void print_update_service(const std::string&);
- void print_unknown_cmd_option(const string&);
+ void print_unknown_cmd_option(const std::string&);
- void print_unknown_protocol(const string&);
+ void print_unknown_protocol(const std::string&);
- void print_load_service_conf(const string&);
+ void print_load_service_conf(const std::string&);
- void print_load_main_conf(const string&);
+ void print_load_main_conf(const std::string&);
- void print_unknown_service_conf_option(const string&);
+ void print_unknown_service_conf_option(const std::string&);
- void print_unknown_main_conf_option(const string&);
+ void print_unknown_main_conf_option(const std::string&);
- void print_error_opening(const string&);
+ void print_error_opening(const std::string&);
- void print_error_config_path(const string&);
+ void print_error_config_path(const std::string&);
- void print_conf_loaded(const string&);
+ void print_conf_loaded(const std::string&);
- void print_conf_not_loaded(const string&);
+ void print_conf_not_loaded(const std::string&);
void print_missing_cmd_service_option();
void print_caught_siguser1();
void print_caught_sighup();
+
+ void print_error_setting_signal();
+
+ void print_init_log_facility();
+
+ void print_offline_mode();
};
#endif
*/
void switch_to_offline(int param)
{
+ updater->get_logger()->print_caught_siguser1();
online_mode = 0;
}
*/
void reload_config(int param)
{
+ updater->get_logger()->print_caught_sighup();
updater->reload_config();
online_mode = 1;
}
/**
* Initialize the signals we handle.
*/
-void init_signals()
+int init_signals()
{
- signal(SIGTERM,terminate);
- signal(SIGUSR1,switch_to_offline);
- signal(SIGHUP,reload_config);
+ sighandler_t ret_val;
+ ret_val = signal(SIGTERM,terminate);
+ if ( ret_val == SIG_ERR )
+ return -1;
+ ret_val = signal(SIGUSR1,switch_to_offline);
+ if ( ret_val == SIG_ERR )
+ return -1;
+ ret_val = signal(SIGHUP,reload_config);
+ if ( ret_val == SIG_ERR )
+ return -1;
}
if ( updater->init_config_from_files() != 0 )
return 0;
+ // init the loggin facility, default stdout
+ updater->init_log_facility();
+
// open pidfile and check for running process
if ( check_for_running_process() != 0)
return 0;
// init signal handling
- init_signals();
-
- // set the configured loggin facility, default stdout
+ if ( init_signals() != 0)
+ {
+ updater->get_logger()->print_error_setting_signal();
+ return 0;
+ }
// initialize daemon mode if configured
bool daemon_mode = updater->get_config()->get_daemon_mode();
// update all configured services
updater->update_services();
}
- sleep(2);
+ else
+ {
+ updater->get_logger()->print_offline_mode();
+ }
+ sleep(4);
}while ( daemon_mode == 1 );
#include "ods.h"
+using namespace std;
/**
* Constructor.
#include "service.h"
#include "logger.h"
-using namespace std;
-
typedef boost::shared_ptr<Logger> LoggerPtr;
class ODS : public Service
private:
- string Hostname;
- string Login;
- string Password;
+ std::string Hostname;
+ std::string Login;
+ std::string Password;
LoggerPtr Log;
public:
- ODS(const LoggerPtr&, const string&, const string&, const string&);
+ ODS(const LoggerPtr&, const std::string&, const std::string&, const std::string&);
~ODS();
- void update(const string&);
+ void update(const std::string&);
};
#include <string>
-using namespace std;
-
class Service
{
public:
virtual ~Service();
- virtual void update(const string&)=0;
+ virtual void update(const std::string&)=0;
};
#include <boost/foreach.hpp>
+using namespace std;
/**
* Default constructor which initializes the member Conf.
*/
void Updater::reload_config()
{
+ // delete all service objects
Conf->delete_services();
+
+ // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
+ Conf->delete_variables_map();
+
+ // load only config files
init_config_from_files();
+
+ // re_init log facility, perhaps new config file options for logger are set.
+ init_log_facility();
+}
+
+
+void Updater::init_log_facility()
+{
+ Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog());
+ Log->print_init_log_facility();
}
LoggerPtr get_logger();
void reload_config();
+
+ void init_log_facility();
};
#endif