From: Wouter Wijngaards Date: Thu, 29 Mar 2007 13:00:36 +0000 (+0000) Subject: Review changes. X-Git-Tag: release-0.2~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0bbc6205ee8fc550a66a51656775aa9cee0d0544;p=thirdparty%2Funbound.git Review changes. git-svn-id: file:///svn/unbound/trunk@209 be551aaa-1e26-0410-a405-d3ace91eadb9 --- diff --git a/daemon/daemon.c b/daemon/daemon.c index 5cfea217b..ae673550d 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -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; inum; 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; diff --git a/daemon/unbound.c b/daemon/unbound.c index 197e5d074..e03cf67c5 100644 --- a/daemon/unbound.c +++ b/daemon/unbound.c @@ -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; diff --git a/daemon/worker.h b/daemon/worker.h index b4bd8bd93..c3b82ae8c 100644 --- a/daemon/worker.h +++ b/daemon/worker.h @@ -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; }; /** diff --git a/doc/Changelog b/doc/Changelog index 6c752edd3..a964327bb 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -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. diff --git a/util/storage/slabhash.h b/util/storage/slabhash.h index 0ceee9264..c3515da88 100644 --- a/util/storage/slabhash.h +++ b/util/storage/slabhash.h @@ -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.