lib/fault: During smb_panic() print process comment and setprocname() title
authorAndrew Bartlett <abartlet@samba.org>
Thu, 20 Jul 2023 02:10:43 +0000 (14:10 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 21 Jul 2023 01:25:37 +0000 (01:25 +0000)
The purpose of this is to make it clear which part of the AD DC (in particular)
has faulted without having to deduce it from the stacktrace.

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
lib/cmdline/cmdline.c
lib/util/fault.c
lib/util/util_process.c
lib/util/util_process.h
lib/util/wscript_build

index 106be10aa0f6da386b7112ece00d5958fa0db14d..de34a798aaf8b2dac8263ef17a7126f714edb3cd 100644 (file)
@@ -21,6 +21,7 @@
 #include "auth/gensec/gensec.h"
 #include "libcli/smb/smb_util.h"
 #include "cmdline_private.h"
+#include "lib/util/util_process.h"
 
 #include <samba/version.h>
 
@@ -296,6 +297,7 @@ poptContext samba_popt_get_context(const char * name,
                return NULL;
        }
 #endif
+       process_save_binary_name(name);
        return poptGetContext(name, argc, argv, options, flags);
 }
 
index 3b1d10dce89285c9d17c5768ebd30b1514723551..10c3720144ad6fed19cd8e16f3f04946dbb9c2bd 100644 (file)
@@ -36,6 +36,7 @@
 #include "debug.h"
 #include "lib/util/signal.h" /* Avoid /usr/include/signal.h */
 #include "fault.h"
+#include "util_process.h"
 
 static struct {
        bool disabled;
@@ -170,9 +171,16 @@ static void smb_panic_default(const char *why)
 
 _PUBLIC_ void smb_panic_log(const char *why)
 {
+       const char *binary_name = process_get_saved_binary_name();
+       const char *short_title = process_get_short_title();
+       const char *long_title = process_get_long_title();
+
        DEBUGSEP(0);
-       DEBUG(0,("INTERNAL ERROR: %s in pid %lld (%s)\n",
+       DEBUG(0,("INTERNAL ERROR: %s in %s (%s) (%s) pid %lld (%s)\n",
                 why,
+                binary_name,
+                short_title,
+                long_title,
                 (unsigned long long)getpid(),
                 SAMBA_VERSION_STRING));
        DEBUG(0,("If you are running a recent Samba version, and "
@@ -189,6 +197,9 @@ _PUBLIC_ void smb_panic_log(const char *why)
 
 /**
    Something really nasty happened - panic !
+
+   This function is in this file to allow sharing the last set process
+   title into the logs before the backtrace
 **/
 _PUBLIC_ void smb_panic(const char *why)
 {
index 4b13c591309a8ab0bcf98f1a516b3eab988e3979..eccbffda1c5e0585938fd6461ed70ce3d69f52cf 100644 (file)
 #include <sys/prctl.h>
 #endif
 
+/*
+ * These variables are static so that we can print them in access them
+ * with process_get_short_title() and process_get_long_title().  The
+ * purpose of this is to allow smb_panic_log() to print them.
+ */
+static char short_comment[16] = {0,};
+static char long_comment[256] = {0,};
+static char binary_name[256];
+
 void process_set_title(const char *short_format, const char *long_format, ...)
 {
 #if defined(HAVE_PRCTL) && defined(PR_SET_NAME)
        if (short_format != NULL) {
-               char short_comment[16] = {0,};
                va_list ap;
 
                va_start(ap, long_format);
@@ -42,7 +50,6 @@ void process_set_title(const char *short_format, const char *long_format, ...)
 #endif
 
        if (long_format != NULL) {
-               char long_comment[256] = {0,};
                va_list ap;
 
                va_start(ap, long_format);
@@ -53,6 +60,33 @@ void process_set_title(const char *short_format, const char *long_format, ...)
        }
 }
 
+const char *process_get_short_title(void)
+{
+       return short_comment;
+}
+
+const char *process_get_long_title(void)
+{
+       return long_comment;
+}
+
+/*
+ * This is just for debugging in a panic, so we don't want to do
+ * anything more than return a fixed pointer, so we save a copy to a
+ * static variable.
+ */
+void process_save_binary_name(const char *progname)
+{
+       strlcpy(binary_name, progname, sizeof(binary_name));
+}
+
+/* Samba binaries will set this during popt handling */
+const char *process_get_saved_binary_name(void)
+{
+       return binary_name;
+}
+
+
 int prctl_set_comment(const char *comment_format, ...)
 {
        char comment[16];
index ccb2a7522326b67e1b8be3b47ef228ff4db6b306..4da135bc6664dac43b03bfab907e540032052aa0 100644 (file)
@@ -47,4 +47,38 @@ int prctl_set_comment(const char *comment_format, ...) PRINTF_ATTRIBUTE(1,2);
 void process_set_title(const char *short_format, const char *long_format, ...)
        PRINTF_ATTRIBUTE(1,3) PRINTF_ATTRIBUTE(2,3);
 
+/**
+ * @brief Get the process comment name set from process_set_title()
+ *
+ * @return              process comment name
+ */
+const char *process_get_short_title(void);
+
+/**
+ * @brief Get the process longname set from process_set_title()
+ *
+ * @return              process longname
+ */
+const char *process_get_long_title(void);
+
+/*
+ * @brief Save the binary name for later printing in smb_panic()
+ *
+ * @param[in]  progname        The binary name at process startup
+ *
+ * This is just for debugging in a panic, so we don't want to do
+ * anything more than return a fixed pointer, so we save a copy to a
+ * static variable.
+ */
+void process_save_binary_name(const char *progname);
+
+/**
+ * @brief Get the binary name set at startup process_save_binary_name()
+ *
+ * @return              binary name set at startup
+ */
+/* Samba binaries will set this during popt handling */
+const char *process_get_saved_binary_name(void);
+
+
 #endif
index 8eac1013394565ae1ba1450aebb5e29110127f1d..b4fcfeaba07c576fcc2eee7a0f0762ab594fae0f 100644 (file)
@@ -79,6 +79,7 @@ bld.SAMBA_SUBSYSTEM('smb-panic',
                     source='''
                     fault.c
                     signal.c
+                    util_process.c
                     ''',
                     deps='''
                     replace
@@ -97,7 +98,6 @@ bld.SAMBA_SUBSYSTEM('samba-util-core',
                     util.c
                     idtree.c
                     substitute.c
-                    util_process.c
                     util_strlist.c
                     strv_util.c
                     bitmap.c