|
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 ------------------------------------
[code:1]
#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");
[/code:1]
----------------------------user space code---------------------------------
[code:1]
#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);
}
[/code:1] |
|