xref: /freebsd/contrib/unbound/daemon/daemon.c (revision 0572ccaa4543b0abef8ef81e384c1d04de9f3da1)
1 /*
2  * daemon/daemon.c - collection of workers that handles requests.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * The daemon consists of global settings and a number of workers.
40  */
41 
42 #include "config.h"
43 #ifdef HAVE_OPENSSL_ERR_H
44 #include <openssl/err.h>
45 #endif
46 
47 #ifdef HAVE_OPENSSL_RAND_H
48 #include <openssl/rand.h>
49 #endif
50 
51 #ifdef HAVE_OPENSSL_CONF_H
52 #include <openssl/conf.h>
53 #endif
54 
55 #ifdef HAVE_OPENSSL_ENGINE_H
56 #include <openssl/engine.h>
57 #endif
58 
59 #ifdef HAVE_TIME_H
60 #include <time.h>
61 #endif
62 #include <sys/time.h>
63 
64 #ifdef HAVE_NSS
65 /* nss3 */
66 #include "nss.h"
67 #endif
68 
69 #include "daemon/daemon.h"
70 #include "daemon/worker.h"
71 #include "daemon/remote.h"
72 #include "daemon/acl_list.h"
73 #include "util/log.h"
74 #include "util/config_file.h"
75 #include "util/data/msgreply.h"
76 #include "util/storage/lookup3.h"
77 #include "util/storage/slabhash.h"
78 #include "services/listen_dnsport.h"
79 #include "services/cache/rrset.h"
80 #include "services/cache/infra.h"
81 #include "services/localzone.h"
82 #include "services/modstack.h"
83 #include "util/module.h"
84 #include "util/random.h"
85 #include "util/tube.h"
86 #include "util/net_help.h"
87 #include "ldns/keyraw.h"
88 #include <signal.h>
89 
90 /** How many quit requests happened. */
91 static int sig_record_quit = 0;
92 /** How many reload requests happened. */
93 static int sig_record_reload = 0;
94 
95 #if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS
96 /** cleaner ssl memory freeup */
97 static void* comp_meth = NULL;
98 #endif
99 #ifdef LEX_HAS_YYLEX_DESTROY
100 /** remove buffers for parsing and init */
101 int ub_c_lex_destroy(void);
102 #endif
103 
104 /** used when no other sighandling happens, so we don't die
105   * when multiple signals in quick succession are sent to us.
106   * @param sig: signal number.
107   * @return signal handler return type (void or int).
108   */
109 static RETSIGTYPE record_sigh(int sig)
110 {
111 #ifdef LIBEVENT_SIGNAL_PROBLEM
112 	verbose(VERB_OPS, "quit on signal, no cleanup and statistics, "
113 		"because installed libevent version is not threadsafe");
114 	exit(0);
115 #endif
116 	switch(sig)
117 	{
118 		case SIGTERM:
119 #ifdef SIGQUIT
120 		case SIGQUIT:
121 #endif
122 #ifdef SIGBREAK
123 		case SIGBREAK:
124 #endif
125 		case SIGINT:
126 			sig_record_quit++;
127 			break;
128 #ifdef SIGHUP
129 		case SIGHUP:
130 			sig_record_reload++;
131 			break;
132 #endif
133 #ifdef SIGPIPE
134 		case SIGPIPE:
135 			break;
136 #endif
137 		default:
138 			log_err("ignoring signal %d", sig);
139 	}
140 }
141 
142 /**
143  * Signal handling during the time when netevent is disabled.
144  * Stores signals to replay later.
145  */
146 static void
147 signal_handling_record(void)
148 {
149 	if( signal(SIGTERM, record_sigh) == SIG_ERR ||
150 #ifdef SIGQUIT
151 		signal(SIGQUIT, record_sigh) == SIG_ERR ||
152 #endif
153 #ifdef SIGBREAK
154 		signal(SIGBREAK, record_sigh) == SIG_ERR ||
155 #endif
156 #ifdef SIGHUP
157 		signal(SIGHUP, record_sigh) == SIG_ERR ||
158 #endif
159 #ifdef SIGPIPE
160 		signal(SIGPIPE, SIG_IGN) == SIG_ERR ||
161 #endif
162 		signal(SIGINT, record_sigh) == SIG_ERR
163 		)
164 		log_err("install sighandler: %s", strerror(errno));
165 }
166 
167 /**
168  * Replay old signals.
169  * @param wrk: worker that handles signals.
170  */
171 static void
172 signal_handling_playback(struct worker* wrk)
173 {
174 #ifdef SIGHUP
175 	if(sig_record_reload)
176 		worker_sighandler(SIGHUP, wrk);
177 #endif
178 	if(sig_record_quit)
179 		worker_sighandler(SIGTERM, wrk);
180 	sig_record_quit = 0;
181 	sig_record_reload = 0;
182 }
183 
184 struct daemon*
185 daemon_init(void)
186 {
187 	struct daemon* daemon = (struct daemon*)calloc(1,
188 		sizeof(struct daemon));
189 #ifdef USE_WINSOCK
190 	int r;
191 	WSADATA wsa_data;
192 #endif
193 	if(!daemon)
194 		return NULL;
195 #ifdef USE_WINSOCK
196 	r = WSAStartup(MAKEWORD(2,2), &wsa_data);
197 	if(r != 0) {
198 		fatal_exit("could not init winsock. WSAStartup: %s",
199 			wsa_strerror(r));
200 	}
201 #endif /* USE_WINSOCK */
202 	signal_handling_record();
203 	checklock_start();
204 #ifdef HAVE_SSL
205 	ERR_load_crypto_strings();
206 	ERR_load_SSL_strings();
207 #  ifdef HAVE_OPENSSL_CONFIG
208 	OPENSSL_config("unbound");
209 #  endif
210 #  ifdef USE_GOST
211 	(void)sldns_key_EVP_load_gost_id();
212 #  endif
213 	OpenSSL_add_all_algorithms();
214 #  if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS
215 	/* grab the COMP method ptr because openssl leaks it */
216 	comp_meth = (void*)SSL_COMP_get_compression_methods();
217 #  endif
218 	(void)SSL_library_init();
219 #  if defined(HAVE_SSL) && defined(OPENSSL_THREADS) && !defined(THREADS_DISABLED)
220 	if(!ub_openssl_lock_init())
221 		fatal_exit("could not init openssl locks");
222 #  endif
223 #elif defined(HAVE_NSS)
224 	if(NSS_NoDB_Init(NULL) != SECSuccess)
225 		fatal_exit("could not init NSS");
226 #endif /* HAVE_SSL or HAVE_NSS */
227 #ifdef HAVE_TZSET
228 	/* init timezone info while we are not chrooted yet */
229 	tzset();
230 #endif
231 	/* open /dev/random if needed */
232 	ub_systemseed((unsigned)time(NULL)^(unsigned)getpid()^0xe67);
233 	daemon->need_to_exit = 0;
234 	modstack_init(&daemon->mods);
235 	if(!(daemon->env = (struct module_env*)calloc(1,
236 		sizeof(*daemon->env)))) {
237 		free(daemon);
238 		return NULL;
239 	}
240 	alloc_init(&daemon->superalloc, NULL, 0);
241 	daemon->acl = acl_list_create();
242 	if(!daemon->acl) {
243 		free(daemon->env);
244 		free(daemon);
245 		return NULL;
246 	}
247 	if(gettimeofday(&daemon->time_boot, NULL) < 0)
248 		log_err("gettimeofday: %s", strerror(errno));
249 	daemon->time_last_stat = daemon->time_boot;
250 	return daemon;
251 }
252 
253 int
254 daemon_open_shared_ports(struct daemon* daemon)
255 {
256 	log_assert(daemon);
257 	if(daemon->cfg->port != daemon->listening_port) {
258 		size_t i;
259 		int reuseport = 0;
260 		struct listen_port* p0;
261 		/* free and close old ports */
262 		if(daemon->ports != NULL) {
263 			for(i=0; i<daemon->num_ports; i++)
264 				listening_ports_free(daemon->ports[i]);
265 			free(daemon->ports);
266 			daemon->ports = NULL;
267 		}
268 		/* see if we want to reuseport */
269 #if defined(__linux__) && defined(SO_REUSEPORT)
270 		if(daemon->cfg->so_reuseport && daemon->cfg->num_threads > 0)
271 			reuseport = 1;
272 #endif
273 		/* try to use reuseport */
274 		p0 = listening_ports_open(daemon->cfg, &reuseport);
275 		if(!p0) {
276 			listening_ports_free(p0);
277 			return 0;
278 		}
279 		if(reuseport) {
280 			/* reuseport was successful, allocate for it */
281 			daemon->num_ports = (size_t)daemon->cfg->num_threads;
282 		} else {
283 			/* do the normal, singleportslist thing,
284 			 * reuseport not enabled or did not work */
285 			daemon->num_ports = 1;
286 		}
287 		if(!(daemon->ports = (struct listen_port**)calloc(
288 			daemon->num_ports, sizeof(*daemon->ports)))) {
289 			listening_ports_free(p0);
290 			return 0;
291 		}
292 		daemon->ports[0] = p0;
293 		if(reuseport) {
294 			/* continue to use reuseport */
295 			for(i=1; i<daemon->num_ports; i++) {
296 				if(!(daemon->ports[i]=
297 					listening_ports_open(daemon->cfg,
298 						&reuseport)) || !reuseport ) {
299 					for(i=0; i<daemon->num_ports; i++)
300 						listening_ports_free(daemon->ports[i]);
301 					free(daemon->ports);
302 					daemon->ports = NULL;
303 					return 0;
304 				}
305 			}
306 		}
307 		daemon->listening_port = daemon->cfg->port;
308 	}
309 	if(!daemon->cfg->remote_control_enable && daemon->rc_port) {
310 		listening_ports_free(daemon->rc_ports);
311 		daemon->rc_ports = NULL;
312 		daemon->rc_port = 0;
313 	}
314 	if(daemon->cfg->remote_control_enable &&
315 		daemon->cfg->control_port != daemon->rc_port) {
316 		listening_ports_free(daemon->rc_ports);
317 		if(!(daemon->rc_ports=daemon_remote_open_ports(daemon->cfg)))
318 			return 0;
319 		daemon->rc_port = daemon->cfg->control_port;
320 	}
321 	return 1;
322 }
323 
324 /**
325  * Setup modules. setup module stack.
326  * @param daemon: the daemon
327  */
328 static void daemon_setup_modules(struct daemon* daemon)
329 {
330 	daemon->env->cfg = daemon->cfg;
331 	daemon->env->alloc = &daemon->superalloc;
332 	daemon->env->worker = NULL;
333 	daemon->env->need_to_validate = 0; /* set by module init below */
334 	if(!modstack_setup(&daemon->mods, daemon->cfg->module_conf,
335 		daemon->env)) {
336 		fatal_exit("failed to setup modules");
337 	}
338 }
339 
340 /**
341  * Obtain allowed port numbers, concatenate the list, and shuffle them
342  * (ready to be handed out to threads).
343  * @param daemon: the daemon. Uses rand and cfg.
344  * @param shufport: the portlist output.
345  * @return number of ports available.
346  */
347 static int daemon_get_shufport(struct daemon* daemon, int* shufport)
348 {
349 	int i, n, k, temp;
350 	int avail = 0;
351 	for(i=0; i<65536; i++) {
352 		if(daemon->cfg->outgoing_avail_ports[i]) {
353 			shufport[avail++] = daemon->cfg->
354 				outgoing_avail_ports[i];
355 		}
356 	}
357 	if(avail == 0)
358 		fatal_exit("no ports are permitted for UDP, add "
359 			"with outgoing-port-permit");
360         /* Knuth shuffle */
361 	n = avail;
362 	while(--n > 0) {
363 		k = ub_random_max(daemon->rand, n+1); /* 0<= k<= n */
364 		temp = shufport[k];
365 		shufport[k] = shufport[n];
366 		shufport[n] = temp;
367 	}
368 	return avail;
369 }
370 
371 /**
372  * Allocate empty worker structures. With backptr and thread-number,
373  * from 0..numthread initialised. Used as user arguments to new threads.
374  * Creates the daemon random generator if it does not exist yet.
375  * The random generator stays existing between reloads with a unique state.
376  * @param daemon: the daemon with (new) config settings.
377  */
378 static void
379 daemon_create_workers(struct daemon* daemon)
380 {
381 	int i, numport;
382 	int* shufport;
383 	log_assert(daemon && daemon->cfg);
384 	if(!daemon->rand) {
385 		unsigned int seed = (unsigned int)time(NULL) ^
386 			(unsigned int)getpid() ^ 0x438;
387 		daemon->rand = ub_initstate(seed, NULL);
388 		if(!daemon->rand)
389 			fatal_exit("could not init random generator");
390 	}
391 	hash_set_raninit((uint32_t)ub_random(daemon->rand));
392 	shufport = (int*)calloc(65536, sizeof(int));
393 	if(!shufport)
394 		fatal_exit("out of memory during daemon init");
395 	numport = daemon_get_shufport(daemon, shufport);
396 	verbose(VERB_ALGO, "total of %d outgoing ports available", numport);
397 
398 	daemon->num = (daemon->cfg->num_threads?daemon->cfg->num_threads:1);
399 	daemon->workers = (struct worker**)calloc((size_t)daemon->num,
400 		sizeof(struct worker*));
401 	for(i=0; i<daemon->num; i++) {
402 		if(!(daemon->workers[i] = worker_create(daemon, i,
403 			shufport+numport*i/daemon->num,
404 			numport*(i+1)/daemon->num - numport*i/daemon->num)))
405 			/* the above is not ports/numthr, due to rounding */
406 			fatal_exit("could not create worker");
407 	}
408 	free(shufport);
409 }
410 
411 #ifdef THREADS_DISABLED
412 /**
413  * Close all pipes except for the numbered thread.
414  * @param daemon: daemon to close pipes in.
415  * @param thr: thread number 0..num-1 of thread to skip.
416  */
417 static void close_other_pipes(struct daemon* daemon, int thr)
418 {
419 	int i;
420 	for(i=0; i<daemon->num; i++)
421 		if(i!=thr) {
422 			if(i==0) {
423 				/* only close read part, need to write stats */
424 				tube_close_read(daemon->workers[i]->cmd);
425 			} else {
426 				/* complete close channel to others */
427 				tube_delete(daemon->workers[i]->cmd);
428 				daemon->workers[i]->cmd = NULL;
429 			}
430 		}
431 }
432 #endif /* THREADS_DISABLED */
433 
434 /**
435  * Function to start one thread.
436  * @param arg: user argument.
437  * @return: void* user return value could be used for thread_join results.
438  */
439 static void*
440 thread_start(void* arg)
441 {
442 	struct worker* worker = (struct worker*)arg;
443 	int port_num = 0;
444 	log_thread_set(&worker->thread_num);
445 	ub_thread_blocksigs();
446 #ifdef THREADS_DISABLED
447 	/* close pipe ends used by main */
448 	tube_close_write(worker->cmd);
449 	close_other_pipes(worker->daemon, worker->thread_num);
450 #endif
451 #if defined(__linux__) && defined(SO_REUSEPORT)
452 	if(worker->daemon->cfg->so_reuseport)
453 		port_num = worker->thread_num;
454 	else
455 		port_num = 0;
456 #endif
457 	if(!worker_init(worker, worker->daemon->cfg,
458 			worker->daemon->ports[port_num], 0))
459 		fatal_exit("Could not initialize thread");
460 
461 	worker_work(worker);
462 	return NULL;
463 }
464 
465 /**
466  * Fork and init the other threads. Main thread returns for special handling.
467  * @param daemon: the daemon with other threads to fork.
468  */
469 static void
470 daemon_start_others(struct daemon* daemon)
471 {
472 	int i;
473 	log_assert(daemon);
474 	verbose(VERB_ALGO, "start threads");
475 	/* skip i=0, is this thread */
476 	for(i=1; i<daemon->num; i++) {
477 		ub_thread_create(&daemon->workers[i]->thr_id,
478 			thread_start, daemon->workers[i]);
479 #ifdef THREADS_DISABLED
480 		/* close pipe end of child */
481 		tube_close_read(daemon->workers[i]->cmd);
482 #endif /* no threads */
483 	}
484 }
485 
486 /**
487  * Stop the other threads.
488  * @param daemon: the daemon with other threads.
489  */
490 static void
491 daemon_stop_others(struct daemon* daemon)
492 {
493 	int i;
494 	log_assert(daemon);
495 	verbose(VERB_ALGO, "stop threads");
496 	/* skip i=0, is this thread */
497 	/* use i=0 buffer for sending cmds; because we are #0 */
498 	for(i=1; i<daemon->num; i++) {
499 		worker_send_cmd(daemon->workers[i], worker_cmd_quit);
500 	}
501 	/* wait for them to quit */
502 	for(i=1; i<daemon->num; i++) {
503 		/* join it to make sure its dead */
504 		verbose(VERB_ALGO, "join %d", i);
505 		ub_thread_join(daemon->workers[i]->thr_id);
506 		verbose(VERB_ALGO, "join success %d", i);
507 	}
508 }
509 
510 void
511 daemon_fork(struct daemon* daemon)
512 {
513 	log_assert(daemon);
514 	if(!acl_list_apply_cfg(daemon->acl, daemon->cfg))
515 		fatal_exit("Could not setup access control list");
516 	if(!(daemon->local_zones = local_zones_create()))
517 		fatal_exit("Could not create local zones: out of memory");
518 	if(!local_zones_apply_cfg(daemon->local_zones, daemon->cfg))
519 		fatal_exit("Could not set up local zones");
520 
521 	/* setup modules */
522 	daemon_setup_modules(daemon);
523 
524 	/* first create all the worker structures, so we can pass
525 	 * them to the newly created threads.
526 	 */
527 	daemon_create_workers(daemon);
528 
529 #if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
530 	/* in libev the first inited base gets signals */
531 	if(!worker_init(daemon->workers[0], daemon->cfg, daemon->ports[0], 1))
532 		fatal_exit("Could not initialize main thread");
533 #endif
534 
535 	/* Now create the threads and init the workers.
536 	 * By the way, this is thread #0 (the main thread).
537 	 */
538 	daemon_start_others(daemon);
539 
540 	/* Special handling for the main thread. This is the thread
541 	 * that handles signals and remote control.
542 	 */
543 #if !(defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP))
544 	/* libevent has the last inited base get signals (or any base) */
545 	if(!worker_init(daemon->workers[0], daemon->cfg, daemon->ports[0], 1))
546 		fatal_exit("Could not initialize main thread");
547 #endif
548 	signal_handling_playback(daemon->workers[0]);
549 
550 	/* Start resolver service on main thread. */
551 	log_info("start of service (%s).", PACKAGE_STRING);
552 	worker_work(daemon->workers[0]);
553 	log_info("service stopped (%s).", PACKAGE_STRING);
554 
555 	/* we exited! a signal happened! Stop other threads */
556 	daemon_stop_others(daemon);
557 
558 	daemon->need_to_exit = daemon->workers[0]->need_to_exit;
559 }
560 
561 void
562 daemon_cleanup(struct daemon* daemon)
563 {
564 	int i;
565 	log_assert(daemon);
566 	/* before stopping main worker, handle signals ourselves, so we
567 	   don't die on multiple reload signals for example. */
568 	signal_handling_record();
569 	log_thread_set(NULL);
570 	/* clean up caches because
571 	 * a) RRset IDs will be recycled after a reload, causing collisions
572 	 * b) validation config can change, thus rrset, msg, keycache clear
573 	 * The infra cache is kept, the timing and edns info is still valid */
574 	slabhash_clear(&daemon->env->rrset_cache->table);
575 	slabhash_clear(daemon->env->msg_cache);
576 	local_zones_delete(daemon->local_zones);
577 	daemon->local_zones = NULL;
578 	/* key cache is cleared by module desetup during next daemon_init() */
579 	daemon_remote_clear(daemon->rc);
580 	for(i=0; i<daemon->num; i++)
581 		worker_delete(daemon->workers[i]);
582 	free(daemon->workers);
583 	daemon->workers = NULL;
584 	daemon->num = 0;
585 	daemon->cfg = NULL;
586 }
587 
588 void
589 daemon_delete(struct daemon* daemon)
590 {
591 	size_t i;
592 	if(!daemon)
593 		return;
594 	modstack_desetup(&daemon->mods, daemon->env);
595 	daemon_remote_delete(daemon->rc);
596 	for(i = 0; i < daemon->num_ports; i++)
597 		listening_ports_free(daemon->ports[i]);
598 	free(daemon->ports);
599 	listening_ports_free(daemon->rc_ports);
600 	if(daemon->env) {
601 		slabhash_delete(daemon->env->msg_cache);
602 		rrset_cache_delete(daemon->env->rrset_cache);
603 		infra_delete(daemon->env->infra_cache);
604 	}
605 	ub_randfree(daemon->rand);
606 	alloc_clear(&daemon->superalloc);
607 	acl_list_delete(daemon->acl);
608 	free(daemon->chroot);
609 	free(daemon->pidfile);
610 	free(daemon->env);
611 #ifdef HAVE_SSL
612 	SSL_CTX_free((SSL_CTX*)daemon->listen_sslctx);
613 	SSL_CTX_free((SSL_CTX*)daemon->connect_sslctx);
614 #endif
615 	free(daemon);
616 #ifdef LEX_HAS_YYLEX_DESTROY
617 	/* lex cleanup */
618 	ub_c_lex_destroy();
619 #endif
620 	/* libcrypto cleanup */
621 #ifdef HAVE_SSL
622 #  if defined(USE_GOST) && defined(HAVE_LDNS_KEY_EVP_UNLOAD_GOST)
623 	sldns_key_EVP_unload_gost();
624 #  endif
625 #  if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS && HAVE_DECL_SK_SSL_COMP_POP_FREE
626 #    ifndef S_SPLINT_S
627 	sk_SSL_COMP_pop_free(comp_meth, (void(*)())CRYPTO_free);
628 #    endif
629 #  endif
630 #  ifdef HAVE_OPENSSL_CONFIG
631 	EVP_cleanup();
632 	ENGINE_cleanup();
633 	CONF_modules_free();
634 #  endif
635 	CRYPTO_cleanup_all_ex_data(); /* safe, no more threads right now */
636 	ERR_remove_state(0);
637 	ERR_free_strings();
638 	RAND_cleanup();
639 #  if defined(HAVE_SSL) && defined(OPENSSL_THREADS) && !defined(THREADS_DISABLED)
640 	ub_openssl_lock_delete();
641 #  endif
642 #elif defined(HAVE_NSS)
643 	NSS_Shutdown();
644 #endif /* HAVE_SSL or HAVE_NSS */
645 	checklock_stop();
646 #ifdef USE_WINSOCK
647 	if(WSACleanup() != 0) {
648 		log_err("Could not WSACleanup: %s",
649 			wsa_strerror(WSAGetLastError()));
650 	}
651 #endif
652 }
653 
654 void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg)
655 {
656         daemon->cfg = cfg;
657 	config_apply(cfg);
658 	if(!daemon->env->msg_cache ||
659 	   cfg->msg_cache_size != slabhash_get_size(daemon->env->msg_cache) ||
660 	   cfg->msg_cache_slabs != daemon->env->msg_cache->size) {
661 		slabhash_delete(daemon->env->msg_cache);
662 		daemon->env->msg_cache = slabhash_create(cfg->msg_cache_slabs,
663 			HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size,
664 			msgreply_sizefunc, query_info_compare,
665 			query_entry_delete, reply_info_delete, NULL);
666 		if(!daemon->env->msg_cache) {
667 			fatal_exit("malloc failure updating config settings");
668 		}
669 	}
670 	if((daemon->env->rrset_cache = rrset_cache_adjust(
671 		daemon->env->rrset_cache, cfg, &daemon->superalloc)) == 0)
672 		fatal_exit("malloc failure updating config settings");
673 	if((daemon->env->infra_cache = infra_adjust(daemon->env->infra_cache,
674 		cfg))==0)
675 		fatal_exit("malloc failure updating config settings");
676 }
677