fops.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Basic fops example, with a fixed size static data buffer.
  3. Usage:
  4. /fops.sh
  5. No, there ain't no official docs:
  6. http://stackoverflow.com/questions/15213932/what-are-the-struct-file-operations-arguments
  7. fops define what the kernel will do on filesystem system calls on all of
  8. /dev, /proc, /sys, and consistute the main method of userland communication
  9. in drivers (syscalls being the other one).
  10. Here we use debugfs.
  11. */
  12. #include <linux/debugfs.h>
  13. #include <linux/errno.h> /* EFAULT */
  14. #include <linux/fs.h>
  15. #include <linux/kernel.h> /* min */
  16. #include <linux/module.h>
  17. #include <linux/printk.h> /* printk */
  18. #include <asm/uaccess.h> /* copy_from_user, copy_to_user */
  19. MODULE_LICENSE("GPL");
  20. static struct dentry *dir = 0;
  21. static char data[] = {'a', 'b', 'c', 'd'};
  22. static int fop_open(struct inode *inode, struct file *file)
  23. {
  24. printk(KERN_INFO "open\n");
  25. return 0;
  26. }
  27. /* @param[in,out] off: gives the initial position into the buffer.
  28. * We must increment this by the ammount of bytes read.
  29. * Then when userland reads the same file descriptor again,
  30. * we start from that point instead.
  31. * */
  32. static ssize_t fop_read(struct file *file, char __user *buf, size_t len, loff_t *off)
  33. {
  34. ssize_t ret;
  35. printk(KERN_INFO "read\n");
  36. printk(KERN_INFO "len = %zu\n", len);
  37. printk(KERN_INFO "off = %lld\n", (long long)*off);
  38. if (sizeof(data) <= *off) {
  39. ret = 0;
  40. } else {
  41. ret = min(len, sizeof(data) - (size_t)*off);
  42. if (copy_to_user(buf, data + *off, ret)) {
  43. ret = -EFAULT;
  44. } else {
  45. *off += ret;
  46. }
  47. }
  48. printk(KERN_INFO "buf = %.*s\n", (int)len, buf);
  49. printk(KERN_INFO "ret = %lld\n", (long long)ret);
  50. return ret;
  51. }
  52. /* Similar to read, but with one notable difference:
  53. * we must return ENOSPC if the user tries to write more
  54. * than the size of our buffer. Otherwise, Bash > just
  55. * keeps trying to write to it infinitely. */
  56. static ssize_t fop_write(struct file *file, const char __user *buf, size_t len, loff_t *off)
  57. {
  58. ssize_t ret;
  59. printk(KERN_INFO "write\n");
  60. printk(KERN_INFO "buf = %.*s\n", (int)len, buf);
  61. printk(KERN_INFO "len = %zu\n", len);
  62. printk(KERN_INFO "off = %lld\n", (long long)*off);
  63. if (sizeof(data) <= *off) {
  64. ret = 0;
  65. } else {
  66. if (sizeof(data) - (size_t)*off < len) {
  67. ret = -ENOSPC;
  68. } else {
  69. if (copy_from_user(data + *off, buf, len)) {
  70. ret = -EFAULT;
  71. } else {
  72. ret = len;
  73. *off += ret;
  74. }
  75. }
  76. }
  77. printk(KERN_INFO "ret = %lld\n", (long long)ret);
  78. return ret;
  79. }
  80. /*
  81. Called on the last close:
  82. http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called-release-in-struct-file-operations-in-the-l
  83. */
  84. static int fop_release (struct inode *inode, struct file *file)
  85. {
  86. printk(KERN_INFO "release\n");
  87. return 0;
  88. }
  89. static loff_t fop_llseek(struct file *filp, loff_t off, int whence)
  90. {
  91. loff_t newpos;
  92. printk(KERN_INFO "llseek\n");
  93. printk(KERN_INFO "off = %lld\n", (long long)off);
  94. printk(KERN_INFO "whence = %lld\n", (long long)whence);
  95. switch(whence) {
  96. case SEEK_SET:
  97. newpos = off;
  98. break;
  99. case SEEK_CUR:
  100. newpos = filp->f_pos + off;
  101. break;
  102. case SEEK_END:
  103. newpos = sizeof(data) + off;
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. if (newpos < 0) return -EINVAL;
  109. filp->f_pos = newpos;
  110. printk(KERN_INFO "newpos = %lld\n", (long long)newpos);
  111. return newpos;
  112. }
  113. static const struct file_operations fops = {
  114. .llseek = fop_llseek,
  115. .open = fop_open,
  116. .read = fop_read,
  117. .release = fop_release,
  118. .write = fop_write,
  119. };
  120. int init_module(void)
  121. {
  122. struct dentry *file;
  123. dir = debugfs_create_dir("kernel_module_cheat", 0);
  124. if (!dir) {
  125. printk(KERN_ALERT "debugfs_create_dir failed");
  126. return -1;
  127. }
  128. file = debugfs_create_file("fops", 0666, dir, NULL, &fops);
  129. if (!file) {
  130. printk(KERN_ALERT "debugfs_create_file failed");
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. void cleanup_module(void)
  136. {
  137. debugfs_remove_recursive(dir);
  138. }