summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorrofl0r <retnyg@gmx.net>2018-12-18 23:48:57 +0000
committerrofl0r <rofl0r@users.noreply.github.com>2019-12-21 00:43:45 +0000
commitfa2ad0cf9ac1bf24c7c4ddb188b5922132e38f73 (patch)
tree75f90c11054c805773a927d438116baedd20538e /src
parentb09d8d927de61e5b4411f8e9f713bfcf10a04796 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/log.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/log.c b/src/log.c
index f85d29d..0ed686b 100644
--- a/src/log.c
+++ b/src/log.c
@@ -29,6 +29,7 @@
#include "utils.h"
#include "vector.h"
#include "conf.h"
+#include <pthread.h>
static const char *syslog_level[] = {
NULL,
@@ -45,6 +46,8 @@ static const char *syslog_level[] = {
#define TIME_LENGTH 16
#define STRING_LENGTH 800
+static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
+
/*
* Global file descriptor for the log file
*/
@@ -165,12 +168,14 @@ void log_message (int level, const char *fmt, ...)
goto out;
if (config.syslog) {
+ pthread_mutex_lock(&log_mutex);
#ifdef HAVE_VSYSLOG_H
vsyslog (level, fmt, args);
#else
vsnprintf (str, STRING_LENGTH, fmt, args);
syslog (level, "%s", str);
#endif
+ pthread_mutex_unlock(&log_mutex);
} else {
char *p;
@@ -196,7 +201,10 @@ void log_message (int level, const char *fmt, ...)
assert (log_file_fd >= 0);
+ pthread_mutex_lock(&log_mutex);
ret = write (log_file_fd, str, strlen (str));
+ pthread_mutex_unlock(&log_mutex);
+
if (ret == -1) {
config.syslog = TRUE;
@@ -207,7 +215,10 @@ void log_message (int level, const char *fmt, ...)
"Falling back to syslog logging");
}
+ pthread_mutex_lock(&log_mutex);
fsync (log_file_fd);
+ pthread_mutex_unlock(&log_mutex);
+
}
out: