【发布时间】:2020-05-16 14:04:07
【问题描述】:
我是 Linux 编程的新手,我正在尝试按照本指南松散地实现一个简单的系统调用:https://medium.com/anubhav-shrimal/adding-a-hello-world-system-call-to-linux-kernel-dad32875872。在我的 linux 内核目录中,我创建了一个名为 my_syscall 的新目录。在那个目录中,我创建了my_syscall.c。这里是my_syscall.c
#include <linux/syscalls.h>
#include <linux/kernel.h>
asmlinkage long sys_my_syscall(int i) {
prink(KERN_INFO "This is the system call.");
return(0);
}
然后,我在my_syscall 目录中创建了一个Makefile,其中包含一行:
obj-y := my_syscall.o
然后我将内核目录中Makefile中的这一行编辑为:
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/ my_syscall/
然后,在目录linux-5.4.15/arch/x86/entry/syscalls 中,我编辑了syscall_64.tbl 以在最后包含以下行:
548 64 my_syscall sys_my_syscall
最后,在linux-5.4.15/include/linux 目录中,我编辑了syscalls.h 文件,将这一行包含在#endif 之前:
asmlinkage long sys_my_syscall(int i);
现在,当我运行命令 sudo make 时,很快就会遇到以下错误:
./arch/x86/include/generated/asm/syscalls_64.h:2664:19: error: conflicting types for 'sys_my_syscall'
__SYSCALL_64(548, sys_my_syscall, )
arch/x86/entry/syscall_64.c:18:60: note: in definition of macro '__SYSCALL-64'
#define __SYSCALL_64(nr, sym, qual) extern asmlinkage long sym(const struct pt_regs *);
In file included from arch/x86/entry/syscall_64.c:7:0:
./include/linux/syscalls.h:1423:17: note: previous declaration of 'sys_my_syscall' was here
asmlinkage long sys_my_syscall(int i);
^
make[3]: *** [arch/x86/entry/syscall_64.o] Error 1
make[2]: *** [arch/x86/entry] Error 2
make[1]: *** [arch/x86] Error 2
make: *** [sub-make] Error 2
我不知道如何处理这个错误。由于类型冲突错误,我想我在某个地方以不同的方式声明了系统调用,但在 my_syscall.c 和 syscalls.h 文件中,声明是相同的。这些是声明系统调用的仅有的两个文件,但它也在syscall_64.tbl 中命名,这似乎是 linux 试图指向我的地方。但是,当我直接按照指南进行操作时,我看不出我在表格中声明它的方式有什么问题。对此的任何帮助将不胜感激!
信息:
内核版本:5.4.15
Linux 发行版:Ubuntu 14
【问题讨论】:
-
可能重复:System call hooking example arguments are incorrect。由于 x86_64 上的 4.17 版“原始”系统调用函数接受 single 参数
const struct pt_regs *,它包含了它们的真实参数。 -
@Tsyvarev 我不认为这个 Q 是 System call hooking example arguments are incorrect 的骗子。这个 Q 是关于添加一个 new 系统调用,而不是挂钩现有的系统调用。
标签: linux linux-kernel system-calls