util: Make subprogram_name thread-specific.
authorBen Pfaff <blp@nicira.com>
Fri, 12 Jul 2013 21:18:01 +0000 (14:18 -0700)
committerBen Pfaff <blp@nicira.com>
Fri, 12 Jul 2013 21:24:29 +0000 (14:24 -0700)
Signed-off-by: Ben Pfaff <blp@nicira.com>
lib/daemon.c
lib/util.c
lib/util.h
lib/vlog.c
lib/worker.c

index 3c1e5c3..2c8cf32 100644 (file)
@@ -400,7 +400,7 @@ monitor_daemon(pid_t daemon_pid)
     char *status_msg;
     int crashes;
 
-    subprogram_name = "monitor";
+    set_subprogram_name("monitor");
     status_msg = xstrdup("healthy");
     last_restart = TIME_MIN;
     crashes = 0;
@@ -470,7 +470,7 @@ monitor_daemon(pid_t daemon_pid)
 
     /* Running in new daemon process. */
     proctitle_restore();
-    subprogram_name = "";
+    set_subprogram_name("");
 }
 
 /* Close standard file descriptors (except any that the client has requested we
index 0ba1ed5..c69d7d1 100644 (file)
@@ -38,9 +38,9 @@ COVERAGE_DEFINE(util_xalloc);
 /* argv[0] without directory names. */
 const char *program_name;
 
-/* Ordinarily "" but set to "monitor" for a monitor process or "worker" for a
- * worker process. */
-const char *subprogram_name = "";
+/* Name for the currently running thread or process, for log messages, process
+ * listings, and debuggers. */
+DEFINE_PER_THREAD_MALLOCED_DATA(char *, subprogram_name);
 
 /* --version option output. */
 static char *program_version;
@@ -281,6 +281,7 @@ ovs_error(int err_no, const char *format, ...)
 void
 ovs_error_valist(int err_no, const char *format, va_list args)
 {
+    const char *subprogram_name = get_subprogram_name();
     int save_errno = errno;
 
     if (subprogram_name[0]) {
@@ -385,6 +386,22 @@ set_program_name__(const char *argv0, const char *version, const char *date,
     }
 }
 
+/* Returns the name of the currently running thread or process. */
+const char *
+get_subprogram_name(void)
+{
+    const char *name = subprogram_name_get();
+    return name ? name : "";
+}
+
+/* Sets 'name' as the name of the currently running thread or process.  (This
+ * appears in log messages.) */
+void
+set_subprogram_name(const char *name)
+{
+    free(subprogram_name_set(xstrdup(name)));
+}
+
 /* Returns a pointer to a string describing the program version.  The
  * caller must not modify or free the returned string.
  */
index c71f027..ae8bfd7 100644 (file)
@@ -86,7 +86,6 @@ void ovs_assert_failure(const char *, const char *, const char *) NO_RETURN;
      (TYPE) (POINTER))
 
 extern const char *program_name;
-extern const char *subprogram_name;
 
 /* Returns the number of elements in ARRAY. */
 #define ARRAY_SIZE(ARRAY) (sizeof ARRAY / sizeof *ARRAY)
@@ -184,6 +183,9 @@ void set_program_name__(const char *name, const char *version,
 #define set_program_name(name) \
         set_program_name__(name, VERSION, __DATE__, __TIME__)
 
+const char *get_subprogram_name(void);
+void set_subprogram_name(const char *name);
+
 const char *get_program_version(void);
 void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp);
 
index a880e07..bbc7c55 100644 (file)
@@ -665,6 +665,7 @@ format_log_message(const struct vlog_module *module, enum vlog_level level,
 
     ds_clear(s);
     for (p = facilities[facility].pattern; *p != '\0'; ) {
+        const char *subprogram_name;
         enum { LEFT, RIGHT } justify = RIGHT;
         int pad = '0';
         size_t length, field, used;
@@ -732,9 +733,11 @@ format_log_message(const struct vlog_module *module, enum vlog_level level,
             ds_put_format(s, "%lld", time_msec() - time_boot_msec());
             break;
         case 't':
+            subprogram_name = get_subprogram_name();
             ds_put_cstr(s, subprogram_name[0] ? subprogram_name : "main");
             break;
         case 'T':
+            subprogram_name = get_subprogram_name();
             if (subprogram_name[0]) {
                 ds_put_format(s, "(%s)", subprogram_name);
             }
index 6904fdd..1a90257 100644 (file)
@@ -362,7 +362,7 @@ worker_main(int fd)
 
     server_sock = fd;
 
-    subprogram_name = "worker";
+    set_subprogram_name("worker");
     proctitle_set("worker process for pid %lu", (unsigned long int) getppid());
     VLOG_INFO("worker process started");