linux How to operate files in kernel -- use get_fs() and set_fs(KERNEL_DS)

本文介绍了如何在Linux内核中使用get_fs()和set_fs(KERNEL_DS)来操作文件,特别是在进行系统调用时解决内核空间与用户空间权限的问题。通过示例代码展示了打开、写入和关闭文件的过程,并解释了get_fs()和set_fs()的作用,以及它们如何帮助内核安全地使用系统调用。
摘要由CSDN通过智能技术生成

 
#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>
 
#defineMY_FILE"/root/LogFile"
 
charbuf[128];
structfile*file=NULL;
 
staticint__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);
return0;
}
 
sprintf(buf,"%s","The Messages.");
 
old_fs=get_fs();
set_fs(KERNEL_DS);
file->f_op-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值