reboot_static.c 5.7 KB
Newer Older
C
cheng_jinsong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * 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.
 */
C
cheng_jinsong 已提交
15 16 17 18
#include <errno.h>

#include "bootstage.h"
#include "hookmgr.h"
C
cheng_jinsong 已提交
19 20 21
#include "init_hook.h"
#include "init_module_engine.h"
#include "plugin_adapter.h"
C
cheng_jinsong 已提交
22 23 24 25 26
#include "securec.h"

#define REBOOT_NAME_PREFIX "reboot,"
#define REBOOT_CMD_PREFIX "reboot."
#define REBOOT_REPLACE_PREFIX "reboot."
27
#define PARAM_CMD_MAX 100
C
cheng_jinsong 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

static int RebootHookWrapper(const HOOK_INFO *hookInfo, void *executionContext)
{
    RebootHookCtx *ctx = (RebootHookCtx *)executionContext;
    InitRebootHook realHook = (InitRebootHook)hookInfo->hookCookie;
    realHook(ctx);
    return 0;
};

int InitAddRebootHook(InitRebootHook hook)
{
    HOOK_INFO info;
    info.stage = INIT_REBOOT;
    info.prio = 0;
    info.hook = RebootHookWrapper;
    info.hookCookie = (void *)hook;
    return HookMgrAddEx(GetBootStageHookMgr(), &info);
}

C
cheng_jinsong 已提交
47 48 49 50
static ParamCmdInfo *g_rebootParamCmdInfos = NULL;
static int g_rebootParamCmdMaxNumber = 0;
static int g_rebootParamCmdValidNumber = 0;
static char *Dup2String(const char *prefix, const char *str)
C
cheng_jinsong 已提交
51
{
C
cheng_jinsong 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    if (str == NULL) {
        return strdup("reboot");
    }
    size_t len = strlen(prefix) + strlen(str) + 1;
    char *tmp = calloc(1, len);
    PLUGIN_CHECK(tmp != NULL, return NULL, "Failed to alloc %s %s", prefix, str);
    int ret = sprintf_s(tmp, len, "%s%s", prefix, str);
    PLUGIN_CHECK(ret > 0, free(tmp);
        return NULL, "Failed to sprintf %s %s", prefix, str);
    return tmp;
}

static int CheckParamCmdExist(const char *cmd)
{
    if (g_rebootParamCmdInfos == NULL) {
        return 0;
    }
    char *cmdName = Dup2String(REBOOT_CMD_PREFIX, cmd);
    PLUGIN_CHECK(cmdName != NULL, return 0, "Failed to copy %s", cmd);
    for (int i = 0; i < g_rebootParamCmdValidNumber; i++) {
C
cheng_jinsong 已提交
72
        if (strcmp(g_rebootParamCmdInfos[i].cmd, cmdName) == 0) {
C
cheng_jinsong 已提交
73 74 75 76 77
            free(cmdName);
            return 1;
        }
    }
    free(cmdName);
C
cheng_jinsong 已提交
78 79 80
    return 0;
}

C
cheng_jinsong 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
static int SetParamCmdInfo(ParamCmdInfo *currInfo, CmdExecutor executor, const char *cmd)
{
    do {
        currInfo->name = Dup2String(REBOOT_NAME_PREFIX, cmd);
        PLUGIN_CHECK(currInfo->name != NULL, break, "Failed to copy %s", cmd);
        currInfo->replace = Dup2String(REBOOT_REPLACE_PREFIX, cmd);
        PLUGIN_CHECK(currInfo->replace != NULL, break, "Failed to copy %s", cmd);
        currInfo->cmd = Dup2String(REBOOT_CMD_PREFIX, cmd);
        PLUGIN_CHECK(currInfo->cmd != NULL, break, "Failed to copy %s", cmd);
        if (executor != NULL) {
            int cmdId = AddCmdExecutor(currInfo->cmd, executor);
            PLUGIN_CHECK(cmdId > 0, break, "Failed to add cmd %s", cmd);
        }
        PLUGIN_LOGV("SetParamCmdInfo '%s' '%s' '%s' ", currInfo->name, currInfo->cmd, currInfo->replace);
        currInfo = NULL;
        g_rebootParamCmdValidNumber++;
        return 0;
    } while (0);
    if (currInfo != NULL) {
        if (currInfo->name != NULL) {
            free(currInfo->name);
        }
        if (currInfo->cmd != NULL) {
            free(currInfo->cmd);
        }
        if (currInfo->replace != NULL) {
            free(currInfo->replace);
        }
    }
    return -1;
}

static int AddRebootCmdExecutor_(const char *cmd, CmdExecutor executor)
{
115
    PLUGIN_CHECK(g_rebootParamCmdMaxNumber <= PARAM_CMD_MAX, return -1, "Param cmd max number exceed limit");
C
cheng_jinsong 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    if (g_rebootParamCmdMaxNumber == 0 || g_rebootParamCmdMaxNumber <= g_rebootParamCmdValidNumber) {
        g_rebootParamCmdMaxNumber += 5; // inc 5 once time
        ParamCmdInfo *cmdInfos = calloc(1, sizeof(ParamCmdInfo) * g_rebootParamCmdMaxNumber);
        PLUGIN_CHECK(cmdInfos != NULL, return -1, "Failed to add reboot cmd %s", cmd);
        if (g_rebootParamCmdInfos != NULL) { // delete old
            // copy from old
            for (int i = 0; i < g_rebootParamCmdValidNumber; i++) {
                cmdInfos[i].name = g_rebootParamCmdInfos[i].name;
                cmdInfos[i].replace = g_rebootParamCmdInfos[i].replace;
                cmdInfos[i].cmd = g_rebootParamCmdInfos[i].cmd;
            }
            free(g_rebootParamCmdInfos);
        }
        g_rebootParamCmdInfos = cmdInfos;
    }
131
    PLUGIN_CHECK(g_rebootParamCmdValidNumber < g_rebootParamCmdMaxNumber, return -1, "Param cmd number exceed limit");
C
cheng_jinsong 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    return SetParamCmdInfo(&g_rebootParamCmdInfos[g_rebootParamCmdValidNumber], executor, cmd);
}

int AddRebootCmdExecutor(const char *cmd, CmdExecutor executor)
{
    PLUGIN_CHECK(cmd != NULL && executor != NULL, return EINVAL, "Invalid input parameter");
    int ret = CheckParamCmdExist(cmd);
    if (ret != 0) {
        PLUGIN_LOGI("Cmd %s exist", cmd);
        return EEXIST;
    }
    return AddRebootCmdExecutor_(cmd, executor);
}

const ParamCmdInfo *GetStartupPowerCtl(size_t *size)
{
    RebootHookCtx context;
    context.reason = "";
    (void)HookMgrExecute(GetBootStageHookMgr(), INIT_REBOOT, (void *)(&context), NULL);
    PLUGIN_LOGI("After install reboot module");
    *size = g_rebootParamCmdValidNumber;
    return g_rebootParamCmdInfos;
}

C
cheng_jinsong 已提交
156 157 158 159 160 161 162 163 164 165
static void InitRebootHook_(RebootHookCtx *ctx)
{
    InitModuleMgrInstall("rebootmodule");
    PLUGIN_LOGI("Install rebootmodule.");
}

MODULE_CONSTRUCTOR(void)
{
    // 执行reboot时调用,安装reboot模块
    InitAddRebootHook(InitRebootHook_);
C
cheng_jinsong 已提交
166
}