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

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
1234下一頁(yè)
最近訪問(wèn)板塊 發(fā)新帖
查看: 8340 | 回復(fù): 31
打印 上一主題 下一主題

何把內(nèi)核中的信息打印到文件 [復(fù)制鏈接]

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

論壇徽章:
0
2 [報(bào)告]
發(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");

復(fù)制代碼

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2006-04-13 14:21 |只看該作者
謝謝斑竹!。

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

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
5 [報(bào)告]
發(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);
復(fù)制代碼


我一般用putty登陸 編寫kernel module. printk信息都存在/var/log/message里了.~

用這個(gè)程序就能顯示在屏幕上了.你可以把print_string 符號(hào)導(dǎo)出來(lái).

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2006-04-13 15:30 |只看該作者
直接用PRINTK,然后在MESSAGES中選出來(lái)多好。

自己寫的使用范圍。尤雰(nèi)核的函數(shù)會(huì)被其它函數(shù)調(diào)用,大部分情況不是mod_init直接調(diào)用的)。死機(jī)的情況多。printk則好多了。

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2006-04-13 17:14 |只看該作者
修改一下/etc/syslog.conf 文件
#kern.*       /dev/console

你打印的東西可能是某個(gè)級(jí)別的信息。比如說(shuō)debug,這用printk 可以控制 。
那么就寫程
kern.debug /var/log/kern_debug.log

-------------------------
printk(KERN_ALERT "Hello, world\n");
對(duì)應(yīng)
/etc/syslog.conf 中的
kern.alert                      /kernel.txt

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

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

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

自己寫的使用范圍。尤雰(nèi)核的函數(shù)會(huì)被其它函數(shù)調(diào)用,大部分情況不是mod_init直接調(diào)用的)。死機(jī)的情況多。printk則好多了。


死機(jī)。」,我的就是這樣!
我把對(duì)文件的操作包了一層,以模塊形式加載,把操作函數(shù)導(dǎo)出。
然后注冊(cè)了個(gè)鉤子函數(shù),使用導(dǎo)出的函數(shù),希望將包的信息寫到指定文件。

情況時(shí)好時(shí)壞,有時(shí)一加載鉤子函數(shù)就死機(jī),有時(shí)多刷幾次163之類的也死機(jī)。。。。

這個(gè)死機(jī)問(wèn)題有沒(méi)有解決的方法阿?謝謝

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


死機(jī)!!哈哈,我的就是這樣!
我把對(duì)文件的操作包了一層,以模塊形式加載,把操作函數(shù)導(dǎo)出。
然后注冊(cè)了個(gè)鉤子函數(shù),使用導(dǎo)出的函數(shù),希望將包的信息寫到指定文件。

情況時(shí)好時(shí)壞,有時(shí)一加載鉤子 ...


你hook的是什么函數(shù)?

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2006-04-20 14:42 |只看該作者
自己寫的,貼上來(lái),個(gè)個(gè)提點(diǎn)意見(jiàn)了。。。。

#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);
您需要登錄后才可以回帖 登錄 | 注冊(cè)

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

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP