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 "control_fd.h"
19 #include "init.h"
20 #include "init_adapter.h"
21 #include "init_log.h"
22 #include "init_param.h"
23 #include "init_context.h"
24 #include "init_service_manager.h"
25 #include "loop_event.h"
26 #include "crash_handler.h"
27 #include "init_hisysevent.h"
28
29 static SignalHandle g_sigHandle = NULL;
30
HandleSigChild(const struct signalfd_siginfo * siginfo)31 static pid_t HandleSigChild(const struct signalfd_siginfo *siginfo)
32 {
33 int procStat = 0;
34 pid_t sigPID = waitpid(-1, &procStat, WNOHANG);
35 if (sigPID <= 0) {
36 return sigPID;
37 }
38 Service* service = GetServiceByPid(sigPID);
39 const char *serviceName = (service == NULL) ? "Unknown" : service->name;
40
41 // check child process exit status
42 if (WIFSIGNALED(procStat)) {
43 INIT_LOGW("Child process %s(pid %d) exit with signal : %d", serviceName, sigPID, WTERMSIG(procStat));
44 ReportChildProcessExit(serviceName, sigPID, WTERMSIG(procStat));
45 } else if (WIFEXITED(procStat)) {
46 INIT_LOGW("Child process %s(pid %d) exit with code : %d", serviceName, sigPID, WEXITSTATUS(procStat));
47 if (service != NULL) {
48 service->lastErrno = WEXITSTATUS(procStat);
49 }
50 }
51 CmdServiceProcessDelClient(sigPID);
52 StopSubInit(sigPID);
53 INIT_LOGW("Service warning %s, SIGCHLD received, pid:%d uid:%d status:%d.",
54 serviceName, sigPID, siginfo->ssi_uid, procStat);
55 CheckWaitPid(sigPID);
56 ServiceReap(service);
57 return sigPID;
58 }
59
ProcessSignal(const struct signalfd_siginfo * siginfo)60 INIT_STATIC void ProcessSignal(const struct signalfd_siginfo *siginfo)
61 {
62 switch (siginfo->ssi_signo) {
63 case SIGCHLD: {
64 while (HandleSigChild(siginfo) > 0) {
65 ;
66 }
67 break;
68 }
69 case SIGTERM: {
70 INIT_LOGI("SigHandler, SIGTERM received.");
71 SystemWriteParam("startup.device.ctl", "stop");
72 // exec reboot use toybox reboot cmd
73 ExecReboot("reboot");
74 break;
75 }
76 default:
77 INIT_LOGI("SigHandler, unsupported signal %d.", siginfo->ssi_signo);
78 break;
79 }
80 }
81
SignalInit(void)82 void SignalInit(void)
83 {
84 if (LE_CreateSignalTask(LE_GetDefaultLoop(), &g_sigHandle, ProcessSignal) == 0) {
85 if (LE_AddSignal(LE_GetDefaultLoop(), g_sigHandle, SIGCHLD) != 0) {
86 INIT_LOGW("start SIGCHLD handler failed");
87 }
88 if (LE_AddSignal(LE_GetDefaultLoop(), g_sigHandle, SIGTERM) != 0) {
89 INIT_LOGW("start SIGTERM handler failed");
90 }
91 }
92 InstallLocalSignalHandler();
93 }
94