1d9f0492fSopenharmony_ci
2d9f0492fSopenharmony_ci/*
3d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
4d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
5d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
6d9f0492fSopenharmony_ci * You may obtain a copy of the License at
7d9f0492fSopenharmony_ci *
8d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
9d9f0492fSopenharmony_ci *
10d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
11d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
12d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
14d9f0492fSopenharmony_ci * limitations under the License.
15d9f0492fSopenharmony_ci */
16d9f0492fSopenharmony_ci#include <errno.h>
17d9f0492fSopenharmony_ci#include <fcntl.h>
18d9f0492fSopenharmony_ci#include <pthread.h>
19d9f0492fSopenharmony_ci#include <sched.h>
20d9f0492fSopenharmony_ci#include <signal.h>
21d9f0492fSopenharmony_ci#include <stdbool.h>
22d9f0492fSopenharmony_ci#include <stdint.h>
23d9f0492fSopenharmony_ci#include <stdio.h>
24d9f0492fSopenharmony_ci#include <sys/capability.h>
25d9f0492fSopenharmony_ci#include <sys/mman.h>
26d9f0492fSopenharmony_ci#include <sys/prctl.h>
27d9f0492fSopenharmony_ci#include <sys/syscall.h>
28d9f0492fSopenharmony_ci#include <sys/time.h>
29d9f0492fSopenharmony_ci#include <sys/types.h>
30d9f0492fSopenharmony_ci#include <sys/uio.h>
31d9f0492fSopenharmony_ci#include <sys/wait.h>
32d9f0492fSopenharmony_ci#include <linux/futex.h>
33d9f0492fSopenharmony_ci
34d9f0492fSopenharmony_ci#include <libunwind.h>
35d9f0492fSopenharmony_ci
36d9f0492fSopenharmony_ci#include "beget_ext.h"
37d9f0492fSopenharmony_ci#include "securec.h"
38d9f0492fSopenharmony_ci#include "init_cmds.h"
39d9f0492fSopenharmony_ci#include "init_log.h"
40d9f0492fSopenharmony_ci#include "crash_handler.h"
41d9f0492fSopenharmony_ci
42d9f0492fSopenharmony_cistatic const SignalInfo g_platformSignals[] = {
43d9f0492fSopenharmony_ci    { SIGABRT, "SIGABRT" },
44d9f0492fSopenharmony_ci    { SIGBUS, "SIGBUS" },
45d9f0492fSopenharmony_ci    { SIGFPE, "SIGFPE" },
46d9f0492fSopenharmony_ci    { SIGILL, "SIGILL" },
47d9f0492fSopenharmony_ci    { SIGSEGV, "SIGSEGV" },
48d9f0492fSopenharmony_ci#if defined(SIGSTKFLT)
49d9f0492fSopenharmony_ci    { SIGSTKFLT, "SIGSTKFLT" },
50d9f0492fSopenharmony_ci#endif
51d9f0492fSopenharmony_ci    { SIGSYS, "SIGSYS" },
52d9f0492fSopenharmony_ci    { SIGTRAP, "SIGTRAP" },
53d9f0492fSopenharmony_ci};
54d9f0492fSopenharmony_ci
55d9f0492fSopenharmony_cistatic void SignalHandler(int sig, siginfo_t *si, void *context)
56d9f0492fSopenharmony_ci{
57d9f0492fSopenharmony_ci    int32_t pid = getpid();
58d9f0492fSopenharmony_ci    if (pid == 1) {
59d9f0492fSopenharmony_ci        ExecReboot("panic");
60d9f0492fSopenharmony_ci    }
61d9f0492fSopenharmony_ci}
62d9f0492fSopenharmony_ci
63d9f0492fSopenharmony_civoid InstallLocalSignalHandler(void)
64d9f0492fSopenharmony_ci{
65d9f0492fSopenharmony_ci    sigset_t set;
66d9f0492fSopenharmony_ci    sigemptyset(&set);
67d9f0492fSopenharmony_ci    struct sigaction action;
68d9f0492fSopenharmony_ci    memset_s(&action, sizeof(action), 0, sizeof(action));
69d9f0492fSopenharmony_ci    action.sa_sigaction = SignalHandler;
70d9f0492fSopenharmony_ci    action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
71d9f0492fSopenharmony_ci
72d9f0492fSopenharmony_ci    for (size_t i = 0; i < sizeof(g_platformSignals) / sizeof(g_platformSignals[0]); i++) {
73d9f0492fSopenharmony_ci        int32_t sig = g_platformSignals[i].sigNo;
74d9f0492fSopenharmony_ci        sigemptyset(&action.sa_mask);
75d9f0492fSopenharmony_ci        sigaddset(&action.sa_mask, sig);
76d9f0492fSopenharmony_ci
77d9f0492fSopenharmony_ci        sigaddset(&set, sig);
78d9f0492fSopenharmony_ci        if (sigaction(sig, &action, NULL) != 0) {
79d9f0492fSopenharmony_ci            BEGET_LOGE("Failed to register signal(%d)", sig);
80d9f0492fSopenharmony_ci        }
81d9f0492fSopenharmony_ci    }
82d9f0492fSopenharmony_ci    sigprocmask(SIG_UNBLOCK, &set, NULL);
83d9f0492fSopenharmony_ci}