xref: /freebsd/contrib/unbound/daemon/daemon.c (revision f05cddf9)
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 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.
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_NSS
60 /* nss3 */
61 #include "nss.h"
62 #endif
63 
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"
69 #include "util/log.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"
83 #include <signal.h>
84 
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;
89 
90 #if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS
91 /** cleaner ssl memory freeup */
92 static void* comp_meth = NULL;
93 #endif
94 #ifdef LEX_HAS_YYLEX_DESTROY
95 /** remove buffers for parsing and init */
96 int ub_c_lex_destroy(void);
97 #endif
98 
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).
103   */
104 static RETSIGTYPE record_sigh(int sig)
105 {
106 #ifdef LIBEVENT_SIGNAL_PROBLEM
107 	verbose(VERB_OPS, "quit on signal, no cleanup and statistics, "
108 		"because installed libevent version is not threadsafe");
109 	exit(0);
110 #endif
111 	switch(sig)
112 	{
113 		case SIGTERM:
114 #ifdef SIGQUIT
115 		case SIGQUIT:
116 #endif
117 #ifdef SIGBREAK
118 		case SIGBREAK:
119 #endif
120 		case SIGINT:
121 			sig_record_quit++;
122 			break;
123 #ifdef SIGHUP
124 		case SIGHUP:
125 			sig_record_reload++;
126 			break;
127 #endif
128 #ifdef SIGPIPE
129 		case SIGPIPE:
130 			break;
131 #endif
132 		default:
133 			log_err("ignoring signal %d", sig);
134 	}
135 }
136 
137 /**
138  * Signal handling during the time when netevent is disabled.
139  * Stores signals to replay later.
140  */
141 static void
142 signal_handling_record(void)
143 {
144 	if( signal(SIGTERM, record_sigh) == SIG_ERR ||
145 #ifdef SIGQUIT
146 		signal(SIGQUIT, record_sigh) == SIG_ERR ||
147 #endif
148 #ifdef SIGBREAK
149 		signal(SIGBREAK, record_sigh) == SIG_ERR ||
150 #endif
151 #ifdef SIGHUP
152 		signal(SIGHUP, record_sigh) == SIG_ERR ||
153 #endif
154 #ifdef SIGPIPE
155 		signal(SIGPIPE, SIG_IGN) == SIG_ERR ||
156 #endif
157 		signal(SIGINT, record_sigh) == SIG_ERR
158 		)
159 		log_err("install sighandler: %s", strerror(errno));
160 }
161 
162 /**
163  * Replay old signals.
164  * @param wrk: worker that handles signals.
165  */
166 static void
167 signal_handling_playback(struct worker* wrk)
168 {
169 #ifdef SIGHUP
170 	if(sig_record_reload)
171 		worker_sighandler(SIGHUP, wrk);
172 #endif
173 	if(sig_record_quit)
174 		worker_sighandler(SIGTERM, wrk);
175 	sig_record_quit = 0;
176 	sig_record_reload = 0;
177 }
178 
179 struct daemon*
180 daemon_init(void)
181 {
182 	struct daemon* daemon = (struct daemon*)calloc(1,
183 		sizeof(struct daemon));
184 #ifdef USE_WINSOCK
185 	int r;
186 	WSADATA wsa_data;
187 #endif
188 	if(!daemon)
189 		return NULL;
190 #ifdef USE_WINSOCK
191 	r = WSAStartup(MAKEWORD(2,2), &wsa_data);
192 	if(r != 0) {
193 		fatal_exit("could not init winsock. WSAStartup: %s",
194 			wsa_strerror(r));
195 	}
196 #endif /* USE_WINSOCK */
197 	signal_handling_record();
198 	checklock_start();
199 #ifdef HAVE_SSL
200 	ERR_load_crypto_strings();
201 	ERR_load_SSL_strings();
202 #  ifdef HAVE_OPENSSL_CONFIG
203 	OPENSSL_config("unbound");
204 #  endif
205 #  ifdef USE_GOST
206 	(void)ldns_key_EVP_load_gost_id();
207 #  endif
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();
212 #  endif
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");
217 #  endif
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 */
222 #ifdef HAVE_TZSET
223 	/* init timezone info while we are not chrooted yet */
224 	tzset();
225 #endif
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)))) {
232 		free(daemon);
233 		return NULL;
234 	}
235 	alloc_init(&daemon->superalloc, NULL, 0);
236 	daemon->acl = acl_list_create();
237 	if(!daemon->acl) {
238 		free(daemon->env);
239 		free(daemon);
240 		return NULL;
241 	}
242 	if(gettimeofday(&daemon->time_boot, NULL) < 0)
243 		log_err("gettimeofday: %s", strerror(errno));
244 	daemon->time_last_stat = daemon->time_boot;
245 	return daemon;
246 }
247 
248 int
249 daemon_open_shared_ports(struct daemon* daemon)
250 {
251 	log_assert(daemon);
252 	if(daemon->cfg->port != daemon->listening_port) {
253 		listening_ports_free(daemon->ports);
254 		if(!(daemon->ports=listening_ports_open(daemon->cfg)))
255 			return 0;
256 		daemon->listening_port = daemon->cfg->port;
257 	}
258 	if(!daemon->cfg->remote_control_enable && daemon->rc_port) {
259 		listening_ports_free(daemon->rc_ports);
260 		daemon->rc_ports = NULL;
261 		daemon->rc_port = 0;
262 	}
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)))
267 			return 0;
268 		daemon->rc_port = daemon->cfg->control_port;
269 	}
270 	return 1;
271 }
272 
273 /**
274  * Setup modules. setup module stack.
275  * @param daemon: the daemon
276  */
277 static void daemon_setup_modules(struct daemon* daemon)
278 {
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,
284 		daemon->env)) {
285 		fatal_exit("failed to setup modules");
286 	}
287 }
288 
289 /**
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.
295  */
296 static int daemon_get_shufport(struct daemon* daemon, int* shufport)
297 {
298 	int i, n, k, temp;
299 	int avail = 0;
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];
304 		}
305 	}
306 	if(avail == 0)
307 		fatal_exit("no ports are permitted for UDP, add "
308 			"with outgoing-port-permit");
309         /* Knuth shuffle */
310 	n = avail;
311 	while(--n > 0) {
312 		k = ub_random_max(daemon->rand, n+1); /* 0<= k<= n */
313 		temp = shufport[k];
314 		shufport[k] = shufport[n];
315 		shufport[n] = temp;
316 	}
317 	return avail;
318 }
319 
320 /**
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.
326  */
327 static void
328 daemon_create_workers(struct daemon* daemon)
329 {
330 	int i, numport;
331 	int* shufport;
332 	log_assert(daemon && daemon->cfg);
333 	if(!daemon->rand) {
334 		unsigned int seed = (unsigned int)time(NULL) ^
335 			(unsigned int)getpid() ^ 0x438;
336 		daemon->rand = ub_initstate(seed, NULL);
337 		if(!daemon->rand)
338 			fatal_exit("could not init random generator");
339 	}
340 	hash_set_raninit((uint32_t)ub_random(daemon->rand));
341 	shufport = (int*)calloc(65536, sizeof(int));
342 	if(!shufport)
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);
346 
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");
356 	}
357 	free(shufport);
358 }
359 
360 #ifdef THREADS_DISABLED
361 /**
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.
365  */
366 static void close_other_pipes(struct daemon* daemon, int thr)
367 {
368 	int i;
369 	for(i=0; i<daemon->num; i++)
370 		if(i!=thr) {
371 			if(i==0) {
372 				/* only close read part, need to write stats */
373 				tube_close_read(daemon->workers[i]->cmd);
374 			} else {
375 				/* complete close channel to others */
376 				tube_delete(daemon->workers[i]->cmd);
377 				daemon->workers[i]->cmd = NULL;
378 			}
379 		}
380 }
381 #endif /* THREADS_DISABLED */
382 
383 /**
384  * Function to start one thread.
385  * @param arg: user argument.
386  * @return: void* user return value could be used for thread_join results.
387  */
388 static void*
389 thread_start(void* arg)
390 {
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);
398 #endif
399 	if(!worker_init(worker, worker->daemon->cfg, worker->daemon->ports, 0))
400 		fatal_exit("Could not initialize thread");
401 
402 	worker_work(worker);
403 	return NULL;
404 }
405 
406 /**
407  * Fork and init the other threads. Main thread returns for special handling.
408  * @param daemon: the daemon with other threads to fork.
409  */
410 static void
411 daemon_start_others(struct daemon* daemon)
412 {
413 	int i;
414 	log_assert(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 */
424 	}
425 }
426 
427 /**
428  * Stop the other threads.
429  * @param daemon: the daemon with other threads.
430  */
431 static void
432 daemon_stop_others(struct daemon* daemon)
433 {
434 	int i;
435 	log_assert(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);
441 	}
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);
448 	}
449 }
450 
451 void
452 daemon_fork(struct daemon* daemon)
453 {
454 	log_assert(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");
461 
462 	/* setup modules */
463 	daemon_setup_modules(daemon);
464 
465 	/* first create all the worker structures, so we can pass
466 	 * them to the newly created threads.
467 	 */
468 	daemon_create_workers(daemon);
469 
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");
474 #endif
475 
476 	/* Now create the threads and init the workers.
477 	 * By the way, this is thread #0 (the main thread).
478 	 */
479 	daemon_start_others(daemon);
480 
481 	/* Special handling for the main thread. This is the thread
482 	 * that handles signals and remote control.
483 	 */
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");
488 #endif
489 	signal_handling_playback(daemon->workers[0]);
490 
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);
495 
496 	/* we exited! a signal happened! Stop other threads */
497 	daemon_stop_others(daemon);
498 
499 	daemon->need_to_exit = daemon->workers[0]->need_to_exit;
500 }
501 
502 void
503 daemon_cleanup(struct daemon* daemon)
504 {
505 	int i;
506 	log_assert(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;
525 	daemon->num = 0;
526 	daemon->cfg = NULL;
527 }
528 
529 void
530 daemon_delete(struct daemon* daemon)
531 {
532 	if(!daemon)
533 		return;
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);
538 	if(daemon->env) {
539 		slabhash_delete(daemon->env->msg_cache);
540 		rrset_cache_delete(daemon->env->rrset_cache);
541 		infra_delete(daemon->env->infra_cache);
542 	}
543 	ub_randfree(daemon->rand);
544 	alloc_clear(&daemon->superalloc);
545 	acl_list_delete(daemon->acl);
546 	free(daemon->chroot);
547 	free(daemon->pidfile);
548 	free(daemon->env);
549 #ifdef HAVE_SSL
550 	SSL_CTX_free((SSL_CTX*)daemon->listen_sslctx);
551 	SSL_CTX_free((SSL_CTX*)daemon->connect_sslctx);
552 #endif
553 	free(daemon);
554 #ifdef LEX_HAS_YYLEX_DESTROY
555 	/* lex cleanup */
556 	ub_c_lex_destroy();
557 #endif
558 	/* libcrypto cleanup */
559 #ifdef HAVE_SSL
560 #  if defined(USE_GOST) && defined(HAVE_LDNS_KEY_EVP_UNLOAD_GOST)
561 	ldns_key_EVP_unload_gost();
562 #  endif
563 #  if HAVE_DECL_SSL_COMP_GET_COMPRESSION_METHODS && HAVE_DECL_SK_SSL_COMP_POP_FREE
564 #    ifndef S_SPLINT_S
565 	sk_SSL_COMP_pop_free(comp_meth, (void(*)())CRYPTO_free);
566 #    endif
567 #  endif
568 #  ifdef HAVE_OPENSSL_CONFIG
569 	EVP_cleanup();
570 	ENGINE_cleanup();
571 	CONF_modules_free();
572 #  endif
573 	CRYPTO_cleanup_all_ex_data(); /* safe, no more threads right now */
574 	ERR_remove_state(0);
575 	ERR_free_strings();
576 	RAND_cleanup();
577 #  if defined(HAVE_SSL) && defined(OPENSSL_THREADS) && !defined(THREADS_DISABLED)
578 	ub_openssl_lock_delete();
579 #  endif
580 #elif defined(HAVE_NSS)
581 	NSS_Shutdown();
582 #endif /* HAVE_SSL or HAVE_NSS */
583 	checklock_stop();
584 #ifdef USE_WINSOCK
585 	if(WSACleanup() != 0) {
586 		log_err("Could not WSACleanup: %s",
587 			wsa_strerror(WSAGetLastError()));
588 	}
589 #endif
590 }
591 
592 void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg)
593 {
594         daemon->cfg = cfg;
595 	config_apply(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");
606 		}
607 	}
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,
612 		cfg))==0)
613 		fatal_exit("malloc failure updating config settings");
614 }
615