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