]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
Review changes.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Thu, 29 Mar 2007 13:00:36 +0000 (13:00 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Thu, 29 Mar 2007 13:00:36 +0000 (13:00 +0000)
git-svn-id: file:///svn/unbound/trunk@209 be551aaa-1e26-0410-a405-d3ace91eadb9

daemon/daemon.c
daemon/unbound.c
daemon/worker.h
doc/Changelog
util/storage/slabhash.h

index 5cfea217b103bcfec2c2377ea8ab33969db5f8d9..ae673550d8de5651cd386f0d579edb68c9e5152e 100644 (file)
@@ -118,8 +118,9 @@ daemon_init()
        signal_handling_record();
        checklock_start();
        daemon->need_to_exit = 0;
-       daemon->msg_cache = slabhash_create(4, HASH_DEFAULT_STARTARRAY,
-               HASH_DEFAULT_MAXMEM, msgreply_sizefunc, query_info_compare,
+       daemon->msg_cache = slabhash_create(HASH_DEFAULT_SLABS, 
+               HASH_DEFAULT_STARTARRAY, HASH_DEFAULT_MAXMEM, 
+               msgreply_sizefunc, query_info_compare,
                query_entry_delete, reply_info_delete, NULL);
        if(!daemon->msg_cache) {
                free(daemon);
@@ -157,7 +158,7 @@ daemon_create_workers(struct daemon* daemon)
                sizeof(struct worker*));
        for(i=0; i<daemon->num; i++) {
                if(!(daemon->workers[i] = worker_create(daemon, i)))
-                       fatal_exit("malloc failure");
+                       fatal_exit("could not create worker");
        }
 }
 
@@ -191,7 +192,6 @@ static void*
 thread_start(void* arg)
 {
        struct worker* worker = (struct worker*)arg;
-       int num = worker->thread_num;
        log_thread_set(&worker->thread_num);
        ub_thread_blocksigs();
 #if !defined(HAVE_PTHREAD) && !defined(HAVE_SOLARIS_THREADS)
@@ -199,10 +199,10 @@ thread_start(void* arg)
        close(worker->cmd_send_fd);
        worker->cmd_send_fd = -1;
        close_other_pipes(worker->daemon, worker->thread_num);
-#endif /* no threads */
+#endif
        if(!worker_init(worker, worker->daemon->cfg, worker->daemon->ports,
                BUFSZ, 0))
-               fatal_exit("Could not initialize thread #%d", num);
+               fatal_exit("Could not initialize thread");
 
        worker_work(worker);
        return NULL;
index 197e5d07479089b1470f4cc5f547d40e2b6fc39d..e03cf67c5b7c0679f8030dbf6e71540953fcf5f5 100644 (file)
@@ -68,7 +68,7 @@ static void usage()
 static void
 apply_dir(struct daemon* daemon, struct config_file* cfg, int cmdline_verbose)
 {
-       /* apply changes if they have changed */
+       /* apply if they have changed */
        daemon->cfg = cfg;
        verbosity = cmdline_verbose + cfg->verbosity;
        if(cfg->directory && cfg->directory[0]) {
@@ -79,7 +79,7 @@ apply_dir(struct daemon* daemon, struct config_file* cfg, int cmdline_verbose)
                        }
                        free(daemon->cwd);
                        if(!(daemon->cwd = strdup(cfg->directory)))
-                               fatal_exit("malloc failed");
+                               log_err("cwd: malloc failed");
                }
        }
        if(cfg->msg_cache_size != slabhash_get_size(daemon->msg_cache) ||
@@ -106,10 +106,16 @@ readpid (const char* file)
        ssize_t l;
 
        if ((fd = open(file, O_RDONLY)) == -1) {
+               if(errno != ENOENT)
+                       log_err("Could not read pidfile %s: %s",
+                               file, strerror(errno));
                return -1;
        }
 
        if (((l = read(fd, pidbuf, sizeof(pidbuf)))) == -1) {
+               if(errno != ENOENT)
+                       log_err("Could not read pidfile %s: %s",
+                               file, strerror(errno));
                close(fd);
                return -1;
        }
@@ -118,7 +124,6 @@ readpid (const char* file)
 
        /* Empty pidfile means no pidfile... */
        if (l == 0) {
-               errno = ENOENT;
                return -1;
        }
 
@@ -156,13 +161,8 @@ static void
 checkoldpid(struct config_file* cfg)
 {
        pid_t old;
-       if((old = readpid(cfg->pidfile)) == -1) {
-               if(errno != ENOENT) {
-                       log_err("Could not read pidfile %s: %s",
-                               cfg->pidfile, strerror(errno));
-               }
-       } else {
-               /** see if it is still alive */
+       if((old = readpid(cfg->pidfile)) != -1) {
+               /* see if it is still alive */
                if(kill(old, 0) == 0 || errno == EPERM)
                        log_warn("unbound is already running as pid %u.", 
                                (unsigned)old);
@@ -171,6 +171,34 @@ checkoldpid(struct config_file* cfg)
        }
 }
 
+/** detach from command line */
+static void
+detach(struct config_file* cfg)
+{
+       int fd;
+       /* Take off... */
+       switch (fork()) {
+               case 0:
+                       break;
+               case -1:
+                       unlink(cfg->pidfile);
+                       fatal_exit("fork failed: %s", strerror(errno));
+               default:
+                       /* exit interactive session */
+                       exit(0);
+       }
+       /* detach */
+       if(setsid() == -1)
+               fatal_exit("setsid() failed: %s", strerror(errno));
+       if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
+               (void)dup2(fd, STDIN_FILENO);
+               (void)dup2(fd, STDOUT_FILENO);
+               (void)dup2(fd, STDERR_FILENO);
+               if (fd > 2)
+                       (void)close(fd);
+       }
+}
+
 /** daemonize, drop user priviliges and chroot if needed */
 static void
 do_chroot(struct daemon* daemon, struct config_file* cfg, int debug_mode)
@@ -199,32 +227,12 @@ do_chroot(struct daemon* daemon, struct config_file* cfg, int debug_mode)
        /* init logfile just before fork */
        log_init(cfg->logfile);
        if(!debug_mode && cfg->do_daemonize) {
-               int fd;
-               /* Take off... */
-               switch (fork()) {
-                       case 0:
-                               break;
-                       case -1:
-                               unlink(cfg->pidfile);
-                               fatal_exit("fork failed: %s", strerror(errno));
-                       default:
-                               /* exit interactive session */
-                               exit(0);
-               }
-               /* detach */
-               if(setsid() == -1)
-                       fatal_exit("setsid() failed: %s", strerror(errno));
-               if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
-                       (void)dup2(fd, STDIN_FILENO);
-                       (void)dup2(fd, STDOUT_FILENO);
-                       (void)dup2(fd, STDERR_FILENO);
-                       if (fd > 2)
-                               (void)close(fd);
-               }
+               detach(cfg);
        }
        if(cfg->pidfile && cfg->pidfile[0]) {
                writepid(cfg->pidfile, getpid());
-               daemon->pidfile = strdup(cfg->pidfile);
+               if(!(daemon->pidfile = strdup(cfg->pidfile)))
+                       log_err("pidf: malloc failed");
        }
 }
 
@@ -235,7 +243,8 @@ do_chroot(struct daemon* daemon, struct config_file* cfg, int debug_mode)
  *    These increase verbosity as specified in the config file.
  * @param debug_mode: if set, do not daemonize.
  */
-static void run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode)
+static void 
+run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode)
 {
        struct config_file* cfg = NULL;
        struct daemon* daemon = NULL;
index b4bd8bd932fe8271ab17933c34042ebaf1f862db..c3b82ae8c4f745a148bccd2ad442c1afae07a919 100644 (file)
@@ -76,12 +76,12 @@ struct work_query {
        struct query_info qinfo;
        /** hash value of the query qinfo */
        hashvalue_t query_hash;
+       /** next query in all-list */
+       struct work_query* all_next;
        /** id of query, in network byteorder. */
        uint16_t query_id;
        /** flags uint16 from query */
        uint16_t query_flags;
-       /** next query in all-list */
-       struct work_query* all_next;
 };
 
 /**
index 6c752edd3fa58c786e151966f5a2d6101b3f7d1f..a964327bb0601e70a9465461d45c4978ac4fcce7 100644 (file)
@@ -3,6 +3,7 @@
          This avoids a copy of the data.
        - do not do useless byteswap on query id. Store reply flags in uint16
          for easier access (and no repeated byteswapping).
+       - reviewed code.
 
 28 March 2007: Wouter
        - new config option: num-queries-per-thread.
index 0ceee926463fdf23f5c170a578c2f1071361376d..c3515da88dac263db356a25433de75f4fb256f5f 100644 (file)
@@ -45,6 +45,9 @@
 #define UTIL_STORAGE_SLABHASH_H
 #include "util/storage/lruhash.h"
 
+/** default number of slabs */
+#define HASH_DEFAULT_SLABS 4
+
 /**
  * Hash table formed from several smaller ones. 
  * This results in a partitioned lruhash table.