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