1/*
2 * daemon/daemon.h - 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#ifndef DAEMON_H
43#define DAEMON_H
44
45#include "util/locks.h"
46#include "util/alloc.h"
47#include "services/modstack.h"
48#ifdef UB_ON_WINDOWS
49#  include "util/winsock_event.h"
50#endif
51struct config_file;
52struct worker;
53struct listen_port;
54struct slabhash;
55struct module_env;
56struct rrset_cache;
57struct acl_list;
58struct local_zones;
59struct ub_randstate;
60struct daemon_remote;
61
62#include "dnstap/dnstap_config.h"
63#ifdef USE_DNSTAP
64struct dt_env;
65#endif
66
67/**
68 * Structure holding worker list.
69 * Holds globally visible information.
70 */
71struct daemon {
72	/** The config settings */
73	struct config_file* cfg;
74	/** the chroot dir in use, NULL if none */
75	char* chroot;
76	/** pidfile that is used */
77	char* pidfile;
78	/** port number that has ports opened. */
79	int listening_port;
80	/** array of listening ports, opened.  Listening ports per worker,
81	 * or just one element[0] shared by the worker threads. */
82	struct listen_port** ports;
83	/** size of ports array */
84	size_t num_ports;
85	/** reuseport is enabled if true */
86	int reuseport;
87	/** port number for remote that has ports opened. */
88	int rc_port;
89	/** listening ports for remote control */
90	struct listen_port* rc_ports;
91	/** remote control connections management (for first worker) */
92	struct daemon_remote* rc;
93	/** ssl context for listening to dnstcp over ssl, and connecting ssl */
94	void* listen_sslctx, *connect_sslctx;
95	/** num threads allocated */
96	int num;
97	/** the worker entries */
98	struct worker** workers;
99	/** do we need to exit unbound (or is it only a reload?) */
100	int need_to_exit;
101	/** master random table ; used for port div between threads on reload*/
102	struct ub_randstate* rand;
103	/** master allocation cache */
104	struct alloc_cache superalloc;
105	/** the module environment master value, copied and changed by threads*/
106	struct module_env* env;
107	/** stack of module callbacks */
108	struct module_stack mods;
109	/** access control, which client IPs are allowed to connect */
110	struct acl_list* acl;
111	/** local authority zones */
112	struct local_zones* local_zones;
113	/** last time of statistics printout */
114	struct timeval time_last_stat;
115	/** time when daemon started */
116	struct timeval time_boot;
117#ifdef USE_DNSTAP
118	/** the dnstap environment master value, copied and changed by threads*/
119	struct dt_env* dtenv;
120#endif
121};
122
123/**
124 * Initialize daemon structure.
125 * @return: The daemon structure, or NULL on error.
126 */
127struct daemon* daemon_init(void);
128
129/**
130 * Open shared listening ports (if needed).
131 * The cfg member pointer must have been set for the daemon.
132 * @param daemon: the daemon.
133 * @return: false on error.
134 */
135int daemon_open_shared_ports(struct daemon* daemon);
136
137/**
138 * Fork workers and start service.
139 * When the routine exits, it is no longer forked.
140 * @param daemon: the daemon.
141 */
142void daemon_fork(struct daemon* daemon);
143
144/**
145 * Close off the worker thread information.
146 * Bring the daemon back into state ready for daemon_fork again.
147 * @param daemon: the daemon.
148 */
149void daemon_cleanup(struct daemon* daemon);
150
151/**
152 * Delete workers, close listening ports.
153 * @param daemon: the daemon.
154 */
155void daemon_delete(struct daemon* daemon);
156
157/**
158 * Apply config settings.
159 * @param daemon: the daemon.
160 * @param cfg: new config settings.
161 */
162void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg);
163
164#endif /* DAEMON_H */
165