Chinaunix

标题: 在内核中添加系统调用的方法和遇到的问题 [打印本页]

作者: chinaunix_lb    时间: 2009-03-13 13:08
标题: 在内核中添加系统调用的方法和遇到的问题
在内核中添加自定义系统调用的步骤,我的linux版本是2.6.17。不同Linux版本的实现可能会有不同。
添加步骤:

在/usr/src/Linux-2.6.x/kernel/目录下创建文件mysyscall.c
#include  
#include  
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
#include
        
#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’
将标红的声明改为标紫的内容就可以成功编译。
但什么原因还不清楚。




本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/83134/showart_1861764.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2