Read and write to files in the file system during Linux kernel driver loading

Source: Internet
Author: User

Utils.h file:

#ifndef __utils_h__ #define __utils_h__void a2f (Char *s); #endif

UTILS.C file:

#include <linux/fs.h>#include<asm/segment.h>#include<asm/uaccess.h>#include<linux/buffer_head.h>#include<linux/string.h>#include"Utils.h"#defineFile_path "/hzh"Static structFile *FP;Static intopen_it () {if(FP)return 0;    mm_segment_t Oldfs; intErr =0; Oldfs=Get_fs ();    Set_fs (Get_ds ()); FP= Filp_open (File_path, o_creat| o_rdwr| O_append,0);    Write to File Set_fs (OLDFS) in append mode; if(Is_err (FP)) {ERR=Ptr_err (FP); return 1; }    return 0;}Static voidclose_it () {if(!FP)return; Filp_close (FP,0); FP=0;}Static intRead_it (unsignedLong LongOffset, unsignedChar*data, unsignedintsize) {    if(!FP)return 0;    mm_segment_t Oldfs; intret; Oldfs=Get_fs ();    Set_fs (Get_ds ()); RET= Vfs_read (FP, data, size, &offset);    Set_fs (OLDFS); returnret;}Static intWrite_it (unsignedLong LongOffset, unsignedChar*data, unsignedintsize) {    if(!FP)return 0;    mm_segment_t Oldfs; intret; Oldfs=Get_fs ();    Set_fs (Get_ds ()); RET= Vfs_write (FP, data, size, &offset);    Set_fs (OLDFS); returnret;}Static intsync_it () {if(!FP)return 0; Vfs_fsync (FP,0); return 0;}voidA2F (Char*s) {if(Open_it ())return; intLen =strlen (s); Write_it (0, S, Len); Write_it (0,"\ r \ n",2);    Sync_it (); //close_it ();}

Read and write to files in the file system during Linux kernel driver loading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.