xref: /OK3568_Linux_fs/kernel/fs/proc/kmsg.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  linux/fs/proc/kmsg.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (C) 1992  by Linus Torvalds
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/types.h>
10*4882a593Smuzhiyun #include <linux/errno.h>
11*4882a593Smuzhiyun #include <linux/time.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/poll.h>
14*4882a593Smuzhiyun #include <linux/proc_fs.h>
15*4882a593Smuzhiyun #include <linux/fs.h>
16*4882a593Smuzhiyun #include <linux/syslog.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/uaccess.h>
19*4882a593Smuzhiyun #include <asm/io.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun extern wait_queue_head_t log_wait;
22*4882a593Smuzhiyun 
kmsg_open(struct inode * inode,struct file * file)23*4882a593Smuzhiyun static int kmsg_open(struct inode * inode, struct file * file)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	return do_syslog(SYSLOG_ACTION_OPEN, NULL, 0, SYSLOG_FROM_PROC);
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun 
kmsg_release(struct inode * inode,struct file * file)28*4882a593Smuzhiyun static int kmsg_release(struct inode * inode, struct file * file)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	(void) do_syslog(SYSLOG_ACTION_CLOSE, NULL, 0, SYSLOG_FROM_PROC);
31*4882a593Smuzhiyun 	return 0;
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
kmsg_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)34*4882a593Smuzhiyun static ssize_t kmsg_read(struct file *file, char __user *buf,
35*4882a593Smuzhiyun 			 size_t count, loff_t *ppos)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	if ((file->f_flags & O_NONBLOCK) &&
38*4882a593Smuzhiyun 	    !do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
39*4882a593Smuzhiyun 		return -EAGAIN;
40*4882a593Smuzhiyun 	return do_syslog(SYSLOG_ACTION_READ, buf, count, SYSLOG_FROM_PROC);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
kmsg_poll(struct file * file,poll_table * wait)43*4882a593Smuzhiyun static __poll_t kmsg_poll(struct file *file, poll_table *wait)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	poll_wait(file, &log_wait, wait);
46*4882a593Smuzhiyun 	if (do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
47*4882a593Smuzhiyun 		return EPOLLIN | EPOLLRDNORM;
48*4882a593Smuzhiyun 	return 0;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun static const struct proc_ops kmsg_proc_ops = {
53*4882a593Smuzhiyun 	.proc_flags	= PROC_ENTRY_PERMANENT,
54*4882a593Smuzhiyun 	.proc_read	= kmsg_read,
55*4882a593Smuzhiyun 	.proc_poll	= kmsg_poll,
56*4882a593Smuzhiyun 	.proc_open	= kmsg_open,
57*4882a593Smuzhiyun 	.proc_release	= kmsg_release,
58*4882a593Smuzhiyun 	.proc_lseek	= generic_file_llseek,
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
proc_kmsg_init(void)61*4882a593Smuzhiyun static int __init proc_kmsg_init(void)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	proc_create("kmsg", S_IRUSR, NULL, &kmsg_proc_ops);
64*4882a593Smuzhiyun 	return 0;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun fs_initcall(proc_kmsg_init);
67