proc文件的简单读写
在开发模块功能时,需要用到一些调试或者传值给模块的方法,最简单的就是构造一个proc文件,然后配置传值或查看 。如下提供了一个简单模板,需要时可以直接复制过去使用。/* Init a test proc file 'btn_test'*/#include <linux/proc_fs.h>static char nvram_query_str[256]={0};static ssize
·
在开发模块功能时,需要用到一些调试或者传值给模块的方法,最简单的就是构造一个proc文件,然后配置传值或查看 。
如下提供了一个简单模板,需要时可以直接复制过去使用。
/* Init a test proc file 'btn_test'*/
#include <linux/proc_fs.h>
static char nvram_query_str[256]={0};
static ssize_t btn_test_read(struct file *file,
char __user *buf,
size_t len, loff_t *pos)
{
int ret=0;
ret = sprintf(buf, "%s", nvram_query_str);
return simple_read_from_buffer(buf, len, pos, buf, strlen(buf));
}
static ssize_t btn_test_write(struct file *file, const char *buff, size_t len, loff_t *offset)
{
memset(nvram_query_str, '\0', sizeof(nvram_query_str));
if ((len > sizeof(nvram_query_str)-1) || (copy_from_user(nvram_query_str, buff, len) != 0))
return -EFAULT;
if('1' == nvram_query_str[0])
{
printk("[%s][%d] Do Restore action.\n", __FUNCTION__, __LINE__);
btnHook_RestoreToDefault(0, NULL);
}
return len;
}
const struct file_operations btn_test_fops = {
.owner = THIS_MODULE,
.read = btn_test_read,
.write = btn_test_write,
};
static int init_proc_btn_test(void)
{
proc_create("btn_test", S_IRUGO, NULL, &btn_test_fops);
printk("[%s][%d] Success.s\n", __FUNCTION__, __LINE__);
return 0;
}
//file: fs/libfs.c
/**
* simple_read_from_buffer - copy data from the buffer to user space
* @to: the user space buffer to read to
* @count: the maximum number of bytes to read
* @ppos: the current position in the buffer
* @from: the buffer to read from
* @available: the size of the buffer
*
* The simple_read_from_buffer() function reads up to @count bytes from the
* buffer @from at offset @ppos into the user space address starting at @to.
*
* On success, the number of bytes read is returned and the offset @ppos is
* advanced by this number, or negative value is returned on error.
**/
ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
const void *from, size_t available)
{
loff_t pos = *ppos;
size_t ret;
if (pos < 0)
return -EINVAL;
if (pos >= available || !count)
return 0;
if (count > available - pos)
count = available - pos;
ret = copy_to_user(to, from + pos, count);
if (ret == count)
return -EFAULT;
count -= ret;
*ppos = pos + count;
return count;
}
EXPORT_SYMBOL(simple_read_from_buffer);
/**
* simple_write_to_buffer - copy data from user space to the buffer
* @to: the buffer to write to
* @available: the size of the buffer
* @ppos: the current position in the buffer
* @from: the user space buffer to read from
* @count: the maximum number of bytes to read
*
* The simple_write_to_buffer() function reads up to @count bytes from the user
* space address starting at @from into the buffer @to at offset @ppos.
*
* On success, the number of bytes written is returned and the offset @ppos is
* advanced by this number, or negative value is returned on error.
**/
ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
const void __user *from, size_t count)
{
loff_t pos = *ppos;
size_t res;
if (pos < 0)
return -EINVAL;
if (pos >= available || !count)
return 0;
if (count > available - pos)
count = available - pos;
res = copy_from_user(to + pos, from, count);
if (res == count)
return -EFAULT;
count -= res;
*ppos = pos + count;
return count;
}
EXPORT_SYMBOL(simple_write_to_buffer);
更多推荐

所有评论(0)