Android应用生死轮回的那些事儿(1) - installd初探

Android应用生死轮回的那些事儿(1) - installd初探

前面我们讨论过dex2oat的过程,下面我们开始探索系统中的重要工具进程,installd守护进程。

我们有PackageManageService这么大的组件了,为什么还需要installd这个守护进程呢?
一句话,是因为权限的问题,PMS只有system权限。installd的作用就是处理需要root权限的操作。
这里写图片描述

installd支持的命令列表

这里写图片描述

4.4中的cmds

4.4版中的命令只有16个

struct cmdinfo cmds[] = {
    { "ping",                 0, do_ping },
    { "install",              4, do_install },
    { "dexopt",               3, do_dexopt },
    { "movedex",              2, do_move_dex },
    { "rmdex",                1, do_rm_dex },
    { "remove",               2, do_remove },
    { "rename",               2, do_rename },
    { "fixuid",               3, do_fixuid },
    { "freecache",            1, do_free_cache },
    { "rmcache",              2, do_rm_cache },
    { "getsize",              6, do_get_size },
    { "rmuserdata",           2, do_rm_user_data },
    { "movefiles",            0, do_f },
    { "linklib",              3, do_linklib },
    { "mkuserdata",           3, do_mk_user_data },
    { "rmuser",               1, do_rm_user },
};

中间的数字表示需要的参数个数:

struct cmdinfo {
    const char *name;
    unsigned numargs;
    int (*func)(char **arg, char reply[REPLY_MAX]);
};
4.4命令总结
  • ping:空命令
  • install:用于安装应用
  • dexopt: 6.0生成dex和oat,5.x之前只管生成dex。
  • movedex:把应用从一个目录移动到另一个目录
  • rmdex:删除应用文件
  • remove:反安装
  • rename:更改数据目录名称
  • fixuid:更改应用的uid
  • freeecache:清除cache目录下的文件
  • rmcache:清除cache目录
  • getsize:计算一个应用占用的空间大小
  • rmuserdata:删除一个用户的所有应用
  • movefiles:运行updatecmds目录下的文件,根据文件描述的内容复制文件
  • linklib:为动态库建立符号链接
  • mkuserdata:为用户创建目录
  • rmuser:删除一个用户的所有文件

5.x版本中的cmds

在5.1版本中,命令一共支持22个。

struct cmdinfo cmds[] = {
    { "ping",                 0, do_ping },
    { "install",              4, do_install },
    { "dexopt",               6, do_dexopt },
    { "markbootcomplete",     1, do_mark_boot_complete },
    { "movedex",              3, do_move_dex },
    { "rmdex",                2, do_rm_dex },
    { "remove",               2, do_remove },
    { "rename",               2, do_rename },
    { "fixuid",               3, do_fixuid },
    { "freecache",            1, do_free_cache },
    { "rmcache",              2, do_rm_cache },
    { "rmcodecache",          2, do_rm_code_cache },
    { "getsize",              7, do_get_size },
    { "rmuserdata",           2, do_rm_user_data },
    { "movefiles",            0, do_movefiles },
    { "linklib",              3, do_linklib },
    { "mkuserdata",           4, do_mk_user_data },
    { "mkuserconfig",         1, do_mk_user_config },
    { "rmuser",               1, do_rm_user },
    { "idmap",                3, do_idmap },
    { "restorecondata",       3, do_restorecon_data },
    { "patchoat",             5, do_patchoat },
};

5.0新增命令6个:
* markbootcomplete: 通知启动完成
* mkuserconfig:5.0新增,为用户创建配置文件
* rmcodecache: 清除代码缓存文件
* idmap:对两个应用的id进行map
* restorecondata:恢复selinux的安全上下文
* patchoat: 对OAT文件进行复定位

6.0版中支持的cmds

从installd.cpp中查看,6.0版本支持下面这些命令,共计25个。

struct cmdinfo cmds[] = {
    { "ping",                 0, do_ping },
    { "install",              5, do_install },
    { "dexopt",               10, do_dexopt },
    { "markbootcomplete",     1, do_mark_boot_complete },
    { "movedex",              3, do_move_dex },
    { "rmdex",                2, do_rm_dex },
    { "remove",               3, do_remove },
    { "rename",               2, do_rename },
    { "fixuid",               4, do_fixuid },
    { "freecache",            2, do_free_cache },
    { "rmcache",              3, do_rm_cache },
    { "rmcodecache",          3, do_rm_code_cache },
    { "getsize",              8, do_get_size },
    { "rmuserdata",           3, do_rm_user_data },
    { "cpcompleteapp",        6, do_cp_complete_app },
    { "movefiles",            0, do_movefiles },
    { "linklib",              4, do_linklib },
    { "mkuserdata",           5, do_mk_user_data },
    { "mkuserconfig",         1, do_mk_user_config },
    { "rmuser",               2, do_rm_user },
    { "idmap",                3, do_idmap },
    { "restorecondata",       4, do_restorecon_data },
    { "createoatdir",         2, do_create_oat_dir },
    { "rmpackagedir",         1, do_rm_package_dir },
    { "linkfile",             3, do_link_file }
};

6.0版本新增4个命令
* cpcompleteapp:复制整个app
* createoatdir:创建OAT的目录
* rmpackagedir:删除包的目录
* linkfile:对文件建立符号链接

并将patchoat这个马甲命令,变回它的原来模样dexopt.

4.4开始就有,后来也没有变化的命令

ping命令

命令参数:0个
版本兼容性:全兼容

static int do_ping(char **arg __unused, char reply[REPLY_MAX] __unused)
{
    return 0;
}
rename

改名是个老的API,三个版本都是兼容的。
参数:
* oldpkgname - 旧的包名
* newpkgname - 新的包名

static int do_rename(char **arg, char reply[REPLY_MAX] __unused)
{
    return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
}

这个的实现也非常简单,主要就是rename嘛:

int renamepkg(const char *oldpkgname, const char *newpkgname)
{
    char oldpkgdir[PKG_PATH_MAX];
    char newpkgdir[PKG_PATH_MAX];

    if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
        return -1;
    if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
        return -1;

    if (rename(oldpkgdir, newpkgdir) < 0) {
        ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
        return -errno;
    }
    return 0;
}
movefiles

执行/system/etc/updatecmds/目录下的文件命令,所以不需要任何参数。

static int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused)
{
    return movefiles();
}

目录定义在:

#define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/"

4.4就有,但是6.0新加了uuid参数的命令

linklib

参数:
* uuid
* pkgname
* asecLibDir
* userId

这个do_linklib写得不好,没有注释。

static int do_linklib(char **arg, char reply[REPLY_MAX] __unused)
{
    return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
}

不过,我们可以看linklib函数的定义来看这些参数:

int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
install命令

参数:4.4和5.x是4个参数,到了6.0变成5个了。
* uuid: 6.0新增
* pkgname
* uid
* gid
* seinfo

6.0版本的install:

static int do_install(char **arg, char reply[REPLY_MAX] __unused)
{
    return install(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]); /* uuid, pkgname, uid, gid, seinfo */
}

5.x之前的install:

static int do_install(char **arg, char reply[REPLY_MAX])
{
    return install(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]); /* pkgname, uid, gid, seinfo */
}

这个重要命令我们后面专题分析

remove

参数:6.0:3个,5.x及4.4:后面两个
* uuid:6.0新增
* pkgname
* userid

static int do_remove(char **arg, char reply[REPLY_MAX] __unused)
{
    return uninstall(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
}
fixuid

还是6.0新增了uuid参数,其它三个版本都一致。
参数:
* uuid
* pkgname:包名
* uid:userid
* gid: groupid

static int do_fixuid(char **arg, char reply[REPLY_MAX] __unused)
{
    return fix_uid(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3])); /* uuid, pkgname, uid, gid */
}
freecache

同样,6.0新增了uuid

参数:
* uuid
* free_size

static int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */
{
    return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */
}
rmcache

同上,6.0新增一个UUID参数。
* uuid
* pkgname
* userid

static int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused)
{
    return delete_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
}
getsize

6.0多了UUID,其它基本一样。

static int do_get_size(char **arg, char reply[REPLY_MAX])
{
    int64_t codesize = 0;
    int64_t datasize = 0;
    int64_t cachesize = 0;
    int64_t asecsize = 0;
    int res = 0;

        /* uuid, pkgdir, userid, apkpath */
    res = get_size(parse_null(arg[0]), arg[1], atoi(arg[2]), arg[3], arg[4], arg[5], arg[6],
            arg[7], &codesize, &datasize, &cachesize, &asecsize);

    /*
     * Each int64_t can take up 22 characters printed out. Make sure it
     * doesn't go over REPLY_MAX in the future.
     */
    snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
            codesize, datasize, cachesize, asecsize);
    return res;
}
rmuserdata

6.0还是只增加了UUID。
参数:

  • uuid
  • pkgname
  • userid
static int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused)
{
    return delete_user_data(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
}
rmuser

参数:

  • uuid
  • userid
static int do_rm_user(char **arg, char reply[REPLY_MAX] __unused)
{
    return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */
}

4.4就有,但是5.0开始增加了指令集参数的命令

movedex

参数:5.0之后3个,因为oat需要指令集信息,4.4只需要前2个。
* src:源地址
* dst:目标地址
* instruction_set:指令集,5.0新增。

static int do_move_dex(char **arg, char reply[REPLY_MAX] __unused)
{
    return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
}
rmdex

参数:5.0以后2个,4.4,1个
* pkgname:包名
* 指令集

static int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused)
{
    return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
}

每个版本都有,但是参数都有变化的命令

dexopt

参数:6.0:10个, 5.x:7个, 4.4:3个
* apk_path
* uid
* is_public
* pkgname,4.4没有
* instruction_set,4.4没有
* dexopt_needed:6.0新增,5.x没有
* vm_saft_mode,4.4没有
* debuggable:6.0新增
* oat_dir:6.0新增
* boot_complete:6.0新增
* should_relocate: 6.0不支持,5.x的值,4.4不支持

6.0的dexopt:

static int do_dexopt(char **arg, char reply[REPLY_MAX] __unused)
{
    /* apk_path, uid, is_public, pkgname, instruction_set,
     * dexopt_needed, vm_safe_mode, debuggable, oat_dir, boot_complete */
    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]),
                  atoi(arg[6]), atoi(arg[7]), arg[8], atoi(arg[9]));
}

5.1的dexopt:

static int do_dexopt(char **arg, char reply[REPLY_MAX])
{
    /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate */
    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]), 0);
}

4.4的dexopt
只支持3个参数。

static int do_dexopt(char **arg, char reply[REPLY_MAX])
{
        /* apk_path, uid, is_public */
    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]));
}
mk_user_data

6.0新增了uuid,5.0开始新增seinfo

static int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused)
{
    return make_user_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]);
                             /* uuid, pkgname, uid, userid, seinfo */
}

5.0开始支持的命令

markbootcomplete

参数:1个,指令集
6.0与5.x一致,4.4不支持。

static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused)
{
    return mark_boot_complete(arg[0] /* instruction set */);
}
rmcodecache

参数:

  • uuid
  • pkgname
  • userid
static int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused)
{
    return delete_code_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
}
mkuserconfig

就一个参数:userid

static int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused)
{
    return make_user_config(atoi(arg[0])); /* userid */
}
restorecondata

5.0新增,6.0增加uuid
参数:

  • uuid
  • pkgName
  • seinfo
  • uid
static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
{
    return restorecon_data(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
                             /* uuid, pkgName, seinfo, uid*/
}
idmap

调用外部的/system/bin/idmap命令。

static int do_idmap(char **arg, char reply[REPLY_MAX] __unused)
{
    return idmap(arg[0], arg[1], atoi(arg[2]));
}

6.0新增的命令

cpcompleteapp

参数:

  • from_uuid
  • to_uuid
  • package_name
  • data_app_name
  • appid
  • seinfo
static int do_cp_complete_app(char **arg, char reply[REPLY_MAX] __unused)
{
    // from_uuid, to_uuid, package_name, data_app_name, appid, seinfo
    return copy_complete_app(parse_null(arg[0]), parse_null(arg[1]), arg[2], arg[3], atoi(arg[4]), arg[5]);
}
createoatdir

还是两个参数:

  • oat_dir:目录名
  • instruction_set:指令集
static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused)
{
    /* oat_dir, instruction_set */
    return create_oat_dir(arg[0], arg[1]);
}
rmpackagedir

就一个参数:
* oat_dir

用处就一个,删除oat目录

static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused)
{
    /* oat_dir */
    return rm_package_dir(arg[0]);
}
linkfile

参数:
* 相对路径
* from_base
* to_base

static int do_link_file(char **arg, char reply[REPLY_MAX] __unused)
{
    /* relative_path, from_base, to_base */
    return link_file(arg[0], arg[1], arg[2]);
}

6.0中取消了的命令

patchoat

短命的命令,只在5.x中才有,6.0中取消了。其实在5.1中也就是个dexopt的马甲,所以6.0中干脆就取消了。

static int do_patchoat(char **arg, char reply[REPLY_MAX]) {
    /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate */
    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 0, 1);
}

这一章我们只是走马观花看一下,下一章我们将深入这些命令,看看应用管理都有一些什么样的installd操作要做,以及它们上层的PMS的逻辑。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jtag特工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值