Arm-Linux transplantation (III)-INIT process Startup Process Analysis

Source: Internet
Author: User

ARM-Linux transplantation (III)-init process Startup Process Analysis

K-style

 

Reprinted please indicate from Hengyang Normal College 08 electric 2 k-style http://blog.csdn.net/ayangke,QQ:843308498 mailbox: yangkeemail@qq.com

 

We usually use busybox to build the necessary applications for the root file system. Busybox determines the operation to be performed by passing in parameters. When the INIT process starts, it actually calls the init_main () function of busybox. Next we will analyze this function to see what the INIT process is like. The source code of the busybox I analyzed is 1.7.0, And the other versions will be slightly different. Some code is omitted. We only look at the key code.

 

First, let's look at the init_main function.

Int init_main (int argc, char ** argv); int init_main (int argc, char ** argv ){................................... ................................... // Initialize lele_init ();.................................... If (argc> 1 &&(! Strcmp (argv [1], "single") |! Strcmp (argv [1], "-s") | LONE_CHAR (argv [1], '1') {new_init_action (RESPAWN, bb_default_login_shell ,"");} else {// because the init process we started does not have any parameters, all argc = 1, the execution of this statement is parse_inittab ();} ................................................................................................ Run_actions (SYSINIT); // run the run_actions (WAIT) process whose acthion is sysinit in the inittab configuration file; // run the run_actions (ONCE) process whose action is WAIT in the inittab configuration file ); // run the process whose action is ONCE in the inittw configuration file ...................................................... While (1) {/* run the processes whose actions are RESPAWN and askfirst in the inittab configuration file. Once the process exits, restart */run_actions (RESPAWN); run_actions (ASKFIRST ); wpid = wait (NULL); while (wpid> 0) {a-> pid = 0;} wpid = waitpid (-1, NULL, WNOHANG );}}

Parse_inittab actually explains the configurations in the/etc/inittab file. If not, set some default settings.

Let's take a look at the configuration format in the inittab file, which is described in the inittab file in the busybox file.

<Id >:< runlevels >:< action >:< process>

Id indicates the output input device. You do not need to set this parameter because/etc/console is already set as standard input/output. If this parameter is not set, input and output are from the console.

Runlevels is ignored completely.

The running time of the action, which indicates the running sequence after inittab interprets. The values include sysinit, respawn, askfirst, wait, once, restart, ctrlaltdel, and andshutdown.

Process is the process to be started.

 

The following describes the prase_inittab function.

Static void parse_inittab (void) {.................................................................................................................. /* INITTAB is a macro # define INITTAB "/etc/inittab". It can be seen that it opens the/etc/inittab file */file = fopen (INITTAB, "r "); // if this file does not exist, call new_init_action to perform some default operations if (file = NULL) {new_init_action (CTRLALTDEL, "reboot", ""); new_init_action (SHUTDOWN, "umount-a-r", ""); if (ENABLE_SWAPONOFF) new_init_action (SHUTDOWN, "swapoff-a", ""); new_init_action (RESTART, "init ", ""); new_init_action (ASKFIRST, bb_default_login_shell ,""); Values (ASKFIRST, second, VC_2); new_init_action (ASKFIRST, second, VC_3); new_init_action (ASKFIRST, second, VC_4); new_init_action (SYSINIT, INIT_SCRIPT, ""); return ;} .................................................................................................................. /* If the inittab file contains content, it will read the content in a row, and then call new_init_action to perform the operation */while (fgets (buf, INIT_BUFFS_SIZE, file )! = NULL) {/* OK, now process it */for (a = actions; a-> name! = 0; a ++) {if (strcmp (a-> name, action) = 0) {if (* id! = '\ 0') {if (strncmp (id, "/dev/", 5) = 0) id + = 5; strcpy (tmpConsole, "/dev/"); safe_strncpy (tmpConsole + 5, id, sizeof (tmpConsole)-5); id = tmpConsole;} new_init_action (a-> action, command, id ); break ;}} ..................................................................................................................} Fclose (file );}

This new_init_action function is actually a chain list of actions in the inittab.

Next we will analyze the operations performed after the prase_inittab is executed by init_main.

It can be seen that after executing prase_initab on init_main to explain the configuration in the inittab file, the running time will be the sysinit process first, so that the wait process can be executed at the same time, followed by once, then run respawn and askfirst in a while (1) function. Once the processes are killed at these two times, their PID is assigned 0, then jump to the start of the while (1) function and start them again. All processes whose Runtime is respawn and askfirst will never be killed unless reboot or shutdown.

 

Next we will summarize the Startup Process of the init process.
1. initialize the Console
2. Description: inittab
3. Run the inittab to run the SYSINIT process.
4. processes whose inittab execution time is WAIT
5. processes whose inittab execution time is ONCE
6. Execute the inittab for the RESPAWN and ASKFRIST processes. If the inittab is exited, run the command again.



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.