[ovs-dev] [PATCH 1/2] util: Allow set_subprogram_name() to take a printf() format string.

Ben Pfaff blp at nicira.com
Thu Sep 5 17:38:39 UTC 2013


This will be convenient in an upcoming commit.

I had to add -Wno-format-zero-length to suppress a GCC warning about a
zero-length format string in this monitor_daemon() call:
    set_subprogram_name("");
I don't know what that warning is good for anyway, and I guess the Clang
developers don't either because Clang didn't warn.

Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 configure.ac |    1 +
 lib/util.c   |   18 +++++++++++++-----
 lib/util.h   |    2 +-
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/configure.ac b/configure.ac
index d6596f9..243171e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -95,6 +95,7 @@ OVS_ENABLE_OPTION([-Wno-sign-compare])
 OVS_ENABLE_OPTION([-Wpointer-arith])
 OVS_ENABLE_OPTION([-Wdeclaration-after-statement])
 OVS_ENABLE_OPTION([-Wformat-security])
+OVS_ENABLE_OPTION([-Wno-format-zero-length])
 OVS_ENABLE_OPTION([-Wswitch-enum])
 OVS_ENABLE_OPTION([-Wunused-parameter])
 OVS_ENABLE_OPTION([-Wstrict-aliasing])
diff --git a/lib/util.c b/lib/util.c
index 3cada4a..4f0d78b 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -399,13 +399,21 @@ get_subprogram_name(void)
     return name ? name : "";
 }
 
-/* Sets 'name' as the name of the currently running thread or process.  (This
- * appears in log messages and may also be visible in system process listings
- * and debuggers.) */
+/* Sets the formatted value of 'format' as the name of the currently running
+ * thread or process.  (This appears in log messages and may also be visible in
+ * system process listings and debuggers.) */
 void
-set_subprogram_name(const char *name)
+set_subprogram_name(const char *format, ...)
 {
-    free(subprogram_name_set(xstrdup(name)));
+    va_list args;
+    char *name;
+
+    va_start(args, format);
+    name = xvasprintf(format, args);
+    va_end(args);
+
+    free(subprogram_name_set(name));
+
 #if HAVE_GLIBC_PTHREAD_SETNAME_NP
     pthread_setname_np(pthread_self(), name);
 #elif HAVE_NETBSD_PTHREAD_SETNAME_NP
diff --git a/lib/util.h b/lib/util.h
index 0db41be..2c8e598 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -207,7 +207,7 @@ void set_program_name__(const char *name, const char *version,
         set_program_name__(name, VERSION, __DATE__, __TIME__)
 
 const char *get_subprogram_name(void);
-void set_subprogram_name(const char *name);
+void set_subprogram_name(const char *format, ...) PRINTF_FORMAT(1, 2);
 
 const char *get_program_version(void);
 void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp);
-- 
1.7.10.4




More information about the dev mailing list