blob: b38ad552887fb5e692787ace19861855b1e4ed0c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/proc/kmsg.c
4 *
5 * Copyright (C) 1992 by Linus Torvalds
6 *
7 */
8
9#include <linux/types.h>
10#include <linux/errno.h>
11#include <linux/time.h>
12#include <linux/kernel.h>
13#include <linux/poll.h>
Alexey Dobriyanae048112008-10-04 14:39:12 +040014#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
Kees Cook00234592010-02-03 15:36:43 -080016#include <linux/syslog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080018#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/io.h>
20
21extern wait_queue_head_t log_wait;
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static int kmsg_open(struct inode * inode, struct file * file)
24{
Kees Cook637241a2013-06-12 14:04:39 -070025 return do_syslog(SYSLOG_ACTION_OPEN, NULL, 0, SYSLOG_FROM_PROC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026}
27
28static int kmsg_release(struct inode * inode, struct file * file)
29{
Kees Cook637241a2013-06-12 14:04:39 -070030 (void) do_syslog(SYSLOG_ACTION_CLOSE, NULL, 0, SYSLOG_FROM_PROC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return 0;
32}
33
34static ssize_t kmsg_read(struct file *file, char __user *buf,
35 size_t count, loff_t *ppos)
36{
Kees Cook00234592010-02-03 15:36:43 -080037 if ((file->f_flags & O_NONBLOCK) &&
Kees Cook637241a2013-06-12 14:04:39 -070038 !do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return -EAGAIN;
Kees Cook637241a2013-06-12 14:04:39 -070040 return do_syslog(SYSLOG_ACTION_READ, buf, count, SYSLOG_FROM_PROC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Al Viro076ccb72017-07-03 01:02:18 -040043static __poll_t kmsg_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 poll_wait(file, &log_wait, wait);
Kees Cook637241a2013-06-12 14:04:39 -070046 if (do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
Linus Torvaldsa9a08842018-02-11 14:34:03 -080047 return EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return 0;
49}
50
51
Alexey Dobriyan97a32532020-02-03 17:37:17 -080052static const struct proc_ops kmsg_proc_ops = {
Alexey Dobriyand919b332020-04-06 20:09:01 -070053 .proc_flags = PROC_ENTRY_PERMANENT,
Alexey Dobriyan97a32532020-02-03 17:37:17 -080054 .proc_read = kmsg_read,
55 .proc_poll = kmsg_poll,
56 .proc_open = kmsg_open,
57 .proc_release = kmsg_release,
58 .proc_lseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
Alexey Dobriyanae048112008-10-04 14:39:12 +040060
61static int __init proc_kmsg_init(void)
62{
Alexey Dobriyan97a32532020-02-03 17:37:17 -080063 proc_create("kmsg", S_IRUSR, NULL, &kmsg_proc_ops);
Alexey Dobriyanae048112008-10-04 14:39:12 +040064 return 0;
65}
Paul Gortmakerabaf3782014-01-23 15:55:45 -080066fs_initcall(proc_kmsg_init);