Start Android local service-start zygote service and Dalvik Environment

Source: Internet
Author: User

According to the analysis on the startup of the android INIT process in the previous section, the INIT process parses and processes the INIT process. RC and init. hardware. the RC two initialization script files define a series of service sections in the two initialization script files. These services, under the boot trigger, use class_start
Default is started sequentially. The process is summarized as follows:

1. init parsing init. RC

2. init put the service in init. RC into the queue and wait for the trigger to trigger.

3. init triggers the boot action through action_for_each_trigger ("Boot", action_add_queue_tail );

4. execute commands under boot in sequence, including the class_start default command

5. The default class of all services is default: Default. Therefore, all services in init. Rc are started.

Zygote Service Startup


The description of the zygote service in init. RC shows that the corresponding program is/system/bin/app_process.

Service zygote/system/bin/app_process-xzygote/system/bin -- zygote -- start-system-Server
Socket zygote stream 666
Onrestart write/sys/android_power/request_state wake
Onrestart write/sys/power/State on
Onrestart restart Media

This service will be triggered during on boot. The program is/system/bin/app_process, and the service name is zygote,-xzygote/system/bin -- zygote.
-- Start-system-server is the parameter list.

After the zygote service is created, create a stream socket file/dev/socket/zygote in the directory with the permission of 666. When the zygote service restarts, restart the media service.

Use Find. /-name android. MK-exec grep-l app_process {}\; command, find it in. the/frameworks/base/cmds/app_process/directory is compiled. The main entry file is:

./Frameworks/base/cmds/app_process/app_main.cpp

Find the main entry function of the program,

118 int main (INT argc, const char * const argv [])
119 {
120 // these are global variables in processstate. cpp
121 margc = argc;
122 margv = argv;
123
124 marglen = 0;
125 for (INT I = 0; I <argc; I ++ ){
126 marglen + = strlen (argv [I]) + 1;
127}
128 marglen --;
129
130 appruntime runtime;
131 const char * ARG;
132 const char * argv0;
133
134 argv0 = argv [0];
135
136 // process command line arguments
137 // ignore argv [0]
138 argc --;
139 argv ++;

141 // everything up to '--' or first non'-'arg goes to the VM
142 // In the parameter list of the zygote service, the parameter starting with '-- and'-'is the Dalvik parameter:/system/bin -- zygote -- start-system-server, hand over to VM for processing

143 int I = runtime. addvmarguments (argc, argv );
144
145 // find the zygote Directory:/system/bin
146 if (I <argc ){
147 runtime. mparentdir = argv [I ++];
148}
149
150 // if the following parameter is -- zygote -- start-system-server, set argv0 = "zygote", startsystemserver = true, and start Java VM
151 if (I <argc ){
152 Arg = argv [I ++];
153 If (0 = strcmp ("-- zygote", ARG )){
154 bool startsystemserver = (I <argc )?
155 strcmp (argv [I], "-- start-system-server") = 0: false;
156 setargv0 (argv0, "zygote ");
157 set_process_name ("zygote ");
158 runtime. Start ("com. Android. Internal. OS. zygoteinit ",
159 startsystemserver );
160} else {
161 set_process_name (argv0 );
162
163 runtime. mclassname = ARG;
164
165 // remainder of argS get passed to startup class main ()
166 runtime. margc = argc-I;

167 runtime. margv = argv + I;
168
169 logv ("app process is starting with PID = % d, class = % S. \ n ",
170 getpid () and runtime. getclassname ());
171 runtime. Start ();
172}
173} else {
174 log_always_fatal ("app_process: no class name or -- zygote supplied .");
175 fprintf (stderr, "error: no class name or -- zygote supplied. \ n ");
176 app_usage ();
177 return 10;
178}
179
180}

Start the VM according to the parameters of service zygote:

Runtime. Start ("com. Android. Internal. OS. zygoteinit", startsystemserver );

Runtime is the object of appruntime, and appruntime is a subclass of androidruntime:

The runtime. Start method is implemented in androidruntime:

859 void androidruntime: Start (const char * classname, const bool startsystemserver)
860 {
861 logd ("\ n >>>>>>>>>>>>>>> androidruntime start <\ n "); // The most conspicuous words in logcat
862
863 char * slashclassname = NULL;
864 char * CP;
865 jnienv * env;
866
867 blocksigpipe ();
868
869 /*
870 * 'startsystemserver = true' means Runtime is obslete and not run from
871 * init. RC anymore, so we print out the boot start event here.
872 */
873 if (startsystemserver ){
874/* track our progress through the boot sequence */
875 const int log_boot_progress_start = 3000;
876 log_event_long (log_boot_progress_start,
877 ns2ms (systemtime (system_time_monotonic )));
878}

880 const char * rootdir = getenv ("android_root"); // obtain the android root directory:/System
881 if (rootdir = NULL ){
882 root dir = "/system ";
883 if (! Hasdir ("/system ")){
884 log_fatal ("No root directory specified, And/Android does not exist .");
885 goto bail;
886}
887 setenv ("android_root", rootdir, 1 );
888}
889
890 // const char * kernelhack = getenv ("ld_assume_kernel ");
891 // logd ("found ld_assume_kernel = '% s' \ n", kernelhack );
892
893/* Start the Virtual Machine */

// Start the Dalvik virtual machine. In the androidruntime: startvm method, the device has a large number of VM parameters, and finally calls jni_createjavavm (pjavavm, penv, & initargs) to start the virtual machine through JNI.
894 if (startvm (& mjavavm, & env )! = 0)

895 goto bail;
896
897 /*
898 * Register Android functions.
899 */
900 if (starregulatory (ENV) <0) {// register the JNI function used by the System
901 LogE ("unable to register all android natives \ n ");
902 goto bail;
903}

910 jclass stringclass;
911 jobjectarray strarray;
912 jstring classnamestr;
913 jstring startsystemserverstr;
914
915 stringclass = env-> findclass ("Java/lang/string"); // find the string class from the Dalvik virtual machine,
916 assert (stringclass! = NULL );
917 strarray = env-> newobjectarray (2, stringclass, null );
// Create a string array with two elements (strarray = new string [2])
918 assert (strarray! = NULL );
919 classnamestr = env-> newstringutf (classname); // create a Java String object. The initial value is classname. In fact, it is the first parameter of START: COM. Android. Internal. OS. zygoteinit.
920 assert (classnamestr! = NULL );
921 env-> setobjectarrayelement (strarray, 0, classnamestr );
// Set the value of the first element of strarray to classnamestr (strarray [0] = classnamestr)
922 startsystemserverstr = env-> newstringutf (startsystemserver?
923 "true": "false ");//
Create a Java String object with the initial value: startsystemserver, which is actually the second parameter of START: True
924 env-> setobjectarrayelement (strarray, 1, startsystemserverstr );
// Set the value of the second strarray element to strarray [1] = startsystemserverstr

925
926 /*
927 * Start VM. This thread becomes the main thread of the VM, and will
928 * not return until the VM exits.
929 */

// According to the above explanation, we can see that the main thread that is going to start the Java Vm and create the VM will continue to run as long as the VM does not exit.
930. jclass startclass;
931 jmethodid startmeth;

933 slashclassname = strdup (classname );
934 for (Cp = slashclassname; * CP! = '\ 0'; CP ++) // convert COM. android. internal. OS. package Separator in zygoteinit '. 'Change to '/': COM/Android/Internal/OS/zygoteinit
935 if (* CP = '.')
936 * CP = '/';
937
938 startclass = env-> findclass (slashclassname );
// Find the zygoteinit class from the VM. Is it necessary to load this class in the VM ....
939 if (startclass = NULL ){
940 LogE ("JavaVM unable to locate class '% s' \ n", slashclassname );
941/* keep going */
942} else {
943 startmeth = env-> getstaticmethodid (startclass, "Main ",
944 "([ljava/lang/string;) V ");
// You can find the main method ID in the COM/Android/Internal/OS/zygoteinit class and join the Java File naming rules, why should the main class name be the same as the file name and the main method be static.
945 if (startmeth = NULL ){
946 LogE ("JavaVM unable to find main () in '% s' \ n", classname );
947/* keep going */
948} else {
949 env-> callstaticvoidmethod (startclass, startmeth, strarray); // call the main method in the zygoteinit class. Isn't this a Java program that runs zygoteinit !!
950
951 # If 0
952 If (env-> predictioncheck ())
953 threadexituncaughtexception (ENV );
954 # endif
955}
956}

957
958 logd ("shutting down VM \ n ");
959 if (mjavavm-> detachcurrentthread ()! = Jni_ OK)
960 logw ("Warning: Unable to detach main thread \ n ");
961 if (mjavavm-> destroyjavavm ()! = 0)
962 logw ("Warning: VM did not shut down cleanly \ n ");
963
964 bail:
965 free (slashclassname );
966}
967
The above analysis shows that the androidruntime: Start method implements the following functions:

1> Start the VM through startvm and register some system JNI functions. At this time, there is no program in the VM, but it is just an empty VM execution environment.

2> with the androidruntime: Start parameter, build the first Java program zygoteinit in the jni code, use it as the main thread of the VM, and pass it two parameters constructed by JNI:

"Com/Android/Internal/OS/zygoteinit" and "true"

Summary:

The Android system starts the/system/bin/app_process program loaded by the INIT process as the zygote service, and then runs runtime in the zygote service. start starts the Dalvik Virtual Machine and loads the zygoteinit class as the first main thread of the Dalvik virtual machine. Now, the Java Runtime Environment for Android is ready.

Related Article

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.