当前位置:   article > 正文

SystemServer的启动_systemserver启动

systemserver启动

frameworks\base\core\java\com\android\internal\os\ZygoteInit.java的forkSystemServer

  1. /* Request to fork the system server process */
  2. pid = Zygote.forkSystemServer(
  3. parsedArgs.mUid, parsedArgs.mGid,
  4. parsedArgs.mGids,
  5. parsedArgs.mRuntimeFlags,
  6. null,
  7. parsedArgs.mPermittedCapabilities,
  8. parsedArgs.mEffectiveCapabilities);
  9. } catch (IllegalArgumentException ex) {
  10. throw new RuntimeException(ex);
  11. }
  12. /* For child process */
  13. if (pid == 0) {
  14. if (hasSecondZygote(abiList)) {
  15. waitForSecondaryZygote(socketName);
  16. }
  17. zygoteServer.closeServerSocket();
  18. return handleSystemServerProcess(parsedArgs);
  19. }

handleSystemServerProcess来启动SyetemServer进程。

handleSystemServerProcess关键代码

  1. /*
  2. * Pass the remaining arguments to SystemServer.
  3. */
  4. return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,
  5. parsedArgs.mDisabledCompatChanges,
  6. parsedArgs.mRemainingArgs, cl);
  1. public static final Runnable zygoteInit(int targetSdkVersion, long[] disabledCompatChanges,
  2. String[] argv, ClassLoader classLoader) {
  3. if (RuntimeInit.DEBUG) {
  4. Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");
  5. }
  6. Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");
  7. RuntimeInit.redirectLogStreams();
  8. RuntimeInit.commonInit();
  9. ZygoteInit.nativeZygoteInit();
  10. return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv,
  11. classLoader);
  12. }

nativeZygoteInit方法在 frameworks/base/core/jni/AndroidRuntime.cpp中定义

  1. int register_com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env)
  2. {
  3. const JNINativeMethod methods[] = {
  4. { "nativeZygoteInit", "()V",
  5. (void*) com_android_internal_os_ZygoteInit_nativeZygoteInit },
  6. };
  7. return jniRegisterNativeMethods(env, "com/android/internal/os/ZygoteInit",
  8. methods, NELEM(methods));
  9. }

 对应的方法为com_android_internal_os_ZygoteInit_nativeZygoteInit

  1. static void com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
  2. {
  3. gCurRuntime->onZygoteInit();
  4. }

这里gCurRuntime是AndroidRuntime类型的指针,AndroidRuntime的子类AppRuntime在app_main.cpp中定义,我们来查看AppRuntime的onZygoteInit函数,代码如下所示。

frameworks\base\cmds\app_process\app_main.cpp

  1. virtual void onZygoteInit()
  2. {
  3. sp<ProcessState> proc = ProcessState::self();
  4. ALOGV("App process: starting thread pool.\n");
  5. //启动一个Binder线程池,这样SyetemServer进程就可以使用Binder来与其他进程进行通信了
  6. proc->startThreadPool();
  7. }
applicationInit在frameworks\base\core\java\com\android\internal\os\RuntimeInit.java中
  1. protected static Runnable applicationInit(int targetSdkVersion, long[] disabledCompatChanges,
  2. String[] argv, ClassLoader classLoader) {
  3. // If the application calls System.exit(), terminate the process
  4. // immediately without running any shutdown hooks. It is not possible to
  5. // shutdown an Android application gracefully. Among other things, the
  6. // Android runtime shutdown hooks close the Binder driver, which can cause
  7. // leftover running threads to crash before the process actually exits.
  8. nativeSetExitWithoutCleanup(true);
  9. VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);
  10. VMRuntime.getRuntime().setDisabledCompatChanges(disabledCompatChanges);
  11. final Arguments args = new Arguments(argv);
  12. // The end of of the RuntimeInit event (see #zygoteInit).
  13. Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
  14. // Remaining arguments are passed to the start class's static main
  15. return findStaticMain(args.startClass, args.startArgs, classLoader);
  16. }

findStaticMain方法主要调用了MethodAndArgsCaller(m, argv);

  1. protected static Runnable findStaticMain(String className, String[] argv,
  2. ClassLoader classLoader) {
  3. Class<?> cl;
  4. try {
  5. cl = Class.forName(className, true, classLoader);
  6. } catch (ClassNotFoundException ex) {
  7. throw new RuntimeException(
  8. "Missing class when invoking static main " + className,
  9. ex);
  10. }
  11. Method m;
  12. try {
  13. m = cl.getMethod("main", new Class[] { String[].class });
  14. } catch (NoSuchMethodException ex) {
  15. throw new RuntimeException(
  16. "Missing static main on " + className, ex);
  17. } catch (SecurityException ex) {
  18. throw new RuntimeException(
  19. "Problem getting static main on " + className, ex);
  20. }
  21. int modifiers = m.getModifiers();
  22. if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
  23. throw new RuntimeException(
  24. "Main method is not public and static on " + className);
  25. }
  26. /*
  27. * This throw gets caught in ZygoteInit.main(), which responds
  28. * by invoking the exception's run() method. This arrangement
  29. * clears up all the stack frames that were required in setting
  30. * up the process.
  31. */
  32. return new MethodAndArgsCaller(m, argv);
  33. }

MethodAndArgsCaller的run方法调用

mMethod.invoke(null, new Object[] { mArgs });

SystemServer的main函数被动态调用。

进入SystemServer

frameworks\base\services\java\com\android\server\SystemServer.java

  1. public static void main(String[] args) {
  2. new SystemServer().run();
  3. }

run方法关键代码如下

  1. System.loadLibrary("android_servers");加载了libandroid_servers.so
  2. // Create the system service manager.
  3. mSystemServiceManager = new SystemServiceManager(mSystemContext);创建SystemServiceManager,它会对系统的服务进行创建、启动和生命周期管理
  4. startBootstrapServices(t);
  5. startCoreServices(t);
  6. startOtherServices(t);
BootstrapServices作用
Installer系统安装apk时的一个服务类,启动完成Installer服务之后才能启动其他的系统服务
ActivityManagerService负责四大组件的启动、切换、调度。
PowerManagerService计算系统中和Power相关的计算,然后决策系统应该如何反应
LightsService管理和显示背光LED
DisplayManagerService用来管理所有显示设备
UserManagerService多用户模式管理
SensorService为系统提供各种感应器服务
PackageManagerService用来对apk进行安装、解析、删除、卸载等等操作
CoreServices
BatteryService管理电池相关的服务
UsageStatsService收集用户使用每一个APP的频率、使用时常
WebViewUpdateServiceWebView更新服务
OtherServices
CameraService摄像头相关服务
AlarmManagerService全局定时器管理服务
InputManagerService管理输入事件
WindowManagerService窗口管理服务
VrManagerServiceVR模式管理服务
BluetoothService蓝牙管理服务
NotificationManagerService通知管理服务
DeviceStorageMonitorService存储相关管理服务
LocationManagerService定位管理服务
AudioService音频相关管理服务
….

 

 要启动那个服务,调用SystemServiceManager的startService方法即可

mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);

frameworks\base\services\core\java\com\android\server\SystemServiceManager.java

  1. public SystemService startService(String className) {
  2. final Class<SystemService> serviceClass = loadClassFromLoader(className,
  3. this.getClass().getClassLoader());
  4. return startService(serviceClass);
  5. }
  1. public <T extends SystemService> T startService(Class<T> serviceClass) {
  2. try {
  3. final String name = serviceClass.getName();
  4. Slog.i(TAG, "Starting " + name);
  5. Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
  6. // Create the service.
  7. if (!SystemService.class.isAssignableFrom(serviceClass)) {
  8. throw new RuntimeException("Failed to create " + name
  9. + ": service must extend " + SystemService.class.getName());
  10. }
  11. final T service;
  12. try {
  13. Constructor<T> constructor = serviceClass.getConstructor(Context.class);
  14. service = constructor.newInstance(mContext);创建service实例
  15. } catch (InstantiationException ex) {
  16. throw new RuntimeException("Failed to create service " + name
  17. + ": service could not be instantiated", ex);
  18. } catch (IllegalAccessException ex) {
  19. throw new RuntimeException("Failed to create service " + name
  20. + ": service must have a public constructor with a Context argument", ex);
  21. } catch (NoSuchMethodException ex) {
  22. throw new RuntimeException("Failed to create service " + name
  23. + ": service must have a public constructor with a Context argument", ex);
  24. } catch (InvocationTargetException ex) {
  25. throw new RuntimeException("Failed to create service " + name
  26. + ": service constructor threw an exception", ex);
  27. }
  28. startService(service);
  29. return service;
  30. } finally {
  31. Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
  32. }
  33. }
  34. public void startService(@NonNull final SystemService service) {
  35. // Register it.
  36. mServices.add(service);
  37. // Start it.
  38. long time = SystemClock.elapsedRealtime();
  39. try {
  40. service.onStart();
  41. } catch (RuntimeException ex) {
  42. throw new RuntimeException("Failed to start service " + service.getClass().getName()
  43. + ": onStart threw an exception", ex);
  44. }
  45. warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
  46. }

总结SyetemServer进程

SyetemServer在启动时做了如下工作:
1.启动Binder线程池,这样就可以与其他进程进行通信。
2.创建SystemServiceManager用于对系统的服务进行创建、启动和生命周期管理。
3.启动各种系统服务。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号