]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
dmesg: Split preparing and printing of buffer
authorTobias Stoeckmann <tobias@stoeckmann.org>
Wed, 4 Feb 2026 18:01:13 +0000 (19:01 +0100)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Wed, 4 Feb 2026 18:37:49 +0000 (19:37 +0100)
Have a strict separation between preparing the buffer and printing
content of buffer. This will help in further changes to more efficiently
prepare output, i.e. pager setup.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
sys-utils/dmesg.c

index 8af67476c9970c3a60742ab70dd38486ef70d990..8bfd712e84f136663c28f6ae822aadaf59759b81 100644 (file)
@@ -273,8 +273,8 @@ struct dmesg_record {
                (_r)->caller_id[0] = 0; \
        } while (0)
 
-static int process_kmsg(struct dmesg_control *ctl);
-static int process_kmsg_file(struct dmesg_control *ctl, char **buf);
+static int print_kmsg(struct dmesg_control *ctl);
+static int print_kmsg_file(struct dmesg_control *ctl, size_t sz);
 
 static int set_level_color(int log_level, const char *mesg, size_t mesgsz)
 {
@@ -713,7 +713,7 @@ static ssize_t read_syslog_buffer(struct dmesg_control *ctl, char **buf)
 /*
  * Top level function to read (and print in case of kmesg) messages
  */
-static ssize_t process_buffer(struct dmesg_control *ctl, char **buf)
+static ssize_t prepare_buffer(struct dmesg_control *ctl, char **buf)
 {
        ssize_t n = -1;
 
@@ -731,12 +731,9 @@ static ssize_t process_buffer(struct dmesg_control *ctl, char **buf)
                break;
        case DMESG_METHOD_KMSG:
                if (ctl->filename)
-                       n = process_kmsg_file(ctl, buf);
+                       n = mmap_file_buffer(ctl, buf);
                else
-                       /*
-                        * Since kernel 3.5.0
-                        */
-                       n = process_kmsg(ctl);
+                       n = ctl->kmsg_first_read;
                break;
        default:
                abort();        /* impossible method -> drop core */
@@ -1355,6 +1352,17 @@ static void print_buffer(struct dmesg_control *ctl,
 {
        struct dmesg_record rec = { .next = buf, .next_size = size };
 
+       if (ctl->method == DMESG_METHOD_KMSG) {
+               if (ctl->filename)
+                       print_kmsg_file(ctl, size);
+               else
+                       /*
+                        * Since kernel 3.5.0
+                        */
+                       print_kmsg(ctl);
+               return;
+       }
+
        if (ctl->raw) {
                raw_print(ctl, buf, size);
                return;
@@ -1511,7 +1519,7 @@ mesg:
  *
  * Returns 0 on success, -1 on error.
  */
-static int process_kmsg(struct dmesg_control *ctl)
+static int print_kmsg(struct dmesg_control *ctl)
 {
        struct dmesg_record rec;
        ssize_t sz;
@@ -1542,20 +1550,15 @@ static int process_kmsg(struct dmesg_control *ctl)
        return 0;
 }
 
-static int process_kmsg_file(struct dmesg_control *ctl, char **buf)
+static int print_kmsg_file(struct dmesg_control *ctl, size_t sz)
 {
        char str[sizeof(ctl->kmsg_buf)];
        struct dmesg_record rec;
-       ssize_t sz;
        size_t len;
 
        if (ctl->method != DMESG_METHOD_KMSG || !ctl->filename)
                return -1;
 
-       sz = mmap_file_buffer(ctl, buf);
-       if (sz == -1)
-               return -1;
-
        while (sz > 0) {
                len = strnlen(ctl->mmap_buff, sz);
                if (len > sizeof(str))
@@ -1566,7 +1569,7 @@ static int process_kmsg_file(struct dmesg_control *ctl, char **buf)
                if (parse_kmsg_record(ctl, &rec, str, len) == 0)
                        print_record(ctl, &rec);
 
-               if (len < (size_t)sz)
+               if (len < sz)
                        len++;
 
                sz -= len;
@@ -1911,7 +1914,7 @@ int main(int argc, char *argv[])
                        errx(EXIT_FAILURE, _("only kmsg supports multi-line messages"));
                if (ctl.pager)
                        pager_redirect();
-               n = process_buffer(&ctl, &buf);
+               n = prepare_buffer(&ctl, &buf);
                if (n > 0)
                        print_buffer(&ctl, buf, n);
                if (!ctl.mmap_buff)