2012-05-08 23:50:50 +07:00
|
|
|
What: /dev/kmsg
|
|
|
|
Date: Mai 2012
|
|
|
|
KernelVersion: 3.5
|
|
|
|
Contact: Kay Sievers <kay@vrfy.org>
|
|
|
|
Description: The /dev/kmsg character device node provides userspace access
|
|
|
|
to the kernel's printk buffer.
|
|
|
|
|
|
|
|
Injecting messages:
|
|
|
|
Every write() to the opened device node places a log entry in
|
|
|
|
the kernel's printk buffer.
|
|
|
|
|
|
|
|
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
|
2019-09-02 18:18:16 +07:00
|
|
|
priority and the next 8 bits the syslog facility number.
|
2012-05-08 23:50:50 +07:00
|
|
|
|
|
|
|
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
|
|
|
|
is not possible to inject messages from userspace with the
|
|
|
|
facility number LOG_KERN (0), to make sure that the origin of
|
|
|
|
the messages can always be reliably determined.
|
|
|
|
|
|
|
|
Accessing the buffer:
|
|
|
|
Every read() from the opened device node receives one record
|
|
|
|
of the kernel's printk buffer.
|
|
|
|
|
|
|
|
The first read() directly following an open() always returns
|
|
|
|
first message in the buffer; there is no kernel-internal
|
|
|
|
persistent state; many readers can concurrently open the device
|
|
|
|
and read from it, without affecting other readers.
|
|
|
|
|
|
|
|
Every read() will receive the next available record. If no more
|
|
|
|
records are available read() will block, or if O_NONBLOCK is
|
|
|
|
used -EAGAIN returned.
|
|
|
|
|
|
|
|
Messages in the record ring buffer get overwritten as whole,
|
|
|
|
there are never partial messages received by read().
|
|
|
|
|
|
|
|
In case messages get overwritten in the circular buffer while
|
|
|
|
the device is kept open, the next read() will return -EPIPE,
|
|
|
|
and the seek position be updated to the next available record.
|
|
|
|
Subsequent reads() will return available records again.
|
|
|
|
|
|
|
|
Unlike the classic syslog() interface, the 64 bit record
|
|
|
|
sequence numbers allow to calculate the amount of lost
|
|
|
|
messages, in case the buffer gets overwritten. And they allow
|
|
|
|
to reconnect to the buffer and reconstruct the read position
|
|
|
|
if needed, without limiting the interface to a single reader.
|
|
|
|
|
|
|
|
The device supports seek with the following parameters:
|
|
|
|
SEEK_SET, 0
|
|
|
|
seek to the first entry in the buffer
|
|
|
|
SEEK_END, 0
|
|
|
|
seek after the last entry in the buffer
|
|
|
|
SEEK_DATA, 0
|
|
|
|
seek after the last record available at the time
|
|
|
|
the last SYSLOG_ACTION_CLEAR was issued.
|
|
|
|
|
kernel/printk: add kmsg SEEK_CUR handling
Userspace libraries, e.g. glibc's dprintf(), perform a SEEK_CUR operation
over any file descriptor requested to make sure the current position isn't
pointing to junk due to previous manipulation of that same fd. And whenever
that fd doesn't have support for such operation, the userspace code expects
-ESPIPE to be returned.
However, when the fd in question references the /dev/kmsg interface, the
current kernel code state returns -EINVAL instead, causing an unexpected
behavior in userspace: in the case of glibc, when -ESPIPE is returned it
gets ignored and the call completes successfully, while returning -EINVAL
forces dprintf to fail without performing any action over that fd:
if (_IO_SEEKOFF (fp, (off64_t)0, _IO_seek_cur, _IOS_INPUT|_IOS_OUTPUT) ==
_IO_pos_BAD && errno != ESPIPE)
return NULL;
With this patch we make sure to return the correct value when SEEK_CUR is
requested over kmsg and also add some kernel doc information to formalize
this behavior.
Link: https://lore.kernel.org/r/20200317103344.574277-1-bmeneg@redhat.com
Cc: linux-kernel@vger.kernel.org
Cc: rostedt@goodmis.org,
Cc: David.Laight@ACULAB.COM
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
Acked-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: Petr Mladek <pmladek@suse.com>
2020-03-17 17:33:44 +07:00
|
|
|
Due to the record nature of this interface with a "read all"
|
|
|
|
behavior and the specific positions each seek operation sets,
|
|
|
|
SEEK_CUR is not supported, returning -ESPIPE (invalid seek) to
|
|
|
|
errno whenever requested.
|
|
|
|
|
2012-05-08 23:50:50 +07:00
|
|
|
The output format consists of a prefix carrying the syslog
|
|
|
|
prefix including priority and facility, the 64 bit message
|
kmsg - export "continuation record" flag to /dev/kmsg
In some cases we are forced to store individual records for a continuation
line print.
Export a flag to allow the external re-construction of the line. The flag
allows us to apply a similar logic externally which is used internally when
the console, /proc/kmsg or the syslog() output is printed.
$ cat /dev/kmsg
4,165,0,-;Free swap = 0kB
4,166,0,-;Total swap = 0kB
6,167,0,c;[
4,168,0,+;0
4,169,0,+;1
4,170,0,+;2
4,171,0,+;3
4,172,0,+;]
6,173,0,-;[0 1 2 3 ]
6,174,0,-;Console: colour VGA+ 80x25
6,175,0,-;console [tty0] enabled
Signed-off-by: Kay Sievers <kay@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-07-17 08:35:30 +07:00
|
|
|
sequence number and the monotonic timestamp in microseconds,
|
|
|
|
and a flag field. All fields are separated by a ','.
|
|
|
|
|
|
|
|
Future extensions might add more comma separated values before
|
|
|
|
the terminating ';'. Unknown fields and values should be
|
|
|
|
gracefully ignored.
|
2012-05-08 23:50:50 +07:00
|
|
|
|
|
|
|
The human readable text string starts directly after the ';'
|
|
|
|
and is terminated by a '\n'. Untrusted values derived from
|
|
|
|
hardware or other facilities are printed, therefore
|
kmsg - export "continuation record" flag to /dev/kmsg
In some cases we are forced to store individual records for a continuation
line print.
Export a flag to allow the external re-construction of the line. The flag
allows us to apply a similar logic externally which is used internally when
the console, /proc/kmsg or the syslog() output is printed.
$ cat /dev/kmsg
4,165,0,-;Free swap = 0kB
4,166,0,-;Total swap = 0kB
6,167,0,c;[
4,168,0,+;0
4,169,0,+;1
4,170,0,+;2
4,171,0,+;3
4,172,0,+;]
6,173,0,-;[0 1 2 3 ]
6,174,0,-;Console: colour VGA+ 80x25
6,175,0,-;console [tty0] enabled
Signed-off-by: Kay Sievers <kay@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-07-17 08:35:30 +07:00
|
|
|
all non-printable characters and '\' itself in the log message
|
|
|
|
are escaped by "\x00" C-style hex encoding.
|
2012-05-08 23:50:50 +07:00
|
|
|
|
|
|
|
A line starting with ' ', is a continuation line, adding
|
|
|
|
key/value pairs to the log message, which provide the machine
|
|
|
|
readable context of the message, for reliable processing in
|
|
|
|
userspace.
|
|
|
|
|
|
|
|
Example:
|
kmsg - export "continuation record" flag to /dev/kmsg
In some cases we are forced to store individual records for a continuation
line print.
Export a flag to allow the external re-construction of the line. The flag
allows us to apply a similar logic externally which is used internally when
the console, /proc/kmsg or the syslog() output is printed.
$ cat /dev/kmsg
4,165,0,-;Free swap = 0kB
4,166,0,-;Total swap = 0kB
6,167,0,c;[
4,168,0,+;0
4,169,0,+;1
4,170,0,+;2
4,171,0,+;3
4,172,0,+;]
6,173,0,-;[0 1 2 3 ]
6,174,0,-;Console: colour VGA+ 80x25
6,175,0,-;console [tty0] enabled
Signed-off-by: Kay Sievers <kay@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-07-17 08:35:30 +07:00
|
|
|
7,160,424069,-;pci_root PNP0A03:00: host bridge window [io 0x0000-0x0cf7] (ignored)
|
2012-05-08 23:50:50 +07:00
|
|
|
SUBSYSTEM=acpi
|
|
|
|
DEVICE=+acpi:PNP0A03:00
|
kmsg - export "continuation record" flag to /dev/kmsg
In some cases we are forced to store individual records for a continuation
line print.
Export a flag to allow the external re-construction of the line. The flag
allows us to apply a similar logic externally which is used internally when
the console, /proc/kmsg or the syslog() output is printed.
$ cat /dev/kmsg
4,165,0,-;Free swap = 0kB
4,166,0,-;Total swap = 0kB
6,167,0,c;[
4,168,0,+;0
4,169,0,+;1
4,170,0,+;2
4,171,0,+;3
4,172,0,+;]
6,173,0,-;[0 1 2 3 ]
6,174,0,-;Console: colour VGA+ 80x25
6,175,0,-;console [tty0] enabled
Signed-off-by: Kay Sievers <kay@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-07-17 08:35:30 +07:00
|
|
|
6,339,5140900,-;NET: Registered protocol family 10
|
|
|
|
30,340,5690716,-;udevd[80]: starting version 181
|
2012-05-08 23:50:50 +07:00
|
|
|
|
|
|
|
The DEVICE= key uniquely identifies devices the following way:
|
|
|
|
b12:8 - block dev_t
|
|
|
|
c127:3 - char dev_t
|
|
|
|
n8 - netdev ifindex
|
|
|
|
+sound:card0 - subsystem:devname
|
|
|
|
|
kmsg - export "continuation record" flag to /dev/kmsg
In some cases we are forced to store individual records for a continuation
line print.
Export a flag to allow the external re-construction of the line. The flag
allows us to apply a similar logic externally which is used internally when
the console, /proc/kmsg or the syslog() output is printed.
$ cat /dev/kmsg
4,165,0,-;Free swap = 0kB
4,166,0,-;Total swap = 0kB
6,167,0,c;[
4,168,0,+;0
4,169,0,+;1
4,170,0,+;2
4,171,0,+;3
4,172,0,+;]
6,173,0,-;[0 1 2 3 ]
6,174,0,-;Console: colour VGA+ 80x25
6,175,0,-;console [tty0] enabled
Signed-off-by: Kay Sievers <kay@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-07-17 08:35:30 +07:00
|
|
|
The flags field carries '-' by default. A 'c' indicates a
|
2019-09-02 18:18:16 +07:00
|
|
|
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.
|
kmsg - export "continuation record" flag to /dev/kmsg
In some cases we are forced to store individual records for a continuation
line print.
Export a flag to allow the external re-construction of the line. The flag
allows us to apply a similar logic externally which is used internally when
the console, /proc/kmsg or the syslog() output is printed.
$ cat /dev/kmsg
4,165,0,-;Free swap = 0kB
4,166,0,-;Total swap = 0kB
6,167,0,c;[
4,168,0,+;0
4,169,0,+;1
4,170,0,+;2
4,171,0,+;3
4,172,0,+;]
6,173,0,-;[0 1 2 3 ]
6,174,0,-;Console: colour VGA+ 80x25
6,175,0,-;console [tty0] enabled
Signed-off-by: Kay Sievers <kay@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-07-17 08:35:30 +07:00
|
|
|
|
2015-06-26 05:01:30 +07:00
|
|
|
By default, kernel tries to avoid fragments by concatenating
|
|
|
|
when it can and fragments are rare; however, when extended
|
|
|
|
console support is enabled, the in-kernel concatenation is
|
|
|
|
disabled and /dev/kmsg output will contain more fragments. If
|
|
|
|
the log consumer performs concatenation, the end result
|
|
|
|
should be the same. In the future, the in-kernel concatenation
|
|
|
|
may be removed entirely and /dev/kmsg users are recommended to
|
|
|
|
implement fragment handling.
|
|
|
|
|
2012-05-08 23:50:50 +07:00
|
|
|
Users: dmesg(1), userspace kernel log consumers
|