A single printk() can contain multiple lines via multiple '\n's,
and toybox just spits it out to a single line with '\x0a' as the delimiter.

Fix it.

Test:
sh -c "printf 'line a\nline b'" > /dev/kmsg

Before patch:
[ 5346.513120] line a\x0aline b

After patch:
[ 5346.513120] line a
               line b

This patch is tested with -r, -T and -t.

Signed-off-by: Park Ju Hyung <[email protected]>
From dd7c4f69eca73c64a22c958468e13e207254b31e Mon Sep 17 00:00:00 2001
From: Park Ju Hyung <[email protected]>
Date: Sun, 1 Mar 2020 22:32:38 +0900
Subject: [PATCH] dmesg: fix multi-line messages

A single printk() can contain multiple lines via multiple '\n's,
and toybox just spits it out to a single line with '\x0a' as the delimiter.

Fix it.

Test:
sh -c "printf 'line a\nline b'" > /dev/kmsg

Before patch:
[ 5346.513120] line a\x0aline b

After patch:
[ 5346.513120] line a
               line b

This patch is tested with -r, -T and -t.

Signed-off-by: Park Ju Hyung <[email protected]>
---
 toys/lsb/dmesg.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/toys/lsb/dmesg.c b/toys/lsb/dmesg.c
index a55dabf5..35e98360 100644
--- a/toys/lsb/dmesg.c
+++ b/toys/lsb/dmesg.c
@@ -48,7 +48,7 @@ static void color(int c)
 static void format_message(char *msg, int new)
 {
   unsigned long long time_s, time_us;
-  int facpri, subsystem, pos;
+  int facpri, subsystem, pos, pad;
   char *p, *text;
 
   // The new /dev/kmsg and the old syslog(2) formats differ slightly.
@@ -72,7 +72,11 @@ static void format_message(char *msg, int new)
   if (toys.optflags&FLAG_r) {
     color(0);
     printf("<%d>", facpri);
-  }
+    if (facpri < 10)
+      pad = 3;
+    else
+      pad = 4;
+  } else pad = 0;
 
   // Format the time.
   if (!(toys.optflags&FLAG_t)) {
@@ -81,8 +85,12 @@ static void format_message(char *msg, int new)
       time_t t = TT.tea+time_s;
       char *ts = ctime(&t);
 
-      printf("[%.*s] ", (int)(strlen(ts)-1), ts);
-    } else printf("[%5lld.%06lld] ", time_s, time_us);
+      pad += strlen(ts) + 2;
+      printf("[%.*s] ", pad - 3, ts);
+    } else {
+      printf("[%5lld.%06lld] ", time_s, time_us);
+      pad += 15;
+    }
   }
 
   // Errors (or worse) are shown in red, subsystems are shown in yellow.
@@ -92,7 +100,19 @@ static void format_message(char *msg, int new)
     text += subsystem;
   }
   color(31*((facpri&7)<=3));
-  xputs(text);
+
+  // A single line from /dev/kmsg can contain multi-line messages
+  while ((p = strstr(text, "\\x0a"))) {
+    *p = '\0';
+    xputs(text);
+    for (int i = 0; i < pad; i++)
+      xputc(' ');
+    text = p + 4;
+  }
+
+  // strstr() failed, no multi-line messages
+  if (p == NULL)
+    xputs(text);
 }
 
 static int xklogctl(int type, char *buf, int len)
-- 
2.25.1

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to