Read and write files in the Linux kernel 2

Source: Internet
Author: User
Tags sprintf

1, Kernel code

#include <linux/kernel.h>#include<linux/init.h>#include<linux/types.h>#include<linux/spinlock.h>#include<linux/blkdev.h>#include<linux/module.h>#include<linux/fs.h>#include<linux/errno.h>#include<linux/mm.h>#include<linux/cdev.h>#include<linux/sched.h>#defineIo_cmd_len 1024#defineChar_dev_name "Klog"typedefstruct {    intfile_id;//Todo    intRw_pos;//Todo    intCmd_type; intBuff_len; CharIo_buff[io_cmd_len- -];} Klog_cmd_info;#if1structFile *KLOG_FP =null;loff_t Klog_pos=0;Static intUser_cmd_proc (Char*user_cmd,Char*out_str) {Klog_cmd_info*klog_cmd = (Klog_cmd_info *) User_cmd; Klog_cmd_info*klog_ack = (Klog_cmd_info *) Out_str; if(Klog_cmd->cmd_type = =1) {//Open File        if(KLOG_FP! =null) filp_close (KLOG_FP, NULL); KLOG_FP= Filp_open (Klog_cmd->io_buff, O_RDWR | O_creat | O_trunc,0644); if(Is_err (KLOG_FP)) {PRINTK ("Filp_open error \ n"); return-1; } Klog_pos=0; sprintf (Klog_ack->io_buff,"Filp_open ok\n"); }        if(Klog_cmd->cmd_type = =2) {//Write Filemm_segment_t Old_fs; Old_fs=Get_fs (); Set_fs (Kernel_ds); //To check whyKlog_pos + = Vfs_write (KLOG_FP, Klog_cmd->io_buff, Klog_cmd->buff_len, &Klog_pos);        Set_fs (OLD_FS); sprintf (Klog_ack->io_buff,"Vfs_write ok\n"); }    if(Klog_cmd->cmd_type = =3) {//Close Filefilp_close (KLOG_FP, NULL); KLOG_FP=NULL; sprintf (Klog_ack->io_buff,"filp_close ok\n"); }        return 0;}#endif#if1intMem_open (structInode *inode,structFile *Filp) {      return 0; }    intMem_release (structInode *inode,structFile *Filp) {      return 0; }  CharUser_cmd[io_cmd_len] = {0};CharOut_str[io_cmd_len] = {0};intMem_ioctl (structFile *file, unsignedintCMD, unsignedLongArg) {PRINTK ("Mem_ioctl:%d \ n", CMD); if(Copy_from_user (User_cmd, (int*) arg, Io_cmd_len))return-Efault;        User_cmd_proc (User_cmd, OUT_STR); if(Copy_to_user (int*) arg, OUT_STR, Io_cmd_len))return-Efault; return 0;}Static intMem_major =0;struct class*pclass =NULL; structCdev My_dev; Static Const structFile_operations Mem_fops ={. Owner=this_module,. Unlocked_ioctl=mem_ioctl,. Open=Mem_open,. Release=Mem_release,}; Static intMemdev_init (void)  {      intresult; dev_t Devno= MKDEV (Mem_major,0); if(Mem_major) {/*static application Device number*/result= Register_chrdev_region (Devno,2, Char_dev_name); } Else{/*dynamically assign device numbers*/result= Alloc_chrdev_region (&devno,0,2, Char_dev_name); Mem_major=MAJOR (DEVNO); }       if(Result <0) {PRINTK ("Alloc_chrdev failed!\n"); returnresult; } cdev_init (&my_dev, &mem_fops); My_dev.owner=This_module; My_dev.ops= &Mem_fops; Cdev_add (&my_dev, MKDEV (Mem_major,0),2);/*Number of Devices 2*/Pclass=class_create (This_module, char_dev_name); if(Is_err (Pclass)) {PRINTK ("class_create failed!\n"); Gotofailed;      } device_create (Pclass, NULL, DEVNO, NULL, char_dev_name); return 0; Failed:cdev_del (&My_dev); Unregister_chrdev_region (Devno,1); returnresult; }    Static voidMemdev_exit (void) {Device_destroy (Pclass, MKDEV (Mem_major,0));        Class_destroy (Pclass); Cdev_del (&My_dev); Unregister_chrdev_region (MKDEV (Mem_major,0),2); }  #endifModule_author ("Derek Yi"); Module_license ("GPL");  Module_init (Memdev_init);  Module_exit (Memdev_exit); 

2, test the code

#include <stdio.h>#include<fcntl.h>#include<stdlib.h>#include<string.h>#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<sys/ioctl.h>#include<fcntl.h>#include<errno.h>#include<signal.h>#include<sys/syscall.h>#defineIo_cmd_len 1024typedefstruct {    intfile_id;//Todo    intRw_pos;//Todo    intCmd_type; intBuff_len; CharIo_buff[io_cmd_len- -];} Klog_cmd_info; Klog_cmd_info Cmd_info;intMain () {intFD; intRET =0; FD= Open ("/dev/klog", O_RDWR); if(FD <0) {printf ("Open Memdev wrong! \ n"); return 0; } sprintf (Cmd_info.io_buff,"/home/derek/klog.txt"); Cmd_info.cmd_type=1; RET= IOCTL (FD,0, &cmd_info); printf ("open:ret=%d rdata:%s\n", ret, cmd_info.io_buff);  for(inti =0; I <Ten; i++) {sprintf (Cmd_info.io_buff,"%d: ==============================\n", i); Cmd_info.buff_len=strlen (Cmd_info.io_buff); Cmd_info.cmd_type=2; RET= IOCTL (FD,0, &cmd_info); printf ("read:ret=%d rdata:%s\n", ret, cmd_info.io_buff); } Cmd_info.cmd_type=3; RET= IOCTL (FD,0, &cmd_info); printf ("close:ret=%d rdata:%s\n", ret, cmd_info.io_buff);    Close (FD); return 0;}

Read and write files in the Linux kernel 2

Related Article

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.