Android5.1.1源碼 - 分析Android系統服務什麼時候被添加到ServiceManager

Android5.1.1源碼 - 分析Android系統服務什麼時候被添加到ServiceManager

@(Android研究)[ServiceManager|PackageManagerService|ActivityManagerService]java


[TOC]android


前言

本文公開首發於阿里聚安全博客:https://jaq.alibaba.com/community/index.htm?spm=0.0.0.0.ycEUXK安全

分析

SystemServer類在文件"frameworks/base/services/java/com/android/server/SystemServer.java"中,在這個類的靜態方法"main"中建立了SystemServer對象,下面是它的源碼:app

/**
 * The main entry point from zygote.
 */
public static void main(String[] args) {
    new SystemServer().run();
}

在建立了SystemServer對象以後調用了SystemServer類的run方法,下面是它的源碼:less

private void run() {

    ......

    // Start services.
    try {
        startBootstrapServices();
        startCoreServices();
        startOtherServices();
    } catch (Throwable ex) {
        Slog.e("System", "******************************************");
        Slog.e("System", "************ Failure starting system services", ex);
        throw ex;
    }

    ......

}

在run方法中調用了啓動服務的方法,在startBootstrapServices方法中啓動了PackageManagerService、ActivityManagerService等服務並將它們添加到了ServiceManager中,下面是startBootstrapServices的源碼:this

/**
 * 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.
 */
private void startBootstrapServices() {
    // Wait for installd to finish starting up so that it has a chance to
    // create critical directories such as /data/user with the appropriate
    // permissions.  We need this to complete before we initialize other services.
    Installer installer = mSystemServiceManager.startService(Installer.class);

    // Activity manager runs the show.
    mActivityManagerService = mSystemServiceManager.startService(
            ActivityManagerService.Lifecycle.class).getService();
    mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
    mActivityManagerService.setInstaller(installer);

    // Power manager needs to be started early because other services need it.
    // Native daemons may be watching for it to be registered so it must be ready
    // to handle incoming binder calls immediately (including being able to verify
    // the permissions for those calls).
    mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);

    // Now that the power manager has been started, let the activity manager
    // initialize power management features.
    mActivityManagerService.initPowerManagement();

    ......

    // Start the package manager.
    Slog.i(TAG, "Package Manager");
    mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
            mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
    mFirstBoot = mPackageManagerService.isFirstBoot();
    mPackageManager = mSystemContext.getPackageManager();

    Slog.i(TAG, "User Service");
    ServiceManager.addService(Context.USER_SERVICE, UserManagerService.getInstance());

    // Initialize attribute cache used to cache resources from packages.
    AttributeCache.init(mSystemContext);

    // Set up the Application instance for the system process and get started.
    mActivityManagerService.setSystemProcess();
}

在上面代碼中PackageManagerService.main建立了PackageManagerService類對象並將它添加到了ServiceManager中。code

PackageManagerService類在文件"frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java"中,下面是它的靜態成員方法main的源碼:server

public static final PackageManagerService main(Context context, Installer installer,
        boolean factoryTest, boolean onlyCore) {
    PackageManagerService m = new PackageManagerService(context, installer,
            factoryTest, onlyCore);
    ServiceManager.addService("package", m);
    return m;
}

PackageManagerService.main方法建立了PackageManagerService對象,而後執行"ServiceManager.addService"方法將這個對象添加到了ServiceManager中。當調用"ServiceManager.getService("package")"語句時就會返回這個PackageManagerService對象。htm

回到SystemServer.startBootstrapServices方法中,在這個方法的最後有這麼一條語句:mActivityManagerService.setSystemProcess();,這條語句將ActivityManagerService對象添加到了ServiceManager中。對象

ActivityManagerService類在文件"frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java"中,下面是ActivityManagerService.setSystemProcess方法的源碼:

public void setSystemProcess() {
    try {
        ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
        ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
        ServiceManager.addService("meminfo", new MemBinder(this));
        ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
        ServiceManager.addService("dbinfo", new DbBinder(this));

        ......
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException(
                "Unable to find android system package", e);
    }
}

在這個方法中向ServiceManager中添加了好幾個服務,Context.ACTIVITY_SERVICE的值是字符串"activity",ProcessStats.SERVICE_NAME的值是字符串"procstats"。語句"ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);"把this添加到了ServiceManager中,這個this就是ActivityManagerService對象。

相關文章
相關標籤/搜索