Linux内核中读写文件数据的方法
来源:互联网 发布:人工智能程序设计 编辑:程序博客网 时间:2024/05/02 04:34
有时候需要在Linuxkernel--大多是在需要调试的驱动程序--中读写文件数据。在kernel中操作文件没有标准库可用,需要利用kernel的一些函数,这些函数主要有: filp_open() filp_close(), vfs_read()vfs_write(),set_fs(),get_fs()等,这些函数在linux/fs.h和asm/uaccess.h头文件中声明。下面介绍主要步骤:
1. 打开文件
filp_open()在kernel中可以打开文件,其原形如下:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/syscalls.h>
#include <asm/unistd.h>
#include <asm/uaccess.h>
#define MY_FILE "/root/LogFile"
char buf[128];
struct file *file = NULL;
static int __init init(void)
{
mm_segment_t old_fs;
printk("Hello, I'm the module that intends to write messages to file.\n");
if(file == NULL)
file = filp_open(MY_FILE, O_RDWR | O_APPEND | O_CREAT, 0644);
if (IS_ERR(file)) {
printk("error occured while opening file %s, exiting...\n", MY_FILE);
return 0;
}
sprintf(buf,"%s", "The Messages.");
old_fs = get_fs();
set_fs(KERNEL_DS);
file->f_op->write(file, (char *)buf, sizeof(buf), &file->f_pos);
set_fs(old_fs);
filp_close(file, NULL);
file = NULL;
return 0;
}
static void __exit fini(void)
{
if(file != NULL)
filp_close(file, NULL);
}
module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件数据的方法
- Linux内核中读写文件/设备的实现方法
- Linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- 面向对象设计--提升抽象层次
- 室内路径分析——C/S B/S端三维路径分析模型比较
- hdu2028 最小公倍数
- Boost学习之正则表达式--regex
- mysql查询今天、昨天、7天、近30天、本月、上一月 数据
- Linux内核中读写文件数据的方法
- (源码)Android综合案例(经典)
- getHibernateTemplate().find方法详解
- 缓存
- 如何提高代码质量
- SAFEARRAY使用
- 导致代码重复的原因
- 如何在可执行程序中嵌入资源文件(linux环境)
- 关注点分离