Android 进阶——系统启动之SystemServer创建并启动Installer服务

Posted CrazyMo_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 进阶——系统启动之SystemServer创建并启动Installer服务相关的知识,希望对你有一定的参考价值。

文章大纲

引言

SystemServer进程在执行startBootstrapServices方法后,首先就通过SystemServiceManager创建和启动Installer系统服务。

以后更多精选系列文章以高度完整的系列形式发布在公众号,真正的形成一套完整的技术栈,欢迎关注,目前第一个系列是关于android系统启动系列的文章,大概有二十几篇干货。

一、Intsaller系统服务概述

\\frameworks\\base\\services\\core\\java\\com\\android\\server\\pm\\Installer.java继承自com.android.server.SystemService,同时持有installd守护进程对应Binder服务的代理对象,本质上就是通过Binder调用去与Linux底层installd守护进程通信完成真正的完成Apk文件格式的优化和转换建立相关的数据目录删除文件安装应用等工作。因此在其他系统核心服务启动前首先被启动,

    private void startBootstrapServices() 
    	...
       Installer installer = mSystemServiceManager.startService(Installer.class);

SystemServiceManager.startService(Installer.class)触发com.android.server.pm.Installer#onStart回调时,首先通过ServerManager获取名为installdBinder服务代理对象,然后调用这个Binder服务代理对象通过socket与Linux的installd 守护进程通信。

package com.android.server.pm;

import com.android.internal.os.BackgroundThread;
import com.android.server.SystemService;
public class Installer extends SystemService 
    private volatile IInstalld mInstalld;

    public Installer(Context context, boolean isolated) 
        super(context);
        mIsolated = isolated;
    

    @Override
    public void onStart() 
        if (mIsolated) 
            mInstalld = null;
         else 
            connect();
        
    

    private void connect() 
        IBinder binder = ServiceManager.getService("installd");
        if (binder != null) 
            try 
                binder.linkToDeath(new DeathRecipient() 
                    @Override
                    public void binderDied() 
                        Slog.w(TAG, "installd died; reconnecting");
                        connect();
                    
                , 0);
            
        

        if (binder != null) 
            mInstalld = IInstalld.Stub.asInterface(binder);
            try 
                invalidateMounts();
            
         else 
            Slog.w(TAG, "installd not found; trying again");
            BackgroundThread.getHandler().postDelayed(() -> 
                connect();
            , DateUtils.SECOND_IN_MILLIS);
        
    
    public void dexopt(String apkPath, int uid, @Nullable String pkgName, String instructionSet,
            int dexoptNeeded, @Nullable String outputPath, int dexFlags,
            String compilerFilter, @Nullable String volumeUuid, @Nullable String sharedLibraries,
            @Nullable String seInfo, boolean downgrade, int targetSdkVersion,
            @Nullable String profileName, @Nullable String dexMetadataPath,
            @Nullable String compilationReason) throws InstallerException 
        if (!checkBeforeRemote()) return;
        try 
            mInstalld.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded, outputPath,
                    dexFlags, compilerFilter, volumeUuid, sharedLibraries, seInfo, downgrade,
                    targetSdkVersion, profileName, dexMetadataPath, compilationReason);
        
    
    public void rmdex(String codePath, String instructionSet) throws InstallerException 
        try 
            mInstalld.rmdex(codePath, instructionSet);
        
    
    public void createUserData(String uuid, int userId, int userSerial, int flags)
            throws InstallerException 
        if (!checkBeforeRemote()) return;
        try 
            mInstalld.createUserData(uuid, userId, userSerial, flags);
        
    
	...
    public void linkNativeLibraryDirectory(String uuid, String packageName, String nativeLibPath32,
            int userId) throws InstallerException 
        if (!checkBeforeRemote()) return;
        try 
            mInstalld.linkNativeLibraryDirectory(uuid, packageName, nativeLibPath32, userId);
        
    
    public void createOatDir(String oatDir, String dexInstructionSet)
            throws InstallerException 
        if (!checkBeforeRemote()) return;
        try 
            mInstalld.createOatDir(oatDir, dexInstructionSet);
        
    
    public void linkFile(String relativePath, String fromBase, String toBase)
            throws InstallerException 
        if (!checkBeforeRemote()) return;
        try 
            mInstalld.linkFile(relativePath, fromBase, toBase);
        
    
    public void deleteOdex(String apkPath, String instructionSet, String outputPath)
            throws InstallerException 
            mInstalld.deleteOdex(apkPath, instructionSet, outputPath);
    
    public void installApkVerity(String filePath, FileDescriptor verityInput, int contentSize)
            throws InstallerException 
        if (!checkBeforeRemote()) return;
         mInstalld.installApkVerity(filePath, verityInput, contentSize);
    
    public boolean reconcileSecondaryDexFile(String apkPath, String packageName, int uid,
            String[] isas, @Nullable String volumeUuid, int flags) throws InstallerException 
            return mInstalld.reconcileSecondaryDexFile(apkPath, packageName, uid, isas,
                    volumeUuid, flags);
    

    public void invalidateMounts() throws InstallerException 
        if (!checkBeforeRemote()) return;
            mInstalld.invalidateMounts();
    
    ...


二、com.android.server.SystemService概述

\\frameworks\\base\\services\\core\\java\\com\\android\\server\\SystemService.java本质上就是对于一些运行在系统进程服务进行了抽象,定义了一些统一的生命周期方法和运行规则(所有方法都必须在系统服务进程的main looper thread中被调用)。

package com.android.server;

import android.os.ServiceManager;
/**
 * The base class for services running in the system process. Override and implement
 * the lifecycle event callback methods as needed.
 * @hide
 */
public abstract class SystemService 
    ...
    private final Context mContext;
    public SystemService(Context context) 
        mContext = context;
    
    public abstract void onStart();
		...
    protected final void publishBinderService(String name, IBinder service) 
        publishBinderService(name, service, false);
    
    protected final void publishBinderService(String name, IBinder service,
            boolean allowIsolated) 
        ServiceManager.addService(name, service, allowIsolated);
    
    protected final IBinder getBinderService(String name) 
        return ServiceManager.getService(name);
    
    protected final <T> void publishLocalService(Class<T> type, T service) 
        LocalServices.addService(type, service);
    
    protected final <T> T getLocalService(Class<T> type) 
        return LocalServices.getService(type);
    

    private SystemServiceManager getManager() 
        return LocalServices.getService(SystemServiceManager.class);
    

三、Intsaller系统服务的启动

1、com.android.server.SystemServer#startBootstrapServices 触发Installer系统服务启动

    private void startBootstrapServices() 
        Installer installer = mSystemServiceManager.startService(Installer.class);

2、com.android.server.SystemServiceManager#startService反射创建并回调com.android.server.pm.Installer#onStart

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

2.1、com.android.server.SystemServiceManager#startService(String className)获取Installer的字节码对象

    public SystemService startService(String className) 
        final Class<SystemService> serviceClass;
        serviceClass = (Class<SystemService>)Class.forName(className);
        return startService(serviceClass);
    

2.2、com.android.server.SystemServiceManager#startService(java.lang.Class< T >)反射创建Installer对象

 /**
     * Creates and starts a system service. The class must be a subclass of
     * @link com.android.server.SystemService.
     */
    public <T extends SystemService> T startService(Class<T> serviceClass) 
        try 
            final String name = serviceClass.getName();
            // Create the service.
            final T service;
            try 
                Constructor<T> constructor = serviceClass.getConstructor(Context.class);
                service = constructor.newInstance(mContext);
             
            startService(service);
            return service;
        
    

2.3、com.android.server.SystemServiceManager#startService(SystemService) 注册到SystemServerManager并触发Installer#onStart回调

 public void startService(@NonNull final SystemService service) 
        // @link ArrayList<SystemService> mServices 注册到SystemServerManager中
        mServices.add(service);
        long time = SystemClock.elapsedRealtime();
        try 
            service.onStart();
        
        warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
    

3、com.android.server.pm.Installer#onStart 获取installd 的Binder服务代理对象

3.1、com.android.server.pm.Installer#onStart 触发获取installd 的Binder服务代理对象

    @Override
    public void onStart() 
        if (mIsolated) 
            mInstalld = null;
         else 
            connect();
        
     

3.2、com.android.server.pm.Installer#connect 真正获取installd 的Binder服务代理对象

    private void connect() 
    	//首先通过ServiceManager 获取installd服务的Binder对象
        IBinder binder = ServiceManager.getService("installd");
        if (binder != null) 
            try 
                binder.linkToDeath(new DeathRecipient() 
                    @Override
                    public void binderDied() 
                        Slog.w(TAG, "installd died; reconnecting");
                        connect();
                    
                , 0);
             catch (RemoteException e) 
                binder = null;
            
        

        if (binder != null) 
            //把Binder对象转为具体的Binder服务对象
            mInstalld = IInstalld.Stub.asInterface(binder);
            try 
                invalidateMounts();
             catch (InstallerException ignored) 
            
         else 
            Slog.w(TAG, "installd not found; trying again");
            BackgroundThread.getHandler().postDelayed(() -> 
                connect();
            , DateUtils.SECOND_IN_MILLIS);
        
    

至此Installer系统服务启动完毕,接着真正开始其他系统服务的启动,敬请参见下集。

以上是关于Android 进阶——系统启动之SystemServer创建并启动Installer服务的主要内容,如果未能解决你的问题,请参考以下文章

Android 进阶——系统启动之SystemServer创建并启动Installer服务

Android 进阶——系统启动之SystemServer创建并启动Installer服务

Android 进阶——系统启动之Framework 核心ActivitityManagerService服务启动

Android 进阶——系统启动之Framework 核心ActivitityManagerService服务启动

Android 进阶——系统启动之Framework 核心ActivitityManagerService服务启动

Android 进阶——系统启动之核心SystemServer进程启动详解