kmsg.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <linux/uaccess.h>
  17. #include <asm/io.h>
  18. extern wait_queue_head_t log_wait;
  19. static int kmsg_open(struct inode * inode, struct file * file)
  20. {
  21. return do_syslog(SYSLOG_ACTION_OPEN, NULL, 0, SYSLOG_FROM_PROC);
  22. }
  23. static int kmsg_release(struct inode * inode, struct file * file)
  24. {
  25. (void) do_syslog(SYSLOG_ACTION_CLOSE, NULL, 0, SYSLOG_FROM_PROC);
  26. return 0;
  27. }
  28. static ssize_t kmsg_read(struct file *file, char __user *buf,
  29. size_t count, loff_t *ppos)
  30. {
  31. if ((file->f_flags & O_NONBLOCK) &&
  32. !do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
  33. return -EAGAIN;
  34. return do_syslog(SYSLOG_ACTION_READ, buf, count, SYSLOG_FROM_PROC);
  35. }
  36. static __poll_t kmsg_poll(struct file *file, poll_table *wait)
  37. {
  38. poll_wait(file, &log_wait, wait);
  39. if (do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC))
  40. return EPOLLIN | EPOLLRDNORM;
  41. return 0;
  42. }
  43. static const struct file_operations proc_kmsg_operations = {
  44. .read = kmsg_read,
  45. .poll = kmsg_poll,
  46. .open = kmsg_open,
  47. .release = kmsg_release,
  48. .llseek = generic_file_llseek,
  49. };
  50. static int __init proc_kmsg_init(void)
  51. {
  52. proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
  53. return 0;
  54. }
  55. fs_initcall(proc_kmsg_init);