Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1912762
  • 博文数量: 383
  • 博客积分: 10011
  • 博客等级: 上将
  • 技术积分: 4061
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-24 18:53
文章分类

全部博文(383)

文章存档

2011年(1)

2010年(9)

2009年(276)

2008年(97)

我的朋友

分类: LINUX

2008-12-08 11:25:53

在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);
}

用dmesg可以看到系统调用的进程号@!
上边希望对大家的内核编程有所帮助!


Makefile参考:
 
PWD = $(shell pwd)
  SRC = /usr/src/linux
  obj-m := hello.o
  module-objs := hello.o

  default:
  make -C $(SRC) SUBDIRS=$(PWD) modules


  clean:
  -rm -f *.o *.ko .*.cmd .*.flags *.mod.c
阅读(911) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~