Only the analysis of the main function is provided here.
Main Work of init:
1. initialize the Log System
2. parse files such as init. RC init % hardware %. RC.
3. Execute early-init action in the two files paresed in step2.
4. device initialization, such as creating all device nodes under/Dev and downloading firmwares
5. initialize the property server, actually the property system is
Working as share memory. logically it looks like a Registry
Under Windows System
6. Execute init action in the two files parsed in step2.
7. Enable Attribute Service
8. Run Earl-boot and boot actions in the two files parsed in step2.
9. Execute execute property action in the two files parsed in step2
10. Enter an endless loop, to wait for device/property set/child process
Exit events. For example, if the SD card is inserted, init will receive a device insertion event, which will
This device creates a node. Most important processes in the system are fork by init, so if they have
If it crashes, init will receive a sigchild signal, which is converted into a sub-process exit event,
So in the loop, init will operate the process exit event and execute the commands defined in *. RC file
Int main (INT argc, char ** argv) {int fd_count = 0; struct pollfd ufds [4]; char * tmpdev; char * debuggable; char TMP [32]; int property_set_fd_init = 0; int signal_fd_init = 0; int keychord_fd_init = 0; If (! Strcmp (basename (argv [0]), "ueventd") return ueventd_main (argc, argv); // What is the role of this function? /* Clear the umask */umask (0); // directory created, the default file permission is 777/* Get the basic filesystem setup we need put * together in the initramdisk on/and then we'll * Let the RC file figure out of the rest. * /// mount several special file systems, including tmpfs, Proc, and sysfs mkdir ("/dev", 0755); mkdir ("/proc ", 0755); mkdir ("/sys", 0755); mount ("tmpfs", "/dev", "tmpfs", 0, "mode = 0755 "); mkdir ("/dev/PTS", 0755); mkdir ("/dev/socket", 0755); MOU NT ("devpts", "/dev/PTS", "devpts", 0, null); mount ("proc", "/proc", "proc", 0, null); mount ("sysfs", "/sys", "sysfs", 0, null); # ifdef init_eng_build Mount ("debugfs ", "/sys/kernel/debug", "debugfs", 0, null ); # endif/* we must have some place other than/to create the * Device nodes for kmsg and null, otherwise we won't * be able to remount/read-only later on. * Now that tmpfs is mounted on/dev, We can actually * Talk to the outside world. * // open the/dev/null device node to block unwanted print information. open_devnull_stdio (); // display the log system/dev/kmsg log_init (); // JK @ MTK, add for aee {# ifdef have_aee_feature # ifdef aee_core_dump {extern int aee_enable_core_dump (INT); If (aee_enable_core_dump (1) <0) {error ("enable core dump fail \ n") ;}# endif // parse init. aee. RC file info ("reading aee config file \ n"); init_parse_config_file (" /Init. aee. RC "); # endif // JK @ MTK, add for aee} Info (" reading config file \ n "); # ifdef use_built_in_factory if (is_factory_boot ()) {// if you enter the factory startup mode, you need to parse the init. factory. RC file // parse init. factory. RC and init. RC file if (init_parse_config_file ("/init. factory. RC ") <0) init_parse_config_file ("/init. RC ");} else {// otherwise, you only need to parse init. RC file init_parse_config_file ("/init. RC ") ;}# else init_parse_config_file ("/init. RC "); # endif /* Pull the kernel CommandLine and ramdisk properties file in * // import the kernel command line ~ // You can view the specific command line by viewing the proc/cmdline file, as shown in the following figure: // problem, the kernel startup command line should be parsed by uboot before the kernel is started, why do I need to parse // import_kernel_cmdline (0) in the INIT process? // obtain the hardware board information and obtain it from/proc/cpuinfo. Get_hardware_name (hardware, & revision); snprintf (TMP, sizeof (TMP), "/init. % S. RC ", hardware); loads (TMP); // execute early-init actions in the two files parsed in step2 action_for_each_trigger (" early-init ", action_add_queue_tail); queue_builtin_action (callback, "wait_for_coldboot_done"); queue_builtin_action (property_init_action, "property_init"); queue_builtin_action (Keychord_init_action, "keychord_init"); queue_builtin_action (lele_init_action, "lele_init"); queue_builtin_action (actions, "set_init_properties "); /* execute all the boot actions to get us started * // execute init actions in the two files parsed in step2 action_for_each_trigger ("init", action_add_queue_tail ); action_for_each_trigger ("early-Fs", action_add_queue_tail); action_fo R_each_trigger ("FS", callback); Evaluate ("post-Fs", callback); queue_builtin_action (response, "property_service_init"); queue_builtin_action (signal_init_action, "signal_init "); queue_builtin_action (check_startup_action, "check_startup");/* execute all the boot actions to get us started * // execute early-boot and boot actions in the two files Parsed in step2 action_for_each_trigger ("early-Boot", action_add_queue_tail); action_for_each_trigger ("Boot", action_add_queue_tail ); /* Run all property triggers based on current state of the properties */queue_builtin_action (actions, "queue_propety_triggers"); # If bootchart queue_builtin_action (bootchart_init_action, "bootchart_init "); # endif for (;) {int NR, I, timeout = -1; execute_one_command (); restart_processes (); If (! Property_set_fd_init & get_property_set_fd ()> 0) {ufds [fd_count]. FD = get_property_set_fd (); ufds [fd_count]. events = Pollin; ufds [fd_count]. revents = 0; fd_count ++; property_set_fd_init = 1;} If (! Signal_fd_init & get_signal_fd ()> 0) {ufds [fd_count]. FD = get_signal_fd (); ufds [fd_count]. events = Pollin; ufds [fd_count]. revents = 0; fd_count ++; signal_fd_init = 1;} If (! Keychord_fd_init & get_keychord_fd ()> 0) {ufds [fd_count]. FD = get_keychord_fd (); ufds [fd_count]. events = Pollin; ufds [fd_count]. revents = 0; fd_count ++; keychord_fd_init = 1;} If (process_needs_restart) {timeout = (process_needs_restart-gettime () * 1000; If (timeout <0) timeout = 0;} If (! Action_queue_empty () | cur_action) Timeout = 0; # If bootchart if (bootchart_count> 0) {If (timeout <0 | timeout> bootchart_polling_ms) Timeout = bootchart_polling_ms; if (bootchart_step () <0 | -- bootchart_count = 0) {bootchart_finish (); bootchart_count = 0 ;}# endif Nr = poll (ufds, fd_count, timeout ); if (NR <= 0) continue; for (I = 0; I <fd_count; I ++) {If (ufds [I]. revents = Pollin) {If (ufds [I]. FD = get_property_set_fd () handle_property_set_fd (); else if (ufds [I]. FD = get_keychord_fd () handle_keychord (); else if (ufds [I]. FD = get_signal_fd () handle_signal () ;}} return 0 ;}
The init parsing script file init. RC file structure analysis can refer to my blog: http://blog.csdn.net/yinwei520/article/details/6598453