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