Chinaunix首页 | 论坛 | 博客
  • 博客访问: 528212
  • 博文数量: 120
  • 博客积分: 3030
  • 博客等级: 中校
  • 技术积分: 1445
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-05 01:00
文章存档

2011年(1)

2009年(2)

2008年(32)

2007年(33)

2006年(52)

我的朋友

分类: LINUX

2008-02-26 14:58:36

转载的 ,出处不详 
在2.6.8中通过模块添加系统调用
 
 我在2.6.8中通过模块添加系统调用,发现了两个问题:
1.是sys_call_table的符号不可以被解析
2.除了283 所有的系统调用号都已经被占用 ,且没有空余。(要是想添加的系统调用号大于283,我们就要先改变unistd.h中的NR_syscalls 改的大一点,还要编译内核)

sys_call_table不可以被解析的问题 ,我通过直接调用他的地址0xc02b2600实现的

这是模块程序:
#include
#include
#include
#include

#include

#include // for copy_to_user
#include // for current macro

#define __NR_pedagogictime 283

static int (*saved)(void);

static int sys_pedagogictime(struct timeval *tv)
{
struct timeval ktv;
do_gettimeofday(&ktv);
copy_to_user(tv,&ktv,sizeof(ktv));
printk(KERN_ALERT"PID %ld called sys_gettimeofday().\n",(long)current->pid);
return 0;
}

int syscall(void)
{
long *systable;
systable=(long*)0xc02b2600;
saved=(int(*)(void))(systable[__NR_pedagogictime]);
systable[__NR_pedagogictime]=(unsigned long)sys_pedagogictime;
return 0;
}

void exit_syscall(void)
{
unsigned long *systable;
systable=( long*)0xc02b2600;
systable[__NR_pedagogictime]=(unsigned long)saved;
}

module_init(syscall);
module_exit(exit_syscall);


把上边这个模块编译成syscall.ko后 加载到内核我们就可以实用这个系统调用了

这是一个应用这个系统调用的程序
#include
#include
#define __NR_pedagogictime 283

_syscall1(int,pedagogictime,struct timeval *,thetime)
struct timeval tv;
int main()
{
//struct timeval tv;

pedagogictime(&tv);
printf("tv_sec:%ld\n",tv.tv_sec);
printf("tv_nsec:%ld\n",tv.tv_usec);

printf("em...,let me sleep for 2 second.:)\n");
sleep(2);
pedagogictime(&tv);
printf("tv_sec:%ld\n",tv.tv_sec);
printf("tv_nsec:%ld\n",tv.tv_usec);
}
阅读(1307) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~