From: Ray Strode Date: Fri, 24 Nov 2023 16:02:33 +0000 (-0500) Subject: ply-utils: Add function for getting current kmsg log levels X-Git-Tag: 23.51.283~17^2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=20b019a4893444dc15ceb0516f29a90ec5d5cc72;p=thirdparty%2Fplymouth.git ply-utils: Add function for getting current kmsg log levels We need to know two things: 1. What log level threshold to follow when showing kernel messages to the console 2. What log level to use if a message comes in without a log level This commit adds a new function `ply_get_kmsg_log_levels` that returns those two values. --- diff --git a/src/libply/ply-utils.c b/src/libply/ply-utils.c index 2de5f401..dd6294f6 100644 --- a/src/libply/ply-utils.c +++ b/src/libply/ply-utils.c @@ -997,6 +997,66 @@ int ply_guess_device_scale (uint32_t width, return get_device_scale (width, height, 0, 0, true); } +void +ply_get_kmsg_log_levels (int *current_log_level, + int *default_log_level) +{ + static double last_update_time = 0; + static int cached_current_log_level = 0; + static int cached_default_log_level = 0; + char log_levels[4096] = ""; + double current_time; + char *field, *fields; + int fd; + + current_time = ply_get_timestamp (); + + if ((current_time - last_update_time) < 1.0) { + *current_log_level = cached_current_log_level; + *default_log_level = cached_default_log_level; + return; + } + + ply_trace ("opening /proc/sys/kernel/printk"); + fd = open ("/proc/sys/kernel/printk", O_RDONLY); + + if (fd < 0) { + ply_trace ("couldn't open it: %m"); + return; + } + + ply_trace ("reading kmsg log levels"); + if (read (fd, log_levels, sizeof(log_levels) - 1) < 0) { + ply_trace ("couldn't read it: %m"); + close (fd); + return; + } + close (fd); + + field = strtok_r (log_levels, " \t", &fields); + + if (field == NULL) { + ply_trace ("Couldn't parse current log level: %m"); + return; + } + + *current_log_level = atoi (field); + + field = strtok_r (NULL, " \t", &fields); + + if (field == NULL) { + ply_trace ("Couldn't parse default log level: %m"); + return; + } + + *default_log_level = atoi (field); + + cached_current_log_level = *current_log_level; + cached_default_log_level = *default_log_level; + + last_update_time = current_time; +} + static const char * ply_get_kernel_command_line (void) { diff --git a/src/libply/ply-utils.h b/src/libply/ply-utils.h index 89bb37fd..8000632b 100644 --- a/src/libply/ply-utils.h +++ b/src/libply/ply-utils.h @@ -148,6 +148,9 @@ int ply_get_device_scale (uint32_t width, int ply_guess_device_scale (uint32_t width, uint32_t height); +void ply_get_kmsg_log_levels (int *current_log_level, + int *default_log_level); + const char *ply_kernel_command_line_get_string_after_prefix (const char *prefix); bool ply_kernel_command_line_has_argument (const char *argument); void ply_kernel_command_line_override (const char *command_line);