#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. SampleDateFormat 高并发下导致 线程 Blocked 从而导致 Tomcat 吞吐一直上不去

    SampleDateFormat 高并发下导致 线程 Blocked 从而导致 Tomcat 吞吐一直上不去

  2. RESTful及API设计(原)

    RESTful是一种架构风格,是由Fielding博士在自己的博士论文中提出并详细论述的. 它是用于指导web系统设计的,而指导API设计只是它的一小部分功能而已,如果只用它指导API设计就太大材小用 ...

  3. git 创建标签 tag

    1. git tag <name>就可以打一个新标签 加上-a参数来创建一个带备注的tag,备注信息由-m指定.如果你未传入-m则创建过程系统会自动为你打开编辑器让你填写备注信息. git ...

  4. kali渗透综合靶机(八)--Billu_b0x靶机

    kali渗透综合靶机(八)--Billu_b0x靶机 靶机下载地址:https://download.vulnhub.com/billu/Billu_b0x.zip 一.主机发现 1.netdisco ...

  5. ABAP 新语法-实例讲解

    主要内容 内联声明 构造表达式 内表操作 Open SQL 其他 本文列出了ABAP新语法的一些使用方式,供大家学习参考. 内联声明 代码实现: *&--------------------- ...

  6. SQL Server中INSERT EXEC语句不能嵌套使用(转载)

    问: I have three stored procedures Sp1, Sp2 and Sp3.The first one (Sp1) will execute the second one ( ...

  7. Web Api全局预防Xss攻击

    本文转载自https://www.cnblogs.com/ruanyifeng/p/4739807.html.对第二种过滤方法的代码进行了一些修改和注释,记录一下免得以后忘了.已经测试过,应该可以直接 ...

  8. Python - 条件控制、循环语句 - 第十二天

    Python 条件控制.循环语句 end 关键字 关键字end可以用于将结果输出到同一行,或者在输出的末尾添加不同的字符,实例如下: Python 条件语句是通过一条或多条语句的执行结果(True 或 ...

  9. HDFS基本原理

    一.什么是HDFS HDFS即Hadoop分布式文件系统(Hadoop Distributed Filesystem),以流式数据访问模式来存储超大文件,它和现有的分布式文件系统有很多共同点.但同时, ...

  10. 别忘了在使用MES系统之前,还有关键一步!

    如果你是不熟悉工业自动化领域的专业人士,又或者是从IT或其他背景进入到操作技术(OT)领域的相关人士,那么我相信你不会后悔读到这篇文章. 我们都想做到智能化生产,想将MES系统,APS系统应用到生产过 ...