]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shutdown: move printk changing code int generic code
authorLennart Poettering <lennart@poettering.net>
Mon, 15 Sep 2025 16:18:31 +0000 (18:18 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 26 Sep 2025 07:39:00 +0000 (09:39 +0200)
src/shared/meson.build
src/shared/printk-util.c [new file with mode: 0644]
src/shared/printk-util.h [new file with mode: 0644]
src/shutdown/shutdown.c

index 9acb83a47e7859269f231ab779c9de3fbd7ae46f..71735aaaa243819f4443415e75b4cdb32ceee3d6 100644 (file)
@@ -154,6 +154,7 @@ shared_sources = files(
         'polkit-agent.c',
         'portable-util.c',
         'pretty-print.c',
+        'printk-util.c',
         'prompt-util.c',
         'ptyfwd.c',
         'qrcode-util.c',
diff --git a/src/shared/printk-util.c b/src/shared/printk-util.c
new file mode 100644 (file)
index 0000000..0f2221b
--- /dev/null
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "alloc-util.h"
+#include "extract-word.h"
+#include "log.h"
+#include "parse-util.h"
+#include "printk-util.h"
+#include "sysctl-util.h"
+
+int sysctl_printk_read(void) {
+        int r;
+
+        _cleanup_free_ char *sysctl_printk_vals = NULL;
+        r = sysctl_read("kernel/printk", &sysctl_printk_vals);
+        if (r < 0)
+                return log_debug_errno(r, "Cannot read sysctl kernel.printk: %m");
+
+        _cleanup_free_ char *sysctl_printk_curr = NULL;
+        const char *p = sysctl_printk_vals;
+        r = extract_first_word(&p, &sysctl_printk_curr, NULL, 0);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to split out kernel printk priority: %m");
+        if (r == 0)
+                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Short read while reading kernel.printk sysctl");
+
+        int current_lvl;
+        r = safe_atoi(sysctl_printk_curr, &current_lvl);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to parse kernel.printk sysctl: %s", sysctl_printk_vals);
+
+        return current_lvl;
+}
+
+int sysctl_printk_write(int l) {
+        int r;
+
+        r = sysctl_writef("kernel/printk", "%i", l);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to set kernel.printk to %i: %m", l);
+
+        return 0;
+}
diff --git a/src/shared/printk-util.h b/src/shared/printk-util.h
new file mode 100644 (file)
index 0000000..e2bd0db
--- /dev/null
@@ -0,0 +1,5 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int sysctl_printk_read(void);
+int sysctl_printk_write(int l);
index 9a114d9be54dd663e9b7b1d9984c3447c9ec9732..97ae7d119ce330881e356db86bc77663ecf3c00f 100644 (file)
@@ -36,6 +36,7 @@
 #include "log.h"
 #include "parse-util.h"
 #include "pidref.h"
+#include "printk-util.h"
 #include "process-util.h"
 #include "reboot-util.h"
 #include "rlimit-util.h"
@@ -272,42 +273,14 @@ int sync_with_progress(int fd) {
         return r;
 }
 
-static int read_current_sysctl_printk_log_level(void) {
-        _cleanup_free_ char *sysctl_printk_vals = NULL, *sysctl_printk_curr = NULL;
-        int current_lvl;
-        const char *p;
-        int r;
-
-        r = sysctl_read("kernel/printk", &sysctl_printk_vals);
-        if (r < 0)
-                return log_debug_errno(r, "Cannot read sysctl kernel.printk: %m");
-
-        p = sysctl_printk_vals;
-        r = extract_first_word(&p, &sysctl_printk_curr, NULL, 0);
-        if (r < 0)
-                return log_debug_errno(r, "Failed to split out kernel printk priority: %m");
-        if (r == 0)
-                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Short read while reading kernel.printk sysctl");
-
-        r = safe_atoi(sysctl_printk_curr, &current_lvl);
-        if (r < 0)
-                return log_debug_errno(r, "Failed to parse kernel.printk sysctl: %s", sysctl_printk_vals);
-
-        return current_lvl;
-}
-
 static void bump_sysctl_printk_log_level(int min_level) {
-        int current_lvl, r;
-
         /* Set the logging level to be able to see messages with log level smaller or equal to min_level */
 
-        current_lvl = read_current_sysctl_printk_log_level();
+        int current_lvl = sysctl_printk_read();
         if (current_lvl < 0 || current_lvl >= min_level + 1)
                 return;
 
-        r = sysctl_writef("kernel/printk", "%i", min_level + 1);
-        if (r < 0)
-                log_debug_errno(r, "Failed to bump kernel.printk to %i: %m", min_level + 1);
+        (void) sysctl_printk_write(min_level + 1);
 }
 
 static void init_watchdog(void) {