[Opendnssec-commits] [keihatsu.kirei.se/svn/dnssec] r3817 - in trunk/OpenDNSSEC/signer/src: daemon signer util

Matthijs Mekking matthijs at nlnetlabs.nl
Fri Aug 27 10:43:00 CEST 2010


Author: matthijs
Date: 2010-08-27 10:43:00 +0200 (Fri, 27 Aug 2010)
New Revision: 3817

Modified:
   trunk/OpenDNSSEC/signer/src/daemon/cfg.c
   trunk/OpenDNSSEC/signer/src/daemon/cfg.h
   trunk/OpenDNSSEC/signer/src/daemon/cmdhandler.c
   trunk/OpenDNSSEC/signer/src/daemon/engine.c
   trunk/OpenDNSSEC/signer/src/daemon/worker.c
   trunk/OpenDNSSEC/signer/src/signer/tools.c
   trunk/OpenDNSSEC/signer/src/signer/tools.h
   trunk/OpenDNSSEC/signer/src/signer/zone.c
   trunk/OpenDNSSEC/signer/src/util/se_malloc.c
Log:
config lock

Modified: trunk/OpenDNSSEC/signer/src/daemon/cfg.c
===================================================================
--- trunk/OpenDNSSEC/signer/src/daemon/cfg.c	2010-08-27 08:38:36 UTC (rev 3816)
+++ trunk/OpenDNSSEC/signer/src/daemon/cfg.c	2010-08-27 08:43:00 UTC (rev 3817)
@@ -85,6 +85,8 @@
 
         /* done */
         se_fclose(cfgfd);
+
+        lock_basic_init(&ecfg->config_lock);
         return ecfg;
     }
 
@@ -204,18 +206,52 @@
 {
     if (config) {
         se_log_debug("clean up config");
-        se_free((void*) config->cfg_filename);
-        se_free((void*) config->zonelist_filename);
-        se_free((void*) config->zonefetch_filename);
-        se_free((void*) config->log_filename);
-        se_free((void*) config->pid_filename);
-        se_free((void*) config->notify_command);
-        se_free((void*) config->clisock_filename);
-        se_free((void*) config->working_dir);
-        se_free((void*) config->username);
-        se_free((void*) config->group);
-        se_free((void*) config->chroot);
+        if (config->cfg_filename) {
+            se_free((void*) config->cfg_filename);
+            config->cfg_filename = NULL;
+        }
+        if (config->zonelist_filename) {
+            se_free((void*) config->zonelist_filename);
+            config->zonelist_filename = NULL;
+        }
+        if (config->zonefetch_filename) {
+            se_free((void*) config->zonefetch_filename);
+            config->zonefetch_filename = NULL;
+        }
+        if (config->log_filename) {
+            se_free((void*) config->log_filename);
+            config->zonefetch_filename = NULL;
+        }
+        if (config->pid_filename) {
+            se_free((void*) config->pid_filename);
+            config->pid_filename = NULL;
+        }
+        if (config->notify_command) {
+            se_free((void*) config->notify_command);
+            config->notify_command = NULL;
+        }
+        if (config->zonefetch_filename) {
+            se_free((void*) config->clisock_filename);
+            config->clisock_filename = NULL;
+        }
+        if (config->working_dir) {
+            se_free((void*) config->working_dir);
+            config->working_dir = NULL;
+        }
+        if (config->username) {
+            se_free((void*) config->username);
+            config->username = NULL;
+        }
+        if (config->group) {
+            se_free((void*) config->group);
+            config->group = NULL;
+        }
+        if (config->chroot) {
+            se_free((void*) config->chroot);
+            config->chroot = NULL;
+        }
         se_free((void*) config);
+        lock_basic_destroy(&config->config_lock);
     } else {
         se_log_warning("cleanup empty config");
     }

Modified: trunk/OpenDNSSEC/signer/src/daemon/cfg.h
===================================================================
--- trunk/OpenDNSSEC/signer/src/daemon/cfg.h	2010-08-27 08:38:36 UTC (rev 3816)
+++ trunk/OpenDNSSEC/signer/src/daemon/cfg.h	2010-08-27 08:43:00 UTC (rev 3817)
@@ -35,6 +35,7 @@
 #define DAEMON_CONFIG_H
 
 #include "config.h"
+#include "scheduler/locks.h"
 
 #include <stdio.h>
 
@@ -59,6 +60,8 @@
     int num_worker_threads;
     int num_signer_threads;
     int verbosity;
+
+    lock_basic_type config_lock;
 };
 
 /**

Modified: trunk/OpenDNSSEC/signer/src/daemon/cmdhandler.c
===================================================================
--- trunk/OpenDNSSEC/signer/src/daemon/cmdhandler.c	2010-08-27 08:38:36 UTC (rev 3816)
+++ trunk/OpenDNSSEC/signer/src/daemon/cmdhandler.c	2010-08-27 08:43:00 UTC (rev 3817)
@@ -156,6 +156,7 @@
     se_log_assert(tbd);
     se_log_assert(cmdc);
     se_log_assert(cmdc->engine);
+    se_log_assert(cmdc->engine->config);
     se_log_assert(cmdc->engine->tasklist);
 
     if (se_strcmp(tbd, "--all") == 0) {
@@ -167,9 +168,11 @@
     se_writen(sockfd, buf, strlen(buf));
 
     /* wake up sleeping workers */
+    lock_basic_lock(&cmdc->engine->config->config_lock);
     for (i=0; i < (size_t) cmdc->engine->config->num_worker_threads; i++) {
         worker_wakeup(cmdc->engine->workers[i]);
     }
+    lock_basic_unlock(&cmdc->engine->config->config_lock);
     return;
 }
 
@@ -191,6 +194,7 @@
     se_log_assert(tbd);
     se_log_assert(cmdc);
     se_log_assert(cmdc->engine);
+    se_log_assert(cmdc->engine->config);
     se_log_assert(cmdc->engine->tasklist);
 
     /* lock tasklist */
@@ -230,18 +234,22 @@
         se_writen(sockfd, buf, strlen(buf));
 
         /* wake up sleeping workers */
+        lock_basic_lock(&cmdc->engine->config->config_lock);
         for (i=0; i < (size_t) cmdc->engine->config->num_worker_threads; i++) {
             worker_wakeup(cmdc->engine->workers[i]);
         }
+        lock_basic_unlock(&cmdc->engine->config->config_lock);
     } else if (found && scheduled) {
         (void)snprintf(buf, ODS_SE_MAXLINE, "Zone %s scheduled for "
             "immediate re-sign.\n", tbd?tbd:"(null)");
         se_writen(sockfd, buf, strlen(buf));
 
         /* wake up sleeping workers */
+        lock_basic_lock(&cmdc->engine->config->config_lock);
         for (i=0; i < (size_t) cmdc->engine->config->num_worker_threads; i++) {
             worker_wakeup(cmdc->engine->workers[i]);
         }
+        lock_basic_unlock(&cmdc->engine->config->config_lock);
     } else if (found && !scheduled) {
         (void)snprintf(buf, ODS_SE_MAXLINE, "Zone %s not scheduled, "
             "already being signed right now!\n", tbd?tbd:"(null)");
@@ -357,6 +365,7 @@
 
     se_log_assert(cmdc);
     se_log_assert(cmdc->engine);
+    se_log_assert(cmdc->engine->config);
     se_log_assert(cmdc->engine->tasklist);
 
     lock_basic_lock(&cmdc->engine->tasklist->tasklist_lock);
@@ -364,9 +373,12 @@
     lock_basic_unlock(&cmdc->engine->tasklist->tasklist_lock);
 
     /* wake up sleeping workers */
+    lock_basic_lock(&cmdc->engine->config->config_lock);
     for (i=0; i < (size_t) cmdc->engine->config->num_worker_threads; i++) {
         worker_wakeup(cmdc->engine->workers[i]);
     }
+    lock_basic_unlock(&cmdc->engine->config->config_lock);
+
     (void)snprintf(buf, ODS_SE_MAXLINE, "All tasks scheduled immediately.\n");
     se_writen(sockfd, buf, strlen(buf));
     return;
@@ -449,8 +461,10 @@
     se_log_assert(cmdc->engine);
     se_log_assert(cmdc->engine->config);
 
+    lock_basic_lock(&cmdc->engine->config->config_lock);
     se_log_init(cmdc->engine->config->log_filename,
         cmdc->engine->config->use_syslog, val);
+    lock_basic_unlock(&cmdc->engine->config->config_lock);
 
     (void)snprintf(buf, ODS_SE_MAXLINE, "Verbosity level set to %i.\n", val);
     se_writen(sockfd, buf, strlen(buf));

Modified: trunk/OpenDNSSEC/signer/src/daemon/engine.c
===================================================================
--- trunk/OpenDNSSEC/signer/src/daemon/engine.c	2010-08-27 08:38:36 UTC (rev 3816)
+++ trunk/OpenDNSSEC/signer/src/daemon/engine.c	2010-08-27 08:43:00 UTC (rev 3817)
@@ -115,7 +115,6 @@
 engine_start_cmdhandler(engine_type* engine)
 {
     se_log_assert(engine);
-    se_log_assert(engine->config);
     se_log_debug("start command handler");
 
     engine->cmdhandler->engine = engine;
@@ -199,10 +198,13 @@
 static int
 engine_privdrop(engine_type* engine)
 {
+    int error;
+
     se_log_assert(engine);
     se_log_assert(engine->config);
     se_log_debug("drop privileges");
 
+    lock_basic_lock(&engine->config->config_lock);
     if (engine->config->username && engine->config->group) {
         se_log_verbose("drop privileges to user %s, group %s",
            engine->config->username, engine->config->group);
@@ -216,9 +218,10 @@
     if (engine->config->chroot) {
         se_log_verbose("chroot to %s", engine->config->chroot);
     }
-
-    return privdrop(engine->config->username, engine->config->group,
+    error = privdrop(engine->config->username, engine->config->group,
         engine->config->chroot);
+    lock_basic_unlock(&engine->config->config_lock);
+    return error;
 }
 
 
@@ -298,7 +301,7 @@
 
     se_log_assert(engine);
     se_log_assert(engine->config);
-
+    lock_basic_lock(&engine->config->config_lock);
     engine->workers = (worker_type**)
         se_calloc((size_t)engine->config->num_worker_threads,
         sizeof(worker_type*));
@@ -307,6 +310,7 @@
         engine->workers[i] = worker_create(i, WORKER_WORKER);
         engine->workers[i]->tasklist = engine->tasklist;
     }
+    lock_basic_unlock(&engine->config->config_lock);
     return;
 }
 
@@ -336,12 +340,14 @@
 
     se_log_assert(engine);
     se_log_assert(engine->config);
+    lock_basic_lock(&engine->config->config_lock);
     for (i=0; i < (size_t) engine->config->num_worker_threads; i++) {
         engine->workers[i]->need_to_exit = 0;
         engine->workers[i]->engineptr = (struct engine_struct*) engine;
         se_thread_create(&engine->workers[i]->thread_id, worker_thread_start,
             engine->workers[i]);
     }
+    lock_basic_unlock(&engine->config->config_lock);
     return;
 }
 
@@ -359,6 +365,8 @@
     se_log_assert(engine->config);
     se_log_debug("stop workers");
 
+    lock_basic_lock(&engine->config->config_lock);
+
     /* tell them to exit and wake up sleepyheads */
     for (i=0; i < (size_t) engine->config->num_worker_threads; i++) {
         engine->workers[i]->need_to_exit = 1;
@@ -369,6 +377,8 @@
         se_thread_join(engine->workers[i]->thread_id);
         engine->workers[i]->engineptr = NULL;
     }
+
+    lock_basic_unlock(&engine->config->config_lock);
     return;
 }
 
@@ -388,12 +398,15 @@
     se_log_debug("perform setup");
 
     /* create command handler (before chowning socket file) */
+    lock_basic_lock(&engine->config->config_lock);
     engine->cmdhandler = cmdhandler_create(engine->config->clisock_filename);
+    lock_basic_unlock(&engine->config->config_lock);
     if (!engine->cmdhandler) {
         return 1;
     }
 
     /* privdrop */
+    lock_basic_lock(&engine->config->config_lock);
     engine->uid = privuid(engine->config->username); /* LEAKS */
     engine->gid = privgid(engine->config->group); /* LEAKS */
     /* TODO: does piddir exists? */
@@ -412,8 +425,10 @@
         chdir(engine->config->working_dir) != 0) {
         se_log_error("setup failed: chdir to %s failed: %s",
             engine->config->working_dir, strerror(errno));
+        lock_basic_unlock(&engine->config->config_lock);
         return 1;
     }
+    lock_basic_unlock(&engine->config->config_lock);
 
     if (engine_privdrop(engine) != 0) {
         se_log_error("setup failed: unable to drop privileges");
@@ -444,10 +459,13 @@
     }
     engine->pid = getpid();
     /* make common with enforcer */
+    lock_basic_lock(&engine->config->config_lock);
     if (write_pidfile(engine->config->pid_filename, engine->pid) == -1) {
+        lock_basic_unlock(&engine->config->config_lock);
         se_log_error("setup failed: unable to write pid file");
         return 1;
     }
+    lock_basic_unlock(&engine->config->config_lock);
     se_log_verbose("running as pid %lu", (unsigned long) engine->pid);
 
     /* start command handler */
@@ -465,11 +483,14 @@
     sigaction(SIGTERM, &action, NULL);
 
     /* set up hsm */
+    lock_basic_lock(&engine->config->config_lock);
     result = hsm_open(engine->config->cfg_filename, hsm_prompt_pin, NULL); /* LEAKS */
     if (result != HSM_OK) {
+        lock_basic_unlock(&engine->config->config_lock);
         se_log_error("Error initializing libhsm (errno %i)", result);
         return 1;
     }
+    lock_basic_unlock(&engine->config->config_lock);
 
     /* set up the work floor */
     engine->tasklist = tasklist_create(); /* tasks */
@@ -547,8 +568,10 @@
     se_log_assert(engine->zonelist);
     se_log_debug("update zone list");
 
+    lock_basic_lock(&engine->config->config_lock);
     new_zlist = zonelist_read(engine->config->zonelist_filename,
         engine->zonelist->last_modified);
+    lock_basic_unlock(&engine->config->config_lock);
     if (!new_zlist) {
         if (buf) {
             /* fstat <= last_modified || rng check failed */
@@ -652,14 +675,25 @@
 {
     pid_t zfpid = 0;
     int result = 0;
+    char* zf_filename = NULL;
+    char* zl_filename = NULL;
+    char* log_filename = NULL;
+    char* grp = NULL;
+    char* usr = NULL;
+    char* chrt = NULL;
+    int use_syslog = 0;
+    int verbosity = 0;
 
     se_log_assert(engine);
     se_log_assert(engine->config);
 
+    lock_basic_lock(&engine->config->config_lock);
     if (!engine->config->zonefetch_filename) {
         /* zone fetcher disabled */
+        lock_basic_unlock(&engine->config->config_lock);
         return 0;
     }
+    lock_basic_unlock(&engine->config->config_lock);
 
     switch ((zfpid = fork())) {
         case -1: /* error */
@@ -681,13 +715,27 @@
 
     se_log_verbose("zone fetcher started (pid=%i)", getpid());
 
-    result = tools_zone_fetcher(engine->config->zonefetch_filename,
-        engine->config->zonelist_filename, engine->config->group,
-        engine->config->username, engine->config->chroot,
-        engine->config->log_filename, engine->config->use_syslog,
-        engine->config->verbosity);
+    lock_basic_lock(&engine->config->config_lock);
+    zf_filename = se_strdup(engine->config->zonefetch_filename);
+    zl_filename = se_strdup(engine->config->zonelist_filename);
+    grp = se_strdup(engine->config->group);
+    usr = se_strdup(engine->config->username);
+    chrt = se_strdup(engine->config->chroot);
+    log_filename = se_strdup(engine->config->log_filename);
+    use_syslog = engine->config->use_syslog;
+    verbosity = engine->config->verbosity;
+    lock_basic_unlock(&engine->config->config_lock);
 
+    result = tools_zone_fetcher(zf_filename, zl_filename, grp, usr,
+        chrt, log_filename, use_syslog, verbosity);
+
     se_log_verbose("zone fetcher stopped", result);
+    if (zf_filename)  { se_free((void*)zf_filename); }
+    if (zl_filename)  { se_free((void*)zl_filename); }
+    if (grp)          { se_free((void*)grp); }
+    if (usr)          { se_free((void*)usr); }
+    if (chrt)         { se_free((void*)chrt); }
+    if (log_filename) { se_free((void*)log_filename); }
 
     parent_cleanup(engine, 0);
     xmlCleanupParser();
@@ -711,7 +759,9 @@
     se_log_assert(engine);
     se_log_assert(engine->config);
 
+    lock_basic_lock(&engine->config->config_lock);
     if (engine->config->zonefetch_filename) {
+        lock_basic_unlock(&engine->config->config_lock);
         if (engine->zfpid > 0) {
             result = kill(engine->zfpid, SIGHUP);
             if (result == -1) {
@@ -724,6 +774,8 @@
             se_log_error("zone fetcher process id unknown, unable to "
                 "stop zone fetcher");
         }
+    } else {
+        lock_basic_unlock(&engine->config->config_lock);
     }
     return;
 }
@@ -803,8 +855,11 @@
     if (engine->cmdhandler != NULL) {
         engine_stop_cmdhandler(engine);
     }
+
+    lock_basic_lock(&engine->config->config_lock);
     (void)unlink(engine->config->pid_filename);
     (void)unlink(engine->config->clisock_filename);
+    lock_basic_unlock(&engine->config->config_lock);
 
 earlyexit:
     engine_cleanup(engine);
@@ -829,9 +884,11 @@
     if (engine) {
         se_log_debug("clean up engine");
         if (engine->workers) {
+            lock_basic_lock(&engine->config->config_lock);
             for (i=0; i < (size_t) engine->config->num_worker_threads; i++) {
                 worker_cleanup(engine->workers[i]);
             }
+            lock_basic_unlock(&engine->config->config_lock);
             se_free((void*) engine->workers);
         }
         if (engine->tasklist) {

Modified: trunk/OpenDNSSEC/signer/src/daemon/worker.c
===================================================================
--- trunk/OpenDNSSEC/signer/src/daemon/worker.c	2010-08-27 08:38:36 UTC (rev 3816)
+++ trunk/OpenDNSSEC/signer/src/daemon/worker.c	2010-08-27 08:43:00 UTC (rev 3817)
@@ -140,6 +140,9 @@
 {
     zone_type* zone = NULL;
     engine_type* engine = (engine_type*) worker->engineptr;
+    char* working_dir = NULL;
+    char* cfg_filename = NULL;
+    int error = 0;
 
     se_log_assert(worker);
     se_log_assert(task);
@@ -196,7 +199,16 @@
             }
             task->what = TASK_AUDIT;
         case TASK_AUDIT:
-            if (tools_audit(zone, engine->config) != 0) {
+            lock_basic_lock(&engine->config->config_lock);
+            working_dir = se_strdup(engine->config->working_dir);
+            cfg_filename = se_strdup(engine->config->cfg_filename);
+            lock_basic_unlock(&engine->config->config_lock);
+            error = tools_audit(zone, working_dir, cfg_filename);
+            if (working_dir)  { se_free((void*)working_dir); }
+            if (cfg_filename) { se_free((void*)cfg_filename); }
+            working_dir = NULL;
+            cfg_filename = NULL;
+            if (error) {
                 se_log_error("task [audit zone %s] failed",
                     task->who?task->who:"(null)");
                 task->what = TASK_SIGN;

Modified: trunk/OpenDNSSEC/signer/src/signer/tools.c
===================================================================
--- trunk/OpenDNSSEC/signer/src/signer/tools.c	2010-08-27 08:38:36 UTC (rev 3816)
+++ trunk/OpenDNSSEC/signer/src/signer/tools.c	2010-08-27 08:43:00 UTC (rev 3817)
@@ -180,7 +180,7 @@
  *
  */
 int
-tools_audit(zone_type* zone, engineconfig_type* config)
+tools_audit(zone_type* zone, char* working_dir, char* cfg_filename)
 {
     char* finalized = NULL;
     char str[SYSTEM_MAXLEN];
@@ -200,19 +200,12 @@
             return 1;
         }
 
-        if (config->working_dir) {
-            snprintf(str, SYSTEM_MAXLEN, "%s -c %s -s %s/%s -z %s > /dev/null",
-                ODS_SE_AUDITOR,
-                config->cfg_filename?config->cfg_filename:ODS_SE_CFGFILE,
-                config->working_dir, finalized?finalized:"(null)",
-                zone->name?zone->name:"(null)");
-        } else {
-            snprintf(str, SYSTEM_MAXLEN, "%s -c %s -s %s -z %s > /dev/null",
-                ODS_SE_AUDITOR,
-                config->cfg_filename?config->cfg_filename:ODS_SE_CFGFILE,
-                finalized?finalized:"(null)",
-                zone->name?zone->name:"(null)");
-        }
+        snprintf(str, SYSTEM_MAXLEN, "%s -c %s -s %s/%s -z %s > /dev/null",
+            ODS_SE_AUDITOR,
+            cfg_filename?cfg_filename:ODS_SE_CFGFILE,
+            working_dir?working_dir:"",
+            finalized?finalized:"(null)",
+            zone->name?zone->name:"(null)");
 
         se_log_debug("system call: %s", str);
         error = system(str);

Modified: trunk/OpenDNSSEC/signer/src/signer/tools.h
===================================================================
--- trunk/OpenDNSSEC/signer/src/signer/tools.h	2010-08-27 08:38:36 UTC (rev 3816)
+++ trunk/OpenDNSSEC/signer/src/signer/tools.h	2010-08-27 08:43:00 UTC (rev 3817)
@@ -81,11 +81,12 @@
 /**
  * Audit zone.
  * \param[in] zone zone
- * \param[in] config configuration settings
+ * \param[in] working_dir working directory
+ * \param[in] cfg_filename conf.xml filename
  * \return int 0 on success, 1 on fail
  *
  */
-int tools_audit(zone_type* zone, engineconfig_type* config);
+int tools_audit(zone_type* zone, char* working_dir, char* cfg_filename);
 
 /**
  * Write zone to output adapter.

Modified: trunk/OpenDNSSEC/signer/src/signer/zone.c
===================================================================
--- trunk/OpenDNSSEC/signer/src/signer/zone.c	2010-08-27 08:38:36 UTC (rev 3816)
+++ trunk/OpenDNSSEC/signer/src/signer/zone.c	2010-08-27 08:43:00 UTC (rev 3817)
@@ -683,8 +683,14 @@
             se_free((void*) zone->policy_name);
             zone->policy_name = NULL;
         }
-        se_free((void*) zone->signconf_filename);
-        se_free((void*) zone->name);
+        if (zone->signconf_filename) {
+            se_free((void*) zone->signconf_filename);
+            zone->signconf_filename = NULL;
+        }
+        if (zone->name) {
+            se_free((void*) zone->name);
+            zone->name = NULL;
+        }
 
         lock_basic_destroy(&zone->zone_lock);
         se_free((void*) zone);

Modified: trunk/OpenDNSSEC/signer/src/util/se_malloc.c
===================================================================
--- trunk/OpenDNSSEC/signer/src/util/se_malloc.c	2010-08-27 08:38:36 UTC (rev 3816)
+++ trunk/OpenDNSSEC/signer/src/util/se_malloc.c	2010-08-27 08:43:00 UTC (rev 3817)
@@ -116,9 +116,13 @@
 char*
 se_strdup(const char *s)
 {
-    char* dup = strdup(s);
-    if (!dup) {
-        se_fatal_exit("memory allocation failed (strdup): out of memory");
+    char* dup = NULL;
+
+    if (s) {
+        dup = strdup(s);
+        if (!dup) {
+            se_fatal_exit("memory allocation failed (strdup): out of memory");
+        }
     }
     return dup;
 }




More information about the Opendnssec-commits mailing list