Skip to content

Commit

Permalink
fatal-signal: Fix clang error due to lock.
Browse files Browse the repository at this point in the history
Due to not acquiring lock, clang reports:
  lib/vlog.c:618:12: error: reading variable 'log_fd' requires holding mutex
  'log_file_mutex' [-Werror,-Wthread-safety-analysis]
  return log_fd;

The patch fixes it by creating a function in vlog.c to write
directly to log file unsafely.

Tested-at: https://travis-ci.org/github/williamtu/ovs-travis/builds/666165883
Fixes: ecd4a8f ("fatal-signal: Log backtrace when no monitor daemon.")
Suggested-by: Ilya Maximets <[email protected]>
Acked-by: Ilya Maximets <[email protected]>
Signed-off-by: William Tu <[email protected]>
  • Loading branch information
williamtu committed Mar 24, 2020
1 parent ae2d6e3 commit ecbc7f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions include/openvswitch/vlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ void vlog_set_syslog_method(const char *method);
/* Configure syslog target. */
void vlog_set_syslog_target(const char *target);

/* Return the log_fd. */
int vlog_get_fd(void);
/* Write directly to log file. */
void vlog_direct_write_to_log_file_unsafe(const char *s);

/* Initialization. */
void vlog_init(void);
Expand Down
8 changes: 2 additions & 6 deletions lib/fatal-signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ send_backtrace_to_monitor(void) {
*/
char str[] = "SIGSEGV detected, backtrace:\n";

if (vlog_get_fd() < 0) {
return;
}

ignore(write(vlog_get_fd(), str, strlen(str)));
vlog_direct_write_to_log_file_unsafe(str);

for (int i = 0; i < dep; i++) {
char line[64];
Expand All @@ -210,7 +206,7 @@ send_backtrace_to_monitor(void) {
unw_bt[i].ip,
unw_bt[i].func,
unw_bt[i].offset);
ignore(write(vlog_get_fd(), line, strlen(line)));
vlog_direct_write_to_log_file_unsafe(line);
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions lib/vlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,19 @@ vlog_set_syslog_target(const char *target)
ovs_rwlock_unlock(&pattern_rwlock);
}

int
vlog_get_fd(void)
/*
* This function writes directly to log file without using async writer or
* taking a lock. Caller must hold 'log_file_mutex' or be sure that it's
* not necessary. Could be used in exceptional cases like dumping of backtrace
* on fatal signals.
*/
void
vlog_direct_write_to_log_file_unsafe(const char *s)
OVS_NO_THREAD_SAFETY_ANALYSIS
{
return log_fd;
if (log_fd >= 0) {
ignore(write(log_fd, s, strlen(s)));
}
}

/* Returns 'false' if 'facility' is not a valid string. If 'facility'
Expand Down

0 comments on commit ecbc7f0

Please sign in to comment.