#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. 【shell脚本】将三个数字进行升序排序===numSort.sh

    从命令输入三个数字进行升序排序(冒泡排序) 原理:比较两个相邻的元素,将值大的元素交换至右端. 脚本内容: [root@VM_0_10_centos shellScript]# cat numSort ...

  2. pixijs shader贴图扫光效果

    pixijs shader贴图扫光效果 直接贴代码 const app = new PIXI.Application({ transparent: true }); document.body.app ...

  3. UVA 291 The House Of Santa Claus DFS

    题目: In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do you re ...

  4. WPF 中如何变相让 ListBox 宽度(Width) 100%,高度(Height) 100%,从而达到 Filled 的效果

    直接贴代码了: XAML: <Window x:Class="HelloWorld.MainWindow" xmlns="http://schemas.micros ...

  5. 修改linux内核加载顺序

    修改内核启动顺序:1.查看当前系统所有的内核# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/gr ...

  6. Python sorted 函数

    Python sorted 函数 sorted 可以对所有可迭代的对象进行排序操作,sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作.从新排序列表. sorted 语法: ...

  7. yii2.0的学习之旅(一)

    一. 通过composer安装yii2.0项目 *本文是根据您已经安装了composer (1)跳转到项目根目录 cd /xxxx/www (2)下载插件 composer global requir ...

  8. Winforn中设置ZedGraoh的GraphPane恢复到初始比例大小

    场景 Winform中实现ZedGraph中曲线右键显示为中文: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100115292 ...

  9. eclipse启动tomcat警告 [SetPropertiesRule]{Server/Service/Engine/Host/Context}

    解决问题:解决办法是:关闭tomcat,双击eclipse下tomcat服务器,在出来的Tomcat server at localhost页面中找到server options选项,选中其中的选项” ...

  10. Java生鲜电商平台-服务器部署设计与架构

    Java生鲜电商平台-服务器部署设计与架构 补充说明:Java开源生鲜电商平台-服务器部署设计与架构,指的是通过服务器正式上线整个项目,进行正式的运营. 回顾整个章节,我们涉及到以下几个方面: 1. ...