xref: /freebsd/contrib/unbound/daemon/daemon.c (revision e17f5b1d)
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/shm_side/shm_main.h"
77 #include "util/storage/lookup3.h"
78 #include "util/storage/slabhash.h"
79 #include "util/tcp_conn_limit.h"
80 #include "services/listen_dnsport.h"
81 #include "services/cache/rrset.h"
82 #include "services/cache/infra.h"
83 #include "services/localzone.h"
84 #include "services/view.h"
85 #include "services/modstack.h"
86 #include "services/authzone.h"
87 #include "util/module.h"
88 #include "util/random.h"
89 #include "util/tube.h"
90 #include "util/net_help.h"
91 #include "sldns/keyraw.h"
92 #include "respip/respip.h"
93 #include <signal.h>
94 
95 #ifdef HAVE_SYSTEMD
96 #include <systemd/sd-daemon.h>
97 #endif
98 
99 /** How many quit requests happened. */
100 static int sig_record_quit = 0;
101 /** How many reload requests happened. */
102 static int sig_record_reload = 0;
103 
104 #if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS
105 /** cleaner ssl memory freeup */
106 static void* comp_meth = NULL;
107 #endif
108 /** remove buffers for parsing and init */
109 int ub_c_lex_destroy(void);
110 
111 /** used when no other sighandling happens, so we don't die
112   * when multiple signals in quick succession are sent to us.
113   * @param sig: signal number.
114   * @return signal handler return type (void or int).
115   */
116 static RETSIGTYPE record_sigh(int sig)
117 {
118 #ifdef LIBEVENT_SIGNAL_PROBLEM
119 	/* cannot log, verbose here because locks may be held */
120 	/* quit on signal, no cleanup and statistics,
121 	   because installed libevent version is not threadsafe */
122 	exit(0);
123 #endif
124 	switch(sig)
125 	{
126 		case SIGTERM:
127 #ifdef SIGQUIT
128 		case SIGQUIT:
129 #endif
130 #ifdef SIGBREAK
131 		case SIGBREAK:
132 #endif
133 		case SIGINT:
134 			sig_record_quit++;
135 			break;
136 #ifdef SIGHUP
137 		case SIGHUP:
138 			sig_record_reload++;
139 			break;
140 #endif
141 #ifdef SIGPIPE
142 		case SIGPIPE:
143 			break;
144 #endif
145 		default:
146 			/* ignoring signal */
147 			break;
148 	}
149 }
150 
151 /**
152  * Signal handling during the time when netevent is disabled.
153  * Stores signals to replay later.
154  */
155 static void
156 signal_handling_record(void)
157 {
158 	if( signal(SIGTERM, record_sigh) == SIG_ERR ||
159 #ifdef SIGQUIT
160 		signal(SIGQUIT, record_sigh) == SIG_ERR ||
161 #endif
162 #ifdef SIGBREAK
163 		signal(SIGBREAK, record_sigh) == SIG_ERR ||
164 #endif
165 #ifdef SIGHUP
166 		signal(SIGHUP, record_sigh) == SIG_ERR ||
167 #endif
168 #ifdef SIGPIPE
169 		signal(SIGPIPE, SIG_IGN) == SIG_ERR ||
170 #endif
171 		signal(SIGINT, record_sigh) == SIG_ERR
172 		)
173 		log_err("install sighandler: %s", strerror(errno));
174 }
175 
176 /**
177  * Replay old signals.
178  * @param wrk: worker that handles signals.
179  */
180 static void
181 signal_handling_playback(struct worker* wrk)
182 {
183 #ifdef SIGHUP
184 	if(sig_record_reload)
185 		worker_sighandler(SIGHUP, wrk);
186 #endif
187 	if(sig_record_quit)
188 		worker_sighandler(SIGTERM, wrk);
189 	sig_record_quit = 0;
190 	sig_record_reload = 0;
191 }
192 
193 struct daemon*
194 daemon_init(void)
195 {
196 	struct daemon* daemon = (struct daemon*)calloc(1,
197 		sizeof(struct daemon));
198 #ifdef USE_WINSOCK
199 	int r;
200 	WSADATA wsa_data;
201 #endif
202 	if(!daemon)
203 		return NULL;
204 #ifdef USE_WINSOCK
205 	r = WSAStartup(MAKEWORD(2,2), &wsa_data);
206 	if(r != 0) {
207 		fatal_exit("could not init winsock. WSAStartup: %s",
208 			wsa_strerror(r));
209 	}
210 #endif /* USE_WINSOCK */
211 	signal_handling_record();
212 	checklock_start();
213 #ifdef HAVE_SSL
214 #  ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
215 	ERR_load_crypto_strings();
216 #  endif
217 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
218 	ERR_load_SSL_strings();
219 #endif
220 #  ifdef USE_GOST
221 	(void)sldns_key_EVP_load_gost_id();
222 #  endif
223 #  if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
224 #    ifndef S_SPLINT_S
225 	OpenSSL_add_all_algorithms();
226 #    endif
227 #  else
228 	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
229 		| OPENSSL_INIT_ADD_ALL_DIGESTS
230 		| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
231 #  endif
232 #  if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS
233 	/* grab the COMP method ptr because openssl leaks it */
234 	comp_meth = (void*)SSL_COMP_get_compression_methods();
235 #  endif
236 #  if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
237 	(void)SSL_library_init();
238 #  else
239 	(void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
240 #  endif
241 #  if defined(HAVE_SSL) && defined(OPENSSL_THREADS) && !defined(THREADS_DISABLED)
242 	if(!ub_openssl_lock_init())
243 		fatal_exit("could not init openssl locks");
244 #  endif
245 #elif defined(HAVE_NSS)
246 	if(NSS_NoDB_Init(NULL) != SECSuccess)
247 		fatal_exit("could not init NSS");
248 #endif /* HAVE_SSL or HAVE_NSS */
249 #ifdef HAVE_TZSET
250 	/* init timezone info while we are not chrooted yet */
251 	tzset();
252 #endif
253 	daemon->need_to_exit = 0;
254 	modstack_init(&daemon->mods);
255 	if(!(daemon->env = (struct module_env*)calloc(1,
256 		sizeof(*daemon->env)))) {
257 		free(daemon);
258 		return NULL;
259 	}
260 	/* init edns_known_options */
261 	if(!edns_known_options_init(daemon->env)) {
262 		free(daemon->env);
263 		free(daemon);
264 		return NULL;
265 	}
266 	alloc_init(&daemon->superalloc, NULL, 0);
267 	daemon->acl = acl_list_create();
268 	if(!daemon->acl) {
269 		edns_known_options_delete(daemon->env);
270 		free(daemon->env);
271 		free(daemon);
272 		return NULL;
273 	}
274 	daemon->tcl = tcl_list_create();
275 	if(!daemon->tcl) {
276 		acl_list_delete(daemon->acl);
277 		edns_known_options_delete(daemon->env);
278 		free(daemon->env);
279 		free(daemon);
280 		return NULL;
281 	}
282 	if(gettimeofday(&daemon->time_boot, NULL) < 0)
283 		log_err("gettimeofday: %s", strerror(errno));
284 	daemon->time_last_stat = daemon->time_boot;
285 	if((daemon->env->auth_zones = auth_zones_create()) == 0) {
286 		acl_list_delete(daemon->acl);
287 		tcl_list_delete(daemon->tcl);
288 		edns_known_options_delete(daemon->env);
289 		free(daemon->env);
290 		free(daemon);
291 		return NULL;
292 	}
293 	return daemon;
294 }
295 
296 int
297 daemon_open_shared_ports(struct daemon* daemon)
298 {
299 	log_assert(daemon);
300 	if(daemon->cfg->port != daemon->listening_port) {
301 		size_t i;
302 		struct listen_port* p0;
303 		daemon->reuseport = 0;
304 		/* free and close old ports */
305 		if(daemon->ports != NULL) {
306 			for(i=0; i<daemon->num_ports; i++)
307 				listening_ports_free(daemon->ports[i]);
308 			free(daemon->ports);
309 			daemon->ports = NULL;
310 		}
311 		/* see if we want to reuseport */
312 #ifdef SO_REUSEPORT
313 		if(daemon->cfg->so_reuseport && daemon->cfg->num_threads > 0)
314 			daemon->reuseport = 1;
315 #endif
316 		/* try to use reuseport */
317 		p0 = listening_ports_open(daemon->cfg, &daemon->reuseport);
318 		if(!p0) {
319 			listening_ports_free(p0);
320 			return 0;
321 		}
322 		if(daemon->reuseport) {
323 			/* reuseport was successful, allocate for it */
324 			daemon->num_ports = (size_t)daemon->cfg->num_threads;
325 		} else {
326 			/* do the normal, singleportslist thing,
327 			 * reuseport not enabled or did not work */
328 			daemon->num_ports = 1;
329 		}
330 		if(!(daemon->ports = (struct listen_port**)calloc(
331 			daemon->num_ports, sizeof(*daemon->ports)))) {
332 			listening_ports_free(p0);
333 			return 0;
334 		}
335 		daemon->ports[0] = p0;
336 		if(daemon->reuseport) {
337 			/* continue to use reuseport */
338 			for(i=1; i<daemon->num_ports; i++) {
339 				if(!(daemon->ports[i]=
340 					listening_ports_open(daemon->cfg,
341 						&daemon->reuseport))
342 					|| !daemon->reuseport ) {
343 					for(i=0; i<daemon->num_ports; i++)
344 						listening_ports_free(daemon->ports[i]);
345 					free(daemon->ports);
346 					daemon->ports = NULL;
347 					return 0;
348 				}
349 			}
350 		}
351 		daemon->listening_port = daemon->cfg->port;
352 	}
353 	if(!daemon->cfg->remote_control_enable && daemon->rc_port) {
354 		listening_ports_free(daemon->rc_ports);
355 		daemon->rc_ports = NULL;
356 		daemon->rc_port = 0;
357 	}
358 	if(daemon->cfg->remote_control_enable &&
359 		daemon->cfg->control_port != daemon->rc_port) {
360 		listening_ports_free(daemon->rc_ports);
361 		if(!(daemon->rc_ports=daemon_remote_open_ports(daemon->cfg)))
362 			return 0;
363 		daemon->rc_port = daemon->cfg->control_port;
364 	}
365 	return 1;
366 }
367 
368 /**
369  * Setup modules. setup module stack.
370  * @param daemon: the daemon
371  */
372 static void daemon_setup_modules(struct daemon* daemon)
373 {
374 	daemon->env->cfg = daemon->cfg;
375 	daemon->env->alloc = &daemon->superalloc;
376 	daemon->env->worker = NULL;
377 	daemon->env->need_to_validate = 0; /* set by module init below */
378 	if(!modstack_setup(&daemon->mods, daemon->cfg->module_conf,
379 		daemon->env)) {
380 		fatal_exit("failed to setup modules");
381 	}
382 	log_edns_known_options(VERB_ALGO, daemon->env);
383 }
384 
385 /**
386  * Obtain allowed port numbers, concatenate the list, and shuffle them
387  * (ready to be handed out to threads).
388  * @param daemon: the daemon. Uses rand and cfg.
389  * @param shufport: the portlist output.
390  * @return number of ports available.
391  */
392 static int daemon_get_shufport(struct daemon* daemon, int* shufport)
393 {
394 	int i, n, k, temp;
395 	int avail = 0;
396 	for(i=0; i<65536; i++) {
397 		if(daemon->cfg->outgoing_avail_ports[i]) {
398 			shufport[avail++] = daemon->cfg->
399 				outgoing_avail_ports[i];
400 		}
401 	}
402 	if(avail == 0)
403 		fatal_exit("no ports are permitted for UDP, add "
404 			"with outgoing-port-permit");
405         /* Knuth shuffle */
406 	n = avail;
407 	while(--n > 0) {
408 		k = ub_random_max(daemon->rand, n+1); /* 0<= k<= n */
409 		temp = shufport[k];
410 		shufport[k] = shufport[n];
411 		shufport[n] = temp;
412 	}
413 	return avail;
414 }
415 
416 /**
417  * Allocate empty worker structures. With backptr and thread-number,
418  * from 0..numthread initialised. Used as user arguments to new threads.
419  * Creates the daemon random generator if it does not exist yet.
420  * The random generator stays existing between reloads with a unique state.
421  * @param daemon: the daemon with (new) config settings.
422  */
423 static void
424 daemon_create_workers(struct daemon* daemon)
425 {
426 	int i, numport;
427 	int* shufport;
428 	log_assert(daemon && daemon->cfg);
429 	if(!daemon->rand) {
430 		daemon->rand = ub_initstate(NULL);
431 		if(!daemon->rand)
432 			fatal_exit("could not init random generator");
433 		hash_set_raninit((uint32_t)ub_random(daemon->rand));
434 	}
435 	shufport = (int*)calloc(65536, sizeof(int));
436 	if(!shufport)
437 		fatal_exit("out of memory during daemon init");
438 	numport = daemon_get_shufport(daemon, shufport);
439 	verbose(VERB_ALGO, "total of %d outgoing ports available", numport);
440 
441 	daemon->num = (daemon->cfg->num_threads?daemon->cfg->num_threads:1);
442 	if(daemon->reuseport && (int)daemon->num < (int)daemon->num_ports) {
443 		log_warn("cannot reduce num-threads to %d because so-reuseport "
444 			"so continuing with %d threads.", (int)daemon->num,
445 			(int)daemon->num_ports);
446 		daemon->num = (int)daemon->num_ports;
447 	}
448 	daemon->workers = (struct worker**)calloc((size_t)daemon->num,
449 		sizeof(struct worker*));
450 	if(!daemon->workers)
451 		fatal_exit("out of memory during daemon init");
452 	if(daemon->cfg->dnstap) {
453 #ifdef USE_DNSTAP
454 		daemon->dtenv = dt_create(daemon->cfg->dnstap_socket_path,
455 			(unsigned int)daemon->num);
456 		if (!daemon->dtenv)
457 			fatal_exit("dt_create failed");
458 		dt_apply_cfg(daemon->dtenv, daemon->cfg);
459 #else
460 		fatal_exit("dnstap enabled in config but not built with dnstap support");
461 #endif
462 	}
463 	for(i=0; i<daemon->num; i++) {
464 		if(!(daemon->workers[i] = worker_create(daemon, i,
465 			shufport+numport*i/daemon->num,
466 			numport*(i+1)/daemon->num - numport*i/daemon->num)))
467 			/* the above is not ports/numthr, due to rounding */
468 			fatal_exit("could not create worker");
469 	}
470 	free(shufport);
471 }
472 
473 #ifdef THREADS_DISABLED
474 /**
475  * Close all pipes except for the numbered thread.
476  * @param daemon: daemon to close pipes in.
477  * @param thr: thread number 0..num-1 of thread to skip.
478  */
479 static void close_other_pipes(struct daemon* daemon, int thr)
480 {
481 	int i;
482 	for(i=0; i<daemon->num; i++)
483 		if(i!=thr) {
484 			if(i==0) {
485 				/* only close read part, need to write stats */
486 				tube_close_read(daemon->workers[i]->cmd);
487 			} else {
488 				/* complete close channel to others */
489 				tube_delete(daemon->workers[i]->cmd);
490 				daemon->workers[i]->cmd = NULL;
491 			}
492 		}
493 }
494 #endif /* THREADS_DISABLED */
495 
496 /**
497  * Function to start one thread.
498  * @param arg: user argument.
499  * @return: void* user return value could be used for thread_join results.
500  */
501 static void*
502 thread_start(void* arg)
503 {
504 	struct worker* worker = (struct worker*)arg;
505 	int port_num = 0;
506 	log_thread_set(&worker->thread_num);
507 	ub_thread_blocksigs();
508 #ifdef THREADS_DISABLED
509 	/* close pipe ends used by main */
510 	tube_close_write(worker->cmd);
511 	close_other_pipes(worker->daemon, worker->thread_num);
512 #endif
513 #ifdef SO_REUSEPORT
514 	if(worker->daemon->cfg->so_reuseport)
515 		port_num = worker->thread_num % worker->daemon->num_ports;
516 	else
517 		port_num = 0;
518 #endif
519 	if(!worker_init(worker, worker->daemon->cfg,
520 			worker->daemon->ports[port_num], 0))
521 		fatal_exit("Could not initialize thread");
522 
523 	worker_work(worker);
524 	return NULL;
525 }
526 
527 /**
528  * Fork and init the other threads. Main thread returns for special handling.
529  * @param daemon: the daemon with other threads to fork.
530  */
531 static void
532 daemon_start_others(struct daemon* daemon)
533 {
534 	int i;
535 	log_assert(daemon);
536 	verbose(VERB_ALGO, "start threads");
537 	/* skip i=0, is this thread */
538 	for(i=1; i<daemon->num; i++) {
539 		ub_thread_create(&daemon->workers[i]->thr_id,
540 			thread_start, daemon->workers[i]);
541 #ifdef THREADS_DISABLED
542 		/* close pipe end of child */
543 		tube_close_read(daemon->workers[i]->cmd);
544 #endif /* no threads */
545 	}
546 }
547 
548 /**
549  * Stop the other threads.
550  * @param daemon: the daemon with other threads.
551  */
552 static void
553 daemon_stop_others(struct daemon* daemon)
554 {
555 	int i;
556 	log_assert(daemon);
557 	verbose(VERB_ALGO, "stop threads");
558 	/* skip i=0, is this thread */
559 	/* use i=0 buffer for sending cmds; because we are #0 */
560 	for(i=1; i<daemon->num; i++) {
561 		worker_send_cmd(daemon->workers[i], worker_cmd_quit);
562 	}
563 	/* wait for them to quit */
564 	for(i=1; i<daemon->num; i++) {
565 		/* join it to make sure its dead */
566 		verbose(VERB_ALGO, "join %d", i);
567 		ub_thread_join(daemon->workers[i]->thr_id);
568 		verbose(VERB_ALGO, "join success %d", i);
569 	}
570 }
571 
572 void
573 daemon_fork(struct daemon* daemon)
574 {
575 	int have_view_respip_cfg = 0;
576 #ifdef HAVE_SYSTEMD
577 	int ret;
578 #endif
579 
580 	log_assert(daemon);
581 	if(!(daemon->views = views_create()))
582 		fatal_exit("Could not create views: out of memory");
583 	/* create individual views and their localzone/data trees */
584 	if(!views_apply_cfg(daemon->views, daemon->cfg))
585 		fatal_exit("Could not set up views");
586 
587 	if(!acl_list_apply_cfg(daemon->acl, daemon->cfg, daemon->views))
588 		fatal_exit("Could not setup access control list");
589 	if(!tcl_list_apply_cfg(daemon->tcl, daemon->cfg))
590 		fatal_exit("Could not setup TCP connection limits");
591 	if(daemon->cfg->dnscrypt) {
592 #ifdef USE_DNSCRYPT
593 		daemon->dnscenv = dnsc_create();
594 		if (!daemon->dnscenv)
595 			fatal_exit("dnsc_create failed");
596 		dnsc_apply_cfg(daemon->dnscenv, daemon->cfg);
597 #else
598 		fatal_exit("dnscrypt enabled in config but unbound was not built with "
599 				   "dnscrypt support");
600 #endif
601 	}
602 	/* create global local_zones */
603 	if(!(daemon->local_zones = local_zones_create()))
604 		fatal_exit("Could not create local zones: out of memory");
605 	if(!local_zones_apply_cfg(daemon->local_zones, daemon->cfg))
606 		fatal_exit("Could not set up local zones");
607 
608 	/* process raw response-ip configuration data */
609 	if(!(daemon->respip_set = respip_set_create()))
610 		fatal_exit("Could not create response IP set");
611 	if(!respip_global_apply_cfg(daemon->respip_set, daemon->cfg))
612 		fatal_exit("Could not set up response IP set");
613 	if(!respip_views_apply_cfg(daemon->views, daemon->cfg,
614 		&have_view_respip_cfg))
615 		fatal_exit("Could not set up per-view response IP sets");
616 	daemon->use_response_ip = !respip_set_is_empty(daemon->respip_set) ||
617 		have_view_respip_cfg;
618 
619 	/* read auth zonefiles */
620 	if(!auth_zones_apply_cfg(daemon->env->auth_zones, daemon->cfg, 1,
621 		&daemon->use_rpz))
622 		fatal_exit("auth_zones could not be setup");
623 
624 	/* setup modules */
625 	daemon_setup_modules(daemon);
626 
627 	/* response-ip-xxx options don't work as expected without the respip
628 	 * module.  To avoid run-time operational surprise we reject such
629 	 * configuration. */
630 	if(daemon->use_response_ip &&
631 		modstack_find(&daemon->mods, "respip") < 0)
632 		fatal_exit("response-ip options require respip module");
633 	/* RPZ response ip triggers don't work as expected without the respip
634 	 * module.  To avoid run-time operational surprise we reject such
635 	 * configuration. */
636 	if(daemon->use_rpz &&
637 		modstack_find(&daemon->mods, "respip") < 0)
638 		fatal_exit("RPZ requires the respip module");
639 
640 	/* first create all the worker structures, so we can pass
641 	 * them to the newly created threads.
642 	 */
643 	daemon_create_workers(daemon);
644 
645 #if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
646 	/* in libev the first inited base gets signals */
647 	if(!worker_init(daemon->workers[0], daemon->cfg, daemon->ports[0], 1))
648 		fatal_exit("Could not initialize main thread");
649 #endif
650 
651 	/* Now create the threads and init the workers.
652 	 * By the way, this is thread #0 (the main thread).
653 	 */
654 	daemon_start_others(daemon);
655 
656 	/* Special handling for the main thread. This is the thread
657 	 * that handles signals and remote control.
658 	 */
659 #if !(defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP))
660 	/* libevent has the last inited base get signals (or any base) */
661 	if(!worker_init(daemon->workers[0], daemon->cfg, daemon->ports[0], 1))
662 		fatal_exit("Could not initialize main thread");
663 #endif
664 	signal_handling_playback(daemon->workers[0]);
665 
666 	if (!shm_main_init(daemon))
667 		log_warn("SHM has failed");
668 
669 	/* Start resolver service on main thread. */
670 #ifdef HAVE_SYSTEMD
671 	ret = sd_notify(0, "READY=1");
672 	if(ret <= 0 && getenv("NOTIFY_SOCKET"))
673 		fatal_exit("sd_notify failed %s: %s. Make sure that unbound has "
674 				"access/permission to use the socket presented by systemd.",
675 				getenv("NOTIFY_SOCKET"),
676 				(ret==0?"no $NOTIFY_SOCKET": strerror(-ret)));
677 #endif
678 	log_info("start of service (%s).", PACKAGE_STRING);
679 	worker_work(daemon->workers[0]);
680 #ifdef HAVE_SYSTEMD
681 	if (daemon->workers[0]->need_to_exit)
682 		sd_notify(0, "STOPPING=1");
683 	else
684 		sd_notify(0, "RELOADING=1");
685 #endif
686 	log_info("service stopped (%s).", PACKAGE_STRING);
687 
688 	/* we exited! a signal happened! Stop other threads */
689 	daemon_stop_others(daemon);
690 
691 	/* Shutdown SHM */
692 	shm_main_shutdown(daemon);
693 
694 	daemon->need_to_exit = daemon->workers[0]->need_to_exit;
695 }
696 
697 void
698 daemon_cleanup(struct daemon* daemon)
699 {
700 	int i;
701 	log_assert(daemon);
702 	/* before stopping main worker, handle signals ourselves, so we
703 	   don't die on multiple reload signals for example. */
704 	signal_handling_record();
705 	log_thread_set(NULL);
706 	/* clean up caches because
707 	 * a) RRset IDs will be recycled after a reload, causing collisions
708 	 * b) validation config can change, thus rrset, msg, keycache clear */
709 	slabhash_clear(&daemon->env->rrset_cache->table);
710 	slabhash_clear(daemon->env->msg_cache);
711 	local_zones_delete(daemon->local_zones);
712 	daemon->local_zones = NULL;
713 	respip_set_delete(daemon->respip_set);
714 	daemon->respip_set = NULL;
715 	views_delete(daemon->views);
716 	daemon->views = NULL;
717 	if(daemon->env->auth_zones)
718 		auth_zones_cleanup(daemon->env->auth_zones);
719 	/* key cache is cleared by module desetup during next daemon_fork() */
720 	daemon_remote_clear(daemon->rc);
721 	for(i=0; i<daemon->num; i++)
722 		worker_delete(daemon->workers[i]);
723 	free(daemon->workers);
724 	daemon->workers = NULL;
725 	daemon->num = 0;
726 	alloc_clear_special(&daemon->superalloc);
727 #ifdef USE_DNSTAP
728 	dt_delete(daemon->dtenv);
729 	daemon->dtenv = NULL;
730 #endif
731 #ifdef USE_DNSCRYPT
732 	dnsc_delete(daemon->dnscenv);
733 	daemon->dnscenv = NULL;
734 #endif
735 	daemon->cfg = NULL;
736 }
737 
738 void
739 daemon_delete(struct daemon* daemon)
740 {
741 	size_t i;
742 	if(!daemon)
743 		return;
744 	modstack_desetup(&daemon->mods, daemon->env);
745 	daemon_remote_delete(daemon->rc);
746 	for(i = 0; i < daemon->num_ports; i++)
747 		listening_ports_free(daemon->ports[i]);
748 	free(daemon->ports);
749 	listening_ports_free(daemon->rc_ports);
750 	if(daemon->env) {
751 		slabhash_delete(daemon->env->msg_cache);
752 		rrset_cache_delete(daemon->env->rrset_cache);
753 		infra_delete(daemon->env->infra_cache);
754 		edns_known_options_delete(daemon->env);
755 		auth_zones_delete(daemon->env->auth_zones);
756 	}
757 	ub_randfree(daemon->rand);
758 	alloc_clear(&daemon->superalloc);
759 	acl_list_delete(daemon->acl);
760 	tcl_list_delete(daemon->tcl);
761 	free(daemon->chroot);
762 	free(daemon->pidfile);
763 	free(daemon->env);
764 #ifdef HAVE_SSL
765 	listen_sslctx_delete_ticket_keys();
766 	SSL_CTX_free((SSL_CTX*)daemon->listen_sslctx);
767 	SSL_CTX_free((SSL_CTX*)daemon->connect_sslctx);
768 #endif
769 	free(daemon);
770 	/* lex cleanup */
771 	ub_c_lex_destroy();
772 	/* libcrypto cleanup */
773 #ifdef HAVE_SSL
774 #  if defined(USE_GOST) && defined(HAVE_LDNS_KEY_EVP_UNLOAD_GOST)
775 	sldns_key_EVP_unload_gost();
776 #  endif
777 #  if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS && HAVE_DECL_SK_SSL_COMP_POP_FREE
778 #    ifndef S_SPLINT_S
779 #      if OPENSSL_VERSION_NUMBER < 0x10100000
780 	sk_SSL_COMP_pop_free(comp_meth, (void(*)())CRYPTO_free);
781 #      endif
782 #    endif
783 #  endif
784 #  ifdef HAVE_OPENSSL_CONFIG
785 	EVP_cleanup();
786 #  if (OPENSSL_VERSION_NUMBER < 0x10100000) && !defined(OPENSSL_NO_ENGINE)
787 	ENGINE_cleanup();
788 #  endif
789 	CONF_modules_free();
790 #  endif
791 #  ifdef HAVE_CRYPTO_CLEANUP_ALL_EX_DATA
792 	CRYPTO_cleanup_all_ex_data(); /* safe, no more threads right now */
793 #  endif
794 #  ifdef HAVE_ERR_FREE_STRINGS
795 	ERR_free_strings();
796 #  endif
797 #  if OPENSSL_VERSION_NUMBER < 0x10100000
798 	RAND_cleanup();
799 #  endif
800 #  if defined(HAVE_SSL) && defined(OPENSSL_THREADS) && !defined(THREADS_DISABLED)
801 	ub_openssl_lock_delete();
802 #  endif
803 #ifndef HAVE_ARC4RANDOM
804 	_ARC4_LOCK_DESTROY();
805 #endif
806 #elif defined(HAVE_NSS)
807 	NSS_Shutdown();
808 #endif /* HAVE_SSL or HAVE_NSS */
809 	checklock_stop();
810 #ifdef USE_WINSOCK
811 	if(WSACleanup() != 0) {
812 		log_err("Could not WSACleanup: %s",
813 			wsa_strerror(WSAGetLastError()));
814 	}
815 #endif
816 }
817 
818 void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg)
819 {
820         daemon->cfg = cfg;
821 	config_apply(cfg);
822 	if(!slabhash_is_size(daemon->env->msg_cache, cfg->msg_cache_size,
823 	   	cfg->msg_cache_slabs)) {
824 		slabhash_delete(daemon->env->msg_cache);
825 		daemon->env->msg_cache = slabhash_create(cfg->msg_cache_slabs,
826 			HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size,
827 			msgreply_sizefunc, query_info_compare,
828 			query_entry_delete, reply_info_delete, NULL);
829 		if(!daemon->env->msg_cache) {
830 			fatal_exit("malloc failure updating config settings");
831 		}
832 	}
833 	if((daemon->env->rrset_cache = rrset_cache_adjust(
834 		daemon->env->rrset_cache, cfg, &daemon->superalloc)) == 0)
835 		fatal_exit("malloc failure updating config settings");
836 	if((daemon->env->infra_cache = infra_adjust(daemon->env->infra_cache,
837 		cfg))==0)
838 		fatal_exit("malloc failure updating config settings");
839 }
840