Linux Device Driver Learning (6)-advanced character driver operations [(3) Access Control of device files]

Source: Internet
Author: User
Access control is sometimes critical for the reliability of a device node. This part of content is only modified on the open and release methods, and some check mechanisms are added. Exclusive Device

The most rigid access control method is to allow a device to be opened (exclusive) by a single process at a time, which is the simplest access control for a device driver. The implementation is very simple. For specific code, see the experiment source code! Module Program link: Scullsingle.tar.gzModule test program link: Scullsingle-test.tar.gzThe experiment of the arm9-lab Board is as follows:
[Tekkaman2440@SBC2440V4]#cd /lib/modules/[Tekkaman2440@SBC2440V4]#insmod scullsingle.ko[Tekkaman2440@SBC2440V4]#cat /proc/devicesCharacter devices:  1 mem  2 pty  3 ttyp  4 /dev/vc/0  4 tty  4 ttyS  5 /dev/tty  5 /dev/console  5 /dev/ptmx  7 vcs 10 misc 13 input 14 sound 81 video4linux 89 i2c 90 mtd116 alsa128 ptm136 pts180 usb189 usb_device204 s3c2410_serial252 scullsingle253 usb_endpoint254 rtcBlock devices:  1 ramdisk256 rfd  7 loop 31 mtdblock 93 nftl 96 inftl179 mmc[Tekkaman2440@SBC2440V4]#mknod -m 666 scullsingle c 252 0[Tekkaman2440@SBC2440V4]#cd /tmp/[Tekkaman2440@SBC2440V4]#./scullsingle-test &[Tekkaman2440@SBC2440V4]#open scullsingle is file=3[Tekkaman2440@SBC2440V4]#echo 12345 > /dev/scullsingle-sh: cannot create /dev/scullsingle: Device or resource busy[Tekkaman2440@SBC2440V4]#cat /dev/scullsinglecat: can't open '/dev/scullsingle
Single User Access

When the device owner is enabled for the first time, the user can open the device multiple times and coordinate multiple processes to perform concurrent operations on the device. At the same time, no other user can open it to avoid external interference. This module is changed by using the completion module, which can be used for both function and test convenience. Module Program link: Completion-singleUID.tar.gzModule test program link: Completion-singleUID-test.tar.gzThe experiment of the arm9-lab Board is as follows:
[Tekkaman2440@SBC2440V4]#insmod /lib/modules/singleUID.ko[Tekkaman2440@SBC2440V4]#cat /proc/devicesCharacter devices:  1 mem  2 pty  3 ttyp  4 /dev/vc/0  4 tty  4 ttyS  5 /dev/tty  5 /dev/console  5 /dev/ptmx  7 vcs 10 misc 13 input 14 sound 81 video4linux 89 i2c 90 mtd116 alsa128 ptm136 pts180 usb189 usb_device204 s3c2410_serial252 singleUID253 usb_endpoint254 rtcBlock devices:  1 ramdisk256 rfd  7 loop 31 mtdblock 93 nftl 96 inftl179 mmc[Tekkaman2440@SBC2440V4]#mknod -m 666 /dev/singleUID c 252 0[Tekkaman2440@SBC2440V4]#/tmp/singleUID_testr&[Tekkaman2440@SBC2440V4]#login tekkamanPassword:Set search library path int /etc/profileSet user path in /etc/profileruning /etc/profile ok[Tekkaman2440@SBC2440V4]#/tmp/singleUID_testwopen singleUID code=-1[Tekkaman2440@SBC2440V4]#/tmp/singleUID_testropen singleUID code=-1[Tekkaman2440@SBC2440V4]#exit[Tekkaman2440@SBC2440V4]#/tmp/singleUID_testwwrite code=0[Tekkaman2440@SBC2440V4]#read code=0[1] + Done /tmp/singleUID_testr
Blocking single-user access

Module Program link: completion-singleUIDnb.tar.gz module test program Link: completion-singleUIDnb-test.tar.gzARM9 experiment board experimental phenomenon is:
[Tekkaman2440@SBC2440V4]#insmod /lib/modules/singleUIDnb.ko[Tekkaman2440@SBC2440V4]#cat /proc/devicesCharacter devices:  1 mem  2 pty  3 ttyp  4 /dev/vc/0  4 tty  4 ttyS  5 /dev/tty  5 /dev/console  5 /dev/ptmx  7 vcs 10 misc 13 input 14 sound 81 video4linux 89 i2c 90 mtd116 alsa128 ptm136 pts180 usb189 usb_device204 s3c2410_serial252 singleUIDnb253 usb_endpoint254 rtcBlock devices:  1 ramdisk256 rfd  7 loop 31 mtdblock 93 nftl 96 inftl179 mmc[Tekkaman2440@SBC2440V4]#mknod -m 666 /dev/singleUIDnb c 252 0[Tekkaman2440@SBC2440V4]#/tmp/singleUID_testrnb&[Tekkaman2440@SBC2440V4]#login tekkamanPassword:Set search library path int /etc/profileSet user path in /etc/profileruning /etc/profile ok[Tekkaman2440@SBC2440V4]#/tmp/singleUID_testrnb &[Tekkaman2440@SBC2440V4]#/tmp/singleUID_testwnb &[Tekkaman2440@SBC2440V4]#exit[Tekkaman2440@SBC2440V4]#ps  PID Uid VSZ Stat Command    1 root 1744 S init    2 root SW< [kthreadd]    3 root SWN [ksoftirqd/0]    4 root SW< [watchdog/0]    5 root SW< [events/0]    6 root SW< [khelper]   59 root SW< [kblockd/0]   60 root SW< [ksuspend_usbd]   63 root SW< [khubd]   65 root SW< [kseriod]   77 root SW [pdflush]   78 root SW [pdflush]   79 root SW< [kswapd0]   80 root SW< [aio/0]  707 root SW< [mtdblockd]  708 root SW< [nftld]  709 root SW< [inftld]  710 root SW< [rfdd]  742 root SW< [kpsmoused]  751 root SW< [kmmcd]  769 root SW< [rpciod/0]  778 root 1752 S -sh  779 root 1744 S init  781 root 1744 S init  782 root 1744 S init  783 root 1744 S init  814 root 1336 D /tmp/singleUID_testrnb  816 tekkaman 1336 S /tmp/singleUID_testrnb  817 tekkaman 1336 S /tmp/singleUID_testwnb  818 root 1744 R ps[Tekkaman2440@SBC2440V4]#/tmp/singleUID_testwnb&[Tekkaman2440@SBC2440V4]#read code=0write code=0write code=0read code=0[2] + Done /tmp/singleUID_testwnb[1] + Done /tmp/singleUID_testrnb[Tekkaman2440@SBC2440V4]#ps  PID Uid VSZ Stat Command    1 root 1744 S init    2 root SW< [kthreadd]    3 root SWN [ksoftirqd/0]    4 root SW< [watchdog/0]    5 root SW< [events/0]    6 root SW< [khelper]   59 root SW< [kblockd/0]   60 root SW< [ksuspend_usbd]   63 root SW< [khubd]   65 root SW< [kseriod]   77 root SW [pdflush]   78 root SW [pdflush]   79 root SW< [kswapd0]   80 root SW< [aio/0]  707 root SW< [mtdblockd]  708 root SW< [nftld]  709 root SW< [inftld]  710 root SW< [rfdd]  742 root SW< [kpsmoused]  751 root SW< [kmmcd]  769 root SW< [rpciod/0]  778 root 1752 S -sh  779 root 1744 S init  781 root 1744 S init  782 root 1744 S init  783 root 1744 S init  820 root 1744 R ps
Copy the device when openAnother technology of access control is to create different private copies of devices based on the conditions on. This is only possible if the device is not bound to a hardware entity. In/dev/tty, similar technologies are used internally to present a "scene" for different/dev entry points of its processes ". This type of access control is rare, but this implementation shows that the kernel code can easily change the running environment of the application, similar to the concept of virtual machines in windows. I modified the example in the book to implement different private copies for different users to use scull, which facilitates testing on the arm9-lab board. The source code of this experiment requires some knowledge of the Linux linked list (in chapter 11th). The next article will introduce it. Module Program link: Scullcloned.tar.gzThe experiment of the arm9-lab Board is as follows:
[Tekkaman2440@SBC2440V4]#cd /lib/modules/[Tekkaman2440@SBC2440V4]#insmod scullcloned.ko[Tekkaman2440@SBC2440V4]#cat /proc/devicesCharacter devices:  1 mem  2 pty  3 ttyp  4 /dev/vc/0  4 tty  4 ttyS  5 /dev/tty  5 /dev/console  5 /dev/ptmx  7 vcs 10 misc 13 input 14 sound 81 video4linux 89 i2c 90 mtd116 alsa128 ptm136 pts180 usb189 usb_device204 s3c2410_serial252 scullcloned253 usb_endpoint254 rtcBlock devices:  1 ramdisk256 rfd  7 loop 31 mtdblock 93 nftl 96 inftl179 mmc[Tekkaman2440@SBC2440V4]#mknod -m 666 scullcloned c 252 0[Tekkaman2440@SBC2440V4]#echo root > /dev/scullcloned[Tekkaman2440@SBC2440V4]#cat /dev/scullclonedroot[Tekkaman2440@SBC2440V4]#login tekkamanPassword:Set search library path int /etc/profileSet user path in /etc/profileruning /etc/profile ok[Tekkaman2440@SBC2440V4]#cat /dev/scullcloned[Tekkaman2440@SBC2440V4]#echo tekkaman >/dev/scullcloned[Tekkaman2440@SBC2440V4]#cat /dev/scullclonedtekkaman[Tekkaman2440@SBC2440V4]#exit[Tekkaman2440@SBC2440V4]#cat /dev/scullclonedroot
Chapter 6 advanced character driver operations in Linux Device Drivers (version 3rd) are finally completed, with many contents and many problems encountered. However, you can learn a lot in the process of solving the problem, so if you have a problem, don't ask others immediately, try to read the source code to find the problem, not only remember the problem, but also learn much.
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.