blob: 2fc92a13f9f8f46a1f51a6882cddbd0c1125e184 [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 Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/io.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static int kmsg_open(struct inode * inode, struct file * file)
21{
Kees Cook637241a2013-06-12 14:04:39 -070022 return do_syslog(SYSLOG_ACTION_OPEN, NULL, 0, SYSLOG_FROM_PROC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023}
24
25static int kmsg_release(struct inode * inode, struct file * file)
26{
Kees Cook637241a2013-06-12 14:04:39 -070027 (void) do_syslog(SYSLOG_ACTION_CLOSE, NULL, 0, SYSLOG_FROM_PROC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return 0;
29}
30
31static ssize_t kmsg_read(struct file *file, char __user *buf,
32 size_t count, loff_t *ppos)
33{
Kees Cook00234592010-02-03 15:36:43 -080034 if ((file->f_flags & O_NONBLOCK) &&
Kees Cook637241a2013-06-12 14:04:39 -070035 !do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return -EAGAIN;
Kees Cook637241a2013-06-12 14:04:39 -070037 return do_syslog(SYSLOG_ACTION_READ, buf, count, SYSLOG_FROM_PROC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Al Viro076ccb72017-07-03 01:02:18 -040040static __poll_t kmsg_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 poll_wait(file, &log_wait, wait);
Kees Cook637241a2013-06-12 14:04:39 -070043 if (do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
Linus Torvaldsa9a08842018-02-11 14:34:03 -080044 return EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return 0;
46}
47
48
Alexey Dobriyan97a32532020-02-03 17:37:17 -080049static const struct proc_ops kmsg_proc_ops = {
Alexey Dobriyand919b332020-04-06 20:09:01 -070050 .proc_flags = PROC_ENTRY_PERMANENT,
Alexey Dobriyan97a32532020-02-03 17:37:17 -080051 .proc_read = kmsg_read,
52 .proc_poll = kmsg_poll,
53 .proc_open = kmsg_open,
54 .proc_release = kmsg_release,
55 .proc_lseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
Alexey Dobriyanae048112008-10-04 14:39:12 +040057
58static int __init proc_kmsg_init(void)
59{
Alexey Dobriyan97a32532020-02-03 17:37:17 -080060 proc_create("kmsg", S_IRUSR, NULL, &kmsg_proc_ops);
Alexey Dobriyanae048112008-10-04 14:39:12 +040061 return 0;
62}
Paul Gortmakerabaf3782014-01-23 15:55:45 -080063fs_initcall(proc_kmsg_init);