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