]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
ply-utils: Add function for getting current kmsg log levels
authorRay Strode <rstrode@redhat.com>
Fri, 24 Nov 2023 16:02:33 +0000 (11:02 -0500)
committerRay Strode <rstrode@redhat.com>
Sat, 2 Dec 2023 23:19:33 +0000 (18:19 -0500)
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.

src/libply/ply-utils.c
src/libply/ply-utils.h

index 2de5f401d998eafbb3f3a6358e997cad7261a093..dd6294f69df8a581708c4f237b042481797e144e 100644 (file)
@@ -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)
 {
index 89bb37fd1ace7892e06016b85d8be10b853bf7d4..8000632b8dc4407a7f673442eb83ef3b804c6477 100644 (file)
@@ -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);