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 <unistd.h> 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 /** 29 * @brief appspawn请求消息构造句柄,不支持多线程消息构建 30 * 31 * 根据业务使用AppSpawnReqMsgCreate/AppSpawnTerminateMsgCreate 构建消息 32 * 如果调用AppSpawnClientSendMsg后,消息句柄不需要处理 33 * 否则需要调用 AppSpawnReqMsgFree 释放句柄 34 * 35 * 所有字符串输入的接口,只能接受合法的字符串,输入null、""、和大于合法长度的字符串都返回错误 36 * 37 */ 38 typedef void *AppSpawnReqMsgHandle; 39 40 /** 41 * @brief 支持多线程获取句柄,这个是线程安全的。使用时,全局创建一个句柄,支持多线程发送对应线程的消息请求 42 * 43 */ 44 typedef void *AppSpawnClientHandle; 45 46 #define INVALID_PERMISSION_INDEX (-1) 47 #define INVALID_REQ_HANDLE NULL 48 #define NWEBSPAWN_SERVER_NAME "nwebspawn" 49 #define APPSPAWN_SERVER_NAME "appspawn" 50 51 #pragma pack(4) 52 #define APP_MAX_GIDS 64 53 #define APP_USER_NAME 64 54 typedef struct { 55 uint32_t uid; // the UNIX uid that the child process setuid() to after fork() 56 uint32_t gid; // the UNIX gid that the child process setgid() to after fork() 57 uint32_t gidCount; // the size of gidTable 58 uint32_t gidTable[APP_MAX_GIDS]; 59 char userName[APP_USER_NAME]; 60 } AppDacInfo; 61 62 typedef struct { 63 int result; 64 pid_t pid; 65 } AppSpawnResult; 66 #pragma pack() 67 68 /** 69 * @brief init spawn client, eg: nwebspawn、appspawn 70 * 71 * @param serviceName service name, eg: nwebspawn、appspawn 72 * @param handle handle for client 73 * @return if succeed return 0,else return other value 74 */ 75 int AppSpawnClientInit(const char *serviceName, AppSpawnClientHandle *handle); 76 /** 77 * @brief destroy client 78 * 79 * @param handle handle for client 80 * @return if succeed return 0,else return other value 81 */ 82 int AppSpawnClientDestroy(AppSpawnClientHandle handle); 83 84 /** 85 * @brief send client request 86 * 87 * @param handle handle for client 88 * @param reqHandle handle for request 89 * @param result result from appspawn service 90 * @return if succeed return 0,else return other value 91 */ 92 int AppSpawnClientSendMsg(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, AppSpawnResult *result); 93 94 typedef enum { 95 MSG_APP_SPAWN = 0, 96 MSG_GET_RENDER_TERMINATION_STATUS, 97 MSG_SPAWN_NATIVE_PROCESS, 98 MSG_DUMP, 99 MSG_BEGET_CMD, 100 MAX_TYPE_INVALID 101 } AppSpawnMsgType; 102 103 /** 104 * @brief create spawn request 105 * 106 * @param msgType msg type. eg: MSG_APP_SPAWN,MSG_SPAWN_NATIVE_PROCESS 107 * @param processName process name, max length is 255 108 * @param reqHandle handle for request message 109 * @return if succeed return 0,else return other value 110 */ 111 int AppSpawnReqMsgCreate(AppSpawnMsgType msgType, const char *processName, AppSpawnReqMsgHandle *reqHandle); 112 113 /** 114 * @brief create request 115 * 116 * @param pid process pid 117 * @param reqHandle handle for request message 118 * @return if succeed return 0,else return other value 119 */ 120 int AppSpawnTerminateMsgCreate(pid_t pid, AppSpawnReqMsgHandle *reqHandle); 121 122 /** 123 * @brief destroy request 124 * 125 * @param reqHandle handle for request 126 */ 127 void AppSpawnReqMsgFree(AppSpawnReqMsgHandle reqHandle); 128 129 /** 130 * @brief set bundle info 131 * 132 * @param reqHandle handle for request message 133 * @param bundleIndex bundle index 134 * @param bundleName bundle name, max length is 255 135 * @return if succeed return 0,else return other value 136 */ 137 int AppSpawnReqMsgSetBundleInfo(AppSpawnReqMsgHandle reqHandle, uint32_t bundleIndex, const char *bundleName); 138 139 /** 140 * @brief set app flags info 141 * 142 * @param reqHandle handle for request message 143 * @param flagIndex flags index from AppFlagsIndex 144 * @return if succeed return 0,else return other value 145 */ 146 typedef enum { 147 APP_FLAGS_COLD_BOOT = 0, 148 APP_FLAGS_BACKUP_EXTENSION = 1, 149 APP_FLAGS_DLP_MANAGER = 2, 150 APP_FLAGS_DEBUGGABLE = 3, 151 APP_FLAGS_ASANENABLED = 4, 152 APP_FLAGS_ACCESS_BUNDLE_DIR = 5, 153 APP_FLAGS_NATIVEDEBUG = 6, 154 APP_FLAGS_NO_SANDBOX = 7, 155 APP_FLAGS_OVERLAY = 8, 156 APP_FLAGS_BUNDLE_RESOURCES = 9, 157 APP_FLAGS_GWP_ENABLED_FORCE, // APP_GWP_ENABLED_FORCE 0x400 158 APP_FLAGS_GWP_ENABLED_NORMAL, // APP_GWP_ENABLED_NORMAL 0x800 159 APP_FLAGS_TSAN_ENABLED, // APP_TSANENABLED 0x1000 160 APP_FLAGS_IGNORE_SANDBOX = 13, // ignore sandbox result 161 APP_FLAGS_ISOLATED_SANDBOX, 162 APP_FLAGS_EXTENSION_SANDBOX, 163 APP_FLAGS_CLONE_ENABLE, 164 APP_FLAGS_DEVELOPER_MODE = 17, 165 APP_FLAGS_BEGETCTL_BOOT, // Start an app from begetctl. 166 APP_FLAGS_ATOMIC_SERVICE, 167 MAX_FLAGS_INDEX = 63, 168 } AppFlagsIndex; 169 170 int AppSpawnReqMsgSetAppFlag(AppSpawnReqMsgHandle reqHandle, AppFlagsIndex flagIndex); 171 172 /** 173 * @brief set dac info 174 * 175 * @param reqHandle handle for request message 176 * @param dacInfo dac info from AppDacInfo 177 * @return if succeed return 0,else return other value 178 */ 179 int AppSpawnReqMsgSetAppDacInfo(AppSpawnReqMsgHandle reqHandle, const AppDacInfo *dacInfo); 180 181 /** 182 * @brief set domain info 183 * 184 * @param reqHandle handle for request message 185 * @param hapFlags hap of flags 186 * @param apl apl value, max length is 31 187 * @return if succeed return 0,else return other value 188 */ 189 int AppSpawnReqMsgSetAppDomainInfo(AppSpawnReqMsgHandle reqHandle, uint32_t hapFlags, const char *apl); 190 191 /** 192 * @brief set internet permission info 193 * 194 * @param reqHandle handle for request message 195 * @param allowInternet 196 * @param setAllowInternet 197 * @return if succeed return 0,else return other value 198 */ 199 int AppSpawnReqMsgSetAppInternetPermissionInfo(AppSpawnReqMsgHandle reqHandle, uint8_t allow, uint8_t setAllow); 200 201 /** 202 * @brief set access token info 203 * 204 * @param reqHandle handle for request message 205 * @param accessTokenIdEx access tokenId 206 * @return if succeed return 0,else return other value 207 */ 208 int AppSpawnReqMsgSetAppAccessToken(AppSpawnReqMsgHandle reqHandle, uint64_t accessTokenIdEx); 209 210 /** 211 * @brief set owner info 212 * 213 * @param reqHandle handle for request message 214 * @param ownerId owner id, max length is 63 215 * @return if succeed return 0,else return other value 216 */ 217 int AppSpawnReqMsgSetAppOwnerId(AppSpawnReqMsgHandle reqHandle, const char *ownerId); 218 219 /** 220 * @brief add permission to message 221 * 222 * @param reqHandle handle for request message 223 * @param permission permission name 224 * @return if succeed return 0,else return other value 225 */ 226 int AppSpawnReqMsgAddPermission(AppSpawnReqMsgHandle reqHandle, const char *permission); 227 228 /** 229 * @brief add permission to message 230 * 231 * @param handle handle for client 232 * @param reqHandle handle for request message 233 * @param permission permission name 234 * @return if succeed return 0,else return other value 235 */ 236 int AppSpawnClientAddPermission(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, const char *permission); 237 238 /** 239 * @brief add extend info to message 240 * 241 * @param reqHandle handle for request message 242 * @param name extend name, max length is 31 243 * @param value extend value, max length is 32768 244 * @param valueLen extend value length 245 * @return if succeed return 0,else return other value 246 */ 247 #define MSG_EXT_NAME_RENDER_CMD "render-cmd" 248 #define MSG_EXT_NAME_HSP_LIST "HspList" 249 #define MSG_EXT_NAME_OVERLAY "Overlay" 250 #define MSG_EXT_NAME_DATA_GROUP "DataGroup" 251 #define MSG_EXT_NAME_APP_ENV "AppEnv" 252 #define MSG_EXT_NAME_BEGET_PID "AppPid" 253 #define MSG_EXT_NAME_BEGET_PTY_NAME "ptyName" 254 #define MSG_EXT_NAME_ACCOUNT_ID "AccountId" 255 256 int AppSpawnReqMsgAddExtInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const uint8_t *value, uint32_t valueLen); 257 258 /** 259 * @brief add extend info to message 260 * 261 * @param reqHandle handle for request message 262 * @param name extend name, max length is 31 263 * @param value extend value, max length is 32767 264 * @return if succeed return 0,else return other value 265 */ 266 int AppSpawnReqMsgAddStringInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const char *value); 267 268 /** 269 * @brief Get the permission index by permission name 270 * 271 * @param handle handle for client 272 * @param permission permission name 273 * @return int32_t permission index, if not exit, return INVALID_PERMISSION_INDEX 274 */ 275 int32_t GetPermissionIndex(AppSpawnClientHandle handle, const char *permission); 276 277 /** 278 * @brief Get the max permission Index 279 * 280 * @param handle handle for client 281 * @return int32_t max permission Index 282 */ 283 int32_t GetMaxPermissionIndex(AppSpawnClientHandle handle); 284 285 /** 286 * @brief Get the permission name by index 287 * 288 * @param handle handle for client 289 * @param index permission index 290 * @return const char* permission name 291 */ 292 const char *GetPermissionByIndex(AppSpawnClientHandle handle, int32_t index); 293 294 #ifdef __cplusplus 295 } 296 #endif 297 298 #endif 299