init_modulemgr.c 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (c) 2020-2021 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 <stdlib.h>
#include "init_log.h"
#include "init_module_engine.h"

static MODULE_MGR *defaultModuleMgr = NULL;

int InitModuleMgrInstall(const char *moduleName)
{
    if (moduleName == NULL) {
        return -1;
    }

    if (defaultModuleMgr == NULL) {
M
Mupceet 已提交
28
        defaultModuleMgr = ModuleMgrCreate("init");
29 30 31 32 33 34 35 36 37 38 39 40 41
    }
    if (defaultModuleMgr == NULL) {
        return -1;
    }

    return ModuleMgrInstall(defaultModuleMgr, moduleName, 0, NULL);
}

void InitModuleMgrUnInstall(const char *moduleName)
{
    ModuleMgrUninstall(defaultModuleMgr, moduleName);
}

42 43 44 45 46 47 48 49 50 51 52 53 54 55
static void InitModuleDump(const MODULE_INFO *moduleInfo)
{
    printf("%s\n", moduleInfo->name);
}

void InitModuleMgrDump(void)
{
    if (defaultModuleMgr == NULL) {
        return;
    }

    ModuleMgrTraversal(defaultModuleMgr, NULL, InitModuleDump);
}

56 57 58 59
static int ModuleMgrCmdInstall(int id, const char *name, int argc, const char **argv)
{
    INIT_ERROR_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
    int ret;
M
Mupceet 已提交
60 61 62 63 64
    if (defaultModuleMgr == NULL) {
        defaultModuleMgr = ModuleMgrCreate("init");
    }
    ret = ModuleMgrInstall(defaultModuleMgr, argv[0], argc-1, argv+1);
    INIT_ERROR_CHECK(ret == 0, return ret, "Install module %s fail errno %d", argv[0], ret);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    return 0;
}

static int ModuleMgrCmdUninstall(int id, const char *name, int argc, const char **argv)
{
    INIT_ERROR_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
    ModuleMgrUninstall(NULL, argv[0]);
    return 0;
}

static int moduleMgrCommandsInit(int stage, int prio, void *cookie)
{
    // "ohos.servicectrl.install"
    (void)AddCmdExecutor("install", ModuleMgrCmdInstall);
    (void)AddCmdExecutor("uninstall", ModuleMgrCmdUninstall);
    // read cfg and start static plugin
    return 0;
}

H
handyohos 已提交
84 85 86
static int loadAutorunModules(int stage, int prio, void *cookie)
{
    MODULE_MGR *autorun = ModuleMgrScan("init/autorun");
M
Mupceet 已提交
87
    INIT_LOGV("Load autorun modules return %p", autorun);
H
handyohos 已提交
88 89 90
    return 0;
}

91 92 93
MODULE_CONSTRUCTOR(void)
{
    // Depends on parameter service
H
handyohos 已提交
94
    InitAddGlobalInitHook(0, loadAutorunModules);
95 96
    InitAddPreCfgLoadHook(0, moduleMgrCommandsInit);
}