subsys-boot-appspawn.md 6.7 KB
Newer Older
L
laiguizhong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
# appspawn应用孵化组件<a name="ZH-CN_TOPIC_0000001063680582"></a>

## 概述

### 功能简介

  应用孵化器,负责接受应用程序框架的命令孵化应用进程,设置其对应权限,并调用应用程序框架的入口。

### 基本概念

  appspawn注册的服务名称为“appspawn”。appspawn 通过监听本地socket,接收来自客户端的请求消息。消息类型为AppParameter的结构体,定义路径为:“interfaces/innerkits/include/appspawn_msg.h“。

  **表 1**  字段说明
  | 字段名 | 说明 |
  | -------- | -------- |
  | processName | 即将启动的应用服务进程名,最大256字节。 |
  | bundleName | 即将启动的应用程序包名,最大256字节。 |
  | soPath | 即应用程序指定的动态库的路径,最大256字节。 |
  | uid | 即将启动的应用进程的uid。 |
  | gid | 即将启动的应用进程的gid。 |
  | gidTable | 即将启动的应用进程组信息,长度由gidCount指定,最大支持64个进程组,必须为正值。 |
  | gidCount | 即将启动的应用进程组个数。 |
  | accessTokenId | 即应用进程权限控制的token id。 |
  | apl | 即应用进程权限控制的apl,最大32字节. |
  | renderCmd | 即图形图像渲染命令, 最大1024字节。 |
  | flags | 即冷启动标志位。 |
  | pid | 即渲染进程pid,查询渲染进程退出状态。 |
  | AppOperateType | 即App操作类型,0: 默认状态; 1:获取渲染终止状态。 |

### 约束与限制
仅限标准系统下使用

## 开发指导

### 场景介绍

- 安全控制

  支持为app设置SELinux标签。

  SELinux标签接口代码如下:
    ```c++
    AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client;
    HapContext hapContext;
    ret = hapContext.HapDomainSetcontext(appProperty->property.apl, appProperty->property.processName);
    if (ret != 0) {
        APPSPAWN_LOGE("AppSpawnServer::Failed to hap domain set context, errno = %d %s",
            errno, appProperty->property.apl);
    } else {
        APPSPAWN_LOGI("AppSpawnServer::Success to hap domain set context, ret = %d", ret);
    }
    ```
- 应用进程控制

  - 支持为app设置AccessToken。
  - 支持重启前,appspawn停止后,可同时停止所有已孵化的app进程。

  AccessToken接口代码如下:
    ```
    AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client;
    int32_t ret = SetSelfTokenID(appProperty->property.accessTokenId);
    APPSPAWN_LOGI("AppSpawnServer::set access token id = %d, ret = %d %d", appProperty->property.accessTokenId, ret, getuid());
    ```

- 冷启动

  支持通过aa命令冷启动应用。
    ```
C
chengjinsong2 已提交
69
    param set startup.appspawn.cold.boot 1 // 打开冷启动开关
L
laiguizhong 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    aa start -d 12345 -a $name -b $package -C
    参考:
    aa start -d 12345 -a ohos.acts.startup.sysparam.function.MainAbility -b ohos.acts.startup.sysparam.function -C
    ```

- 应用沙盒

  应用独立运行于自身沙盒环境。应用沙盒中,只保留应用依赖的库或文件,同时,应用之间数据的也进行隔离。

### 接口说明

  接口定义路径: “/interfaces/innerkits/include/client_socket.h“,接口说明参见表2。

  **表 2**  接口说明
  | 接口名 | 说明 |
  | -------- | -------- |
  | CreateClient | 创建client。 |
  | CloseClient | 关闭client。 |
  | ConnectSocket | 向appspawn服务发起连接请求。 |
  | WriteSocketMessage | 发送消息到appspawn服务。 |
  | ReadSocketMessage | 接收来自appspawn服务的消息。 |

### 开发步骤

  沙盒配置说明:

L
laiguizhong 已提交
96
  ```
L
laiguizhong 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    {
        "common" : [{                                           // 应用沙盒通用挂载项
            "top-sandbox-switch": "ON",                         // 沙盒总开关 ON: 打开, OFF: 关闭
            "app-base" : [{
                "sandbox-root" : "/mnt/sandbox/<PackageName>",  // 沙盒根路径
                "mount-paths" : [{
                        "src-path" : "/config",                 // mount的源目录
                        "sandbox-path" : "/config",             // 沙盒挂载路径
                        "sandbox-flags" : [ "bind", "rec" ],    // 挂载方式
                        "check-action-status": "false"          // false 不检查当前项挂载结果, true: 检查当前项挂载结果
                    }
                ],
                "symbol-links" : [{                             // link 的目录项
                        "target-name" : "/system/bin",          // link 的源目录
                        "link-name" : "/bin",                   // 链接名称
                        "check-action-status": "false"
                    }
                ]
            }],
        // 应用独有配置参考
        "individual" : [{                                        // 个别应用单独挂载项
            "com.ohos.medialibrary.MediaLibraryDataA" : [{       // 应用名
                "sandbox-switch": "ON",                          // ON: 挂载沙盒路径, OFF: 挂载根路径
                "sandbox-root" : "/mnt/sandbox/<PackageName>",   // 沙盒根路径
                "mount-paths" : [{
                        "src-path" : "/storage/media/<currentUserId>",
                        "sandbox-path" : "/storage/media",
                        "sandbox-flags" : [ "bind", "rec" ],
                        "check-action-status": "false"
                    }
                ],
                "symbol-links" : []
            }]
        }]
    }
  ```

   参考沙盒配置说明,修改配置文件。

   - 进入设备下:/system/etc/sandbox/路径下,直接修改对应沙盒配置文件, 重新启动。
   - 代码路径下:base/startup/appspawn_standard, 修改对应沙盒配置文件。

  **表 3**  沙盒配置文件解释

  | 沙盒配置文件 | 解释 |
  | -------- | -------- |
  | appdata-sandbox64.json | 64位系统的沙盒配置 |
  | appdata-sandbox.json | 32位系统的沙盒配置 |
  | product-sandbox.json  | 应用沙盒的产品差异化配置 |

### 开发实例
以launcher应用新增独有配置应用为例:
  ```c++
  "com.ohos.launcher" : [{
      "sandbox-switch": "ON",
      "sandbox-root" : "/mnt/sandbox/<PackageName>",
      "mount-paths" : [{
              "src-path" : "/data/app/el1/bundle/public/",
              "sandbox-path" : "/data/bundles/",
              "sandbox-flags" : [ "bind", "rec" ],
              "check-action-status": "true"
          }
      ],
      "symbol-links" : []
  }],
  ```

## 常见问题

### 冷启动应用失败

   &emsp;**现象描述**
L
laiguizhong 已提交
169
   <br>&emsp;&emsp;通过命令冷启动应用, 应用拉起失败。
L
laiguizhong 已提交
170 171

   &emsp;**解决方法**
C
chengjinsong2 已提交
172
    <br>&emsp;&emsp;1. 需要设置 param set startup.appspawn.cold.boot 1生效。
L
laiguizhong 已提交
173
    <br>&emsp;&emsp;2. 确认冷启动命令是否正确。