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