In the hardware Abstraction Layer module, we are invoking the open function to open the corresponding device file. For example, in the Hardware Abstraction Layer module Freg developed in the 2.3.2 section, the function Freg_device_open calls the Open function to open the device file/dev/freg.
if (dev->fd = open (Device_name, o_rdwr)) = =-1) {LOGE ("Failed to open DEVICE File/dev/freg--%s."), Strer Ror (errno)); (Dev); Return-efault; 64}
If you do not modify the access rights of the device file/def/freg, the application calls the Freg_device_open function to open the device file/def/freg will fail, and the log output from line 61st can see the following:
Failed to Open/dev/hello--Permission denied.
This means that the current user does not have permission to open the device file/dev/freg. By default, only the root user has access to the system's device files. Since the generic application does not have root privileges, there is no permission to open the device file/dev/freg.
The solution to this problem is to grant access to the device file/dev/freg to other users other than root. We know that in a Linux system, you can modify the access rights of a device file at system startup by using UDEV rules 3. However, the Android system does not implement the UDEV mechanism, so we cannot define UDEV rules to grant access to the device file/dev/freg by other users other than root. However, Android provides an additional uevent mechanism to modify the access rights of the device files when the system is started.
In the System/core/rootdir directory there is a configuration file named Ueventd.rc, we can add the following line to modify the device file/dev/freg access rights.
/dev/freg 0666 Root root
This means that all users can access the device file/dev/freg, which can open the device file/dev/freg and read and write its contents. In this way, other users in the system can call the Freg_device_open function to open the device file/dev/freg, in addition to the root user.
After modifying the ueventd.rc file, you will need to recompile the Android source code project so that the newly modified device file/dev/freg access rights will take effect. Here, we introduce a method that does not have to recompile the Android source code project can make the modified device file/dev/freg access permission to take effect.
When compiling the Android source code project, the file system/core/rootdir/ueventd.rc will be copied to the Out/target/product/generic/root directory, And eventually packaged in the ramdisk.img image file. When the Android system starts, the Ueventd.rc file in the ramdisk.img image file is installed in the root directory of the device, and the INIT process resolves its contents and modifies the access rights of the corresponding device files. Therefore, as long as we can modify the contents of the Ueventd.rc file in the ramdisk.img image file, we can modify the access rights of the device file/dev/freg. The following is a detailed description of how to modify the Ueventd.rc file in the ramdisk.img image file.
1. Unzip the ramdisk.img image file
The image file Ramdisk.img is a gzip file, so we can perform an gunzip command to decompress it.
[Email protected]:~/android$ mv./out/target/product/generic/ramdisk.img./ramdisk.img.gz
[Email protected]:~/android$ gunzip./ramdisk.img.gz
We renamed RAMDISK.IMG to ramdisk.img.gz and then called the Gunzip command to decompress it. The extracted ramdisk.img files are saved in the ~/android directory.
2. Restore the ramdisk.img image file
The extracted ramdisk.img file is an archive file in Cpio4 format, so we can execute the cpio command to de-archive it.
[Email protected]:~/android $ mkdir ramdisk [email protected]:~/android $ CD./ramdisk/[Email Protected]:~/android/ramdi sk$ Cpio-i-F. /ramdisk.img
Files that are released after the archive are saved in the ~/android/ramdisk directory.
3. Modify the Ueventd.rc file
Go to the ~/android/ramdisk directory, locate the Ueventd.rc file, and add the following line to it:
/dev/freg 0666 Root root
This line of content gives all users in the system access to the device file/dev/freg.
4. Repackaging ramdisk.img image files
[email protected]:~/android/ramdisk$ rm-f. /ramdisk.img [email protected]:~/android/ramdisk$ Find. | Cpio-o-H newc >. /ramdisk.img.unzip [email protected]:~/android/ramdisk$ CD. [email protected]:~/android$ gzip-c./ramdisk.img.unzip >./ramdisk.img.gz [email protected]:~/android $ rm-f./ramdisk.img.unzip [email protected] : ~/android$ rm-r./ramdisk [email protected]:~/android$ mv./ramdisk.img.gz./out/target/product/generic/ramdisk.img
In this way, the ueventd.rc file in the ramdisk.img image file after repackaging is modified, and the INIT process is given to all users in the system to access the device file/dev/freg after booting. _______________________________________ 3 Udev is a new feature of the Linux 2.6 kernel to replace the original DEVFS, Linux The system default Device management tool. The udev mechanism runs as a daemon, managing the device files in the/dev directory by listening for the uevent that are issued, including adding or removing device files, modifying access rights to device files, and so on. 4 Cpio is an archive file that contains other files and related information, which can be referenced in http://www.gnu.org/software/cpio/.
[Android] Modify device access rights