2 * daemon/daemon.c - collection of workers that handles requests.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
39 * The daemon consists of global settings and a number of workers.
43 #ifdef HAVE_OPENSSL_ERR_H
44 #include <openssl/err.h>
47 #ifdef HAVE_OPENSSL_RAND_H
48 #include <openssl/rand.h>
51 #ifdef HAVE_OPENSSL_CONF_H
52 #include <openssl/conf.h>
55 #ifdef HAVE_OPENSSL_ENGINE_H
56 #include <openssl/engine.h>
64 #include <ldns/ldns.h>
65 #include "daemon/daemon.h"
66 #include "daemon/worker.h"
67 #include "daemon/remote.h"
68 #include "daemon/acl_list.h"
70 #include "util/config_file.h"
71 #include "util/data/msgreply.h"
72 #include "util/storage/lookup3.h"
73 #include "util/storage/slabhash.h"
74 #include "services/listen_dnsport.h"
75 #include "services/cache/rrset.h"
76 #include "services/cache/infra.h"
77 #include "services/localzone.h"
78 #include "services/modstack.h"
79 #include "util/module.h"
80 #include "util/random.h"
81 #include "util/tube.h"
82 #include "util/net_help.h"
85 /** How many quit requests happened. */
86 static int sig_record_quit = 0;
87 /** How many reload requests happened. */
88 static int sig_record_reload = 0;
90 #if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS
91 /** cleaner ssl memory freeup */
92 static void* comp_meth = NULL;
94 #ifdef LEX_HAS_YYLEX_DESTROY
95 /** remove buffers for parsing and init */
96 int ub_c_lex_destroy(void);
99 /** used when no other sighandling happens, so we don't die
100 * when multiple signals in quick succession are sent to us.
101 * @param sig: signal number.
102 * @return signal handler return type (void or int).
104 static RETSIGTYPE record_sigh(int sig)
106 #ifdef LIBEVENT_SIGNAL_PROBLEM
107 verbose(VERB_OPS, "quit on signal, no cleanup and statistics, "
108 "because installed libevent version is not threadsafe");
133 log_err("ignoring signal %d", sig);
138 * Signal handling during the time when netevent is disabled.
139 * Stores signals to replay later.
142 signal_handling_record(void)
144 if( signal(SIGTERM, record_sigh) == SIG_ERR ||
146 signal(SIGQUIT, record_sigh) == SIG_ERR ||
149 signal(SIGBREAK, record_sigh) == SIG_ERR ||
152 signal(SIGHUP, record_sigh) == SIG_ERR ||
155 signal(SIGPIPE, SIG_IGN) == SIG_ERR ||
157 signal(SIGINT, record_sigh) == SIG_ERR
159 log_err("install sighandler: %s", strerror(errno));
163 * Replay old signals.
164 * @param wrk: worker that handles signals.
167 signal_handling_playback(struct worker* wrk)
170 if(sig_record_reload)
171 worker_sighandler(SIGHUP, wrk);
174 worker_sighandler(SIGTERM, wrk);
176 sig_record_reload = 0;
182 struct daemon* daemon = (struct daemon*)calloc(1,
183 sizeof(struct daemon));
191 r = WSAStartup(MAKEWORD(2,2), &wsa_data);
193 fatal_exit("could not init winsock. WSAStartup: %s",
196 #endif /* USE_WINSOCK */
197 signal_handling_record();
200 ERR_load_crypto_strings();
201 ERR_load_SSL_strings();
202 # ifdef HAVE_OPENSSL_CONFIG
203 OPENSSL_config("unbound");
206 (void)ldns_key_EVP_load_gost_id();
208 OpenSSL_add_all_algorithms();
209 # if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS
210 /* grab the COMP method ptr because openssl leaks it */
211 comp_meth = (void*)SSL_COMP_get_compression_methods();
213 (void)SSL_library_init();
214 # if defined(HAVE_SSL) && defined(OPENSSL_THREADS) && !defined(THREADS_DISABLED)
215 if(!ub_openssl_lock_init())
216 fatal_exit("could not init openssl locks");
218 #elif defined(HAVE_NSS)
219 if(NSS_NoDB_Init(NULL) != SECSuccess)
220 fatal_exit("could not init NSS");
221 #endif /* HAVE_SSL or HAVE_NSS */
223 /* init timezone info while we are not chrooted yet */
226 /* open /dev/random if needed */
227 ub_systemseed((unsigned)time(NULL)^(unsigned)getpid()^0xe67);
228 daemon->need_to_exit = 0;
229 modstack_init(&daemon->mods);
230 if(!(daemon->env = (struct module_env*)calloc(1,
231 sizeof(*daemon->env)))) {
235 alloc_init(&daemon->superalloc, NULL, 0);
236 daemon->acl = acl_list_create();
242 if(gettimeofday(&daemon->time_boot, NULL) < 0)
243 log_err("gettimeofday: %s", strerror(errno));
244 daemon->time_last_stat = daemon->time_boot;
249 daemon_open_shared_ports(struct daemon* daemon)
252 if(daemon->cfg->port != daemon->listening_port) {
253 listening_ports_free(daemon->ports);
254 if(!(daemon->ports=listening_ports_open(daemon->cfg)))
256 daemon->listening_port = daemon->cfg->port;
258 if(!daemon->cfg->remote_control_enable && daemon->rc_port) {
259 listening_ports_free(daemon->rc_ports);
260 daemon->rc_ports = NULL;
263 if(daemon->cfg->remote_control_enable &&
264 daemon->cfg->control_port != daemon->rc_port) {
265 listening_ports_free(daemon->rc_ports);
266 if(!(daemon->rc_ports=daemon_remote_open_ports(daemon->cfg)))
268 daemon->rc_port = daemon->cfg->control_port;
274 * Setup modules. setup module stack.
275 * @param daemon: the daemon
277 static void daemon_setup_modules(struct daemon* daemon)
279 daemon->env->cfg = daemon->cfg;
280 daemon->env->alloc = &daemon->superalloc;
281 daemon->env->worker = NULL;
282 daemon->env->need_to_validate = 0; /* set by module init below */
283 if(!modstack_setup(&daemon->mods, daemon->cfg->module_conf,
285 fatal_exit("failed to setup modules");
290 * Obtain allowed port numbers, concatenate the list, and shuffle them
291 * (ready to be handed out to threads).
292 * @param daemon: the daemon. Uses rand and cfg.
293 * @param shufport: the portlist output.
294 * @return number of ports available.
296 static int daemon_get_shufport(struct daemon* daemon, int* shufport)
300 for(i=0; i<65536; i++) {
301 if(daemon->cfg->outgoing_avail_ports[i]) {
302 shufport[avail++] = daemon->cfg->
303 outgoing_avail_ports[i];
307 fatal_exit("no ports are permitted for UDP, add "
308 "with outgoing-port-permit");
312 k = ub_random_max(daemon->rand, n+1); /* 0<= k<= n */
314 shufport[k] = shufport[n];
321 * Allocate empty worker structures. With backptr and thread-number,
322 * from 0..numthread initialised. Used as user arguments to new threads.
323 * Creates the daemon random generator if it does not exist yet.
324 * The random generator stays existing between reloads with a unique state.
325 * @param daemon: the daemon with (new) config settings.
328 daemon_create_workers(struct daemon* daemon)
332 log_assert(daemon && daemon->cfg);
334 unsigned int seed = (unsigned int)time(NULL) ^
335 (unsigned int)getpid() ^ 0x438;
336 daemon->rand = ub_initstate(seed, NULL);
338 fatal_exit("could not init random generator");
340 hash_set_raninit((uint32_t)ub_random(daemon->rand));
341 shufport = (int*)calloc(65536, sizeof(int));
343 fatal_exit("out of memory during daemon init");
344 numport = daemon_get_shufport(daemon, shufport);
345 verbose(VERB_ALGO, "total of %d outgoing ports available", numport);
347 daemon->num = (daemon->cfg->num_threads?daemon->cfg->num_threads:1);
348 daemon->workers = (struct worker**)calloc((size_t)daemon->num,
349 sizeof(struct worker*));
350 for(i=0; i<daemon->num; i++) {
351 if(!(daemon->workers[i] = worker_create(daemon, i,
352 shufport+numport*i/daemon->num,
353 numport*(i+1)/daemon->num - numport*i/daemon->num)))
354 /* the above is not ports/numthr, due to rounding */
355 fatal_exit("could not create worker");
360 #ifdef THREADS_DISABLED
362 * Close all pipes except for the numbered thread.
363 * @param daemon: daemon to close pipes in.
364 * @param thr: thread number 0..num-1 of thread to skip.
366 static void close_other_pipes(struct daemon* daemon, int thr)
369 for(i=0; i<daemon->num; i++)
372 /* only close read part, need to write stats */
373 tube_close_read(daemon->workers[i]->cmd);
375 /* complete close channel to others */
376 tube_delete(daemon->workers[i]->cmd);
377 daemon->workers[i]->cmd = NULL;
381 #endif /* THREADS_DISABLED */
384 * Function to start one thread.
385 * @param arg: user argument.
386 * @return: void* user return value could be used for thread_join results.
389 thread_start(void* arg)
391 struct worker* worker = (struct worker*)arg;
392 log_thread_set(&worker->thread_num);
393 ub_thread_blocksigs();
394 #ifdef THREADS_DISABLED
395 /* close pipe ends used by main */
396 tube_close_write(worker->cmd);
397 close_other_pipes(worker->daemon, worker->thread_num);
399 if(!worker_init(worker, worker->daemon->cfg, worker->daemon->ports, 0))
400 fatal_exit("Could not initialize thread");
407 * Fork and init the other threads. Main thread returns for special handling.
408 * @param daemon: the daemon with other threads to fork.
411 daemon_start_others(struct daemon* daemon)
415 verbose(VERB_ALGO, "start threads");
416 /* skip i=0, is this thread */
417 for(i=1; i<daemon->num; i++) {
418 ub_thread_create(&daemon->workers[i]->thr_id,
419 thread_start, daemon->workers[i]);
420 #ifdef THREADS_DISABLED
421 /* close pipe end of child */
422 tube_close_read(daemon->workers[i]->cmd);
423 #endif /* no threads */
428 * Stop the other threads.
429 * @param daemon: the daemon with other threads.
432 daemon_stop_others(struct daemon* daemon)
436 verbose(VERB_ALGO, "stop threads");
437 /* skip i=0, is this thread */
438 /* use i=0 buffer for sending cmds; because we are #0 */
439 for(i=1; i<daemon->num; i++) {
440 worker_send_cmd(daemon->workers[i], worker_cmd_quit);
442 /* wait for them to quit */
443 for(i=1; i<daemon->num; i++) {
444 /* join it to make sure its dead */
445 verbose(VERB_ALGO, "join %d", i);
446 ub_thread_join(daemon->workers[i]->thr_id);
447 verbose(VERB_ALGO, "join success %d", i);
452 daemon_fork(struct daemon* daemon)
455 if(!acl_list_apply_cfg(daemon->acl, daemon->cfg))
456 fatal_exit("Could not setup access control list");
457 if(!(daemon->local_zones = local_zones_create()))
458 fatal_exit("Could not create local zones: out of memory");
459 if(!local_zones_apply_cfg(daemon->local_zones, daemon->cfg))
460 fatal_exit("Could not set up local zones");
463 daemon_setup_modules(daemon);
465 /* first create all the worker structures, so we can pass
466 * them to the newly created threads.
468 daemon_create_workers(daemon);
470 #if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
471 /* in libev the first inited base gets signals */
472 if(!worker_init(daemon->workers[0], daemon->cfg, daemon->ports, 1))
473 fatal_exit("Could not initialize main thread");
476 /* Now create the threads and init the workers.
477 * By the way, this is thread #0 (the main thread).
479 daemon_start_others(daemon);
481 /* Special handling for the main thread. This is the thread
482 * that handles signals and remote control.
484 #if !(defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP))
485 /* libevent has the last inited base get signals (or any base) */
486 if(!worker_init(daemon->workers[0], daemon->cfg, daemon->ports, 1))
487 fatal_exit("Could not initialize main thread");
489 signal_handling_playback(daemon->workers[0]);
491 /* Start resolver service on main thread. */
492 log_info("start of service (%s).", PACKAGE_STRING);
493 worker_work(daemon->workers[0]);
494 log_info("service stopped (%s).", PACKAGE_STRING);
496 /* we exited! a signal happened! Stop other threads */
497 daemon_stop_others(daemon);
499 daemon->need_to_exit = daemon->workers[0]->need_to_exit;
503 daemon_cleanup(struct daemon* daemon)
507 /* before stopping main worker, handle signals ourselves, so we
508 don't die on multiple reload signals for example. */
509 signal_handling_record();
510 log_thread_set(NULL);
511 /* clean up caches because
512 * a) RRset IDs will be recycled after a reload, causing collisions
513 * b) validation config can change, thus rrset, msg, keycache clear
514 * The infra cache is kept, the timing and edns info is still valid */
515 slabhash_clear(&daemon->env->rrset_cache->table);
516 slabhash_clear(daemon->env->msg_cache);
517 local_zones_delete(daemon->local_zones);
518 daemon->local_zones = NULL;
519 /* key cache is cleared by module desetup during next daemon_init() */
520 daemon_remote_clear(daemon->rc);
521 for(i=0; i<daemon->num; i++)
522 worker_delete(daemon->workers[i]);
523 free(daemon->workers);
524 daemon->workers = NULL;
530 daemon_delete(struct daemon* daemon)
534 modstack_desetup(&daemon->mods, daemon->env);
535 daemon_remote_delete(daemon->rc);
536 listening_ports_free(daemon->ports);
537 listening_ports_free(daemon->rc_ports);
539 slabhash_delete(daemon->env->msg_cache);
540 rrset_cache_delete(daemon->env->rrset_cache);
541 infra_delete(daemon->env->infra_cache);
543 ub_randfree(daemon->rand);
544 alloc_clear(&daemon->superalloc);
545 acl_list_delete(daemon->acl);
546 free(daemon->chroot);
547 free(daemon->pidfile);
550 SSL_CTX_free((SSL_CTX*)daemon->listen_sslctx);
551 SSL_CTX_free((SSL_CTX*)daemon->connect_sslctx);
554 #ifdef LEX_HAS_YYLEX_DESTROY
558 /* libcrypto cleanup */
560 # if defined(USE_GOST) && defined(HAVE_LDNS_KEY_EVP_UNLOAD_GOST)
561 ldns_key_EVP_unload_gost();
563 # if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS && HAVE_DECL_SK_SSL_COMP_POP_FREE
565 sk_SSL_COMP_pop_free(comp_meth, (void(*)())CRYPTO_free);
568 # ifdef HAVE_OPENSSL_CONFIG
573 CRYPTO_cleanup_all_ex_data(); /* safe, no more threads right now */
577 # if defined(HAVE_SSL) && defined(OPENSSL_THREADS) && !defined(THREADS_DISABLED)
578 ub_openssl_lock_delete();
580 #elif defined(HAVE_NSS)
582 #endif /* HAVE_SSL or HAVE_NSS */
585 if(WSACleanup() != 0) {
586 log_err("Could not WSACleanup: %s",
587 wsa_strerror(WSAGetLastError()));
592 void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg)
596 if(!daemon->env->msg_cache ||
597 cfg->msg_cache_size != slabhash_get_size(daemon->env->msg_cache) ||
598 cfg->msg_cache_slabs != daemon->env->msg_cache->size) {
599 slabhash_delete(daemon->env->msg_cache);
600 daemon->env->msg_cache = slabhash_create(cfg->msg_cache_slabs,
601 HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size,
602 msgreply_sizefunc, query_info_compare,
603 query_entry_delete, reply_info_delete, NULL);
604 if(!daemon->env->msg_cache) {
605 fatal_exit("malloc failure updating config settings");
608 if((daemon->env->rrset_cache = rrset_cache_adjust(
609 daemon->env->rrset_cache, cfg, &daemon->superalloc)) == 0)
610 fatal_exit("malloc failure updating config settings");
611 if((daemon->env->infra_cache = infra_adjust(daemon->env->infra_cache,
613 fatal_exit("malloc failure updating config settings");