1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <condition_variable>
17800b99b8Sopenharmony_ci#include <csignal>
18800b99b8Sopenharmony_ci#include <cstdlib>
19800b99b8Sopenharmony_ci#include <memory>
20800b99b8Sopenharmony_ci#include <mutex>
21800b99b8Sopenharmony_ci#include <thread>
22800b99b8Sopenharmony_ci#ifdef HISYSEVENT_ENABLE
23800b99b8Sopenharmony_ci#include "crash_validator.h"
24800b99b8Sopenharmony_ci
25800b99b8Sopenharmony_cistatic std::shared_ptr<OHOS::HiviewDFX::CrashValidator> g_validator = nullptr;
26800b99b8Sopenharmony_cistatic void SigIntHandler()
27800b99b8Sopenharmony_ci{
28800b99b8Sopenharmony_ci    constexpr time_t breakTime = 5;
29800b99b8Sopenharmony_ci    static time_t lastHandleTime = 0;
30800b99b8Sopenharmony_ci    auto now = time(nullptr);
31800b99b8Sopenharmony_ci    if (now - lastHandleTime < breakTime) {
32800b99b8Sopenharmony_ci        raise(SIGTERM);
33800b99b8Sopenharmony_ci    } else {
34800b99b8Sopenharmony_ci        if (g_validator != nullptr) {
35800b99b8Sopenharmony_ci            g_validator->Dump(1);
36800b99b8Sopenharmony_ci        }
37800b99b8Sopenharmony_ci    }
38800b99b8Sopenharmony_ci    lastHandleTime = now;
39800b99b8Sopenharmony_ci}
40800b99b8Sopenharmony_ci
41800b99b8Sopenharmony_ciint main(int argc, char *argv[])
42800b99b8Sopenharmony_ci{
43800b99b8Sopenharmony_ci    sigset_t waitMask;
44800b99b8Sopenharmony_ci    sigemptyset(&waitMask);
45800b99b8Sopenharmony_ci    sigaddset(&waitMask, SIGINT);
46800b99b8Sopenharmony_ci    sigprocmask(SIG_SETMASK, &waitMask, nullptr);
47800b99b8Sopenharmony_ci    g_validator = std::make_shared<OHOS::HiviewDFX::CrashValidator>();
48800b99b8Sopenharmony_ci    if (g_validator != nullptr) {
49800b99b8Sopenharmony_ci        g_validator->InitSysEventListener();
50800b99b8Sopenharmony_ci    }
51800b99b8Sopenharmony_ci    int sig = 0;
52800b99b8Sopenharmony_ci    int ret = -1;
53800b99b8Sopenharmony_ci    do {
54800b99b8Sopenharmony_ci        ret = sigwait(&waitMask, &sig);
55800b99b8Sopenharmony_ci        SigIntHandler();
56800b99b8Sopenharmony_ci    } while (ret == 0);
57800b99b8Sopenharmony_ci    return 0;
58800b99b8Sopenharmony_ci}
59800b99b8Sopenharmony_ci#endif
60