• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 APPSPAWN_H
17 #define APPSPAWN_H
18 
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /**
29  * @brief appspawn请求消息构造句柄,不支持多线程消息构建
30  *
31  * 根据业务使用AppSpawnReqMsgCreate/AppSpawnTerminateMsgCreate 构建消息
32  * 如果调用AppSpawnClientSendMsg后,消息句柄不需要处理
33  * 否则需要调用 AppSpawnReqMsgFree 释放句柄
34  *
35  * 所有字符串输入的接口,只能接受合法的字符串,输入null、""、和大于合法长度的字符串都返回错误
36  *
37  */
38 typedef void *AppSpawnReqMsgHandle;
39 
40 /**
41  * @brief 支持多线程获取句柄,这个是线程安全的。使用时,全局创建一个句柄,支持多线程发送对应线程的消息请求
42  *
43  */
44 typedef void *AppSpawnClientHandle;
45 
46 #define INVALID_PERMISSION_INDEX (-1)
47 #define INVALID_REQ_HANDLE NULL
48 #define NWEBSPAWN_SERVER_NAME "nwebspawn"
49 #define APPSPAWN_SERVER_NAME "appspawn"
50 #define CJAPPSPAWN_SERVER_NAME "cjappspawn"
51 #define NWEBSPAWN_RESTART "nwebRestart"
52 #define NATIVESPAWN_SERVER_NAME "nativespawn"
53 
54 #pragma pack(4)
55 #define APP_MAX_GIDS 64
56 #define APP_USER_NAME 64
57 #define APP_MAX_FD_COUNT 16
58 #define APP_FDENV_PREFIX "APPSPAWN_FD_"
59 #define APP_FDNAME_MAXLEN 20
60 typedef struct {
61     uint32_t uid;       // the UNIX uid that the child process setuid() to after fork()
62     uint32_t gid;       // the UNIX gid that the child process setgid() to after fork()
63     uint32_t gidCount;  // the size of gidTable
64     uint32_t gidTable[APP_MAX_GIDS];
65     char userName[APP_USER_NAME];
66 } AppDacInfo;
67 
68 typedef struct {
69     int result;
70     pid_t pid;
71 } AppSpawnResult;
72 #pragma pack()
73 
74 /**
75  * @brief init spawn client, eg: nwebspawn、appspawn
76  *
77  * @param serviceName service name, eg: nwebspawn、appspawn
78  * @param handle handle for client
79  * @return if succeed return 0,else return other value
80  */
81 int AppSpawnClientInit(const char *serviceName, AppSpawnClientHandle *handle);
82 /**
83  * @brief destroy client
84  *
85  * @param handle handle for client
86  * @return if succeed return 0,else return other value
87  */
88 int AppSpawnClientDestroy(AppSpawnClientHandle handle);
89 
90 /**
91  * @brief send client request
92  *
93  * @param handle handle for client
94  * @param reqHandle handle for request
95  * @param result result from appspawn service
96  * @return if succeed return 0,else return other value
97  */
98 int AppSpawnClientSendMsg(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, AppSpawnResult *result);
99 
100 typedef enum {
101     MSG_APP_SPAWN = 0,
102     MSG_GET_RENDER_TERMINATION_STATUS,
103     MSG_SPAWN_NATIVE_PROCESS,
104     MSG_DUMP,
105     MSG_BEGET_CMD,
106     MSG_BEGET_SPAWNTIME,
107     MSG_UPDATE_MOUNT_POINTS,
108     MSG_RESTART_SPAWNER,
109     MAX_TYPE_INVALID
110 } AppSpawnMsgType;
111 
112 /**
113  * @brief create spawn request
114  *
115  * @param msgType msg type. eg: MSG_APP_SPAWN,MSG_SPAWN_NATIVE_PROCESS
116  * @param processName process name, max length is 255
117  * @param reqHandle handle for request message
118  * @return if succeed return 0,else return other value
119  */
120 int AppSpawnReqMsgCreate(AppSpawnMsgType msgType, const char *processName, AppSpawnReqMsgHandle *reqHandle);
121 
122 /**
123  * @brief create request
124  *
125  * @param pid process pid
126  * @param reqHandle handle for request message
127  * @return if succeed return 0,else return other value
128  */
129 int AppSpawnTerminateMsgCreate(pid_t pid, AppSpawnReqMsgHandle *reqHandle);
130 
131 /**
132  * @brief destroy request
133  *
134  * @param reqHandle handle for request
135  */
136 void AppSpawnReqMsgFree(AppSpawnReqMsgHandle reqHandle);
137 
138 /**
139  * @brief set bundle info
140  *
141  * @param reqHandle handle for request message
142  * @param bundleIndex bundle index
143  * @param bundleName bundle name, max length is 255
144  * @return if succeed return 0,else return other value
145  */
146 int AppSpawnReqMsgSetBundleInfo(AppSpawnReqMsgHandle reqHandle, uint32_t bundleIndex, const char *bundleName);
147 
148 /**
149  * @brief set app flags info
150  *
151  * @param reqHandle handle for request message
152  * @param flagIndex flags index from AppFlagsIndex
153  * @return if succeed return 0,else return other value
154  */
155 typedef enum {
156     APP_FLAGS_COLD_BOOT = 0,
157     APP_FLAGS_BACKUP_EXTENSION = 1,
158     APP_FLAGS_DLP_MANAGER = 2,
159     APP_FLAGS_DEBUGGABLE = 3,
160     APP_FLAGS_ASANENABLED = 4,
161     APP_FLAGS_ACCESS_BUNDLE_DIR = 5,
162     APP_FLAGS_NATIVEDEBUG = 6,
163     APP_FLAGS_NO_SANDBOX = 7,
164     APP_FLAGS_OVERLAY = 8,
165     APP_FLAGS_BUNDLE_RESOURCES = 9,
166     APP_FLAGS_GWP_ENABLED_FORCE,   // APP_GWP_ENABLED_FORCE 0x400
167     APP_FLAGS_GWP_ENABLED_NORMAL,  // APP_GWP_ENABLED_NORMAL 0x800
168     APP_FLAGS_TSAN_ENABLED,  // APP_TSANENABLED 0x1000
169     APP_FLAGS_IGNORE_SANDBOX = 13,  // ignore sandbox result
170     APP_FLAGS_ISOLATED_SANDBOX,
171     APP_FLAGS_EXTENSION_SANDBOX,
172     APP_FLAGS_CLONE_ENABLE,
173     APP_FLAGS_DEVELOPER_MODE = 17,
174     APP_FLAGS_BEGETCTL_BOOT, // Start an app from begetctl.
175     APP_FLAGS_ATOMIC_SERVICE,
176     APP_FLAGS_CHILDPROCESS,
177     APP_FLAGS_HWASAN_ENABLED = 21,
178     APP_FLAGS_UBSAN_ENABLED = 22,
179     APP_FLAGS_ISOLATED_SANDBOX_TYPE,
180     APP_FLAGS_ISOLATED_SELINUX_LABEL,
181     APP_FLAGS_ISOLATED_SECCOMP_TYPE,
182     APP_FLAGS_ISOLATED_NETWORK,
183     APP_FLAGS_ISOLATED_DATAGROUP,
184     APP_FLAGS_TEMP_JIT = 28,
185     MAX_FLAGS_INDEX = 63,
186 } AppFlagsIndex;
187 
188 int AppSpawnReqMsgSetAppFlag(AppSpawnReqMsgHandle reqHandle, AppFlagsIndex flagIndex);
189 
190 /**
191  * @brief set dac info
192  *
193  * @param reqHandle handle for request message
194  * @param dacInfo dac info from AppDacInfo
195  * @return if succeed return 0,else return other value
196  */
197 int AppSpawnReqMsgSetAppDacInfo(AppSpawnReqMsgHandle reqHandle, const AppDacInfo *dacInfo);
198 
199 /**
200  * @brief set domain info
201  *
202  * @param reqHandle handle for request message
203  * @param hapFlags hap of flags
204  * @param apl apl value, max length is 31
205  * @return if succeed return 0,else return other value
206  */
207 int AppSpawnReqMsgSetAppDomainInfo(AppSpawnReqMsgHandle reqHandle, uint32_t hapFlags, const char *apl);
208 
209 /**
210  * @brief set internet permission info
211  *
212  * @param reqHandle handle for request message
213  * @param allowInternet
214  * @param setAllowInternet
215  * @return if succeed return 0,else return other value
216  */
217 int AppSpawnReqMsgSetAppInternetPermissionInfo(AppSpawnReqMsgHandle reqHandle, uint8_t allow, uint8_t setAllow);
218 
219 /**
220  * @brief set access token info
221  *
222  * @param reqHandle handle for request message
223  * @param accessTokenIdEx access tokenId
224  * @return if succeed return 0,else return other value
225  */
226 int AppSpawnReqMsgSetAppAccessToken(AppSpawnReqMsgHandle reqHandle, uint64_t accessTokenIdEx);
227 
228 /**
229  * @brief set owner info
230  *
231  * @param reqHandle handle for request message
232  * @param ownerId owner id, max length is 63
233  * @return if succeed return 0,else return other value
234  */
235 int AppSpawnReqMsgSetAppOwnerId(AppSpawnReqMsgHandle reqHandle, const char *ownerId);
236 
237 /**
238  * @brief add permission to message
239  *
240  * @param reqHandle handle for request message
241  * @param permission permission name
242  * @return if succeed return 0,else return other value
243  */
244 int AppSpawnReqMsgAddPermission(AppSpawnReqMsgHandle reqHandle, const char *permission);
245 
246 /**
247  * @brief add permission to message
248  *
249  * @param handle handle for client
250  * @param reqHandle handle for request message
251  * @param permission permission name
252  * @return if succeed return 0,else return other value
253  */
254 int AppSpawnClientAddPermission(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, const char *permission);
255 
256 /**
257  * @brief add extend info to message
258  *
259  * @param reqHandle handle for request message
260  * @param name extend name, max length is 31
261  * @param value extend value, max length is 32768
262  * @param valueLen extend value length
263  * @return if succeed return 0,else return other value
264  */
265 #define MSG_EXT_NAME_RENDER_CMD "render-cmd"
266 #define MSG_EXT_NAME_HSP_LIST "HspList"
267 #define MSG_EXT_NAME_OVERLAY "Overlay"
268 #define MSG_EXT_NAME_DATA_GROUP "DataGroup"
269 #define MSG_EXT_NAME_APP_ENV "AppEnv"
270 #define MSG_EXT_NAME_APP_EXTENSION "AppExtension"
271 #define MSG_EXT_NAME_BEGET_PID "AppPid"
272 #define MSG_EXT_NAME_BEGET_PTY_NAME "ptyName"
273 #define MSG_EXT_NAME_ACCOUNT_ID "AccountId"
274 #define MSG_EXT_NAME_PROVISION_TYPE "ProvisionType"
275 #define MSG_EXT_NAME_PROCESS_TYPE "ProcessType"
276 #define MSG_EXT_NAME_MAX_CHILD_PROCCESS_MAX "MaxChildProcess"
277 #define MSG_EXT_NAME_APP_FD "AppFd"
278 
279 int AppSpawnReqMsgAddExtInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const uint8_t *value, uint32_t valueLen);
280 
281 /**
282  * @brief add extend info to message
283  *
284  * @param reqHandle handle for request message
285  * @param name extend name, max length is 31
286  * @param value extend value, max length is 32767
287  * @return if succeed return 0,else return other value
288  */
289 int AppSpawnReqMsgAddStringInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const char *value);
290 
291 /**
292  * @brief add fd info to message
293  *
294  * @param reqHandle handle for request message
295  * @param name fd name
296  * @param value fd value
297  * @return if succeed return 0,else return other value
298  */
299 int AppSpawnReqMsgAddFd(AppSpawnReqMsgHandle reqHandle, const char* fdName, int fd);
300 
301 /**
302  * @brief Get the permission index by permission name
303  *
304  * @param handle handle for client
305  * @param permission permission name
306  * @return int32_t permission index, if not exit, return INVALID_PERMISSION_INDEX
307  */
308 int32_t GetPermissionIndex(AppSpawnClientHandle handle, const char *permission);
309 
310 /**
311  * @brief Get the max permission Index
312  *
313  * @param handle handle for client
314  * @return int32_t max permission Index
315  */
316 int32_t GetMaxPermissionIndex(AppSpawnClientHandle handle);
317 
318 /**
319  * @brief Get the permission name by index
320  *
321  * @param handle handle for client
322  * @param index permission index
323  * @return const char* permission name
324  */
325 const char *GetPermissionByIndex(AppSpawnClientHandle handle, int32_t index);
326 
327 #ifdef __cplusplus
328 }
329 #endif
330 
331 #endif
332