1238106Sdes/* 2238106Sdes * daemon/daemon.h - collection of workers that handles requests. 3238106Sdes * 4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved. 5238106Sdes * 6238106Sdes * This software is open source. 7238106Sdes * 8238106Sdes * Redistribution and use in source and binary forms, with or without 9238106Sdes * modification, are permitted provided that the following conditions 10238106Sdes * are met: 11238106Sdes * 12238106Sdes * Redistributions of source code must retain the above copyright notice, 13238106Sdes * this list of conditions and the following disclaimer. 14238106Sdes * 15238106Sdes * Redistributions in binary form must reproduce the above copyright notice, 16238106Sdes * this list of conditions and the following disclaimer in the documentation 17238106Sdes * and/or other materials provided with the distribution. 18238106Sdes * 19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may 20238106Sdes * be used to endorse or promote products derived from this software without 21238106Sdes * specific prior written permission. 22238106Sdes * 23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34238106Sdes */ 35238106Sdes 36238106Sdes/** 37238106Sdes * \file 38238106Sdes * 39238106Sdes * The daemon consists of global settings and a number of workers. 40238106Sdes */ 41238106Sdes 42238106Sdes#ifndef DAEMON_H 43238106Sdes#define DAEMON_H 44238106Sdes 45238106Sdes#include "util/locks.h" 46238106Sdes#include "util/alloc.h" 47238106Sdes#include "services/modstack.h" 48238106Sdes#ifdef UB_ON_WINDOWS 49238106Sdes# include "util/winsock_event.h" 50238106Sdes#endif 51238106Sdesstruct config_file; 52238106Sdesstruct worker; 53238106Sdesstruct listen_port; 54238106Sdesstruct slabhash; 55238106Sdesstruct module_env; 56238106Sdesstruct rrset_cache; 57238106Sdesstruct acl_list; 58238106Sdesstruct local_zones; 59238106Sdesstruct ub_randstate; 60238106Sdesstruct daemon_remote; 61238106Sdes 62285206Sdes#include "dnstap/dnstap_config.h" 63285206Sdes#ifdef USE_DNSTAP 64285206Sdesstruct dt_env; 65285206Sdes#endif 66285206Sdes 67238106Sdes/** 68238106Sdes * Structure holding worker list. 69238106Sdes * Holds globally visible information. 70238106Sdes */ 71238106Sdesstruct daemon { 72238106Sdes /** The config settings */ 73238106Sdes struct config_file* cfg; 74238106Sdes /** the chroot dir in use, NULL if none */ 75238106Sdes char* chroot; 76238106Sdes /** pidfile that is used */ 77238106Sdes char* pidfile; 78238106Sdes /** port number that has ports opened. */ 79238106Sdes int listening_port; 80269257Sdes /** array of listening ports, opened. Listening ports per worker, 81269257Sdes * or just one element[0] shared by the worker threads. */ 82269257Sdes struct listen_port** ports; 83269257Sdes /** size of ports array */ 84269257Sdes size_t num_ports; 85285206Sdes /** reuseport is enabled if true */ 86285206Sdes int reuseport; 87238106Sdes /** port number for remote that has ports opened. */ 88238106Sdes int rc_port; 89238106Sdes /** listening ports for remote control */ 90238106Sdes struct listen_port* rc_ports; 91238106Sdes /** remote control connections management (for first worker) */ 92238106Sdes struct daemon_remote* rc; 93238106Sdes /** ssl context for listening to dnstcp over ssl, and connecting ssl */ 94238106Sdes void* listen_sslctx, *connect_sslctx; 95238106Sdes /** num threads allocated */ 96238106Sdes int num; 97238106Sdes /** the worker entries */ 98238106Sdes struct worker** workers; 99238106Sdes /** do we need to exit unbound (or is it only a reload?) */ 100238106Sdes int need_to_exit; 101238106Sdes /** master random table ; used for port div between threads on reload*/ 102238106Sdes struct ub_randstate* rand; 103238106Sdes /** master allocation cache */ 104238106Sdes struct alloc_cache superalloc; 105238106Sdes /** the module environment master value, copied and changed by threads*/ 106238106Sdes struct module_env* env; 107238106Sdes /** stack of module callbacks */ 108238106Sdes struct module_stack mods; 109238106Sdes /** access control, which client IPs are allowed to connect */ 110238106Sdes struct acl_list* acl; 111238106Sdes /** local authority zones */ 112238106Sdes struct local_zones* local_zones; 113238106Sdes /** last time of statistics printout */ 114238106Sdes struct timeval time_last_stat; 115238106Sdes /** time when daemon started */ 116238106Sdes struct timeval time_boot; 117285206Sdes#ifdef USE_DNSTAP 118285206Sdes /** the dnstap environment master value, copied and changed by threads*/ 119285206Sdes struct dt_env* dtenv; 120285206Sdes#endif 121238106Sdes}; 122238106Sdes 123238106Sdes/** 124238106Sdes * Initialize daemon structure. 125238106Sdes * @return: The daemon structure, or NULL on error. 126238106Sdes */ 127238106Sdesstruct daemon* daemon_init(void); 128238106Sdes 129238106Sdes/** 130238106Sdes * Open shared listening ports (if needed). 131238106Sdes * The cfg member pointer must have been set for the daemon. 132238106Sdes * @param daemon: the daemon. 133238106Sdes * @return: false on error. 134238106Sdes */ 135238106Sdesint daemon_open_shared_ports(struct daemon* daemon); 136238106Sdes 137238106Sdes/** 138238106Sdes * Fork workers and start service. 139238106Sdes * When the routine exits, it is no longer forked. 140238106Sdes * @param daemon: the daemon. 141238106Sdes */ 142238106Sdesvoid daemon_fork(struct daemon* daemon); 143238106Sdes 144238106Sdes/** 145238106Sdes * Close off the worker thread information. 146238106Sdes * Bring the daemon back into state ready for daemon_fork again. 147238106Sdes * @param daemon: the daemon. 148238106Sdes */ 149238106Sdesvoid daemon_cleanup(struct daemon* daemon); 150238106Sdes 151238106Sdes/** 152238106Sdes * Delete workers, close listening ports. 153238106Sdes * @param daemon: the daemon. 154238106Sdes */ 155238106Sdesvoid daemon_delete(struct daemon* daemon); 156238106Sdes 157238106Sdes/** 158238106Sdes * Apply config settings. 159238106Sdes * @param daemon: the daemon. 160238106Sdes * @param cfg: new config settings. 161238106Sdes */ 162238106Sdesvoid daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg); 163238106Sdes 164238106Sdes#endif /* DAEMON_H */ 165