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