Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<linux/kernel.h>
- #include<linux/module.h>
- #include<linux/init.h>
- #include<linux/proc_fs.h>
- #include<asm/uaccess.h>
- #include<linux/slab.h>
- #include<linux/cred.h>
- #define PROC_NAME "procfile2"
- static char *msg="Trying to open proc file system";
- static char *proc_buffer;
- static struct proc_dir_entry *our_proc_file;
- static struct proc_dir_entry *proc_root;
- static struct cred *proc_current;
- static int bytes_read=0;
- static int proc_open(struct inode *inode,struct file *file)
- {
- printk(KERN_INFO "proc file open() \n");
- try_module_get(THIS_MODULE);
- return 0;
- }
- static int proc_close(struct inode *inode,struct file *file)
- {
- printk(KERN_INFO "proc file close() \n");
- module_put(THIS_MODULE);
- return 0;
- }
- static ssize_t proc_read(struct file *filp,char __user *buffer,size_t len,loff_t *offset)
- {
- printk(KERN_INFO "proc file read() \n");
- printk(KERN_INFO "User Buffer length %d \n",len);
- if(bytes_read)
- {
- printk(KERN_INFO "Read operation END \n");
- bytes_read=0;
- printk(KERN_INFO "Bytes read after read %d \n",bytes_read);
- return 0;
- }
- while(*msg != '\0')
- {
- put_user(*(msg++),buffer++);
- len--;
- bytes_read++;
- }
- printk(KERN_INFO "User Buffer length after %d \n",len);
- printk(KERN_INFO "Bytes read from kernel %d \n",bytes_read);
- return bytes_read;
- }
- static ssize_t proc_write(struct file *filp,const char __user *buffer,size_t len,loff_t *offset)
- {
- printk(KERN_INFO "proc file write() \n");
- printk(KERN_INFO "Buffer length %d \n",len);
- proc_buffer=kmalloc(len,GFP_KERNEL);
- while(len-- != 1)
- {
- get_user(*(proc_buffer+bytes_read),buffer+bytes_read);
- bytes_read++;
- }
- *(proc_buffer+bytes_read)='\0';
- printk(KERN_INFO "User input is %s \n",proc_buffer);
- printk(KERN_INFO "Bytes read is %d \n",bytes_read);
- kfree(proc_buffer);
- printk(KERN_INFO "User input after free %s \n",proc_buffer);
- bytes_read=0;
- printk(KERN_INFO "Write operation END \n");
- return bytes_read;
- }
- static int module_permission(struct inode *inode, int op)
- {
- printk(KERN_INFO "permission is %d \n",op);
- if(op==4||(op==2 && (proc_current->euid == 0)))
- return 0;
- else
- return -EACCES;
- }
- static struct file_operations fops={
- .read=proc_read,
- .write=proc_write,
- .open=proc_open,
- .release=proc_close
- };
- static struct inode_operations iops={
- .permission=module_permission
- };
- static int __init proc_init(void)
- {
- printk(KERN_INFO "Init module loaded \n");
- our_proc_file=create_proc_entry(PROC_NAME, 0644, NULL);
- if(our_proc_file == NULL)
- {
- remove_proc_entry(PROC_NAME,proc_root);
- printk(KERN_INFO "Error in creating proc file \n");
- return -ENOMEM;
- }
- our_proc_file->proc_fops=&fops;
- our_proc_file->proc_iops=&iops;
- our_proc_file->mode=S_IFREG | S_IRUGO | S_IWUSR;
- our_proc_file->uid=0;
- our_proc_file->gid=0;
- our_proc_file->size=80;
- printk(KERN_INFO "proc file created in '/proc' \n");
- return 0;
- }
- void __exit proc_exit(void)
- {
- printk(KERN_INFO "Exit module loaded \n");
- remove_proc_entry(PROC_NAME,proc_root);
- printk(KERN_INFO "proc file removed from '/proc' \n");
- }
- module_init(proc_init);
- module_exit(proc_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement