log.c: protect logging facility with a mutex
since the write syscall is used instead of stdio, accesses have been safe already, but it's better to use a mutex anyway to prevent out- of-order writes.
This commit is contained in:
parent
b09d8d927d
commit
fa2ad0cf9a
11
src/log.c
11
src/log.c
@ -29,6 +29,7 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
static const char *syslog_level[] = {
|
static const char *syslog_level[] = {
|
||||||
NULL,
|
NULL,
|
||||||
@ -45,6 +46,8 @@ static const char *syslog_level[] = {
|
|||||||
#define TIME_LENGTH 16
|
#define TIME_LENGTH 16
|
||||||
#define STRING_LENGTH 800
|
#define STRING_LENGTH 800
|
||||||
|
|
||||||
|
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Global file descriptor for the log file
|
* Global file descriptor for the log file
|
||||||
*/
|
*/
|
||||||
@ -165,12 +168,14 @@ void log_message (int level, const char *fmt, ...)
|
|||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
if (config.syslog) {
|
if (config.syslog) {
|
||||||
|
pthread_mutex_lock(&log_mutex);
|
||||||
#ifdef HAVE_VSYSLOG_H
|
#ifdef HAVE_VSYSLOG_H
|
||||||
vsyslog (level, fmt, args);
|
vsyslog (level, fmt, args);
|
||||||
#else
|
#else
|
||||||
vsnprintf (str, STRING_LENGTH, fmt, args);
|
vsnprintf (str, STRING_LENGTH, fmt, args);
|
||||||
syslog (level, "%s", str);
|
syslog (level, "%s", str);
|
||||||
#endif
|
#endif
|
||||||
|
pthread_mutex_unlock(&log_mutex);
|
||||||
} else {
|
} else {
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
@ -196,7 +201,10 @@ void log_message (int level, const char *fmt, ...)
|
|||||||
|
|
||||||
assert (log_file_fd >= 0);
|
assert (log_file_fd >= 0);
|
||||||
|
|
||||||
|
pthread_mutex_lock(&log_mutex);
|
||||||
ret = write (log_file_fd, str, strlen (str));
|
ret = write (log_file_fd, str, strlen (str));
|
||||||
|
pthread_mutex_unlock(&log_mutex);
|
||||||
|
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
config.syslog = TRUE;
|
config.syslog = TRUE;
|
||||||
|
|
||||||
@ -207,7 +215,10 @@ void log_message (int level, const char *fmt, ...)
|
|||||||
"Falling back to syslog logging");
|
"Falling back to syslog logging");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_lock(&log_mutex);
|
||||||
fsync (log_file_fd);
|
fsync (log_file_fd);
|
||||||
|
pthread_mutex_unlock(&log_mutex);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
|
Loading…
Reference in New Issue
Block a user