1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#ifndef OHOS_ABILITY_RUNTIME_APP_SPAWN_CLIENT_H
17eace7efcSopenharmony_ci#define OHOS_ABILITY_RUNTIME_APP_SPAWN_CLIENT_H
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <array>
20eace7efcSopenharmony_ci#include <map>
21eace7efcSopenharmony_ci#include <memory>
22eace7efcSopenharmony_ci#include <set>
23eace7efcSopenharmony_ci#include <string>
24eace7efcSopenharmony_ci#include <unistd.h>
25eace7efcSopenharmony_ci#include <vector>
26eace7efcSopenharmony_ci
27eace7efcSopenharmony_ci#include "appexecfwk_errors.h"
28eace7efcSopenharmony_ci#include "appspawn.h"
29eace7efcSopenharmony_ci#include "child_process_info.h"
30eace7efcSopenharmony_ci#include "data_group_info.h"
31eace7efcSopenharmony_ci#include "nocopyable.h"
32eace7efcSopenharmony_ci#include "shared/base_shared_bundle_info.h"
33eace7efcSopenharmony_ci
34eace7efcSopenharmony_cinamespace OHOS {
35eace7efcSopenharmony_cinamespace AppExecFwk {
36eace7efcSopenharmony_cienum class SpawnConnectionState { STATE_NOT_CONNECT, STATE_CONNECTED, STATE_CONNECT_FAILED };
37eace7efcSopenharmony_ciusing HspList = std::vector<BaseSharedBundleInfo>;
38eace7efcSopenharmony_ciusing DataGroupInfoList = std::vector<DataGroupInfo>;
39eace7efcSopenharmony_ciconst int32_t MAX_FLAG_INDEX = 32;
40eace7efcSopenharmony_ciconst int32_t MAX_PROC_NAME_LEN = 256;
41eace7efcSopenharmony_ciconst int32_t START_FLAG_BASE = 1;
42eace7efcSopenharmony_ciconst int32_t MAX_COST_TIME = 500;
43eace7efcSopenharmony_cistruct AppSpawnStartMsg {
44eace7efcSopenharmony_ci    int32_t uid;
45eace7efcSopenharmony_ci    int32_t gid;
46eace7efcSopenharmony_ci    std::vector<int32_t> gids;
47eace7efcSopenharmony_ci    std::string procName;
48eace7efcSopenharmony_ci    std::string soPath;
49eace7efcSopenharmony_ci    uint32_t accessTokenId;
50eace7efcSopenharmony_ci    std::string apl;
51eace7efcSopenharmony_ci    std::string bundleName;
52eace7efcSopenharmony_ci    std::string renderParam; // only nweb spawn need this param.
53eace7efcSopenharmony_ci    int32_t pid;
54eace7efcSopenharmony_ci    int32_t code = 0; // 0: default, MSG_APP_SPAWN; 1: MSG_SPAWN_NATIVE_PROCESS; 2: MSG_GET_RENDER_TERMINATION_STATUS
55eace7efcSopenharmony_ci    uint32_t flags;
56eace7efcSopenharmony_ci    int32_t bundleIndex;   // when dlp launch another app used, default is 0
57eace7efcSopenharmony_ci    uint8_t setAllowInternet;
58eace7efcSopenharmony_ci    uint8_t allowInternet; // hap socket allowed
59eace7efcSopenharmony_ci    uint8_t reserved1;
60eace7efcSopenharmony_ci    uint8_t reserved2;
61eace7efcSopenharmony_ci    uint64_t accessTokenIdEx;
62eace7efcSopenharmony_ci    uint32_t hapFlags = 0; // whether is pre installed hap
63eace7efcSopenharmony_ci    HspList hspList; // list of harmony shared package
64eace7efcSopenharmony_ci    std::string overlayInfo; // overlay hap resource path list
65eace7efcSopenharmony_ci    DataGroupInfoList dataGroupInfoList; // list of harmony shared package
66eace7efcSopenharmony_ci    uint32_t mountPermissionFlags;
67eace7efcSopenharmony_ci    std::set<std::string> permissions;
68eace7efcSopenharmony_ci    std::map<std::string, std::string> appEnv; // environment variable to be set to the process
69eace7efcSopenharmony_ci    std::string ownerId;
70eace7efcSopenharmony_ci    std::string provisionType;
71eace7efcSopenharmony_ci    bool atomicServiceFlag = false;
72eace7efcSopenharmony_ci    std::string atomicAccount = "";
73eace7efcSopenharmony_ci    bool isolatedExtension = false; // whether is isolatedExtension
74eace7efcSopenharmony_ci    std::string extensionSandboxPath;
75eace7efcSopenharmony_ci    bool strictMode = false; // whether is strict mode
76eace7efcSopenharmony_ci    std::string processType = "";
77eace7efcSopenharmony_ci    int32_t maxChildProcess = 0;
78eace7efcSopenharmony_ci    int32_t childProcessType = CHILD_PROCESS_TYPE_NOT_CHILD;
79eace7efcSopenharmony_ci    std::map<std::string, int32_t> fds;
80eace7efcSopenharmony_ci    bool isolationMode = false;
81eace7efcSopenharmony_ci};
82eace7efcSopenharmony_ci
83eace7efcSopenharmony_ciconstexpr auto LEN_PID = sizeof(pid_t);
84eace7efcSopenharmony_cistruct StartFlags {
85eace7efcSopenharmony_ci    static const int COLD_START = 0;
86eace7efcSopenharmony_ci    static const int BACKUP_EXTENSION = 1;
87eace7efcSopenharmony_ci    static const int DLP_MANAGER = 2;
88eace7efcSopenharmony_ci    static const int DEBUGGABLE = 3;
89eace7efcSopenharmony_ci    static const int ASANENABLED = 4;
90eace7efcSopenharmony_ci    static const int ACCESS_BUNDLE_DIR = 5;
91eace7efcSopenharmony_ci    static const int NATIVEDEBUG = 6;
92eace7efcSopenharmony_ci    static const int NO_SANDBOX = 7;
93eace7efcSopenharmony_ci    static const int OVERLAY = 8;
94eace7efcSopenharmony_ci    static const int BUNDLE_RESOURCES = 9;
95eace7efcSopenharmony_ci    static const int GWP_ENABLED_FORCE = 10;
96eace7efcSopenharmony_ci    static const int GWP_ENABLED_NORMAL = 11;
97eace7efcSopenharmony_ci    static const int TSANENABLED = 12;
98eace7efcSopenharmony_ci    static const int EXTENSION_CONTROLLED = 13;
99eace7efcSopenharmony_ci    static const int HWASANENABLED = 21;
100eace7efcSopenharmony_ci    static const int UBSANENABLED = 22;
101eace7efcSopenharmony_ci    static const int TEMP_JIT_ALLOW = 28;
102eace7efcSopenharmony_ci};
103eace7efcSopenharmony_ci
104eace7efcSopenharmony_ciunion AppSpawnPidMsg {
105eace7efcSopenharmony_ci    pid_t pid = 0;
106eace7efcSopenharmony_ci    char pidBuf[LEN_PID];
107eace7efcSopenharmony_ci};
108eace7efcSopenharmony_ci
109eace7efcSopenharmony_ciclass AppSpawnClient {
110eace7efcSopenharmony_cipublic:
111eace7efcSopenharmony_ci    /**
112eace7efcSopenharmony_ci     * Constructor.
113eace7efcSopenharmony_ci     */
114eace7efcSopenharmony_ci    explicit AppSpawnClient(bool isNWebSpawn = false);
115eace7efcSopenharmony_ci
116eace7efcSopenharmony_ci    /**
117eace7efcSopenharmony_ci     * Constructor by service name
118eace7efcSopenharmony_ci     */
119eace7efcSopenharmony_ci    explicit AppSpawnClient(const char* serviceName);
120eace7efcSopenharmony_ci
121eace7efcSopenharmony_ci    /**
122eace7efcSopenharmony_ci     * Destructor
123eace7efcSopenharmony_ci     */
124eace7efcSopenharmony_ci    virtual ~AppSpawnClient();
125eace7efcSopenharmony_ci
126eace7efcSopenharmony_ci    /**
127eace7efcSopenharmony_ci     * Disable copy.
128eace7efcSopenharmony_ci     */
129eace7efcSopenharmony_ci    DISALLOW_COPY_AND_MOVE(AppSpawnClient);
130eace7efcSopenharmony_ci
131eace7efcSopenharmony_ci    /**
132eace7efcSopenharmony_ci     * Try connect to appSpawn.
133eace7efcSopenharmony_ci     */
134eace7efcSopenharmony_ci    ErrCode OpenConnection();
135eace7efcSopenharmony_ci
136eace7efcSopenharmony_ci    /**
137eace7efcSopenharmony_ci     * Close the connect of appspawn.
138eace7efcSopenharmony_ci     */
139eace7efcSopenharmony_ci    void CloseConnection();
140eace7efcSopenharmony_ci
141eace7efcSopenharmony_ci    /**
142eace7efcSopenharmony_ci     * Return the connect state.
143eace7efcSopenharmony_ci     */
144eace7efcSopenharmony_ci    SpawnConnectionState QueryConnectionState() const;
145eace7efcSopenharmony_ci
146eace7efcSopenharmony_ci    /**
147eace7efcSopenharmony_ci     * Return the clent handle.
148eace7efcSopenharmony_ci     */
149eace7efcSopenharmony_ci    AppSpawnClientHandle GetAppSpawnClientHandle() const;
150eace7efcSopenharmony_ci
151eace7efcSopenharmony_ci    /**
152eace7efcSopenharmony_ci     * Set dac info.
153eace7efcSopenharmony_ci     *
154eace7efcSopenharmony_ci     * @param startMsg, request message.
155eace7efcSopenharmony_ci     * @param reqHandle, handle for request message
156eace7efcSopenharmony_ci     */
157eace7efcSopenharmony_ci    int32_t SetDacInfo(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle);
158eace7efcSopenharmony_ci
159eace7efcSopenharmony_ci    /**
160eace7efcSopenharmony_ci     * Set mount permission.
161eace7efcSopenharmony_ci     *
162eace7efcSopenharmony_ci     * @param startMsg, request message.
163eace7efcSopenharmony_ci     * @param reqHandle, handle for request message
164eace7efcSopenharmony_ci     */
165eace7efcSopenharmony_ci    int32_t SetMountPermission(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle);
166eace7efcSopenharmony_ci
167eace7efcSopenharmony_ci    /**
168eace7efcSopenharmony_ci     * Set start flags.
169eace7efcSopenharmony_ci     *
170eace7efcSopenharmony_ci     * @param startMsg, request message.
171eace7efcSopenharmony_ci     * @param reqHandle, handle for request message
172eace7efcSopenharmony_ci     */
173eace7efcSopenharmony_ci    int32_t SetStartFlags(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle);
174eace7efcSopenharmony_ci
175eace7efcSopenharmony_ci    /**
176eace7efcSopenharmony_ci     * Set extra info: render-cmd, HspList, Overlay, DataGroup, AppEnv.
177eace7efcSopenharmony_ci     *
178eace7efcSopenharmony_ci     * @param startMsg, request message.
179eace7efcSopenharmony_ci     * @param reqHandle, handle for request message
180eace7efcSopenharmony_ci     */
181eace7efcSopenharmony_ci    int32_t AppspawnSetExtMsg(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle);
182eace7efcSopenharmony_ci
183eace7efcSopenharmony_ci    /**
184eace7efcSopenharmony_ci     * Set extra info: provision_type, max_child_process.
185eace7efcSopenharmony_ci     *
186eace7efcSopenharmony_ci     * @param startMsg, request message.
187eace7efcSopenharmony_ci     * @param reqHandle, handle for request message
188eace7efcSopenharmony_ci     */
189eace7efcSopenharmony_ci    int32_t AppspawnSetExtMsgMore(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle);
190eace7efcSopenharmony_ci
191eace7efcSopenharmony_ci    /**
192eace7efcSopenharmony_ci     * Create default appspawn msg.
193eace7efcSopenharmony_ci     *
194eace7efcSopenharmony_ci     * @param startMsg, request message.
195eace7efcSopenharmony_ci     * @param reqHandle, handle for request message
196eace7efcSopenharmony_ci     */
197eace7efcSopenharmony_ci    int32_t AppspawnCreateDefaultMsg(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle);
198eace7efcSopenharmony_ci
199eace7efcSopenharmony_ci    /**
200eace7efcSopenharmony_ci     * Verify startMsg.
201eace7efcSopenharmony_ci     *
202eace7efcSopenharmony_ci     * @param startMsg, request message.
203eace7efcSopenharmony_ci     */
204eace7efcSopenharmony_ci    bool VerifyMsg(const AppSpawnStartMsg &startMsg);
205eace7efcSopenharmony_ci
206eace7efcSopenharmony_ci    /**
207eace7efcSopenharmony_ci     * Start request to nwebspawn process.
208eace7efcSopenharmony_ci     */
209eace7efcSopenharmony_ci    virtual int32_t PreStartNWebSpawnProcess();
210eace7efcSopenharmony_ci
211eace7efcSopenharmony_ci    /**
212eace7efcSopenharmony_ci     * AppSpawnClient core function, Start request to appSpawn.
213eace7efcSopenharmony_ci     *
214eace7efcSopenharmony_ci     * @param startMsg, request message.
215eace7efcSopenharmony_ci     * @param pid, pid of app process, get from appSpawn.
216eace7efcSopenharmony_ci     */
217eace7efcSopenharmony_ci    virtual int32_t StartProcess(const AppSpawnStartMsg &startMsg, pid_t &pid);
218eace7efcSopenharmony_ci
219eace7efcSopenharmony_ci    /**
220eace7efcSopenharmony_ci     * Get render process termination status.
221eace7efcSopenharmony_ci     *
222eace7efcSopenharmony_ci     * @param startMsg, request message.
223eace7efcSopenharmony_ci     * @param status, termination status of render process, get from appSpawn.
224eace7efcSopenharmony_ci     */
225eace7efcSopenharmony_ci    virtual int32_t GetRenderProcessTerminationStatus(const AppSpawnStartMsg &startMsg, int &status);
226eace7efcSopenharmony_ci
227eace7efcSopenharmony_ciprivate:
228eace7efcSopenharmony_ci    std::string serviceName_ = APPSPAWN_SERVER_NAME;
229eace7efcSopenharmony_ci    AppSpawnClientHandle handle_ = nullptr;
230eace7efcSopenharmony_ci    SpawnConnectionState state_ = SpawnConnectionState::STATE_NOT_CONNECT;
231eace7efcSopenharmony_ci
232eace7efcSopenharmony_ci    int32_t SetChildProcessTypeStartFlag(const AppSpawnReqMsgHandle &reqHandle, int32_t childProcessType);
233eace7efcSopenharmony_ci
234eace7efcSopenharmony_ci    int32_t SetExtMsgFds(const AppSpawnReqMsgHandle &reqHandle, const std::map<std::string, int32_t> &fds);
235eace7efcSopenharmony_ci
236eace7efcSopenharmony_ci    int32_t SetIsolationModeFlag(const AppSpawnStartMsg &startMsg, const AppSpawnReqMsgHandle &reqHandle);
237eace7efcSopenharmony_ci};
238eace7efcSopenharmony_ci}  // namespace AppExecFwk
239eace7efcSopenharmony_ci}  // namespace OHOS
240eace7efcSopenharmony_ci#endif  // OHOS_ABILITY_RUNTIME_APP_SPAWN_CLIENT_H
241