init_signal_handler.c 3.2 KB
Newer Older
W
wenjun 已提交
1
/*
Z
zhong_ning 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <signal.h>
#include <sys/wait.h>
17

M
Mupceet 已提交
18
#include "control_fd.h"
C
cheng_jinsong 已提交
19
#include "init.h"
20
#include "init_adapter.h"
Z
zhong_ning 已提交
21
#include "init_log.h"
X
xionglei6 已提交
22
#include "init_param.h"
W
wenjun 已提交
23
#include "init_service_manager.h"
X
xionglei6 已提交
24
#include "loop_event.h"
M
mamingshuai 已提交
25

C
cheng_jinsong 已提交
26
static SignalHandle g_sigHandle = NULL;
M
mamingshuai 已提交
27

C
cheng_jinsong 已提交
28
INIT_STATIC void ProcessSignal(const struct signalfd_siginfo *siginfo)
W
wenjun 已提交
29
{
X
xionglei6 已提交
30
    switch (siginfo->ssi_signo) {
W
wenjun 已提交
31 32 33 34 35 36 37 38
        case SIGCHLD: {
            pid_t sigPID;
            int procStat = 0;
            while (1) {
                sigPID = waitpid(-1, &procStat, WNOHANG);
                if (sigPID <= 0) {
                    break;
                }
C
fix log  
cheng_jinsong 已提交
39
                Service* service = GetServiceByPid(sigPID);
Z
zhong_ning 已提交
40 41
                // check child process exit status
                if (WIFSIGNALED(procStat)) {
C
cheng_jinsong 已提交
42
                    INIT_LOGE("Child process %s(pid %d) exit with signal : %d",
C
cheng_jinsong 已提交
43
                        service == NULL ? "Unknown" : service->name, sigPID, WTERMSIG(procStat));
C
cheng_jinsong 已提交
44
                } else if (WIFEXITED(procStat)) {
C
fix log  
cheng_jinsong 已提交
45 46
                    INIT_LOGE("Child process %s(pid %d) exit with code : %d",
                        service == NULL ? "Unknown" : service->name, sigPID, WEXITSTATUS(procStat));
C
cheng_jinsong 已提交
47 48 49
                } else {
                    INIT_LOGE("Child process %s(pid %d) exit with invalid status : %d",
                        service == NULL ? "Unknown" : service->name, sigPID, procStat);
Z
zhong_ning 已提交
50
                }
M
Mupceet 已提交
51
                CmdServiceProcessDelClient(sigPID);
Y
yanmengzhao1 已提交
52 53 54
                INIT_LOGI("SigHandler, SIGCHLD received, Service:%s pid:%d uid:%d status:%d.",
                    service == NULL ? "Unknown" : service->name,
                    sigPID, siginfo->ssi_uid, procStat);
M
mamingshuai 已提交
55
                CheckWaitPid(sigPID);
M
maplestory 已提交
56
                ServiceReap(service);
W
wenjun 已提交
57 58 59 60
            }
            break;
        }
        case SIGTERM: {
Z
zhong_ning 已提交
61
            INIT_LOGI("SigHandler, SIGTERM received.");
X
xionglei6 已提交
62 63 64
            SystemWriteParam("startup.device.ctl", "stop");
            // exec reboot use toybox reboot cmd
            ExecReboot("reboot");
W
wenjun 已提交
65 66 67
            break;
        }
        default:
X
xionglei6 已提交
68
            INIT_LOGI("SigHandler, unsupported signal %d.", siginfo->ssi_signo);
W
wenjun 已提交
69 70 71 72
            break;
    }
}

73
void SignalInit(void)
Z
zhong_ning 已提交
74
{
C
cheng_jinsong 已提交
75 76 77 78 79 80 81
    if (LE_CreateSignalTask(LE_GetDefaultLoop(), &g_sigHandle, ProcessSignal) == 0) {
        if (LE_AddSignal(LE_GetDefaultLoop(), g_sigHandle, SIGCHLD) != 0) {
            INIT_LOGW("start SIGCHLD handler failed");
        }
        if (LE_AddSignal(LE_GetDefaultLoop(), g_sigHandle, SIGTERM) != 0) {
            INIT_LOGW("start SIGTERM handler failed");
        }
Z
zhong_ning 已提交
82
    }
C
cheng_jinsong 已提交
83
}