• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <signal.h>
16 #include <sys/wait.h>
17 
18 #include "init_adapter.h"
19 #include "init_log.h"
20 #include "init_param.h"
21 #include "init_service_manager.h"
22 #include "loop_event.h"
23 
24 SignalHandle g_sigHandle = NULL;
25 
ProcessSignal(const struct signalfd_siginfo * siginfo)26 static void ProcessSignal(const struct signalfd_siginfo *siginfo)
27 {
28     switch (siginfo->ssi_signo) {
29         case SIGCHLD: {
30             pid_t sigPID;
31             int procStat = 0;
32             while (1) {
33                 sigPID = waitpid(-1, &procStat, WNOHANG);
34                 if (sigPID <= 0) {
35                     break;
36                 }
37                 // check child process exit status
38                 if (WIFSIGNALED(procStat)) {
39                     INIT_LOGE("Child process %d exit with signal: %d", sigPID, WTERMSIG(procStat));
40                 }
41                 if (WIFEXITED(procStat)) {
42                     INIT_LOGE("Child process %d exit with code : %d", sigPID, WEXITSTATUS(procStat));
43                 }
44                 INIT_LOGI("SigHandler, SIGCHLD received, sigPID = %d.", sigPID);
45                 CheckWaitPid(sigPID);
46                 ServiceReap(GetServiceByPid(sigPID));
47             }
48             break;
49         }
50         case SIGTERM: {
51             INIT_LOGI("SigHandler, SIGTERM received.");
52             SystemWriteParam("startup.device.ctl", "stop");
53             // exec reboot use toybox reboot cmd
54             ExecReboot("reboot");
55             break;
56         }
57         default:
58             INIT_LOGI("SigHandler, unsupported signal %d.", siginfo->ssi_signo);
59             break;
60     }
61 }
62 
SignalInit(void)63 void SignalInit(void)
64 {
65     if (LE_CreateSignalTask(LE_GetDefaultLoop(), &g_sigHandle, ProcessSignal) != 0) {
66         INIT_LOGW("initialize signal handler failed");
67         return;
68     }
69     if (LE_AddSignal(LE_GetDefaultLoop(), g_sigHandle, SIGCHLD) != 0) {
70         INIT_LOGW("start SIGCHLD handler failed");
71     }
72     if (LE_AddSignal(LE_GetDefaultLoop(), g_sigHandle, SIGTERM) != 0) {
73         INIT_LOGW("start SIGTERM handler failed");
74     }
75 }