162306a36Sopenharmony_ciWhat: /dev/kmsg 262306a36Sopenharmony_ciDate: Mai 2012 362306a36Sopenharmony_ciKernelVersion: 3.5 462306a36Sopenharmony_ciContact: Kay Sievers <kay@vrfy.org> 562306a36Sopenharmony_ciDescription: The /dev/kmsg character device node provides userspace access 662306a36Sopenharmony_ci to the kernel's printk buffer. 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci Injecting messages: 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci Every write() to the opened device node places a log entry in 1162306a36Sopenharmony_ci the kernel's printk buffer. 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci The logged line can be prefixed with a <N> syslog prefix, which 1462306a36Sopenharmony_ci carries the syslog priority and facility. The single decimal 1562306a36Sopenharmony_ci prefix number is composed of the 3 lowest bits being the syslog 1662306a36Sopenharmony_ci priority and the next 8 bits the syslog facility number. 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci If no prefix is given, the priority number is the default kernel 1962306a36Sopenharmony_ci log priority and the facility number is set to LOG_USER (1). It 2062306a36Sopenharmony_ci is not possible to inject messages from userspace with the 2162306a36Sopenharmony_ci facility number LOG_KERN (0), to make sure that the origin of 2262306a36Sopenharmony_ci the messages can always be reliably determined. 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci Accessing the buffer: 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci Every read() from the opened device node receives one record 2762306a36Sopenharmony_ci of the kernel's printk buffer. 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci The first read() directly following an open() always returns 3062306a36Sopenharmony_ci first message in the buffer; there is no kernel-internal 3162306a36Sopenharmony_ci persistent state; many readers can concurrently open the device 3262306a36Sopenharmony_ci and read from it, without affecting other readers. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci Every read() will receive the next available record. If no more 3562306a36Sopenharmony_ci records are available read() will block, or if O_NONBLOCK is 3662306a36Sopenharmony_ci used -EAGAIN returned. 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci Messages in the record ring buffer get overwritten as whole, 3962306a36Sopenharmony_ci there are never partial messages received by read(). 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci In case messages get overwritten in the circular buffer while 4262306a36Sopenharmony_ci the device is kept open, the next read() will return -EPIPE, 4362306a36Sopenharmony_ci and the seek position be updated to the next available record. 4462306a36Sopenharmony_ci Subsequent reads() will return available records again. 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci Unlike the classic syslog() interface, the 64 bit record 4762306a36Sopenharmony_ci sequence numbers allow to calculate the amount of lost 4862306a36Sopenharmony_ci messages, in case the buffer gets overwritten. And they allow 4962306a36Sopenharmony_ci to reconnect to the buffer and reconstruct the read position 5062306a36Sopenharmony_ci if needed, without limiting the interface to a single reader. 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci The device supports seek with the following parameters: 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci SEEK_SET, 0 5562306a36Sopenharmony_ci seek to the first entry in the buffer 5662306a36Sopenharmony_ci SEEK_END, 0 5762306a36Sopenharmony_ci seek after the last entry in the buffer 5862306a36Sopenharmony_ci SEEK_DATA, 0 5962306a36Sopenharmony_ci seek after the last record available at the time 6062306a36Sopenharmony_ci the last SYSLOG_ACTION_CLEAR was issued. 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci Other seek operations or offsets are not supported because of 6362306a36Sopenharmony_ci the special behavior this device has. The device allows to read 6462306a36Sopenharmony_ci or write only whole variable length messages (records) that are 6562306a36Sopenharmony_ci stored in a ring buffer. 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci Because of the non-standard behavior also the error values are 6862306a36Sopenharmony_ci non-standard. -ESPIPE is returned for non-zero offset. -EINVAL 6962306a36Sopenharmony_ci is returned for other operations, e.g. SEEK_CUR. This behavior 7062306a36Sopenharmony_ci and values are historical and could not be modified without the 7162306a36Sopenharmony_ci risk of breaking userspace. 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci The output format consists of a prefix carrying the syslog 7462306a36Sopenharmony_ci prefix including priority and facility, the 64 bit message 7562306a36Sopenharmony_ci sequence number and the monotonic timestamp in microseconds, 7662306a36Sopenharmony_ci and a flag field. All fields are separated by a ','. 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci Future extensions might add more comma separated values before 7962306a36Sopenharmony_ci the terminating ';'. Unknown fields and values should be 8062306a36Sopenharmony_ci gracefully ignored. 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci The human readable text string starts directly after the ';' 8362306a36Sopenharmony_ci and is terminated by a '\n'. Untrusted values derived from 8462306a36Sopenharmony_ci hardware or other facilities are printed, therefore 8562306a36Sopenharmony_ci all non-printable characters and '\' itself in the log message 8662306a36Sopenharmony_ci are escaped by "\x00" C-style hex encoding. 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci A line starting with ' ', is a continuation line, adding 8962306a36Sopenharmony_ci key/value pairs to the log message, which provide the machine 9062306a36Sopenharmony_ci readable context of the message, for reliable processing in 9162306a36Sopenharmony_ci userspace. 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci Example:: 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci 7,160,424069,-;pci_root PNP0A03:00: host bridge window [io 0x0000-0x0cf7] (ignored) 9662306a36Sopenharmony_ci SUBSYSTEM=acpi 9762306a36Sopenharmony_ci DEVICE=+acpi:PNP0A03:00 9862306a36Sopenharmony_ci 6,339,5140900,-;NET: Registered protocol family 10 9962306a36Sopenharmony_ci 30,340,5690716,-;udevd[80]: starting version 181 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci The DEVICE= key uniquely identifies devices the following way: 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci ============ ================= 10462306a36Sopenharmony_ci b12:8 block dev_t 10562306a36Sopenharmony_ci c127:3 char dev_t 10662306a36Sopenharmony_ci n8 netdev ifindex 10762306a36Sopenharmony_ci +sound:card0 subsystem:devname 10862306a36Sopenharmony_ci ============ ================= 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci The flags field carries '-' by default. A 'c' indicates a 11162306a36Sopenharmony_ci fragment of a line. Note, that these hints about continuation 11262306a36Sopenharmony_ci lines are not necessarily correct, and the stream could be 11362306a36Sopenharmony_ci interleaved with unrelated messages, but merging the lines in 11462306a36Sopenharmony_ci the output usually produces better human readable results. A 11562306a36Sopenharmony_ci similar logic is used internally when messages are printed to 11662306a36Sopenharmony_ci the console, /proc/kmsg or the syslog() syscall. 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci By default, kernel tries to avoid fragments by concatenating 11962306a36Sopenharmony_ci when it can and fragments are rare; however, when extended 12062306a36Sopenharmony_ci console support is enabled, the in-kernel concatenation is 12162306a36Sopenharmony_ci disabled and /dev/kmsg output will contain more fragments. If 12262306a36Sopenharmony_ci the log consumer performs concatenation, the end result 12362306a36Sopenharmony_ci should be the same. In the future, the in-kernel concatenation 12462306a36Sopenharmony_ci may be removed entirely and /dev/kmsg users are recommended to 12562306a36Sopenharmony_ci implement fragment handling. 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ciUsers: dmesg(1), userspace kernel log consumers 128