xref: /freebsd/contrib/unbound/daemon/daemon.h (revision d439598dd0d341b0c0b77151ba904e09c42f8421)
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 struct config_file;
49 struct worker;
50 struct listen_port;
51 struct slabhash;
52 struct module_env;
53 struct rrset_cache;
54 struct acl_list;
55 struct local_zones;
56 struct views;
57 struct ub_randstate;
58 struct daemon_remote;
59 struct respip_set;
60 struct shm_main_info;
61 struct cookie_secrets;
62 
63 #include "dnstap/dnstap_config.h"
64 #ifdef USE_DNSTAP
65 struct dt_env;
66 #endif
67 
68 #include "dnscrypt/dnscrypt_config.h"
69 #ifdef USE_DNSCRYPT
70 struct dnsc_env;
71 #endif
72 
73 /**
74  * Structure holding worker list.
75  * Holds globally visible information.
76  */
77 struct daemon {
78 	/** The config settings */
79 	struct config_file* cfg;
80 	/** the chroot dir in use, NULL if none */
81 	char* chroot;
82 	/** pidfile that is used */
83 	char* pidfile;
84 	/** port number that has ports opened. */
85 	int listening_port;
86 	/** array of listening ports, opened.  Listening ports per worker,
87 	 * or just one element[0] shared by the worker threads. */
88 	struct listen_port** ports;
89 	/** size of ports array */
90 	size_t num_ports;
91 	/** reuseport is enabled if true */
92 	int reuseport;
93 	/** port number for remote that has ports opened. */
94 	int rc_port;
95 	/** listening ports for remote control */
96 	struct listen_port* rc_ports;
97 	/** remote control connections management (for first worker) */
98 	struct daemon_remote* rc;
99 	/** ssl context for listening to dnstcp over ssl, and connecting ssl */
100 	void* listen_sslctx, *connect_sslctx;
101 	/** num threads allocated */
102 	int num;
103 	/** num threads allocated in the previous config or 0 at first */
104 	int old_num;
105 	/** the worker entries */
106 	struct worker** workers;
107 	/** per-worker allocation cache */
108 	struct alloc_cache **worker_allocs;
109 	/** do we need to exit unbound (or is it only a reload?) */
110 	int need_to_exit;
111 	/** master random table ; used for port div between threads on reload*/
112 	struct ub_randstate* rand;
113 	/** master allocation cache */
114 	struct alloc_cache superalloc;
115 	/** the module environment master value, copied and changed by threads*/
116 	struct module_env* env;
117 	/** stack of module callbacks */
118 	struct module_stack mods;
119 	/** The module stack has been inited */
120 	int mods_inited;
121 	/** access control, which client IPs are allowed to connect */
122 	struct acl_list* acl;
123 	/** access control, which interfaces are allowed to connect */
124 	struct acl_list* acl_interface;
125 	/** TCP connection limit, limit connections from client IPs */
126 	struct tcl_list* tcl;
127 	/** local authority zones */
128 	struct local_zones* local_zones;
129 	/** last time of statistics printout */
130 	struct timeval time_last_stat;
131 	/** time when daemon started */
132 	struct timeval time_boot;
133 	/** views structure containing view tree */
134 	struct views* views;
135 #ifdef USE_DNSTAP
136 	/** the dnstap environment master value, copied and changed by threads*/
137 	struct dt_env* dtenv;
138 #endif
139 	struct shm_main_info* shm_info;
140 	/** response-ip set with associated actions and tags. */
141 	struct respip_set* respip_set;
142 	/** some response-ip tags or actions are configured if true */
143 	int use_response_ip;
144 	/** some RPZ policies are configured */
145 	int use_rpz;
146 #ifdef USE_DNSCRYPT
147 	/** the dnscrypt environment */
148 	struct dnsc_env* dnscenv;
149 #endif
150 	/** reuse existing cache on reload if other conditions allow it. */
151 	int reuse_cache;
152 	/** the EDNS cookie secrets from the cookie-secret-file */
153 	struct cookie_secrets* cookie_secrets;
154 };
155 
156 /**
157  * Initialize daemon structure.
158  * @return: The daemon structure, or NULL on error.
159  */
160 struct daemon* daemon_init(void);
161 
162 /**
163  * Open shared listening ports (if needed).
164  * The cfg member pointer must have been set for the daemon.
165  * @param daemon: the daemon.
166  * @return: false on error.
167  */
168 int daemon_open_shared_ports(struct daemon* daemon);
169 
170 /**
171  * Do daemon setup that needs privileges
172  * like opening privileged ports or opening device files.
173  * The cfg member pointer must have been set for the daemon.
174  * @param daemon: the daemon.
175  * @return: false on error.
176  */
177 int daemon_privileged(struct daemon* daemon);
178 
179 /**
180  * Fork workers and start service.
181  * When the routine exits, it is no longer forked.
182  * @param daemon: the daemon.
183  */
184 void daemon_fork(struct daemon* daemon);
185 
186 /**
187  * Close off the worker thread information.
188  * Bring the daemon back into state ready for daemon_fork again.
189  * @param daemon: the daemon.
190  */
191 void daemon_cleanup(struct daemon* daemon);
192 
193 /**
194  * Delete workers, close listening ports.
195  * @param daemon: the daemon.
196  */
197 void daemon_delete(struct daemon* daemon);
198 
199 /**
200  * Apply config settings.
201  * @param daemon: the daemon.
202  * @param cfg: new config settings.
203  */
204 void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg);
205 
206 #endif /* DAEMON_H */
207