]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
- Support pthread_setname_np, and variants, to set the name on spawned 1400/head
authorYorgos Thessalonikefs <yorgos@nlnetlabs.nl>
Fri, 6 Feb 2026 13:15:32 +0000 (14:15 +0100)
committerYorgos Thessalonikefs <yorgos@nlnetlabs.nl>
Fri, 6 Feb 2026 13:17:04 +0000 (14:17 +0100)
  threads for easier debugging/monitoring.

configure.ac
daemon/daemon.c
daemon/remote.c
dnstap/dtstream.c
util/locks.h

index 41eedc2fdef0a356e3fd1c30598903c1231913b1..ddf31b787ac239a68cfbd4269b1f9c144ee1c4ce 100644 (file)
@@ -731,6 +731,73 @@ int main(void) {return 0;}
                ])
 fi
 
+if test x_$ub_have_pthreads != x_no; then
+       # Long checks to support pthread_setname_np().
+       # Some OSes have the extra non-portable functions in a specific
+       # header file.
+       AC_CHECK_HEADERS([pthread_np.h],,, [AC_INCLUDES_DEFAULT])
+       # MacOS only has 1 argument, the name.
+       AC_MSG_CHECKING([whether pthread_setname_np has only 1 argument])
+       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT
+#include <pthread.h>
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif
+       ],[
+           (void)pthread_setname_np("");
+       ])],[
+           AC_MSG_RESULT(yes)
+           AC_DEFINE(HAVE_PTHREAD_SETNAME_NP1, 1, [Define if pthread_setname_np has only 1 argument.])
+       ],[
+           AC_MSG_RESULT(no)
+       ])
+       # NetBSD has 3 arguments to allow for formatting of the name.
+       AC_MSG_CHECKING([whether pthread_setname_np has 3 arguments])
+       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT
+#include <pthread.h>
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif
+       ],[
+           (void)pthread_setname_np(0, "", NULL);
+       ])],[
+           AC_MSG_RESULT(yes)
+           AC_DEFINE(HAVE_PTHREAD_SETNAME_NP3, 1, [Define if pthread_setname_np has 3 arguments.])
+       ],[
+           AC_MSG_RESULT(no)
+       ])
+       # Most OSes have the common 2 arguments, thread and name.
+       AC_MSG_CHECKING([whether pthread_setname_np has the common 2 arguments])
+       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT
+#include <pthread.h>
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif
+       ],[
+           (void)pthread_setname_np(0, "");
+       ])],[
+           AC_MSG_RESULT(yes)
+           AC_DEFINE(HAVE_PTHREAD_SETNAME_NP, 1, [Define if pthread_setname_np has the common 2 arguments.])
+       ],[
+           AC_MSG_RESULT(no)
+       ])
+       # FreeBSD/OpenBSD use a slightly different function name.
+       AC_MSG_CHECKING([whether pthread_setname_np exists as pthread_set_name_np instead])
+       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT
+#include <pthread.h>
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif
+       ],[
+           (void)pthread_set_name_np(0, "");
+       ])],[
+           AC_MSG_RESULT(yes)
+           AC_DEFINE(HAVE_PTHREAD_SET_NAME_NP, 1, [Define if pthread_setname_np exists as pthread_set_name_np instead.])
+       ],[
+           AC_MSG_RESULT(no)
+       ])
+fi
+
 # check solaris thread library
 AC_ARG_WITH(solaris-threads, AS_HELP_STRING([--with-solaris-threads],[use solaris native thread library.]), [ ],[ withval="no" ])
 ub_have_sol_threads=no
index 72cd0dc82439f59e8bbf23c9d60348ae98f280eb..bc358df0c0206e7ab35d5c72eff6b68341dd9597 100644 (file)
@@ -662,7 +662,14 @@ thread_start(void* arg)
 {
        struct worker* worker = (struct worker*)arg;
        int port_num = 0;
+       log_assert(worker->thr_id);
        set_log_thread_id(worker, worker->daemon->cfg);
+       {
+               char name[16]; /* seems to be the safest size between
+                                 different OSes */
+               snprintf(name, sizeof(name), "unbound/%u", worker->thread_num);
+               ub_thread_setname(worker->thr_id, name);
+       }
        ub_thread_blocksigs();
 #ifdef THREADS_DISABLED
        /* close pipe ends used by main */
index 00e7dd21d6bb9bbb9fff3d878edd5b72a1bfb855..69ffff4baeacb31f82793c053ad34b3cdeb43196 100644 (file)
@@ -6635,6 +6635,8 @@ 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;
+       const char name[16] = "unbound/freload"; /* seems to be the safest size
+                                                   between different OSes */
 
 #if defined(HAVE_GETTID) && !defined(THREADS_DISABLED)
        fast_reload_thread->thread_tid = gettid();
@@ -6644,6 +6646,8 @@ static void* fast_reload_thread_main(void* arg)
 #endif
                log_thread_set(&fast_reload_thread->threadnum);
 
+       ub_thread_setname(fast_reload_thread->tid, name);
+
        verbose(VERB_ALGO, "start fast reload thread");
        if(fast_reload_thread->fr_verb >= 1) {
                fr_init_time(&time_start, &time_read, &time_construct,
index 30db6f219402263afd7cd0875caca483f5a1d8b0..09bdfd66279407ac9a0212f83f01092160e10285 100644 (file)
@@ -2133,6 +2133,8 @@ static void* dnstap_io(void* arg)
        struct dt_io_thread* dtio = (struct dt_io_thread*)arg;
        time_t secs = 0;
        struct timeval now;
+       const char name[16] = "unbound/dnstap"; /* seems to be the safest size
+                                                  between different OSes */
 
 #if defined(HAVE_GETTID) && !defined(THREADS_DISABLED)
        dtio->thread_tid = gettid();
@@ -2142,6 +2144,8 @@ static void* dnstap_io(void* arg)
 #endif
                log_thread_set(&dtio->threadnum);
 
+       ub_thread_setname(dtio->tid, name);
+
        /* setup */
        verbose(VERB_ALGO, "start dnstap io thread");
        dtio_setup_base(dtio, &secs, &now);
index eb698cb759aa05c9e3cc1c0e2af448a3e7826e5c..9b1b10b3756f7e6bd9f5b671fa2a1998be2a33da 100644 (file)
@@ -178,6 +178,30 @@ typedef pthread_key_t ub_thread_key_type;
 #define ub_thread_key_set(key, v) LOCKRET(pthread_setspecific(key, v))
 #define ub_thread_key_get(key) pthread_getspecific(key)
 
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif
+#if defined(HAVE_PTHREAD_SET_NAME_NP)
+       #define ub_thread_setname(thread, name) do {    \
+               (void)pthread_set_name_np(thread, name);\
+               } while(0)
+#elif defined(HAVE_PTHREAD_SETNAME_NP1)
+       #define ub_thread_setname(thread, name) do {    \
+               (void)pthread_setname_np(name);         \
+               } while(0)
+#elif defined(HAVE_PTHREAD_SETNAME_NP3)
+       #define ub_thread_setname(thread, name) do {            \
+               (void)pthread_setname_np(thread, name, NULL);   \
+               } while(0)
+#elif defined(HAVE_PTHREAD_SETNAME_NP)
+       #define ub_thread_setname(thread, name) do {    \
+               (void)pthread_setname_np(thread, name); \
+               } while(0)
+#else
+       #define ub_thread_setname(thread, name) /* nop */
+#endif /* HAVE_PTHREAD_SET_NAME_NP */
+
+
 #else /* we do not HAVE_PTHREAD */
 #ifdef HAVE_SOLARIS_THREADS
 
@@ -215,6 +239,7 @@ typedef thread_key_t ub_thread_key_type;
 #define ub_thread_key_create(key, f) LOCKRET(thr_keycreate(key, f))
 #define ub_thread_key_set(key, v) LOCKRET(thr_setspecific(key, v))
 void* ub_thread_key_get(ub_thread_key_type key);
+#define ub_thread_setname(thread, name) /* nop */
 
 
 #else /* we do not HAVE_SOLARIS_THREADS and no PTHREADS */
@@ -253,6 +278,7 @@ typedef DWORD ub_thread_key_type;
 void ub_thread_key_create(ub_thread_key_type* key, void* f);
 void ub_thread_key_set(ub_thread_key_type key, void* v);
 void* ub_thread_key_get(ub_thread_key_type key);
+#define ub_thread_setname(thread, name) /* nop */
 
 #else /* we do not HAVE_SOLARIS_THREADS, PTHREADS or WINDOWS_THREADS */
 
@@ -294,6 +320,7 @@ typedef void* ub_thread_key_type;
 #define ub_thread_key_create(key, f) (*(key)) = NULL
 #define ub_thread_key_set(key, v) (key) = (v)
 #define ub_thread_key_get(key) (key)
+#define ub_thread_setname(thread, name) /* nop */
 
 #endif /* HAVE_WINDOWS_THREADS */
 #endif /* HAVE_SOLARIS_THREADS */