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的更多相关文章
随机推荐
- torch_12_dataset和dataLoader,Batchnormalization解读
参考博客https://blog.csdn.net/qq_36556893/article/details/86505934 深度学习入门之pytorch https://github.com/L1a ...
- Appium移动自动化测试-----(三)Intellij IDEA + Android SDK + Genymotion Emulator
下载安装Intellij IDEA 略 下载Android SDK http://tools.android-studio.org/index.php/sdk 下载后解压 http://www. ...
- 明解C语言 入门篇 第十一章答案
练习11-1 /* 用指针实现的字符串的改写 */ #include <stdio.h> int main(void) { "; printf("p = \" ...
- poj-3230 Travel
One traveler travels among cities. He has to pay for this while he can get some incomes. Now there a ...
- SqlHelper发布——比你期望的还要多的多(例如比MyBatis-Pagehelper性能更高)
SqlHelper发布——比Mybatis-PageHelper性能更高 起源 前段时间开启了一个新的项目,在选择分页插件时,发现github上很流行的一个是pagehelper,在百度上搜索了一下, ...
- mysql 8.0 group by 不对的问题
select version(),@@sql_mode;SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
- 正式开放 | 阿里云 10 亿级镜像服务正式支持 Helm Charts,云原生交付再加速!
作者 | 阿里巴巴高级开发工程师 谢于宁(予栖) 2018 年 6 月,Helm 正式加入了 CNCF 孵化项目: 2018 年 8 月,据 CNCF 的调研表明,有百分之六十八的开发者选择了 Hel ...
- Zabbix server 更改数据库密码
Zabbix server 更改数据库密码 当我们的 Zabbix server 使用的数据库信息发生了改变,我们需要更改旧配置. 涉及到数据库配置信息的文件有2个,分别是 /etc/zabbix/z ...
- 创建vue3项目
最近准备做一个vue的小项目关于vue3的使用. 首先在vscode全局安装vue脚手架,npm i -g @vue/cli. 然后创建vue项目,vue create mydemo(项目名). 接下 ...
- Python从零开始——字典Dict
一:Python字典知识概览 . 二:字典常见操作 三:字典内置操作函数