免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: xiaozhe2000
打印 上一主题 下一主题

何把内核中的信息打印到文件 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2006-04-13 13:54 |显示全部楼层

  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/fs.h>
  5. #include <linux/string.h>
  6. #include <linux/mm.h>
  7. #include <linux/syscalls.h>
  8. #include <asm/unistd.h>
  9. #include <asm/uaccess.h>

  10. #define MY_FILE "/root/LogFile"

  11. char buf[128];
  12. struct file *file = NULL;



  13. static int __init init(void)
  14. {
  15.         mm_segment_t old_fs;
  16.         printk("Hello, I'm the module that intends to write messages to file.\n");


  17.         if(file == NULL)
  18.                 file = filp_open(MY_FILE, O_RDWR | O_APPEND | O_CREAT, 0644);
  19.         if (IS_ERR(file)) {
  20.                 printk("error occured while opening file %s, exiting...\n", MY_FILE);
  21.                 return 0;
  22.         }

  23.         sprintf(buf,"%s", "The Messages.");

  24.         old_fs = get_fs();
  25.         set_fs(KERNEL_DS);
  26.         file->f_op->write(file, (char *)buf, sizeof(buf), &file->f_pos);
  27.         set_fs(old_fs);


  28.         return 0;
  29. }

  30. static void __exit fini(void)
  31. {
  32.         if(file != NULL)
  33.                 filp_close(file, NULL);
  34. }

  35. module_init(init);
  36. module_exit(fini);
  37. MODULE_LICENSE("GPL");

复制代码

论坛徽章:
0
2 [报告]
发表于 2006-04-20 14:24 |显示全部楼层
原帖由 tomorrow0530 于 2006-4-20 14:17 发表


死机!!哈哈,我的就是这样!
我把对文件的操作包了一层,以模块形式加载,把操作函数导出。
然后注册了个钩子函数,使用导出的函数,希望将包的信息写到指定文件。

情况时好时坏,有时一加载钩子 ...


你hook的是什么函数?

论坛徽章:
0
3 [报告]
发表于 2006-04-20 14:47 |显示全部楼层
netfilter的hook点是在softirq的上下文中, 不可以睡眠(亦即:阻塞当前进程), 读写磁盘文件恰恰是可能导致睡眠的。

记得wheelz斑竹的建议是: 如果你一定要在softirq上下文中读写文件, 那么, 生成一个内核线程来做这件事。

论坛徽章:
0
4 [报告]
发表于 2006-04-20 16:40 |显示全部楼层
printk在哪里都可以用的

论坛徽章:
0
5 [报告]
发表于 2006-04-21 09:51 |显示全部楼层
原帖由 思一克 于 2006-4-20 16:53 发表
to alb,

放有些函数中不可以。比如和schedule()有关的函数,比如要switch context的许多地方,立即死机。
还有些地方(中断),理论上可以用,但实际随机地死机。


以前没遇到过呢, 有空试验下

论坛徽章:
0
6 [报告]
发表于 2006-04-25 15:42 |显示全部楼层

回复 26楼 obrire 的帖子

>>如果没有串口初始化呢?
/dev/console初始化之前,可以用early_printk打印信息。

to huyongzs:
>>gcc -g -o kprinter kernelprinter.c
2.4和2.6编译过程不一样。 如果是前者, 去掉-g试试; 后者的话看精华区的文档吧(我转贴的ibm站点的那篇)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP