星期六, 11月 28, 2009

Ubuntu中加入System call

首先在/usr/src/linux/kernel/sys.c中加入想要的syscall如下

/* adding helloworld system call */
#include

asmlinkage int sys_helloworld() {
printk(KERN_EMERG "Hello World!\n");
return 0;
}

當然也可以自行新增.c檔案,但是要記得#include
說明一下,asmlinkage表示我們的程式碼是用c寫的,而非c++
printk會把string放到/var/log/syslog中,
KERN_EMERG表示此log等級是緊急了(當然是在此亂打的)
然後還有3個檔案要修改的分別是

/usr/src/linux/arch/x86/include/asm/unistd_32.h中加上
#define __NR_helloworld 337
後面的號碼要記得看最後一個syscall號碼在加上1,
此號碼用來定義syscall的唯一編號

/usr/src/linux/arch/x86/include/asm/syscalls.h
在這些註解後面
/* X86_32 only */
/* kernel/sys.c */
加上
asmlinkage int sys_helloworld();
用來註冊system call

/usr/src/linux/arch/x86/kernel/syscall_table_32.S
加上
.long sys_helloworld

然後重新編譯核心
就可以來測試囉!

#include
#include
#include

#define __NR_helloworld 337 /* or whatever you set it in unistd.h */

int helloworld() {
return (int) syscall(__NR_helloworld);
};

main () {
printf("The return code from the helloworld system call is %d\n", helloworld());
}


此文章參考自:http://macboypro.wordpress.com/2009/05/15/adding-a-custom-system-call-to-the-linux-os/

沒有留言: