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 TEMP_JIT_ALLOW = 28; 101 }; 102 103 union AppSpawnPidMsg { 104 pid_t pid = 0; 105 char pidBuf[LEN_PID]; 106 }; 107 108 class AppSpawnClient { 109 public: 110 /** 111 * Constructor. 112 */ 113 explicit AppSpawnClient(bool isNWebSpawn = false); 114 115 /** 116 * Constructor by service name 117 */ 118 explicit AppSpawnClient(const char* serviceName); 119 120 /** 121 * Destructor 122 */ 123 virtual ~AppSpawnClient(); 124 125 /** 126 * Disable copy. 127 */ 128 DISALLOW_COPY_AND_MOVE(AppSpawnClient); 129 130 /** 131 * Try connect to appSpawn. 132 */ 133 ErrCode OpenConnection(); 134 135 /** 136 * Close the connect of appspawn. 137 */ 138 void CloseConnection(); 139 140 /** 141 * Return the connect state. 142 */ 143 SpawnConnectionState QueryConnectionState() const; 144 145 /** 146 * Return the clent handle. 147 */ 148 AppSpawnClientHandle GetAppSpawnClientHandle() const; 149 150 /** 151 * Set dac info. 152 * 153 * @param startMsg, request message. 154 * @param reqHandle, handle for request message 155 */ 156 int32_t SetDacInfo(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 157 158 /** 159 * Set mount permission. 160 * 161 * @param startMsg, request message. 162 * @param reqHandle, handle for request message 163 */ 164 int32_t SetMountPermission(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 165 166 /** 167 * Set start flags. 168 * 169 * @param startMsg, request message. 170 * @param reqHandle, handle for request message 171 */ 172 int32_t SetStartFlags(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 173 174 /** 175 * Set extra info: render-cmd, HspList, Overlay, DataGroup, AppEnv. 176 * 177 * @param startMsg, request message. 178 * @param reqHandle, handle for request message 179 */ 180 int32_t AppspawnSetExtMsg(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 181 182 /** 183 * Set extra info: provision_type, max_child_process. 184 * 185 * @param startMsg, request message. 186 * @param reqHandle, handle for request message 187 */ 188 int32_t AppspawnSetExtMsgMore(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 189 190 /** 191 * Create default appspawn msg. 192 * 193 * @param startMsg, request message. 194 * @param reqHandle, handle for request message 195 */ 196 int32_t AppspawnCreateDefaultMsg(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle); 197 198 /** 199 * Verify startMsg. 200 * 201 * @param startMsg, request message. 202 */ 203 bool VerifyMsg(const AppSpawnStartMsg &startMsg); 204 205 /** 206 * Start request to nwebspawn process. 207 */ 208 virtual int32_t PreStartNWebSpawnProcess(); 209 210 /** 211 * AppSpawnClient core function, Start request to appSpawn. 212 * 213 * @param startMsg, request message. 214 * @param pid, pid of app process, get from appSpawn. 215 */ 216 virtual int32_t StartProcess(const AppSpawnStartMsg &startMsg, pid_t &pid); 217 218 /** 219 * Get render process termination status. 220 * 221 * @param startMsg, request message. 222 * @param status, termination status of render process, get from appSpawn. 223 */ 224 virtual int32_t GetRenderProcessTerminationStatus(const AppSpawnStartMsg &startMsg, int &status); 225 226 private: 227 std::string serviceName_ = APPSPAWN_SERVER_NAME; 228 AppSpawnClientHandle handle_ = nullptr; 229 SpawnConnectionState state_ = SpawnConnectionState::STATE_NOT_CONNECT; 230 231 int32_t SetChildProcessTypeStartFlag(const AppSpawnReqMsgHandle &reqHandle, int32_t childProcessType); 232 233 int32_t SetExtMsgFds(const AppSpawnReqMsgHandle &reqHandle, const std::map<std::string, int32_t> &fds); 234 235 int32_t SetIsolationModeFlag(const AppSpawnStartMsg &startMsg, const AppSpawnReqMsgHandle &reqHandle); 236 }; 237 } // namespace AppExecFwk 238 } // namespace OHOS 239 #endif // OHOS_ABILITY_RUNTIME_APP_SPAWN_CLIENT_H 240