Harald Hoyer fe20ad
From f173657bccae4dd0cbfe0a7ed33ebc9117b74f05 Mon Sep 17 00:00:00 2001
Harald Hoyer fe20ad
From: Lennart Poettering <lennart@poettering.net>
Harald Hoyer fe20ad
Date: Wed, 24 Jul 2013 07:24:05 +0200
Harald Hoyer fe20ad
Subject: [PATCH] core: synchronously block when logging
Harald Hoyer fe20ad
Harald Hoyer fe20ad
Previously, the logging sockets were asynchronous and if clogged we'd
Harald Hoyer fe20ad
lose messages. We did this to be extra careful given that PID 1 might
Harald Hoyer fe20ad
need to spawn the logging daemon as response to PID 1's own log messages
Harald Hoyer fe20ad
and we really should avoid a deadlock in that case.
Harald Hoyer fe20ad
Harald Hoyer fe20ad
As it turns out this causes loss of too many messages, hence make the
Harald Hoyer fe20ad
socket blocking again, however put a time limit on it to avoid unbounded
Harald Hoyer fe20ad
deadlocks in the unlikely case they happen.
Harald Hoyer fe20ad
Harald Hoyer fe20ad
https://bugs.freedesktop.org/show_bug.cgi?id=66664
Harald Hoyer fe20ad
---
Harald Hoyer fe20ad
 src/shared/log.c | 12 ++++++++----
Harald Hoyer fe20ad
 1 file changed, 8 insertions(+), 4 deletions(-)
Harald Hoyer fe20ad
Harald Hoyer fe20ad
diff --git a/src/shared/log.c b/src/shared/log.c
Harald Hoyer fe20ad
index 27317f7..8f4995a 100644
Harald Hoyer fe20ad
--- a/src/shared/log.c
Harald Hoyer fe20ad
+++ b/src/shared/log.c
Harald Hoyer fe20ad
@@ -115,16 +115,20 @@ void log_close_syslog(void) {
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 static int create_log_socket(int type) {
Harald Hoyer fe20ad
         int fd;
Harald Hoyer fe20ad
+        struct timeval tv;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        /* All output to the syslog/journal fds we do asynchronously,
Harald Hoyer fe20ad
-         * and if the buffers are full we just drop the messages */
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
-        fd = socket(AF_UNIX, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
Harald Hoyer fe20ad
+        fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
Harald Hoyer fe20ad
         if (fd < 0)
Harald Hoyer fe20ad
                 return -errno;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         fd_inc_sndbuf(fd, SNDBUF_SIZE);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
+        /* We need a blocking fd here since we'd otherwise lose
Harald Hoyer fe20ad
+        messages way too early. However, let's not hang forever in the
Harald Hoyer fe20ad
+        unlikely case of a deadlock. */
Harald Hoyer fe20ad
+        timeval_store(&tv, 1*USEC_PER_MINUTE);
Harald Hoyer fe20ad
+        setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
Harald Hoyer fe20ad
+
Harald Hoyer fe20ad
         return fd;
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad