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 #define HYBRIDSPAWN_SERVER_NAME "hybridspawn" 55 56 #pragma pack(4) 57 #define APP_MAX_GIDS 64 58 #define APP_USER_NAME 64 59 #define APP_MAX_FD_COUNT 16 60 #define APP_FDENV_PREFIX "APPSPAWN_FD_" 61 #define APP_FDNAME_MAXLEN 20 62 #define PIPE_FD_LENGTH 2 63 64 typedef struct { 65 uint32_t uid; // the UNIX uid that the child process setuid() to after fork() 66 uint32_t gid; // the UNIX gid that the child process setgid() to after fork() 67 uint32_t gidCount; // the size of gidTable 68 uint32_t gidTable[APP_MAX_GIDS]; 69 char userName[APP_USER_NAME]; 70 } AppDacInfo; 71 72 typedef struct { 73 int result; 74 pid_t pid; 75 } AppSpawnResult; 76 #pragma pack() 77 78 /** 79 * @brief init spawn client, eg: nwebspawn、appspawn 80 * 81 * @param serviceName service name, eg: nwebspawn、appspawn 82 * @param handle handle for client 83 * @return if succeed return 0,else return other value 84 */ 85 int AppSpawnClientInit(const char *serviceName, AppSpawnClientHandle *handle); 86 /** 87 * @brief destroy client 88 * 89 * @param handle handle for client 90 * @return if succeed return 0,else return other value 91 */ 92 int AppSpawnClientDestroy(AppSpawnClientHandle handle); 93 94 /** 95 * @brief send client request 96 * 97 * @param handle handle for client 98 * @param reqHandle handle for request 99 * @param result result from appspawn service 100 * @return if succeed return 0,else return other value 101 */ 102 int AppSpawnClientSendMsg(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, AppSpawnResult *result); 103 104 /** 105 * @brief send client user lock status request 106 * 107 * @param userId user id 108 * @param isLocked lock status 109 * @return if succeed return 0,else return other value 110 */ 111 int AppSpawnClientSendUserLockStatus(uint32_t userId, bool isLocked); 112 113 typedef enum { 114 MSG_APP_SPAWN = 0, 115 MSG_GET_RENDER_TERMINATION_STATUS, 116 MSG_SPAWN_NATIVE_PROCESS, 117 MSG_DUMP, 118 MSG_BEGET_CMD, 119 MSG_BEGET_SPAWNTIME, 120 MSG_UPDATE_MOUNT_POINTS, 121 MSG_RESTART_SPAWNER, 122 MSG_DEVICE_DEBUG, 123 MSG_UNINSTALL_DEBUG_HAP, 124 MSG_LOCK_STATUS, 125 MSG_OBSERVE_PROCESS_SIGNAL_STATUS, 126 MSG_LOAD_WEBLIB_IN_APPSPAWN, 127 MAX_TYPE_INVALID 128 } AppSpawnMsgType; 129 130 /** 131 * @brief create spawn request 132 * 133 * @param msgType msg type. eg: MSG_APP_SPAWN,MSG_SPAWN_NATIVE_PROCESS 134 * @param processName process name, max length is 255 135 * @param reqHandle handle for request message 136 * @return if succeed return 0,else return other value 137 */ 138 int AppSpawnReqMsgCreate(AppSpawnMsgType msgType, const char *processName, AppSpawnReqMsgHandle *reqHandle); 139 140 /** 141 * @brief create request 142 * 143 * @param pid process pid 144 * @param reqHandle handle for request message 145 * @return if succeed return 0,else return other value 146 */ 147 int AppSpawnTerminateMsgCreate(pid_t pid, AppSpawnReqMsgHandle *reqHandle); 148 149 /** 150 * @brief destroy request 151 * 152 * @param reqHandle handle for request 153 */ 154 void AppSpawnReqMsgFree(AppSpawnReqMsgHandle reqHandle); 155 156 /** 157 * @brief set bundle info 158 * 159 * @param reqHandle handle for request message 160 * @param bundleIndex bundle index 161 * @param bundleName bundle name, max length is 255 162 * @return if succeed return 0,else return other value 163 */ 164 int AppSpawnReqMsgSetBundleInfo(AppSpawnReqMsgHandle reqHandle, uint32_t bundleIndex, const char *bundleName); 165 166 /** 167 * @brief set app flags info 168 * 169 * @param reqHandle handle for request message 170 * @param flagIndex flags index from AppFlagsIndex 171 * @return if succeed return 0,else return other value 172 */ 173 typedef enum { 174 APP_FLAGS_COLD_BOOT = 0, 175 APP_FLAGS_BACKUP_EXTENSION = 1, 176 APP_FLAGS_DLP_MANAGER = 2, 177 APP_FLAGS_DEBUGGABLE = 3, 178 APP_FLAGS_ASANENABLED = 4, 179 APP_FLAGS_ACCESS_BUNDLE_DIR = 5, 180 APP_FLAGS_NATIVEDEBUG = 6, 181 APP_FLAGS_NO_SANDBOX = 7, 182 APP_FLAGS_OVERLAY = 8, 183 APP_FLAGS_BUNDLE_RESOURCES = 9, 184 APP_FLAGS_GWP_ENABLED_FORCE, // APP_GWP_ENABLED_FORCE 0x400 185 APP_FLAGS_GWP_ENABLED_NORMAL, // APP_GWP_ENABLED_NORMAL 0x800 186 APP_FLAGS_TSAN_ENABLED, // APP_TSANENABLED 0x1000 187 APP_FLAGS_IGNORE_SANDBOX = 13, // ignore sandbox result 188 APP_FLAGS_ISOLATED_SANDBOX, 189 APP_FLAGS_EXTENSION_SANDBOX, 190 APP_FLAGS_CLONE_ENABLE, 191 APP_FLAGS_DEVELOPER_MODE = 17, 192 APP_FLAGS_BEGETCTL_BOOT, // Start an app from begetctl. 193 APP_FLAGS_ATOMIC_SERVICE, 194 APP_FLAGS_CHILDPROCESS, 195 APP_FLAGS_HWASAN_ENABLED = 21, 196 APP_FLAGS_UBSAN_ENABLED = 22, 197 APP_FLAGS_ISOLATED_SANDBOX_TYPE, 198 APP_FLAGS_ISOLATED_SELINUX_LABEL, 199 APP_FLAGS_ISOLATED_SECCOMP_TYPE, 200 APP_FLAGS_ISOLATED_NETWORK, 201 APP_FLAGS_ISOLATED_DATAGROUP, 202 APP_FLAGS_TEMP_JIT = 28, 203 APP_FLAGS_PRE_INSTALLED_HAP = 29, 204 APP_FLAGS_GET_ALL_PROCESSES = 30, 205 APP_FLAGS_CUSTOM_SANDBOX = 31, 206 APP_FLAGS_SET_CAPS_FOWNER, 207 APP_FLAGS_ALLOW_IOURING = 33, 208 APP_FLAGS_UNLOCKED_STATUS = 34, 209 APP_FLAGS_FILE_CROSS_APP = 35, 210 APP_FLAGS_FILE_ACCESS_COMMON_DIR = 36, 211 MAX_FLAGS_INDEX = 63, 212 } AppFlagsIndex; 213 214 int AppSpawnReqMsgSetAppFlag(AppSpawnReqMsgHandle reqHandle, AppFlagsIndex flagIndex); 215 216 /** 217 * @brief set dac info 218 * 219 * @param reqHandle handle for request message 220 * @param dacInfo dac info from AppDacInfo 221 * @return if succeed return 0,else return other value 222 */ 223 int AppSpawnReqMsgSetAppDacInfo(AppSpawnReqMsgHandle reqHandle, const AppDacInfo *dacInfo); 224 225 /** 226 * @brief set domain info 227 * 228 * @param reqHandle handle for request message 229 * @param hapFlags hap of flags 230 * @param apl apl value, max length is 31 231 * @return if succeed return 0,else return other value 232 */ 233 int AppSpawnReqMsgSetAppDomainInfo(AppSpawnReqMsgHandle reqHandle, uint32_t hapFlags, const char *apl); 234 235 /** 236 * @brief set internet permission info 237 * 238 * @param reqHandle handle for request message 239 * @param allowInternet 240 * @param setAllowInternet 241 * @return if succeed return 0,else return other value 242 */ 243 int AppSpawnReqMsgSetAppInternetPermissionInfo(AppSpawnReqMsgHandle reqHandle, uint8_t allow, uint8_t setAllow); 244 245 /** 246 * @brief set access token info 247 * 248 * @param reqHandle handle for request message 249 * @param accessTokenIdEx access tokenId 250 * @return if succeed return 0,else return other value 251 */ 252 int AppSpawnReqMsgSetAppAccessToken(AppSpawnReqMsgHandle reqHandle, uint64_t accessTokenIdEx); 253 254 /** 255 * @brief set owner info 256 * 257 * @param reqHandle handle for request message 258 * @param ownerId owner id, max length is 63 259 * @return if succeed return 0,else return other value 260 */ 261 int AppSpawnReqMsgSetAppOwnerId(AppSpawnReqMsgHandle reqHandle, const char *ownerId); 262 263 /** 264 * @brief add permission to message 265 * 266 * @param reqHandle handle for request message 267 * @param permission permission name 268 * @return if succeed return 0,else return other value 269 */ 270 int AppSpawnReqMsgAddPermission(AppSpawnReqMsgHandle reqHandle, const char *permission); 271 272 /** 273 * @brief add permission to message 274 * 275 * @param handle handle for client 276 * @param reqHandle handle for request message 277 * @param permission permission name 278 * @return if succeed return 0,else return other value 279 */ 280 int AppSpawnClientAddPermission(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, const char *permission); 281 282 /** 283 * @brief add extend info to message 284 * 285 * @param reqHandle handle for request message 286 * @param name extend name, max length is 31 287 * @param value extend value, max length is 32768 288 * @param valueLen extend value length 289 * @return if succeed return 0,else return other value 290 */ 291 #define MSG_EXT_NAME_RENDER_CMD "render-cmd" 292 #define MSG_EXT_NAME_HSP_LIST "HspList" 293 #define MSG_EXT_NAME_OVERLAY "Overlay" 294 #define MSG_EXT_NAME_DATA_GROUP "DataGroup" 295 #define MSG_EXT_NAME_APP_ENV "AppEnv" 296 #define MSG_EXT_NAME_APP_EXTENSION "AppExtension" 297 #define MSG_EXT_NAME_BEGET_PID "AppPid" 298 #define MSG_EXT_NAME_BEGET_PTY_NAME "ptyName" 299 #define MSG_EXT_NAME_ACCOUNT_ID "AccountId" 300 #define MSG_EXT_NAME_PROVISION_TYPE "ProvisionType" 301 #define MSG_EXT_NAME_PROCESS_TYPE "ProcessType" 302 #define MSG_EXT_NAME_MAX_CHILD_PROCCESS_MAX "MaxChildProcess" 303 #define MSG_EXT_NAME_APP_FD "AppFd" 304 #define MSG_EXT_NAME_JIT_PERMISSIONS "Permissions" 305 #define MSG_EXT_NAME_USERID "uid" 306 #define MSG_EXT_NAME_EXTENSION_TYPE "ExtensionType" 307 #define MSG_EXT_NAME_API_TARGET_VERSION "APITargetVersion" 308 #define MSG_EXT_NAME_PARENT_UID "ParentUid" 309 310 int AppSpawnReqMsgAddExtInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const uint8_t *value, uint32_t valueLen); 311 312 /** 313 * @brief add extend info to message 314 * 315 * @param reqHandle handle for request message 316 * @param name extend name, max length is 31 317 * @param value extend value, max length is 32767 318 * @return if succeed return 0,else return other value 319 */ 320 int AppSpawnReqMsgAddStringInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const char *value); 321 322 /** 323 * @brief add fd info to message 324 * 325 * @param reqHandle handle for request message 326 * @param name fd name 327 * @param value fd value 328 * @return if succeed return 0,else return other value 329 */ 330 int AppSpawnReqMsgAddFd(AppSpawnReqMsgHandle reqHandle, const char* fdName, int fd); 331 332 /** 333 * @brief Get the permission index by permission name 334 * 335 * @param handle handle for client 336 * @param permission permission name 337 * @return int32_t permission index, if not exit, return INVALID_PERMISSION_INDEX 338 */ 339 int32_t GetPermissionIndex(AppSpawnClientHandle handle, const char *permission); 340 341 /** 342 * @brief Get the max permission Index 343 * 344 * @param handle handle for client 345 * @return int32_t max permission Index 346 */ 347 int32_t GetMaxPermissionIndex(AppSpawnClientHandle handle); 348 349 /** 350 * @brief Get the permission name by index 351 * 352 * @param handle handle for client 353 * @param index permission index 354 * @return const char* permission name 355 */ 356 const char *GetPermissionByIndex(AppSpawnClientHandle handle, int32_t index); 357 358 /** 359 * @brief set up a pipe fd to capture the exit reason of appspawn's child process 360 * 361 * @param fd fd for write signal info 362 * @return if succeed return 0,else return other value 363 */ 364 int SpawnListenFdSet(int fd); 365 366 /** 367 * @brief close the listener for appspawn's child process exits 368 * 369 * @return if succeed return 0,else return other value 370 */ 371 int SpawnListenCloseSet(void); 372 373 /** 374 * @brief set up a pipe fd to capture the exit reason of nativespawn's child process 375 * 376 * @param fd fd for write signal info 377 * @return if succeed return 0,else return other value 378 */ 379 int NativeSpawnListenFdSet(int fd); 380 381 /** 382 * @brief close the listener for nativespawn's child process exits 383 * 384 * @return if succeed return 0,else return other value 385 */ 386 int NativeSpawnListenCloseSet(void); 387 388 #ifdef __cplusplus 389 } 390 #endif 391 392 #endif 393