Skip to content

Commit 8ece3b3

Browse files
bmenegpmladek
authored andcommitted
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/[email protected] Cc: [email protected] Cc: [email protected], Cc: [email protected] Signed-off-by: Bruno Meneguele <[email protected]> Acked-by: Sergey Senozhatsky <[email protected]> Signed-off-by: Petr Mladek <[email protected]>
1 parent 325606a commit 8ece3b3

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Documentation/ABI/testing/dev-kmsg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ Description: The /dev/kmsg character device node provides userspace access
5656
seek after the last record available at the time
5757
the last SYSLOG_ACTION_CLEAR was issued.
5858

59+
Due to the record nature of this interface with a "read all"
60+
behavior and the specific positions each seek operation sets,
61+
SEEK_CUR is not supported, returning -ESPIPE (invalid seek) to
62+
errno whenever requested.
63+
5964
The output format consists of a prefix carrying the syslog
6065
prefix including priority and facility, the 64 bit message
6166
sequence number and the monotonic timestamp in microseconds,

kernel/printk/printk.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,16 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
974974
user->idx = log_next_idx;
975975
user->seq = log_next_seq;
976976
break;
977+
case SEEK_CUR:
978+
/*
979+
* It isn't supported due to the record nature of this
980+
* interface: _SET _DATA and _END point to very specific
981+
* record positions, while _CUR would be more useful in case
982+
* of a byte-based log. Because of that, return the default
983+
* errno value for invalid seek operation.
984+
*/
985+
ret = -ESPIPE;
986+
break;
977987
default:
978988
ret = -EINVAL;
979989
}

0 commit comments

Comments
 (0)