Dragonfly 发表于 2003-5-29 22:53:22

add system call

there is a discussion on mailing list about how to add a system call. i repost my solution here.
---------------------------
I test the add system call today and I believe it is possible to add system
call in module. i think the reason to modify the kernel code is to persist
the system call in kernel only. if u only need to dynamically use it. this is a
way. Wait for comments:

compile the module using a simple makefile, see my post in bbs;
compile the user space code using gcc -o call call.c is enough

------------------kernel module ------------------------------------

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>

#ifndef __NR_addtotal
#define __NR_addtotal 222
#endif

int (*old_sys) (struct pt_regs regs);
extern void *sys_call_table[];

asmlinkage int
sys_myfunc (int myinput)
{
printk ("1. jjww printk from kernel %d\n", myinput);
return myinput;
}

int
hello_init (void)
{
old_sys = sys_call_table[__NR_addtotal];
sys_call_table[__NR_addtotal] = sys_myfunc;
return 0;
}

void
hello_clean (void)
{
if (sys_call_table[__NR_addtotal] != sys_myfunc)
    {
      printk ("!!WARNING!! Another module hook\n");
    }
sys_call_table[__NR_addtotal] = old_sys;
}

module_init (hello_init);
module_exit (hello_clean);

MODULE_LICENSE ("GPL");

----------------------------user space code---------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <linux/unistd.h>

#ifndef __NR_addtotal
#define __NR_addtotal   222
#endif

_syscall1 (int, addtotal, int, num)
   int main ()
{
int i, j;

do
    printf ("Please input a number\n");
while (scanf ("%d", &i) == EOF);
if ((j = addtotal (i)) == -1)
    {
      printf ("Error occurred in syscall-addtotal();\n");
      exit (1);
    }
printf ("return %d \n", j);
}

jjww 发表于 2003-5-30 11:05:42

喔喔,谢谢!!
有一点,我在编译的时候总是说
myfunc2.c:12: syntax error before "int"
我想是由于asmlinkage定义的头文件没有包括。
我是加了#include <linux/linkage.h>
才行的。

Dragonfly 发表于 2003-5-30 11:09:57

o, hehe, minor issue. my gcc is more powerful than u?:wink::wink:

wzy123 发表于 2004-5-18 20:31:29

我怎么试了还是不行呢,我的内核是2.4.20-8
谢谢!

jinzhcheng 发表于 2004-5-19 09:28:41

Re: add system call

there is a discussion on mailing list about how to add a system call. i repost my solution here.
---------------------------
I test the add system call today and I believe it is possible to add system
call in module. i think the reason to modify the kernel code is to persist
the system call in kernel only. if u only need to dynamically use it. this is a
way. Wait for comments:

compile the module using a simple makefile, see my post in bbs;
compile the user space code using gcc -o call call.c is enough


老大,你的makefile在哪里阿?我没有找到,给个链接吧 :oops:
多谢!

wzy123 发表于 2004-5-19 11:30:03

是呀,以前的讨论我没看见,能不能再讲一下后给个链接呢,在内核里加系统调用时,是
2.4.18版本的
谢谢了
页: [1]
查看完整版本: add system call