]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal/journald-kmsg.c
man: fix incorrectly placed full stop
[thirdparty/systemd.git] / src / journal / journald-kmsg.c
index 27bb7a969c4fc7e3c0e87a824ca6009e18c44776..6e7c806fd8218bde2f51a36f395dd607579504c4 100644 (file)
@@ -1,22 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2011 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
 
 #include <fcntl.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #include <unistd.h>
 
-#include "libudev.h"
+#include "sd-device.h"
 #include "sd-messages.h"
 
 #include "alloc-util.h"
+#include "device-util.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "format-util.h"
 #include "string-util.h"
 
 void server_forward_kmsg(
-        Server *s,
-        int priority,
-        const char *identifier,
-        const char *message,
-        const struct ucred *ucred) {
+                Server *s,
+                int priority,
+                const char *identifier,
+                const char *message,
+                const struct ucred *ucred) {
 
         _cleanup_free_ char *ident_buf = NULL;
         struct iovec iovec[5];
@@ -111,7 +94,7 @@ static bool is_us(const char *identifier, const char *pid) {
                streq(identifier, program_invocation_short_name);
 }
 
-static void dev_kmsg_record(Server *s, const char *p, size_t l) {
+void dev_kmsg_record(Server *s, char *p, size_t l) {
 
         _cleanup_free_ char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL, *identifier = NULL, *pid = NULL;
         struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS];
@@ -138,7 +121,7 @@ static void dev_kmsg_record(Server *s, const char *p, size_t l) {
         if (r < 0 || priority < 0 || priority > 999)
                 return;
 
-        if (s->forward_to_kmsg && (priority & LOG_FACMASK) != LOG_KERN)
+        if (s->forward_to_kmsg && LOG_FAC(priority) != LOG_KERN)
                 return;
 
         l -= (e - p) + 1;
@@ -209,7 +192,7 @@ static void dev_kmsg_record(Server *s, const char *p, size_t l) {
 
                 e = memchr(k, '\n', l);
                 if (!e)
-                        return;
+                        goto finish;
 
                 *e = 0;
 
@@ -227,26 +210,22 @@ static void dev_kmsg_record(Server *s, const char *p, size_t l) {
         }
 
         if (kernel_device) {
-                struct udev_device *ud;
+                _cleanup_(sd_device_unrefp) sd_device *d = NULL;
 
-                ud = udev_device_new_from_device_id(s->udev, kernel_device);
-                if (ud) {
+                if (sd_device_new_from_device_id(&d, kernel_device) >= 0) {
                         const char *g;
-                        struct udev_list_entry *ll;
                         char *b;
 
-                        g = udev_device_get_devnode(ud);
-                        if (g) {
-                                b = strappend("_UDEV_DEVNODE=", g);
+                        if (sd_device_get_devname(d, &g) >= 0) {
+                                b = strjoin("_UDEV_DEVNODE=", g);
                                 if (b) {
                                         iovec[n++] = IOVEC_MAKE_STRING(b);
                                         z++;
                                 }
                         }
 
-                        g = udev_device_get_sysname(ud);
-                        if (g) {
-                                b = strappend("_UDEV_SYSNAME=", g);
+                        if (sd_device_get_sysname(d, &g) >= 0) {
+                                b = strjoin("_UDEV_SYSNAME=", g);
                                 if (b) {
                                         iovec[n++] = IOVEC_MAKE_STRING(b);
                                         z++;
@@ -254,25 +233,19 @@ static void dev_kmsg_record(Server *s, const char *p, size_t l) {
                         }
 
                         j = 0;
-                        ll = udev_device_get_devlinks_list_entry(ud);
-                        udev_list_entry_foreach(ll, ll) {
+                        FOREACH_DEVICE_DEVLINK(d, g) {
 
-                                if (j > N_IOVEC_UDEV_FIELDS)
+                                if (j >= N_IOVEC_UDEV_FIELDS)
                                         break;
 
-                                g = udev_list_entry_get_name(ll);
-                                if (g) {
-                                        b = strappend("_UDEV_DEVLINK=", g);
-                                        if (b) {
-                                                iovec[n++] = IOVEC_MAKE_STRING(b);
-                                                z++;
-                                        }
+                                b = strjoin("_UDEV_DEVLINK=", g);
+                                if (b) {
+                                        iovec[n++] = IOVEC_MAKE_STRING(b);
+                                        z++;
                                 }
 
                                 j++;
                         }
-
-                        udev_device_unref(ud);
                 }
         }
 
@@ -287,7 +260,7 @@ static void dev_kmsg_record(Server *s, const char *p, size_t l) {
         if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
                 iovec[n++] = IOVEC_MAKE_STRING(syslog_facility);
 
-        if ((priority & LOG_FACMASK) == LOG_KERN)
+        if (LOG_FAC(priority) == LOG_KERN)
                 iovec[n++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel");
         else {
                 pl -= syslog_parse_identifier((const char**) &p, &identifier, &pid);
@@ -298,13 +271,13 @@ static void dev_kmsg_record(Server *s, const char *p, size_t l) {
                         goto finish;
 
                 if (identifier) {
-                        syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
+                        syslog_identifier = strjoin("SYSLOG_IDENTIFIER=", identifier);
                         if (syslog_identifier)
                                 iovec[n++] = IOVEC_MAKE_STRING(syslog_identifier);
                 }
 
                 if (pid) {
-                        syslog_pid = strappend("SYSLOG_PID=", pid);
+                        syslog_pid = strjoin("SYSLOG_PID=", pid);
                         if (syslog_pid)
                                 iovec[n++] = IOVEC_MAKE_STRING(syslog_pid);
                 }
@@ -342,7 +315,7 @@ static int server_read_dev_kmsg(Server *s) {
                 if (IN_SET(errno, EAGAIN, EINTR, EPIPE))
                         return 0;
 
-                return log_error_errno(errno, "Failed to read from kernel: %m");
+                return log_error_errno(errno, "Failed to read from /dev/kmsg: %m");
         }
 
         dev_kmsg_record(s, buffer, l);
@@ -443,19 +416,23 @@ fail:
 }
 
 int server_open_kernel_seqnum(Server *s) {
-        _cleanup_close_ int fd;
+        _cleanup_close_ int fd = -1;
+        const char *fn;
         uint64_t *p;
         int r;
 
         assert(s);
 
-        /* We store the seqnum we last read in an mmaped file. That
-         * way we can just use it like a variable, but it is
-         * persistent and automatically flushed at reboot. */
+        /* We store the seqnum we last read in an mmapped file. That way we can just use it like a variable,
+         * but it is persistent and automatically flushed at reboot. */
+
+        if (!s->read_kmsg)
+                return 0;
 
-        fd = open("/run/systemd/journal/kernel-seqnum", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
+        fn = strjoina(s->runtime_directory, "/kernel-seqnum");
+        fd = open(fn, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
         if (fd < 0) {
-                log_error_errno(errno, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
+                log_error_errno(errno, "Failed to open %s, ignoring: %m", fn);
                 return 0;
         }