http://blog.youkuaiyun.com/zhangjs0322/article/details/8963748
本文以linux-3.5.4内核、x86平台、64位,为例进行说明。
添加新的系统调用,共需修改3处文件:
(1)分配系统调用号:include/asm-generic/unistd.h
(2)修改系统调用表:arch/x86/syscalls/syscall_64.tbl。较早版本的内核,修改的是arch/x86/kernel/syscall_table_32.S文件。
(3)添加处理函数:kernel/sys.c,(不是必须在这个文件中添加,可在其他文件或新建文件)
1、分配系统调用号
修改include/asm-generic/unistd.h文件,设系统调用号为314,不冲突即可。同时修改__NR_syscalls的值274为275。
[cpp] view plaincopyprint?
#define __NR_mysyscall 313
__SYSCALL(__NR_mysyscall, sys_mysyscall)
#undef __NR_syscalls
#define __NR_syscalls 273
#define __NR_mysyscall 313 __SYSCALL(__NR_mysyscall, sys_mysyscall) #undef __NR_syscalls#define __NR_syscalls 273
2、修改系统调用表
修改arch/x86/syscalls/syscall_64.tbl文件,根据原有表内容的格式,在322行处添加如下内容
[cpp] view plaincopyprint?
313 64 mysyscall sys_mysyscall
313 64 mysyscall sys_mysyscall
3、添加处理函数
修改kernel/sys.c文件。添加不带参数的系统调用处理函数。
[cpp] view plaincopyprint?
SYSCALL_DEFINE0(mysyscall)
{
//在此处加入遍历进程的代码
struct task_struct *p;
printk("********************************************\n");
printk("------------the output of mysyscall------------\n");
printk("********************************************\n\n");
printk("%-20s %-6s %-6s %-20s\n","Name","pid","state","ParentName");
for(p = &init_task; (p = next_task(p)) != &init_task;)
printk("%-20s %-6d %-6d %-20s\n",p->comm , p->pid, p->state, p->parent->comm);
return 0;
}
SYSCALL_DEFINE0(mysyscall){ //在此处加入遍历进程的代码 struct task_struct *p; printk("********************************************\n"); printk("------------the output of mysyscall------------\n"); printk("********************************************\n\n"); printk("%-20s %-6s %-6s %-20s\n","Name","pid","state","ParentName"); for(p = &init_task; (p = next_task(p)) != &init_task;) printk("%-20s %-6d %-6d %-20s\n",p->comm , p->pid, p->state, p->parent->comm); return 0;}
4、系统调用测试程序
编写简单的测试程序
[cpp] view plaincopyprint?
#include <linux/unistd.h>
#include <sys/syscall.h>
//系统调用号根据实验具体
#define __NR_mysyscall 313
//数字而定
int main()
{
syscall(__NR_mysyscall); /*或 syscall(313) */
}