免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 7801 | 回复: 31

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

论坛徽章:
0
发表于 2006-04-13 13:41 |显示全部楼层
我在内核中加了几个函数,要输出一些数据到文件中,以便处理,请问内核中提供了输出到文件的函数了么?谢谢!

论坛徽章:
0
发表于 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
发表于 2006-04-13 14:21 |显示全部楼层
谢谢斑竹!!!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2006-04-13 14:36 |显示全部楼层
我给你个打印在屏幕上的.

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2006-04-13 14:38 |显示全部楼层

  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/sched.h>
  5. #include <linux/tty.h>
  6. MODULE_LICENSE("GPL");
  7. MODULE_AUTHOR("mq110");
  8. static void print_string(char *str)
  9. {
  10.     struct tty_struct *my_tty;
  11.     my_tty = current->signal->tty;
  12.     if (my_tty != NULL)
  13.     {
  14.         my_tty->driver->write(my_tty,0,str,strlen(str));
  15.         my_tty->driver->write(my_tty,0,"\015\013",2);
  16.     }
  17. }
  18. static int __init print_string_init(void)
  19. {
  20.     print_string("Hello world!");
  21.     return 0;
  22. }
  23. static void __exit print_string_exit(void)
  24. {
  25.     print_string("Goodbye world!");
  26. }
  27. module_init(print_string_init);
  28. module_exit(print_string_exit);
复制代码


我一般用putty登陆 编写kernel module. printk信息都存在/var/log/message里了.~

用这个程序就能显示在屏幕上了.你可以把print_string 符号导出来.

论坛徽章:
0
发表于 2006-04-13 15:30 |显示全部楼层
直接用PRINTK,然后在MESSAGES中选出来多好。

自己写的使用范围小(加入内核的函数会被其它函数调用,大部分情况不是mod_init直接调用的)。死机的情况多。printk则好多了。

论坛徽章:
0
发表于 2006-04-13 17:14 |显示全部楼层
修改一下/etc/syslog.conf 文件
#kern.*       /dev/console

你打印的东西可能是某个级别的信息。比如说debug,这用printk 可以控制 。
那么就写程
kern.debug /var/log/kern_debug.log

-------------------------
printk(KERN_ALERT "Hello, world\n");
对应
/etc/syslog.conf 中的
kern.alert                      /kernel.txt

实验成功,修改后要执行
server syslogd restart 重启日志服务。
此方法等于用日志服务帮你做这个事情。该信息用
dmesg 命令也可以看到。

[ 本帖最后由 maluyao 于 2006-4-13 17:31 编辑 ]

论坛徽章:
0
发表于 2006-04-20 14:17 |显示全部楼层
原帖由 思一克 于 2006-4-13 15:30 发表
直接用PRINTK,然后在MESSAGES中选出来多好。

自己写的使用范围小(加入内核的函数会被其它函数调用,大部分情况不是mod_init直接调用的)。死机的情况多。printk则好多了。


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

情况时好时坏,有时一加载钩子函数就死机,有时多刷几次163之类的也死机。。。。

这个死机问题有没有解决的方法阿?谢谢

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


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

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


你hook的是什么函数?

论坛徽章:
0
发表于 2006-04-20 14:42 |显示全部楼层
自己写的,贴上来,个个提点意见了。。。。

#define __KERNEL__
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h> //NIPQUAD()
#include <linux/netdevice.h> //struct net_device
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/in.h> //IPPROTO_TCP


static struct nf_hook_ops nfho;


static char *drop_if = "lo";


unsigned int hook_func(unsigned int hooknum,
                       struct sk_buff **skb,
                       const struct net_device *in,
                       const struct net_device *out,
                       int (*okfn)(struct sk_buff *))
{
  if (strcmp(in->name, drop_if) != 0) {

        static const char nulldevname[IFNAMSIZ]={0};
        struct sk_buff *sb = *skb;

        static unsigned int target_ip = 0;
        static unsigned short target_port = 0;
       
        static unsigned int source_ip = 0;
            static unsigned short source_port = 0;
       
        static char *in_dev_name = "";
        static char *out_dev_name = "";

        struct file *filp;

        struct iphdr *ip;
        struct tcphdr *tcp;

        ip = sb->nh.iph;
        tcp = (struct tcphdr *)(sb->data + (sb->nh.iph->ihl * 4));

        in_dev_name = in?in->name:nulldevname;
        out_dev_name = out?out->name:nulldevname;
        source_ip = ip->saddr;
        target_ip = ip->daddr;
        target_port = ((((tcp->dest) >> 8 ) & 0xff )|(((tcp->dest) & 0xff) << 8));
        source_port = ((((tcp->source) >> 8 ) & 0xff )|(((tcp->source) & 0xff) << 8));


        if ((filp = klib_fopen("/root/logfile", O_APPEND|O_WRONLY, S_IRUSR | S_IWUSR)) == NULL) {
        printk("Can't open file\n");
        return NF_ACCEPT;
        }
       
               
        if(ip->protocol == IPPROTO_TCP)
        {
          klib_fprintf(filp,"Accepted TCP packet in_dev_name:%s out_dev_name:%s MAC:", in_dev_name, out_dev_name);
           if ((sb)->dev && (sb)->dev->hard_header_len && (sb)->mac.raw != (void*)ip) {
             int i;
             unsigned char *p = (sb)->mac.raw;
             for (i = 0; i < (sb)->dev->hard_header_len; i++,p++)
                 klib_fprintf(filp,"%02x%c", *p,
                        i==(sb)->dev->hard_header_len - 1
                        ? ' ':':');
           } else
               klib_fputc(' ', filp);

           klib_fprintf(filp,"  source_ip:%u.%u.%u.%u target_ip:%u.%u.%u.%u source_port:%u  target_port:%u\n",
             NIPQUAD(source_ip), NIPQUAD(target_ip), source_port, target_port);
             klib_fprintf(filp,"  TCP\n");
        }else
            klib_fprintf(filp," NOT TCP\n");
          
            

        klib_fclose(filp);
  }
        return NF_ACCEPT;          
}


static int init_func()
{

    nfho.hook     = hook_func;   
    nfho.hooknum  = NF_IP_PRE_ROUTING;
    nfho.pf       = PF_INET;
    nfho.priority = NF_IP_PRI_FIRST;

    nf_register_hook(&nfho);
   
    return 0;
}
   

static void cleanup_func()
{
    nf_unregister_hook(&nfho);
}
module_init(init_func);
MODULE_LICENSE("GPL");
module_exit(cleanup_func);
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP