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