Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我的系统调用导致分段错误(内核转储)_C_Linux_System Calls - Fatal编程技术网

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 {

我正在基于此编写一个简单的系统调用,并将我的系统调用引导到新内核中,但当我编译并执行测试时,它会导致分段错误(内核转储)。 我的My_syscall.h(教程中相当于hello.c)如下所示

#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-in
copy\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;
}