QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1801|回复: 5

add system call

[复制链接]
发表于 2003-5-29 22:53:22 | 显示全部楼层 |阅读模式
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]
发表于 2003-5-30 11:05:42 | 显示全部楼层
喔喔,谢谢!!
有一点,我在编译的时候总是说
myfunc2.c:12: syntax error before "int"
我想是由于asmlinkage定义的头文件没有包括。
我是加了#include <linux/linkage.h>
才行的。
回复

使用道具 举报

 楼主| 发表于 2003-5-30 11:09:57 | 显示全部楼层
o, hehe, minor issue. my gcc is more powerful than u?    
回复

使用道具 举报

发表于 2004-5-18 20:31:29 | 显示全部楼层
我怎么试了还是不行呢,我的内核是2.4.20-8
谢谢!
回复

使用道具 举报

发表于 2004-5-19 09:28:41 | 显示全部楼层

Re: add system call

[quote:fdf5fd5f9f="Dragonfly"]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
[/code][/quote]

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

使用道具 举报

发表于 2004-5-19 11:30:03 | 显示全部楼层
是呀,以前的讨论我没看见,能不能再讲一下后给个链接呢,在内核里加系统调用时,是
2.4.18版本的
谢谢了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-16 08:35 , Processed in 0.043346 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表