1 /*
2 * Copyright (c) 2022 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 <hisysevent.h>
17
18 #include "appspawn_hook.h"
19 #include "appspawn_manager.h"
20 #include "appspawn_utils.h"
21
22 using namespace OHOS::HiviewDFX;
23 namespace OHOS {
24 namespace system {
25 constexpr char KEY_PROCESS_EXIT[] = "PROCESS_EXIT";
26 constexpr char KEY_NAME[] = "PROCESS_NAME";
27 constexpr char KEY_PID[] = "PID";
28 constexpr char KEY_UID[] = "UID";
29 constexpr char KEY_STATUS[] = "STATUS";
30 constexpr int32_t MAX_NAME_LENGTH = 1024;
31
ProcessMgrRemoveApp(const char * processName,int pid,int uid,int status)32 void ProcessMgrRemoveApp(const char* processName, int pid, int uid, int status)
33 {
34 std::string pname = "Unknown";
35 if ((processName != NULL) && (strlen(processName) <= MAX_NAME_LENGTH)) {
36 pname = std::string(processName, strlen(processName));
37 }
38
39 int signal = 0;
40 if (WIFSIGNALED(status)) {
41 signal = WTERMSIG(status);
42 }
43 if (WIFEXITED(status)) {
44 signal = WEXITSTATUS(status);
45 }
46
47 if (signal != 0) {
48 int ret = HiSysEventWrite(HiSysEvent::Domain::STARTUP, KEY_PROCESS_EXIT, HiSysEvent::EventType::BEHAVIOR,
49 KEY_NAME, pname, KEY_PID, pid, KEY_UID, uid, KEY_STATUS, status);
50 if (ret != 0) {
51 APPSPAWN_LOGE("ProcessMgrRemoveApp error, ret: %{public}d", ret);
52 }
53 }
54 }
55 } // namespace system
56 } // namespace OHOS
57
ProcessMgrRemoveApp(const AppSpawnMgr * content,const AppSpawnedProcessInfo * appInfo)58 static int ProcessMgrRemoveApp(const AppSpawnMgr *content, const AppSpawnedProcessInfo *appInfo)
59 {
60 OHOS::system::ProcessMgrRemoveApp(appInfo->name, appInfo->pid, appInfo->uid, appInfo->exitStatus);
61 return 0;
62 }
63
MODULE_CONSTRUCTOR(void)64 MODULE_CONSTRUCTOR(void)
65 {
66 APPSPAWN_LOGV("Load sys event module ...");
67 AddProcessMgrHook(STAGE_SERVER_APP_DIED, 0, ProcessMgrRemoveApp);
68 }
69