printkd
#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的更多相关文章
随机推荐
- ICT638 Mobile and App Development
Assessment Cover SheetStudent ID CohortStudent NameProgrammeEnrolledDiploma in Information Technolog ...
- springboot学习源码
springbootTest 学习源码链接 启动前,需要创建数据库表,修改自己的链接配置 create database test01; use test01; CREATE TABLE catego ...
- IDEA快速修复错误快捷键
有的时候在IDEA中编写代码,会出现错误提示,比如需要处理异常 将光标移动到出错,也就是划红线的地方,行首会出现一个小灯泡,点击会出现图二,可以按照提示进行修复
- 搭建rsyslog日志服务器
环境配置 centos7系统 client1:192.168.91.17 centos7系统 master:192.168.91.18 rsyslog客户端配置 1.rsyslog安装 yum ins ...
- Logstash:处理多个input
Logstash:处理多个input Logstash的整个pipleline分为三个部分: input插件:提取数据. 这可以来自日志文件,TCP或UDP侦听器,若干协议特定插件(如syslog或I ...
- express捕获全局异常的三种方法
场景 express的路由里抛出异常后,全局中间件没办法捕获,需要在所有的路由函数里写try catch,这坑爹的逻辑让人每次都要多写n行代码 官方错误捕获中件间代码如下 app.use(functi ...
- Out,ref,params修饰符,可选参数,命名参数
out输出,在调用函数中声明,在被调用函数中赋值: ref在调用函数中赋值,后调用: params修饰符,static double CalculateAverage(params[] values) ...
- WPF控件模板(6)
什么是ControlTemplate? ControlTemplate(控件模板)不仅是用于来定义控件的外观.样式, 还可通过控件模板的触发器(ControlTemplate.Triggers)修改控 ...
- maven 学习---部署基于Maven的war文件到Tomcat
在本教程中,我们将学习如何使用Maven的Tomcat插件打包并部署一个WAR文件到Tomcat(Tomcat的6和7. 要用到工具: Maven 3 Tomcat 6.0.37 Tomcat 7.0 ...
- Scrum冲刺第二篇
一.每日例会 会议照片 成员 昨日已完成的工作 今日计划完成的工作 工作中遇到的困难 陈嘉欣 撰写博客,管理成员提交代码 每日博客,根据队员代码问题更改规范文档安排后续工作 队员提交的代码管理困难 邓 ...