• 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_HOOK_H
17 #define APPSPAWN_HOOK_H
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 
24 #include "appspawn_msg.h"
25 #include "hookmgr.h"
26 #include "list.h"
27 
28 #ifdef __cplusplus
29 #if __cplusplus
30 extern "C" {
31 #endif
32 #endif
33 
34 typedef struct TagAppSpawnMgr AppSpawnMgr;
35 typedef struct TagAppSpawningCtx AppSpawningCtx;
36 typedef struct AppSpawnContent AppSpawnContent;
37 typedef struct AppSpawnClient AppSpawnClient;
38 typedef struct TagAppSpawnedProcess AppSpawnedProcessInfo;
39 
40 typedef enum {
41     EXT_DATA_APP_SANDBOX,        // 加载appdata-sandbox-app.json配置文件
42     EXT_DATA_NAMESPACE,
43     EXT_DATA_ISOLATED_SANDBOX,   // 加载appdata-sandbox-isolated-new.json配置文件
44     EXT_DATA_RENDER_SANDBOX,     // 加载appdata-sandbox-render.json配置文件
45     EXT_DATA_GPU_SANDBOX,        // 加载appdata-sandbox-gpu.json配置文件
46     EXT_DATA_DEBUG_HAP_SANDBOX,  // 加载appdata-sandbox-debug.json配置文件
47     EXT_DATA_COUNT,
48 } ExtDataType;
49 
50 struct TagAppSpawnExtData;
51 typedef void (*AppSpawnExtDataFree)(struct TagAppSpawnExtData *data);
52 typedef void (*AppSpawnExtDataDump)(struct TagAppSpawnExtData *data);
53 typedef struct TagAppSpawnExtData {
54     ListNode node;
55     uint32_t dataId;
56     AppSpawnExtDataFree freeNode;
57     AppSpawnExtDataDump dumpNode;
58 } AppSpawnExtData;
59 
60 typedef enum TagAppSpawnHookStage {
61     // 服务状态处理
62     STAGE_SERVER_PRELOAD  = 10,
63     STAGE_SERVER_LOCK,
64     STAGE_SERVER_EXIT,
65     // 应用状态处理
66     STAGE_SERVER_APP_ADD,
67     STAGE_SERVER_APP_DIED,
68     STAGE_SERVER_APP_UMOUNT,
69     // run before fork
70     STAGE_PARENT_PRE_FORK = 20,
71     STAGE_PARENT_POST_FORK = 21,
72     STAGE_PARENT_PRE_RELY = 22,
73     STAGE_PARENT_POST_RELY = 23,
74     STAGE_PARENT_UNINSTALL,
75     // run in child process
76     STAGE_CHILD_PRE_COLDBOOT = 30, // clear env, set token before cold boot
77     STAGE_CHILD_EXECUTE,
78     STAGE_CHILD_PRE_RELY,
79     STAGE_CHILD_POST_RELY,
80     STAGE_CHILD_PRE_RUN,
81     STAGE_MAX
82 } AppSpawnHookStage;
83 
84 typedef enum TagAppSpawnHookPrio {
85     HOOK_PRIO_HIGHEST = 1000,
86     HOOK_PRIO_COMMON = 2000,
87     HOOK_PRIO_SANDBOX = 3000,
88     HOOK_PRIO_PROPERTY = 4000,
89     HOOK_PRIO_LOWEST = 5000,
90 } AppSpawnHookPrio;
91 
92 /**
93  * @brief 预加载处理函数
94  *
95  * @param content appspawn appspawn管理数据
96  * @return int
97  */
98 typedef int (*ServerStageHook)(AppSpawnMgr *content);
99 
100 /**
101  * @brief 应用孵化各阶段注册函数
102  *
103  * @param content appspawn appspawn管理数据
104  * @param property 业务孵化数据
105  * @return int
106  */
107 typedef int (*AppSpawnHook)(AppSpawnMgr *content, AppSpawningCtx *property);
108 
109 /**
110  * @brief 业务进程变化注册函数
111  *
112  * @param content appspawn appspawn管理数据
113  * @param appInfo 业务进程信息
114  * @return int
115  */
116 typedef int (*ProcessChangeHook)(const AppSpawnMgr *content, const AppSpawnedProcessInfo *appInfo);
117 
118 /**
119  * @brief 添加服务阶段的处理函数
120  *
121  * @param stage 阶段信息
122  * @param prio 优先级
123  * @param hook 预加载处理函数
124  * @return int
125  */
126 int AddServerStageHook(AppSpawnHookStage stage, int prio, ServerStageHook hook);
127 
128 /**
129  * @brief 添加预加载处理函数
130  *
131  * @param prio 优先级
132  * @param hook 预加载处理函数
133  * @return int
134  */
AddPreloadHook(int prio,ServerStageHook hook)135 __attribute__((always_inline)) inline int AddPreloadHook(int prio, ServerStageHook hook)
136 {
137     return AddServerStageHook(STAGE_SERVER_PRELOAD, prio, hook);
138 }
139 
140 /**
141  * @brief 按阶段添加应用孵化处理函数
142  *
143  * @param stage 阶段信息
144  * @param prio 优先级
145  * @param hook 应用孵化阶段处理函数
146  * @return int
147  */
148 int AddAppSpawnHook(AppSpawnHookStage stage, int prio, AppSpawnHook hook);
149 
150 /**
151  * @brief 添加业务进程处理函数
152  *
153  * @param stage 阶段信息
154  * @param prio 优先级
155  * @param hook 业务进程变化处理函数
156  * @return int
157  */
158 int AddProcessMgrHook(AppSpawnHookStage stage, int prio, ProcessChangeHook hook);
159 
160 typedef int (*ChildLoop)(AppSpawnContent *content, AppSpawnClient *client);
161 /**
162  * @brief 注册子进程run函数
163  *
164  * @param content
165  * @param loop
166  */
167 void RegChildLooper(AppSpawnContent *content, ChildLoop loop);
168 
169 /**
170  * @brief 按mode创建文件件
171  *
172  * @param path 路径
173  * @param mode mode
174  * @param lastPath 是否文件名
175  * @return int 结果
176  */
177 int MakeDirRec(const char *path, mode_t mode, int lastPath);
CreateSandboxDir(const char * path,mode_t mode)178 __attribute__((always_inline)) inline int CreateSandboxDir(const char *path, mode_t mode)
179 {
180     return MakeDirRec(path, mode, 1);
181 }
182 
183 // 扩展变量
184 typedef struct TagSandboxContext SandboxContext;
185 typedef struct TagVarExtraData VarExtraData;
186 typedef int (*ReplaceVarHandler)(const SandboxContext *context,
187     const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData);
188 /**
189  * @brief 注册变量替换处理函数
190  *
191  * @param name 变量名
192  * @param handler 处理函数
193  * @return int
194  */
195 int AddVariableReplaceHandler(const char *name, ReplaceVarHandler handler);
196 
197 typedef struct TagAppSpawnSandboxCfg AppSpawnSandboxCfg;
198 typedef int (*ProcessExpandSandboxCfg)(const SandboxContext *context,
199     const AppSpawnSandboxCfg *appSandbox, const char *name);
200 #define EXPAND_CFG_HANDLER_PRIO_START 3
201 
202 /**
203  * @brief 注册扩展属性处理函数
204  *
205  * @param name 扩展变量名
206  * @param handleExpandCfg  处理函数
207  * @return int
208  */
209 int RegisterExpandSandboxCfgHandler(const char *name, int prio, ProcessExpandSandboxCfg handleExpandCfg);
210 
211 #ifndef MODULE_DESTRUCTOR
212 #define MODULE_CONSTRUCTOR(void) static void _init(void) __attribute__((constructor)); static void _init(void)
213 #define MODULE_DESTRUCTOR(void) static void _destroy(void) __attribute__((destructor)); static void _destroy(void)
214 #endif
215 
216 #ifdef __cplusplus
217 #if __cplusplus
218 }
219 #endif
220 #endif
221 #endif
222