• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "beget_ext.h"
35 #include "securec.h"
36 #include "init_cmds.h"
37 #include "init_log.h"
38 #include "init_service.h"
39 #include "hookmgr.h"
40 #include "bootstage.h"
41 #include "crash_handler.h"
42 
43 static const SignalInfo g_platformSignals[] = {
44     { SIGABRT, "SIGABRT" },
45     { SIGBUS, "SIGBUS" },
46     { SIGFPE, "SIGFPE" },
47     { SIGILL, "SIGILL" },
48     { SIGSEGV, "SIGSEGV" },
49 #if defined(SIGSTKFLT)
50     { SIGSTKFLT, "SIGSTKFLT" },
51 #endif
52     { SIGSYS, "SIGSYS" },
53     { SIGTRAP, "SIGTRAP" },
54 };
55 
DoCriticalInit(void)56 static void DoCriticalInit(void)
57 {
58 #ifndef OHOS_LITE
59     Service service = {
60         .pid = 1,
61         .name = "init",
62     };
63 
64     BEGET_LOGI("ServiceReap init begin");
65     HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_REAP, (void *)&service, NULL);
66     BEGET_LOGI("ServiceReap init end!");
67 #endif
68 }
69 
SignalHandler(int sig,siginfo_t * si,void * context)70 static void SignalHandler(int sig, siginfo_t *si, void *context)
71 {
72     int32_t pid = getpid();
73     if (pid == 1) {
74         sleep(1);
75         DoCriticalInit();
76         ExecReboot("panic");
77     } else {
78         exit(-1);
79     }
80 }
81 
InstallLocalSignalHandler(void)82 void InstallLocalSignalHandler(void)
83 {
84     sigset_t set;
85     sigemptyset(&set);
86     struct sigaction action;
87     memset_s(&action, sizeof(action), 0, sizeof(action));
88     action.sa_sigaction = SignalHandler;
89     action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
90 
91     for (size_t i = 0; i < sizeof(g_platformSignals) / sizeof(g_platformSignals[0]); i++) {
92         int32_t sig = g_platformSignals[i].sigNo;
93         sigemptyset(&action.sa_mask);
94         sigaddset(&action.sa_mask, sig);
95 
96         sigaddset(&set, sig);
97         if (sigaction(sig, &action, NULL) != 0) {
98             BEGET_LOGE("Failed to register signal(%d)", sig);
99         }
100     }
101     sigprocmask(SIG_UNBLOCK, &set, NULL);
102 }