X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Futil.c;h=76c33cd7cee0df99f87123ddd4194f400b40d97b;hb=bd3ad2c3508aaf7283f3ee041ecd3e5108471b21;hp=0ba1ed559b6c0d2195400a4291871dcc9b4d722a;hpb=85606e05b691be7c2f2d4bcf0e91170b71ec8fbb;p=sliver-openvswitch.git diff --git a/lib/util.c b/lib/util.c index 0ba1ed559..76c33cd7c 100644 --- a/lib/util.c +++ b/lib/util.c @@ -38,15 +38,17 @@ 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; /* Buffer used by ovs_strerror(). */ -DEFINE_PER_THREAD_DATA(struct { char s[128]; }, strerror_buffer, { "" }); +DEFINE_STATIC_PER_THREAD_DATA(struct { char s[128]; }, + strerror_buffer, + { "" }); void ovs_assert_failure(const char *where, const char *function, @@ -281,6 +283,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 +388,30 @@ 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 and may also be visible in system process listings + * and debuggers.) */ +void +set_subprogram_name(const char *name) +{ + free(subprogram_name_set(xstrdup(name))); +#if HAVE_GLIBC_PTHREAD_SETNAME_NP + pthread_setname_np(pthread_self(), name); +#elif HAVE_NETBSD_PTHREAD_SETNAME_NP + pthread_setname_np(pthread_self(), "%s", name); +#elif HAVE_PTHREAD_SET_NAME_NP + pthread_set_name_np(pthread_self(), name); +#endif +} + /* Returns a pointer to a string describing the program version. The * caller must not modify or free the returned string. */