kmsg.c 1.5 KB

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