#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
static int __init hello_start(void)
{
struct file* my_fd;
my_fd = filp_open ("/tmp/foobar", O_WRONLY | O_APPEND, 0);
if (IS_ERR (my_fd))
{
printk (KERN_ERR "Failed to open file. err: %d.\n", my_fd);
}
else
{
my_fd->f_op->write (my_fd, "some data", 10, &my_fd->f_pos);
}
printk(KERN_INFO "Loading hello module...\n");
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "hello_end.\n");
}
module_init(hello_start);
module_exit(hello_end);
Run Code Online (Sandbox Code Playgroud)
上面的代码在文件中写入时给出错误-14.我在这做错了什么?
这是dmesg输出:
[19551.674999] Write returned: -14.
[19551.675004] Loading hello module...
Run Code Online (Sandbox Code Playgroud)
(in )的write成员声明如下:struct file_operationsinclude/linux/fs.h
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
Run Code Online (Sandbox Code Playgroud)
注意__user第二个参数上的标记,它告诉你它正在期待一个用户空间指针.当你像从内核那样调用它时,你传递的是一个内核空间指针; 因此你的记忆错误.
@ ShinTakezou链接到"acct.c"代码是你想要看的; 特别是,要求set_fs内核使用自己的数据段作为"用户"数据段.
| 归档时间: |
|
| 查看次数: |
2606 次 |
| 最近记录: |