{
cerr << "Missing option to initialize service. Protocol, host, login and password must be specified." << endl;
}
+
+
+void Logger::print_runnig_as_daemon(const int pid)
+{
+ cout << "Runnig as daemon: " << pid << endl;
+}
+
+void Logger::print_daemon_mode(const bool daemon_mode)
+{
+ string mode = "disabled";
+ if (daemon_mode == true)
+ mode = "enabled";
+ cout << "Daemon mode is " << mode << "." << endl;
+}
+
+
+void Logger::print_error_fork()
+{
+ cerr << "Error while trying to fork." << endl;
+}
+
+
+void Logger::print_pid_found(const int pid)
+{
+ cout << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
+}
+
+
+void Logger::print_process_already_running(const int pid)
+{
+ cerr << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
+}
void print_conf_not_loaded(const string&);
void print_missing_cmd_service_option();
+
+ void print_runnig_as_daemon(const int);
+
+ void print_daemon_mode(const bool);
+
+ void print_error_fork();
+
+ void print_pid_found(const int);
+
+ void print_process_already_running(const int);
};
#endif
#define REVISION 1
#define RELEASE 0
+#define PIDFILE "/var/run/bpdyndnsd.pid"
+
#include <iostream>
#include <list>
#include <string>
#include "dhs.cpp"
#include "ods.cpp"
+#include <sys/types.h>
+#include <signal.h>
+
+
using namespace std;
typedef boost::shared_ptr<Updater> UpdaterPtr;
/**
+ * Checks if a bpdyndnsd process is already running.
+ * @param updater Shared Pointer to updater, needed for logging.
+ * @return 0 if process not running already or pid of already running process.
+ */
+int check_for_running_process(UpdaterPtr updater)
+{
+ ifstream pidfile(PIDFILE);
+ if ( pidfile.is_open() )
+ {
+ int pid;
+ pidfile >> pid;
+ if ( pid > 0 )
+ updater->get_logger()->print_pid_found(pid);
+ else
+ return 0;
+
+ // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
+ if ( kill(pid,0) == 0)
+ {
+ updater->get_logger()->print_process_already_running(pid);
+ pidfile.close();
+ return pid;
+ }
+ }
+ pidfile.close();
+ return 0;
+}
+
+void write_pidfile(int pid)
+{
+ ofstream pidfile(PIDFILE);
+ if ( pidfile.is_open() )
+ {
+ pidfile << pid << endl;
+ }
+ pidfile.close();
+}
+
+/**
* @brief The main part.
* @param argc Number of arguments
* @param argv Command line arguments
if ( updater->init_config_from_files() != 0 )
return 0;
- cout << "Loglevel: " << updater->get_config()->get_loglevel() << endl;
+ // open pidfile and check for running process
+ if ( check_for_running_process(updater) != 0)
+ return 0;
// set the configured loggin facility, default stdout
// initialize daemon mode if configured
-
- // update all configured services
- updater->update_services();
+ bool daemon_mode = updater->get_config()->get_daemon_mode();
+ updater->get_logger()->print_daemon_mode(daemon_mode);
+
+ // fork if daemon mode is enabled
+ if ( daemon_mode == 1 )
+ {
+ int pid = fork();
+ if ( pid < 0 )
+ {
+ // error fork
+ updater->get_logger()->print_error_fork();
+ return 0;
+ }
+ else if ( pid > 0 )
+ {
+ // parent
+ write_pidfile(pid);
+ updater->get_logger()->print_runnig_as_daemon(pid);
+ return 0;
+ }
+ // child starts here
+ }
+
+ // service processing starts here
+ do
+ {
+ // update all configured services
+ updater->update_services();
+ sleep(3);
+
+ }while ( daemon_mode == 1 );
return 0;
}