From a38a37d66cecbf236b2362aae76bf33b2d9b8311 Mon Sep 17 00:00:00 2001 From: "W.C.A. Wijngaards" Date: Fri, 3 Nov 2023 11:44:22 +0100 Subject: [PATCH] - fast-reload, make a thread to service the unbound-control command. --- Makefile.in | 2 +- daemon/daemon.c | 2 ++ daemon/daemon.h | 3 +++ daemon/remote.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++- daemon/remote.h | 29 +++++++++++++++++++++ 5 files changed, 102 insertions(+), 2 deletions(-) diff --git a/Makefile.in b/Makefile.in index 22fb75c12..a1d98a971 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1296,7 +1296,7 @@ remote.lo remote.o: $(srcdir)/daemon/remote.c config.h $(srcdir)/daemon/remote.h $(srcdir)/validator/val_anchor.h $(srcdir)/iterator/iterator.h $(srcdir)/services/outbound_list.h \ $(srcdir)/iterator/iter_fwd.h $(srcdir)/iterator/iter_hints.h $(srcdir)/iterator/iter_delegpt.h \ $(srcdir)/services/outside_network.h $(srcdir)/sldns/str2wire.h $(srcdir)/sldns/parseutil.h \ - $(srcdir)/sldns/wire2str.h + $(srcdir)/sldns/wire2str.h $(srcdir)/util/locks.h stats.lo stats.o: $(srcdir)/daemon/stats.c config.h $(srcdir)/daemon/stats.h $(srcdir)/util/timehist.h \ $(srcdir)/libunbound/unbound.h $(srcdir)/daemon/worker.h $(srcdir)/libunbound/worker.h $(srcdir)/sldns/sbuffer.h \ $(srcdir)/util/data/packed_rrset.h $(srcdir)/util/storage/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/log.h \ diff --git a/daemon/daemon.c b/daemon/daemon.c index 193608d40..186ae42f2 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -861,6 +861,8 @@ daemon_cleanup(struct daemon* daemon) dnsc_delete(daemon->dnscenv); daemon->dnscenv = NULL; #endif + if(daemon->fast_reload_thread) + fast_reload_thread_stop(daemon->fast_reload_thread); daemon->cfg = NULL; } diff --git a/daemon/daemon.h b/daemon/daemon.h index 57665446d..69e9a7280 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -58,6 +58,7 @@ struct ub_randstate; struct daemon_remote; struct respip_set; struct shm_main_info; +struct fast_reload_thread; #include "dnstap/dnstap_config.h" #ifdef USE_DNSTAP @@ -146,6 +147,8 @@ struct daemon { #endif /** reuse existing cache on reload if other conditions allow it. */ int reuse_cache; + /** the fast reload thread, or NULL */ + struct fast_reload_thread* fast_reload_thread; }; /** diff --git a/daemon/remote.c b/daemon/remote.c index 1ce9f2119..bdc4dd4f8 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -658,7 +658,7 @@ do_fast_reload(RES* ssl, struct worker* worker) { if(!ssl_printf(ssl, "start fast_reload\n")) return; - (void)worker; + fast_reload_thread_start(ssl, worker); } /** do the verbosity command */ @@ -3369,3 +3369,69 @@ int remote_control_callback(struct comm_point* c, void* arg, int err, clean_point(rc, s); return 0; } + +#ifndef THREADS_DISABLED +/** fast reload thread. the thread main function */ +static void* fast_reload_thread_main(void* arg) +{ + struct fast_reload_thread* frio = (struct fast_reload_thread*)arg; + log_thread_set(&frio->threadnum); + + verbose(VERB_ALGO, "start fast reload thread"); + + verbose(VERB_ALGO, "stop fast reload thread"); + return NULL; +} +#endif /* !THREADS_DISABLED */ + +/** fast reload thread. setup the thread info */ +static int +fast_reload_thread_setup(struct worker* worker) +{ + int numworkers = worker->daemon->num; + worker->daemon->fast_reload_thread = (struct fast_reload_thread*) + calloc(1, sizeof(*worker->daemon->fast_reload_thread)); + if(!worker->daemon->fast_reload_thread) + return 0; + /* The thread id printed in logs, numworker+1 is the dnstap thread. + * This is numworkers+2. */ + worker->daemon->fast_reload_thread->threadnum = numworkers+2; + return 1; +} + +/** fast reload thread. desetup and delete the thread info. */ +static void +fast_reload_thread_desetup(struct fast_reload_thread* fast_reload_thread) +{ + if(!fast_reload_thread) + return; + free(fast_reload_thread); +} + +void +fast_reload_thread_start(RES* ssl, struct worker* worker) +{ + if(worker->daemon->fast_reload_thread) { + log_err("fast reload thread already running"); + return; + } + if(!fast_reload_thread_setup(worker)) { + if(!ssl_printf(ssl, "error could not setup thread\n")) + return; + return; + } + worker->daemon->fast_reload_thread->started = 1; +#ifndef THREADS_DISABLED + ub_thread_create(&worker->daemon->fast_reload_thread->tid, + fast_reload_thread_main, worker->daemon->fast_reload_thread); +#else +#endif +} + +void +fast_reload_thread_stop(struct fast_reload_thread* fast_reload_thread) +{ + if(!fast_reload_thread) + return; + fast_reload_thread_desetup(fast_reload_thread); +} diff --git a/daemon/remote.h b/daemon/remote.h index 4902803f5..cb80d4e84 100644 --- a/daemon/remote.h +++ b/daemon/remote.h @@ -48,6 +48,7 @@ #ifdef HAVE_OPENSSL_SSL_H #include #endif +#include "util/locks.h" struct config_file; struct listen_list; struct listen_port; @@ -118,6 +119,21 @@ struct remote_stream { }; typedef struct remote_stream RES; +/** + * Fast reload thread structure + */ +struct fast_reload_thread { + /** the thread number for the dtio thread, + * must be first to cast thread arg to int* in checklock code. */ + int threadnum; + /** event base, for event handling */ + void* event_base; + /** thread id, of the io thread */ + ub_thread_type tid; + /** if the io processing has started */ + int started; +}; + /** * Create new remote control state for the daemon. * @param cfg: config file with key file settings. @@ -203,4 +219,17 @@ int ssl_printf(RES* ssl, const char* format, ...) int ssl_read_line(RES* ssl, char* buf, size_t max); #endif /* HAVE_SSL */ +/** + * Start fast reload thread + * @param ssl: the RES connection to print to. + * @param worker: the remote servicing worker. + */ +void fast_reload_thread_start(RES* ssl, struct worker* worker); + +/** + * Stop fast reload thread + * @param fast_reload_thread: the thread struct. + */ +void fast_reload_thread_stop(struct fast_reload_thread* fast_reload_thread); + #endif /* DAEMON_REMOTE_H */ -- 2.47.2