From 3109899299e28884261f54363e84b1090b574e39 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Thu, 20 Jul 2023 14:10:43 +1200 Subject: [PATCH] lib/fault: During smb_panic() print process comment and setprocname() title 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 Reviewed-by: Ralph Boehme --- lib/cmdline/cmdline.c | 2 ++ lib/util/fault.c | 13 ++++++++++++- lib/util/util_process.c | 38 ++++++++++++++++++++++++++++++++++++-- lib/util/util_process.h | 34 ++++++++++++++++++++++++++++++++++ lib/util/wscript_build | 2 +- 5 files changed, 85 insertions(+), 4 deletions(-) diff --git a/lib/cmdline/cmdline.c b/lib/cmdline/cmdline.c index 106be10aa0f..de34a798aaf 100644 --- a/lib/cmdline/cmdline.c +++ b/lib/cmdline/cmdline.c @@ -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 @@ -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); } diff --git a/lib/util/fault.c b/lib/util/fault.c index 3b1d10dce89..10c3720144a 100644 --- a/lib/util/fault.c +++ b/lib/util/fault.c @@ -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) { diff --git a/lib/util/util_process.c b/lib/util/util_process.c index 4b13c591309..eccbffda1c5 100644 --- a/lib/util/util_process.c +++ b/lib/util/util_process.c @@ -26,11 +26,19 @@ #include #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]; diff --git a/lib/util/util_process.h b/lib/util/util_process.h index ccb2a752232..4da135bc666 100644 --- a/lib/util/util_process.h +++ b/lib/util/util_process.h @@ -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 diff --git a/lib/util/wscript_build b/lib/util/wscript_build index 8eac1013394..b4fcfeaba07 100644 --- a/lib/util/wscript_build +++ b/lib/util/wscript_build @@ -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