1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef APPSPAWN_H 17 #define APPSPAWN_H 18 19 #include <stdint.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <stdbool.h> 23 #include <unistd.h> 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 /** 30 * @brief appspawn请求消息构造句柄,不支持多线程消息构建 31 * 32 * 根据业务使用AppSpawnReqMsgCreate/AppSpawnTerminateMsgCreate 构建消息 33 * 如果调用AppSpawnClientSendMsg后,消息句柄不需要处理 34 * 否则需要调用 AppSpawnReqMsgFree 释放句柄 35 * 36 * 所有字符串输入的接口,只能接受合法的字符串,输入null、""、和大于合法长度的字符串都返回错误 37 * 38 */ 39 typedef void *AppSpawnReqMsgHandle; 40 41 /** 42 * @brief 支持多线程获取句柄,这个是线程安全的。使用时,全局创建一个句柄,支持多线程发送对应线程的消息请求 43 * 44 */ 45 typedef void *AppSpawnClientHandle; 46 47 #define INVALID_PERMISSION_INDEX (-1) 48 #define INVALID_REQ_HANDLE NULL 49 #define NWEBSPAWN_SERVER_NAME "nwebspawn" 50 #define APPSPAWN_SERVER_NAME "appspawn" 51 #define CJAPPSPAWN_SERVER_NAME "cjappspawn" 52 #define NWEBSPAWN_RESTART "nwebRestart" 53 #define NATIVESPAWN_SERVER_NAME "nativespawn" 54 55 #pragma pack(4) 56 #define APP_MAX_GIDS 64 57 #define APP_USER_NAME 64 58 #define APP_MAX_FD_COUNT 16 59 #define APP_FDENV_PREFIX "APPSPAWN_FD_" 60 #define APP_FDNAME_MAXLEN 20 61 typedef struct { 62 uint32_t uid; // the UNIX uid that the child process setuid() to after fork() 63 uint32_t gid; // the UNIX gid that the child process setgid() to after fork() 64 uint32_t gidCount; // the size of gidTable 65 uint32_t gidTable[APP_MAX_GIDS]; 66 char userName[APP_USER_NAME]; 67 } AppDacInfo; 68 69 typedef struct { 70 int result; 71 pid_t pid; 72 } AppSpawnResult; 73 #pragma pack() 74 75 /** 76 * @brief init spawn client, eg: nwebspawn、appspawn 77 * 78 * @param serviceName service name, eg: nwebspawn、appspawn 79 * @param handle handle for client 80 * @return if succeed return 0,else return other value 81 */ 82 int AppSpawnClientInit(const char *serviceName, AppSpawnClientHandle *handle); 83 /** 84 * @brief destroy client 85 * 86 * @param handle handle for client 87 * @return if succeed return 0,else return other value 88 */ 89 int AppSpawnClientDestroy(AppSpawnClientHandle handle); 90 91 /** 92 * @brief send client request 93 * 94 * @param handle handle for client 95 * @param reqHandle handle for request 96 * @param result result from appspawn service 97 * @return if succeed return 0,else return other value 98 */ 99 int AppSpawnClientSendMsg(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, AppSpawnResult *result); 100 101 /** 102 * @brief send client user lock status request 103 * 104 * @param userId user id 105 * @param isLocked lock status 106 * @return if succeed return 0,else return other value 107 */ 108 int AppSpawnClientSendUserLockStatus(uint32_t userId, bool isLocked); 109 110 typedef enum { 111 MSG_APP_SPAWN = 0, 112 MSG_GET_RENDER_TERMINATION_STATUS, 113 MSG_SPAWN_NATIVE_PROCESS, 114 MSG_DUMP, 115 MSG_BEGET_CMD, 116 MSG_BEGET_SPAWNTIME, 117 MSG_UPDATE_MOUNT_POINTS, 118 MSG_RESTART_SPAWNER, 119 MSG_DEVICE_DEBUG, 120 MSG_UNINSTALL_DEBUG_HAP, 121 MSG_LOCK_STATUS, 122 MAX_TYPE_INVALID 123 } AppSpawnMsgType; 124 125 /** 126 * @brief create spawn request 127 * 128 * @param msgType msg type. eg: MSG_APP_SPAWN,MSG_SPAWN_NATIVE_PROCESS 129 * @param processName process name, max length is 255 130 * @param reqHandle handle for request message 131 * @return if succeed return 0,else return other value 132 */ 133 int AppSpawnReqMsgCreate(AppSpawnMsgType msgType, const char *processName, AppSpawnReqMsgHandle *reqHandle); 134 135 /** 136 * @brief create request 137 * 138 * @param pid process pid 139 * @param reqHandle handle for request message 140 * @return if succeed return 0,else return other value 141 */ 142 int AppSpawnTerminateMsgCreate(pid_t pid, AppSpawnReqMsgHandle *reqHandle); 143 144 /** 145 * @brief destroy request 146 * 147 * @param reqHandle handle for request 148 */ 149 void AppSpawnReqMsgFree(AppSpawnReqMsgHandle reqHandle); 150 151 /** 152 * @brief set bundle info 153 * 154 * @param reqHandle handle for request message 155 * @param bundleIndex bundle index 156 * @param bundleName bundle name, max length is 255 157 * @return if succeed return 0,else return other value 158 */ 159 int AppSpawnReqMsgSetBundleInfo(AppSpawnReqMsgHandle reqHandle, uint32_t bundleIndex, const char *bundleName); 160 161 /** 162 * @brief set app flags info 163 * 164 * @param reqHandle handle for request message 165 * @param flagIndex flags index from AppFlagsIndex 166 * @return if succeed return 0,else return other value 167 */ 168 typedef enum { 169 APP_FLAGS_COLD_BOOT = 0, 170 APP_FLAGS_BACKUP_EXTENSION = 1, 171 APP_FLAGS_DLP_MANAGER = 2, 172 APP_FLAGS_DEBUGGABLE = 3, 173 APP_FLAGS_ASANENABLED = 4, 174 APP_FLAGS_ACCESS_BUNDLE_DIR = 5, 175 APP_FLAGS_NATIVEDEBUG = 6, 176 APP_FLAGS_NO_SANDBOX = 7, 177 APP_FLAGS_OVERLAY = 8, 178 APP_FLAGS_BUNDLE_RESOURCES = 9, 179 APP_FLAGS_GWP_ENABLED_FORCE, // APP_GWP_ENABLED_FORCE 0x400 180 APP_FLAGS_GWP_ENABLED_NORMAL, // APP_GWP_ENABLED_NORMAL 0x800 181 APP_FLAGS_TSAN_ENABLED, // APP_TSANENABLED 0x1000 182 APP_FLAGS_IGNORE_SANDBOX = 13, // ignore sandbox result 183 APP_FLAGS_ISOLATED_SANDBOX, 184 APP_FLAGS_EXTENSION_SANDBOX, 185 APP_FLAGS_CLONE_ENABLE, 186 APP_FLAGS_DEVELOPER_MODE = 17, 187 APP_FLAGS_BEGETCTL_BOOT, // Start an app from begetctl. 188 APP_FLAGS_ATOMIC_SERVICE, 189 APP_FLAGS_CHILDPROCESS, 190 APP_FLAGS_HWASAN_ENABLED = 21, 191 APP_FLAGS_UBSAN_ENABLED = 22, 192 APP_FLAGS_ISOLATED_SANDBOX_TYPE, 193 APP_FLAGS_ISOLATED_SELINUX_LABEL, 194 APP_FLAGS_ISOLATED_SECCOMP_TYPE, 195 APP_FLAGS_ISOLATED_NETWORK, 196 APP_FLAGS_ISOLATED_DATAGROUP, 197 APP_FLAGS_TEMP_JIT = 28, 198 MAX_FLAGS_INDEX = 63, 199 } AppFlagsIndex; 200 201 int AppSpawnReqMsgSetAppFlag(AppSpawnReqMsgHandle reqHandle, AppFlagsIndex flagIndex); 202 203 /** 204 * @brief set dac info 205 * 206 * @param reqHandle handle for request message 207 * @param dacInfo dac info from AppDacInfo 208 * @return if succeed return 0,else return other value 209 */ 210 int AppSpawnReqMsgSetAppDacInfo(AppSpawnReqMsgHandle reqHandle, const AppDacInfo *dacInfo); 211 212 /** 213 * @brief set domain info 214 * 215 * @param reqHandle handle for request message 216 * @param hapFlags hap of flags 217 * @param apl apl value, max length is 31 218 * @return if succeed return 0,else return other value 219 */ 220 int AppSpawnReqMsgSetAppDomainInfo(AppSpawnReqMsgHandle reqHandle, uint32_t hapFlags, const char *apl); 221 222 /** 223 * @brief set internet permission info 224 * 225 * @param reqHandle handle for request message 226 * @param allowInternet 227 * @param setAllowInternet 228 * @return if succeed return 0,else return other value 229 */ 230 int AppSpawnReqMsgSetAppInternetPermissionInfo(AppSpawnReqMsgHandle reqHandle, uint8_t allow, uint8_t setAllow); 231 232 /** 233 * @brief set access token info 234 * 235 * @param reqHandle handle for request message 236 * @param accessTokenIdEx access tokenId 237 * @return if succeed return 0,else return other value 238 */ 239 int AppSpawnReqMsgSetAppAccessToken(AppSpawnReqMsgHandle reqHandle, uint64_t accessTokenIdEx); 240 241 /** 242 * @brief set owner info 243 * 244 * @param reqHandle handle for request message 245 * @param ownerId owner id, max length is 63 246 * @return if succeed return 0,else return other value 247 */ 248 int AppSpawnReqMsgSetAppOwnerId(AppSpawnReqMsgHandle reqHandle, const char *ownerId); 249 250 /** 251 * @brief add permission to message 252 * 253 * @param reqHandle handle for request message 254 * @param permission permission name 255 * @return if succeed return 0,else return other value 256 */ 257 int AppSpawnReqMsgAddPermission(AppSpawnReqMsgHandle reqHandle, const char *permission); 258 259 /** 260 * @brief add permission to message 261 * 262 * @param handle handle for client 263 * @param reqHandle handle for request message 264 * @param permission permission name 265 * @return if succeed return 0,else return other value 266 */ 267 int AppSpawnClientAddPermission(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, const char *permission); 268 269 /** 270 * @brief add extend info to message 271 * 272 * @param reqHandle handle for request message 273 * @param name extend name, max length is 31 274 * @param value extend value, max length is 32768 275 * @param valueLen extend value length 276 * @return if succeed return 0,else return other value 277 */ 278 #define MSG_EXT_NAME_RENDER_CMD "render-cmd" 279 #define MSG_EXT_NAME_HSP_LIST "HspList" 280 #define MSG_EXT_NAME_OVERLAY "Overlay" 281 #define MSG_EXT_NAME_DATA_GROUP "DataGroup" 282 #define MSG_EXT_NAME_APP_ENV "AppEnv" 283 #define MSG_EXT_NAME_APP_EXTENSION "AppExtension" 284 #define MSG_EXT_NAME_BEGET_PID "AppPid" 285 #define MSG_EXT_NAME_BEGET_PTY_NAME "ptyName" 286 #define MSG_EXT_NAME_ACCOUNT_ID "AccountId" 287 #define MSG_EXT_NAME_PROVISION_TYPE "ProvisionType" 288 #define MSG_EXT_NAME_PROCESS_TYPE "ProcessType" 289 #define MSG_EXT_NAME_MAX_CHILD_PROCCESS_MAX "MaxChildProcess" 290 #define MSG_EXT_NAME_APP_FD "AppFd" 291 #define MSG_EXT_NAME_USERID "uid" 292 #define MSG_EXT_NAME_JIT_PERMISSIONS "JITPermissions" 293 294 int AppSpawnReqMsgAddExtInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const uint8_t *value, uint32_t valueLen); 295 296 /** 297 * @brief add extend info to message 298 * 299 * @param reqHandle handle for request message 300 * @param name extend name, max length is 31 301 * @param value extend value, max length is 32767 302 * @return if succeed return 0,else return other value 303 */ 304 int AppSpawnReqMsgAddStringInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const char *value); 305 306 /** 307 * @brief add fd info to message 308 * 309 * @param reqHandle handle for request message 310 * @param name fd name 311 * @param value fd value 312 * @return if succeed return 0,else return other value 313 */ 314 int AppSpawnReqMsgAddFd(AppSpawnReqMsgHandle reqHandle, const char* fdName, int fd); 315 316 /** 317 * @brief Get the permission index by permission name 318 * 319 * @param handle handle for client 320 * @param permission permission name 321 * @return int32_t permission index, if not exit, return INVALID_PERMISSION_INDEX 322 */ 323 int32_t GetPermissionIndex(AppSpawnClientHandle handle, const char *permission); 324 325 /** 326 * @brief Get the max permission Index 327 * 328 * @param handle handle for client 329 * @return int32_t max permission Index 330 */ 331 int32_t GetMaxPermissionIndex(AppSpawnClientHandle handle); 332 333 /** 334 * @brief Get the permission name by index 335 * 336 * @param handle handle for client 337 * @param index permission index 338 * @return const char* permission name 339 */ 340 const char *GetPermissionByIndex(AppSpawnClientHandle handle, int32_t index); 341 342 #ifdef __cplusplus 343 } 344 #endif 345 346 #endif 347