]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
debug: support %m output format again
authorVictor Julien <vjulien@oisf.net>
Fri, 25 Feb 2022 14:40:41 +0000 (15:40 +0100)
committerVictor Julien <vjulien@oisf.net>
Tue, 12 Apr 2022 05:53:53 +0000 (07:53 +0200)
Use thread local storage to avoid the previous dead lock issues.

src/threads.c
src/threads.h
src/util-debug.c

index 1eb4dcb44c1d129a58bd8cd97b7de340bc780814..8d671d382885e791af7df567671863306a251c1d 100644 (file)
@@ -30,6 +30,7 @@
 #include "util-debug.h"
 #include "threads.h"
 
+thread_local char t_thread_name[THREAD_NAME_LEN + 1];
 #ifdef UNITTESTS /* UNIT TESTS */
 
 /**
index d0cb125377f0bef74cb8e9a4bf5d45e01921413f..c11c054bc8dba285746c9bc562941475a385f8f8 100644 (file)
 #ifndef __THREADS_H__
 #define __THREADS_H__
 
+#ifndef THREAD_NAME_LEN
+#define THREAD_NAME_LEN 16
+#endif
+
 #if defined(TLS_C11)
 #define thread_local _Thread_local
 #elif defined(TLS_GNU)
@@ -83,7 +87,6 @@ enum {
 #endif
 #if HAVE_SYS_PRCTL_H
 #include <sys/prctl.h>
-#define THREAD_NAME_LEN 16
 #endif
 
 enum {
@@ -262,28 +265,31 @@ enum {
 })
 #endif /* OS FREEBSD */
 
+extern thread_local char t_thread_name[THREAD_NAME_LEN + 1];
 /*
  * OS specific macro's for setting the thread name. "top" can display
  * this name.
  */
 #if defined OS_FREEBSD /* FreeBSD */
 /** \todo Add implementation for FreeBSD */
-#define SCSetThreadName(n) ({ \
-    char tname[16] = ""; \
-    if (strlen(n) > 16) \
-        SCLogDebug("Thread name is too long, truncating it..."); \
-    strlcpy(tname, n, 16); \
-    pthread_set_name_np(pthread_self(), tname); \
-})
+#define SCSetThreadName(n)                                                                         \
+    ({                                                                                             \
+        char tname[THREAD_NAME_LEN] = "";                                                          \
+        if (strlen(n) > THREAD_NAME_LEN)                                                           \
+            SCLogDebug("Thread name is too long, truncating it...");                               \
+        strlcpy(tname, n, THREAD_NAME_LEN);                                                        \
+        strlcpy(t_thread_name, n, sizeof(t_thread_name));                                          \
+        pthread_set_name_np(pthread_self(), tname);                                                \
+    })
 #elif defined __OpenBSD__ /* OpenBSD */
 /** \todo Add implementation for OpenBSD */
-#define SCSetThreadName(n) (0)
+#define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
 #elif defined OS_WIN32 /* Windows */
 /** \todo Add implementation for Windows */
-#define SCSetThreadName(n) (0)
+#define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
 #elif defined OS_DARWIN /* Mac OS X */
 /** \todo Add implementation for MacOS */
-#define SCSetThreadName(n) (0)
+#define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
 #elif defined PR_SET_NAME /* PR_SET_NAME */
 /**
  * \brief Set the threads name
@@ -294,11 +300,14 @@ enum {
         if (strlen(n) > THREAD_NAME_LEN)                                                           \
             SCLogDebug("Thread name is too long, truncating it...");                               \
         strlcpy(tname, n, THREAD_NAME_LEN);                                                        \
+        strlcpy(t_thread_name, n, sizeof(t_thread_name));                                          \
         if (prctl(PR_SET_NAME, tname, 0, 0, 0) < 0)                                                \
             SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno));            \
     })
 #else
-#define SCSetThreadName(n) (0)
+#define SCSetThreadName(n) ({ \
+    strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
+}
 #endif
 
 
index 20d0709612a5656ba4bfc6d96c6ec7cb5becfd33..b96d75feef9880222e7f4a4bf5ac91a7826f31a1 100644 (file)
@@ -367,17 +367,8 @@ static SCError SCLogMessageGetBuffer(
 
             case SC_LOG_FMT_TM:
                 temp_fmt[0] = '\0';
-/* disabled to prevent dead lock:
- * log or alloc (which calls log on error) can call TmThreadsGetCallingThread
- * which will lock tv_root_lock. This can happen while we already hold this
- * lock. */
-#if 0
-                ThreadVars *tv = TmThreadsGetCallingThread();
-                cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg),
-                              "%s%s", substr, ((tv != NULL)? tv->name: "UNKNOWN TM"));
-#endif
-                cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer),
-                              "%s%s", substr, "N/A");
+                cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s%s%s", substr,
+                        yellow, t_thread_name, reset);
                 if (cw < 0)
                     return SC_ERR_SPRINTF;
                 temp += cw;