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      MSG_DEVICE_DEBUG,
110      MAX_TYPE_INVALID
111  } AppSpawnMsgType;
112  
113  /**
114   * @brief create spawn request
115   *
116   * @param msgType msg type. eg: MSG_APP_SPAWN,MSG_SPAWN_NATIVE_PROCESS
117   * @param processName process name, max length is 255
118   * @param reqHandle handle for request message
119   * @return if succeed return 0,else return other value
120   */
121  int AppSpawnReqMsgCreate(AppSpawnMsgType msgType, const char *processName, AppSpawnReqMsgHandle *reqHandle);
122  
123  /**
124   * @brief create request
125   *
126   * @param pid process pid
127   * @param reqHandle handle for request message
128   * @return if succeed return 0,else return other value
129   */
130  int AppSpawnTerminateMsgCreate(pid_t pid, AppSpawnReqMsgHandle *reqHandle);
131  
132  /**
133   * @brief destroy request
134   *
135   * @param reqHandle handle for request
136   */
137  void AppSpawnReqMsgFree(AppSpawnReqMsgHandle reqHandle);
138  
139  /**
140   * @brief set bundle info
141   *
142   * @param reqHandle handle for request message
143   * @param bundleIndex bundle index
144   * @param bundleName bundle name, max length is 255
145   * @return if succeed return 0,else return other value
146   */
147  int AppSpawnReqMsgSetBundleInfo(AppSpawnReqMsgHandle reqHandle, uint32_t bundleIndex, const char *bundleName);
148  
149  /**
150   * @brief set app flags info
151   *
152   * @param reqHandle handle for request message
153   * @param flagIndex flags index from AppFlagsIndex
154   * @return if succeed return 0,else return other value
155   */
156  typedef enum {
157      APP_FLAGS_COLD_BOOT = 0,
158      APP_FLAGS_BACKUP_EXTENSION = 1,
159      APP_FLAGS_DLP_MANAGER = 2,
160      APP_FLAGS_DEBUGGABLE = 3,
161      APP_FLAGS_ASANENABLED = 4,
162      APP_FLAGS_ACCESS_BUNDLE_DIR = 5,
163      APP_FLAGS_NATIVEDEBUG = 6,
164      APP_FLAGS_NO_SANDBOX = 7,
165      APP_FLAGS_OVERLAY = 8,
166      APP_FLAGS_BUNDLE_RESOURCES = 9,
167      APP_FLAGS_GWP_ENABLED_FORCE,   // APP_GWP_ENABLED_FORCE 0x400
168      APP_FLAGS_GWP_ENABLED_NORMAL,  // APP_GWP_ENABLED_NORMAL 0x800
169      APP_FLAGS_TSAN_ENABLED,  // APP_TSANENABLED 0x1000
170      APP_FLAGS_IGNORE_SANDBOX = 13,  // ignore sandbox result
171      APP_FLAGS_ISOLATED_SANDBOX,
172      APP_FLAGS_EXTENSION_SANDBOX,
173      APP_FLAGS_CLONE_ENABLE,
174      APP_FLAGS_DEVELOPER_MODE = 17,
175      APP_FLAGS_BEGETCTL_BOOT, // Start an app from begetctl.
176      APP_FLAGS_ATOMIC_SERVICE,
177      APP_FLAGS_CHILDPROCESS,
178      APP_FLAGS_HWASAN_ENABLED = 21,
179      APP_FLAGS_UBSAN_ENABLED = 22,
180      APP_FLAGS_ISOLATED_SANDBOX_TYPE,
181      APP_FLAGS_ISOLATED_SELINUX_LABEL,
182      APP_FLAGS_ISOLATED_SECCOMP_TYPE,
183      APP_FLAGS_ISOLATED_NETWORK,
184      APP_FLAGS_ISOLATED_DATAGROUP,
185      APP_FLAGS_TEMP_JIT = 28,
186      MAX_FLAGS_INDEX = 63,
187  } AppFlagsIndex;
188  
189  int AppSpawnReqMsgSetAppFlag(AppSpawnReqMsgHandle reqHandle, AppFlagsIndex flagIndex);
190  
191  /**
192   * @brief set dac info
193   *
194   * @param reqHandle handle for request message
195   * @param dacInfo dac info from AppDacInfo
196   * @return if succeed return 0,else return other value
197   */
198  int AppSpawnReqMsgSetAppDacInfo(AppSpawnReqMsgHandle reqHandle, const AppDacInfo *dacInfo);
199  
200  /**
201   * @brief set domain info
202   *
203   * @param reqHandle handle for request message
204   * @param hapFlags hap of flags
205   * @param apl apl value, max length is 31
206   * @return if succeed return 0,else return other value
207   */
208  int AppSpawnReqMsgSetAppDomainInfo(AppSpawnReqMsgHandle reqHandle, uint32_t hapFlags, const char *apl);
209  
210  /**
211   * @brief set internet permission info
212   *
213   * @param reqHandle handle for request message
214   * @param allowInternet
215   * @param setAllowInternet
216   * @return if succeed return 0,else return other value
217   */
218  int AppSpawnReqMsgSetAppInternetPermissionInfo(AppSpawnReqMsgHandle reqHandle, uint8_t allow, uint8_t setAllow);
219  
220  /**
221   * @brief set access token info
222   *
223   * @param reqHandle handle for request message
224   * @param accessTokenIdEx access tokenId
225   * @return if succeed return 0,else return other value
226   */
227  int AppSpawnReqMsgSetAppAccessToken(AppSpawnReqMsgHandle reqHandle, uint64_t accessTokenIdEx);
228  
229  /**
230   * @brief set owner info
231   *
232   * @param reqHandle handle for request message
233   * @param ownerId owner id, max length is 63
234   * @return if succeed return 0,else return other value
235   */
236  int AppSpawnReqMsgSetAppOwnerId(AppSpawnReqMsgHandle reqHandle, const char *ownerId);
237  
238  /**
239   * @brief add permission to message
240   *
241   * @param reqHandle handle for request message
242   * @param permission permission name
243   * @return if succeed return 0,else return other value
244   */
245  int AppSpawnReqMsgAddPermission(AppSpawnReqMsgHandle reqHandle, const char *permission);
246  
247  /**
248   * @brief add permission to message
249   *
250   * @param handle handle for client
251   * @param reqHandle handle for request message
252   * @param permission permission name
253   * @return if succeed return 0,else return other value
254   */
255  int AppSpawnClientAddPermission(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, const char *permission);
256  
257  /**
258   * @brief add extend info to message
259   *
260   * @param reqHandle handle for request message
261   * @param name extend name, max length is 31
262   * @param value extend value, max length is 32768
263   * @param valueLen extend value length
264   * @return if succeed return 0,else return other value
265   */
266  #define MSG_EXT_NAME_RENDER_CMD "render-cmd"
267  #define MSG_EXT_NAME_HSP_LIST "HspList"
268  #define MSG_EXT_NAME_OVERLAY "Overlay"
269  #define MSG_EXT_NAME_DATA_GROUP "DataGroup"
270  #define MSG_EXT_NAME_APP_ENV "AppEnv"
271  #define MSG_EXT_NAME_APP_EXTENSION "AppExtension"
272  #define MSG_EXT_NAME_BEGET_PID "AppPid"
273  #define MSG_EXT_NAME_BEGET_PTY_NAME "ptyName"
274  #define MSG_EXT_NAME_ACCOUNT_ID "AccountId"
275  #define MSG_EXT_NAME_PROVISION_TYPE "ProvisionType"
276  #define MSG_EXT_NAME_PROCESS_TYPE "ProcessType"
277  #define MSG_EXT_NAME_MAX_CHILD_PROCCESS_MAX "MaxChildProcess"
278  #define MSG_EXT_NAME_APP_FD "AppFd"
279  
280  int AppSpawnReqMsgAddExtInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const uint8_t *value, uint32_t valueLen);
281  
282  /**
283   * @brief add extend info to message
284   *
285   * @param reqHandle handle for request message
286   * @param name extend name, max length is 31
287   * @param value extend value, max length is 32767
288   * @return if succeed return 0,else return other value
289   */
290  int AppSpawnReqMsgAddStringInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const char *value);
291  
292  /**
293   * @brief add fd info to message
294   *
295   * @param reqHandle handle for request message
296   * @param name fd name
297   * @param value fd value
298   * @return if succeed return 0,else return other value
299   */
300  int AppSpawnReqMsgAddFd(AppSpawnReqMsgHandle reqHandle, const char* fdName, int fd);
301  
302  /**
303   * @brief Get the permission index by permission name
304   *
305   * @param handle handle for client
306   * @param permission permission name
307   * @return int32_t permission index, if not exit, return INVALID_PERMISSION_INDEX
308   */
309  int32_t GetPermissionIndex(AppSpawnClientHandle handle, const char *permission);
310  
311  /**
312   * @brief Get the max permission Index
313   *
314   * @param handle handle for client
315   * @return int32_t max permission Index
316   */
317  int32_t GetMaxPermissionIndex(AppSpawnClientHandle handle);
318  
319  /**
320   * @brief Get the permission name by index
321   *
322   * @param handle handle for client
323   * @param index permission index
324   * @return const char* permission name
325   */
326  const char *GetPermissionByIndex(AppSpawnClientHandle handle, int32_t index);
327  
328  #ifdef __cplusplus
329  }
330  #endif
331  
332  #endif
333