poll.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#poll */
  2. #include <linux/debugfs.h>
  3. #include <linux/delay.h> /* usleep_range */
  4. #include <linux/errno.h> /* EFAULT */
  5. #include <linux/fs.h>
  6. #include <linux/jiffies.h>
  7. #include <linux/kernel.h> /* min */
  8. #include <linux/kthread.h>
  9. #include <linux/module.h>
  10. #include <linux/poll.h>
  11. #include <linux/printk.h> /* printk */
  12. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  13. #include <linux/wait.h> /* wait_queue_head_t, wait_event_interruptible, wake_up_interruptible */
  14. #include <uapi/linux/stat.h> /* S_IRUSR */
  15. static int ret0 = 0;
  16. module_param(ret0, int, S_IRUSR | S_IWUSR);
  17. MODULE_PARM_DESC(i, "if 1, always return 0 from poll");
  18. static char readbuf[1024];
  19. static size_t readbuflen;
  20. static struct dentry *debugfs_file;
  21. static struct task_struct *kthread;
  22. static wait_queue_head_t waitqueue;
  23. static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  24. {
  25. ssize_t ret;
  26. if (copy_to_user(buf, readbuf, readbuflen)) {
  27. ret = -EFAULT;
  28. } else {
  29. ret = readbuflen;
  30. }
  31. /* This is normal pipe behaviour: data gets drained once a reader reads from it. */
  32. /* https://stackoverflow.com/questions/1634580/named-pipes-fifos-on-unix-with-multiple-readers */
  33. readbuflen = 0;
  34. return ret;
  35. }
  36. /* If you return 0 here, then the kernel will sleep until an event
  37. * happens in the queue. and then call this again, because of the call to poll_wait. */
  38. unsigned int poll(struct file *filp, struct poll_table_struct *wait)
  39. {
  40. pr_info("poll\n");
  41. /* This doesn't sleep. It just makes the kernel call poll again if we return 0. */
  42. poll_wait(filp, &waitqueue, wait);
  43. if (readbuflen && !ret0) {
  44. pr_info("return POLLIN\n");
  45. return POLLIN;
  46. } else {
  47. pr_info("return 0\n");
  48. return 0;
  49. }
  50. }
  51. static int lkmc_kthread_func(void *data)
  52. {
  53. while (!kthread_should_stop()) {
  54. readbuflen = snprintf(
  55. readbuf,
  56. sizeof(readbuf),
  57. "%llu",
  58. (unsigned long long)jiffies
  59. );
  60. usleep_range(1000000, 1000001);
  61. pr_info("wake_up\n");
  62. wake_up(&waitqueue);
  63. }
  64. return 0;
  65. }
  66. static const struct file_operations fops = {
  67. .owner = THIS_MODULE,
  68. .read = read,
  69. .poll = poll
  70. };
  71. static int myinit(void)
  72. {
  73. debugfs_file = debugfs_create_file(
  74. "lkmc_poll", S_IRUSR | S_IWUSR, NULL, NULL, &fops);
  75. init_waitqueue_head(&waitqueue);
  76. kthread = kthread_create(lkmc_kthread_func, NULL, "mykthread");
  77. wake_up_process(kthread);
  78. return 0;
  79. }
  80. static void myexit(void)
  81. {
  82. kthread_stop(kthread);
  83. debugfs_remove(debugfs_file);
  84. }
  85. module_init(myinit)
  86. module_exit(myexit)
  87. MODULE_LICENSE("GPL");