Android Init Process Analysis

Source: Internet
Author: User

The Android init process is used to start the zygote process to launch the Android world.
Init process source code in the top-level directory of the/system/core/init
Use Find-name android.mk-exec grep-l "init" {} \; To find the source code, the next Android service program also uses this command to find the source code.


/system/core/init/init.c
The entry function for the entire INIT process
669 int main (int argc, char **argv)
Init_parse_config_file ("/init.rc");
snprintf (TMP, sizeof (TMP), "/init.%s.rc", hardware);
722 Init_parse_config_file (TMP);
Resolves two file script files. After parsing, the event is triggered by conditions, and finally the program enters the dead loop, polling the monitoring properties, keys, signals

There are two important processes in Android for. Zygote and Servermanger two services.
431 Service Zygote/system/bin/app_process-xzygote/system/bin--zygote--start-system-server
432 class Main
433 Socket Zygote Stream 666
434 Onrestart Write/sys/android_power/request_state Wake
435 Onrestart Write/sys/power/state on
436 Onrestart Restart Media
437 Onrestart Restart netd
438


Find-name android.mk-exec grep-l "app_process" {} \; used to find the source of the zygote service.
Source Path/android4.0/frameworks/base/cmds/app_process$
App_main.cpp is the program entry for the zygote process.
Both Java, C + +, and C programs are the main portals.

Zygote = true;
Nicename = "Zygote";
Startsystemserver = true;


if (zygote) {
189 Runtime.start ("Com.android.internal.os.ZygoteInit", "Start-system-server");


Call the runtime's Start method, which is defined within the ancestor class of the runtime.
In Frameworks/base/core/jni/androidruntime.cpp
Androidruntime::start void (const char* className, const char* options)
800 {
/* Start the virtual machine */
831 jnienv* env;
832 if (STARTVM (&MJAVAVM, &env)! = 0) {
833 return;
834}
835 onvmcreated (env);
836
837/*

Com/android/internal/os/zygoteinit "

To prepare the parameters for the next Java environment, prepare a Java environment string array
855 Stringclass = Env->findclass ("java/lang/string");
856 assert (Stringclass! = NULL);
857 Strarray = Env->newobjectarray (2, Stringclass, NULL);
858 assert (Strarray! = NULL);
859 classnamestr = Env->newstringutf (className);
860 assert (Classnamestr! = NULL);
861 env->setobjectarrayelement (strarray, 0, CLASSNAMESTR);
862 optionsstr = Env->newstringutf (options);
863 env->setobjectarrayelement (Strarray, 1, OPTIONSSTR);

Strarray[2] = {"Com.android.internal.os.ZygoteInit", "Start-system-server"};


866 * Start VM. This thread becomes the main thread of the VM, and would
867 * Not return until the VM exits.
868 */
869 char* slashclassname = Toslashclassname (className), package name converted to path
870 Jclass Startclass = Env->findclass (slashclassname); Find Java class files in C environment
871 if (Startclass = = NULL) {
872 LOGE ("JAVAVM Unable to locate class '%s ' \ n", slashclassname);
873/* Keep going */
874} else {
875 Jmethodid Startmeth = Env->getstaticmethodid (Startclass, "main", find void Main (string[]) method within the found Java file
876 "([ljava/lang/string;) V");
877 if (Startmeth = = NULL) {
878 LOGE ("JAVAVM Unable to find main () in '%s ' \ n", className);
879/* Keep going */
880} else {
881 Env->callstaticvoidmethod (Startclass, Startmeth, Strarray); Jump from the C environment to the Java World with parameters
882

Java World:
Find-name Main
Android4.0/frameworks/base/core/java/com/android/internal/os/zygoteinit.java Android, the world's beginning of the entire Java world

if (Argv[1].equals ("Start-system-server")) {
536 Startsystemserver (); Determines whether the System_server service is started based on the parameters passed
System_server Service Source in: Android4.0/frameworks/base/services/java/com/android/server/systemserver.java


--------------->>>>>>
Native public static void Init1 (string[] args); The function modified by native is a jni function, in C, environment.


Systemserver.java to find his entrance. Main

System.loadlibrary ("Android_servers"); Jump from the Java world to the C environment, you need to load the C environment library file, load well before jumping.
Init1 (args);

Find-name android.mk-exec grep-l "android_servers" {} \;

/android4.0/frameworks/base/services/jni/com_android_server_systemserver.cpp

Static Jninativemethod gmethods[] = {
*/* Name, signature, Funcptr */
"Init1", "([ljava/lang/string;) V", (void*) android_server_systemserver_init1}, Jni writes the relationship in advance, Associating the native code of a Java call with an execution function inside C
37};


So the ANDROID_SERVER_SYSTEMSERVER_INIT1 function is executed.
The extern "C" int system_init (); Description from another C + + file named System_init.cpp


Find-name system_init.cpp file on
android4.0/frameworks$/base/cmds/system_server/library/system_init.cpp


System_init launched two services Sensorservice, Surfaceflinger
And recall Java, execute init2 ();
Jclass clazz = Env->findclass ("Com/android/server/systemserver");
94 if (Clazz = = NULL) {
Unknown_error return;
96}
Jmethodid Methodid = Env->getstaticmethodid (Clazz, "Init2", "() V");
98 if (Methodid = = NULL) {
return unknown_error;
100}
101 Env->callstaticvoidmethod (Clazz, Methodid); Go back to the Java environment and execute Init2

Android Init Process Analysis

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.