1 /* 2 * Copyright (c) 2021-2025 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 OHOS_ABILITY_RUNTIME_APP_SPAWN_CLIENT_H 17 #define OHOS_ABILITY_RUNTIME_APP_SPAWN_CLIENT_H 18 19 #include <array> 20 #include <map> 21 #include <memory> 22 #include <set> 23 #include <string> 24 #include <unistd.h> 25 #include <vector> 26 27 #include "appexecfwk_errors.h" 28 #include "appspawn.h" 29 #include "bundle_info.h" 30 #ifdef SUPPORT_CHILD_PROCESS 31 #include "child_process_info.h" 32 #endif // SUPPORT_CHILD_PROCESS 33 #include "data_group_info.h" 34 #include "nocopyable.h" 35 #include "shared/base_shared_bundle_info.h" 36 37 namespace OHOS { 38 namespace AppExecFwk { 39 enum class SpawnConnectionState { STATE_NOT_CONNECT, STATE_CONNECTED, STATE_CONNECT_FAILED }; 40 using HspList = std::vector<BaseSharedBundleInfo>; 41 using DataGroupInfoList = std::vector<DataGroupInfo>; 42 using JITPermissionsMap = std::map<std::string, std::string>; 43 const int32_t MAX_FLAG_INDEX = 32; 44 const int32_t MAX_PROC_NAME_LEN = 256; 45 const int32_t START_FLAG_BASE = 1; 46 const int32_t MAX_COST_TIME = 500; 47 struct AppSpawnStartMsg { 48 uint8_t setAllowInternet; 49 uint8_t allowInternet; // hap socket allowed 50 uint8_t reserved1; 51 uint8_t reserved2; 52 bool atomicServiceFlag = false; 53 bool isolatedExtension = false; // whether is isolatedExtension 54 bool strictMode = false; // whether is strict mode 55 bool isolationMode = false; 56 bool isolatedNetworkFlag = false; 57 bool isolatedSELinuxFlag = false; 58 bool isolatedSandboxFlagLegacy = false; // APP_FLAGS_EXTENSION_SANDBOX legacy 59 bool isScreenLockDataProtect = false; 60 bool isCustomSandboxFlag = false; 61 int32_t uid; 62 int32_t gid; 63 int32_t pid; 64 int32_t code = 0; // 0: default, MSG_APP_SPAWN; 1: MSG_SPAWN_NATIVE_PROCESS; 2: MSG_GET_RENDER_TERMINATION_STATUS 65 int32_t bundleIndex; // when dlp launch another app used, default is 0 66 int32_t maxChildProcess = 0; 67 #ifdef SUPPORT_CHILD_PROCESS 68 int32_t childProcessType = CHILD_PROCESS_TYPE_NOT_CHILD; 69 #endif // SUPPORT_CHILD_PROCESS 70 uint32_t accessTokenId; 71 uint32_t flags; 72 uint32_t hapFlags = 0; // whether is pre installed hap 73 uint32_t mountPermissionFlags; 74 uint64_t accessTokenIdEx; 75 std::vector<int32_t> gids; 76 std::string procName; 77 std::string soPath; 78 std::string apl; 79 std::string bundleName; 80 std::string renderParam; // only nweb spawn need this param. 81 std::string overlayInfo; // overlay hap resource path list 82 std::string ownerId; 83 std::string provisionType; 84 std::string atomicAccount = ""; 85 std::string extensionSandboxPath; 86 std::string processType = ""; 87 std::string extensionTypeName; 88 HspList hspList; // list of harmony shared package 89 std::set<std::string> permissions; 90 std::map<std::string, std::string> appEnv; // environment variable to be set to the process 91 std::map<std::string, int32_t> fds; 92 DataGroupInfoList dataGroupInfoList; // list of harmony shared package 93 JITPermissionsMap jitPermissionsMap; // map of JIT permissions 94 }; 95 96 constexpr auto LEN_PID = sizeof(pid_t); 97 struct StartFlags { 98 static const int COLD_START = 0; 99 static const int BACKUP_EXTENSION = 1; 100 static const int DLP_MANAGER = 2; 101 static const int DEBUGGABLE = 3; 102 static const int ASANENABLED = 4; 103 static const int ACCESS_BUNDLE_DIR = 5; 104 static const int NATIVEDEBUG = 6; 105 static const int NO_SANDBOX = 7; 106 static const int OVERLAY = 8; 107 static const int BUNDLE_RESOURCES = 9; 108 static const int GWP_ENABLED_FORCE = 10; 109 static const int GWP_ENABLED_NORMAL = 11; 110 static const int TSANENABLED = 12; 111 static const int EXTENSION_CONTROLLED = 13; 112 static const int HWASANENABLED = 21; 113 static const int UBSANENABLED = 22; 114 static const int TEMP_JIT_ALLOW = 28; 115 }; 116 117 struct CreateStartMsgParam { 118 bool strictMode = false; 119 bool networkEnableFlags = true; 120 bool saEnableFlags = true; 121 ExtensionAbilityType extensionAbilityType = ExtensionAbilityType::UNSPECIFIED; 122 uint32_t startFlags = 0; 123 int32_t uid = -1; 124 int32_t bundleIndex = 0; 125 std::shared_ptr<AAFwk::Want> want = nullptr; 126 std::string moduleName; 127 std::string abilityName; 128 std::string processName; 129 BundleInfo bundleInfo; 130 BundleType bundleType = BundleType::APP; 131 }; 132 133 union AppSpawnPidMsg { 134 pid_t pid = 0; 135 char pidBuf[LEN_PID]; 136 }; 137 138 class AppSpawnClient { 139 public: 140 /** 141 * Constructor. 142 */ 143 explicit AppSpawnClient(bool isNWebSpawn = false); 144 145 /** 146 * Constructor by service name 147 */ 148 explicit AppSpawnClient(const char* serviceName); 149 150 /** 151 * Destructor 152 */ 153 virtual ~AppSpawnClient(); 154 155 /** 156 * Disable copy. 157 */ 158 DISALLOW_COPY_AND_MOVE(AppSpawnClient); 159 160 /** 161 * Try connect to appSpawn. 162 */ 163 ErrCode OpenConnection(); 164 165 /** 166 * Close the connect of appspawn. 167 */ 168 void CloseConnection(); 169 170 /** 171 * Return the connect state. 172 */ 173 SpawnConnectionState QueryConnectionState() const; 174 175 /** 176 * Return the clent handle. 177 */ 178 AppSpawnClientHandle GetAppSpawnClientHandle() const; 179 180 /** 181 * Set dac info. 182 * 183 * @param startMsg, request message. 184 * @param reqHandle, handle for request message 185 */ 186 int32_t SetDacInfo(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 187 188 /** 189 * Set mount permission. 190 * 191 * @param startMsg, request message. 192 * @param reqHandle, handle for request message 193 */ 194 int32_t SetMountPermission(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 195 196 /** 197 * Set start flags. 198 * 199 * @param startMsg, request message. 200 * @param reqHandle, handle for request message 201 */ 202 int32_t SetStartFlags(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 203 204 /** 205 * Set extra info: render-cmd, HspList, Overlay, DataGroup, AppEnv. 206 * 207 * @param startMsg, request message. 208 * @param reqHandle, handle for request message 209 */ 210 int32_t AppspawnSetExtMsg(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 211 212 /** 213 * Set extra info: provision_type, max_child_process. 214 * 215 * @param startMsg, request message. 216 * @param reqHandle, handle for request message 217 */ 218 int32_t AppspawnSetExtMsgMore(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 219 220 /** 221 * Create default appspawn msg. 222 * 223 * @param startMsg, request message. 224 * @param reqHandle, handle for request message 225 */ 226 int32_t AppspawnCreateDefaultMsg(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 227 228 /** 229 * Verify startMsg. 230 * 231 * @param startMsg, request message. 232 */ 233 bool VerifyMsg(const AppSpawnStartMsg &startMsg); 234 235 /** 236 * Send appSpawn uninstall debug hap message. 237 * 238 * @param userId, the user id. 239 */ 240 int32_t SendAppSpawnUninstallDebugHapMsg(int32_t userId); 241 242 /** 243 * Start request to nwebspawn process. 244 */ 245 virtual int32_t PreStartNWebSpawnProcess(); 246 247 /** 248 * AppSpawnClient core function, Start request to appSpawn. 249 * 250 * @param startMsg, request message. 251 * @param pid, pid of app process, get from appSpawn. 252 */ 253 virtual int32_t StartProcess(const AppSpawnStartMsg &startMsg, pid_t &pid); 254 255 /** 256 * Get render process termination status. 257 * 258 * @param startMsg, request message. 259 * @param status, termination status of render process, get from appSpawn. 260 */ 261 virtual int32_t GetRenderProcessTerminationStatus(const AppSpawnStartMsg &startMsg, int &status); 262 263 private: 264 std::string serviceName_ = APPSPAWN_SERVER_NAME; 265 AppSpawnClientHandle handle_ = nullptr; 266 SpawnConnectionState state_ = SpawnConnectionState::STATE_NOT_CONNECT; 267 268 #ifdef SUPPORT_CHILD_PROCESS 269 int32_t SetChildProcessTypeStartFlag(const AppSpawnReqMsgHandle &reqHandle, int32_t childProcessType); 270 #endif // SUPPORT_CHILD_PROCESS 271 272 int32_t SetExtMsgFds(const AppSpawnReqMsgHandle &reqHandle, const std::map<std::string, int32_t> &fds); 273 274 int32_t SetIsolationModeFlag(const AppSpawnStartMsg &startMsg, const AppSpawnReqMsgHandle &reqHandle); 275 276 int32_t SetStrictMode(const AppSpawnStartMsg &startMsg, const AppSpawnReqMsgHandle &reqHandle); 277 }; 278 } // namespace AppExecFwk 279 } // namespace OHOS 280 #endif // OHOS_ABILITY_RUNTIME_APP_SPAWN_CLIENT_H 281