1 函数说明:
struct proc_dir_entry *proc_mkdir( const char *name , struct proc_dir_entry *parent );
函数功能:创建一个目录
参数 :const char *name 要创建的目录名
struct proc_dir_entry *parent 父目录的结构体指针,如果为空,就在/proc目录下创建一个目录
返回值:失败返回NULL
struct proc_dir_entry *proc_create( const char *name, umode_t mode, struct proc_dir_entry *parent,
const struct file_operations *proc_fops)
函数功能:创建一个PROC文件
参数:const char *name 要创建的PROC文件名
umode_t mode 文件的访问权限,例如0777 0666 0644,
struct proc_dir_entry *parent 父目录的指针,如果为空就创建在/proc目录下
const struct file_operations *proc_fops PROC文件操作的函数指针,如open close read write等等
返回值:失败返回NULL
struct proc_dir_entry *proc_create_data(const char *name, umode_t mode, struct proc_dir_entry *parent,
const struct file_operations *proc_fops, void * data);
这个函数和proc_create的功能是基本一样的,只是多的一个参数void * data
void * data这个是传递给proc文件的数据,在文件的操作函数中,可以在read write之类的操作中调用PDE_DATA(file_inode(file)); 来获取这个指针。
void remove_proc_entry(const char *name, struct proc_dir_entry *parent);
函数功能:删除一个PROC文件
参数:const char *name 要删除的文件名
struct proc_dir_entry *parent 父目录的指针
2 示例代码:
static struct proc_dir_entry * test_entry = NULL;
static struct proc_dir_entry * test_root = NULL;
static ssize_t test_read(struct file * file,char __user *data,size_t len,loff_t *off)
{
char *ptr = PDE_DATA(file_inode(file));
printk("%s\n",ptr);
printk("proc read\n");
return 0;
}
static ssize_t test_write(struct file * file,const char __user* data,size_t len,loff_t *off)
{
printk("proc write\n");
return len;
}
static struct file_operations test_proc_ops = {
.owner = THIS_MODULE,
.read = test_read,
.write = test_write
};
char temp[]="this is test\0";
static int proc_init(void)
{
proc_root = proc_mkdir("test_dir", NULL);
test_entry = proc_create_data("test",0666,proc_root,&test_proc_ops ,&temp );
if(!test_entry){
printk(KERN_ERR"can't create proc file \n" );
return -EFAULT;
}
return 0;
}
static void proc_exit(void)
{
if(test_entry )
{
remove_proc_entry("test",proc_root);
}
}
如果调用了proc_init,我们就可以在/proc/test_dir目录下有一个test的文件
如果我们执行cat /proc/test_dir/test
就可以看到
this is test
proc read
的串口信息了
同样的如果执行echo "123" /proc/test_dir/test
我们就可以看到
proc write
的串口信息了
struct proc_dir_entry *proc_mkdir( const char *name , struct proc_dir_entry *parent );
函数功能:创建一个目录
参数 :const char *name 要创建的目录名
struct proc_dir_entry *parent 父目录的结构体指针,如果为空,就在/proc目录下创建一个目录
返回值:失败返回NULL
struct proc_dir_entry *proc_create( const char *name, umode_t mode, struct proc_dir_entry *parent,
const struct file_operations *proc_fops)
函数功能:创建一个PROC文件
参数:const char *name 要创建的PROC文件名
umode_t mode 文件的访问权限,例如0777 0666 0644,
struct proc_dir_entry *parent 父目录的指针,如果为空就创建在/proc目录下
const struct file_operations *proc_fops PROC文件操作的函数指针,如open close read write等等
返回值:失败返回NULL
struct proc_dir_entry *proc_create_data(const char *name, umode_t mode, struct proc_dir_entry *parent,
const struct file_operations *proc_fops, void * data);
这个函数和proc_create的功能是基本一样的,只是多的一个参数void * data
void * data这个是传递给proc文件的数据,在文件的操作函数中,可以在read write之类的操作中调用PDE_DATA(file_inode(file)); 来获取这个指针。
void remove_proc_entry(const char *name, struct proc_dir_entry *parent);
函数功能:删除一个PROC文件
参数:const char *name 要删除的文件名
struct proc_dir_entry *parent 父目录的指针
2 示例代码:
static struct proc_dir_entry * test_entry = NULL;
static struct proc_dir_entry * test_root = NULL;
static ssize_t test_read(struct file * file,char __user *data,size_t len,loff_t *off)
{
char *ptr = PDE_DATA(file_inode(file));
printk("%s\n",ptr);
printk("proc read\n");
return 0;
}
static ssize_t test_write(struct file * file,const char __user* data,size_t len,loff_t *off)
{
printk("proc write\n");
return len;
}
static struct file_operations test_proc_ops = {
.owner = THIS_MODULE,
.read = test_read,
.write = test_write
};
char temp[]="this is test\0";
static int proc_init(void)
{
proc_root = proc_mkdir("test_dir", NULL);
test_entry = proc_create_data("test",0666,proc_root,&test_proc_ops ,&temp );
if(!test_entry){
printk(KERN_ERR"can't create proc file \n" );
return -EFAULT;
}
return 0;
}
static void proc_exit(void)
{
if(test_entry )
{
remove_proc_entry("test",proc_root);
}
}
如果调用了proc_init,我们就可以在/proc/test_dir目录下有一个test的文件
如果我们执行cat /proc/test_dir/test
就可以看到
this is test
proc read
的串口信息了
同样的如果执行echo "123" /proc/test_dir/test
我们就可以看到
proc write
的串口信息了