免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 932 | 回复: 0
打印 上一主题 下一主题

在内核中添加系统调用的方法和遇到的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP