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