Advertisement
hasan4791

procfile.c

Jan 7th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include<linux/kernel.h>
  2. #include<linux/module.h>
  3. #include<linux/init.h>
  4. #include<linux/proc_fs.h>
  5. #include<asm/uaccess.h>
  6. #include<linux/slab.h>
  7. #include<linux/cred.h>
  8.  
  9. #define PROC_NAME "procfile2"
  10.  
  11. static char *msg="Trying to open proc file system";
  12. static char *proc_buffer;
  13. static struct proc_dir_entry *our_proc_file;
  14. static struct proc_dir_entry *proc_root;
  15. static struct cred *proc_current;
  16. static int bytes_read=0;
  17.  
  18. static int proc_open(struct inode *inode,struct file *file)
  19. {
  20. printk(KERN_INFO "proc file open() \n");
  21. try_module_get(THIS_MODULE);
  22. return 0;
  23. }
  24.  
  25. static int proc_close(struct inode *inode,struct file *file)
  26. {
  27. printk(KERN_INFO "proc file close() \n");
  28. module_put(THIS_MODULE);
  29. return 0;
  30. }
  31.  
  32. static ssize_t proc_read(struct file *filp,char __user *buffer,size_t len,loff_t *offset)
  33. {
  34. printk(KERN_INFO "proc file read() \n");
  35. printk(KERN_INFO "User Buffer length %d \n",len);
  36. if(bytes_read)
  37. {
  38. printk(KERN_INFO "Read operation END \n");
  39. bytes_read=0;
  40. printk(KERN_INFO "Bytes read after read %d \n",bytes_read);
  41. return 0;
  42. }
  43. while(*msg != '\0')
  44. {
  45. put_user(*(msg++),buffer++);
  46. len--;
  47. bytes_read++;
  48. }
  49. printk(KERN_INFO "User Buffer length after %d \n",len);
  50. printk(KERN_INFO "Bytes read from kernel %d \n",bytes_read);
  51. return bytes_read;
  52. }
  53.  
  54. static ssize_t proc_write(struct file *filp,const char __user *buffer,size_t len,loff_t *offset)
  55. {
  56. printk(KERN_INFO "proc file write() \n");
  57. printk(KERN_INFO "Buffer length %d \n",len);
  58. proc_buffer=kmalloc(len,GFP_KERNEL);
  59. while(len-- != 1)
  60. {
  61. get_user(*(proc_buffer+bytes_read),buffer+bytes_read);
  62. bytes_read++;
  63. }
  64. *(proc_buffer+bytes_read)='\0';
  65. printk(KERN_INFO "User input is %s \n",proc_buffer);
  66. printk(KERN_INFO "Bytes read is %d \n",bytes_read);
  67. kfree(proc_buffer);
  68. printk(KERN_INFO "User input after free %s \n",proc_buffer);
  69. bytes_read=0;
  70. printk(KERN_INFO "Write operation END \n");
  71. return bytes_read;
  72. }
  73.  
  74. static int module_permission(struct inode *inode, int op)
  75. {
  76. printk(KERN_INFO "permission is %d \n",op);
  77. if(op==4||(op==2 && (proc_current->euid == 0)))
  78. return 0;
  79. else
  80. return -EACCES;
  81. }
  82.  
  83. static struct file_operations fops={
  84. .read=proc_read,
  85. .write=proc_write,
  86. .open=proc_open,
  87. .release=proc_close
  88. };
  89.  
  90. static struct inode_operations iops={
  91. .permission=module_permission
  92. };
  93.  
  94. static int __init proc_init(void)
  95. {
  96. printk(KERN_INFO "Init module loaded \n");
  97. our_proc_file=create_proc_entry(PROC_NAME, 0644, NULL);
  98. if(our_proc_file == NULL)
  99. {
  100. remove_proc_entry(PROC_NAME,proc_root);
  101. printk(KERN_INFO "Error in creating proc file \n");
  102. return -ENOMEM;
  103. }
  104. our_proc_file->proc_fops=&fops;
  105. our_proc_file->proc_iops=&iops;
  106. our_proc_file->mode=S_IFREG | S_IRUGO | S_IWUSR;
  107. our_proc_file->uid=0;
  108. our_proc_file->gid=0;
  109. our_proc_file->size=80;
  110. printk(KERN_INFO "proc file created in '/proc' \n");
  111. return 0;
  112. }
  113.  
  114. void __exit proc_exit(void)
  115. {
  116. printk(KERN_INFO "Exit module loaded \n");
  117. remove_proc_entry(PROC_NAME,proc_root);
  118. printk(KERN_INFO "proc file removed from '/proc' \n");
  119. }
  120.  
  121. module_init(proc_init);
  122. module_exit(proc_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement