1 /* 2 * daemon/worker.h - worker that handles a pending list of 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 * This file describes the worker structure that holds a list of 40 * pending requests and handles them. 41 */ 42 43 #ifndef DAEMON_WORKER_H 44 #define DAEMON_WORKER_H 45 46 #include "libunbound/worker.h" 47 #include "util/netevent.h" 48 #include "util/locks.h" 49 #include "util/alloc.h" 50 #include "util/data/msgreply.h" 51 #include "util/data/msgparse.h" 52 #include "daemon/stats.h" 53 #include "util/module.h" 54 #include "dnstap/dnstap.h" 55 struct listen_dnsport; 56 struct outside_network; 57 struct config_file; 58 struct daemon; 59 struct listen_port; 60 struct ub_randstate; 61 struct regional; 62 struct tube; 63 struct daemon_remote; 64 struct query_info; 65 66 /** worker commands */ 67 enum worker_commands { 68 /** make the worker quit */ 69 worker_cmd_quit, 70 /** obtain statistics */ 71 worker_cmd_stats, 72 /** obtain statistics without statsclear */ 73 worker_cmd_stats_noreset, 74 /** execute remote control command */ 75 worker_cmd_remote, 76 /** for fast-reload, perform stop */ 77 worker_cmd_reload_stop, 78 /** for fast-reload, start again */ 79 worker_cmd_reload_start, 80 /** for fast-reload, poll to make sure worker has released data */ 81 worker_cmd_reload_poll 82 }; 83 84 /** 85 * Structure holding working information for unbound. 86 * Holds globally visible information. 87 */ 88 struct worker { 89 /** the thread number (in daemon array). First in struct for debug. */ 90 int thread_num; 91 /** global shared daemon structure */ 92 struct daemon* daemon; 93 /** thread id */ 94 ub_thread_type thr_id; 95 #ifdef HAVE_GETTID 96 /** thread tid, the LWP id. */ 97 pid_t thread_tid; 98 #endif 99 /** pipe, for commands for this worker */ 100 struct tube* cmd; 101 /** the event base this worker works with */ 102 struct comm_base* base; 103 /** the frontside listening interface where request events come in */ 104 struct listen_dnsport* front; 105 /** the backside outside network interface to the auth servers */ 106 struct outside_network* back; 107 /** the signal handler */ 108 struct comm_signal* comsig; 109 /** commpoint to listen to commands. */ 110 struct comm_point* cmd_com; 111 /** timer for statistics */ 112 struct comm_timer* stat_timer; 113 /** ratelimit for errors, time value */ 114 time_t err_limit_time; 115 /** ratelimit for errors, packet count */ 116 unsigned int err_limit_count; 117 118 /** random() table for this worker. */ 119 struct ub_randstate* rndstate; 120 /** do we need to restart or quit (on signal) */ 121 int need_to_exit; 122 /** allocation cache for this thread */ 123 struct alloc_cache *alloc; 124 /** per thread statistics */ 125 struct ub_server_stats stats; 126 /** thread scratch regional */ 127 struct regional* scratchpad; 128 129 /** module environment passed to modules, changed for this thread */ 130 struct module_env env; 131 132 #ifdef USE_DNSTAP 133 /** dnstap environment, changed for this thread */ 134 struct dt_env dtenv; 135 #endif 136 /** reuse existing cache on reload if other conditions allow it. */ 137 int reuse_cache; 138 }; 139 140 /** 141 * Create the worker structure. Bare bones version, zeroed struct, 142 * with backpointers only. Use worker_init on it later. 143 * @param daemon: the daemon that this worker thread is part of. 144 * @param id: the thread number from 0.. numthreads-1. 145 * @return: the new worker or NULL on alloc failure. 146 */ 147 struct worker* worker_create(struct daemon* daemon, int id); 148 149 /** 150 * Initialize worker. 151 * Allocates event base, listens to ports 152 * @param worker: worker to initialize, created with worker_create. 153 * @param cfg: configuration settings. 154 * @param ports: list of shared query ports. 155 * @param do_sigs: if true, worker installs signal handlers. 156 * @return: false on error. 157 */ 158 int worker_init(struct worker* worker, struct config_file *cfg, 159 struct listen_port* ports, int do_sigs); 160 161 /** 162 * Make worker work. 163 */ 164 void worker_work(struct worker* worker); 165 166 /** 167 * Delete worker. 168 */ 169 void worker_delete(struct worker* worker); 170 171 /** 172 * Send a command to a worker. Uses blocking writes. 173 * @param worker: worker to send command to. 174 * @param cmd: command to send. 175 */ 176 void worker_send_cmd(struct worker* worker, enum worker_commands cmd); 177 178 /** 179 * Init worker stats - includes server_stats_init, outside network and mesh. 180 * @param worker: the worker to init 181 */ 182 void worker_stats_clear(struct worker* worker); 183 184 #endif /* DAEMON_WORKER_H */ 185