当前位置: 技术问答>linux和unix
一个关于在驱动程序中向文件中写数据的小问题
来源: 互联网 发布时间:2015-11-11
本文导语: 我在修改无线驱动程序的时候,希望在驱动程序中向一个文件中写入一些数据,大致代码如下: struct file *ch_fd; char buf[4]; int len; ch_fd = filp_open("/tmp/channel", O_CREAT|O_RDWR, 0777); if (IS_ERR(ch_fd)) { printk(RT_DEBUG_TRAC...
我在修改无线驱动程序的时候,希望在驱动程序中向一个文件中写入一些数据,大致代码如下:
struct file *ch_fd;
char buf[4];
int len;
ch_fd = filp_open("/tmp/channel", O_CREAT|O_RDWR, 0777);
if (IS_ERR(ch_fd))
{
printk(RT_DEBUG_TRACE, "--> Error %ld opening %sn", -PTR_ERR(ch_fd),"/tmp/channel");
}
else
{
if (ch_fd->f_op && ch_fd->f_op->write)
{
memset(buf, 0x00, 4);
sprintf(buf, "%d",pAd->PortCfg.Channel);
retval = ch_fd->f_op->write(ch_fd, buf, len, &ch_fd->f_pos);
}
retval=filp_close(ch_fd,NULL);
}
可运行结果是文件生成了,权限为:-rwxr-xr-x,可是文件中并没有写入数据,write()函数指针返回的值retval=-14。这段代码有什么问题,为什么不能往文件中写数据。望高手告知。谢谢
struct file *ch_fd;
char buf[4];
int len;
ch_fd = filp_open("/tmp/channel", O_CREAT|O_RDWR, 0777);
if (IS_ERR(ch_fd))
{
printk(RT_DEBUG_TRACE, "--> Error %ld opening %sn", -PTR_ERR(ch_fd),"/tmp/channel");
}
else
{
if (ch_fd->f_op && ch_fd->f_op->write)
{
memset(buf, 0x00, 4);
sprintf(buf, "%d",pAd->PortCfg.Channel);
retval = ch_fd->f_op->write(ch_fd, buf, len, &ch_fd->f_pos);
}
retval=filp_close(ch_fd,NULL);
}
可运行结果是文件生成了,权限为:-rwxr-xr-x,可是文件中并没有写入数据,write()函数指针返回的值retval=-14。这段代码有什么问题,为什么不能往文件中写数据。望高手告知。谢谢
|
好像需要修改边界地址值吧。
A file object contains a field f_op. This field points to a file_operations structure that is used to store the file operations table. This structure contains pointers to functions such as read() and write(). By invoking this read (or write) and passing as its first parameter a pointer to the file object, one can perform a read on the given file. The other three parameters for a read (or write) are the buffer in which to place the bytes read, the number of bytes to be read, and the position in the file to begin the read. Only one problem exists. The buffer is in kernel space.
The read operation is going to perform a check on this buffer to make sure it is in user space. Obviously this check will fail. This check will consist of comparing the address of the buffer with the value in the addr_limit field of the task_struct structure of the current process. A user space process will typically have an addr_limit value of 0xc0000000. We want to use the function set_fs() to change this addr_limit value to 0xffffffff. This will allow the check on the address of the buffer, which is in kernel space to pass.
可以参看代码:
http://www.cise.ufl.edu/~mfoster/research/kernel/LJArticle/ReadWriteFile.c
A file object contains a field f_op. This field points to a file_operations structure that is used to store the file operations table. This structure contains pointers to functions such as read() and write(). By invoking this read (or write) and passing as its first parameter a pointer to the file object, one can perform a read on the given file. The other three parameters for a read (or write) are the buffer in which to place the bytes read, the number of bytes to be read, and the position in the file to begin the read. Only one problem exists. The buffer is in kernel space.
The read operation is going to perform a check on this buffer to make sure it is in user space. Obviously this check will fail. This check will consist of comparing the address of the buffer with the value in the addr_limit field of the task_struct structure of the current process. A user space process will typically have an addr_limit value of 0xc0000000. We want to use the function set_fs() to change this addr_limit value to 0xffffffff. This will allow the check on the address of the buffer, which is in kernel space to pass.
可以参看代码:
http://www.cise.ufl.edu/~mfoster/research/kernel/LJArticle/ReadWriteFile.c