2 * daemon/worker.h - worker that handles a pending list of requests.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file describes the worker structure that holds a list of
40 * pending requests and handles them.
43 #ifndef DAEMON_WORKER_H
44 #define DAEMON_WORKER_H
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 struct listen_dnsport;
55 struct outside_network;
64 /** worker commands */
65 enum worker_commands {
66 /** make the worker quit */
68 /** obtain statistics */
70 /** obtain statistics without statsclear */
71 worker_cmd_stats_noreset,
72 /** execute remote control command */
77 * Structure holding working information for unbound.
78 * Holds globally visible information.
81 /** the thread number (in daemon array). First in struct for debug. */
83 /** global shared daemon structure */
84 struct daemon* daemon;
87 /** pipe, for commands for this worker */
89 /** the event base this worker works with */
90 struct comm_base* base;
91 /** the frontside listening interface where request events come in */
92 struct listen_dnsport* front;
93 /** the backside outside network interface to the auth servers */
94 struct outside_network* back;
95 /** ports to be used by this worker. */
97 /** number of ports for this worker */
99 /** the signal handler */
100 struct comm_signal* comsig;
101 /** commpoint to listen to commands. */
102 struct comm_point* cmd_com;
103 /** timer for statistics */
104 struct comm_timer* stat_timer;
106 /** random() table for this worker. */
107 struct ub_randstate* rndstate;
108 /** do we need to restart or quit (on signal) */
110 /** allocation cache for this thread */
111 struct alloc_cache alloc;
112 /** per thread statistics */
113 struct server_stats stats;
114 /** thread scratch regional */
115 struct regional* scratchpad;
117 /** module environment passed to modules, changed for this thread */
118 struct module_env env;
122 * Create the worker structure. Bare bones version, zeroed struct,
123 * with backpointers only. Use worker_init on it later.
124 * @param daemon: the daemon that this worker thread is part of.
125 * @param id: the thread number from 0.. numthreads-1.
126 * @param ports: the ports it is allowed to use, array.
127 * @param n: the number of ports.
128 * @return: the new worker or NULL on alloc failure.
130 struct worker* worker_create(struct daemon* daemon, int id, int* ports, int n);
134 * Allocates event base, listens to ports
135 * @param worker: worker to initialize, created with worker_create.
136 * @param cfg: configuration settings.
137 * @param ports: list of shared query ports.
138 * @param do_sigs: if true, worker installs signal handlers.
139 * @return: false on error.
141 int worker_init(struct worker* worker, struct config_file *cfg,
142 struct listen_port* ports, int do_sigs);
147 void worker_work(struct worker* worker);
152 void worker_delete(struct worker* worker);
155 * Send a command to a worker. Uses blocking writes.
156 * @param worker: worker to send command to.
157 * @param cmd: command to send.
159 void worker_send_cmd(struct worker* worker, enum worker_commands cmd);
162 * Init worker stats - includes server_stats_init, outside network and mesh.
163 * @param worker: the worker to init
165 void worker_stats_clear(struct worker* worker);
167 #endif /* DAEMON_WORKER_H */