From 19c82c19c009eefe975ae95c8b709fc93f5f4c39 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Fri, 2 Dec 2022 15:17:20 +0100 Subject: lib/util: add process_set_title() Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison --- lib/util/util_process.c | 32 +++++++++++++++++++++++++++++--- lib/util/util_process.h | 13 +++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/util/util_process.c b/lib/util/util_process.c index bb5dc15fa98..4b13c591309 100644 --- a/lib/util/util_process.c +++ b/lib/util/util_process.c @@ -26,16 +26,42 @@ #include #endif -int prctl_set_comment(const char *comment_format, ...) +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); + vsnprintf(short_comment, sizeof(short_comment), short_format, ap); + va_end(ap); + + prctl(PR_SET_NAME, (unsigned long) short_comment, 0, 0, 0); + } +#endif + + if (long_format != NULL) { + char long_comment[256] = {0,}; + va_list ap; + + va_start(ap, long_format); + vsnprintf(long_comment, sizeof(long_comment), long_format, ap); + va_end(ap); + + setproctitle("%s", long_comment); + } +} + +int prctl_set_comment(const char *comment_format, ...) +{ char comment[16]; va_list ap; + va_start(ap, comment_format); vsnprintf(comment, sizeof(comment), comment_format, ap); va_end(ap); - return prctl(PR_SET_NAME, (unsigned long) comment, 0, 0, 0); -#endif + process_set_title("%s", "%s", comment); return 0; } diff --git a/lib/util/util_process.h b/lib/util/util_process.h index 5b337d32aec..ccb2a752232 100644 --- a/lib/util/util_process.h +++ b/lib/util/util_process.h @@ -34,4 +34,17 @@ */ int prctl_set_comment(const char *comment_format, ...) PRINTF_ATTRIBUTE(1,2); +/** + * @brief Set the process comment name and longname + * + * @param[in] short_format The comment to set which shouldn't be longer than 16 + * 16 characters (including \0). + * @param[in] long_format The format string and arguments to produce the long + * form of the process name. + * + * @return -1 on error, 0 on success. + */ +void process_set_title(const char *short_format, const char *long_format, ...) + PRINTF_ATTRIBUTE(1,3) PRINTF_ATTRIBUTE(2,3); + #endif -- cgit v1.2.1