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 #include "appspawn_modulemgr.h"
17
18 #include "appspawn_hook.h"
19 #include "appspawn_manager.h"
20 #include "appspawn_utils.h"
21 #include "hookmgr.h"
22 #include "modulemgr.h"
23
24 typedef struct {
25 const AppSpawnContent *content;
26 const AppSpawnedProcessInfo *appInfo;
27 } AppSpawnAppArg;
28
29 static struct {
30 MODULE_MGR *moduleMgr;
31 AppSpawnModuleType type;
32 const char *moduleName;
33 } g_moduleMgr[MODULE_MAX] = {
34 {NULL, MODULE_DEFAULT, "appspawn"},
35 {NULL, MODULE_APPSPAWN, "appspawn/appspawn"},
36 {NULL, MODULE_NWEBSPAWN, "appspawn/nwebspawn"},
37 {NULL, MODULE_COMMON, "appspawn/common"},
38 {NULL, MODULE_NATIVESPAWN, "appspawn/nativespawn"},
39 {NULL, MODULE_HYBRIDSPAWN, "appspawn/appspawn"},
40 };
41 static HOOK_MGR *g_appspawnHookMgr = NULL;
42
AppSpawnModuleMgrInstall(const char * moduleName)43 int AppSpawnModuleMgrInstall(const char *moduleName)
44 {
45 if (moduleName == NULL) {
46 return -1;
47 }
48 int type = MODULE_DEFAULT;
49 if (g_moduleMgr[type].moduleMgr == NULL) {
50 g_moduleMgr[type].moduleMgr = ModuleMgrCreate(g_moduleMgr[type].moduleName);
51 }
52 if (g_moduleMgr[type].moduleMgr == NULL) {
53 return -1;
54 }
55 #ifndef APPSPAWN_TEST
56 return ModuleMgrInstall(g_moduleMgr[type].moduleMgr, moduleName, 0, NULL);
57 #else
58 return 0;
59 #endif
60 }
61
AppSpawnModuleMgrUnInstall(int type)62 void AppSpawnModuleMgrUnInstall(int type)
63 {
64 if ((type < 0) || (type >= MODULE_MAX)) {
65 return;
66 }
67 if (g_moduleMgr[type].moduleMgr == NULL) {
68 return;
69 }
70 ModuleMgrDestroy(g_moduleMgr[type].moduleMgr);
71 g_moduleMgr[type].moduleMgr = NULL;
72 }
73
AppSpawnLoadAutoRunModules(int type)74 int AppSpawnLoadAutoRunModules(int type)
75 {
76 if ((type < 0) || (type >= MODULE_MAX)) {
77 return -1;
78 }
79 if (g_moduleMgr[type].moduleMgr != NULL) {
80 return 0;
81 }
82 APPSPAWN_LOGI("AppSpawnLoadAutoRunModules: %{public}d moduleName: %{public}s", type, g_moduleMgr[type].moduleName);
83 #ifndef APPSPAWN_TEST
84 g_moduleMgr[type].moduleMgr = ModuleMgrScan(g_moduleMgr[type].moduleName);
85 return g_moduleMgr[type].moduleMgr == NULL ? -1 : 0;
86 #else
87 return 0;
88 #endif
89 }
90
GetAppSpawnHookMgr(void)91 HOOK_MGR *GetAppSpawnHookMgr(void)
92 {
93 if (g_appspawnHookMgr != NULL) {
94 return g_appspawnHookMgr;
95 }
96 g_appspawnHookMgr = HookMgrCreate("appspawn");
97 return g_appspawnHookMgr;
98 }
99
DeleteAppSpawnHookMgr(void)100 void DeleteAppSpawnHookMgr(void)
101 {
102 HookMgrDestroy(g_appspawnHookMgr);
103 g_appspawnHookMgr = NULL;
104 }
105
UpdateAppSpawnTime(int used)106 static void UpdateAppSpawnTime(int used)
107 {
108 if (used < GetAppSpawnMgr()->spawnTime.minAppspawnTime) {
109 GetAppSpawnMgr()->spawnTime.minAppspawnTime = used;
110 APPSPAWN_LOGI("spawn min time: %{public}d", GetAppSpawnMgr()->spawnTime.minAppspawnTime);
111 }
112 if (used > GetAppSpawnMgr()->spawnTime.maxAppspawnTime) {
113 GetAppSpawnMgr()->spawnTime.maxAppspawnTime = used;
114 APPSPAWN_LOGI("spawn max time: %{public}d", GetAppSpawnMgr()->spawnTime.maxAppspawnTime);
115 }
116 }
117
ServerStageHookRun(const HOOK_INFO * hookInfo,void * executionContext)118 static int ServerStageHookRun(const HOOK_INFO *hookInfo, void *executionContext)
119 {
120 AppSpawnHookArg *arg = (AppSpawnHookArg *)executionContext;
121 ServerStageHook realHook = (ServerStageHook)hookInfo->hookCookie;
122 return realHook((void *)arg->content);
123 }
124
PreHookExec(const HOOK_INFO * hookInfo,void * executionContext)125 static void PreHookExec(const HOOK_INFO *hookInfo, void *executionContext)
126 {
127 AppSpawnHookArg *arg = (AppSpawnHookArg *)executionContext;
128 AppSpawnMgr *spawnMgr = (AppSpawnMgr *)arg->content;
129 clock_gettime(CLOCK_MONOTONIC, &spawnMgr->perLoadStart);
130 APPSPAWN_LOGI("Hook stage: %{public}d prio: %{public}d start", hookInfo->stage, hookInfo->prio);
131 }
132
PostHookExec(const HOOK_INFO * hookInfo,void * executionContext,int executionRetVal)133 static void PostHookExec(const HOOK_INFO *hookInfo, void *executionContext, int executionRetVal)
134 {
135 AppSpawnHookArg *arg = (AppSpawnHookArg *)executionContext;
136 AppSpawnMgr *spawnMgr = (AppSpawnMgr *)arg->content;
137 clock_gettime(CLOCK_MONOTONIC, &spawnMgr->perLoadEnd);
138 uint64_t diff = DiffTime(&spawnMgr->perLoadStart, &spawnMgr->perLoadEnd);
139 APPSPAWN_LOGI("Hook stage: %{public}d prio: %{public}d end time %{public}" PRId64 " ns result: %{public}d",
140 hookInfo->stage, hookInfo->prio, diff, executionRetVal);
141 UpdateAppSpawnTime(diff);
142 }
143
ServerStageHookExecute(AppSpawnHookStage stage,AppSpawnContent * content)144 int ServerStageHookExecute(AppSpawnHookStage stage, AppSpawnContent *content)
145 {
146 APPSPAWN_CHECK(content != NULL, return APPSPAWN_ARG_INVALID, "Invalid content");
147 APPSPAWN_CHECK((stage >= STAGE_SERVER_PRELOAD) && (stage <= STAGE_SERVER_EXIT),
148 return APPSPAWN_ARG_INVALID, "Invalid stage %{public}d", (int)stage);
149 AppSpawnHookArg arg;
150 arg.content = content;
151 arg.client = NULL;
152 HOOK_EXEC_OPTIONS options;
153 options.flags = TRAVERSE_STOP_WHEN_ERROR;
154 options.preHook = PreHookExec;
155 options.postHook = PostHookExec;
156 int ret = HookMgrExecute(GetAppSpawnHookMgr(), stage, (void *)(&arg), &options);
157 APPSPAWN_LOGV("Execute hook [%{public}d] result %{public}d", stage, ret);
158 return ret == ERR_NO_HOOK_STAGE ? 0 : ret;
159 }
160
AddServerStageHook(AppSpawnHookStage stage,int prio,ServerStageHook hook)161 int AddServerStageHook(AppSpawnHookStage stage, int prio, ServerStageHook hook)
162 {
163 APPSPAWN_CHECK(hook != NULL, return APPSPAWN_ARG_INVALID, "Invalid hook");
164 APPSPAWN_CHECK((stage >= STAGE_SERVER_PRELOAD) && (stage <= STAGE_SERVER_EXIT),
165 return APPSPAWN_ARG_INVALID, "Invalid stage %{public}d", (int)stage);
166 HOOK_INFO info;
167 info.stage = stage;
168 info.prio = prio;
169 info.hook = ServerStageHookRun;
170 info.hookCookie = (void *)hook;
171 APPSPAWN_LOGI("AddServerStageHook prio: %{public}d", prio);
172 return HookMgrAddEx(GetAppSpawnHookMgr(), &info);
173 }
174
AppSpawnHookRun(const HOOK_INFO * hookInfo,void * executionContext)175 static int AppSpawnHookRun(const HOOK_INFO *hookInfo, void *executionContext)
176 {
177 AppSpawnForkArg *arg = (AppSpawnForkArg *)executionContext;
178 AppSpawnHook realHook = (AppSpawnHook)hookInfo->hookCookie;
179 return realHook((AppSpawnMgr *)arg->content, (AppSpawningCtx *)arg->client);
180 }
181
PreAppSpawnHookExec(const HOOK_INFO * hookInfo,void * executionContext)182 static void PreAppSpawnHookExec(const HOOK_INFO *hookInfo, void *executionContext)
183 {
184 AppSpawnHookArg *arg = (AppSpawnHookArg *)executionContext;
185 clock_gettime(CLOCK_MONOTONIC, &arg->tmStart);
186 APPSPAWN_LOGV("Hook stage: %{public}d prio: %{public}d start", hookInfo->stage, hookInfo->prio);
187 }
188
PostAppSpawnHookExec(const HOOK_INFO * hookInfo,void * executionContext,int executionRetVal)189 static void PostAppSpawnHookExec(const HOOK_INFO *hookInfo, void *executionContext, int executionRetVal)
190 {
191 AppSpawnHookArg *arg = (AppSpawnHookArg *)executionContext;
192 clock_gettime(CLOCK_MONOTONIC, &arg->tmEnd);
193 uint64_t diff = DiffTime(&arg->tmStart, &arg->tmEnd);
194 APPSPAWN_LOGV("Hook stage: %{public}d prio: %{public}d end time %{public}" PRId64 " ns result: %{public}d",
195 hookInfo->stage, hookInfo->prio, diff, executionRetVal);
196 }
197
AppSpawnHookExecute(AppSpawnHookStage stage,uint32_t flags,AppSpawnContent * content,AppSpawnClient * client)198 int AppSpawnHookExecute(AppSpawnHookStage stage, uint32_t flags, AppSpawnContent *content, AppSpawnClient *client)
199 {
200 APPSPAWN_CHECK(content != NULL && client != NULL, return APPSPAWN_ARG_INVALID, "Invalid arg");
201 APPSPAWN_LOGV("Execute hook [%{public}d] for app: %{public}s", stage, GetProcessName((AppSpawningCtx *)client));
202 APPSPAWN_CHECK((stage >= STAGE_PARENT_PRE_FORK) && (stage <= STAGE_CHILD_PRE_RUN),
203 return APPSPAWN_ARG_INVALID, "Invalid stage %{public}d", (int)stage);
204 AppSpawnHookArg forkArg;
205 forkArg.client = client;
206 forkArg.content = content;
207 HOOK_EXEC_OPTIONS options;
208 options.flags = (int)flags; // TRAVERSE_STOP_WHEN_ERROR : 0;
209 options.preHook = PreAppSpawnHookExec;
210 options.postHook = PostAppSpawnHookExec;
211 int ret = HookMgrExecute(GetAppSpawnHookMgr(), stage, (void *)(&forkArg), &options);
212 ret = (ret == ERR_NO_HOOK_STAGE) ? 0 : ret;
213 if (ret != 0) {
214 APPSPAWN_LOGE("Execute hook [%{public}d] result %{public}d", stage, ret);
215 }
216 return ret;
217 }
218
AppSpawnExecuteClearEnvHook(AppSpawnContent * content,AppSpawnClient * client)219 int AppSpawnExecuteClearEnvHook(AppSpawnContent *content, AppSpawnClient *client)
220 {
221 return AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, HOOK_STOP_WHEN_ERROR, content, client);
222 }
223
AppSpawnExecuteSpawningHook(AppSpawnContent * content,AppSpawnClient * client)224 int AppSpawnExecuteSpawningHook(AppSpawnContent *content, AppSpawnClient *client)
225 {
226 return AppSpawnHookExecute(STAGE_CHILD_EXECUTE, HOOK_STOP_WHEN_ERROR, content, client);
227 }
228
AppSpawnExecutePostReplyHook(AppSpawnContent * content,AppSpawnClient * client)229 int AppSpawnExecutePostReplyHook(AppSpawnContent *content, AppSpawnClient *client)
230 {
231 return AppSpawnHookExecute(STAGE_CHILD_POST_RELY, HOOK_STOP_WHEN_ERROR, content, client);
232 }
233
AppSpawnExecutePreReplyHook(AppSpawnContent * content,AppSpawnClient * client)234 int AppSpawnExecutePreReplyHook(AppSpawnContent *content, AppSpawnClient *client)
235 {
236 return AppSpawnHookExecute(STAGE_CHILD_PRE_RELY, HOOK_STOP_WHEN_ERROR, content, client);
237 }
238
AppSpawnEnvClear(AppSpawnContent * content,AppSpawnClient * client)239 void AppSpawnEnvClear(AppSpawnContent *content, AppSpawnClient *client)
240 {
241 (void)AppSpawnHookExecute(STAGE_CHILD_PRE_RUN, 0, content, client);
242 }
243
AddAppSpawnHook(AppSpawnHookStage stage,int prio,AppSpawnHook hook)244 int AddAppSpawnHook(AppSpawnHookStage stage, int prio, AppSpawnHook hook)
245 {
246 APPSPAWN_CHECK(hook != NULL, return APPSPAWN_ARG_INVALID, "Invalid hook");
247 APPSPAWN_CHECK((stage >= STAGE_PARENT_PRE_FORK) && (stage <= STAGE_CHILD_PRE_RUN),
248 return APPSPAWN_ARG_INVALID, "Invalid stage %{public}d", (int)stage);
249 HOOK_INFO info;
250 info.stage = stage;
251 info.prio = prio;
252 info.hook = AppSpawnHookRun;
253 info.hookCookie = (void *)hook;
254 APPSPAWN_LOGI("AddAppSpawnHook stage: %{public}d prio: %{public}d", stage, prio);
255 return HookMgrAddEx(GetAppSpawnHookMgr(), &info);
256 }
257
ProcessMgrHookExecute(AppSpawnHookStage stage,const AppSpawnContent * content,const AppSpawnedProcessInfo * appInfo)258 int ProcessMgrHookExecute(AppSpawnHookStage stage, const AppSpawnContent *content,
259 const AppSpawnedProcessInfo *appInfo)
260 {
261 APPSPAWN_CHECK(content != NULL && appInfo != NULL,
262 return APPSPAWN_ARG_INVALID, "Invalid hook");
263 APPSPAWN_CHECK((stage >= STAGE_SERVER_APP_ADD) && (stage <= STAGE_SERVER_APP_UMOUNT),
264 return APPSPAWN_ARG_INVALID, "Invalid stage %{public}d", (int)stage);
265
266 AppSpawnAppArg arg;
267 arg.appInfo = appInfo;
268 arg.content = content;
269 int ret = HookMgrExecute(GetAppSpawnHookMgr(), stage, (void *)(&arg), NULL);
270 return ret == ERR_NO_HOOK_STAGE ? 0 : ret;
271 }
272
ProcessMgrHookRun(const HOOK_INFO * hookInfo,void * executionContext)273 static int ProcessMgrHookRun(const HOOK_INFO *hookInfo, void *executionContext)
274 {
275 AppSpawnAppArg *arg = (AppSpawnAppArg *)executionContext;
276 ProcessChangeHook realHook = (ProcessChangeHook)hookInfo->hookCookie;
277 return realHook((AppSpawnMgr *)arg->content, arg->appInfo);
278 }
279
AddProcessMgrHook(AppSpawnHookStage stage,int prio,ProcessChangeHook hook)280 int AddProcessMgrHook(AppSpawnHookStage stage, int prio, ProcessChangeHook hook)
281 {
282 APPSPAWN_CHECK(hook != NULL, return APPSPAWN_ARG_INVALID, "Invalid hook");
283 APPSPAWN_CHECK((stage >= STAGE_SERVER_APP_ADD) && (stage <= STAGE_SERVER_APP_UMOUNT),
284 return APPSPAWN_ARG_INVALID, "Invalid stage %{public}d", (int)stage);
285 HOOK_INFO info;
286 info.stage = stage;
287 info.prio = prio;
288 info.hook = ProcessMgrHookRun;
289 info.hookCookie = hook;
290 return HookMgrAddEx(GetAppSpawnHookMgr(), &info);
291 }
292
RegChildLooper(struct AppSpawnContent * content,ChildLoop loop)293 void RegChildLooper(struct AppSpawnContent *content, ChildLoop loop)
294 {
295 APPSPAWN_CHECK(content != NULL && loop != NULL, return, "Invalid content for RegChildLooper");
296 content->runChildProcessor = loop;
297 }
298