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 (void)ProcessServiceDied(service);
41
42 // check child process exit status
43 if (WIFSIGNALED(procStat)) {
44 INIT_LOGW("Child process %s(pid %d) exit with signal : %d", serviceName, sigPID, WTERMSIG(procStat));
45 ReportChildProcessExit(serviceName, sigPID, WTERMSIG(procStat));
46 } else if (WIFEXITED(procStat)) {
47 INIT_LOGW("Child process %s(pid %d) exit with code : %d", serviceName, sigPID, WEXITSTATUS(procStat));
48 if (service != NULL) {
49 service->lastErrno = WEXITSTATUS(procStat);
50 }
51 }
52 CmdServiceProcessDelClient(sigPID);
53 StopSubInit(sigPID);
54 INIT_LOGW("Service warning %s, SIGCHLD received, pid:%d uid:%d status:%d.",
55 serviceName, sigPID, siginfo->ssi_uid, procStat);
56 CheckWaitPid(sigPID);
57 ServiceReap(service);
58 return sigPID;
59 }
60
ProcessSignal(const struct signalfd_siginfo * siginfo)61 INIT_STATIC void ProcessSignal(const struct signalfd_siginfo *siginfo)
62 {
63 switch (siginfo->ssi_signo) {
64 case SIGCHLD: {
65 while (HandleSigChild(siginfo) > 0) {
66 ;
67 }
68 break;
69 }
70 case SIGTERM: {
71 INIT_LOGI("SigHandler, SIGTERM received.");
72 SystemWriteParam("startup.device.ctl", "stop");
73 // exec reboot use toybox reboot cmd
74 ExecReboot("reboot");
75 break;
76 }
77 default:
78 INIT_LOGI("SigHandler, unsupported signal %d.", siginfo->ssi_signo);
79 break;
80 }
81 }
82
SignalInit(void)83 void SignalInit(void)
84 {
85 if (LE_CreateSignalTask(LE_GetDefaultLoop(), &g_sigHandle, ProcessSignal) == 0) {
86 if (LE_AddSignal(LE_GetDefaultLoop(), g_sigHandle, SIGCHLD) != 0) {
87 INIT_LOGW("start SIGCHLD handler failed");
88 }
89 if (LE_AddSignal(LE_GetDefaultLoop(), g_sigHandle, SIGTERM) != 0) {
90 INIT_LOGW("start SIGTERM handler failed");
91 }
92 }
93 InstallLocalSignalHandler();
94 }
95