#include "lib_errors.h"
#include "lib/hook.h"
#include "printfrr.h"
+#include "frr_pthread.h"
#ifndef SUNOS_5
#include <sys/un.h>
void zlog_filter_clear(void)
{
- pthread_mutex_lock(&loglock);
- zlog_filter_count = 0;
- pthread_mutex_unlock(&loglock);
+ frr_with_mutex(&loglock) {
+ zlog_filter_count = 0;
+ }
}
int zlog_filter_add(const char *filter)
{
- pthread_mutex_lock(&loglock);
+ frr_with_mutex(&loglock) {
+ if (zlog_filter_count >= ZLOG_FILTERS_MAX)
+ return 1;
- int ret = 0;
+ if (zlog_filter_lookup(filter) != -1)
+ /* Filter already present */
+ return -1;
- if (zlog_filter_count >= ZLOG_FILTERS_MAX) {
- ret = 1;
- goto done;
- }
+ strlcpy(zlog_filters[zlog_filter_count], filter,
+ sizeof(zlog_filters[0]));
- if (zlog_filter_lookup(filter) != -1) {
- /* Filter already present */
- ret = -1;
- goto done;
- }
+ if (zlog_filters[zlog_filter_count][0] == '\0')
+ /* Filter was either empty or didn't get copied
+ * correctly
+ */
+ return -1;
- strlcpy(zlog_filters[zlog_filter_count], filter,
- sizeof(zlog_filters[0]));
-
- if (zlog_filters[zlog_filter_count][0] == '\0') {
- /* Filter was either empty or didn't get copied correctly */
- ret = -1;
- goto done;
+ zlog_filter_count++;
}
-
- zlog_filter_count++;
-
-done:
- pthread_mutex_unlock(&loglock);
- return ret;
+ return 0;
}
int zlog_filter_del(const char *filter)
{
- pthread_mutex_lock(&loglock);
-
- int found_idx = zlog_filter_lookup(filter);
- int last_idx = zlog_filter_count - 1;
- int ret = 0;
+ frr_with_mutex(&loglock) {
+ int found_idx = zlog_filter_lookup(filter);
+ int last_idx = zlog_filter_count - 1;
- if (found_idx == -1) {
- /* Didn't find the filter to delete */
- ret = -1;
- goto done;
- }
-
- /* Adjust the filter array */
- memmove(zlog_filters[found_idx], zlog_filters[found_idx + 1],
- (last_idx - found_idx) * sizeof(zlog_filters[0]));
+ if (found_idx == -1)
+ /* Didn't find the filter to delete */
+ return -1;
- zlog_filter_count--;
+ /* Adjust the filter array */
+ memmove(zlog_filters[found_idx], zlog_filters[found_idx + 1],
+ (last_idx - found_idx) * sizeof(zlog_filters[0]));
-done:
- pthread_mutex_unlock(&loglock);
- return ret;
+ zlog_filter_count--;
+ }
+ return 0;
}
/* Dump all filters to buffer, delimited by new line */
int zlog_filter_dump(char *buf, size_t max_size)
{
- pthread_mutex_lock(&loglock);
-
- int ret = 0;
int len = 0;
- for (int i = 0; i < zlog_filter_count; i++) {
- ret = snprintf(buf + len, max_size - len, " %s\n",
- zlog_filters[i]);
- len += ret;
- if ((ret < 0) || ((size_t)len >= max_size)) {
- len = -1;
- goto done;
+ frr_with_mutex(&loglock) {
+ for (int i = 0; i < zlog_filter_count; i++) {
+ int ret;
+ ret = snprintf(buf + len, max_size - len, " %s\n",
+ zlog_filters[i]);
+ len += ret;
+ if ((ret < 0) || ((size_t)len >= max_size))
+ return -1;
}
}
-done:
- pthread_mutex_unlock(&loglock);
return len;
}
/* va_list version of zlog. */
void vzlog(int priority, const char *format, va_list args)
{
- pthread_mutex_lock(&loglock);
+ frr_mutex_lock_autounlock(&loglock);
char proto_str[32] = "";
int original_errno = errno;
if (msg != buf)
XFREE(MTYPE_TMP, msg);
errno = original_errno;
- pthread_mutex_unlock(&loglock);
}
int vzlog_test(int priority)
{
- pthread_mutex_lock(&loglock);
-
- int ret = 0;
+ frr_mutex_lock_autounlock(&loglock);
struct zlog *zl = zlog_default;
/* When zlog_default is also NULL, use stderr for logging. */
if (zl == NULL)
- ret = 1;
+ return 1;
/* Syslog output */
else if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
- ret = 1;
+ return 1;
/* File output. */
else if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
- ret = 1;
+ return 1;
/* stdout output. */
else if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
- ret = 1;
+ return 1;
/* Terminal monitor. */
else if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
- ret = 1;
+ return 1;
- pthread_mutex_unlock(&loglock);
-
- return ret;
+ return 0;
}
/*
void zlog_signal(int signo, const char *action, void *siginfo_v,
void *program_counter)
{
-#ifdef SA_SIGINFO
siginfo_t *siginfo = siginfo_v;
-#endif
time_t now;
char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
+ 100];
msgstart = fb.pos;
bprintfrr(&fb, "Received signal %d at %lld", signo, (long long)now);
-#ifdef SA_SIGINFO
if (program_counter)
bprintfrr(&fb, " (si_addr 0x%tx, PC 0x%tx)",
(ptrdiff_t)siginfo->si_addr,
else
bprintfrr(&fb, " (si_addr 0x%tx)",
(ptrdiff_t)siginfo->si_addr);
-#endif /* SA_SIGINFO */
bprintfrr(&fb, "; %s\n", action);
crash_write(&fb, msgstart);
openlog(progname, syslog_flags, zl->facility);
- pthread_mutex_lock(&loglock);
- zlog_default = zl;
- pthread_mutex_unlock(&loglock);
+ frr_with_mutex(&loglock) {
+ zlog_default = zl;
+ }
#ifdef HAVE_GLIBC_BACKTRACE
/* work around backtrace() using lazily resolved dynamically linked
void closezlog(void)
{
- pthread_mutex_lock(&loglock);
+ frr_mutex_lock_autounlock(&loglock);
+
struct zlog *zl = zlog_default;
closelog();
XFREE(MTYPE_ZLOG, zl);
zlog_default = NULL;
- pthread_mutex_unlock(&loglock);
}
/* Called from command.c. */
void zlog_set_level(zlog_dest_t dest, int log_level)
{
- pthread_mutex_lock(&loglock);
- zlog_default->maxlvl[dest] = log_level;
- pthread_mutex_unlock(&loglock);
+ frr_with_mutex(&loglock) {
+ zlog_default->maxlvl[dest] = log_level;
+ }
}
int zlog_set_file(const char *filename, int log_level)
if (fp == NULL) {
ret = 0;
} else {
- pthread_mutex_lock(&loglock);
- zl = zlog_default;
-
- /* Set flags. */
- zl->filename = XSTRDUP(MTYPE_ZLOG, filename);
- zl->maxlvl[ZLOG_DEST_FILE] = log_level;
- zl->fp = fp;
- logfile_fd = fileno(fp);
- pthread_mutex_unlock(&loglock);
+ frr_with_mutex(&loglock) {
+ zl = zlog_default;
+
+ /* Set flags. */
+ zl->filename = XSTRDUP(MTYPE_ZLOG, filename);
+ zl->maxlvl[ZLOG_DEST_FILE] = log_level;
+ zl->fp = fp;
+ logfile_fd = fileno(fp);
+ }
}
return ret;
/* Reset opend file. */
int zlog_reset_file(void)
{
- pthread_mutex_lock(&loglock);
+ frr_mutex_lock_autounlock(&loglock);
struct zlog *zl = zlog_default;
XFREE(MTYPE_ZLOG, zl->filename);
zl->filename = NULL;
- pthread_mutex_unlock(&loglock);
-
return 1;
}