yuri r7646 - in branches/OpenDNSSEC-reload-rework: . enforcer-ng/src enforcer-ng/src/daemon

commits at svn.opendnssec.org commits at svn.opendnssec.org
Thu Feb 13 17:06:00 CET 2014


Author: yuri
Date: Thu Feb 13 17:06:00 2014
New Revision: 7646
URL: http://fisheye.opendnssec.org/changelog/opendnssec?cs=7646

Log:
Overhauling daemon structure to better support reloading

Added:
   branches/OpenDNSSEC-reload-rework/
      - copied from r7644, trunk/OpenDNSSEC/
Modified:
   branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/cmdhandler.c
   branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/engine.c
   branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/engine.h
   branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/signal.c
   branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/signal.h
   branches/OpenDNSSEC-reload-rework/enforcer-ng/src/ods-enforcerd.c

Modified: branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/cmdhandler.c
==============================================================================
--- trunk/OpenDNSSEC/enforcer-ng/src/daemon/cmdhandler.c	Wed Feb 12 14:24:39 2014	(r7644)
+++ branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/cmdhandler.c	Thu Feb 13 17:06:00 2014	(r7646)
@@ -420,6 +420,9 @@
                "                       the earliest scheduled task.\n"
 #endif
                "flush                  Execute all scheduled tasks immediately.\n"
+        );
+    ods_writen(sockfd, buf, strlen(buf));
+    (void) snprintf(buf, ODS_SE_MAXLINE,
                "running                Returns acknowledgment that the engine is running.\n"
                "reload                 Reload the engine.\n"
                "stop                   Stop the engine and terminate the process.\n"
@@ -463,6 +466,9 @@
                "                       the earliest scheduled task.\n"
 #endif
                "flush                  Execute all scheduled tasks immediately.\n"
+        );
+    ods_writen(sockfd, buf, strlen(buf));
+    (void) snprintf(buf, ODS_SE_MAXLINE,
                "running                Returns acknowledgment that the engine is running.\n"
                "reload                 Reload the engine.\n"
                "stop                   Stop the engine and terminate the process.\n"
@@ -743,7 +749,7 @@
     }
 
     ods_log_debug("[%s] done", module_str);
-    engine = cmdhandler->engine;
+    engine = cmdhandler->engine;/*todo check & remove me*/
     engine->cmdhandler_done = 1;
     return;
 }

Modified: branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/engine.c
==============================================================================
--- trunk/OpenDNSSEC/enforcer-ng/src/daemon/engine.c	Wed Feb 12 14:24:39 2014	(r7644)
+++ branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/engine.c	Thu Feb 13 17:06:00 2014	(r7646)
@@ -72,8 +72,8 @@
  * Create engine.
  *
  */
-static engine_type*
-engine_create(void)
+engine_type*
+engine_alloc(void)
 {
     engine_type* engine;
     allocator_type* allocator = allocator_create(malloc, free);
@@ -86,36 +86,37 @@
         return NULL;
     }
     engine->allocator = allocator;
-    engine->config = NULL;
-    engine->workers = NULL;
-    engine->cmdhandler = NULL;
-    engine->cmdhandler_done = 0;
-    engine->pid = -1;
-    engine->uid = -1;
-    engine->gid = -1;
-    engine->daemonize = 0;
-    engine->need_to_exit = 0;
-    engine->need_to_reload = 0;
-    engine->setup_error = 0;
 
-    engine->signal = SIGNAL_INIT;
     lock_basic_init(&engine->signal_lock);
     lock_basic_set(&engine->signal_cond);
 
     engine->taskq = schedule_create(engine->allocator);
     if (!engine->taskq) {
-        engine_cleanup(engine);
+        allocator_deallocate(allocator, (void*) engine);
         return NULL;
     }
     engine->signq = fifoq_create(engine->allocator);
     if (!engine->signq) {
-        engine_cleanup(engine);
+        schedule_cleanup(engine->taskq);
+        allocator_deallocate(allocator, (void*) engine);
+        allocator_cleanup(allocator);
         return NULL;
     }
     return engine;
 }
 
-
+void
+engine_dealloc(engine_type* engine)
+{
+    allocator_type* allocator = engine->allocator;
+    schedule_cleanup(engine->taskq);
+    fifoq_cleanup(engine->signq);
+    lock_basic_destroy(&engine->signal_lock);
+    lock_basic_off(&engine->signal_cond);
+    allocator_deallocate(allocator, (void*) engine);
+    allocator_cleanup(allocator);
+}
+    
 /**
  * Start command handler.
  *
@@ -332,16 +333,15 @@
  * Set up engine and return the setup status.
  *
  */
-static ods_status
-engine_setup_and_return_status(engine_type* engine)
+ods_status
+engine_setup(engine_type* engine)
 {
-    struct sigaction action;
     int fd;
 
     ods_log_debug("[%s] enforcer setup", engine_str);
-    if (!engine || !engine->config) {
-        return ODS_STATUS_ASSERT_ERR;
-    }
+
+    ods_log_init(engine->config->log_filename, 
+        engine->config->use_syslog, engine->config->verbosity);
 
     /* create command handler (before chowning socket file) */
     engine->cmdhandler = cmdhandler_create(engine->allocator,
@@ -389,9 +389,11 @@
                     (void)dup2(fd, STDERR_FILENO);
                     if (fd > 2) (void)close(fd);
                 }
+                engine->daemonize = 0; /* don't fork again on reload */
                 break;
             default: /* parent */
-                engine_cleanup(engine);
+                //~ engine_cleanup(engine);
+                /* TODO, clean egine, clean config, clean xml, PB */
                 engine = NULL;
                 xmlCleanupParser();
                 xmlCleanupGlobals();
@@ -409,19 +411,12 @@
     ods_log_verbose("[%s] running as pid %lu", engine_str,
         (unsigned long) engine->pid);
 
-    /* catch signals */
-    signal_set_engine(engine);
-    action.sa_handler = signal_handler;
-    sigfillset(&action.sa_mask);
-    action.sa_flags = 0;
-    sigaction(SIGHUP, &action, NULL);
-    sigaction(SIGTERM, &action, NULL);
-
     /* create workers */
     engine_create_workers(engine);
 
     /* start command handler */
-    engine_start_cmdhandler(engine);
+    engine->cmdhandler_done = 0;
+    //~ engine_start_cmdhandler(engine);
 
     /* write pidfile */
     if (util_write_pidfile(engine->config->pid_filename, engine->pid) == -1) {
@@ -434,83 +429,100 @@
 }
 
 /**
- * Set up engine.
+ * Clean up engine.
  *
  */
 void
-engine_setup(engine_type* engine, handled_xxxx_cmd_type *commands, 
-             help_xxxx_cmd_type *help)
+engine_teardown(engine_type* engine)
 {
-    ods_status status;
+    size_t i = 0;
 
-    engine->commands = commands;
-    engine->help = help;
-    status = engine_setup_and_return_status(engine);
-    if (status != ODS_STATUS_OK) {
-        ods_log_error("[%s] setup failed: %s", engine_str,
-                      ods_status2str(status));
-        engine->setup_error = 1;
-        if (status != ODS_STATUS_WRITE_PIDFILE_ERR) {
-            /* command handler had not yet been started */
-            engine->cmdhandler_done = 1;
+    if (!engine) return;
+    if (engine->config) {
+        if (engine->config->pid_filename) {
+            (void)unlink(engine->config->pid_filename);
+        }
+        if (engine->config->clisock_filename) {
+            (void)unlink(engine->config->clisock_filename);
+        }
+    }
+    if (engine->workers && engine->config) {
+        for (i=0; i < (size_t) engine->config->num_worker_threads; i++) {
+            worker_cleanup(engine->workers[i]);
         }
+        allocator_deallocate(engine->allocator, (void*) engine->workers);
     }
+    cmdhandler_cleanup(engine->cmdhandler);
+}
+
+void
+engine_init(engine_type* engine, int daemonize,
+    handled_xxxx_cmd_type *commands, help_xxxx_cmd_type *help)
+{
+    struct sigaction action;
+
+    engine->config = NULL;
+    engine->workers = NULL;
+    engine->cmdhandler = NULL;
+    engine->cmdhandler_done = 1;
+    engine->pid = -1;
+    engine->uid = -1;
+    engine->gid = -1;
+    engine->need_to_exit = 0;
+    engine->need_to_reload = 0;
+    engine->daemonize = daemonize;
+    engine->commands = commands;
+    engine->help = help;
+    /* catch signals */
+    signal_set_engine(engine);
+    action.sa_handler = signal_handler;
+    sigfillset(&action.sa_mask);
+    action.sa_flags = 0;
+    sigaction(SIGHUP, &action, NULL);
+    sigaction(SIGTERM, &action, NULL);
+    sigaction(SIGINT, &action, NULL);
 }
 
 /**
  * Run engine, run!.
  *
  */
-static void
+int
 engine_run(engine_type* engine, start_cb_t start, int single_run)
 {
-    if (!engine) {
-        return;
-    }
+    int error;
     ods_log_assert(engine);
-
+    ods_log_info("[%s] enforcer started", engine_str);
+    
+    error = hsm_open(engine->config->cfg_filename, hsm_prompt_pin);
+    if (error != HSM_OK) {
+        char* error =  hsm_get_error(NULL);
+        if (error != NULL) {
+            ods_log_error("[%s] %s", engine_str, error);
+            free(error);
+        } else {
+            ods_log_crit("[%s] error opening libhsm (errno %i)", engine_str,
+                error);
+        }
+        return 1;
+    }
+    
+    engine->need_to_reload = 0;
+    engine_start_cmdhandler(engine);
     engine_start_workers(engine);
 
-    lock_basic_lock(&engine->signal_lock);
-    /* [LOCK] signal */
-    engine->signal = SIGNAL_RUN;
-    /* [UNLOCK] signal */
-    lock_basic_unlock(&engine->signal_lock);
-
     /* call the external start callback function */
     start(engine);
     
     while (!engine->need_to_exit && !engine->need_to_reload) {
-        lock_basic_lock(&engine->signal_lock);
-        /* [LOCK] signal */
-        engine->signal = signal_capture(engine->signal);
-        switch (engine->signal) {
-            case SIGNAL_RUN:
-                ods_log_assert(1);
-                break;
-            case SIGNAL_RELOAD:
-                engine->need_to_reload = 1;
-                break;
-            case SIGNAL_SHUTDOWN:
-                engine->need_to_exit = 1;
-                break;
-            default:
-                ods_log_warning("[%s] invalid signal captured: %d, "
-                    "keep running", engine_str, signal);
-                engine->signal = SIGNAL_RUN;
-                break;
-        }
-        /* [UNLOCK] signal */
-        lock_basic_unlock(&engine->signal_lock);
-
         if (single_run) {
             engine->need_to_exit = 1;
             /* FIXME: all tasks need to terminate, then set need_to_exit to 1 */
         }
 
         lock_basic_lock(&engine->signal_lock);
-        /* [LOCK] signal */
-        if (engine->signal == SIGNAL_RUN && !single_run) {
+        /* [LOCK] signal, recheck reload and lock */
+        if (!engine->need_to_exit && !engine->need_to_reload && !single_run) {
            ods_log_debug("[%s] taking a break", engine_str);
            lock_basic_sleep(&engine->signal_cond, &engine->signal_lock, 0);
         }
@@ -519,180 +531,9 @@
     }
     ods_log_debug("[%s] enforcer halted", engine_str);
     engine_stop_workers(engine);
-    return;
-}
-
-
-/**
- * Engine runloop
- *
- */
-
-void 
-engine_runloop(engine_type* engine, start_cb_t start, int single_run)
-{
-    /* run */
-    while (engine->need_to_exit == 0) {
-        /* set up hsm */
-        /* TODO: On a reload the hsm configuration could have been 
-         * changed. Currently if we are unable to reopen the hsms we
-         * have no choice but bail out. We must change this and try
-         * to reopen the old repository list. hint: use hsm_open2 */
-        int result = hsm_open(engine->config->cfg_filename, hsm_prompt_pin);
-        if (result != HSM_OK) {
-            char* error =  hsm_get_error(NULL);
-            if (error != NULL) {
-                ods_log_error("[%s] %s", engine_str, error);
-                free(error);
-            } else {
-                ods_log_crit("[%s] error opening libhsm (errno %i)", engine_str,
-                    result);
-            }
-            engine->setup_error = 1;
-            break;
-        }
-    
-        if (engine->need_to_reload) {
-            ods_log_info("[%s] enforcer reloading", engine_str);
-            engine->need_to_reload = 0;
-        } else {
-            ods_log_info("[%s] enforcer started", engine_str);
-            /* try to recover from backups */
-            /* not for now:
-             engine_recover_from_backups(engine);
-             */
-        }
-        
-        engine_run(engine, start, single_run);
-        hsm_close();
-    }
-    
-    /* shutdown */
-    ods_log_info("[%s] enforcer shutdown", engine_str);
-    if (engine->cmdhandler != NULL) {
-        engine_stop_cmdhandler(engine);
-    }
-}
-
-
-/**
- * Start engine.
- *
- */
-engine_type *
-engine_start(const char* cfgfile, int cmdline_verbosity, int daemonize,
-    int info)
-{
-    engine_type* engine = NULL;
-    int use_syslog = 0;
-    ods_status status = ODS_STATUS_OK;
-
-    ods_log_assert(cfgfile);
-    ods_log_init(NULL, use_syslog, cmdline_verbosity);
-    ods_log_verbose("[%s] starting enforcer", engine_str);
-
-    /* initialize */
-    xmlInitGlobals();
-    xmlInitParser();
-    xmlInitThreads();
-    engine = engine_create();
-    if (!engine) {
-        ods_fatal_exit("[%s] create failed", engine_str);
-        return NULL;
-    }
-    engine->daemonize = daemonize;
-
-    /* config */
-    engine->config = engine_config(engine->allocator, cfgfile,
-        cmdline_verbosity);
-    status = engine_config_check(engine->config);
-    if (status != ODS_STATUS_OK) {
-        ods_log_error("[%s] cfgfile %s has errors", engine_str, cfgfile);
-        engine_stop(engine);
-        return NULL;
-    }
-    if (info) {
-        engine_config_print(stdout, engine->config); /* for debugging */
-        engine_stop(engine);
-        return NULL;
-    }
-
-    /* open log */
-    ods_log_init(engine->config->log_filename, engine->config->use_syslog,
-       engine->config->verbosity);
-
-    /* setup */
-    tzset(); /* for portability */
-
-    /* initialize protobuf and protobuf-orm */
-    ods_protobuf_initialize();
-    ods_orm_initialize();
-    
-    return engine;
-}
-
-
-/**
- * Stop engine.
- *
- */
-void
-engine_stop(engine_type *engine)
-{
-    ods_orm_shutdown();
-    ods_protobuf_shutdown();
-
-    if (engine && engine->config) {
-        if (engine->config->pid_filename) {
-            (void)unlink(engine->config->pid_filename);
-        }
-        if (engine->config->clisock_filename) {
-            (void)unlink(engine->config->clisock_filename);
-        }
-    }
-    engine_cleanup(engine);
-    engine = NULL;
-    ods_log_close();
-    xmlCleanupParser();
-    xmlCleanupGlobals();
-    xmlCleanupThreads();
-}
-
-/**
- * Clean up engine.
- *
- */
-void
-engine_cleanup(engine_type* engine)
-{
-    size_t i = 0;
-    allocator_type* allocator;
-    cond_basic_type signal_cond;
-    lock_basic_type signal_lock;
-
-    if (!engine) {
-        return;
-    }
-    allocator = engine->allocator;
-    signal_cond = engine->signal_cond;
-    signal_lock = engine->signal_lock;
-
-    if (engine->workers && engine->config) {
-        for (i=0; i < (size_t) engine->config->num_worker_threads; i++) {
-            worker_cleanup(engine->workers[i]);
-        }
-        allocator_deallocate(allocator, (void*) engine->workers);
-    }
-    schedule_cleanup(engine->taskq);
-    fifoq_cleanup(engine->signq);
-    cmdhandler_cleanup(engine->cmdhandler);
-    engine_config_cleanup(engine->config);
-    allocator_deallocate(allocator, (void*) engine);
-
-    lock_basic_destroy(&signal_lock);
-    lock_basic_off(&signal_cond);
-    allocator_cleanup(allocator);
-    return;
+    engine_stop_cmdhandler(engine);
+    (void) hsm_close();
+    return 0;
 }
 
 void

Modified: branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/engine.h
==============================================================================
--- trunk/OpenDNSSEC/enforcer-ng/src/daemon/engine.h	Wed Feb 12 14:24:39 2014	(r7644)
+++ branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/engine.h	Thu Feb 13 17:06:00 2014	(r7646)
@@ -80,9 +80,7 @@
     int daemonize;
     int need_to_exit;
     int need_to_reload;
-    int setup_error;
 
-    sig_atomic_t signal;
     cond_basic_type signal_cond;
     lock_basic_type signal_lock;
 };
@@ -97,12 +95,9 @@
  * \return engine_type* engine to use or NULL when engine couldn't start
  *
  */
-engine_type *engine_start(const char* cfgfile, int cmdline_verbosity,
-    int daemonize, int info);
-
 
 /**
- * Setup the engine started by engine_start
+ * Setup the engine started by engine_create
  * \param[in] engine the engine returned from engine_start
  * \param[in] commands NULL terminated list of command functions for 
  *            the engine that the command handler can run.
@@ -110,8 +105,17 @@
  *            for the command to a socket.
  */
 
-void engine_setup(engine_type *engine, handled_xxxx_cmd_type *commands,
-                  help_xxxx_cmd_type *help);
+ods_status engine_setup(engine_type* engine);
+/**
+ * Clean up engine.
+ * \param[in] engine engine
+ *
+ */
+void engine_teardown(engine_type* engine);
+
+void
+engine_init(engine_type* engine, int daemonize,
+    handled_xxxx_cmd_type *commands, help_xxxx_cmd_type *help);
 
 typedef void (*start_cb_t)(engine_type* engine);
 
@@ -121,9 +125,10 @@
  * the engine is ready to stop.
  * \param[in] engine the engine returned from engine_start
  * \param[in] single_run run once
+ * \return 0 if terminated normally, 1 on unrecoverable error.
  *
  */
-void engine_runloop(engine_type* engine, start_cb_t start, int single_run);
+int engine_run(engine_type* engine, start_cb_t start, int single_run);
 
 /**
  * Stop the engine after engine_runloop returns.
@@ -145,12 +150,10 @@
  * \param[in] engine engine */
 void engine_start_workers(engine_type* engine);
 
-/**
- * Clean up engine.
- * \param[in] engine engine
- *
- */
-void engine_cleanup(engine_type* engine);
+
+
+engine_type* engine_alloc(void);
+void engine_dealloc(engine_type* engine);
 
 /**
  * Set all task to immediate execution and wake up all workers.

Modified: branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/signal.c
==============================================================================
--- trunk/OpenDNSSEC/enforcer-ng/src/daemon/signal.c	Wed Feb 12 14:24:39 2014	(r7644)
+++ branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/signal.c	Thu Feb 13 17:06:00 2014	(r7646)
@@ -39,8 +39,6 @@
 
 #include <signal.h>
 
-static int signal_hup_recvd = 0;
-static int signal_term_recvd = 0;
 static engine_type* signal_engine = NULL;
 static const char* signal_str = "signal";
 
@@ -66,8 +64,8 @@
     switch (sig) {
         case SIGHUP:
             ods_log_debug("[%s] SIGHUP received", signal_str);
-            signal_hup_recvd++;
             if (signal_engine) {
+                signal_engine->need_to_reload = 1;
                 lock_basic_lock(&signal_engine->signal_lock);
                 /* [LOCK] signal */
                 lock_basic_alarm(&signal_engine->signal_cond);
@@ -75,10 +73,11 @@
                 lock_basic_unlock(&signal_engine->signal_lock);
             }
             break;
+        case SIGINT:
         case SIGTERM:
             ods_log_debug("[%s] SIGTERM received", signal_str);
-            signal_term_recvd++;
             if (signal_engine) {
+                signal_engine->need_to_exit = 1;
                 lock_basic_lock(&signal_engine->signal_lock);
                 /* [LOCK] signal */
                 lock_basic_alarm(&signal_engine->signal_cond);
@@ -87,25 +86,9 @@
             }
             break;
         default:
+            ods_log_debug("[%s] Spurious signal %d received", 
+                signal_str, sig);
             break;
     }
     return;
 }
-
-
-/**
- * Capture signal.
- *
- */
-sig_atomic_t
-signal_capture(sig_atomic_t dflsig)
-{
-    if (signal_term_recvd) {
-        signal_term_recvd = 0;
-        return SIGNAL_SHUTDOWN;
-    } else if (signal_hup_recvd) {
-        signal_hup_recvd = 0;
-        return SIGNAL_RELOAD;
-    }
-    return dflsig;
-}

Modified: branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/signal.h
==============================================================================
--- trunk/OpenDNSSEC/enforcer-ng/src/daemon/signal.h	Wed Feb 12 14:24:39 2014	(r7644)
+++ branches/OpenDNSSEC-reload-rework/enforcer-ng/src/daemon/signal.h	Thu Feb 13 17:06:00 2014	(r7646)
@@ -42,11 +42,6 @@
 extern "C" {
 #endif
 
-#define SIGNAL_RUN 0
-#define SIGNAL_INIT 1
-#define SIGNAL_RELOAD 2
-#define SIGNAL_SHUTDOWN 3
-
 struct engine_struct;
 
 /**

Modified: branches/OpenDNSSEC-reload-rework/enforcer-ng/src/ods-enforcerd.c
==============================================================================
--- trunk/OpenDNSSEC/enforcer-ng/src/ods-enforcerd.c	Wed Feb 12 14:24:39 2014	(r7644)
+++ branches/OpenDNSSEC-reload-rework/enforcer-ng/src/ods-enforcerd.c	Thu Feb 13 17:06:00 2014	(r7646)
@@ -33,7 +33,8 @@
 
 #include "config.h"
 #include "daemon/engine.h"
-
+#include "shared/protobuf.h"
+#include "daemon/orm.h"
 
 /* Pull in the commands that have been implemented for the enforcer */
 #include "enforcer/autostart_cmd.h"
@@ -74,10 +75,13 @@
 #include <getopt.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <libxml/parser.h>
+
 
 #define AUTHOR_NAME "Matthijs Mekking, Yuri Schaeffer, René Post"
 #define COPYRIGHT_STR "Copyright (C) 2010-2011 NLnet Labs OpenDNSSEC"
 
+static const char* enforcerd_str = "engine";
 
 /**
  * Prints usage.
@@ -216,6 +220,38 @@
     NULL
 };
 
+void
+program_setup(int cmdline_verbosity)
+{
+    /* for now just log to stderr */
+    ods_log_init(NULL, 0, cmdline_verbosity);
+    ods_log_verbose("[%s] starting enforcer", enforcerd_str);
+
+    /* initialize */
+    xmlInitGlobals();
+    xmlInitParser();
+    xmlInitThreads();
+    
+    /* setup */
+    tzset(); /* for portability */
+
+    /* initialize protobuf and protobuf-orm */
+    ods_protobuf_initialize();
+    ods_orm_initialize();
+}
+
+void
+program_teardown()
+{
+    ods_orm_shutdown();
+    ods_protobuf_shutdown();
+
+    ods_log_close();
+
+    xmlCleanupParser();
+    xmlCleanupGlobals();
+    xmlCleanupThreads();
+}
 
 /**
  * Main. start engine and run it.
@@ -224,6 +260,10 @@
 int
 main(int argc, char* argv[])
 {
+    ods_status status;
+    engine_type *engine;
+    engineconfig_type* cfg;
+    int returncode;
     int c;
     int options_index = 0;
     int info = 0;
@@ -233,9 +273,6 @@
     const char* cfgfile = ODS_SE_CFGFILE;
     static struct option long_options[] = {
         {"single-run", no_argument, 0, '1'},
-#if HAVE_READ_CONFIG_FROM_EXTERNAL_FILE
-        {"config", required_argument, 0, 'c'},
-#endif
         {"no-daemon", no_argument, 0, 'd'},
         {"help", no_argument, 0, 'h'},
         {"info", no_argument, 0, 'i'},
@@ -245,13 +282,7 @@
     };
 
     /* parse the commandline */
-    while ((c=getopt_long(argc, argv, 
-#if HAVE_READ_CONFIG_FROM_EXTERNAL_FILE
-                          "1c:dhivV"
-#else
-                          "1dhivV"
-#endif
-                          ,
+    while ((c=getopt_long(argc, argv, "1dhivV",
         long_options, &options_index)) != -1) {
         switch (c) {
             case '1':
@@ -263,7 +294,6 @@
             case 'h':
                 usage(stdout);
                 exit(0);
-                break;
             case 'i':
                 info = 1;
                 break;
@@ -273,11 +303,9 @@
             case 'V':
                 version(stdout);
                 exit(0);
-                break;
             default:
                 usage(stderr);
                 exit(2);
-                break;
         }
     }
     argc -= optind;
@@ -289,34 +317,75 @@
 
 #ifdef ENFORCER_TIMESHIFT
     if (getenv("ENFORCER_TIMESHIFT")) {
-        fprintf(stdout, "WARNING: timeshift %s detected, this is a fixed point in time.\n",
-            getenv("ENFORCER_TIMESHIFT"));
+        fprintf(stdout, "WARNING: timeshift %s detected, this is a"
+            " fixed point in time.\n", getenv("ENFORCER_TIMESHIFT"));
     } else {
         fprintf(stdout, "DEBUG: timeshift mode enabled, but not set.\n");
     }
 #endif /* ENFORCER_TIMESHIFT */
 
     /* main stuff */
-    fprintf(stdout, "OpenDNSSEC key and signing policy enforcer version %s\n", PACKAGE_VERSION);
+    fprintf(stdout, "OpenDNSSEC key and signing policy enforcer version %s\n", 
+        PACKAGE_VERSION);
     
-    {
-        engine_type *engine;
-        if ((engine = engine_start(cfgfile, cmdline_verbosity, daemonize, info))) {
-            engine_setup(engine,enforcer_commands,enforcer_help);
-            /* if setup fails we need a non-zero exit code */
-            if (engine->setup_error) {
-                fprintf(stdout, "Setup failed. Aborting.\n");
-                exit(3);
+    program_setup(cmdline_verbosity); /* setup basic logging, xml, PB */
+    engine = engine_alloc(); /* Let's create an engine only once */
+    if (!engine) {
+        ods_log_crit("Could not start engine");
+        program_teardown();
+        return 1;
+    }
+    engine_init(engine, daemonize, enforcer_commands, enforcer_help);
+    
+    returncode = 0;
+    while (!engine->need_to_exit) {
+        /* Parse config file */
+        cfg = engine_config(engine->allocator, cfgfile, cmdline_verbosity);
+        /* does it make sense? */
+        if (engine_config_check(cfg) != ODS_STATUS_OK) {
+            /* it does not, do we have a previous config loaded? */
+            if (engine->config) {
+                ods_log_error("[%s] cfgfile %s has errors, continuing"
+                    " with old config", enforcerd_str, cfgfile);
+            } else {
+                ods_log_crit("[%s] cfgfile %s has errors", enforcerd_str, cfgfile);
+                returncode = 2;
+                engine_config_cleanup(cfg); /* antagonist of engine_config() */
+                break;
             }
-            engine_runloop(engine,autostart,single_run);
-            if (engine->setup_error) {
-                fprintf(stdout, "Setup failed. Aborting.\n");
-                exit(4);
+        } else {
+            engine->config = cfg;
+        }
+        
+        /* Print config and exit */
+        if (info) {
+            engine_config_print(stdout, engine->config); /* for debugging */
+            engine_config_cleanup(engine->config);
+            break;
+        }
+        
+        /* do daemon housekeeping: pid, privdrop, fork, log */
+        if ((status = engine_setup(engine)) != ODS_STATUS_OK) {
+            ods_log_error("setup failed: %s", ods_status2str(status));
+            if (!daemonize)
+                fprintf(stderr, "setup failed: %s", ods_status2str(status));
+            returncode = 3;
+            engine->need_to_exit = 1;
+        } else {
+            if (engine_run(engine, autostart, single_run)) {
+                returncode = 4;
+                engine->need_to_exit = 1;
             }
-            engine_stop(engine);
+            engine_teardown(engine); /* antagonist of engine_setup() */
         }
+        engine_config_cleanup(engine->config); /* antagonist of engine_config() */
+        engine->config = NULL;
+        if (!engine->need_to_exit) 
+            ods_log_info("[%s] enforcer reloading", enforcerd_str);
     }
-
-    /* done */
-    return 0;
+    engine_dealloc(engine); /* antagonist of engine_alloc() */
+    ods_log_info("[%s] enforcerd stopped with exitcode %d", 
+        enforcerd_str, returncode);
+    program_teardown(); /* antagonist of program_setup() */
+    return returncode;
 }



More information about the Opendnssec-commits mailing list