- 論壇徽章:
- 0
|
由于傳統(tǒng)的添加自己的系統(tǒng)調(diào)用的方法,需要重新編譯內(nèi)核,比較浪費(fèi)時(shí)間,尤其是有錯(cuò)誤時(shí)更是不便調(diào)試.本文將呈現(xiàn)采用模塊動(dòng)態(tài)加載的方法添加自己的系統(tǒng)調(diào)用.
本實(shí)驗(yàn)已在RedHat9.0下試驗(yàn)成功.
模塊代碼為:
/******* syscall.c ***********/
#include
#include
#include
#include
#include
#define __NR_testsyscall 253 //選一個(gè)沒(méi)有用到的系統(tǒng)調(diào)用號(hào)
MODULE_LICENSE("GPL");
extern void *sys_call_table[];
asmlinkage int sys_testsyscall()
{
printk("hello world!\n");
return 0;
}
int init_module()
{
sys_call_table[__NR_testsyscall]=sys_testsyscall;
printk("system call testsyscall() loaded success\n");
printk("sys_call_table addr:0x%lx\n",(unsigned long)sys_call_table);
printk("sys_testsyscall() addr:0x%lx\n",(unsigned long) &sys_call_table[__NR_testsyscall]);
printk("n=%ld\n",((unsigned long)&sys_call_table[__NR_testsyscall] -(unsigned long)sys_call_table)/4);
return 0;
}
void cleanup_module()
{
printk("In the exit module!\n");
}
相應(yīng)的測(cè)試程序:
/********** test.c ***************/
#include
#include
#define __NR_testsyscall 253
_syscall0(int,testsyscall)
int main()
{ int i;
i= testsyscall();
printf("i=%d\n",i);
return 0;
}
我所用的Makefile:
INCLUDE=/usr/src/linux/include
CFLAGS=-g -Wall -D__KERNEL__ -DMODULE -I $(INCLUDE)
syscall.o:syscall.c
gcc $(CFLAGS) -c syscall.c -o syscall.o
然后, make生成 syscall.o,再 insmod syscall.o 將其加載到內(nèi)核.
最后編譯test.c,生成可執(zhí)行文件test, 運(yùn)行測(cè)試.
用dmesg查看進(jìn)程希望,若成功輸出: hello world!
則表示實(shí)驗(yàn)成功!
實(shí)驗(yàn)雖然簡(jiǎn)單,但仍可體現(xiàn)采用模塊添加自己的系統(tǒng)調(diào)用的方法.希望對(duì)瀏覽此文的同道中人有所幫助.
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u2/67414/showart_1713090.html |
|