fops.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#file-operations */
  2. #include <linux/debugfs.h>
  3. #include <linux/errno.h> /* EFAULT */
  4. #include <linux/fs.h> /* file_operations */
  5. #include <linux/kernel.h> /* min */
  6. #include <linux/module.h>
  7. #include <linux/printk.h> /* printk */
  8. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  9. #include <uapi/linux/stat.h> /* S_IRUSR */
  10. static struct dentry *debugfs_file;
  11. static char data[] = {'a', 'b', 'c', 'd'};
  12. static int open(struct inode *inode, struct file *filp)
  13. {
  14. pr_info("open\n");
  15. return 0;
  16. }
  17. /* @param[in,out] off: gives the initial position into the buffer.
  18. * We must increment this by the ammount of bytes read.
  19. * Then when userland reads the same file descriptor again,
  20. * we start from that point instead.
  21. */
  22. static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  23. {
  24. ssize_t ret;
  25. pr_info("read\n");
  26. pr_info("len = %zu\n", len);
  27. pr_info("off = %lld\n", (long long)*off);
  28. if (sizeof(data) <= *off) {
  29. ret = 0;
  30. } else {
  31. ret = min(len, sizeof(data) - (size_t)*off);
  32. if (copy_to_user(buf, data + *off, ret)) {
  33. ret = -EFAULT;
  34. } else {
  35. *off += ret;
  36. }
  37. }
  38. pr_info("buf = %.*s\n", (int)len, buf);
  39. pr_info("ret = %lld\n", (long long)ret);
  40. return ret;
  41. }
  42. /* Similar to read, but with one notable difference:
  43. * we must return ENOSPC if the user tries to write more
  44. * than the size of our buffer. Otherwise, Bash > just
  45. * keeps trying to write to it infinitely.
  46. */
  47. static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  48. {
  49. ssize_t ret;
  50. pr_info("write\n");
  51. pr_info("len = %zu\n", len);
  52. pr_info("off = %lld\n", (long long)*off);
  53. if (sizeof(data) <= *off) {
  54. ret = 0;
  55. } else {
  56. if (sizeof(data) - (size_t)*off < len) {
  57. ret = -ENOSPC;
  58. } else {
  59. if (copy_from_user(data + *off, buf, len)) {
  60. ret = -EFAULT;
  61. } else {
  62. ret = len;
  63. pr_info("buf = %.*s\n", (int)len, data + *off);
  64. *off += ret;
  65. }
  66. }
  67. }
  68. pr_info("ret = %lld\n", (long long)ret);
  69. return ret;
  70. }
  71. /* Called on the last close:
  72. * http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called-release-in-struct-file-operations-in-the-l
  73. */
  74. static int release(struct inode *inode, struct file *filp)
  75. {
  76. pr_info("release\n");
  77. return 0;
  78. }
  79. static loff_t llseek(struct file *filp, loff_t off, int whence)
  80. {
  81. loff_t newpos;
  82. pr_info("llseek\n");
  83. pr_info("off = %lld\n", (long long)off);
  84. pr_info("whence = %lld\n", (long long)whence);
  85. switch(whence) {
  86. case SEEK_SET:
  87. newpos = off;
  88. break;
  89. case SEEK_CUR:
  90. newpos = filp->f_pos + off;
  91. break;
  92. case SEEK_END:
  93. newpos = sizeof(data) + off;
  94. break;
  95. default:
  96. return -EINVAL;
  97. }
  98. if (newpos < 0) return -EINVAL;
  99. filp->f_pos = newpos;
  100. pr_info("newpos = %lld\n", (long long)newpos);
  101. return newpos;
  102. }
  103. static const struct file_operations fops = {
  104. /* Prevents rmmod while fops are running.
  105. * Try removing this for poll, which waits a lot. */
  106. .owner = THIS_MODULE,
  107. .llseek = llseek,
  108. .open = open,
  109. .read = read,
  110. .release = release,
  111. .write = write,
  112. };
  113. static int myinit(void)
  114. {
  115. debugfs_file = debugfs_create_file("lkmc_fops", S_IRUSR | S_IWUSR, NULL, NULL, &fops);
  116. return 0;
  117. }
  118. static void myexit(void)
  119. {
  120. debugfs_remove_recursive(debugfs_file);
  121. }
  122. module_init(myinit)
  123. module_exit(myexit)
  124. MODULE_LICENSE("GPL");