亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
1234下一頁
最近訪問板塊 發(fā)新帖
查看: 8347 | 回復: 31
打印 上一主題 下一主題

何把內核中的信息打印到文件 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2006-04-13 13:41 |只看該作者 |倒序瀏覽
我在內核中加了幾個函數,要輸出一些數據到文件中,以便處理,請問內核中提供了輸出到文件的函數了么?謝謝!

論壇徽章:
0
2 [報告]
發(fā)表于 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
3 [報告]
發(fā)表于 2006-04-13 14:21 |只看該作者
謝謝斑竹。!

論壇徽章:
1
榮譽版主
日期:2011-11-23 16:44:17
4 [報告]
發(fā)表于 2006-04-13 14:36 |只看該作者
我給你個打印在屏幕上的.

論壇徽章:
1
榮譽版主
日期:2011-11-23 16:44:17
5 [報告]
發(fā)表于 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
6 [報告]
發(fā)表于 2006-04-13 15:30 |只看該作者
直接用PRINTK,然后在MESSAGES中選出來多好。

自己寫的使用范圍。尤雰群说暮瘮禃黄渌瘮嫡{用,大部分情況不是mod_init直接調用的)。死機的情況多。printk則好多了。

論壇徽章:
0
7 [報告]
發(fā)表于 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

實驗成功,修改后要執(zhí)行
server syslogd restart 重啟日志服務。
此方法等于用日志服務幫你做這個事情。該信息用
dmesg 命令也可以看到。

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

論壇徽章:
0
8 [報告]
發(fā)表于 2006-04-20 14:17 |只看該作者
原帖由 思一克 于 2006-4-13 15:30 發(fā)表
直接用PRINTK,然后在MESSAGES中選出來多好。

自己寫的使用范圍。尤雰群说暮瘮禃黄渌瘮嫡{用,大部分情況不是mod_init直接調用的)。死機的情況多。printk則好多了。


死機。」,我的就是這樣!
我把對文件的操作包了一層,以模塊形式加載,把操作函數導出。
然后注冊了個鉤子函數,使用導出的函數,希望將包的信息寫到指定文件。

情況時好時壞,有時一加載鉤子函數就死機,有時多刷幾次163之類的也死機。。。。

這個死機問題有沒有解決的方法阿?謝謝

論壇徽章:
0
9 [報告]
發(fā)表于 2006-04-20 14:24 |只看該作者
原帖由 tomorrow0530 于 2006-4-20 14:17 發(fā)表


死機!哈哈,我的就是這樣!
我把對文件的操作包了一層,以模塊形式加載,把操作函數導出。
然后注冊了個鉤子函數,使用導出的函數,希望將包的信息寫到指定文件。

情況時好時壞,有時一加載鉤子 ...


你hook的是什么函數?

論壇徽章:
0
10 [報告]
發(fā)表于 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);
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP