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
22using namespace OHOS::HiviewDFX;
23namespace OHOS {
24namespace system {
25constexpr char KEY_PROCESS_EXIT[] = "PROCESS_EXIT";
26constexpr char KEY_NAME[] = "PROCESS_NAME";
27constexpr char KEY_PID[] = "PID";
28constexpr char KEY_UID[] = "UID";
29constexpr char KEY_STATUS[] = "STATUS";
30constexpr int32_t MAX_NAME_LENGTH = 1024;
31
32void 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    HiSysEventWrite(HiSysEvent::Domain::STARTUP, KEY_PROCESS_EXIT, HiSysEvent::EventType::BEHAVIOR,
40        KEY_NAME, pname, KEY_PID, pid, KEY_UID, uid, KEY_STATUS, status);
41}
42}  // namespace system
43}  // namespace OHOS
44
45static int ProcessMgrRemoveApp(const AppSpawnMgr *content, const AppSpawnedProcessInfo *appInfo)
46{
47    OHOS::system::ProcessMgrRemoveApp(appInfo->name, appInfo->pid, appInfo->uid, appInfo->exitStatus);
48    return 0;
49}
50
51MODULE_CONSTRUCTOR(void)
52{
53    APPSPAWN_LOGV("Load sys event module ...");
54    AddProcessMgrHook(STAGE_SERVER_APP_DIED, 0, ProcessMgrRemoveApp);
55}
56