Advertisement
xeritt

Proc read and write module linux kernel 5

May 23rd, 2024
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/proc_fs.h>
  4. #include <linux/sched.h>
  5. #include <linux/uaccess.h>
  6. #include <linux/slab.h>
  7.  
  8. int len,temp;
  9. char *msg;
  10.  
  11. ssize_t read_proc(struct file *filp, char *buf, size_t count, loff_t *offp ) {
  12.     if(count > temp) {
  13.         count = temp;
  14.     }
  15.     temp = temp - count;
  16.     copy_to_user(buf, msg, count);
  17.     if(count == 0)
  18.         temp = len;
  19.     return count;
  20. }
  21.  
  22. ssize_t write_proc(struct file *filp, const char *buf, size_t count, loff_t *offp) {
  23.     copy_from_user(msg, buf, count);
  24.     len = count;
  25.     temp = len;
  26.     return count;
  27. }
  28.  
  29. static const struct proc_ops proc_fops = {
  30.     proc_read: read_proc,
  31.     proc_write: write_proc,
  32. };
  33.  
  34. void create_new_proc_entry(void) { //use of void for no arguments is compulsory now
  35.     proc_create("proc", 0, NULL, &proc_fops);
  36.     msg = kmalloc(10 * sizeof(char), GFP_KERNEL);
  37. }
  38.  
  39. int proc_init (void) {
  40.     create_new_proc_entry();
  41.     return 0;
  42. }
  43.  
  44. void proc_cleanup(void) {
  45.     remove_proc_entry("hello", NULL);
  46.     kfree(msg);
  47. }
  48.  
  49. MODULE_LICENSE("GPL");
  50. MODULE_AUTHOR("Fantomas");
  51. module_init(proc_init);
  52. module_exit(proc_cleanup);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement