20145209&20145309 Information Security System Design Foundation Experiment report (4)

Source: Internet
Author: User

Experiment steps to read and understand source code
  • The Demo_read,demo_write function completes the drive read-write interface function, the Do_write function realizes the data written by the user in reverse order, reads the transformed data through the reading function. This is just a demonstration of the implementation of the interface and kernel-driven processing of the user's data.

  • Code:

    #Define Device_name"Demo"Static ssize_tDemo_write(struct file *filp,Constchar * buffer, size_t count) {Char drv_buf[];copy_from_user (drv_buf, buffer, count);..}Static ssize_tDemo_read(struct file *filp,Char *buffer,size_t count,loff_t *ppos) {Char drv_buf[];copy_to_user (buffer, drv_buf,count);StaticIntDemo_ioctl(struct Inode *inode,struct file *file,Unsignedint cmd,UnsignedLong arg) {}StaticIntDemo_open(struct Inode *inode,struct file *file) {}StaticIntDemo_release(struct Inode *inode,struct file *filp) {MOD_DEC_USE_COUNT;DPRINTK ("Device release\n");Return0;}Staticstruct File_operations demo_fops = {owner:this_module,write:demo_write, read:demo_read, Ioctl:demo_ioctl,open:demo_ Open,release:demo_release,};#Ifdef CONFIG_DEVFS_FSStaticdevfs_handle_t Devfs_demo_dir, Devfs_demoraw;#endifStaticint __InitDemo_init(void) {int result;#ifdef config_devfs_fsdevfs_demo_dir = Devfs_mk_dir (Null"Demo",NULL);d Evfs_demoraw = Devfs_register (Devfs_demo_dir,"0", Devfs_fl_default,demo_major, Demo_minor, S_IFCHR | S_IRUSR | S_iwusr,&demo_fops,NULL);Elseset_module_owner (&demo_fops); result = Register_chrdev (Demo_major,"SCULLC", &demo_fops);if (Result < 0) return result;< Span class= "Hljs-keyword" >if (demo_major = = 0) demo_major = result; /* dynamic */#ENDIFPRINTK ( Device_name  "initialized\n"); return 0;} static void __exit demo_exit ( void) {Unregister_chrdev (demo_major,  "unloaded\n");} Module_init (Demo_init); Module_exit (demo_exit);           
Open method
    • The Open method provides the ability of the driver to initialize the device to prepare for subsequent device operations, and the open operation generally increments the usage count to prevent the module from being unloaded from the kernel before the file is closed. Increment usage count
    • Check for specific device errors.
    • Initializes the device if it is opened for the first time.
    • Identify the secondary device number and modify the F_op pointer if necessary.
    • Assign and fill in the data in the Filp->private_data.
Compile driver module and test program
    • There are two methods of compiling in Makefile, which can also be compiled using GCC on this machine or cross compiler, which is compiled by the cross compiler.
Test driver
    • If you compile with GCC, you need to set up the device node with the following command, and if you use a cross-compiler, you do not need to establish a device node.
Modify Makefile

Make compilation

Experimental results

Make error, need to create a bin folder

20145209&20145309 Information Security System Design Foundation Experiment report (4)

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.