在内核中添加系统调用的方法和遇到的问题
时间:2009-03-13 来源:chinaunix_lb
在内核中添加自定义系统调用的步骤,我的linux版本是2.6.17。不同Linux版本的实现可能会有不同。
添加步骤:
一
在/usr/src/Linux-2.6.x/kernel/目录下创建文件mysyscall.c
#include <linux/linkage.h>
#include <linux/kernel.h>
asmlinkage int sys_mysyscall () {
printk(KERN_EMERG “My Syscall \n”);
return(1);
} 二 修改文件/usr/src/Linux-2.6.17/include/asm-i386/unistd.h,定义自己的系统调用号 #define __NR_mysyscall 317 将最后一行的系统调用号总数加一 #define NR_syscalls 318 三 修改usr/src/Linux-2.6.17/arch/i386/kernel/syscall_table.S,添加系统调用服务例程
.long sys_mysyscall 四
修改makefile文件/usr/src/Linux-2.6.17/kernel/Makefile,添加自定义系统调用的目标文件
obj-y += mysyscall.o 五 重新编译内核 cd /usr/src/Linux-2.6.17 make mrproper make clean make menuconfig make make modules_install make install 六 重启系统,进入编译好的内核的系统 七 编写测试程序test.c,测试自定义的系统调用是否成功 #include <linux/errno.h>
#include <sys/syscall.h> #define __NR_mysyscall 317
//_syscall0(int,mysyscall); int mysyscall()
{
return syscall(__NR_mysyscall);
} int main()
{
mysyscall();
return 0;
}
编译后运行,即可看到输出结果。
遇到问题 mysyscall.c:10: error: expected declaration specifiers or ‘...’ before ‘mysyscall’ 将标红的声明改为标紫的内容就可以成功编译。 但什么原因还不清楚。
#include <linux/linkage.h>
#include <linux/kernel.h>
asmlinkage int sys_mysyscall () {
printk(KERN_EMERG “My Syscall \n”);
return(1);
} 二 修改文件/usr/src/Linux-2.6.17/include/asm-i386/unistd.h,定义自己的系统调用号 #define __NR_mysyscall 317 将最后一行的系统调用号总数加一 #define NR_syscalls 318 三 修改usr/src/Linux-2.6.17/arch/i386/kernel/syscall_table.S,添加系统调用服务例程
.long sys_mysyscall 四
修改makefile文件/usr/src/Linux-2.6.17/kernel/Makefile,添加自定义系统调用的目标文件
obj-y += mysyscall.o 五 重新编译内核 cd /usr/src/Linux-2.6.17 make mrproper make clean make menuconfig make make modules_install make install 六 重启系统,进入编译好的内核的系统 七 编写测试程序test.c,测试自定义的系统调用是否成功 #include <linux/errno.h>
#include <sys/syscall.h> #define __NR_mysyscall 317
//_syscall0(int,mysyscall); int mysyscall()
{
return syscall(__NR_mysyscall);
} int main()
{
mysyscall();
return 0;
}
编译后运行,即可看到输出结果。
遇到问题 mysyscall.c:10: error: expected declaration specifiers or ‘...’ before ‘mysyscall’ 将标红的声明改为标紫的内容就可以成功编译。 但什么原因还不清楚。
相关阅读 更多 +