免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1637 | 回复: 9
打印 上一主题 下一主题

怎么用用户态的系统调用啊. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-14 13:13 |只看该作者 |倒序浏览

  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/syscalls.h>
  4. #include <asm/unistd.h>

  5. static int __init init(void)
  6. {
  7.     char buf[1024]={0};
  8.     sys_getcwd(buf,sizeof(buf));
  9.     printk("<1> %s\n",buf);
  10.     return 0;
  11. }

  12. static void __exit clean(void)
  13. {
  14.     return ;
  15. }

  16. module_init(init);
  17. module_exit(clean);
复制代码


make 以后make -C /lib/modules/2.6.9-5.ELsmp/build M=/home/code/kernel/018 modules
make[1]: Entering directory `/usr/src/kernels/2.6.9-5.EL-smp-i686'
  CC [M]  /home/code/kernel/018/018.o
  Building modules, stage 2.
  MODPOST
*** Warning: "sys_getcwd" [/home/code/kernel/018/018.ko] undefined!
  LD [M]  /home/code/kernel/018/018.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.9-5.EL-smp-i686'

论坛徽章:
0
2 [报告]
发表于 2005-12-14 13:23 |只看该作者
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/syscalls.h>
  5. #include <linux/slab.h>
  6. #include <linux/mm.h>
  7. #include <asm/uaccess.h>
  8. #include <asm/unistd.h>

  9. #define __NR_sys_getcwd1 __NR_getcwd
  10. int errno;

  11. static inline _syscall2(long, sys_getcwd1, char *, s, int , sz);

  12. static int __init init(void)
  13. {
  14.         int sz = 1024;
  15.         char *s = (char *)kmalloc(sz, GFP_KERNEL);
  16.         mm_segment_t old_fs;
  17.         old_fs = get_fs();

  18.         set_fs(KERNEL_DS);
  19.         sys_getcwd1( s,sz);
  20.         set_fs(old_fs);

  21.         printk(KERN_ALERT "CWD is %s\n", s);
  22.         kfree(s);

  23.         return 0;
  24. }

  25. static void __exit fini(void)
  26. {
  27.         printk("getcwd:Bye.");
  28. }

  29. module_init(init);
  30. module_exit(fini);

复制代码

论坛徽章:
0
3 [报告]
发表于 2005-12-14 13:41 |只看该作者
谢谢.能解释一下
#define __NR_sys_getcwd1 __NR_getcwd
int errno;

static inline _syscall2(long, sys_getcwd1, char *, s, int , sz);

是什么意思吗?

论坛徽章:
0
4 [报告]
发表于 2005-12-14 13:43 |只看该作者
哦.我看到了 在asm/unistd.h里有_syscall2的宏定义.

论坛徽章:
0
5 [报告]
发表于 2005-12-14 13:48 |只看该作者
_syscall*我看懂了.
那 #define __NR_sys_getcwd1 __NR_getcwd
这个宏定义是什么意思呢?

论坛徽章:
0
6 [报告]
发表于 2005-12-14 13:49 |只看该作者
sys_getcwd1
和这个宏定义有什么联系呢?多谢指点.

论坛徽章:
0
7 [报告]
发表于 2005-12-14 13:56 |只看该作者
(__NR_##name)
看到了.原来是这样.

那为什么不用 __NR_getcwd呢?
而非要重新定义一个 宏呢?

[ 本帖最后由 ruige 于 2005-12-14 13:57 编辑 ]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
8 [报告]
发表于 2005-12-14 14:14 |只看该作者
不用重新定义 一个新宏 也是可以的.
static inline _syscall2(long, sys_getcwd1, char *, s, int , sz);

第一个参数是char *吧?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
9 [报告]
发表于 2005-12-14 14:59 |只看该作者
刚刚看到 syscalls.h 头里 确实定义的是long型. 返回指针简化 都定义的是long型.可以这样理解吧?

论坛徽章:
0
10 [报告]
发表于 2005-12-14 15:22 |只看该作者
这样做,不要加sys_:

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

_syscall2(long, getcwd, char *, s, int , sz)
.......

getcwd(...);

[ 本帖最后由 caibird3rd 于 2005-12-14 15:24 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP