[systemd-devel] [PATCH] Avoid a busy systemd-journald in LXC environments

Werner Fink werner at suse.de
Mon Dec 2 07:27:52 PST 2013


that is the systemd-journald may ignore /dec/kmsg which are not a valid
device but a lint to /dev/null as done in LinuX Containers (LXC).  This
change adds straight forward a check for /dev/kmsg.

Signed-off-by: Werner Fink <werner at suse.de>
---
 src/journal/journald-kmsg.c |   27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git src/journal/journald-kmsg.c src/journal/journald-kmsg.c
index af49d85..03d8f90 100644
--- src/journal/journald-kmsg.c
+++ src/journal/journald-kmsg.c
@@ -23,7 +23,9 @@
 #include <sys/epoll.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <sys/stat.h>
 #include <sys/socket.h>
+#include <sys/types.h>
 
 #include <systemd/sd-messages.h>
 #include <libudev.h>
@@ -377,12 +379,32 @@ int server_flush_dev_kmsg(Server *s) {
 
 int server_open_dev_kmsg(Server *s) {
         struct epoll_event ev;
+        struct stat st;
 
         assert(s);
 
         s->dev_kmsg_fd = open("/dev/kmsg", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
         if (s->dev_kmsg_fd < 0) {
-                log_warning("Failed to open /dev/kmsg, ignoring: %m");
+                /* Do not warn as it may not exists in LXC environments */
+                if (errno != ENOENT)
+                        log_warning("Failed to open /dev/kmsg, ignoring: %m");
+                return 0;
+        }
+
+        if (fstat(s->dev_kmsg_fd, &st) < 0) {
+                log_error("Failed to stat /dev/kmsg fd, ignoring: %m");
+                close_nointr_nofail(s->dev_kmsg_fd);
+                s->dev_kmsg_fd = -1;
+                return 0;
+        }
+
+        if (!S_ISCHR(st.st_mode) || major(st.st_rdev) != 1 || minor(st.st_rdev) != 11) {
+                int old_errno = errno;
+                errno = ENODEV;
+                log_warning("Irregular device /dev/kmsg, ignoring: %m");
+                errno = old_errno;
+                close_nointr_nofail(s->dev_kmsg_fd);
+                s->dev_kmsg_fd = -1;
                 return 0;
         }
 
@@ -391,6 +413,9 @@ int server_open_dev_kmsg(Server *s) {
         ev.data.fd = s->dev_kmsg_fd;
         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->dev_kmsg_fd, &ev) < 0) {
 
+                close_nointr_nofail(s->dev_kmsg_fd);
+                s->dev_kmsg_fd = -1;
+
                 /* This will fail with EPERM on older kernels where
                  * /dev/kmsg is not readable. */
                 if (errno == EPERM)
-- 
1.7.9.2



More information about the systemd-devel mailing list