ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
803fb7
From 1c33de9e1370bc56e10f3b5306e27c8aa6a18873 Mon Sep 17 00:00:00 2001
803fb7
From: Michal Sekletar <msekleta@redhat.com>
803fb7
Date: Mon, 1 Feb 2016 10:44:58 +0100
803fb7
Subject: [PATCH] journalctl: make "journalctl /dev/sda" work
803fb7
803fb7
Currently when journalctl is called with path to block device node we
803fb7
add following match _KERNEL_DEVICE=b$MAJOR:$MINOR.
803fb7
803fb7
That is not sufficient to actually obtain logs about the disk because
803fb7
dev_printk() kernel helper puts to /dev/kmsg information about the
803fb7
device in following format, +$SUBSYSTEM:$ADDRESS,
803fb7
e.g. "+pci:pci:0000:00:14.0".
803fb7
803fb7
Now we will walk upward the syspath and add match for every device in
803fb7
format produced by dev_printk() as well as match for its device node if
803fb7
it exists.
803fb7
803fb7
Cherry-picked from: 795ab08f783e78e85f1493879f13ac44cb113b00
803fb7
Resolves: #947636
803fb7
---
803fb7
 Makefile.am              |   3 +-
803fb7
 src/journal/journalctl.c | 118 ++++++++++++++++++++++++++++++++++++++---------
803fb7
 2 files changed, 97 insertions(+), 24 deletions(-)
803fb7
803fb7
diff --git a/Makefile.am b/Makefile.am
803fb7
index 2645f66bc..255937643 100644
803fb7
--- a/Makefile.am
803fb7
+++ b/Makefile.am
803fb7
@@ -4245,7 +4245,8 @@ journalctl_LDADD = \
803fb7
 	libsystemd-journal-internal.la \
803fb7
 	libsystemd-internal.la \
803fb7
 	libsystemd-logs.la \
803fb7
-	libsystemd-shared.la
803fb7
+	libsystemd-shared.la \
803fb7
+	libudev-core.la
803fb7
 
803fb7
 if HAVE_ACL
803fb7
 journalctl_LDADD += \
803fb7
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
803fb7
index 836d7d214..3db1cd24e 100644
803fb7
--- a/src/journal/journalctl.c
803fb7
+++ b/src/journal/journalctl.c
803fb7
@@ -63,6 +63,8 @@
803fb7
 #include "mkdir.h"
803fb7
 #include "bus-util.h"
803fb7
 #include "bus-error.h"
803fb7
+#include "udev.h"
803fb7
+#include "udev-util.h"
803fb7
 
803fb7
 #define DEFAULT_FSS_INTERVAL_USEC (15*USEC_PER_MINUTE)
803fb7
 
803fb7
@@ -134,6 +136,80 @@ typedef struct boot_id_t {
803fb7
         LIST_FIELDS(struct boot_id_t, boot_list);
803fb7
 } boot_id_t;
803fb7
 
803fb7
+static int add_matches_for_device(sd_journal *j, const char *devpath) {
803fb7
+        int r;
803fb7
+        _cleanup_udev_unref_ struct udev *udev = NULL;
803fb7
+        _cleanup_udev_device_unref_ struct udev_device *device = NULL;
803fb7
+        struct udev_device *d = NULL;
803fb7
+        struct stat st;
803fb7
+
803fb7
+        assert(j);
803fb7
+        assert(devpath);
803fb7
+
803fb7
+        if (!path_startswith(devpath, "/dev/")) {
803fb7
+                log_error("Devpath does not start with /dev/");
803fb7
+                return -EINVAL;
803fb7
+        }
803fb7
+
803fb7
+        udev = udev_new();
803fb7
+        if (!udev)
803fb7
+                return log_oom();
803fb7
+
803fb7
+        r = stat(devpath, &st);
803fb7
+        if (r < 0)
803fb7
+                log_error_errno(errno, "Couldn't stat file: %m");
803fb7
+
803fb7
+        d = device = udev_device_new_from_devnum(udev, S_ISBLK(st.st_mode) ? 'b' : 'c', st.st_rdev);
803fb7
+        if (!device)
803fb7
+                return log_error_errno(errno, "Failed to get udev device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev));
803fb7
+
803fb7
+        while (d) {
803fb7
+                _cleanup_free_ char *match = NULL;
803fb7
+                const char *subsys, *sysname, *devnode;
803fb7
+
803fb7
+                subsys = udev_device_get_subsystem(d);
803fb7
+                if (!subsys) {
803fb7
+                        d = udev_device_get_parent(d);
803fb7
+                        continue;
803fb7
+                }
803fb7
+
803fb7
+                sysname = udev_device_get_sysname(d);
803fb7
+                if (!sysname) {
803fb7
+                        d = udev_device_get_parent(d);
803fb7
+                        continue;
803fb7
+                }
803fb7
+
803fb7
+                match = strjoin("_KERNEL_DEVICE=+", subsys, ":", sysname, NULL);
803fb7
+                if (!match)
803fb7
+                        return log_oom();
803fb7
+
803fb7
+                r = sd_journal_add_match(j, match, 0);
803fb7
+                if (r < 0)
803fb7
+                        return log_error_errno(r, "Failed to add match: %m");
803fb7
+
803fb7
+                devnode = udev_device_get_devnode(d);
803fb7
+                if (devnode) {
803fb7
+                        _cleanup_free_ char *match1 = NULL;
803fb7
+
803fb7
+                        r = stat(devnode, &st);
803fb7
+                        if (r < 0)
803fb7
+                                return log_error_errno(r, "Failed to stat() device node \"%s\": %m", devnode);
803fb7
+
803fb7
+                        r = asprintf(&match1, "_KERNEL_DEVICE=%c%u:%u", S_ISBLK(st.st_mode) ? 'b' : 'c', major(st.st_rdev), minor(st.st_rdev));
803fb7
+                        if (r < 0)
803fb7
+                                return log_oom();
803fb7
+
803fb7
+                        r = sd_journal_add_match(j, match1, 0);
803fb7
+                        if (r < 0)
803fb7
+                                return log_error_errno(r, "Failed to add match: %m");
803fb7
+                }
803fb7
+
803fb7
+                d = udev_device_get_parent(d);
803fb7
+        }
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
 static void pager_open_if_enabled(void) {
803fb7
 
803fb7
         if (arg_no_pager)
803fb7
@@ -788,13 +864,12 @@ static int add_matches(sd_journal *j, char **args) {
803fb7
                         have_term = false;
803fb7
 
803fb7
                 } else if (path_is_absolute(*i)) {
803fb7
-                        _cleanup_free_ char *p, *t = NULL, *t2 = NULL;
803fb7
+                        _cleanup_free_ char *p, *t = NULL, *t2 = NULL, *interpreter = NULL;
803fb7
                         const char *path;
803fb7
-                        _cleanup_free_ char *interpreter = NULL;
803fb7
                         struct stat st;
803fb7
 
803fb7
                         p = canonicalize_file_name(*i);
803fb7
-                        path = p ? p : *i;
803fb7
+                        path = p ?: *i;
803fb7
 
803fb7
                         if (stat(path, &st) < 0)
803fb7
                                 return log_error_errno(errno, "Couldn't stat file: %m");
803fb7
@@ -808,40 +883,37 @@ static int add_matches(sd_journal *j, char **args) {
803fb7
                                                 return log_oom();
803fb7
 
803fb7
                                         t = strappend("_COMM=", comm);
803fb7
+                                        if (!t)
803fb7
+                                                return log_oom();
803fb7
 
803fb7
                                         /* Append _EXE only if the interpreter is not a link.
803fb7
                                            Otherwise, it might be outdated often. */
803fb7
-                                        if (lstat(interpreter, &st) == 0 &&
803fb7
-                                            !S_ISLNK(st.st_mode)) {
803fb7
+                                        if (lstat(interpreter, &st) == 0 && !S_ISLNK(st.st_mode)) {
803fb7
                                                 t2 = strappend("_EXE=", interpreter);
803fb7
                                                 if (!t2)
803fb7
                                                         return log_oom();
803fb7
                                         }
803fb7
-                                } else
803fb7
+                                } else {
803fb7
                                         t = strappend("_EXE=", path);
803fb7
-                        } else if (S_ISCHR(st.st_mode)) {
803fb7
-                                if (asprintf(&t, "_KERNEL_DEVICE=c%u:%u",
803fb7
-                                             major(st.st_rdev),
803fb7
-                                             minor(st.st_rdev)) < 0)
803fb7
-                                        return -ENOMEM;
803fb7
-                        } else if (S_ISBLK(st.st_mode)) {
803fb7
-                                if (asprintf(&t, "_KERNEL_DEVICE=b%u:%u",
803fb7
-                                             major(st.st_rdev),
803fb7
-                                             minor(st.st_rdev)) < 0)
803fb7
-                                        return -ENOMEM;
803fb7
+                                        if (!t)
803fb7
+                                                return log_oom();
803fb7
+                                }
803fb7
+
803fb7
+                                r = sd_journal_add_match(j, t, 0);
803fb7
+
803fb7
+                                if (r >=0 && t2)
803fb7
+                                        r = sd_journal_add_match(j, t2, 0);
803fb7
+
803fb7
+                        } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
803fb7
+                                r = add_matches_for_device(j, path);
803fb7
+                                if (r < 0)
803fb7
+                                        return r;
803fb7
                         } else {
803fb7
                                 log_error("File is neither a device node, nor regular file, nor executable: %s", *i);
803fb7
                                 return -EINVAL;
803fb7
                         }
803fb7
 
803fb7
-                        if (!t)
803fb7
-                                return log_oom();
803fb7
-
803fb7
-                        r = sd_journal_add_match(j, t, 0);
803fb7
-                        if (t2)
803fb7
-                                r = sd_journal_add_match(j, t2, 0);
803fb7
                         have_term = true;
803fb7
-
803fb7
                 } else {
803fb7
                         r = sd_journal_add_match(j, *i, 0);
803fb7
                         have_term = true;