• 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 #include <fcntl.h>
17 #include <limits.h>
18 #include <sched.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 
22 #include <sys/ipc.h>
23 #include <sys/mman.h>
24 #include <sys/mount.h>
25 #include <sys/signalfd.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29 
30 #include "appspawn_adapter.h"
31 #include "appspawn_hook.h"
32 #include "appspawn_msg.h"
33 #include "appspawn_manager.h"
34 #include "securec.h"
35 
36 #define SLEEP_DURATION 3000 // us
37 #define EXIT_APP_TIMEOUT 1000000 // us
38 
39 static AppSpawnMgr *g_appSpawnMgr = NULL;
40 
CreateAppSpawnMgr(int mode)41 AppSpawnMgr *CreateAppSpawnMgr(int mode)
42 {
43     APPSPAWN_CHECK_ONLY_EXPER(mode < MODE_INVALID, return NULL);
44     if (g_appSpawnMgr != NULL) {
45         return g_appSpawnMgr;
46     }
47     AppSpawnMgr *appMgr = (AppSpawnMgr *)calloc(1, sizeof(AppSpawnMgr));
48     APPSPAWN_CHECK(appMgr != NULL, return NULL, "Failed to alloc memory for appspawn");
49     appMgr->content.longProcName = NULL;
50     appMgr->content.longProcNameLen = 0;
51     appMgr->content.mode = mode;
52     appMgr->content.sandboxNsFlags = 0;
53     appMgr->content.wdgOpened = 0;
54     appMgr->servicePid = getpid();
55     appMgr->server = NULL;
56     appMgr->sigHandler = NULL;
57     OH_ListInit(&appMgr->appQueue);
58     OH_ListInit(&appMgr->diedQueue);
59     OH_ListInit(&appMgr->appSpawnQueue);
60     appMgr->diedAppCount = 0;
61     OH_ListInit(&appMgr->extData);
62     g_appSpawnMgr = appMgr;
63     g_appSpawnMgr->spawnTime.minAppspawnTime = APPSPAWN_MAX_TIME;
64     g_appSpawnMgr->spawnTime.maxAppspawnTime = 0;
65     return appMgr;
66 }
67 
GetAppSpawnMgr(void)68 AppSpawnMgr *GetAppSpawnMgr(void)
69 {
70     return g_appSpawnMgr;
71 }
72 
GetAppSpawnContent(void)73 AppSpawnContent *GetAppSpawnContent(void)
74 {
75     return g_appSpawnMgr == NULL ? NULL : &g_appSpawnMgr->content;
76 }
77 
SpawningQueueDestroy(ListNode * node)78 static void SpawningQueueDestroy(ListNode *node)
79 {
80     AppSpawningCtx *property = ListEntry(node, AppSpawningCtx, node);
81     DeleteAppSpawningCtx(property);
82 }
83 
ExtDataDestroy(ListNode * node)84 static void ExtDataDestroy(ListNode *node)
85 {
86     AppSpawnExtData *extData = ListEntry(node, AppSpawnExtData, node);
87     AppSpawnExtDataFree freeNode = extData->freeNode;
88     if (freeNode) {
89         freeNode(extData);
90     }
91 }
92 
DeleteAppSpawnMgr(AppSpawnMgr * mgr)93 void DeleteAppSpawnMgr(AppSpawnMgr *mgr)
94 {
95     APPSPAWN_CHECK_ONLY_EXPER(mgr != NULL, return);
96     OH_ListRemoveAll(&mgr->appQueue, NULL);
97     OH_ListRemoveAll(&mgr->diedQueue, NULL);
98     OH_ListRemoveAll(&mgr->appSpawnQueue, SpawningQueueDestroy);
99     OH_ListRemoveAll(&mgr->extData, ExtDataDestroy);
100 
101     APPSPAWN_LOGV("DeleteAppSpawnMgr %{public}d %{public}d", mgr->servicePid, getpid());
102     free(mgr);
103     if (g_appSpawnMgr == mgr) {
104         g_appSpawnMgr = NULL;
105     }
106 }
107 
TraversalSpawnedProcess(AppTraversal traversal,void * data)108 void TraversalSpawnedProcess(AppTraversal traversal, void *data)
109 {
110     APPSPAWN_CHECK_ONLY_EXPER(g_appSpawnMgr != NULL && traversal != NULL, return);
111     ListNode *node = g_appSpawnMgr->appQueue.next;
112     while (node != &g_appSpawnMgr->appQueue) {
113         ListNode *next = node->next;
114         AppSpawnedProcess *appInfo = ListEntry(node, AppSpawnedProcess, node);
115         traversal(g_appSpawnMgr, appInfo, data);
116         node = next;
117     }
118 }
119 
AppInfoPidComparePro(ListNode * node,void * data)120 static int AppInfoPidComparePro(ListNode *node, void *data)
121 {
122     AppSpawnedProcess *node1 = ListEntry(node, AppSpawnedProcess, node);
123     pid_t pid = *(pid_t *)data;
124     return node1->pid - pid;
125 }
126 
AppInfoNameComparePro(ListNode * node,void * data)127 static int AppInfoNameComparePro(ListNode *node, void *data)
128 {
129     AppSpawnedProcess *node1 = ListEntry(node, AppSpawnedProcess, node);
130     return strcmp(node1->name, (char *)data);
131 }
132 
AppInfoCompareProc(ListNode * node,ListNode * newNode)133 static int AppInfoCompareProc(ListNode *node, ListNode *newNode)
134 {
135     AppSpawnedProcess *node1 = ListEntry(node, AppSpawnedProcess, node);
136     AppSpawnedProcess *node2 = ListEntry(newNode, AppSpawnedProcess, node);
137     return node1->pid - node2->pid;
138 }
139 
AddSpawnedProcess(pid_t pid,const char * processName)140 AppSpawnedProcess *AddSpawnedProcess(pid_t pid, const char *processName)
141 {
142     APPSPAWN_CHECK(g_appSpawnMgr != NULL && processName != NULL, return NULL, "Invalid mgr or process name");
143     APPSPAWN_CHECK(pid > 0, return NULL, "Invalid pid for %{public}s", processName);
144     size_t len = strlen(processName) + 1;
145     APPSPAWN_CHECK(len > 1, return NULL, "Invalid processName for %{public}s", processName);
146     AppSpawnedProcess *node = (AppSpawnedProcess *)calloc(1, sizeof(AppSpawnedProcess) + len + 1);
147     APPSPAWN_CHECK(node != NULL, return NULL, "Failed to malloc for appinfo");
148 
149     node->pid = pid;
150     node->max = 0;
151     node->uid = 0;
152     node->exitStatus = 0;
153     int ret = strcpy_s(node->name, len, processName);
154     APPSPAWN_CHECK(ret == 0, free(node);
155         return NULL, "Failed to strcpy process name");
156 
157     OH_ListInit(&node->node);
158     APPSPAWN_LOGI("Add %{public}s, pid=%{public}d success", processName, pid);
159     OH_ListAddWithOrder(&g_appSpawnMgr->appQueue, &node->node, AppInfoCompareProc);
160     return node;
161 }
162 
TerminateSpawnedProcess(AppSpawnedProcess * node)163 void TerminateSpawnedProcess(AppSpawnedProcess *node)
164 {
165     APPSPAWN_CHECK_ONLY_EXPER(g_appSpawnMgr != NULL && node != NULL, return);
166     // delete node
167     OH_ListRemove(&node->node);
168     OH_ListInit(&node->node);
169     if (!IsNWebSpawnMode(g_appSpawnMgr)) {
170         free(node);
171         return;
172     }
173     if (g_appSpawnMgr->diedAppCount >= MAX_DIED_PROCESS_COUNT) {
174         AppSpawnedProcess *oldApp = ListEntry(g_appSpawnMgr->diedQueue.next, AppSpawnedProcess, node);
175         OH_ListRemove(&oldApp->node);
176         OH_ListInit(&oldApp->node);
177         free(oldApp);
178         g_appSpawnMgr->diedAppCount--;
179     }
180     APPSPAWN_LOGI("ProcessAppDied %{public}s, pid=%{public}d", node->name, node->pid);
181     OH_ListAddTail(&g_appSpawnMgr->diedQueue, &node->node);
182     g_appSpawnMgr->diedAppCount++;
183 }
184 
GetSpawnedProcess(pid_t pid)185 AppSpawnedProcess *GetSpawnedProcess(pid_t pid)
186 {
187     APPSPAWN_CHECK_ONLY_EXPER(g_appSpawnMgr != NULL, return NULL);
188     ListNode *node = OH_ListFind(&g_appSpawnMgr->appQueue, &pid, AppInfoPidComparePro);
189     APPSPAWN_CHECK_ONLY_EXPER(node != NULL, return NULL);
190     return ListEntry(node, AppSpawnedProcess, node);
191 }
192 
GetSpawnedProcessByName(const char * name)193 AppSpawnedProcess *GetSpawnedProcessByName(const char *name)
194 {
195     APPSPAWN_CHECK_ONLY_EXPER(g_appSpawnMgr != NULL, return NULL);
196     APPSPAWN_CHECK_ONLY_EXPER(name != NULL, return NULL);
197 
198     ListNode *node = OH_ListFind(&g_appSpawnMgr->appQueue, (void *)name, AppInfoNameComparePro);
199     APPSPAWN_CHECK_ONLY_EXPER(node != NULL, return NULL);
200     return ListEntry(node, AppSpawnedProcess, node);
201 }
202 
DumpProcessSpawnStack(pid_t pid)203 static void DumpProcessSpawnStack(pid_t pid)
204 {
205 #if (!defined(CJAPP_SPAWN) && !defined(NATIVE_SPAWN))
206     DumpSpawnStack(pid);
207     DumpSpawnStack(getpid());
208 #endif
209     kill(pid, SIGKILL);
210     APPSPAWN_LOGI("Dump stack finished");
211 }
212 
KillAndWaitStatus(pid_t pid,int sig,int * exitStatus)213 int KillAndWaitStatus(pid_t pid, int sig, int *exitStatus)
214 {
215     APPSPAWN_CHECK_ONLY_EXPER(exitStatus != NULL, return 0);
216     *exitStatus = -1;
217     if (pid <= 0) {
218         return 0;
219     }
220 
221     if (kill(pid, sig) != 0) {
222         APPSPAWN_LOGE("unable to kill process, pid: %{public}d ret %{public}d", pid, errno);
223         return -1;
224     }
225 
226     int retry = 0;
227     pid_t exitPid = 0;
228     while (retry * SLEEP_DURATION < EXIT_APP_TIMEOUT) {
229         exitPid = waitpid(pid, exitStatus, WNOHANG);
230         if (exitPid == pid) {
231             return 0;
232         }
233         usleep(SLEEP_DURATION);
234         retry++;
235     }
236 
237     DumpProcessSpawnStack(pid);
238     APPSPAWN_LOGE("waitpid failed, pid: %{public}d %{public}d, status: %{public}d", exitPid, pid, *exitStatus);
239 
240     return -1;
241 }
242 
GetProcessTerminationStatus(pid_t pid)243 static int GetProcessTerminationStatus(pid_t pid)
244 {
245     APPSPAWN_CHECK_ONLY_EXPER(g_appSpawnMgr != NULL, return -1);
246     APPSPAWN_LOGV("GetProcessTerminationStatus pid: %{public}d ", pid);
247     if (pid <= 0) {
248         return 0;
249     }
250     int exitStatus = 0;
251     ListNode *node = OH_ListFind(&g_appSpawnMgr->diedQueue, &pid, AppInfoPidComparePro);
252     if (node != NULL) {
253         AppSpawnedProcess *info = ListEntry(node, AppSpawnedProcess, node);
254         exitStatus = info->exitStatus;
255         OH_ListRemove(node);
256         OH_ListInit(node);
257         free(info);
258         if (g_appSpawnMgr->diedAppCount > 0) {
259             g_appSpawnMgr->diedAppCount--;
260         }
261         return exitStatus;
262     }
263     AppSpawnedProcess *app = GetSpawnedProcess(pid);
264     if (app == NULL) {
265         APPSPAWN_LOGE("unable to get process, pid: %{public}d ", pid);
266         return -1;
267     }
268 
269     if (KillAndWaitStatus(pid, SIGKILL, &exitStatus) == 0) { // kill success, delete app
270         OH_ListRemove(&app->node);
271         OH_ListInit(&app->node);
272         free(app);
273     }
274     return exitStatus;
275 }
276 
CreateAppSpawningCtx(void)277 AppSpawningCtx *CreateAppSpawningCtx(void)
278 {
279     static uint32_t requestId = 0;
280     AppSpawningCtx *property = (AppSpawningCtx *)malloc(sizeof(AppSpawningCtx));
281     APPSPAWN_CHECK(property != NULL, return NULL, "Failed to create AppSpawningCtx ");
282     property->client.id = ++requestId;
283     property->client.flags = 0;
284     property->forkCtx.watcherHandle = NULL;
285     property->forkCtx.pidFdWatcherHandle = NULL;
286     property->forkCtx.coldRunPath = NULL;
287     property->forkCtx.timer = NULL;
288     property->forkCtx.fd[0] = -1;
289     property->forkCtx.fd[1] = -1;
290     property->forkCtx.childMsg = NULL;
291     property->message = NULL;
292     property->pid = 0;
293     property->state = APP_STATE_IDLE;
294     OH_ListInit(&property->node);
295     if (g_appSpawnMgr) {
296         OH_ListAddTail(&g_appSpawnMgr->appSpawnQueue, &property->node);
297     }
298     return property;
299 }
300 
DeleteAppSpawningCtx(AppSpawningCtx * property)301 void DeleteAppSpawningCtx(AppSpawningCtx *property)
302 {
303     APPSPAWN_CHECK_ONLY_EXPER(property != NULL, return);
304     APPSPAWN_LOGV("DeleteAppSpawningCtx");
305 
306     DeleteAppSpawnMsg(property->message);
307 
308     OH_ListRemove(&property->node);
309     if (property->forkCtx.timer) {
310         LE_StopTimer(LE_GetDefaultLoop(), property->forkCtx.timer);
311         property->forkCtx.timer = NULL;
312     }
313     if (property->forkCtx.watcherHandle) {
314         LE_RemoveWatcher(LE_GetDefaultLoop(), property->forkCtx.watcherHandle);
315         property->forkCtx.watcherHandle = NULL;
316     }
317     if (property->forkCtx.coldRunPath) {
318         free(property->forkCtx.coldRunPath);
319         property->forkCtx.coldRunPath = NULL;
320     }
321     if (property->forkCtx.fd[0] >= 0) {
322         close(property->forkCtx.fd[0]);
323     }
324     if (property->forkCtx.fd[1] >= 0) {
325         close(property->forkCtx.fd[1]);
326     }
327 
328     free(property);
329 }
330 
AppPropertyComparePid(ListNode * node,void * data)331 static int AppPropertyComparePid(ListNode *node, void *data)
332 {
333     AppSpawningCtx *property = ListEntry(node, AppSpawningCtx, node);
334     if (property->pid == *(pid_t *)data) {
335         return 0;
336     }
337     return 1;
338 }
339 
GetAppSpawningCtxByPid(pid_t pid)340 AppSpawningCtx *GetAppSpawningCtxByPid(pid_t pid)
341 {
342     APPSPAWN_CHECK_ONLY_EXPER(g_appSpawnMgr != NULL, return NULL);
343     ListNode *node = OH_ListFind(&g_appSpawnMgr->appSpawnQueue, (void *)&pid, AppPropertyComparePid);
344     APPSPAWN_CHECK_ONLY_EXPER(node != NULL, return NULL);
345     return ListEntry(node, AppSpawningCtx, node);
346 }
347 
AppSpawningCtxTraversal(ProcessTraversal traversal,void * data)348 void AppSpawningCtxTraversal(ProcessTraversal traversal, void *data)
349 {
350     APPSPAWN_CHECK_ONLY_EXPER(g_appSpawnMgr != NULL && traversal != NULL, return);
351     ListNode *node = g_appSpawnMgr->appSpawnQueue.next;
352     while (node != &g_appSpawnMgr->appSpawnQueue) {
353         ListNode *next = node->next;
354         AppSpawningCtx *ctx = ListEntry(node, AppSpawningCtx, node);
355         traversal(g_appSpawnMgr, ctx, data);
356         node = next;
357     }
358 }
359 
DumpAppSpawnQueue(ListNode * node,void * data)360 static int DumpAppSpawnQueue(ListNode *node, void *data)
361 {
362     AppSpawningCtx *property = ListEntry(node, AppSpawningCtx, node);
363     APPSPAPWN_DUMP("app property id: %{public}u flags: %{public}x",
364         property->client.id, property->client.flags);
365     APPSPAPWN_DUMP("app property state: %{public}d", property->state);
366 
367     DumpAppSpawnMsg(property->message);
368     return 0;
369 }
370 
DumpAppQueue(ListNode * node,void * data)371 static int DumpAppQueue(ListNode *node, void *data)
372 {
373     AppSpawnedProcess *appInfo = ListEntry(node, AppSpawnedProcess, node);
374     uint64_t diff = DiffTime(&appInfo->spawnStart, &appInfo->spawnEnd);
375     APPSPAPWN_DUMP("App info uid: %{public}u pid: %{public}x", appInfo->uid, appInfo->pid);
376     APPSPAPWN_DUMP("App info name: %{public}s exitStatus: 0x%{public}x spawn time: %{public}" PRIu64 " us ",
377         appInfo->name, appInfo->exitStatus, diff);
378     return 0;
379 }
380 
DumpExtData(ListNode * node,void * data)381 static int DumpExtData(ListNode *node, void *data)
382 {
383     AppSpawnExtData *extData = ListEntry(node, AppSpawnExtData, node);
384     if (extData->dumpNode) {
385         extData->dumpNode(extData);
386     }
387     return 0;
388 }
389 
ProcessAppSpawnDumpMsg(const AppSpawnMsgNode * message)390 void ProcessAppSpawnDumpMsg(const AppSpawnMsgNode *message)
391 {
392     APPSPAWN_CHECK_ONLY_EXPER(g_appSpawnMgr != NULL && message != NULL, return);
393     FILE *stream = NULL;
394     uint32_t len = 0;
395     char *ptyName = GetAppSpawnMsgExtInfo(message, "pty-name", &len);
396     if (ptyName != NULL) {
397         APPSPAWN_LOGI("Dump info to file '%{public}s'", ptyName);
398         char canonicalPtyPath[PATH_MAX] = { 0 };
399         if (realpath(ptyName, canonicalPtyPath) == NULL) {
400             return;
401         }
402 
403         stream = fopen(canonicalPtyPath, "w");
404         SetDumpToStream(stream);
405     } else {
406         SetDumpToStream(stdout);
407     }
408     APPSPAPWN_DUMP("Dump appspawn info start ... ");
409     APPSPAPWN_DUMP("APP spawning queue: ");
410     OH_ListTraversal((ListNode *)&g_appSpawnMgr->appSpawnQueue, NULL, DumpAppSpawnQueue, 0);
411     APPSPAPWN_DUMP("APP queue: ");
412     OH_ListTraversal((ListNode *)&g_appSpawnMgr->appQueue, "App queue", DumpAppQueue, 0);
413     APPSPAPWN_DUMP("APP died queue: ");
414     OH_ListTraversal((ListNode *)&g_appSpawnMgr->diedQueue, "App died queue", DumpAppQueue, 0);
415     APPSPAPWN_DUMP("Ext data: ");
416     OH_ListTraversal((ListNode *)&g_appSpawnMgr->extData, "Ext data", DumpExtData, 0);
417     APPSPAPWN_DUMP("Dump appspawn info finish ");
418     if (stream != NULL) {
419         (void)fflush(stream);
420         fclose(stream);
421 #ifdef APPSPAWN_TEST
422         SetDumpToStream(stdout);
423 #else
424         SetDumpToStream(NULL);
425 #endif
426     }
427 }
428 
ProcessTerminationStatusMsg(const AppSpawnMsgNode * message,AppSpawnResult * result)429 int ProcessTerminationStatusMsg(const AppSpawnMsgNode *message, AppSpawnResult *result)
430 {
431     APPSPAWN_CHECK_ONLY_EXPER(g_appSpawnMgr != NULL && message != NULL, return -1);
432     APPSPAWN_CHECK_ONLY_EXPER(result != NULL, return -1);
433     if (!IsNWebSpawnMode(g_appSpawnMgr)) {
434         return APPSPAWN_MSG_INVALID;
435     }
436     result->result = -1;
437     result->pid = 0;
438     pid_t *pid = (pid_t *)GetAppSpawnMsgInfo(message, TLV_RENDER_TERMINATION_INFO);
439     if (pid == NULL) {
440         return -1;
441     }
442     // get render process termination status, only nwebspawn need this logic.
443     result->pid = *pid;
444     result->result = GetProcessTerminationStatus(*pid);
445     return 0;
446 }
447