linux之家
linux之家-让学习更轻松

你的位置:首页>>Linux编程学习>>何把内核中的信息打印到文件

何把内核中的信息打印到文件

发表时间:2007-9-25 下午 06:07:11浏览次数:

提问:何把内核中的信息打印到文件

我在内核中加了几个函数,要输出一些数据到文件中,以便处理,请问内核中提供了输出到文件的函数了么?谢谢!

解决办法:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/syscalls.h>
#include <asm/unistd.h>
#include <asm/uaccess.h>

#define MY_FILE "/root/LogFile"

char buf[128];
struct file *file = NULL;

 

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


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

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

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


        return 0;
}

static void __exit fini(void)
{
        if(file != NULL)
                filp_close(file, NULL);
}

module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");

读者体会
暂无回复
姓名:
评论内容:
     
热点导读
LINUX + ORACL
Linux系统高负载 My
Linux系统高负载 My
Linux系统高负载 My
Linux系统高负载 My
RHCE实战考试经验
RHCE--实力的最终测试
如何备考RHCE
RHCE应考指南
Linux相关的四个认证
国家Linux技术水平认证
CSIP对其在国家Linu
使用Linux做Radiu
Linux认证基本知识介绍
Oracle数据库开发(二
红帽Linux获美国政府最
Linux操作系统安全配置
如何用Linux Live
物理隔离 Linux如何贯
用SRP建立安全的Linu
Linux安全攻略 了解系
Linux安全攻略 服务器
微软要用数据证明Vista
用LVS在Linux上搭建
Linux操作系统安装VM
下载工具介绍 aMule在
Linux操作系统中安装E
Linux操作系统下网络互
Linux应用问答系列之硬
Linux系统下设置用户登
Copyright 2006-2007 linux77.com Inc. All Rights Reserved
Emai:skyzznj@126.com