Android init process analysis ueventd
(The lazy recently remembered that I still have csdn for a long time and I haven't taken care of it. This android init has been lying in my draft box for almost five years. Let's change it a little)
Ueventd is mainly responsible for creating device nodes and setting permissions. By using uevent, the Service monitors the messages sent by the driver for further processing.
Ueventd is actually the same binary as init, but it takes different branches. See the previous section.
The overall ueventd code is relatively simple, mainly in three parts:
Parse ueventd. rc to initialize the device information loop polling uevent message
The main functions and related functions are as follows:
Int ueventd_main (int argc, char ** argv) {// Like init, no std input output open_devnull_stdio (); // initialize the kernel log to enable the ueventd log, output data to the klog_init (); // parse and process the rc file import_kernel_cmdline (0, import_kernel_nv) of ueventd through the log of kernel printk; get_hardware_name (hardware, & revision ); ueventd_parse_config_file (/ueventd. rc); snprintf (tmp, sizeof (tmp),/ueventd. % s. rc, hardware); ueventd_parse_config_file (tmp); // The device initializes the device_init (); // polling uevent message to manage the device ufd. events = POLLIN; ufd. fd = get_device_fd (); while (1) {ufd. revents = 0; nr = poll (& ufd, 1,-1); if (nr <= 0) continue; if (ufd. revents = POLLIN) handle_device_fd (); // polling to message, Processing event message }}
Processing and parsing ueventd. rc
Compared with init. rc, this part is very simple and has nothing special to do. The rc file is used to control the permissions of directory nodes. For example:
/dev/ttyUSB2 0666 radio radio/dev/ts0710mux* 0640 radio radio/dev/ppp 0666 radio vpn# sysfs properties/sys/devices/virtual/input/input* enable 0666 system system/sys/devices/virtual/input/input* poll_delay 0666 system system
You do not need to expand the details.
Device Initialization
When the kernel loads a device, it sends a uevent event to userspace through socket. In init, it creates a device node by accepting these uevent events. The main function is device_init ()
The initialization function is device_init.
void device_init(void){ suseconds_t t0, t1; struct stat info; int fd; sehandle = NULL; if (is_selinux_enabled() > 0) { sehandle = selinux_android_file_context_handle(); } /* is 256K enough? udev uses 16MB! */ device_fd = uevent_open_socket(256*1024, true); if(device_fd < 0) return; fcntl(device_fd, F_SETFD, FD_CLOEXEC); fcntl(device_fd, F_SETFL, O_NONBLOCK); if (stat(coldboot_done, &info) < 0) { t0 = get_usecs(); 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, ((long) (t1 - t0))); } else { log_event_print(skipping coldboot, already done); }}
Open_uevent_socket ();
This is to open the socket of uevent. The uevent here uses the netlink kernel event to notify the user State (NETLINK_KOBJECT_UEVENT). It is a very good way for two-way data transmission between the kernel and the user State, except eventd, netd and vold also use uevent.
In the initialization function, coldboot is a relatively important function, which literally means cold start. Its function is to re-operate the drivers that have been added before uventd is started, let them send the event message again, and the upper-layer number will be processed accordingly.
Here we traverse the/sys/class,/sys/block, And/sys/devices:
static void do_coldboot(DIR *d){ struct dirent *de; int dfd, fd; dfd = dirfd(d); fd = openat(dfd, uevent, O_WRONLY); if(fd >= 0) { write(fd, add, 4); close(fd); handle_device_fd(); } while((de = readdir(d))) { DIR *d2; if(de->d_type != DT_DIR || de->d_name[0] == '.') continue; fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); if(fd < 0) continue; d2 = fdopendir(fd); if(d2 == 0) close(fd); else { do_coldboot(d2); closedir(d2); } }}Write (fd, add, 4) activates the kernel, resends the uevent and handle_device_fd () of the add event, and processes the response to the event message.
Uevent Message Processing
After initialization, the daemon program only needs to polling a new event. When polling arrives, it calls handle_device_fd (); to process the event. You can look at this function:
void handle_device_fd(){ char msg[UEVENT_MSG_LEN+2]; int n; while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ continue; msg[n] = ''; msg[n+1] = ''; struct uevent uevent; parse_event(msg, &uevent); handle_device_event(&uevent); handle_firmware_event(&uevent); }}
The function is to receive the event message sent by the kernel, and then parser the message to process the corresponding message event.
Here:
static void handle_device_event(struct uevent *uevent){ if (!strcmp(uevent->action,add) || !strcmp(uevent->action, change)) fixup_sys_perms(uevent->path); if (!strncmp(uevent->subsystem, block, 5)) { handle_block_device_event(uevent); } else if (!strncmp(uevent->subsystem, platform, 8)) { handle_platform_device_event(uevent); } else { handle_generic_device_event(uevent); }}
The main function is to create/delete a device Node Based on the sent uevent, and set and update some node permissions based on the permissions described in ueventd. rc.
static void handle_firmware_event(struct uevent *uevent){ pid_t pid; int ret; if(strcmp(uevent->subsystem, firmware)) return; if(strcmp(uevent->action, add)) return; /* we fork, to avoid making large memory allocations in init proper */ pid = fork(); if (!pid) { process_firmware_event(uevent); exit(EXIT_SUCCESS); }}If you have a coprocessor, you also need to download the firmware of the coprocessor. Here is the instruction for the coprocessor to download firmware, fork a sub-process for processing.