提交 cce477f8 编写于 作者: H handyohos

feat: add plugin engine for init.

1)通过ohos_native_stub_library为init提供libinit_plugin_engine打桩库,解决插件的编译链接问题。
2)通过ohos_native_stub_versionscript为init进程export开放API供插件调用。
3)通过libinit_stub_empty创建空的so库解决dlopen插件时找不到库的问题。
Signed-off-by: Nhandyohos <zhangxiaotian@huawei.com>
Change-Id: I631723ce7a388d5f2067edf12068f7eb1e4c21da
上级 c52d3ee6
...@@ -48,7 +48,8 @@ ...@@ -48,7 +48,8 @@
"//base/startup/init_lite/interfaces/innerkits/file:libfile", "//base/startup/init_lite/interfaces/innerkits/file:libfile",
"//base/startup/init_lite/interfaces/innerkits/socket:libsocket", "//base/startup/init_lite/interfaces/innerkits/socket:libsocket",
"//base/startup/init_lite/services/loopevent:loopevent", "//base/startup/init_lite/services/loopevent:loopevent",
"//base/startup/init_lite/interfaces/innerkits/plugin:libplugin", "//base/startup/init_lite/services/init/plugin_engine:libinit_plugin_engine",
"//base/startup/init_lite/services/init/plugin_engine:libinit_stub_empty",
"//base/startup/init_lite/device_info:device_info_group", "//base/startup/init_lite/device_info:device_info_group",
"//base/startup/init_lite/interfaces/innerkits/sandbox:libsandbox" "//base/startup/init_lite/interfaces/innerkits/sandbox:libsandbox"
], ],
...@@ -76,6 +77,15 @@ ...@@ -76,6 +77,15 @@
] ]
}, },
"name": "//base/startup/init_lite/interfaces/innerkits:libbeget_proxy" "name": "//base/startup/init_lite/interfaces/innerkits:libbeget_proxy"
},
{
"header": {
"header_base": "//base/startup/init_lite/services/init/plugin_engine/include",
"header_files": [
"init_plugin_engine.h"
]
},
"name": "//base/startup/init_lite/services/init/plugin_engine:libinit_plugin_engine"
} }
], ],
"test": [ "test": [
......
/*
* 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_plugin.h"
static PluginInterface g_pluginInterface = {};
PluginInterface *GetPluginInterface(void)
{
return &g_pluginInterface;
}
...@@ -102,6 +102,7 @@ if (defined(ohos_lite)) { ...@@ -102,6 +102,7 @@ if (defined(ohos_lite)) {
} else { } else {
import("//base/startup/init_lite/begetd.gni") import("//base/startup/init_lite/begetd.gni")
import("//build/ohos.gni") import("//build/ohos.gni")
import("//build/ohos/native_stub/native_stub.gni")
ohos_executable("init") { ohos_executable("init") {
sources = [ sources = [
...@@ -155,6 +156,8 @@ if (defined(ohos_lite)) { ...@@ -155,6 +156,8 @@ if (defined(ohos_lite)) {
"//third_party/cJSON:cjson_static", "//third_party/cJSON:cjson_static",
] ]
deps += [ "//base/startup/init_lite/services/init/plugin_engine:libinit_stub_versionscript" ]
cflags = [] cflags = []
if (use_musl) { if (use_musl) {
...@@ -195,6 +198,11 @@ if (defined(ohos_lite)) { ...@@ -195,6 +198,11 @@ if (defined(ohos_lite)) {
if (defined(product_name) && product_name == "rk3568") { if (defined(product_name) && product_name == "rk3568") {
defines += [ "PRODUCT_RK" ] defines += [ "PRODUCT_RK" ]
} }
version_script = get_label_info(
"//base/startup/init_lite/services/init/plugin_engine:libinit_stub_versionscript",
"target_gen_dir") + "/" + get_label_info(
"//base/startup/init_lite/services/init/plugin_engine:libinit_stub_versionscript",
"name") + stub_version_script_suffix
defines += [ "_GNU_SOURCE" ] defines += [ "_GNU_SOURCE" ]
install_images = [ install_images = [
"system", "system",
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "init_plugin.h"
#include "list.h" #include "list.h"
#ifdef __cplusplus #ifdef __cplusplus
...@@ -72,8 +71,7 @@ void PluginManagerInit(void); ...@@ -72,8 +71,7 @@ void PluginManagerInit(void);
int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd); int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd);
int ParseInitCfg(const char *configFile, void *context); int ParseInitCfg(const char *configFile, void *context);
typedef PluginInterface *(*GetPluginInterfaceFunc)();
int SetPluginInterface(void);
#ifdef __cplusplus #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }
......
...@@ -12,16 +12,27 @@ ...@@ -12,16 +12,27 @@
# limitations under the License. # limitations under the License.
import("//build/ohos.gni") import("//build/ohos.gni")
import("//build/ohos/native_stub/native_stub.gni")
ohos_shared_library("libplugin") { config("libinit_plugin_engine_config") {
sources = [ "init_plugin.c" ] include_dirs =
[ "//base/startup/init_lite/services/init/plugin_engine/include" ]
}
include_dirs = [ ohos_native_stub_library("libinit_plugin_engine") {
"//base/startup/init_lite/interfaces/innerkits/include", output_extension = "so"
"//base/startup/init_lite/services/log", public_configs = [ ":libinit_plugin_engine_config" ]
] stub_description_file = "./stub/libinit.stub.json"
}
deps = [ "//third_party/bounds_checking_function:libsec_static" ] ohos_native_stub_versionscript("libinit_stub_versionscript") {
install_enable = true stub_description_file = "./stub/libinit.stub.json"
}
ohos_native_stub_library("libinit_stub_empty") {
output_extension = "so"
stub_description_file = "./stub/libinit.stub.empty.json"
part_name = "init" part_name = "init"
install_enable = true
symlink_target_name = [ "libinit_plugin_engine.so" ]
} }
...@@ -26,17 +26,18 @@ extern "C" { ...@@ -26,17 +26,18 @@ extern "C" {
#define PLUGIN_CONSTRUCTOR(void) static void _init(void) __attribute__((constructor)); static void _init(void) #define PLUGIN_CONSTRUCTOR(void) static void _init(void) __attribute__((constructor)); static void _init(void)
#define PLUGIN_DESTRUCTOR(void) static void _destroy(void) __attribute__((destructor)); static void _destroy(void) #define PLUGIN_DESTRUCTOR(void) static void _destroy(void) __attribute__((destructor)); static void _destroy(void)
typedef struct { int SystemWriteParam(const char *name, const char *value);
int (*pluginRegister)(const char *name, const char *config, int (*pluginInit)(void), void (*pluginExit)(void));
int (*addCmdExecutor)(const char *cmdName, int SystemReadParam(const char *name, char *value, unsigned int *len);
int (*CmdExecutor)(int id, const char *name, int argc, const char **argv));
void (*removeCmdExecutor)(const char *cmdName, int id); typedef int (*CmdExecutor)(int id, const char *name, int argc, const char **argv);
int (*systemWriteParam)(const char *name, const char *value);
int (*systemReadParam)(const char *name, char *value, unsigned int *len); int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd);
int (*securityLabelSet)(const char *name, const char *label, const char *paraType);
} PluginInterface; void RemoveCmdExecutor(const char *cmdName, int id);
PluginInterface *GetPluginInterface(void); int PluginRegister(const char *name, const char *config, int (*pluginInit)(void), void (*pluginExit)(void));
#ifdef __cplusplus #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }
......
[
{ "name": "SystemWriteParam" },
{ "name": "SystemReadParam" },
{ "name": "AddCmdExecutor" },
{ "name": "RemoveCmdExecutor" },
{ "name": "PluginRegister" }
]
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include "init_utils.h" #include "init_utils.h"
#include "init_log.h" #include "init_log.h"
#include "init_group_manager.h" #include "init_group_manager.h"
#include "init_plugin.h"
#include "init_service_manager.h" #include "init_service_manager.h"
#include "securec.h" #include "securec.h"
...@@ -59,7 +58,7 @@ int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd) ...@@ -59,7 +58,7 @@ int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd)
return cmdExec->id; return cmdExec->id;
} }
static void RemoveCmdExecutor(const char *cmdName, int id) void RemoveCmdExecutor(const char *cmdName, int id)
{ {
INIT_ERROR_CHECK(cmdName != NULL, return, "Invalid input param"); INIT_ERROR_CHECK(cmdName != NULL, return, "Invalid input param");
InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_CMDS, cmdName); InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_CMDS, cmdName);
...@@ -273,7 +272,7 @@ static PluginInfo *GetPluginInfo(const char *name) ...@@ -273,7 +272,7 @@ static PluginInfo *GetPluginInfo(const char *name)
return info; return info;
} }
static int PluginRegister(const char *name, const char *config, int (*pluginInit)(void), void (*pluginExit)(void)) int PluginRegister(const char *name, const char *config, int (*pluginInit)(void), void (*pluginExit)(void))
{ {
INIT_LOGI("PluginRegister %s %p %p", name, pluginInit, pluginExit); INIT_LOGI("PluginRegister %s %p %p", name, pluginInit, pluginExit);
INIT_ERROR_CHECK(name != NULL, return -1, "Invalid plugin name"); INIT_ERROR_CHECK(name != NULL, return -1, "Invalid plugin name");
...@@ -364,47 +363,6 @@ void PluginManagerInit(void) ...@@ -364,47 +363,6 @@ void PluginManagerInit(void)
// "ohos.servicectrl.install" // "ohos.servicectrl.install"
(void)AddCmdExecutor("install", PluginCmdInstall); (void)AddCmdExecutor("install", PluginCmdInstall);
(void)AddCmdExecutor("uninstall", PluginCmdUninstall); (void)AddCmdExecutor("uninstall", PluginCmdUninstall);
// register interface
SetPluginInterface();
// read cfg and start static plugin // read cfg and start static plugin
LoadPluginCfg(); LoadPluginCfg();
} }
int SetPluginInterface(void)
{
static PluginInterface *pluginInterface = NULL;
if (pluginInterface != NULL) {
return 0;
}
char *realPath = GetRealPath("/system/lib/libplugin.z.so");
#ifndef STARTUP_INIT_TEST
void* handle = dlopen(realPath, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
if (handle == NULL) {
INIT_LOGE("Failed to load module %s, err %s", realPath, dlerror());
free(realPath);
return -1;
}
GetPluginInterfaceFunc getPluginInterface = (GetPluginInterfaceFunc)dlsym(handle, "GetPluginInterface");
#else
GetPluginInterfaceFunc getPluginInterface = GetPluginInterface;
#endif
INIT_LOGI("PluginManagerInit getPluginInterface %p ", getPluginInterface);
if (getPluginInterface != NULL) {
pluginInterface = getPluginInterface();
if (pluginInterface != NULL) {
pluginInterface->pluginRegister = PluginRegister;
pluginInterface->addCmdExecutor = AddCmdExecutor;
pluginInterface->removeCmdExecutor = RemoveCmdExecutor;
pluginInterface->systemWriteParam = SystemWriteParam;
pluginInterface->systemReadParam = SystemReadParam;
pluginInterface->securityLabelSet = NULL;
}
INIT_LOGI("PluginManagerInit pluginInterface %p %p %p",
pluginInterface, pluginInterface->pluginRegister, getPluginInterface);
}
free(realPath);
#ifndef STARTUP_INIT_TEST
dlclose(handle);
#endif
return 0;
}
...@@ -27,13 +27,14 @@ ohos_shared_library("libbootchart") { ...@@ -27,13 +27,14 @@ ohos_shared_library("libbootchart") {
] ]
deps = [ deps = [
"//base/startup/init_lite/interfaces/innerkits/plugin:libplugin",
"//base/startup/init_lite/services/log:init_log", "//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/services/utils:libinit_utils", "//base/startup/init_lite/services/utils:libinit_utils",
"//third_party/bounds_checking_function:libsec_static", "//third_party/bounds_checking_function:libsec_static",
"//third_party/cJSON:cjson_static", "//third_party/cJSON:cjson_static",
] ]
external_deps = [ "init:libinit_plugin_engine" ]
part_name = "init" part_name = "init"
module_install_dir = "lib/plugin" module_install_dir = "lib/plugin"
} }
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "init_plugin.h" #include "init_plugin_engine.h"
#include "init_param.h" #include "init_param.h"
#include "init_utils.h" #include "init_utils.h"
#include "plugin_adapter.h" #include "plugin_adapter.h"
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#define NANO_PRE_JIFFY 10000000 #define NANO_PRE_JIFFY 10000000
static BootchartCtrl *g_bootchartCtrl = NULL; static BootchartCtrl *g_bootchartCtrl = NULL;
static PluginInterface *g_pluginInterface = NULL;
static long long GetJiffies(void) static long long GetJiffies(void)
{ {
...@@ -79,9 +78,7 @@ static void BootchartLogHeader(void) ...@@ -79,9 +78,7 @@ static void BootchartLogHeader(void)
char release[PARAM_VALUE_LEN_MAX] = {}; char release[PARAM_VALUE_LEN_MAX] = {};
uint32_t len = sizeof(release); uint32_t len = sizeof(release);
if (g_pluginInterface->systemReadParam != NULL) { (void)SystemReadParam("hw_sc.build.os.releasetype", release, &len);
(void)g_pluginInterface->systemReadParam("hw_sc.build.os.releasetype", release, &len);
}
char *cmdLine = ReadFileToBuffer("/proc/cmdline", g_bootchartCtrl->buffer, g_bootchartCtrl->bufferSize); char *cmdLine = ReadFileToBuffer("/proc/cmdline", g_bootchartCtrl->buffer, g_bootchartCtrl->bufferSize);
PLUGIN_CHECK(cmdLine != NULL, return, "Failed to open file /data/bootchart/header"); PLUGIN_CHECK(cmdLine != NULL, return, "Failed to open file /data/bootchart/header");
...@@ -222,15 +219,9 @@ static void BootchartDestory(void) ...@@ -222,15 +219,9 @@ static void BootchartDestory(void)
static int DoBootchartStart(void) static int DoBootchartStart(void)
{ {
if (g_pluginInterface == NULL) {
PLUGIN_LOGI("Invalid bootchart plugin");
return -1;
}
char enable[4] = {}; // 4 enable size char enable[4] = {}; // 4 enable size
uint32_t size = sizeof(enable); uint32_t size = sizeof(enable);
if (g_pluginInterface->systemReadParam != NULL) { SystemReadParam("init.bootchart.enabled", enable, &size);
g_pluginInterface->systemReadParam("init.bootchart.enabled", enable, &size);
}
if (strcmp(enable, "1") != 0) { if (strcmp(enable, "1") != 0) {
PLUGIN_LOGI("Not bootcharting"); PLUGIN_LOGI("Not bootcharting");
return 0; return 0;
...@@ -298,13 +289,8 @@ static PluginCmd g_bootchartCmds[] = { ...@@ -298,13 +289,8 @@ static PluginCmd g_bootchartCmds[] = {
static int BootchartInit(void) static int BootchartInit(void)
{ {
PLUGIN_LOGI("BootchartInit ");
g_pluginInterface = GetPluginInterface();
PLUGIN_CHECK(g_pluginInterface != NULL && g_pluginInterface->addCmdExecutor != NULL, return -1,
"Invalid install parameter");
for (int i = 0; i < (int)(sizeof(g_bootchartCmds) / sizeof(g_bootchartCmds[0])); i++) { for (int i = 0; i < (int)(sizeof(g_bootchartCmds) / sizeof(g_bootchartCmds[0])); i++) {
g_bootchartCmds[i].index = g_pluginInterface->addCmdExecutor( g_bootchartCmds[i].index = AddCmdExecutor(
g_bootchartCmds[i].name, g_bootchartCmds[i].cmdExecutor); g_bootchartCmds[i].name, g_bootchartCmds[i].cmdExecutor);
PLUGIN_LOGI("BootchartInit %d", g_bootchartCmds[i].index); PLUGIN_LOGI("BootchartInit %d", g_bootchartCmds[i].index);
} }
...@@ -314,20 +300,14 @@ static int BootchartInit(void) ...@@ -314,20 +300,14 @@ static int BootchartInit(void)
static void BootchartExit(void) static void BootchartExit(void)
{ {
PLUGIN_LOGI("BootchartExit %d", g_bootchartCmds[0]); PLUGIN_LOGI("BootchartExit %d", g_bootchartCmds[0]);
PLUGIN_CHECK(g_pluginInterface != NULL && g_pluginInterface->removeCmdExecutor != NULL, return,
"Invalid install parameter");
for (int i = 0; i < (int)(sizeof(g_bootchartCmds) / sizeof(g_bootchartCmds[0])); i++) { for (int i = 0; i < (int)(sizeof(g_bootchartCmds) / sizeof(g_bootchartCmds[0])); i++) {
g_pluginInterface->removeCmdExecutor(g_bootchartCmds[i].name, g_bootchartCmds[i].index); RemoveCmdExecutor(g_bootchartCmds[i].name, g_bootchartCmds[i].index);
} }
} }
PLUGIN_CONSTRUCTOR(void) PLUGIN_CONSTRUCTOR(void)
{ {
g_pluginInterface = GetPluginInterface(); PluginRegister("bootchart", NULL, BootchartInit, BootchartExit);
if (g_pluginInterface != NULL && g_pluginInterface->pluginRegister != NULL) { PLUGIN_LOGI("bootchart pluginInterface %p %p",
g_pluginInterface->pluginRegister("bootchart", NULL, BootchartInit, BootchartExit);
}
PLUGIN_LOGI("bootchart pluginInterface %p %p %p",
g_pluginInterface, g_pluginInterface->pluginRegister, GetPluginInterface,
BootchartInit, BootchartExit); BootchartInit, BootchartExit);
} }
\ No newline at end of file
...@@ -30,11 +30,13 @@ ohos_shared_library("libpluginparamtest") { ...@@ -30,11 +30,13 @@ ohos_shared_library("libpluginparamtest") {
] ]
deps = [ deps = [
"//base/startup/init_lite/interfaces/innerkits/plugin:libplugin",
"//base/startup/init_lite/services/log:agent_log", "//base/startup/init_lite/services/log:agent_log",
"//base/startup/init_lite/services/utils:libinit_tools", "//base/startup/init_lite/services/utils:libinit_tools",
"//third_party/bounds_checking_function:libsec_static", "//third_party/bounds_checking_function:libsec_static",
] ]
external_deps = [ "init:libinit_plugin_engine" ]
part_name = "init" part_name = "init"
module_install_dir = "lib/plugin" module_install_dir = "lib/plugin"
} }
......
文件模式从 100644 更改为 100755
...@@ -18,18 +18,15 @@ ...@@ -18,18 +18,15 @@
#include "plugin_test.h" #include "plugin_test.h"
#include "init_param.h" #include "init_param.h"
#include "init_plugin.h" #include "init_plugin_engine.h"
#define MAX_COUNT 1000 #define MAX_COUNT 1000
#define TEST_CMD_NAME "param_randrom_write" #define TEST_CMD_NAME "param_randrom_write"
static PluginInterface *g_pluginInterface = NULL;
static int g_testCmdIndex = 0; static int g_testCmdIndex = 0;
static int PluginParamCmdWriteParam(int id, const char *name, int argc, const char **argv) static int PluginParamCmdWriteParam(int id, const char *name, int argc, const char **argv)
{ {
PLUGIN_LOGI("PluginParamCmdWriteParam %d %s", id, name); PLUGIN_LOGI("PluginParamCmdWriteParam %d %s", id, name);
PLUGIN_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter"); PLUGIN_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
PLUGIN_CHECK(g_pluginInterface != NULL && g_pluginInterface->systemWriteParam != NULL,
return -1, "Invalid install parameter");
PLUGIN_LOGI("PluginParamCmdWriteParam argc %d %s", argc, argv[0]); PLUGIN_LOGI("PluginParamCmdWriteParam argc %d %s", argc, argv[0]);
int maxCount = MAX_COUNT; int maxCount = MAX_COUNT;
if (argc > 1) { if (argc > 1) {
...@@ -42,7 +39,7 @@ static int PluginParamCmdWriteParam(int id, const char *name, int argc, const ch ...@@ -42,7 +39,7 @@ static int PluginParamCmdWriteParam(int id, const char *name, int argc, const ch
const int wait = READ_DURATION + READ_DURATION; // 100ms const int wait = READ_DURATION + READ_DURATION; // 100ms
int ret = sprintf_s(buffer, sizeof(buffer), "%d", count); int ret = sprintf_s(buffer, sizeof(buffer), "%d", count);
if (ret > 0) { if (ret > 0) {
g_pluginInterface->systemWriteParam(argv[0], buffer); SystemWriteParam(argv[0], buffer);
usleep(wait); usleep(wait);
} }
count++; count++;
...@@ -52,30 +49,21 @@ static int PluginParamCmdWriteParam(int id, const char *name, int argc, const ch ...@@ -52,30 +49,21 @@ static int PluginParamCmdWriteParam(int id, const char *name, int argc, const ch
static int PluginParamTestInit(void) static int PluginParamTestInit(void)
{ {
g_pluginInterface = GetPluginInterface(); g_testCmdIndex = AddCmdExecutor(TEST_CMD_NAME, PluginParamCmdWriteParam);
PLUGIN_CHECK(g_pluginInterface != NULL && g_pluginInterface->addCmdExecutor != NULL,
return -1, "Invalid install parameter");
g_testCmdIndex = g_pluginInterface->addCmdExecutor(TEST_CMD_NAME, PluginParamCmdWriteParam);
PLUGIN_LOGI("PluginParamTestInit %d", g_testCmdIndex); PLUGIN_LOGI("PluginParamTestInit %d", g_testCmdIndex);
return 0; return 0;
} }
static void PluginParamTestExit(void) static void PluginParamTestExit(void)
{ {
PLUGIN_LOGI("PluginParamTestExit %d", g_testCmdIndex); RemoveCmdExecutor(TEST_CMD_NAME, g_testCmdIndex);
PLUGIN_CHECK(g_pluginInterface != NULL && g_pluginInterface->removeCmdExecutor != NULL,
return, "Invalid install parameter");
g_pluginInterface->removeCmdExecutor(TEST_CMD_NAME, g_testCmdIndex);
} }
PLUGIN_CONSTRUCTOR(void) PLUGIN_CONSTRUCTOR(void)
{ {
g_pluginInterface = GetPluginInterface(); PluginRegister("pluginparamtest",
if (g_pluginInterface != NULL && g_pluginInterface->pluginRegister != NULL) {
g_pluginInterface->pluginRegister("pluginparamtest",
"/system/etc/plugin/plugin_param_test.cfg", "/system/etc/plugin/plugin_param_test.cfg",
PluginParamTestInit, PluginParamTestExit); PluginParamTestInit, PluginParamTestExit);
} PLUGIN_LOGI("PLUGIN_CONSTRUCTOR pluginInterface %p",
PLUGIN_LOGI("PLUGIN_CONSTRUCTOR pluginInterface %p %p %p", g_pluginInterface->pluginRegister);
g_pluginInterface, g_pluginInterface->pluginRegister, GetPluginInterface);
} }
...@@ -36,7 +36,6 @@ ohos_unittest("init_ut") { ...@@ -36,7 +36,6 @@ ohos_unittest("init_ut") {
"//base/startup/init_lite/interfaces/innerkits/file/init_file.c", "//base/startup/init_lite/interfaces/innerkits/file/init_file.c",
"//base/startup/init_lite/interfaces/innerkits/fs_manager/fstab.c", "//base/startup/init_lite/interfaces/innerkits/fs_manager/fstab.c",
"//base/startup/init_lite/interfaces/innerkits/fs_manager/fstab_mount.c", "//base/startup/init_lite/interfaces/innerkits/fs_manager/fstab_mount.c",
"//base/startup/init_lite/interfaces/innerkits/plugin/init_plugin.c",
"//base/startup/init_lite/interfaces/innerkits/reboot/init_reboot_innerkits.c", "//base/startup/init_lite/interfaces/innerkits/reboot/init_reboot_innerkits.c",
"//base/startup/init_lite/interfaces/innerkits/sandbox/sandbox.c", "//base/startup/init_lite/interfaces/innerkits/sandbox/sandbox.c",
"//base/startup/init_lite/interfaces/innerkits/sandbox/sandbox_namespace.c", "//base/startup/init_lite/interfaces/innerkits/sandbox/sandbox_namespace.c",
...@@ -197,7 +196,10 @@ ohos_unittest("init_ut") { ...@@ -197,7 +196,10 @@ ohos_unittest("init_ut") {
] ]
defines += [ "_GNU_SOURCE" ] defines += [ "_GNU_SOURCE" ]
external_deps = [ "hiviewdfx_hilog_native:libhilog" ] external_deps = [
"hiviewdfx_hilog_native:libhilog",
"init:libinit_plugin_engine",
]
if (param_feature_watcher) { if (param_feature_watcher) {
external_deps += [ external_deps += [
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include "init_group_manager.h" #include "init_group_manager.h"
#include "init_hashmap.h" #include "init_hashmap.h"
#include "init_param.h" #include "init_param.h"
#include "init_plugin.h" #include "init_plugin_engine.h"
#include "init_plugin_manager.h" #include "init_plugin_manager.h"
#include "init_unittest.h" #include "init_unittest.h"
#include "init_utils.h" #include "init_utils.h"
...@@ -45,19 +45,16 @@ int TestCmdExecutor(int id, const char *name, int argc, const char **argv) ...@@ -45,19 +45,16 @@ int TestCmdExecutor(int id, const char *name, int argc, const char **argv)
HWTEST_F(PluginUnitTest, PluginAddCmd, TestSize.Level1) HWTEST_F(PluginUnitTest, PluginAddCmd, TestSize.Level1)
{ {
InitServiceSpace(); InitServiceSpace();
SetPluginInterface();
PluginManagerInit(); PluginManagerInit();
PluginInterface *pluginInterface = GetPluginInterface();
ASSERT_NE(pluginInterface, nullptr);
const char *testName = "testCmd1"; const char *testName = "testCmd1";
const char *cmdContent = "testCmd1 test1 test2 test3"; const char *cmdContent = "testCmd1 test1 test2 test3";
int cmdExecId1 = pluginInterface->addCmdExecutor(testName, TestCmdExecutor); int cmdExecId1 = AddCmdExecutor(testName, TestCmdExecutor);
ASSERT_NE(cmdExecId1 > 0, 0); ASSERT_NE(cmdExecId1 > 0, 0);
int cmdExecId2 = pluginInterface->addCmdExecutor("testCmd2", TestCmdExecutor); int cmdExecId2 = AddCmdExecutor("testCmd2", TestCmdExecutor);
ASSERT_NE(cmdExecId2 > 0, 0); ASSERT_NE(cmdExecId2 > 0, 0);
cmdExecId2 = pluginInterface->addCmdExecutor("testCmd3", TestCmdExecutor); cmdExecId2 = AddCmdExecutor("testCmd3", TestCmdExecutor);
ASSERT_NE(cmdExecId2 > 0, 0); ASSERT_NE(cmdExecId2 > 0, 0);
int cmdExecId4 = pluginInterface->addCmdExecutor("testCmd4", TestCmdExecutor); int cmdExecId4 = AddCmdExecutor("testCmd4", TestCmdExecutor);
ASSERT_NE(cmdExecId4 > 0, 0); ASSERT_NE(cmdExecId4 > 0, 0);
int cmdIndex = 0; int cmdIndex = 0;
...@@ -74,31 +71,24 @@ HWTEST_F(PluginUnitTest, PluginAddCmd, TestSize.Level1) ...@@ -74,31 +71,24 @@ HWTEST_F(PluginUnitTest, PluginAddCmd, TestSize.Level1)
ASSERT_EQ(cmdExecId1, g_cmdExecId); ASSERT_EQ(cmdExecId1, g_cmdExecId);
// del // del
pluginInterface->removeCmdExecutor("testCmd4", cmdExecId4); RemoveCmdExecutor("testCmd4", cmdExecId4);
} }
static int PluginTestInit(void) static int PluginTestInit(void)
{ {
PluginInterface *pluginInterface = GetPluginInterface(); g_cmdExecId = AddCmdExecutor("testCmd4", TestCmdExecutor);
if (pluginInterface != nullptr && pluginInterface->addCmdExecutor != nullptr) {
g_cmdExecId = pluginInterface->addCmdExecutor("testCmd4", TestCmdExecutor);
}
return 0; return 0;
} }
static void PluginTestExit(void) static void PluginTestExit(void)
{ {
PluginInterface *pluginInterface = GetPluginInterface(); RemoveCmdExecutor("testCmd4", g_cmdExecId);
ASSERT_NE(pluginInterface, nullptr);
pluginInterface->removeCmdExecutor("testCmd4", g_cmdExecId);
} }
HWTEST_F(PluginUnitTest, PluginInstallTest, TestSize.Level1) HWTEST_F(PluginUnitTest, PluginInstallTest, TestSize.Level1)
{ {
PluginInterface *pluginInterface = GetPluginInterface();
ASSERT_NE(pluginInterface, nullptr);
const char *moduleName = "testplugin"; const char *moduleName = "testplugin";
pluginInterface->pluginRegister(moduleName, PluginRegister(moduleName,
"/home/axw/init_ut/etc/init/plugin_param_test.cfg", "/home/axw/init_ut/etc/init/plugin_param_test.cfg",
PluginTestInit, PluginTestExit); PluginTestInit, PluginTestExit);
PluginInstall(moduleName, NULL); PluginInstall(moduleName, NULL);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册