#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/namei.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/workqueue.h>

#define BUFSIZE (1024)

static struct file *pFile = NULL;
static char *pBuf = NULL;
static struct delayed_work printkd_work;
static struct workqueue_struct *printkd_workqueue = NULL;

int printkd2sd(const char *buf, int bufLength)
{
    mm_segment_t old_fs;
    if(NULL == buf)
    {
        printk("%s pBuf pFile null\n", __func__);
        ;
    }
    if (IS_ERR(pFile))
    {
        printk("%s pFile null\n", __func__ );
        ;
    }
    old_fs = get_fs();
    set_fs(KERNEL_DS);
    if(bufLength != pFile->f_op->write(pFile, (char *)buf, bufLength, &pFile->f_pos) )
    {
        printk("%s: write err\n", __FUNCTION__);
        set_fs(old_fs);
        ;
    }
    set_fs(old_fs);
    ;
}

int printkd2buf(char *buf, unsigned short len)
{
    ;
    ;
     == len))
    {
        printk("%s(), par err\n");
        ;
    }
    if(len >= BUFSIZE)
    {
        ret = printkd2sd(pBuf, p);
        p = ;
        ret = printkd2sd(buf, len);
    }
     - p) )
    {
        memcpy( (pBuf + p), buf, len);
        p = p + len;
    }
    else
    {
        ret = printkd2sd(pBuf, p);
        memcpy(pBuf, buf, len);
        p = len;
    }
    return ret;
}

static asmlinkage int vscnprintkd(const char *fmt, va_list args)
{
    ];
    int ret;
    unsigned int printed_len;
    printed_len = vscnprintf( printk_buf , (),  fmt,  args);
    ret = printkd2buf(printk_buf,  printed_len );
    return ret;
}

static void  printkd_init_work(struct work_struct *work)
{
    mm_segment_t old_fs;
    pFile = filp_open();
    if (IS_ERR(pFile))
    {
        printk("%s filp_open error\n", __func__ );
        ;
    }
    pBuf = vmalloc(BUFSIZE + );
    if(NULL == pBuf)
    {
        printk("%s vmalloc err\n", __func__);
        goto close_file;
    }
    printkd2buf("printkd log start...\n", sizeof("printkd log start...\n") );
    printk("%s(), ok!\n",  __func__);
    return;

close_file:
    filp_close(pFile, NULL);
    pFile = NULL;
    printk("%s(), err!\n",  __func__);
    queue_delayed_work(printkd_workqueue, &printkd_work, HZ * );
    return;
}

int printkd_init(void )
{
    printkd_workqueue = create_workqueue("printkd_init");
    INIT_DELAYED_WORK(&printkd_work, printkd_init_work);
    queue_delayed_work(printkd_workqueue, &printkd_work, HZ * );
}

int printkd_read_log( char **pBuf)
{
    struct inode *inode = NULL;
    mm_segment_t oldfs;
    char *buf = NULL;
    unsigned ;
    int ret;
    if((NULL == pFile) || (NULL == pBuf) )
    {
        printk("%s pFile pBuf null\n",  __func__);
        ;
    }
    oldfs = get_fs();
    set_fs(KERNEL_DS);
    if (!pFile->f_op)
    {
        printk("%s: File Operation Method Error\n", __FUNCTION__);
        goto __fs_close;
    }
    inode = pFile->f_path.dentry->d_inode;
    if (!inode)
    {
        printk("%s: Get inode from pFile failed\n", __FUNCTION__);
        goto __fs_close;
    }
    file_length = i_size_read(inode->i_mapping->host);
     && (file_length <  * ) ))
    {
        printk("%s file size error\n", __FUNCTION__);
        goto __fs_close;
    }
    buf = vmalloc(file_length +  );
    if (!buf)
    {
        printk("%s alloctation memory failed\n", __FUNCTION__);
        goto __fs_close;
    }
    pFile->f_op->llseek(pFile, , SEEK_SET);
    ret = vfs_read(pFile, buf, file_length, &pFile->f_pos);
    if (ret  != file_length)
    {
        printk("%s: vfs_read error, file_length:%d, ret:%d\n", __FUNCTION__, file_length, ret);
        pFile->f_op->llseek(pFile, , SEEK_END);
        goto free_buf;
    }
    pFile->f_op->llseek(pFile, , SEEK_END);
    printk("%s file_length:0x%x\n", __func__, file_length);
    *pBuf = buf;
    set_fs(oldfs);
    printk("%s(), ok!\n",  __func__);
    return file_length;

free_buf:
    vfree(buf);
__fs_close:
    set_fs(oldfs);
    printk("%s(), error!\n",  __func__);
    ;
}

asmlinkage int printkd(const char *fmt, ...)
{
    va_list args;
    int r;
    va_start(args, fmt);
    r = vscnprintkd(fmt, args);
    va_end(args);
    return r;
}

printkd的更多相关文章

随机推荐

  1. OAuth2.0授权码模式

    OAuth2.0简单说就是一种授权的协议,OAuth2.0在客户端与服务提供商之间,设置了一个授权层(authorization layer).客户端不能直接登录服务提供商,只能登录授权层,以此将用户 ...

  2. python 统计使用技巧

    python 统计使用技巧 # 1.不输入回车获取值 注:需要tty模块配合. fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) ...

  3. Redis学习记录及Jedis代码示例

    文章目录 二.Redis简介 三.Redis安装 1. 下载并解压安装 2. 安装C语言编译环境 3. 修改安装位置 4. 编译安装 5.启动Redis服务器 ①默认启动 ②定制配置项启动 [1]准备 ...

  4. 『大 树形dp』

    大 Description 滑稽树上滑稽果,滑稽树下你和我,滑稽树前做游戏,滑稽多又多.树上有 n 个节点,它们构成了一棵树,每个节点都有一个滑稽值. 一个大的连通块是指其中最大滑稽值和最小滑稽值之差 ...

  5. kali渗透综合靶机(五)--zico2靶机

    kali渗透综合靶机(五)--zico2靶机 靶机地址:https://www.vulnhub.com/series/zico2,137/#modal210download 一.主机发现 1.netd ...

  6. mongodb实现文件存储系统

    前言:这种坑很深呀,要对应mongodb的版本跟php支持的版本,然后,如果要用composer安装第三方的库,一定要一一对应的 正片开始! 开发环境: 系统:window 开发语言:php+apac ...

  7. 网格弹簧质点系统模拟(Spring-Mass System by Fast Method)附源码(转载)

    转载:  https://www.cnblogs.com/shushen/p/5311828.html 弹簧质点模型的求解方法包括显式欧拉积分和隐式欧拉积分等方法,其中显式欧拉积分求解快速,但积分步长 ...

  8. java基于NIO的分散读取文件,然后统一聚合后写入文件

    分散读取:对于一个文件,可以分散的读取数据,可以快速的读取,好比多个线程在分段同时读取: 聚合写入:为了提高效率,一般读取到的数据都是分散的,要快速写入,就需要把分散的数据聚集在一起,然后一块写入到文 ...

  9. 基于WEB的网上购物系统-ssh源码

    基于WEB的网上购物系统主要功能包括:前台用户登录退出.注册.在线购物.修改个人信息.后台商品管理等等.本系统结构如下:(1)商品浏览模块:        实现浏览最新商品        实现按商品名 ...

  10. 【IPHONE开发-OBJECTC入门学习】文件的操作,读写复制文件

    转自:http://blog.csdn.net/java886o/article/details/9041547 FileTools.h FileTools.m #import "FileT ...