**************************************** **************************************** **************************************** ***
Author: EasyWave time: 2012.08.01
Category: Android source code analysis statement: reprinted. Please keep the link
NOTE: If any error occurs, please correct it. These are my Learning Log articles ......
**************************************** **************************************** **************************************** ***
In my blogLearning Based on goldfish and android2.3.5: the startup mechanism of Android [2]In section, we introduce the uevent event mechanism, this blog post will give a more in-depth analysis of the uevent mechanism and how android detects the hotplug event of the device through the uevent mechanism through the data transmitted by the kernel. At the same time, we also analyzed andorid_ids. Let's take out the get_android_id () function and analyze it. The Code is as follows:
Static int get_android_id (const char * id) {unsigned int I; for (I = 0; I <ARRAY_SIZE (android_ids); I ++) // find the android_ids table if (! Strcmp (id, android_ids [I]. name) // compares name return android_ids [I]. aid in android_ids; // returns aid return 0 in the andorid_ids table ;}
Andorid_ids is in the android_filesystem_config.h file, and the specific location is in andorid2.3.5/system/core/include/private. The specific code is as follows:
struct android_id_info { const char *name; unsigned aid;};static const struct android_id_info android_ids[] = { { "root", AID_ROOT, }, { "system", AID_SYSTEM, }, { "radio", AID_RADIO, }, { "bluetooth", AID_BLUETOOTH, }, { "graphics", AID_GRAPHICS, }, { "input", AID_INPUT, }, { "audio", AID_AUDIO, }, { "camera", AID_CAMERA, }, { "log", AID_LOG, }, { "compass", AID_COMPASS, }, { "mount", AID_MOUNT, }, { "wifi", AID_WIFI, }, { "dhcp", AID_DHCP, }, { "adb", AID_ADB, }, { "install", AID_INSTALL, }, { "media", AID_MEDIA, }, { "nfc", AID_NFC, }, { "shell", AID_SHELL, }, { "cache", AID_CACHE, }, { "diag", AID_DIAG, }, { "net_bt_admin", AID_NET_BT_ADMIN, }, { "net_bt", AID_NET_BT, }, { "sdcard_rw", AID_SDCARD_RW, }, { "vpn", AID_VPN, }, { "keystore", AID_KEYSTORE, }, { "usb", AID_USB, }, { "gps", AID_GPS, }, { "inet", AID_INET, }, { "net_raw", AID_NET_RAW, }, { "net_admin", AID_NET_ADMIN, }, { "misc", AID_MISC, }, { "nobody", AID_NOBODY, },};#define android_id_count \ (sizeof(android_ids) / sizeof(android_ids[0]))
Macro defines the following code:
/* This is the master Users and Groups config for the platform.** DO NOT EVER RENUMBER.*/#define AID_ROOT 0 /* traditional unix root user */#define AID_SYSTEM 1000 /* system server */#define AID_RADIO 1001 /* telephony subsystem, RIL */#define AID_BLUETOOTH 1002 /* bluetooth subsystem */#define AID_GRAPHICS 1003 /* graphics devices */#define AID_INPUT 1004 /* input devices */#define AID_AUDIO 1005 /* audio devices */#define AID_CAMERA 1006 /* camera devices */#define AID_LOG 1007 /* log devices */#define AID_COMPASS 1008 /* compass device */#define AID_MOUNT 1009 /* mountd socket */#define AID_WIFI 1010 /* wifi subsystem */#define AID_ADB 1011 /* android debug bridge (adbd) */#define AID_INSTALL 1012 /* group for installing packages */#define AID_MEDIA 1013 /* mediaserver process */#define AID_DHCP 1014 /* dhcp client */#define AID_SDCARD_RW 1015 /* external storage write access */#define AID_VPN 1016 /* vpn system */#define AID_KEYSTORE 1017 /* keystore subsystem */#define AID_USB 1018 /* USB devices */#define AID_GPS 1021 /* GPS daemon */#define AID_UNUSED1 1022 /* deprecated, DO NOT USE */#define AID_RFU1 1023 /* RFU */#define AID_RFU2 1024 /* RFU */#define AID_NFC 1025 /* nfc subsystem */#define AID_SHELL 2000 /* adb and debug shell user */#define AID_CACHE 2001 /* cache access */#define AID_DIAG 2002 /* access to diagnostic resources *//* The 3000 series are intended for use as supplemental group id's only. * They indicate special Android capabilities that the kernel is aware of. */#define AID_NET_BT_ADMIN 3001 /* bluetooth: create any socket */#define AID_NET_BT 3002 /* bluetooth: create sco, rfcomm or l2cap sockets */#define AID_INET 3003 /* can create AF_INET and AF_INET6 sockets */#define AID_NET_RAW 3004 /* can create raw INET sockets */#define AID_NET_ADMIN 3005 /* can configure interfaces and routing tables. */#define AID_MISC 9998 /* access to misc storage */#define AID_NOBODY 9999#define AID_APP 10000 /* first app user */
The above are all partial excerpts,If you want to understand the code, you must have the source code of android2.3.5 at hand. Otherwise, you still cannot understand the code.. Do you still remember this function? Void set_device_permission (int nargs, char ** args), view my previous blog. Here, only the Code related to the above section is extracted, as follows:
Ret = get_android_id (args [2]); // obtain the name defined by android_id. For details, see the android_ids table. If (ret <0) {ERROR ("invalid uid '% s' \ n", args [2]); free (tmp); return;} uid = ret; // set the user id ret = get_android_id (args [3]); // obtain the id defined by android_id. For details, see the android_ids table. if (ret <0) {ERROR ("invalid gid '% s' \ n", args [3]); free (tmp); return;} gid = ret; // set the group id add_dev_perms (name, attr, perm, uid, gid, prefix); // Add it to the linked list.
To/dev/null 0666RootRoot to analyze it. How can we get it?Uid
Ret = get_android_id (args [2]); this is the third item in the front/dev/null, that is, the red bold part. After matching with the root, the AID_ROOT is read.
Use/dev/null 0666 rootRootLet's analyze it. How can we get it?Gid
Ret = get_android_id (args [3]); this is the fourth item in the previous/dev/null, that is, the blue bold part. After matching with the root, the AID_ROOT is read.
Now let's take a look at the code that ueventd_main () has not been fully analyzed. The source code analysis continues. Of course, first describe the source code location: In Andorid2.3.5 source code system/core/init. c. The detailed code is as follows:
Int ueventd_main (int argc, char ** argv) {struct pollfd ufd; int nr; char tmp [32]; open_devnull_stdio (); log_init (); INFO ("starting ueventd \ n"); get_hardware_name (hardware, & revision); ueventd_parse_config_file ("/ueventd. rc "); snprintf (tmp, sizeof (tmp),"/ueventd. % s. rc ", hardware); ueventd_parse_config_file (tmp); // parse ueventd. xxxxxx. rc file, for example, goldfish is ueventd. goldfish. rc file. For more information, see ueventd. rc device _ Init (); // initialize the uevent socket, which is used to dynamically capture device status changes from the Linux kernel and handle cold boot events at the same time. Ufd. events = POLLIN; // POLLIN indicates that when the kernel has data readable, ufd is returned without blocking. fd = get_device_fd (); // get the specific file descriptor fd from the kernel. while (1) {// loop operation ufd. revents = 0; nr = poll (& ufd, 1,-1); if (nr <= 0) continue; if (ufd. revents = POLLIN) handle_device_fd (); // handle specific device change events passed from the kernel, such as null, usb, and ttyS0. This is my understanding. If anything is wrong, please forgive me.} // As for the kernel ueventd event, you need to understand the linux Device Driver Model, this will be detailed in the Linux Kernel section later, because the driver model is very complicated .}
If the application needs to detect the hot swapping event of the device, this special socket is generally used. In linux, this NETLINK is used. We can detect the hot swapping action of the device through NETLINK_KOBJ_UEVEN in this socket. In fact, the android file system is similar to busybox and udev. In busybox, mdev is used to automatically capture all linux device drivers, which are generally captured from the sys directory. The system automatically assigns the Primary and Secondary device numbers of the corresponding device based on the driver name, as well as the character devices, Block devices, and network devices .. If we can use this method to understand the andorid file system, it will be of great help to analyze the android file system.
If you talk too much nonsense, let's take a look at the device_init () function first. The specific position of the function is in system/core/init/devices. c. The Code is as follows:
Void device_init (void) {suseconds_t t0, t1; struct stat info; int fd; device_fd = open_uevent_socket (); // create a socket, and establish a connection between the process ID and the socket if (device_fd <0) return; fcntl (device_fd, F_SETFD, FD_CLOEXEC); // set the properties of the device file, force lock fcntl (device_fd, F_SETFL, O_NONBLOCK) at the same time; // The android system will generate one under dev/during cold start. coldboot_done file. Check this file. If (stat (coldboot_done, & info) <0) {// query the status of coldboot_done and store it in info. if it fails, the countdown enters the cold start. T0 = get_usecs (); // get the current time coldboot ("/sys/class"); coldboot ("/sys/block "); coldboot ("/sys/devices"); t1 = get_usecs (); fd = open (coldboot_done, O_WRONLY | O_CREAT, 0000); close (fd ); log_event_print ("coldboot % ld uS \ n", (long) (t1-t0);} else {log_event_print ("skipping coldboot, already done \ n "); // cold start completed }}
The open_uevent_socket () function is analyzed in detail. This function monitors device driver changes in the kernel in real time. Therefore, let's take a detailed analysis.
Static int open_uevent_socket (void) {struct sockaddr_nl addr; int sz = 64*1024; // XXX larger? Udev uses 16 MB! Int on = 1; int s; memset (& addr, 0, sizeof (addr); addr. nl_family = AF_NETLINK; // communicates with the kernel through AF_NETLINK. addr. nl_pid = getpid (); // obtain the process PID addr. nl_groups = 0 xffffffff; // defines the socket descriptor, which is an asynchronous communication mechanism. SOCK_DGRAM means that no connection is unreliable and s = socket (PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT ); // The specific event is NETLINK_KOBJECT_UEVENT. For this event, you must have a certain understanding of the linux Device Driver Model. if (s <0) // if it fails,-1 is returned directly, that is, 0 xFFFFFF. Return-1; setsockopt (s, SOL_SOCKET, SO_RCVBUFFORCE, & sz, sizeof (sz); // SO_RCVBUFFORCE is a special receiving buffer, setsockopt (s, SOL_SOCKET, SO_PASSCRED, & on, sizeof (on); // SO_PASSCRED indicates: A credit certificate that allows the recipient to send auxiliary information. If (bind (s, (struct sockaddr *) & addr, sizeof (addr) <0) {// associate the process with the socket. Close (s); return-1; // if it fails, return-1.} return s; // return successful}
After analyzing this step, the ueventd mechanism is basically similar. Check the code in the device_init () function. The Code is as follows:
Ufd. events = POLLIN; ufd. fd = get_device_fd (); // get the specific file descriptor fd that has data passed from the kernel. while (1) {ufd. revents = 0; // first clear the event of the actual action nr = poll (& ufd, 1,-1); // monitor the event if (nr <= 0) // if no event persists, if (ufd. revents = POLLIN) // If an event is monitored, the event is processed, such as a character device, block device, or network device. Handle_device_fd (); // The specific processing event is in this function. }
It may be difficult to understand ueventd because you need to have a certain understanding of the device driver model of the Linux kernel, you also need to understand the operating mechanism of the device driver mode of the kernel. To put it simply, in the Linux kernel, as long as the device driver is loaded, A uevent event will be generated. If you can print out the kernel debugging information, you will see such information as add @ xxxxxxx and remove the device driver. How does the android kernel start ueventd? Detailed instructions are provided in init. rc. Some code of init. rc is as follows:
on early-init start ueventdon initsysclktz 0loglevel 3# setup the global environment export PATH /sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin export LD_LIBRARY_PATH /vendor/lib:/system/lib export ANDROID_BOOTLOGO 1 export ANDROID_ROOT /system export ANDROID_ASSETS /system/app export ANDROID_DATA /data export EXTERNAL_STORAGE /mnt/sdcard export ASEC_MOUNTPOINT /mnt/asec export LOOP_MOUNTPOINT /mnt/obb
Have you seen start uventd in on early-init? Yes. Because init is a daemon and the first program to be called after the kernel is started. The init process first crawls the init. rc file. After the kernel is started, why does init be the first program to run. In the future, we will create a blog post to detail this issue. I will not go into details here. I have a good idea about ueventd. Basically, the analysis is complete. In fact, ueventd is used to initialize all the device drivers in the kernel and allocate all the devices through ueventd. Of course, this is based on the specific existing device driver of the kernel.
In the end, the source code analysis of Andriod should continue with "learning from android2.3.5: setting up the Android startup mechanism [4]".