init_hook.c 7.6 KB
Newer Older
M
Mupceet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

/*
 * 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.
 */

#include "init_hook.h"
#include "init_service.h"
#include "init_utils.h"
#include "plugin_adapter.h"
#include "securec.h"
#include "init_module_engine.h"
#include "init_group_manager.h"
C
cheng_jinsong 已提交
24
#include "init_param.h"
M
Mupceet 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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
#include "hookmgr.h"
#include "bootstage.h"

static int ServiceExtDataCompareProc(ListNode *node, void *data)
{
    ServiceExtData *item = ListEntry(node, ServiceExtData, node);
    if (item->dataId == *(uint32_t *)data) {
        return 0;
    }
    return -1;
}

static ServiceExtData *GetServiceExtData_(Service *service, uint32_t id)
{
    ListNode *node = OH_ListFind(&service->extDataNode, (void *)&id, ServiceExtDataCompareProc);
    return (ServiceExtData *)node;
}

ServiceExtData *AddServiceExtData(const char *serviceName, uint32_t id, void *data, uint32_t dataLen)
{
    Service *service = GetServiceByName(serviceName);
    PLUGIN_CHECK(service != NULL, return NULL, "Can not find service for %s", serviceName);
    ServiceExtData *extData = GetServiceExtData_(service, id);
    if (extData != NULL) {
        return NULL;
    }
    extData = calloc(1, sizeof(ServiceExtData) + dataLen);
    PLUGIN_CHECK(extData != NULL, return NULL, "Can not alloc extData for %d", id);
    OH_ListInit(&extData->node);
    extData->dataId = id;
    if (data != NULL) {
        int ret = memcpy_s(extData->data, dataLen, data, dataLen);
        if (ret == 0) {
            OH_ListAddTail(&service->extDataNode, &extData->node);
            return extData;
        }
    } else {
        OH_ListAddTail(&service->extDataNode, &extData->node);
        return extData;
    }
    free(extData);
    return NULL;
}

void DelServiceExtData(const char *serviceName, uint32_t id)
{
    Service *service = GetServiceByName(serviceName);
    PLUGIN_CHECK(service != NULL, return, "Can not find service for %s", serviceName);
    ServiceExtData *extData = GetServiceExtData_(service, id);
    if (extData == NULL) {
        return;
    }
    OH_ListRemove(&extData->node);
    free(extData);
}

ServiceExtData *GetServiceExtData(const char *serviceName, uint32_t id)
{
    Service *service = GetServiceByName(serviceName);
    PLUGIN_CHECK (service != NULL, return NULL, "Can not find service for %s", serviceName);
    return GetServiceExtData_(service, id);
}

static int ServiceClearHookWrapper(const HOOK_INFO *hookInfo, void *executionContext)
{
    SERVICE_INFO_CTX *ctx = (SERVICE_INFO_CTX *)executionContext;
    ServiceHook realHook = (ServiceHook)hookInfo->hookCookie;
    realHook(ctx);
    return 0;
};

int InitAddClearServiceHook(ServiceHook hook)
{
    HOOK_INFO info;
    info.stage = INIT_SERVICE_CLEAR;
    info.prio = 0;
    info.hook = ServiceClearHookWrapper;
    info.hookCookie = (void *)hook;
    return HookMgrAddEx(GetBootStageHookMgr(), &info);
}

C
codex  
chengjinsong 已提交
106
static int CmdClear(int id, const char *name, int argc, const char **argv)
M
Mupceet 已提交
107 108 109
{
    SERVICE_INFO_CTX ctx = {0};
    ctx.reserved = argc >= 1 ? argv[0] : NULL;
C
codex  
chengjinsong 已提交
110
    PLUGIN_LOGI("CmdClear %s cmd: %s", name, ctx.reserved);
M
Mupceet 已提交
111 112 113 114 115 116 117 118 119 120 121

    InitGroupNode *node = GetNextGroupNode(NODE_TYPE_SERVICES, NULL);
    while (node != NULL) {
        if (node->data.service == NULL) {
            node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
            continue;
        }
        ctx.serviceName = node->name;
        HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_CLEAR, (void *)&ctx, NULL);
        node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
    }
122 123
    ctx.reserved = "clearInitBootevent";
    HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_CLEAR, (void *)&ctx, NULL);
M
Mupceet 已提交
124 125 126
    return 0;
}

C
cheng_jinsong 已提交
127 128 129 130 131 132 133 134 135
static void SetLogLevel_(const char *value)
{
    unsigned int level;
    int ret = StringToUint(value, &level);
    PLUGIN_CHECK(ret == 0, return, "Failed make %s to unsigned int", value);
    PLUGIN_LOGI("Set log level is %d", level);
    SetInitLogLevel(level);
}

C
cheng_jinsong 已提交
136 137 138
static int CmdSetLogLevel(int id, const char *name, int argc, const char **argv)
{
    UNUSED(id);
C
cheng_jinsong 已提交
139 140 141
    UNUSED(name);
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid input args");
    const char *value = strrchr(argv[0], '.');
C
cheng_jinsong 已提交
142
    PLUGIN_CHECK(value != NULL, return -1, "Failed get \'.\' from string %s", argv[0]);
C
cheng_jinsong 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    SetLogLevel_(value + 1);
    return 0;
}

static int initCmd(int id, const char *name, int argc, const char **argv)
{
    UNUSED(id);
    // process cmd by name
    PLUGIN_LOGI("initCmd %s argc %d", name, argc);
    for (int i = 0; i < argc; i++) {
        PLUGIN_LOGI("initCmd %s", argv[i]);
    }
    if (argc > 1 && strcmp(argv[0], "setloglevel") == 0) {
        SetLogLevel_(argv[1]);
    }
C
cheng_jinsong 已提交
158 159 160 161
    return 0;
}

static int ParamSetInitCmdHook(const HOOK_INFO *hookInfo, void *cookie)
M
Mupceet 已提交
162
{
C
codex  
chengjinsong 已提交
163
    AddCmdExecutor("clear", CmdClear);
C
cheng_jinsong 已提交
164
    AddCmdExecutor("setloglevel", CmdSetLogLevel);
C
cheng_jinsong 已提交
165
    AddCmdExecutor("initcmd", initCmd);
M
Mupceet 已提交
166 167 168
    return 0;
}

C
cheng_jinsong 已提交
169 170 171 172 173 174 175 176 177
static int DumpTrigger(const char *fmt, ...)
{
    va_list vargs;
    va_start(vargs, fmt);
    InitLog(INIT_INFO, INIT_LOG_DOMAIN, INIT_LOG_TAG, fmt, vargs);
    va_end(vargs);
    return 0;
}

C
cheng_jinsong 已提交
178
static void DumpServiceHook(void)
C
cheng_jinsong 已提交
179 180 181 182 183 184 185 186 187
{
    // check and dump all jobs
    char dump[8] = {0}; // 8 len
    uint32_t len = sizeof(dump);
    int ret = SystemReadParam("persist.init.debug.dump.trigger", dump, &len);
    PLUGIN_LOGV("boot dump %s ret %d", dump, ret);
    if (ret == 0 && strcmp(dump, "1") == 0) {
        SystemDumpTriggers(1, DumpTrigger);
    }
C
cheng_jinsong 已提交
188 189 190 191 192 193 194
    return;
}

static void InitLogLevelFromPersist(void)
{
    char logLevel[2] = {0}; // 2 is set param "persist.init.debug.loglevel" value length.
    uint32_t len = sizeof(logLevel);
C
cheng_jinsong 已提交
195
    int ret = SystemReadParam(INIT_DEBUG_LEVEL, logLevel, &len);
C
cheng_jinsong 已提交
196
    INIT_INFO_CHECK(ret == 0, return, "Can not get log level from param, keep the original loglevel.");
C
cheng_jinsong 已提交
197
    SetLogLevel_(logLevel);
C
cheng_jinsong 已提交
198 199 200 201 202 203 204 205 206
    return;
}

static int InitDebugHook(const HOOK_INFO *info, void *cookie)
{
    UNUSED(info);
    UNUSED(cookie);
    InitLogLevelFromPersist();
    DumpServiceHook();
C
cheng_jinsong 已提交
207 208 209
    return 0;
}

C
cheng_jinsong 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
// clear extend memory
static int BootCompleteCmd(const HOOK_INFO *hookInfo, void *executionContext)
{
    PLUGIN_LOGI("boot start complete");
    UNUSED(hookInfo);
    UNUSED(executionContext);

    // clear hook
    HookMgrDel(GetBootStageHookMgr(), INIT_GLOBAL_INIT, NULL);
    HookMgrDel(GetBootStageHookMgr(), INIT_PRE_PARAM_SERVICE, NULL);
    HookMgrDel(GetBootStageHookMgr(), INIT_PRE_PARAM_LOAD, NULL);
    HookMgrDel(GetBootStageHookMgr(), INIT_PRE_CFG_LOAD, NULL);
    HookMgrDel(GetBootStageHookMgr(), INIT_SERVICE_PARSE, NULL);
    HookMgrDel(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, NULL);
    HookMgrDel(GetBootStageHookMgr(), INIT_POST_CFG_LOAD, NULL);
    HookMgrDel(GetBootStageHookMgr(), INIT_JOB_PARSE, NULL);
    // clear cmd
    RemoveCmdExecutor("loadSelinuxPolicy", -1);
    return 0;
}

M
Mupceet 已提交
231 232
MODULE_CONSTRUCTOR(void)
{
C
cheng_jinsong 已提交
233
    HookMgrAdd(GetBootStageHookMgr(), INIT_BOOT_COMPLETE, 0, BootCompleteCmd);
C
cheng_jinsong 已提交
234
    InitAddGlobalInitHook(0, ParamSetInitCmdHook);
C
cheng_jinsong 已提交
235
    // Depends on parameter service
C
cheng_jinsong 已提交
236
    InitAddPostPersistParamLoadHook(0, InitDebugHook);
M
Mupceet 已提交
237
}