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