From: W.C.A. Wijngaards Date: Tue, 28 Jan 2020 14:51:39 +0000 (+0100) Subject: dnstap io, without threads, logs from the main event loop. X-Git-Tag: 1.11.0rc1~120^2~72 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1e4165d25be668118aa8421719af06ce8877fcd4;p=thirdparty%2Funbound.git dnstap io, without threads, logs from the main event loop. --- diff --git a/daemon/worker.c b/daemon/worker.c index d14409570..759fd0457 100644 --- a/daemon/worker.c +++ b/daemon/worker.c @@ -1883,7 +1883,8 @@ worker_init(struct worker* worker, struct config_file *cfg, && worker->thread_num == 0 #endif ) { - if(!dt_io_thread_start(dtenv->dtio)) { + if(!dt_io_thread_start(dtenv->dtio, comm_base_internal( + worker->base))) { log_err("could not start dnstap io thread"); worker_delete(worker); return 0; @@ -1937,7 +1938,6 @@ worker_delete(struct worker* worker) wsvc_desetup_worker(worker); #endif /* UB_ON_WINDOWS */ } - comm_base_delete(worker->base); #ifdef USE_DNSTAP if(worker->daemon->cfg->dnstap #ifndef THREADS_DISABLED @@ -1948,6 +1948,7 @@ worker_delete(struct worker* worker) } dt_deinit(&worker->dtenv); #endif /* USE_DNSTAP */ + comm_base_delete(worker->base); ub_randfree(worker->rndstate); alloc_clear(&worker->alloc); regional_destroy(worker->env.scratch); diff --git a/dnstap/dtstream.c b/dnstap/dtstream.c index f21641049..9f7345b3a 100644 --- a/dnstap/dtstream.c +++ b/dnstap/dtstream.c @@ -398,6 +398,7 @@ static void dtio_reconnect_enable(struct dt_io_thread* dtio) { struct timeval tv; int msec; + if(dtio->want_to_exit) return; if(dtio->reconnect_is_added) return; /* already done */ @@ -892,6 +893,7 @@ static void dtio_cmd_cb(int fd, short ATTR_UNUSED(bits), void* arg) } } +#ifndef THREADS_DISABLED /** setup the event base for the dnstap io thread */ static void dtio_setup_base(struct dt_io_thread* dtio, time_t* secs, struct timeval* now) @@ -902,6 +904,7 @@ static void dtio_setup_base(struct dt_io_thread* dtio, time_t* secs, fatal_exit("dnstap io: could not create event_base"); } } +#endif /* THREADS_DISABLED */ /** setup the cmd event for dnstap io */ static void dtio_setup_cmd(struct dt_io_thread* dtio) @@ -1145,7 +1148,9 @@ static void dtio_desetup(struct dt_io_thread* dtio) dtio_reconnect_del(dtio); ub_event_free(dtio->reconnect_timer); dtio_cur_msg_free(dtio); +#ifndef THREADS_DISABLED ub_event_base_free(dtio->event_base); +#endif } /** setup a start control message */ @@ -1248,6 +1253,7 @@ static void dtio_open_output(struct dt_io_thread* dtio) #endif /* HAVE_SYS_UN_H */ } +#ifndef THREADS_DISABLED /** the IO thread function for the DNSTAP IO */ static void* dnstap_io(void* arg) { @@ -1274,8 +1280,9 @@ static void* dnstap_io(void* arg) dtio_desetup(dtio); return NULL; } +#endif /* THREADS_DISABLED */ -int dt_io_thread_start(struct dt_io_thread* dtio) +int dt_io_thread_start(struct dt_io_thread* dtio, void* event_base_nothr) { /* set up the thread, can fail */ #ifndef USE_WINSOCK @@ -1293,17 +1300,28 @@ int dt_io_thread_start(struct dt_io_thread* dtio) /* start the thread */ dtio->started = 1; +#ifndef THREADS_DISABLED ub_thread_create(&dtio->tid, dnstap_io, dtio); +#else + dtio->event_base = event_base_nothr; + dtio_setup_cmd(dtio); + dtio_setup_reconnect(dtio); + dtio_open_output(dtio); + dtio_add_output_event_write(dtio); +#endif return 1; } void dt_io_thread_stop(struct dt_io_thread* dtio) { +#ifndef THREADS_DISABLED uint8_t cmd = DTIO_COMMAND_STOP; +#endif if(!dtio) return; if(!dtio->started) return; verbose(VERB_ALGO, "dnstap io: send stop cmd"); +#ifndef THREADS_DISABLED while(1) { ssize_t r = write(dtio->commandpipe[1], &cmd, sizeof(cmd)); if(r == -1) { @@ -1324,6 +1342,7 @@ void dt_io_thread_stop(struct dt_io_thread* dtio) break; } dtio->started = 0; +#endif /* THREADS_DISABLED */ #ifndef USE_WINSOCK close(dtio->commandpipe[1]); @@ -1331,5 +1350,10 @@ void dt_io_thread_stop(struct dt_io_thread* dtio) _close(dtio->commandpipe[1]); #endif dtio->commandpipe[1] = -1; +#ifndef THREADS_DISABLED ub_thread_join(dtio->tid); +#else + dtio->want_to_exit = 1; + dtio_desetup(dtio); +#endif } diff --git a/dnstap/dtstream.h b/dnstap/dtstream.h index 473479d8f..c2b898b86 100644 --- a/dnstap/dtstream.h +++ b/dnstap/dtstream.h @@ -345,9 +345,12 @@ void dt_io_thread_unregister_queue(struct dt_io_thread* dtio, /** * Start the io thread * @param dtio: the io thread. + * @param event_base_nothr: the event base to attach the events to, in case + * we are running without threads. With threads, this is ignored + * and a thread is started to process the dnstap log messages. * @return false on failure. */ -int dt_io_thread_start(struct dt_io_thread* dtio); +int dt_io_thread_start(struct dt_io_thread* dtio, void* event_base_nothr); /** * Stop the io thread