커 널 모듈 읽 기 및 쓰기/proc 생 성
2187 단어 커 널 커 널
/*************************************************
* seq_file proc
* 3.10
* Author: ZhangNa
* Date: 2015-5-17
* *************************************************/
#include
#include
#include
#include
#include
#include
#include
static char *str = NULL;
/*5, show
*
* proc file */
static int my_proc_show(struct seq_file *m, void *v)
{
/* printfk
* seq_file
* ldd3 91 */
// seq_printf(m, "current kernel time is %ld
", jiffies);
seq_printf(m, "str is %s
", str);
return 0;
}
/*3, open write */
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);
}
/*2, proc_create flie_operations
* my ,
* seq single ,
* open
* ldd3 93 */
static struct file_operations my_fops = {
.owner = THIS_MODULE,
.open = my_proc_open,
.release = single_release,
.read = seq_read,
.llseek = seq_lseek,
.write = my_proc_write,
};
static int __init my_init(void)
{
struct proc_dir_entry *file;
/*3.10 proc
* file_operations*/
/*1, proc , flie_operations*/
file = proc_create("abc_proc", 0644, NULL, &my_fops);
if(!file)
return -ENOMEM;
return 0;
}
/*6, proc */
static void __exit my_exit(void)
{
remove_proc_entry("abc_proc", NULL);
kfree(str);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZhangNa");