1、proc_create实例
static int mytest_flag = 0;//create flag variable
static ssize_t mytest_proc_write(struct file *file,const char __user *buffer,size_t count,loff_t *pos)
{
char mode;
if(count > 0)
{
if(get_user(mode,buffer))
return EFAULT;
mytest_flag = (mode != '0');
}
return count;
}
static int mytest_proc_show(struct seq_file *seq,void *v)
{
seq_puts(seq,mytest_flag ?"True\n":"False\n");
return 0;
}
static int mytest_proc_open(struct inode *inode,struct file *file)
{
return single_open(file,mytest_proc_show,inode->i_private);
}
static const struct file_operations myTest_proc_fops = {
.open = mytest_proc_open,
.read = seq_read,
.write = mytest_proc_write,
.llseek = seq_lseek,
.release = single_release,
};
static int battery_probe(struct platform_device *dev)
{ 。。。。。。
struct proc_dir_entry *testFile = proc_create("myTest",0644,NULL,&myTest_proc_fops);
if(NULL == testFile)
printk("%s,proc_create error.\n",__func__);
}
执行结果:
1)在CMD输入adb shell进入linux 文件系统;
2)进入proc目录
3)查看myTest结点,通过cat myTest或echo 1 > myTest,可对结点myTest进行读写操作
注意头文件:proc_fs.h
2、proc_create和create_proc_entry的区别
在最新linux内核中,不在使用create_proc_entry,请使用proc_create()进行结点建立
3、一些实用函数实现
extern struct proc_dir_entry *proc_symlink(const char *,
struct proc_dir_entry *, const char *);
extern struct proc_dir_entry *proc_mkdir(const char *, struct proc_dir_entry *);
extern struct proc_dir_entry *proc_mkdir_data(const char *, umode_t,
struct proc_dir_entry *, void *);
extern struct proc_dir_entry *proc_mkdir_mode(const char *, umode_t,
struct proc_dir_entry *);
extern struct proc_dir_entry *proc_create_data(const char *, umode_t,
struct proc_dir_entry *,
const struct file_operations *,
void *);
static inline struct proc_dir_entry *proc_create(
const char *name, umode_t mode, struct proc_dir_entry *parent,
const struct file_operations *proc_fops);
参考:http://www.ibm.com/developerworks/cn/linux/l-proc.html#ibm-pcon