]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
dmesg: Add release_buffer function
authorTobias Stoeckmann <tobias@stoeckmann.org>
Wed, 4 Feb 2026 18:15:21 +0000 (19:15 +0100)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Wed, 4 Feb 2026 18:37:52 +0000 (19:37 +0100)
The buffer handling is properly split into three functions now:

- prepare_buffer
- print_buffer
- release_buffer

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

index 8bfd712e84f136663c28f6ae822aadaf59759b81..85007c2a6155bcde40c73d71ef2507294e4d356c 100644 (file)
@@ -742,6 +742,18 @@ static ssize_t prepare_buffer(struct dmesg_control *ctl, char **buf)
        return n;
 }
 
+static void release_buffer(struct dmesg_control *ctl, char *buf)
+{
+       if (!ctl->mmap_buff)
+               free(buf);
+       if (ctl->kmsg >= 0)
+               close(ctl->kmsg);
+       if (ctl->json && ul_jsonwrt_is_ready(&ctl->jfmt)) {
+               ul_jsonwrt_array_close(&ctl->jfmt);
+               ul_jsonwrt_root_close(&ctl->jfmt);
+       }
+}
+
 static int fwrite_hex(const char *buf, size_t size, FILE *out)
 {
        size_t i;
@@ -1917,14 +1929,7 @@ int main(int argc, char *argv[])
                n = prepare_buffer(&ctl, &buf);
                if (n > 0)
                        print_buffer(&ctl, buf, n);
-               if (!ctl.mmap_buff)
-                       free(buf);
-               if (ctl.kmsg >= 0)
-                       close(ctl.kmsg);
-               if (ctl.json && ul_jsonwrt_is_ready(&ctl.jfmt)) {
-                       ul_jsonwrt_array_close(&ctl.jfmt);
-                       ul_jsonwrt_root_close(&ctl.jfmt);
-               }
+               release_buffer(&ctl, buf);
                if (n < 0)
                        err(EXIT_FAILURE, _("read kernel buffer failed"));
                else if (ctl.action == SYSLOG_ACTION_READ_CLEAR)