Skip to content

Commit

Permalink
util: Make subprogram_name thread-specific.
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Pfaff <[email protected]>
  • Loading branch information
blp committed Jul 12, 2013
1 parent 4c694ff commit bc9fb3a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions lib/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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.
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions lib/vlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit bc9fb3a

Please sign in to comment.