daandemeyer / rpms / systemd

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