linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] printk: Make continuation flags from /dev/kmsg useful again
       [not found] <20191119170632.52119-1-james.byrne@origamienergy.com>
@ 2019-11-19 17:08 ` James Byrne
  2019-11-19 17:08 ` [PATCH 2/2] printk: Support message continuation from /dev/kmsg James Byrne
  1 sibling, 0 replies; 2+ messages in thread
From: James Byrne @ 2019-11-19 17:08 UTC (permalink / raw)
  To: LKML; +Cc: James Byrne, Petr Mladek, Sergey Senozhatsky, Steven Rostedt

Commit 5aa068ea4082 ("printk: remove games with previous record flags")
abolished the practice of setting the log flag to 'c' for the first
continuation line and '+' for subsequent lines. Since this change all
continuation lines are flagged with 'c' and '+' is never used.

From the point of view of a consumer of /dev/kmsg, the behaviour now
is not very useful because you cannot join lines together based on the
'c' flag since you do not know whether a line starting with 'c' was
actually a continuation of the previous line or not.

This commit changes the flag field emitted in /dev/kmsg to expose the
log buffer flags in a more useful way:

- If LOG_NEWLINE=1 and LOG_CONT=0 the flag will be '-', meaning this is
a single self-contained line.
- If LOG_NEWLINE=0 and LOG_CONT=0 the flag will be 'c', meaning that
this is potentially the start of a sequence of continuation lines.
- If LOG_NEWLINE=0 and LOG_CONT=1 the flag will be '+', meaning that
this is the middle of a sequence of continuation lines.
- If LOG_NEWLINE=1 and LOG_CONT=1 the flag will be '*', meaning that
this is the end of a sequence of continuation lines.

This allows a consumer to concatenate continuations in a straightforward
manner.

Signed-off-by: James Byrne <james.byrne@origamienergy.com>
---

 Documentation/ABI/testing/dev-kmsg | 14 ++++++++------
 kernel/printk/printk.c             | 19 +++++++++++++++++--
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/Documentation/ABI/testing/dev-kmsg b/Documentation/ABI/testing/dev-kmsg
index f307506eb54c..6326deeaf5e3 100644
--- a/Documentation/ABI/testing/dev-kmsg
+++ b/Documentation/ABI/testing/dev-kmsg
@@ -90,12 +90,14 @@ Description:	The /dev/kmsg character device node provides userspace access
 		  +sound:card0 - subsystem:devname
 
 		The flags field carries '-' by default. A 'c' indicates a
-		fragment of a line. Note, that these hints about continuation
-		lines are not necessarily correct, and the stream could be
-		interleaved with unrelated messages, but merging the lines in
-		the output usually produces better human readable results. A
-		similar logic is used internally when messages are printed to
-		the console, /proc/kmsg or the syslog() syscall.
+		fragment of a line. Following fragments are flagged with '+'
+		and the final fragment in a sequence is flagged with '*'. Note
+		that these hints about continuation lines are not necessarily
+		correct, and the stream could be interleaved with unrelated
+		messages, but merging the lines in the output usually produces
+		better human readable results. A similar logic is used
+		internally when messages are printed to the console, /proc/kmsg
+		or the syslog() syscall.
 
 		By default, kernel tries to avoid fragments by concatenating
 		when it can and fragments are rare; however, when extended
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index ca65327a6de8..a3db7f5e56d9 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -715,7 +715,7 @@ static ssize_t msg_print_ext_header(char *buf, size_t size,
 				    struct printk_log *msg, u64 seq)
 {
 	u64 ts_usec = msg->ts_nsec;
-	char caller[20];
+	char flag, caller[20];
 #ifdef CONFIG_PRINTK_CALLER
 	u32 id = msg->caller_id;
 
@@ -727,9 +727,24 @@ static ssize_t msg_print_ext_header(char *buf, size_t size,
 
 	do_div(ts_usec, 1000);
 
+	switch (msg->flags & (LOG_CONT|LOG_NEWLINE)) {
+	case LOG_CONT|LOG_NEWLINE:
+		flag = '*';
+		break;
+	case LOG_CONT:
+		flag = '+';
+		break;
+	case LOG_NEWLINE:
+		flag = '-';
+		break;
+	default:
+		flag = 'c';
+		break;
+	}
+
 	return scnprintf(buf, size, "%u,%llu,%llu,%c%s;",
 			 (msg->facility << 3) | msg->level, seq, ts_usec,
-			 msg->flags & LOG_CONT ? 'c' : '-', caller);
+			 flag, caller);
 }
 
 static ssize_t msg_print_ext_body(char *buf, size_t size,
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] printk: Support message continuation from /dev/kmsg
       [not found] <20191119170632.52119-1-james.byrne@origamienergy.com>
  2019-11-19 17:08 ` [PATCH 1/2] printk: Make continuation flags from /dev/kmsg useful again James Byrne
@ 2019-11-19 17:08 ` James Byrne
  1 sibling, 0 replies; 2+ messages in thread
From: James Byrne @ 2019-11-19 17:08 UTC (permalink / raw)
  To: LKML; +Cc: James Byrne, Petr Mladek, Sergey Senozhatsky, Steven Rostedt

Since commit 4bcc595ccd80 ("printk: reinstate KERN_CONT for printing
continuation lines") the behaviour of messages written into /dev/kmsg
from user space has changed. Previously if you wrote a message that
did not end with a newline, followed by one ending with a newline, the
second message was treated as a continuation of the first. This is no
longer the case since for a message to be treated as a continuation, an
explicit KERN_CONT is required at the start, and this cannot be used in
messages written via /dev/kmsg.

This commit allows bit 11 of the facility/level number to be used to set
the continuation flag, so you can write two messages that you want to be
joined into /dev/kmsg like this:

<13>This is a continu
<2061>ation message.\n

Signed-off-by: James Byrne <james.byrne@origamienergy.com>
---

 Documentation/ABI/testing/dev-kmsg | 6 +++++-
 kernel/printk/printk.c             | 6 ++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/testing/dev-kmsg b/Documentation/ABI/testing/dev-kmsg
index 6326deeaf5e3..793cf22595fe 100644
--- a/Documentation/ABI/testing/dev-kmsg
+++ b/Documentation/ABI/testing/dev-kmsg
@@ -12,7 +12,8 @@ Description:	The /dev/kmsg character device node provides userspace access
 		The logged line can be prefixed with a <N> syslog prefix, which
 		carries the syslog priority and facility. The single decimal
 		prefix number is composed of the 3 lowest bits being the syslog
-		priority and the next 8 bits the syslog facility number.
+		priority, the next 8 bits the syslog facility number and the
+		next bit a continuation flag.
 
 		If no prefix is given, the priority number is the default kernel
 		log priority and the facility number is set to LOG_USER (1). It
@@ -20,6 +21,9 @@ Description:	The /dev/kmsg character device node provides userspace access
 		facility number LOG_KERN (0), to make sure that the origin of
 		the messages can always be reliably determined.
 
+		Setting bit 11 of the prefix number, the continuation flag, is
+		equivalent to prefixing a kernel printk message with KERN_CONT.
+
 		Accessing the buffer:
 		Every read() from the opened device node receives one record
 		of the kernel's printk buffer.
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index a3db7f5e56d9..d04353076e92 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -451,6 +451,7 @@ static u32 clear_idx;
 
 #define LOG_LEVEL(v)		((v) & 0x07)
 #define LOG_FACILITY(v)		((v) >> 3 & 0xff)
+#define LOG_CONT_USER(v)	((v) & 0x800)
 
 /* record buffer */
 #define LOG_ALIGN __alignof__(struct printk_log)
@@ -869,6 +870,8 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
 			level = LOG_LEVEL(u);
 			if (LOG_FACILITY(u) != 0)
 				facility = LOG_FACILITY(u);
+			if (LOG_CONT_USER(u) != 0)
+				facility |= 0x100;
 			endp++;
 			len -= endp - line;
 			line = endp;
@@ -1954,6 +1957,9 @@ int vprintk_store(int facility, int level,
 			text_len -= 2;
 			text += 2;
 		}
+	} else if (facility & 0x100) {
+		lflags |= LOG_CONT;
+		facility &= 0xff;
 	}
 
 	if (level == LOGLEVEL_DEFAULT)
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-11-19 17:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20191119170632.52119-1-james.byrne@origamienergy.com>
2019-11-19 17:08 ` [PATCH 1/2] printk: Make continuation flags from /dev/kmsg useful again James Byrne
2019-11-19 17:08 ` [PATCH 2/2] printk: Support message continuation from /dev/kmsg James Byrne

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).