Advertisement
xosski

Buffer_counter

Jan 9th, 2025
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. // SPDX-License-Identifier: GPL
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/moduleparam.h>
  5. #include <linux/i2c.h>
  6. #include <linux/utsname.h>
  7. #include <linux/cdev.h>
  8. #include <linux/fs.h>
  9. #include <linux/slab.h>
  10.  
  11. MODULE_LICENSE("GPL");
  12. MODULE_AUTHOR("Marko Puskovic");
  13.  
  14. static int local_buf_size = 10;
  15. module_param(local_buf_size, int, 0644);
  16.  
  17. static char *local_buf;
  18. static int hello_count = 1;
  19.  
  20. static dev_t hello_dev;
  21. static struct cdev hello_cdev;
  22.  
  23. static ssize_t hello_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos);
  24. static ssize_t hello_read(struct file *file, char __user *buf, size_t count, loff_t *ppos);
  25.  
  26. static const struct file_operations hello_fops = {
  27. .owner = THIS_MODULE,
  28. .read = hello_read,
  29. .write = hello_write,
  30. };
  31.  
  32. static size_t my_min(size_t a, size_t b)
  33. {
  34. return (a > b) ? b : a;
  35. }
  36.  
  37. static int __init hello_version_init(void)
  38. {
  39. int err;
  40.  
  41. cdev_init(&hello_cdev, &hello_fops);
  42.  
  43. pr_alert("You are currently using Linux %s.\n",
  44. init_uts_ns.name.release);
  45.  
  46. local_buf = kzalloc(local_buf_size, GFP_KERNEL);
  47. pr_alert("Initialized buffer of size %d at address 0x%p.\n",
  48. local_buf_size, local_buf);
  49. if (!local_buf) {
  50. err = -ENOMEM;
  51. goto err_exit;
  52. }
  53.  
  54. if (alloc_chrdev_region(&hello_dev, 0, hello_count, "zad1")) {
  55. err = -ENODEV;
  56. goto err_free_buff;
  57. }
  58.  
  59. pr_info("Major: %d, Minor: %d\n", MAJOR(hello_dev), MINOR(hello_dev));
  60. cdev_init(&hello_cdev, &hello_fops);
  61.  
  62. if (cdev_add(&hello_cdev, hello_dev, hello_count)) {
  63. err = -ENODEV;
  64. goto err_dev_unregister;
  65. }
  66.  
  67. return 0;
  68.  
  69. err_dev_unregister:
  70. unregister_chrdev_region(hello_dev, hello_count);
  71.  
  72. err_free_buff:
  73. kfree(local_buf);
  74.  
  75. err_exit:
  76. return err;
  77. }
  78.  
  79. static void __exit hello_version_exit(void)
  80. {
  81. time64_t unload_time = ktime_get_real_seconds();
  82.  
  83. pr_alert("Uninitializing module zad1 at time: %lld seconds\n", unload_time);
  84. cdev_del(&hello_cdev);
  85. unregister_chrdev_region(hello_dev, hello_count);
  86. kfree(local_buf);
  87. }
  88.  
  89. static ssize_t hello_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  90. {
  91. int len = strlen(local_buf + *ppos);
  92. int to_transfer = my_min(len, count);
  93.  
  94. if (to_transfer == 0)
  95. return 0;
  96.  
  97. if (copy_to_user(buf, local_buf + *ppos, to_transfer))
  98. return -EFAULT;
  99.  
  100. *ppos += to_transfer;
  101.  
  102. int i, lc_count = 0;
  103. for (i = 0; i < local_buf_size; ++i)
  104. if (local_buf[i] >= 'a' && local_buf[i] <= 'z')
  105. ++lc_count;
  106.  
  107. if (lc_count < 4)
  108. pr_alert("Number of lowercase letters is less than 4\n");
  109. else
  110. pr_alert("Number of lowercase letters is 4 or greater than 4\n");
  111.  
  112. return to_transfer;
  113. }
  114.  
  115. static ssize_t hello_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  116. {
  117. int to_transfer = my_min(local_buf_size - *ppos, count);
  118.  
  119. if (to_transfer == 0)
  120. return 0;
  121.  
  122. if (copy_from_user(local_buf + *ppos, buf, to_transfer))
  123. return -EFAULT;
  124.  
  125. *ppos += to_transfer;
  126.  
  127. int cifre = 0, i;
  128. for (i = 0; i < to_transfer; ++i)
  129. if (local_buf[i] >= '0' && local_buf[i] <= '9')
  130. ++cifre;
  131.  
  132. pr_alert("Number of digits: %d\n", cifre);
  133.  
  134. return to_transfer;
  135. }
  136.  
  137. module_init(hello_version_init);
  138. module_exit(hello_version_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement