// {@code r == null} in the parent (zygote) process, and {@code r != null} in the // child (system_server) process. //如果是zygote进程,则r==null,如果不是zygote进程,也就是systemServer进程,则执行下面的代码 if (r != null) { r.run(); return; } } // ...... }
privatestaticnativeintnativeForkSystemServer(int uid, int gid, int[] gids, int runtimeFlags, int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
/** * 代码位于:frameworks/base/core/jni/com_android_internal_os_Zygote.cpp * JNIEnv* env **/ static jint com_android_internal_os_Zygote_nativeForkSystemServer( JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities, jlong effective_capabilities) { // ...... //从zygote进程fork出子进程,并返回processId pid_tpid= ForkCommon(env, true, fds_to_close, fds_to_ignore, true); if (pid == 0) { // System server prcoess does not need data isolation so no need to // know pkg_data_info_list. SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits, permitted_capabilities, effective_capabilities, MOUNT_EXTERNAL_DEFAULT, nullptr, nullptr, true, false, nullptr, nullptr, /* is_top_app= */false, /* pkg_data_info_list */ nullptr, /* whitelisted_data_info_list */ nullptr, false, false); } elseif (pid > 0) { // ...... } return pid; }
/** * 代码位于:/framework/base/core/java/com/android/internal/os/ZygoteInit.java * Finish remaining work for the newly forked system server process. */ privatestatic Runnable handleSystemServerProcess(ZygoteArguments parsedArgs) { // set umask to 0077 so new files and directories will default to owner-only permissions. // /* * Pass the remaining arguments to SystemServer. * 将其余参数传递给SystemServer。 */ return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion, parsedArgs.mDisabledCompatChanges, parsedArgs.mRemainingArgs, cl); }
protected static Runnable applicationInit(int targetSdkVersion, long[] disabledCompatChanges, String[] argv, ClassLoader classLoader) { // If the application calls System.exit(), terminate the process // immediately without running any shutdown hooks. It is not possible to // shutdown an Android application gracefully. Among other things, the // Android runtime shutdown hooks close the Binder driver, which can cause // leftover running threads to crash before the process actually exits. nativeSetExitWithoutCleanup(true);
VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion); VMRuntime.getRuntime().setDisabledCompatChanges(disabledCompatChanges); // 进行参数的封装解析 final Arguments args = new Arguments(argv);
// The end of of the RuntimeInit event (see #zygoteInit). Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
// Remaining arguments are passed to the start class's static main // 调用main方法。并将参数传入过去 return findStaticMain(args.startClass, args.startArgs, classLoader); }
try { cl = Class.forName(className, true, classLoader); } catch (ClassNotFoundException ex) { thrownewRuntimeException( "Missing class when invoking static main " + className, ex); }
Method m; try { m = cl.getMethod("main", newClass[] { String[].class }); } catch (NoSuchMethodException ex) { thrownewRuntimeException( "Missing static main on " + className, ex); } catch (SecurityException ex) { thrownewRuntimeException( "Problem getting static main on " + className, ex); }
intmodifiers= m.getModifiers(); if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) { thrownewRuntimeException( "Main method is not public and static on " + className); }
/* * This throw gets caught in ZygoteInit.main(), which responds * by invoking the exception's run() method. This arrangement * clears up all the stack frames that were required in setting * up the process. */ returnnewMethodAndArgsCaller(m, argv); }
/** * 代码位于:/framework/base/core/java/com/android/server/SystemServer.java * The main entry point from zygote. */ publicstaticvoidmain(String[] args) { newSystemServer().run(); } /** * 代码位于:/framework/base/core/java/com/android/server/SystemServer.java */ publicSystemServer() { // Check for factory test mode. mFactoryTestMode = FactoryTest.getMode();
// Record process start information. // Note SYSPROP_START_COUNT will increment by *2* on a FDE device when it fully boots; // one for the password screen, second for the actual boot. mStartCount = SystemProperties.getInt(SYSPROP_START_COUNT, 0) + 1; mRuntimeStartElapsedTime = SystemClock.elapsedRealtime(); mRuntimeStartUptime = SystemClock.uptimeMillis(); Process.setStartTimes(mRuntimeStartElapsedTime, mRuntimeStartUptime);
// Remember if it's runtime restart(when sys.boot_completed is already set) or reboot // We don't use "mStartCount > 1" here because it'll be wrong on a FDE device. // TODO: mRuntimeRestart will *not* be set to true if the proccess crashes before // sys.boot_completed is set. Fix it. mRuntimeRestart = "1".equals(SystemProperties.get("sys.boot_completed")); } /** * 代码位于:/framework/base/core/java/com/android/server/SystemServer.java */ privatevoidrun() { TimingsTraceAndSlogt=newTimingsTraceAndSlog(); try { // ......
// Create the system service manager. // 创建SystemServiceManager.对系统服务进行创建、启动和生命周期管理。 mSystemServiceManager = newSystemServiceManager(mSystemContext); mSystemServiceManager.setStartInfo(mRuntimeRestart, mRuntimeStartElapsedTime, mRuntimeStartUptime); LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Prepare the thread pool for init tasks that can be parallelized SystemServerInitThreadPool.start(); // ...... } finally { t.traceEnd(); // InitBeforeStartServices }
// Setup the default WTF handler RuntimeInit.setDefaultApplicationWtfHandler(SystemServer::handleEarlySystemWtf);
/** * Starts the small tangle of critical services that are needed to get the system off the * ground. These services have complex mutual dependencies which is why we initialize them all * in one place here. Unless your service is also entwined in these dependencies, it should be * initialized in one of the other functions. */ privatevoidstartBootstrapServices(@NonNull TimingsTraceAndSlog t) { t.traceBegin("startBootstrapServices");
// FileIntegrityService responds to requests from apps and the system. It needs to run after // the source (i.e. keystore) is ready, and before the apps (or the first customer in the // system) run. // 启动文件完整性服务 t.traceBegin("StartFileIntegrityService"); mSystemServiceManager.startService(FileIntegrityService.class); t.traceEnd();
// Start receiving calls from HIDL services. Start in in a separate thread // because it need to connect to SensorManager. This have to start // after START_SENSOR_SERVICE is done. SystemServerInitThreadPool.submit(() -> { TimingsTraceAndSlogtraceLog= TimingsTraceAndSlog.newAsyncLog(); traceLog.traceBegin(START_HIDL_SERVICES); startHidlServices(); traceLog.traceEnd(); }, START_HIDL_SERVICES);
if (!isWatch && enableVrService) { t.traceBegin("StartVrManagerService"); mSystemServiceManager.startService(VrManagerService.class); t.traceEnd(); }