Advertisement
xeritt

Proc read and write module linux kernel 4.15

Nov 22nd, 2018
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. /**
  2. ##Makefile
  3. obj-m += proc.o
  4.  
  5. all:
  6.     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
  7. clean:
  8.     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  9. */
  10.  
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/sched.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/slab.h>
  17.  
  18. int len,temp;
  19.  
  20. char *msg;
  21.  
  22. ssize_t read_proc(struct file *filp,char *buf,size_t count,loff_t *offp )
  23. {
  24.     if(count>temp)
  25.     {
  26.         count=temp;
  27.     }
  28.     temp=temp-count;
  29.     copy_to_user(buf,msg, count);
  30.     if(count==0)
  31.         temp=len;
  32.  
  33.     return count;
  34. }
  35.  
  36. ssize_t write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp)
  37. {
  38.     copy_from_user(msg,buf,count);
  39.     len=count;
  40.     temp=len;
  41.     return count;
  42. }
  43.  
  44. struct file_operations proc_fops = {
  45. read:
  46.     read_proc,
  47. write:
  48.     write_proc
  49. };
  50.  
  51. void create_new_proc_entry(void)  //use of void for no arguments is compulsory now
  52. {
  53.     proc_create("hello",0,NULL,&proc_fops);
  54.     msg=kmalloc(10*sizeof(char), GFP_KERNEL);
  55. }
  56.  
  57.  
  58. int proc_init (void) {
  59.     create_new_proc_entry();
  60.     return 0;
  61. }
  62.  
  63. void proc_cleanup(void) {
  64.     remove_proc_entry("hello",NULL);
  65.     kfree(msg);
  66. }
  67.  
  68. MODULE_LICENSE("GPL");
  69. module_init(proc_init);
  70. module_exit(proc_cleanup);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement