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
42 static 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
SignalHandler(int sig,siginfo_t * si,void * context)55 static void SignalHandler(int sig, siginfo_t *si, void *context)
56 {
57 int32_t pid = getpid();
58 if (pid == 1) {
59 sleep(1);
60 ExecReboot("panic");
61 } else {
62 exit(-1);
63 }
64 }
65
InstallLocalSignalHandler(void)66 void InstallLocalSignalHandler(void)
67 {
68 sigset_t set;
69 sigemptyset(&set);
70 struct sigaction action;
71 memset_s(&action, sizeof(action), 0, sizeof(action));
72 action.sa_sigaction = SignalHandler;
73 action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
74
75 for (size_t i = 0; i < sizeof(g_platformSignals) / sizeof(g_platformSignals[0]); i++) {
76 int32_t sig = g_platformSignals[i].sigNo;
77 sigemptyset(&action.sa_mask);
78 sigaddset(&action.sa_mask, sig);
79
80 sigaddset(&set, sig);
81 if (sigaction(sig, &action, NULL) != 0) {
82 BEGET_LOGE("Failed to register signal(%d)", sig);
83 }
84 }
85 sigprocmask(SIG_UNBLOCK, &set, NULL);
86 }