1/*
2 * Copyright (c) 2023 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 <condition_variable>
17#include <csignal>
18#include <cstdlib>
19#include <memory>
20#include <mutex>
21#include <thread>
22#ifdef HISYSEVENT_ENABLE
23#include "crash_validator.h"
24
25static std::shared_ptr<OHOS::HiviewDFX::CrashValidator> g_validator = nullptr;
26static void SigIntHandler()
27{
28    constexpr time_t breakTime = 5;
29    static time_t lastHandleTime = 0;
30    auto now = time(nullptr);
31    if (now - lastHandleTime < breakTime) {
32        raise(SIGTERM);
33    } else {
34        if (g_validator != nullptr) {
35            g_validator->Dump(1);
36        }
37    }
38    lastHandleTime = now;
39}
40
41int main(int argc, char *argv[])
42{
43    sigset_t waitMask;
44    sigemptyset(&waitMask);
45    sigaddset(&waitMask, SIGINT);
46    sigprocmask(SIG_SETMASK, &waitMask, nullptr);
47    g_validator = std::make_shared<OHOS::HiviewDFX::CrashValidator>();
48    if (g_validator != nullptr) {
49        g_validator->InitSysEventListener();
50    }
51    int sig = 0;
52    int ret = -1;
53    do {
54        ret = sigwait(&waitMask, &sig);
55        SigIntHandler();
56    } while (ret == 0);
57    return 0;
58}
59#endif
60