【发布时间】:2013-03-19 14:33:24
【问题描述】:
系统调用写它的定义如下:
SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, size_t, count)
{
struct file *file;
ssize_t ret = -EBADF;
int fput_needed;
file = fget_light(fd, &fput_needed);
if (file) {
loff_t pos = file_pos_read(file);
ret = vfs_write(file, buf, count, &pos);
file_pos_write(file, pos);
fput_light(file, fput_needed);
}
return ret;
}
我想复制变量 buf 来修改您的内容和 然后在以下位置使用这个新变量:
vfs_write(file, new_buf, count, &pos);
我尝试使用kmalloc 将内存分配给char 指针变量,然后我使用copy_from_user() 进行复制。最后我在vfs_write() 使用了新变量。重新编译内核并重新启动系统后,我收到内核恐慌错误消息。
这是我生成内核恐慌错误消息的实现:
SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, size_t, count){
struct file *file;
ssize_t ret = -EBADF;
int fput_needed;
char *data;
data = kmalloc(count, GFP_KERNEL);
if(!data)
return ret;
copy_from_user(data, buf, count);
file = fget_light(fd, &fput_needed);
if (file) {
loff_t pos = file_pos_read(file);
ret = vfs_write(file, data, count, &pos);
file_pos_write(file, pos);
fput_light(file, fput_needed);
}
return ret;
}
如何在内核模式下进行复制?
我使用的是 Linux Mint 12 - 内核版本:3.0.30
【问题讨论】:
-
我尝试使用“kmalloc”将内存分配给 char 指针变量,然后使用 copy_from_user() 进行复制。最后,我在 vfs_write() 处使用了新变量。重新编译内核并重新启动系统后,我收到内核恐慌错误消息。