android6.0 sm进程(获取MountService信息)

一、编译

我们先来看看sm的Android.mk

# Copyright 2015 The Android Open Source Project
#
LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := sm
include $(BUILD_JAVA_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := sm
LOCAL_SRC_FILES := sm
LOCAL_MODULE_CLASS := EXECUTABLES
LOCAL_MODULE_TAGS := optional
include $(BUILD_PREBUILT)

下面第二个编译肯定是编一个sm的二进制进程,下面我们再来看看sm的代码


二、源码

public final class Sm {
    private static final String TAG = "Sm";

    IMountService mSm;

    private String[] mArgs;
    private int mNextArg;
    private String mCurArgData;

    public static void main(String[] args) {
        boolean success = false;
        try {
            new Sm().run(args);//主要看run函数
            success = true;
        } catch (Exception e) {
            if (e instanceof IllegalArgumentException) {
                showUsage();
            }
            Log.e(TAG, "Error", e);
            System.err.println("Error: " + e);
        }
        System.exit(success ? 0 : 1);
    }

    public void run(String[] args) throws Exception {
        if (args.length < 1) {
            throw new IllegalArgumentException();
        }

        mSm = IMountService.Stub.asInterface(ServiceManager.getService("mount"));//得到MountService的binder
        if (mSm == null) {
            throw new RemoteException("Failed to find running mount service");
        }

        mArgs = args;
        String op = args[0];
        mNextArg = 1;

        if ("list-disks".equals(op)) {//根据各个命令执行不同函数
            runListDisks();
        } else if ("list-volumes".equals(op)) {
            runListVolumes();
        } else if ("has-adoptable".equals(op)) {
            runHasAdoptable();
        } else if ("get-primary-storage-uuid".equals(op)) {
            runGetPrimaryStorageUuid();
        } else if ("set-force-adoptable".equals(op)) {
            runSetForceAdoptable();
        } else if ("partition".equals(op)) {
            runPartition();
        } else if ("mount".equals(op)) {
            runMount();
        } else if ("unmount".equals(op)) {
            runUnmount();
        } else if ("format".equals(op)) {
            runFormat();
        } else if ("benchmark".equals(op)) {
            runBenchmark();
        } else if ("forget".equals(op)) {
            runForget();
        } else {
            throw new IllegalArgumentException();
        }
    }

我们随便挑个list-volumes命令,执行runListVolumes函数

    public void runListVolumes() throws RemoteException {
        final String filter = nextArg();
        final int filterType;
        if ("public".equals(filter)) {
            filterType = VolumeInfo.TYPE_PUBLIC;
        } else if ("private".equals(filter)) {
            filterType = VolumeInfo.TYPE_PRIVATE;
        } else if ("emulated".equals(filter)) {
            filterType = VolumeInfo.TYPE_EMULATED;
        } else {
            filterType = -1;
        }

        final VolumeInfo[] vols = mSm.getVolumes(0);//调用MountService的getVolumes接口
        for (VolumeInfo vol : vols) {
            if (filterType == -1 || filterType == vol.getType()) {
                final String envState = VolumeInfo.getEnvironmentForState(vol.getState());
                System.out.println(vol.getId() + " " + envState + " " + vol.getFsUuid());
            }
        }
    }


这个就是sm进程的功能,就是给用户一个入口获取MountService的一些状态,和dumpsys mount有点像。

下面是sm支持的一些命令:

root@lte26007:/proc/1667/fd # sm
usage: sm list-disks [adoptable]
       sm list-volumes [public|private|emulated|all]
       sm has-adoptable
       sm get-primary-storage-uuid
       sm set-force-adoptable [true|false]

       sm partition DISK [public|private|mixed] [ratio]
       sm mount VOLUME
       sm unmount VOLUME
       sm format VOLUME
       sm benchmark VOLUME

       sm forget [UUID|all]





你可能感兴趣的:(android存储)