shutdown: move printk changing code int generic code

This commit is contained in:
Lennart Poettering
2025-09-15 18:18:31 +02:00
parent 97940e77a9
commit 45c04464dc
4 changed files with 51 additions and 30 deletions

View 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',

42
src/shared/printk-util.c Normal file
View File

@@ -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;
}

5
src/shared/printk-util.h Normal file
View File

@@ -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);

View 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) {