1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <linux/module.h> /* Specifically, a module */ #include <linux/kernel.h> /* We're doing kernel work */ #include <linux/proc_fs.h> /* Necessary because we use the proc fs */ #include <asm/uaccess.h> /* for copy_from_user */ #include <linux/seq_file.h> //using seq_printf #include <linux/slab.h> // Using kzalloc
#define PROCFS_NAME "debug_sy"
MODULE_AUTHOR("Yi Sun"); static char *str = NULL;
static int my_proc_show(struct seq_file *m,void *v){ seq_printf(m,"%s\n",str); return 0; }
static ssize_t my_proc_write(struct file* file,const char __user *buffer,size_t count,loff_t *f_pos){ char *tmp = kzalloc((count+1),GFP_KERNEL); if(!tmp)return -ENOMEM; if(copy_from_user(tmp,buffer,count)){ kfree(tmp); return EFAULT; } kfree(str); str=tmp; return count; }
static int my_proc_open(struct inode *inode,struct file *file){ return single_open(file,my_proc_show,NULL); }
static const struct proc_ops my_fops={ .proc_open = my_proc_open, .proc_release = single_release, .proc_read = seq_read, .proc_lseek = seq_lseek, .proc_write = my_proc_write };
static int __init hello_init(void){ struct proc_dir_entry *entry; entry = proc_create(PROCFS_NAME,0777,NULL,&my_fops); if(!entry){ return -1; }else{ printk(KERN_INFO "create proc file successfully\n"); } return 0; }
static void __exit hello_exit(void){ remove_proc_entry(PROCFS_NAME,NULL); printk(KERN_INFO "Goodbye world!\n"); }
module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL");
|