xref: /openbmc/linux/fs/proc/kmsg.c (revision e3f12f06)
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 
187c0f6ba6SLinus 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 {
221da177e4SLinus Torvalds 	return do_syslog(SYSLOG_ACTION_OPEN, NULL, 0, SYSLOG_FROM_PROC);
23637241a9SKees Cook }
241da177e4SLinus Torvalds 
kmsg_release(struct inode * inode,struct file * file)251da177e4SLinus Torvalds static int kmsg_release(struct inode * inode, struct file * file)
261da177e4SLinus Torvalds {
271da177e4SLinus Torvalds 	(void) do_syslog(SYSLOG_ACTION_CLOSE, NULL, 0, SYSLOG_FROM_PROC);
28637241a9SKees Cook 	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 {
341da177e4SLinus Torvalds 	if ((file->f_flags & O_NONBLOCK) &&
3500234592SKees Cook 	    !do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
36637241a9SKees Cook 		return -EAGAIN;
371da177e4SLinus Torvalds 	return do_syslog(SYSLOG_ACTION_READ, buf, count, SYSLOG_FROM_PROC);
38637241a9SKees Cook }
391da177e4SLinus Torvalds 
kmsg_poll(struct file * file,poll_table * wait)401da177e4SLinus Torvalds static __poll_t kmsg_poll(struct file *file, poll_table *wait)
41076ccb76SAl Viro {
421da177e4SLinus Torvalds 	poll_wait(file, &log_wait, wait);
431da177e4SLinus Torvalds 	if (do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
44637241a9SKees Cook 		return EPOLLIN | EPOLLRDNORM;
45a9a08845SLinus Torvalds 	return 0;
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds static const struct proc_ops kmsg_proc_ops = {
5097a32539SAlexey Dobriyan 	.proc_flags	= PROC_ENTRY_PERMANENT,
51d919b33dSAlexey 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,
5697a32539SAlexey Dobriyan };
571da177e4SLinus Torvalds 
proc_kmsg_init(void)58ae048112SAlexey Dobriyan static int __init proc_kmsg_init(void)
59ae048112SAlexey Dobriyan {
60ae048112SAlexey Dobriyan 	proc_create("kmsg", S_IRUSR, NULL, &kmsg_proc_ops);
6197a32539SAlexey Dobriyan 	return 0;
62ae048112SAlexey Dobriyan }
63ae048112SAlexey Dobriyan fs_initcall(proc_kmsg_init);
64abaf3787SPaul Gortmaker