C 我的系统调用导致分段错误(内核转储)
我正在基于此编写一个简单的系统调用,并将我的系统调用引导到新内核中,但当我编译并执行测试时,它会导致分段错误(内核转储)。 我的My_syscall.h(教程中相当于hello.c)如下所示C 我的系统调用导致分段错误(内核转储),c,linux,system-calls,C,Linux,System Calls,我正在基于此编写一个简单的系统调用,并将我的系统调用引导到新内核中,但当我编译并执行测试时,它会导致分段错误(内核转储)。 我的My_syscall.h(教程中相当于hello.c)如下所示 #include <linux/module.h> #include <linux/kernel.h> #include <linux/sched.h> #include <asm/current.h> struct smallStruct {
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <asm/current.h>
struct smallStruct {
int integer;
char str[20];
};
struct bigStruct {
long id;
struct smallStruct a;
struct smallStruct b;
struct smallStruct c;
};
asmlinkage long test_syscall(int pid, struct bigStruct *info)
{
info->id = pid;
info->a.integer = 0;
info->a.str[0] = 'A';
info->a.str[1] = '\0';
info->b.integer = 1;
info->b.str[0] = 'B';
info->b.str[1] = '\0';
info->c.integer = 2;
info->c.str[0] = 'C';
info->c.str[1] = '\0';
return 0;
}
我的测试中的info
似乎根本没有被系统调用修改。
我甚至在my_syscall.h中使用了printk
,而传递给我的syscall的pid
根本不正确。这就是printk
在dmesg
my_syscall中的id
应该是pid
,它在我的测试中以1的值通过。(syscall(548,1,&info);
)
我知道时间很长,但如果有人能解释一下我的工具有什么问题,我将不胜感激,
谢谢。您正在将作为进程堆栈一部分的用户指针传递给内核。这是一种糟糕的做法,而且永远不会起作用,因为内核内存和用户内存是独立的实体。您需要使用系统API将指针传递到内核内存,并从用户传递到内核内存。查看复制用户和从用户功能复制用户。您还可以使用ioctl方法,在用户/应用程序和内核之间创建一个后门不要将文本作为图片发布。看看现有的系统调用如何处理用户指针。要表示进程id(我猜是pid?),可以使用
p\u id
数据类型。它转换为整数,而不是长的Look-incopy\u to\u user
?请发布您在测试中使用的相同代码。非常感谢,我会尝试,我真的很感谢您的帮助,我希望您有一个愉快的一天^^您能解释一下为什么“syscall(548,1,&info);”我得到的pid
应该是1,但是当我在dmesg
上打印k时,它有如图所示的随机值,请我看到很多人这样做,他们成功地传递了价值观。
#include <sys/syscall.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct smallStruct
{
int integer;
char str[20];
};
struct bigStruct
{
long id;
struct smallStruct a;
struct smallStruct b;
struct smallStruct c;
};
int main()
{
long sys_return_value;
struct bigStruct info;
sys_return_value = syscall(548, 1, &info);
printf("id = %ld, return %ld\n", info.id, sys_return_value);
return 0;
}