1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/proc/kmsg.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1992 by Linus Torvalds
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/errno.h>
111da177e4SLinus Torvalds #include <linux/time.h>
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/poll.h>
14ae048112SAlexey Dobriyan #include <linux/proc_fs.h>
151da177e4SLinus Torvalds #include <linux/fs.h>
1600234592SKees Cook #include <linux/syslog.h>
171da177e4SLinus Torvalds
181da177e4SLinus Torvalds #include <asm/io.h>
191da177e4SLinus Torvalds
kmsg_open(struct inode * inode,struct file * file)201da177e4SLinus Torvalds static int kmsg_open(struct inode * inode, struct file * file)
211da177e4SLinus Torvalds {
22637241a9SKees Cook return do_syslog(SYSLOG_ACTION_OPEN, NULL, 0, SYSLOG_FROM_PROC);
231da177e4SLinus Torvalds }
241da177e4SLinus Torvalds
kmsg_release(struct inode * inode,struct file * file)251da177e4SLinus Torvalds static int kmsg_release(struct inode * inode, struct file * file)
261da177e4SLinus Torvalds {
27637241a9SKees Cook (void) do_syslog(SYSLOG_ACTION_CLOSE, NULL, 0, SYSLOG_FROM_PROC);
281da177e4SLinus Torvalds return 0;
291da177e4SLinus Torvalds }
301da177e4SLinus Torvalds
kmsg_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)311da177e4SLinus Torvalds static ssize_t kmsg_read(struct file *file, char __user *buf,
321da177e4SLinus Torvalds size_t count, loff_t *ppos)
331da177e4SLinus Torvalds {
3400234592SKees Cook if ((file->f_flags & O_NONBLOCK) &&
35637241a9SKees Cook !do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
361da177e4SLinus Torvalds return -EAGAIN;
37637241a9SKees Cook return do_syslog(SYSLOG_ACTION_READ, buf, count, SYSLOG_FROM_PROC);
381da177e4SLinus Torvalds }
391da177e4SLinus Torvalds
kmsg_poll(struct file * file,poll_table * wait)40076ccb76SAl Viro static __poll_t kmsg_poll(struct file *file, poll_table *wait)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds poll_wait(file, &log_wait, wait);
43637241a9SKees Cook if (do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
44a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDNORM;
451da177e4SLinus Torvalds return 0;
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds
481da177e4SLinus Torvalds
4997a32539SAlexey Dobriyan static const struct proc_ops kmsg_proc_ops = {
50*d919b33dSAlexey Dobriyan .proc_flags = PROC_ENTRY_PERMANENT,
5197a32539SAlexey Dobriyan .proc_read = kmsg_read,
5297a32539SAlexey Dobriyan .proc_poll = kmsg_poll,
5397a32539SAlexey Dobriyan .proc_open = kmsg_open,
5497a32539SAlexey Dobriyan .proc_release = kmsg_release,
5597a32539SAlexey Dobriyan .proc_lseek = generic_file_llseek,
561da177e4SLinus Torvalds };
57ae048112SAlexey Dobriyan
proc_kmsg_init(void)58ae048112SAlexey Dobriyan static int __init proc_kmsg_init(void)
59ae048112SAlexey Dobriyan {
6097a32539SAlexey Dobriyan proc_create("kmsg", S_IRUSR, NULL, &kmsg_proc_ops);
61ae048112SAlexey Dobriyan return 0;
62ae048112SAlexey Dobriyan }
63abaf3787SPaul Gortmaker fs_initcall(proc_kmsg_init);
64