13b552b92SKay SieversWhat: /dev/kmsg 23b552b92SKay SieversDate: Mai 2012 33b552b92SKay SieversKernelVersion: 3.5 43b552b92SKay SieversContact: Kay Sievers <kay@vrfy.org> 53b552b92SKay SieversDescription: The /dev/kmsg character device node provides userspace access 63b552b92SKay Sievers to the kernel's printk buffer. 73b552b92SKay Sievers 83b552b92SKay Sievers Injecting messages: 934433332SMauro Carvalho Chehab 103b552b92SKay Sievers Every write() to the opened device node places a log entry in 113b552b92SKay Sievers the kernel's printk buffer. 123b552b92SKay Sievers 133b552b92SKay Sievers The logged line can be prefixed with a <N> syslog prefix, which 143b552b92SKay Sievers carries the syslog priority and facility. The single decimal 153b552b92SKay Sievers prefix number is composed of the 3 lowest bits being the syslog 16085a3a8fSJames Byrne priority and the next 8 bits the syslog facility number. 173b552b92SKay Sievers 183b552b92SKay Sievers If no prefix is given, the priority number is the default kernel 193b552b92SKay Sievers log priority and the facility number is set to LOG_USER (1). It 203b552b92SKay Sievers is not possible to inject messages from userspace with the 213b552b92SKay Sievers facility number LOG_KERN (0), to make sure that the origin of 223b552b92SKay Sievers the messages can always be reliably determined. 233b552b92SKay Sievers 243b552b92SKay Sievers Accessing the buffer: 2534433332SMauro Carvalho Chehab 263b552b92SKay Sievers Every read() from the opened device node receives one record 273b552b92SKay Sievers of the kernel's printk buffer. 283b552b92SKay Sievers 293b552b92SKay Sievers The first read() directly following an open() always returns 303b552b92SKay Sievers first message in the buffer; there is no kernel-internal 313b552b92SKay Sievers persistent state; many readers can concurrently open the device 323b552b92SKay Sievers and read from it, without affecting other readers. 333b552b92SKay Sievers 343b552b92SKay Sievers Every read() will receive the next available record. If no more 353b552b92SKay Sievers records are available read() will block, or if O_NONBLOCK is 363b552b92SKay Sievers used -EAGAIN returned. 373b552b92SKay Sievers 383b552b92SKay Sievers Messages in the record ring buffer get overwritten as whole, 393b552b92SKay Sievers there are never partial messages received by read(). 403b552b92SKay Sievers 413b552b92SKay Sievers In case messages get overwritten in the circular buffer while 423b552b92SKay Sievers the device is kept open, the next read() will return -EPIPE, 433b552b92SKay Sievers and the seek position be updated to the next available record. 443b552b92SKay Sievers Subsequent reads() will return available records again. 453b552b92SKay Sievers 463b552b92SKay Sievers Unlike the classic syslog() interface, the 64 bit record 473b552b92SKay Sievers sequence numbers allow to calculate the amount of lost 483b552b92SKay Sievers messages, in case the buffer gets overwritten. And they allow 493b552b92SKay Sievers to reconnect to the buffer and reconstruct the read position 503b552b92SKay Sievers if needed, without limiting the interface to a single reader. 513b552b92SKay Sievers 523b552b92SKay Sievers The device supports seek with the following parameters: 5334433332SMauro Carvalho Chehab 543b552b92SKay Sievers SEEK_SET, 0 553b552b92SKay Sievers seek to the first entry in the buffer 563b552b92SKay Sievers SEEK_END, 0 573b552b92SKay Sievers seek after the last entry in the buffer 583b552b92SKay Sievers SEEK_DATA, 0 593b552b92SKay Sievers seek after the last record available at the time 603b552b92SKay Sievers the last SYSLOG_ACTION_CLEAR was issued. 613b552b92SKay Sievers 62bc885f1aSBruno Meneguele Other seek operations or offsets are not supported because of 63bc885f1aSBruno Meneguele the special behavior this device has. The device allows to read 64bc885f1aSBruno Meneguele or write only whole variable length messages (records) that are 65bc885f1aSBruno Meneguele stored in a ring buffer. 66bc885f1aSBruno Meneguele 67bc885f1aSBruno Meneguele Because of the non-standard behavior also the error values are 68bc885f1aSBruno Meneguele non-standard. -ESPIPE is returned for non-zero offset. -EINVAL 69bc885f1aSBruno Meneguele is returned for other operations, e.g. SEEK_CUR. This behavior 70bc885f1aSBruno Meneguele and values are historical and could not be modified without the 71bc885f1aSBruno Meneguele risk of breaking userspace. 72bc885f1aSBruno Meneguele 733b552b92SKay Sievers The output format consists of a prefix carrying the syslog 743b552b92SKay Sievers prefix including priority and facility, the 64 bit message 75d39f3d77SKay Sievers sequence number and the monotonic timestamp in microseconds, 76d39f3d77SKay Sievers and a flag field. All fields are separated by a ','. 77d39f3d77SKay Sievers 78d39f3d77SKay Sievers Future extensions might add more comma separated values before 79d39f3d77SKay Sievers the terminating ';'. Unknown fields and values should be 80d39f3d77SKay Sievers gracefully ignored. 813b552b92SKay Sievers 823b552b92SKay Sievers The human readable text string starts directly after the ';' 833b552b92SKay Sievers and is terminated by a '\n'. Untrusted values derived from 843b552b92SKay Sievers hardware or other facilities are printed, therefore 85d39f3d77SKay Sievers all non-printable characters and '\' itself in the log message 86d39f3d77SKay Sievers are escaped by "\x00" C-style hex encoding. 873b552b92SKay Sievers 883b552b92SKay Sievers A line starting with ' ', is a continuation line, adding 893b552b92SKay Sievers key/value pairs to the log message, which provide the machine 903b552b92SKay Sievers readable context of the message, for reliable processing in 913b552b92SKay Sievers userspace. 923b552b92SKay Sievers 9334433332SMauro Carvalho Chehab Example:: 9434433332SMauro Carvalho Chehab 95d39f3d77SKay Sievers 7,160,424069,-;pci_root PNP0A03:00: host bridge window [io 0x0000-0x0cf7] (ignored) 963b552b92SKay Sievers SUBSYSTEM=acpi 973b552b92SKay Sievers DEVICE=+acpi:PNP0A03:00 98d39f3d77SKay Sievers 6,339,5140900,-;NET: Registered protocol family 10 99d39f3d77SKay Sievers 30,340,5690716,-;udevd[80]: starting version 181 1003b552b92SKay Sievers 1013b552b92SKay Sievers The DEVICE= key uniquely identifies devices the following way: 10234433332SMauro Carvalho Chehab 10334433332SMauro Carvalho Chehab ============ ================= 10434433332SMauro Carvalho Chehab b12:8 block dev_t 10534433332SMauro Carvalho Chehab c127:3 char dev_t 10634433332SMauro Carvalho Chehab n8 netdev ifindex 10734433332SMauro Carvalho Chehab +sound:card0 subsystem:devname 10834433332SMauro Carvalho Chehab ============ ================= 1093b552b92SKay Sievers 110d39f3d77SKay Sievers The flags field carries '-' by default. A 'c' indicates a 111085a3a8fSJames Byrne fragment of a line. Note, that these hints about continuation 112085a3a8fSJames Byrne lines are not necessarily correct, and the stream could be 113085a3a8fSJames Byrne interleaved with unrelated messages, but merging the lines in 114085a3a8fSJames Byrne the output usually produces better human readable results. A 115085a3a8fSJames Byrne similar logic is used internally when messages are printed to 116085a3a8fSJames Byrne the console, /proc/kmsg or the syslog() syscall. 117d39f3d77SKay Sievers 1186fe29354STejun Heo By default, kernel tries to avoid fragments by concatenating 1196fe29354STejun Heo when it can and fragments are rare; however, when extended 1206fe29354STejun Heo console support is enabled, the in-kernel concatenation is 1216fe29354STejun Heo disabled and /dev/kmsg output will contain more fragments. If 1226fe29354STejun Heo the log consumer performs concatenation, the end result 1236fe29354STejun Heo should be the same. In the future, the in-kernel concatenation 1246fe29354STejun Heo may be removed entirely and /dev/kmsg users are recommended to 1256fe29354STejun Heo implement fragment handling. 1266fe29354STejun Heo 1273b552b92SKay SieversUsers: dmesg(1), userspace kernel log consumers 128