xref: /freebsd/contrib/unbound/daemon/daemon.c (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
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 "sldns/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 	/* cannot log, verbose here because locks may be held */
113 	/* quit on signal, no cleanup and statistics,
114 	   because installed libevent version is not threadsafe */
115 	exit(0);
116 #endif
117 	switch(sig)
118 	{
119 		case SIGTERM:
120 #ifdef SIGQUIT
121 		case SIGQUIT:
122 #endif
123 #ifdef SIGBREAK
124 		case SIGBREAK:
125 #endif
126 		case SIGINT:
127 			sig_record_quit++;
128 			break;
129 #ifdef SIGHUP
130 		case SIGHUP:
131 			sig_record_reload++;
132 			break;
133 #endif
134 #ifdef SIGPIPE
135 		case SIGPIPE:
136 			break;
137 #endif
138 		default:
139 			/* ignoring signal */
140 			break;
141 	}
142 }
143 
144 /**
145  * Signal handling during the time when netevent is disabled.
146  * Stores signals to replay later.
147  */
148 static void
149 signal_handling_record(void)
150 {
151 	if( signal(SIGTERM, record_sigh) == SIG_ERR ||
152 #ifdef SIGQUIT
153 		signal(SIGQUIT, record_sigh) == SIG_ERR ||
154 #endif
155 #ifdef SIGBREAK
156 		signal(SIGBREAK, record_sigh) == SIG_ERR ||
157 #endif
158 #ifdef SIGHUP
159 		signal(SIGHUP, record_sigh) == SIG_ERR ||
160 #endif
161 #ifdef SIGPIPE
162 		signal(SIGPIPE, SIG_IGN) == SIG_ERR ||
163 #endif
164 		signal(SIGINT, record_sigh) == SIG_ERR
165 		)
166 		log_err("install sighandler: %s", strerror(errno));
167 }
168 
169 /**
170  * Replay old signals.
171  * @param wrk: worker that handles signals.
172  */
173 static void
174 signal_handling_playback(struct worker* wrk)
175 {
176 #ifdef SIGHUP
177 	if(sig_record_reload)
178 		worker_sighandler(SIGHUP, wrk);
179 #endif
180 	if(sig_record_quit)
181 		worker_sighandler(SIGTERM, wrk);
182 	sig_record_quit = 0;
183 	sig_record_reload = 0;
184 }
185 
186 struct daemon*
187 daemon_init(void)
188 {
189 	struct daemon* daemon = (struct daemon*)calloc(1,
190 		sizeof(struct daemon));
191 #ifdef USE_WINSOCK
192 	int r;
193 	WSADATA wsa_data;
194 #endif
195 	if(!daemon)
196 		return NULL;
197 #ifdef USE_WINSOCK
198 	r = WSAStartup(MAKEWORD(2,2), &wsa_data);
199 	if(r != 0) {
200 		fatal_exit("could not init winsock. WSAStartup: %s",
201 			wsa_strerror(r));
202 	}
203 #endif /* USE_WINSOCK */
204 	signal_handling_record();
205 	checklock_start();
206 #ifdef HAVE_SSL
207 	ERR_load_crypto_strings();
208 	ERR_load_SSL_strings();
209 #  ifdef HAVE_OPENSSL_CONFIG
210 	OPENSSL_config("unbound");
211 #  endif
212 #  ifdef USE_GOST
213 	(void)sldns_key_EVP_load_gost_id();
214 #  endif
215 	OpenSSL_add_all_algorithms();
216 #  if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS
217 	/* grab the COMP method ptr because openssl leaks it */
218 	comp_meth = (void*)SSL_COMP_get_compression_methods();
219 #  endif
220 	(void)SSL_library_init();
221 #  if defined(HAVE_SSL) && defined(OPENSSL_THREADS) && !defined(THREADS_DISABLED)
222 	if(!ub_openssl_lock_init())
223 		fatal_exit("could not init openssl locks");
224 #  endif
225 #elif defined(HAVE_NSS)
226 	if(NSS_NoDB_Init(NULL) != SECSuccess)
227 		fatal_exit("could not init NSS");
228 #endif /* HAVE_SSL or HAVE_NSS */
229 #ifdef HAVE_TZSET
230 	/* init timezone info while we are not chrooted yet */
231 	tzset();
232 #endif
233 	/* open /dev/random if needed */
234 	ub_systemseed((unsigned)time(NULL)^(unsigned)getpid()^0xe67);
235 	daemon->need_to_exit = 0;
236 	modstack_init(&daemon->mods);
237 	if(!(daemon->env = (struct module_env*)calloc(1,
238 		sizeof(*daemon->env)))) {
239 		free(daemon);
240 		return NULL;
241 	}
242 	alloc_init(&daemon->superalloc, NULL, 0);
243 	daemon->acl = acl_list_create();
244 	if(!daemon->acl) {
245 		free(daemon->env);
246 		free(daemon);
247 		return NULL;
248 	}
249 	if(gettimeofday(&daemon->time_boot, NULL) < 0)
250 		log_err("gettimeofday: %s", strerror(errno));
251 	daemon->time_last_stat = daemon->time_boot;
252 	return daemon;
253 }
254 
255 int
256 daemon_open_shared_ports(struct daemon* daemon)
257 {
258 	log_assert(daemon);
259 	if(daemon->cfg->port != daemon->listening_port) {
260 		size_t i;
261 		struct listen_port* p0;
262 		daemon->reuseport = 0;
263 		/* free and close old ports */
264 		if(daemon->ports != NULL) {
265 			for(i=0; i<daemon->num_ports; i++)
266 				listening_ports_free(daemon->ports[i]);
267 			free(daemon->ports);
268 			daemon->ports = NULL;
269 		}
270 		/* see if we want to reuseport */
271 #ifdef SO_REUSEPORT
272 		if(daemon->cfg->so_reuseport && daemon->cfg->num_threads > 0)
273 			daemon->reuseport = 1;
274 #endif
275 		/* try to use reuseport */
276 		p0 = listening_ports_open(daemon->cfg, &daemon->reuseport);
277 		if(!p0) {
278 			listening_ports_free(p0);
279 			return 0;
280 		}
281 		if(daemon->reuseport) {
282 			/* reuseport was successful, allocate for it */
283 			daemon->num_ports = (size_t)daemon->cfg->num_threads;
284 		} else {
285 			/* do the normal, singleportslist thing,
286 			 * reuseport not enabled or did not work */
287 			daemon->num_ports = 1;
288 		}
289 		if(!(daemon->ports = (struct listen_port**)calloc(
290 			daemon->num_ports, sizeof(*daemon->ports)))) {
291 			listening_ports_free(p0);
292 			return 0;
293 		}
294 		daemon->ports[0] = p0;
295 		if(daemon->reuseport) {
296 			/* continue to use reuseport */
297 			for(i=1; i<daemon->num_ports; i++) {
298 				if(!(daemon->ports[i]=
299 					listening_ports_open(daemon->cfg,
300 						&daemon->reuseport))
301 					|| !daemon->reuseport ) {
302 					for(i=0; i<daemon->num_ports; i++)
303 						listening_ports_free(daemon->ports[i]);
304 					free(daemon->ports);
305 					daemon->ports = NULL;
306 					return 0;
307 				}
308 			}
309 		}
310 		daemon->listening_port = daemon->cfg->port;
311 	}
312 	if(!daemon->cfg->remote_control_enable && daemon->rc_port) {
313 		listening_ports_free(daemon->rc_ports);
314 		daemon->rc_ports = NULL;
315 		daemon->rc_port = 0;
316 	}
317 	if(daemon->cfg->remote_control_enable &&
318 		daemon->cfg->control_port != daemon->rc_port) {
319 		listening_ports_free(daemon->rc_ports);
320 		if(!(daemon->rc_ports=daemon_remote_open_ports(daemon->cfg)))
321 			return 0;
322 		daemon->rc_port = daemon->cfg->control_port;
323 	}
324 	return 1;
325 }
326 
327 /**
328  * Setup modules. setup module stack.
329  * @param daemon: the daemon
330  */
331 static void daemon_setup_modules(struct daemon* daemon)
332 {
333 	daemon->env->cfg = daemon->cfg;
334 	daemon->env->alloc = &daemon->superalloc;
335 	daemon->env->worker = NULL;
336 	daemon->env->need_to_validate = 0; /* set by module init below */
337 	if(!modstack_setup(&daemon->mods, daemon->cfg->module_conf,
338 		daemon->env)) {
339 		fatal_exit("failed to setup modules");
340 	}
341 }
342 
343 /**
344  * Obtain allowed port numbers, concatenate the list, and shuffle them
345  * (ready to be handed out to threads).
346  * @param daemon: the daemon. Uses rand and cfg.
347  * @param shufport: the portlist output.
348  * @return number of ports available.
349  */
350 static int daemon_get_shufport(struct daemon* daemon, int* shufport)
351 {
352 	int i, n, k, temp;
353 	int avail = 0;
354 	for(i=0; i<65536; i++) {
355 		if(daemon->cfg->outgoing_avail_ports[i]) {
356 			shufport[avail++] = daemon->cfg->
357 				outgoing_avail_ports[i];
358 		}
359 	}
360 	if(avail == 0)
361 		fatal_exit("no ports are permitted for UDP, add "
362 			"with outgoing-port-permit");
363         /* Knuth shuffle */
364 	n = avail;
365 	while(--n > 0) {
366 		k = ub_random_max(daemon->rand, n+1); /* 0<= k<= n */
367 		temp = shufport[k];
368 		shufport[k] = shufport[n];
369 		shufport[n] = temp;
370 	}
371 	return avail;
372 }
373 
374 /**
375  * Allocate empty worker structures. With backptr and thread-number,
376  * from 0..numthread initialised. Used as user arguments to new threads.
377  * Creates the daemon random generator if it does not exist yet.
378  * The random generator stays existing between reloads with a unique state.
379  * @param daemon: the daemon with (new) config settings.
380  */
381 static void
382 daemon_create_workers(struct daemon* daemon)
383 {
384 	int i, numport;
385 	int* shufport;
386 	log_assert(daemon && daemon->cfg);
387 	if(!daemon->rand) {
388 		unsigned int seed = (unsigned int)time(NULL) ^
389 			(unsigned int)getpid() ^ 0x438;
390 		daemon->rand = ub_initstate(seed, NULL);
391 		if(!daemon->rand)
392 			fatal_exit("could not init random generator");
393 	}
394 	hash_set_raninit((uint32_t)ub_random(daemon->rand));
395 	shufport = (int*)calloc(65536, sizeof(int));
396 	if(!shufport)
397 		fatal_exit("out of memory during daemon init");
398 	numport = daemon_get_shufport(daemon, shufport);
399 	verbose(VERB_ALGO, "total of %d outgoing ports available", numport);
400 
401 	daemon->num = (daemon->cfg->num_threads?daemon->cfg->num_threads:1);
402 	if(daemon->reuseport && (int)daemon->num < (int)daemon->num_ports) {
403 		log_warn("cannot reduce num-threads to %d because so-reuseport "
404 			"so continuing with %d threads.", (int)daemon->num,
405 			(int)daemon->num_ports);
406 		daemon->num = (int)daemon->num_ports;
407 	}
408 	daemon->workers = (struct worker**)calloc((size_t)daemon->num,
409 		sizeof(struct worker*));
410 	if(daemon->cfg->dnstap) {
411 #ifdef USE_DNSTAP
412 		daemon->dtenv = dt_create(daemon->cfg->dnstap_socket_path,
413 			(unsigned int)daemon->num);
414 		if (!daemon->dtenv)
415 			fatal_exit("dt_create failed");
416 		dt_apply_cfg(daemon->dtenv, daemon->cfg);
417 #else
418 		fatal_exit("dnstap enabled in config but not built with dnstap support");
419 #endif
420 	}
421 	for(i=0; i<daemon->num; i++) {
422 		if(!(daemon->workers[i] = worker_create(daemon, i,
423 			shufport+numport*i/daemon->num,
424 			numport*(i+1)/daemon->num - numport*i/daemon->num)))
425 			/* the above is not ports/numthr, due to rounding */
426 			fatal_exit("could not create worker");
427 	}
428 	free(shufport);
429 }
430 
431 #ifdef THREADS_DISABLED
432 /**
433  * Close all pipes except for the numbered thread.
434  * @param daemon: daemon to close pipes in.
435  * @param thr: thread number 0..num-1 of thread to skip.
436  */
437 static void close_other_pipes(struct daemon* daemon, int thr)
438 {
439 	int i;
440 	for(i=0; i<daemon->num; i++)
441 		if(i!=thr) {
442 			if(i==0) {
443 				/* only close read part, need to write stats */
444 				tube_close_read(daemon->workers[i]->cmd);
445 			} else {
446 				/* complete close channel to others */
447 				tube_delete(daemon->workers[i]->cmd);
448 				daemon->workers[i]->cmd = NULL;
449 			}
450 		}
451 }
452 #endif /* THREADS_DISABLED */
453 
454 /**
455  * Function to start one thread.
456  * @param arg: user argument.
457  * @return: void* user return value could be used for thread_join results.
458  */
459 static void*
460 thread_start(void* arg)
461 {
462 	struct worker* worker = (struct worker*)arg;
463 	int port_num = 0;
464 	log_thread_set(&worker->thread_num);
465 	ub_thread_blocksigs();
466 #ifdef THREADS_DISABLED
467 	/* close pipe ends used by main */
468 	tube_close_write(worker->cmd);
469 	close_other_pipes(worker->daemon, worker->thread_num);
470 #endif
471 #ifdef SO_REUSEPORT
472 	if(worker->daemon->cfg->so_reuseport)
473 		port_num = worker->thread_num % worker->daemon->num_ports;
474 	else
475 		port_num = 0;
476 #endif
477 	if(!worker_init(worker, worker->daemon->cfg,
478 			worker->daemon->ports[port_num], 0))
479 		fatal_exit("Could not initialize thread");
480 
481 	worker_work(worker);
482 	return NULL;
483 }
484 
485 /**
486  * Fork and init the other threads. Main thread returns for special handling.
487  * @param daemon: the daemon with other threads to fork.
488  */
489 static void
490 daemon_start_others(struct daemon* daemon)
491 {
492 	int i;
493 	log_assert(daemon);
494 	verbose(VERB_ALGO, "start threads");
495 	/* skip i=0, is this thread */
496 	for(i=1; i<daemon->num; i++) {
497 		ub_thread_create(&daemon->workers[i]->thr_id,
498 			thread_start, daemon->workers[i]);
499 #ifdef THREADS_DISABLED
500 		/* close pipe end of child */
501 		tube_close_read(daemon->workers[i]->cmd);
502 #endif /* no threads */
503 	}
504 }
505 
506 /**
507  * Stop the other threads.
508  * @param daemon: the daemon with other threads.
509  */
510 static void
511 daemon_stop_others(struct daemon* daemon)
512 {
513 	int i;
514 	log_assert(daemon);
515 	verbose(VERB_ALGO, "stop threads");
516 	/* skip i=0, is this thread */
517 	/* use i=0 buffer for sending cmds; because we are #0 */
518 	for(i=1; i<daemon->num; i++) {
519 		worker_send_cmd(daemon->workers[i], worker_cmd_quit);
520 	}
521 	/* wait for them to quit */
522 	for(i=1; i<daemon->num; i++) {
523 		/* join it to make sure its dead */
524 		verbose(VERB_ALGO, "join %d", i);
525 		ub_thread_join(daemon->workers[i]->thr_id);
526 		verbose(VERB_ALGO, "join success %d", i);
527 	}
528 }
529 
530 void
531 daemon_fork(struct daemon* daemon)
532 {
533 	log_assert(daemon);
534 	if(!acl_list_apply_cfg(daemon->acl, daemon->cfg))
535 		fatal_exit("Could not setup access control list");
536 	if(!(daemon->local_zones = local_zones_create()))
537 		fatal_exit("Could not create local zones: out of memory");
538 	if(!local_zones_apply_cfg(daemon->local_zones, daemon->cfg))
539 		fatal_exit("Could not set up local zones");
540 
541 	/* setup modules */
542 	daemon_setup_modules(daemon);
543 
544 	/* first create all the worker structures, so we can pass
545 	 * them to the newly created threads.
546 	 */
547 	daemon_create_workers(daemon);
548 
549 #if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
550 	/* in libev the first inited base gets signals */
551 	if(!worker_init(daemon->workers[0], daemon->cfg, daemon->ports[0], 1))
552 		fatal_exit("Could not initialize main thread");
553 #endif
554 
555 	/* Now create the threads and init the workers.
556 	 * By the way, this is thread #0 (the main thread).
557 	 */
558 	daemon_start_others(daemon);
559 
560 	/* Special handling for the main thread. This is the thread
561 	 * that handles signals and remote control.
562 	 */
563 #if !(defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP))
564 	/* libevent has the last inited base get signals (or any base) */
565 	if(!worker_init(daemon->workers[0], daemon->cfg, daemon->ports[0], 1))
566 		fatal_exit("Could not initialize main thread");
567 #endif
568 	signal_handling_playback(daemon->workers[0]);
569 
570 	/* Start resolver service on main thread. */
571 	log_info("start of service (%s).", PACKAGE_STRING);
572 	worker_work(daemon->workers[0]);
573 	log_info("service stopped (%s).", PACKAGE_STRING);
574 
575 	/* we exited! a signal happened! Stop other threads */
576 	daemon_stop_others(daemon);
577 
578 	daemon->need_to_exit = daemon->workers[0]->need_to_exit;
579 }
580 
581 void
582 daemon_cleanup(struct daemon* daemon)
583 {
584 	int i;
585 	log_assert(daemon);
586 	/* before stopping main worker, handle signals ourselves, so we
587 	   don't die on multiple reload signals for example. */
588 	signal_handling_record();
589 	log_thread_set(NULL);
590 	/* clean up caches because
591 	 * a) RRset IDs will be recycled after a reload, causing collisions
592 	 * b) validation config can change, thus rrset, msg, keycache clear
593 	 * The infra cache is kept, the timing and edns info is still valid */
594 	slabhash_clear(&daemon->env->rrset_cache->table);
595 	slabhash_clear(daemon->env->msg_cache);
596 	local_zones_delete(daemon->local_zones);
597 	daemon->local_zones = NULL;
598 	/* key cache is cleared by module desetup during next daemon_init() */
599 	daemon_remote_clear(daemon->rc);
600 	for(i=0; i<daemon->num; i++)
601 		worker_delete(daemon->workers[i]);
602 	free(daemon->workers);
603 	daemon->workers = NULL;
604 	daemon->num = 0;
605 #ifdef USE_DNSTAP
606 	dt_delete(daemon->dtenv);
607 #endif
608 	daemon->cfg = NULL;
609 }
610 
611 void
612 daemon_delete(struct daemon* daemon)
613 {
614 	size_t i;
615 	if(!daemon)
616 		return;
617 	modstack_desetup(&daemon->mods, daemon->env);
618 	daemon_remote_delete(daemon->rc);
619 	for(i = 0; i < daemon->num_ports; i++)
620 		listening_ports_free(daemon->ports[i]);
621 	free(daemon->ports);
622 	listening_ports_free(daemon->rc_ports);
623 	if(daemon->env) {
624 		slabhash_delete(daemon->env->msg_cache);
625 		rrset_cache_delete(daemon->env->rrset_cache);
626 		infra_delete(daemon->env->infra_cache);
627 	}
628 	ub_randfree(daemon->rand);
629 	alloc_clear(&daemon->superalloc);
630 	acl_list_delete(daemon->acl);
631 	free(daemon->chroot);
632 	free(daemon->pidfile);
633 	free(daemon->env);
634 #ifdef HAVE_SSL
635 	SSL_CTX_free((SSL_CTX*)daemon->listen_sslctx);
636 	SSL_CTX_free((SSL_CTX*)daemon->connect_sslctx);
637 #endif
638 	free(daemon);
639 #ifdef LEX_HAS_YYLEX_DESTROY
640 	/* lex cleanup */
641 	ub_c_lex_destroy();
642 #endif
643 	/* libcrypto cleanup */
644 #ifdef HAVE_SSL
645 #  if defined(USE_GOST) && defined(HAVE_LDNS_KEY_EVP_UNLOAD_GOST)
646 	sldns_key_EVP_unload_gost();
647 #  endif
648 #  if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS && HAVE_DECL_SK_SSL_COMP_POP_FREE
649 #    ifndef S_SPLINT_S
650 	sk_SSL_COMP_pop_free(comp_meth, (void(*)())CRYPTO_free);
651 #    endif
652 #  endif
653 #  ifdef HAVE_OPENSSL_CONFIG
654 	EVP_cleanup();
655 	ENGINE_cleanup();
656 	CONF_modules_free();
657 #  endif
658 	CRYPTO_cleanup_all_ex_data(); /* safe, no more threads right now */
659 	ERR_remove_state(0);
660 	ERR_free_strings();
661 	RAND_cleanup();
662 #  if defined(HAVE_SSL) && defined(OPENSSL_THREADS) && !defined(THREADS_DISABLED)
663 	ub_openssl_lock_delete();
664 #  endif
665 #elif defined(HAVE_NSS)
666 	NSS_Shutdown();
667 #endif /* HAVE_SSL or HAVE_NSS */
668 	checklock_stop();
669 #ifdef USE_WINSOCK
670 	if(WSACleanup() != 0) {
671 		log_err("Could not WSACleanup: %s",
672 			wsa_strerror(WSAGetLastError()));
673 	}
674 #endif
675 }
676 
677 void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg)
678 {
679         daemon->cfg = cfg;
680 	config_apply(cfg);
681 	if(!daemon->env->msg_cache ||
682 	   cfg->msg_cache_size != slabhash_get_size(daemon->env->msg_cache) ||
683 	   cfg->msg_cache_slabs != daemon->env->msg_cache->size) {
684 		slabhash_delete(daemon->env->msg_cache);
685 		daemon->env->msg_cache = slabhash_create(cfg->msg_cache_slabs,
686 			HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size,
687 			msgreply_sizefunc, query_info_compare,
688 			query_entry_delete, reply_info_delete, NULL);
689 		if(!daemon->env->msg_cache) {
690 			fatal_exit("malloc failure updating config settings");
691 		}
692 	}
693 	if((daemon->env->rrset_cache = rrset_cache_adjust(
694 		daemon->env->rrset_cache, cfg, &daemon->superalloc)) == 0)
695 		fatal_exit("malloc failure updating config settings");
696 	if((daemon->env->infra_cache = infra_adjust(daemon->env->infra_cache,
697 		cfg))==0)
698 		fatal_exit("malloc failure updating config settings");
699 }
700