]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
- On Linux systems log the system-wide unique thread ID instead of
authorYorgos Thessalonikefs <yorgos@nlnetlabs.nl>
Fri, 23 Jan 2026 16:08:55 +0000 (17:08 +0100)
committerYorgos Thessalonikefs <yorgos@nlnetlabs.nl>
Fri, 23 Jan 2026 16:08:55 +0000 (17:08 +0100)
  Unbound's internal thread counter.

daemon/daemon.c
daemon/remote.c
daemon/remote.h
daemon/worker.c
dnstap/dtstream.c
dnstap/dtstream.h
util/log.c

index f882bb9ad62ed1f4eb8ded38fa34115c89455c49..eb27fd3ab6c90119e24e1572686fa52cc8e1e4bb 100644 (file)
@@ -631,6 +631,25 @@ static void close_other_pipes(struct daemon* daemon, int thr)
 }
 #endif /* THREADS_DISABLED */
 
+/**
+ * Function to set the thread local log ID.
+ * Either the internal thread number, or the LWP ID on Linux based on
+ * configuration.
+ */
+static void
+set_log_thread_id(struct worker* worker, struct config_file* cfg)
+{
+       (void)cfg;
+       log_assert(worker);
+#if defined(HAVE_GETTID) && !defined(THREADS_DISABLED)
+       worker->thread_tid = gettid();
+       if(1/*cfg->log_thread_id*/)
+               log_thread_set(&worker->thread_tid);
+       else
+#endif
+               log_thread_set(&worker->thread_num);
+}
+
 /**
  * Function to start one thread. 
  * @param arg: user argument.
@@ -641,7 +660,7 @@ thread_start(void* arg)
 {
        struct worker* worker = (struct worker*)arg;
        int port_num = 0;
-       log_thread_set(&worker->thread_num);
+       set_log_thread_id(worker, worker->daemon->cfg);
        ub_thread_blocksigs();
 #ifdef THREADS_DISABLED
        /* close pipe ends used by main */
@@ -801,9 +820,13 @@ daemon_fork(struct daemon* daemon)
                fatal_exit("RPZ requires the respip module");
 
        /* first create all the worker structures, so we can pass
-        * them to the newly created threads. 
+        * them to the newly created threads.
         */
        daemon_create_workers(daemon);
+       /* Set it for the first (main) worker since it does not take part in
+        * the thread_start() procedure.
+        */
+       set_log_thread_id(daemon->workers[0], daemon->cfg);
 
 #if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
        /* in libev the first inited base gets signals */
index 0d55619c29faff60ff4f36a7abc0ee890082fb25..9732ace6df2a105b57076538ac944001d5f84e1f 100644 (file)
@@ -6627,7 +6627,14 @@ static void* fast_reload_thread_main(void* arg)
        struct fast_reload_thread* fast_reload_thread = (struct fast_reload_thread*)arg;
        struct timeval time_start, time_read, time_construct, time_reload,
                time_end;
-       log_thread_set(&fast_reload_thread->threadnum);
+
+#if defined(HAVE_GETTID) && !defined(THREADS_DISABLED)
+       fast_reload_thread->thread_tid = gettid();
+       if(fast_reload_thread->thread_tid_log)
+               log_thread_set(&fast_reload_thread->thread_tid);
+       else
+#endif
+               log_thread_set(&fast_reload_thread->threadnum);
 
        verbose(VERB_ALGO, "start fast reload thread");
        if(fast_reload_thread->fr_verb >= 1) {
@@ -7015,6 +7022,9 @@ fast_reload_thread_setup(struct worker* worker, int fr_verb, int fr_nopause,
        lock_basic_init(&fr->fr_output_lock);
        lock_protect(&fr->fr_output_lock, fr->fr_output,
                sizeof(*fr->fr_output));
+#ifdef HAVE_GETTID
+       fr->thread_tid_log = 1; /* worker->env->cfg->log_thread_id */
+#endif
        return 1;
 }
 
index 064d7b7fccced0a75fe221de6fbae5f0b8e0e479..77c00a5976f06403e64035dddf37ccc164a09ead 100644 (file)
@@ -206,6 +206,12 @@ struct fast_reload_thread {
        int commpair[2];
        /** thread id, of the io thread */
        ub_thread_type tid;
+#ifdef HAVE_GETTID
+       /** thread tid, the LWP id */
+       pid_t thread_tid;
+       /** if logging should include the LWP id */
+       int thread_tid_log;
+#endif
        /** if the io processing has started */
        int started;
        /** if the thread has to quit */
index 0f0af7457c807751f8c1375d38e9ad0e9df19eda..3c3af8d8ffab78b0248c68ddd3025b860791dcee 100644 (file)
@@ -2184,9 +2184,6 @@ worker_init(struct worker* worker, struct config_file *cfg,
        struct dt_env* dtenv = &worker->dtenv;
 #else
        void* dtenv = NULL;
-#endif
-#ifdef HAVE_GETTID
-       worker->thread_tid = gettid();
 #endif
        worker->need_to_exit = 0;
        worker->base = comm_base_create(do_sigs);
index 39d43403bc6232f852b0353df949b5502fe28ff9..748305ce79a7e6d1c60e0b5acabdbc3f95e23ef4 100644 (file)
@@ -448,6 +448,9 @@ int dt_io_thread_apply_cfg(struct dt_io_thread* dtio, struct config_file *cfg)
                dtio->tls_use_sni = cfg->tls_use_sni;
 #endif /* HAVE_SSL */
        }
+#ifdef HAVE_GETTID
+       dtio->thread_tid_log = 1 /*cfg->log_thread_id*/;
+#endif
        return 1;
 }
 
@@ -2130,7 +2133,14 @@ static void* dnstap_io(void* arg)
        struct dt_io_thread* dtio = (struct dt_io_thread*)arg;
        time_t secs = 0;
        struct timeval now;
-       log_thread_set(&dtio->threadnum);
+
+#if defined(HAVE_GETTID) && !defined(THREADS_DISABLED)
+       dtio->thread_tid = gettid();
+       if(dtio->thread_tid_log)
+               log_thread_set(&dtio->thread_tid);
+       else
+#endif
+               log_thread_set(&dtio->threadnum);
 
        /* setup */
        verbose(VERB_ALGO, "start dnstap io thread");
index f87d6dc8d386c6cbf94789c369795a9f0022d01d..4c0a6285fe3c6e1f2e755faf9d4d75388dd49164 100644 (file)
@@ -131,6 +131,12 @@ struct dt_io_thread {
        struct dt_io_list_item* io_list_iter;
        /** thread id, of the io thread */
        ub_thread_type tid;
+#ifdef HAVE_GETTID
+       /** thread tid, the LWP id */
+       pid_t thread_tid;
+       /** if logging should include the LWP id */
+       int thread_tid_log;
+#endif
        /** if the io processing has started */
        int started;
        /** ssl context for the io thread, for tls connections. type SSL_CTX* */
index b75cf065feb858f7665c71a7fc5be716d8d7cf18..f2beafc873392759eb69c90785ae11cb5056441c 100644 (file)
@@ -174,10 +174,10 @@ void log_thread_set(int* num)
 
 int log_thread_get(void)
 {
-       unsigned int* tid;
+       int* tid;
        if(!key_created) return 0;
-       tid = (unsigned int*)ub_thread_key_get(logkey);
-       return (int)(tid?*tid:0);
+       tid = ub_thread_key_get(logkey);
+       return (tid?*tid:0);
 }
 
 void log_ident_set(const char* id)
@@ -229,7 +229,7 @@ log_vmsg(int pri, const char* type,
        const char *format, va_list args)
 {
        char message[MAXSYSLOGMSGLEN];
-       unsigned int* tid = (unsigned int*)ub_thread_key_get(logkey);
+       int tid = log_thread_get();
        time_t now;
 #if defined(HAVE_STRFTIME) && defined(HAVE_LOCALTIME_R) 
        char tmbuf[32];
@@ -241,8 +241,8 @@ log_vmsg(int pri, const char* type,
        vsnprintf(message, sizeof(message), format, args);
 #ifdef HAVE_SYSLOG_H
        if(logging_to_syslog) {
-               syslog(pri, "[%d:%x] %s: %s", 
-                       (int)getpid(), tid?*tid:0, type, message);
+               syslog(pri, "[%d:%d] %s: %s",
+                       (int)getpid(), tid, type, message);
                return;
        }
 #elif defined(UB_ON_WINDOWS)
@@ -263,8 +263,8 @@ log_vmsg(int pri, const char* type,
                        tp=MSG_GENERIC_SUCCESS;
                        wt=EVENTLOG_SUCCESS;
                }
-               snprintf(m, sizeof(m), "[%s:%x] %s: %s", 
-                       ident, tid?*tid:0, type, message);
+               snprintf(m, sizeof(m), "[%s:%d] %s: %s",
+                       ident, tid, type, message);
                s = RegisterEventSource(NULL, SERVICE_NAME);
                if(!s) return;
                ReportEvent(s, wt, 0, tp, NULL, 1, 0, &str, NULL);
@@ -294,9 +294,9 @@ log_vmsg(int pri, const char* type,
                        tzbuf[3] = ':';
                        tzbuf[6] = 0;
                }
-               fprintf(logfile, "%s.%3.3d%s %s[%d:%x] %s: %s\n",
+               fprintf(logfile, "%s.%3.3d%s %s[%d:%d] %s: %s\n",
                        tmbuf, (int)tv.tv_usec/1000, tzbuf,
-                       ident, (int)getpid(), tid?*tid:0, type, message);
+                       ident, (int)getpid(), tid, type, message);
 #ifdef UB_ON_WINDOWS
                /* line buffering does not work on windows */
                fflush(logfile);
@@ -310,19 +310,19 @@ log_vmsg(int pri, const char* type,
        if(log_time_asc && strftime(tmbuf, sizeof(tmbuf), "%b %d %H:%M:%S",
                localtime_r(&now, &tm))%(sizeof(tmbuf)) != 0) {
                /* %sizeof buf!=0 because old strftime returned max on error */
-               fprintf(logfile, "%s %s[%d:%x] %s: %s\n", tmbuf, 
-                       ident, (int)getpid(), tid?*tid:0, type, message);
+               fprintf(logfile, "%s %s[%d:%d] %s: %s\n", tmbuf,
+                       ident, (int)getpid(), tid, type, message);
        } else
 #elif defined(UB_ON_WINDOWS)
        if(log_time_asc && GetTimeFormat(LOCALE_USER_DEFAULT, 0, NULL, NULL,
                tmbuf, sizeof(tmbuf)) && GetDateFormat(LOCALE_USER_DEFAULT, 0,
                NULL, NULL, dtbuf, sizeof(dtbuf))) {
-               fprintf(logfile, "%s %s %s[%d:%x] %s: %s\n", dtbuf, tmbuf, 
-                       ident, (int)getpid(), tid?*tid:0, type, message);
+               fprintf(logfile, "%s %s %s[%d:%d] %s: %s\n", dtbuf, tmbuf,
+                       ident, (int)getpid(), tid, type, message);
        } else
 #endif
-       fprintf(logfile, "[" ARG_LL "d] %s[%d:%x] %s: %s\n", (long long)now, 
-               ident, (int)getpid(), tid?*tid:0, type, message);
+       fprintf(logfile, "[" ARG_LL "d] %s[%d:%d] %s: %s\n", (long long)now,
+               ident, (int)getpid(), tid, type, message);
 #ifdef UB_ON_WINDOWS
        /* line buffering does not work on windows */
        fflush(logfile);