未验证 提交 814ebf05 编写于 作者: O openharmony_ci 提交者: Gitee

!334 添加bootchart功能

Merge pull request !334 from 熊磊/bootchart
......@@ -41,6 +41,7 @@
"//base/startup/init_lite/watchdog:watchdog",
"//base/startup/init_lite/services/begetctl:begetctl",
"//base/startup/init_lite/services/begetctl:paramshell",
"//base/startup/init_lite/services/plugin:plugin",
"//base/startup/init_lite/interfaces/innerkits:libbegetutil",
"//base/startup/init_lite/interfaces/innerkits:libservice_watcher",
"//base/startup/init_lite/interfaces/innerkits/file:libfile",
......
......@@ -297,7 +297,7 @@ int MountOneItem(FstabItem *item)
mountFlags = GetMountFlags(item->mountOptions, fsSpecificData, sizeof(fsSpecificData));
if (!IsSupportedFilesystem(item->fsType)) {
BEGET_LOGE("Unsupported file system \" %s \"", item->fsType);
return -1;
return 0;
}
if (FM_MANAGER_WAIT_ENABLED(item->fsManagerFlags)) {
WaitForFile(item->deviceName, WAIT_MAX_SECOND);
......
......@@ -112,7 +112,7 @@ void OpenLogDevice(void);
#endif
#endif
#define BEGET_LOG_FILE "beget.log"
#define BEGET_LOG_FILE "begetctrl.log"
#define BEGET_LABEL "BEGET"
#define BEGET_LOGI(fmt, ...) STARTUP_LOGI(BEGET_LOG_FILE, BEGET_LABEL, fmt, ##__VA_ARGS__)
#define BEGET_LOGE(fmt, ...) STARTUP_LOGE(BEGET_LOG_FILE, BEGET_LABEL, fmt, ##__VA_ARGS__)
......
......@@ -29,10 +29,11 @@ extern "C" {
typedef struct {
int (*pluginRegister)(const char *name, const char *config, int (*pluginInit)(void), void (*pluginExit)(void));
int (*addCmdExecutor)(const char *cmdName,
void (*CmdExecutor)(int id, const char *name, int argc, const char **argv));
int (*CmdExecutor)(int id, const char *name, int argc, const char **argv));
void (*removeCmdExecutor)(const char *cmdName, int id);
int (*systemWriteParam)(const char *name, const char *value);
int (*systemReadParam)(const char *name, char *value, unsigned int *len);
int (*securityLabelSet)(const char *name, const char *label, const char *paraType);
} PluginInterface;
PluginInterface *GetPluginInterface(void);
......
......@@ -29,8 +29,8 @@ extern "C" {
typedef enum {
SERVICE_IDLE = 0, // service add
SERVICE_STARTING, // service start
SERVICE_STARTED,
SERVICE_READY,
SERVICE_STARTED, // service ok
SERVICE_READY, // service ok
SERVICE_STOPPING,
SERVICE_STOPPED,
SERVICE_ERROR,
......
......@@ -31,7 +31,7 @@ static void ServiceStateChange(const char *key, const char *value, void *context
const InitArgInfo *statusMap = GetServieStatusMap(&size);
ServiceStatus status = (ServiceStatus)GetMapValue(value, statusMap, size, SERVICE_IDLE);
if (strlen(key) > strlen(STARTUP_SERVICE_CTL)) {
callback(key + strlen(STARTUP_SERVICE_CTL), status);
callback(key + strlen(STARTUP_SERVICE_CTL) + 1, status);
} else {
BEGET_LOGE("Invalid service name %s %s", key, value);
}
......
......@@ -296,6 +296,11 @@ if (defined(ohos_lite)) {
part_name = "init"
}
ohos_prebuilt_etc("plugin_modules") {
source = "//base/startup/init_lite/services/etc/plugin_modules.cfg"
part_name = "init"
}
group("init_etc") {
deps = [
":boot.group",
......@@ -307,6 +312,7 @@ if (defined(ohos_lite)) {
":ohos.para",
":ohos.para.dac",
":passwd",
":plugin_modules",
":syscap.json",
":syscap.para",
":systemcapability.json",
......
......@@ -15,6 +15,7 @@ import("//build/ohos.gni")
ohos_executable("begetctl") {
sources = [
"bootchart_cmd.c",
"init_cmd_reboot.c",
"main.c",
"misc_daemon.cpp",
......@@ -30,6 +31,7 @@ ohos_executable("begetctl") {
}
include_dirs = [
".",
"shell",
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/log",
......@@ -37,6 +39,7 @@ ohos_executable("begetctl") {
"//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include",
"//base/startup/init_lite/services/include/param/",
"//base/startup/init_lite/services/param/include",
"//base/startup/init_lite/services/loopevent/include",
"//third_party/bounds_checking_function/include",
]
deps = [
......@@ -46,6 +49,19 @@ ohos_executable("begetctl") {
"//third_party/bounds_checking_function:libsec_static",
]
if (param_test) {
sources += [ "//base/startup/init_lite/test/plugintest/plugin_param_cmd.c" ]
deps += [
"//base/startup/init_lite/interfaces/innerkits:libservice_watcher",
"//base/startup/init_lite/services/loopevent:loopevent",
"//base/startup/init_lite/services/param/watcher:param_watcheragent",
]
defines += [
"OHOS_SERVICE_DUMP",
"INIT_TEST",
]
}
symlink_target_name = [
"misc_daemon",
"reboot",
......@@ -81,6 +97,7 @@ ohos_executable("paramshell") {
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include",
"//base/startup/init_lite/services/include/param/",
"//base/startup/init_lite/services/loopevent/include",
"//base/startup/init_lite/services/param/include",
"//third_party/bounds_checking_function/include",
]
......@@ -93,8 +110,13 @@ ohos_executable("paramshell") {
if (param_test) {
sources += [ "//base/startup/init_lite/test/plugintest/plugin_param_cmd.c" ]
deps += [
"//base/startup/init_lite/interfaces/innerkits:libservice_watcher",
"//base/startup/init_lite/services/loopevent:loopevent",
"//base/startup/init_lite/services/param/watcher:param_watcheragent",
"//base/startup/init_lite/test/plugintest:pluginparamtest",
]
defines += [
"OHOS_SERVICE_DUMP",
"INIT_TEST",
]
}
......
......@@ -39,6 +39,7 @@ void demoExit(void);
int SetParamShellPrompt(BShellHandle shell, const char *param);
int32_t BShellParamCmdRegister(BShellHandle shell, int execMode);
int32_t BShellCmdRegister(BShellHandle shell, int execMode);
#ifdef __cplusplus
#if __cplusplus
......
/*
* Copyright (c) 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 <errno.h>
#include <stdio.h>
#include <string.h>
#include "begetctl.h"
#include "sys_param.h"
static int bootchartCmdEnable(BShellHandle shell, int argc, char **argv)
{
if (argc < 1) {
char *helpArgs[] = {"bootchart", NULL};
BShellCmdHelp(shell, 1, helpArgs);
}
SystemSetParameter("init.bootchart.enabled", "1");
return 0;
}
static int bootchartCmdDisable(BShellHandle shell, int argc, char **argv)
{
if (argc < 1) {
char *helpArgs[] = {"bootchart", NULL};
BShellCmdHelp(shell, 1, helpArgs);
}
SystemSetParameter("init.bootchart.enabled", "0");
return 0;
}
static int bootchartCmdStart(BShellHandle shell, int argc, char **argv)
{
if (argc < 1) {
char *helpArgs[] = {"bootchart", NULL};
BShellCmdHelp(shell, 1, helpArgs);
}
char enable[4] = {}; // 4 enable size
uint32_t size = sizeof(enable);
int ret = SystemGetParameter("init.bootchart.enabled", enable, &size);
if (ret != 0 || strcmp(enable, "1") != 0) {
BShellEnvOutput(shell, "Not bootcharting\r\n");
return 0;
}
SystemSetParameter("ohos.servicectrl.bootchart", "start");
return 0;
}
static int bootchartCmdStop(BShellHandle shell, int argc, char **argv)
{
if (argc < 1) {
char *helpArgs[] = {"bootchart", NULL};
BShellCmdHelp(shell, 1, helpArgs);
}
SystemSetParameter("ohos.servicectrl.bootchart", "stop");
return 0;
}
MODULE_CONSTRUCTOR(void)
{
CmdInfo infos[] = {
{"bootchart", bootchartCmdEnable, "bootchart enable", "bootchart enable", "bootchart enable"},
{"bootchart", bootchartCmdDisable, "bootchart disable", "bootchart disable", "bootchart disable"},
{"bootchart", bootchartCmdStart, "bootchart start", "bootchart start", "bootchart start"},
{"bootchart", bootchartCmdStop, "bootchart stop", "bootchart stop", "bootchart stop"},
};
for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
BShellEnvRegitsterCmd(GetShellHandle(), &infos[i]);
}
}
......@@ -30,6 +30,9 @@ static int main_cmd(BShellHandle shell, int argc, char* argv[])
if (argc == REBOOT_CMD_NUMBER && strcmp(argv[1], "shutdown") != 0 &&
strcmp(argv[1], "updater") != 0 &&
strcmp(argv[1], "flashd") != 0 &&
#ifdef INIT_TEST
strcmp(argv[1], "charing") != 0 &&
#endif
#ifdef PRODUCT_RK
strcmp(argv[1], "loader") != 0 &&
#endif
......@@ -64,6 +67,9 @@ MODULE_CONSTRUCTOR(void)
{"reboot", main_cmd, "reboot and boot into updater", "reboot updater[:options]", ""},
{"reboot", main_cmd, "reboot and boot into flashd", "reboot flashd", ""},
{"reboot", main_cmd, "reboot and boot into flashd", "reboot flashd[:options]", ""},
#ifdef INIT_TEST
{"reboot", main_cmd, "reboot and boot into charing", "reboot charing", ""},
#endif
#ifdef PRODUCT_RK
{"reboot", main_cmd, "reboot loader", "reboot loader", ""}
#endif
......
......@@ -66,6 +66,9 @@ int main(int argc, char *argv[])
}
SetInitLogLevel(0);
BShellParamCmdRegister(g_handle, 0);
#ifdef INIT_TEST
BShellCmdRegister(g_handle, 0);
#endif
BShellEnvDirectExecute(g_handle, number, args);
demoExit();
return 0;
......
......@@ -387,6 +387,8 @@ static int32_t BShellParamCmdShell(BShellHandle shell, int32_t argc, char *argv[
g_isSetTerminal = 1;
pid_t pid = fork();
if (pid == 0) {
setuid(2000); // 2000 shell group
setgid(2000); // 2000 shell group
if (argc >= 2) { // 2 min argc
char *args[] = {SHELL_NAME, argv[1], NULL};
ret = execv(CMD_PATH, args);
......
......@@ -345,8 +345,6 @@ static void BShellEnvProcessInput(BShellHandle handle, char data)
{
BSH_CHECK(handle != NULL, return, "Invalid shell env");
BShellEnv *shell = (BShellEnv *)handle;
BSH_LOGV("BShellEnvProcessInput %s shellState %d, input 0x%x",
shell->prompt, shell->shellState, data);
if (shell->shellState == BSH_IN_NORMAL) {
BShellKey *key = BShellEnvGetKey(handle, data);
if (key != NULL) {
......@@ -547,7 +545,6 @@ int32_t BShellEnvRegitsterCmd(BShellHandle handle, CmdInfo *cmdInfo)
}
cmd->next = shell->command;
shell->command = cmd;
BSH_LOGV("BShellEnvRegitsterCmd cmd %s multikey %s", cmd->name, cmd->multikey);
return 0;
}
......
......@@ -68,8 +68,6 @@ int main(int argc, char *args[])
if (tcgetattr(0, &tio)) {
return -1;
}
setuid(2000); // 2000 shell group
setgid(2000); // 2000 shell group
tio.c_lflag &= ~(ECHO | ICANON | ISIG);
tio.c_cc[VTIME] = 0;
tio.c_cc[VMIN] = 1;
......@@ -92,6 +90,9 @@ int main(int argc, char *args[])
}
}
BShellParamCmdRegister(g_handle, 1);
#ifdef INIT_TEST
BShellCmdRegister(g_handle, 1);
#endif
BShellEnvStart(g_handle);
BShellEnvLoop(g_handle);
} while (0);
......
......@@ -22,7 +22,7 @@
#include "init_log.h"
#include "securec.h"
#define BSH_LOG_FILE "paramshell.log"
#define BSH_LOG_FILE "begetctrl.log"
#define BSH_LABEL "SHELL"
#define BSH_LOGI(fmt, ...) STARTUP_LOGI(BSH_LOG_FILE, BSH_LABEL, fmt, ##__VA_ARGS__)
#define BSH_LOGE(fmt, ...) STARTUP_LOGE(BSH_LOG_FILE, BSH_LABEL, fmt, ##__VA_ARGS__)
......
{
"jobs": [],
"services": [],
"groups": []
"jobs": [
"param:sys.usb.config=hdc && param:sys.usb.configfs=1",
"param:sys.usb.ffs.ready=1 && param:sys.usb.config=hdc && param:sys.usb.configfs=1",
"boot && param:persist.sys.usb.config=*",
"param:sys.usb.config=none && param:sys.usb.configfs=0",
"boot && param:const.debuggable=1",
"param:sys.usb.config=none && param:sys.usb.configfs=1",
"load_persist_props_action"
],
"services": [
"ueventd",
"console",
"watchdog_service",
"hdf_devmgr",
"samgr",
"param_watcher"
],
"groups": [
]
}
\ No newline at end of file
{
"modules" : [
{
"name": "bootchart",
"start-mode" : "static",
"lib-name" : "libbootchart.z.so"
}
]
}
\ No newline at end of file
......@@ -87,6 +87,7 @@ HashMapHandle GetGroupHashMap(int type);
#ifdef STARTUP_INIT_TEST
InitWorkspace *GetInitWorkspace(void);
#endif
int GetBootModeFromMisc(void);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -27,6 +27,7 @@ extern "C" {
#endif
#define DEFAULT_PLUGIN_PATH "/system/lib/plugin"
#define DEFAULT_PLUGIN_CFG "/system/etc/plugin_modules.cfg"
typedef enum {
PLUGIN_STATE_IDLE,
PLUGIN_STATE_INIT,
......@@ -36,9 +37,11 @@ typedef enum {
typedef struct PluginInfo_ {
int state;
int startMode;
int (*pluginInit)();
void (*pluginExit)();
char *name;
char *libName;
} PluginInfo;
typedef struct {
......@@ -47,7 +50,7 @@ typedef struct {
char *name;
} PluginCmd;
typedef void (*CmdExecutor)(int id, const char *name, int argc, const char **argv);
typedef int (*CmdExecutor)(int id, const char *name, int argc, const char **argv);
typedef struct {
ListNode node;
int id;
......@@ -56,10 +59,11 @@ typedef struct {
void PluginExecCmdByName(const char *name, const char *cmdContent);
void PluginExecCmdByCmdIndex(int index, const char *cmdContent);
int PluginExecCmd(const char *name, int argc, const char **argv);
const char *PluginGetCmdIndex(const char *cmdStr, int *index);
int PluginUninstall(const char *name);
int PluginInstall(const char *name);
int PluginInstall(const char *name, const char *libName);
void PluginManagerInit(void);
int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd);
......
......@@ -239,6 +239,9 @@ static int BindCpuCore(Service *service)
if (service == NULL) {
return SERVICE_SUCCESS;
}
if (CPU_COUNT(&service->cpuSet) == 0) {
return SERVICE_SUCCESS;
}
#ifndef __LITEOS__
int pid = getpid();
if (sched_setaffinity(pid, sizeof(service->cpuSet), &service->cpuSet) != 0) {
......
......@@ -187,7 +187,15 @@ void InitServiceSpace(void)
g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr));
if (ret != 0) {
INIT_LOGE("%s", "Failed to get boot group");
#ifdef INIT_TEST
if (GetBootModeFromMisc() == GROUP_CHARING) {
strcpy_s(g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr), "device.charing.group");
} else {
strcpy_s(g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr), BOOT_GROUP_DEFAULT);
}
#else
strcpy_s(g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr), BOOT_GROUP_DEFAULT);
#endif
}
free(data);
}
......
......@@ -28,3 +28,8 @@ void ExecReboot(const char *value)
}
return;
}
int GetBootModeFromMisc(void)
{
return 0;
}
......@@ -34,7 +34,6 @@ int main(int argc, char * const argv[])
} else {
LogInit();
}
LogInit();
SystemInit();
SystemExecuteRcs();
SystemConfig();
......
......@@ -254,12 +254,13 @@ static void BootStateChange(const char *content)
}
#if defined(OHOS_SERVICE_DUMP)
static void SystemDump(int id, const char *name, int argc, const char **argv)
static int SystemDump(int id, const char *name, int argc, const char **argv)
{
INIT_ERROR_CHECK(argv != NULL && argc >= 1, return, "Invalid install parameter");
INIT_ERROR_CHECK(argv != NULL && argc >= 1, return 0, "Invalid install parameter");
INIT_LOGI("Dump system info %s", argv[0]);
DumpAllServices();
DumpParametersAndTriggers();
return 0;
}
#endif
......
......@@ -16,12 +16,14 @@
#include <dlfcn.h>
#include "cJSON.h"
#include "init_param.h"
#include "init_utils.h"
#include "init_log.h"
#include "securec.h"
#include "init_group_manager.h"
#include "init_plugin.h"
#include "init_service_manager.h"
#include "securec.h"
#define MAX_CMD_ARGC 10
static int g_cmdExecutorId = 0;
......@@ -115,6 +117,25 @@ void PluginExecCmdByName(const char *name, const char *cmdContent)
PluginExecCmd_(cmd, cmdContent);
}
int PluginExecCmd(const char *name, int argc, const char **argv)
{
INIT_ERROR_CHECK(name != NULL, return -1, "Invalid cmd ");
InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_CMDS, name);
if (groupNode == NULL || groupNode->data.cmd == NULL) {
return -1;
}
PluginCmd *cmd = groupNode->data.cmd;
INIT_LOGI("PluginExecCmd index %s", cmd->name);
int ret = 0;
ListNode *node = cmd->cmdExecutor.next;
while (node != &cmd->cmdExecutor) {
PluginCmdExecutor *cmdExec = ListEntry(node, PluginCmdExecutor, node);
ret = cmdExec->execCmd(cmdExec->id, cmd->name, argc, argv);
node = node->next;
}
return ret;
}
static int CompareCmdId(const HashNode *node, const void *key)
{
InitGroupNode *groupNode = HASHMAP_ENTRY(node, InitGroupNode, hashNode);
......@@ -171,13 +192,18 @@ const char *PluginGetCmdIndex(const char *cmdStr, int *index)
return cmd->name;
}
static int LoadModule(const char *name)
static int LoadModule(const char *name, const char *libName)
{
char path[128] = {0}; // 128 path for plugin
int ret = sprintf_s(path, sizeof(path), "%s/lib%s.z.so", DEFAULT_PLUGIN_PATH, name);
int ret = 0;
if (libName == NULL) {
ret = sprintf_s(path, sizeof(path), "%s/lib%s.z.so", DEFAULT_PLUGIN_PATH, name);
} else {
ret = sprintf_s(path, sizeof(path), "%s/%s", DEFAULT_PLUGIN_PATH, libName);
}
INIT_ERROR_CHECK(ret > 0, return -1, "Failed to sprintf path %s", name);
char *realPath = GetRealPath(path);
void* handle = dlopen(realPath, RTLD_LAZY);
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);
......@@ -188,11 +214,10 @@ static int LoadModule(const char *name)
return 0;
}
int PluginInstall(const char *name)
int PluginInstall(const char *name, const char *libName)
{
SetPluginInterface();
// load so, and init module
int ret = LoadModule(name);
int ret = LoadModule(name, libName);
INIT_ERROR_CHECK(ret == 0, return -1, "pluginInit is null %s", name);
PluginInfo *info = NULL;
......@@ -200,17 +225,15 @@ int PluginInstall(const char *name)
if (groupNode != NULL && groupNode->data.pluginInfo != NULL) {
info = groupNode->data.pluginInfo;
}
if (info == NULL) {
return -1;
}
INIT_ERROR_CHECK(info != NULL, return -1, "Failed to pluginInit %s", name);
INIT_LOGI("PluginInstall %s %d", name, info->state);
if (info->state == PLUGIN_STATE_INIT) {
INIT_ERROR_CHECK(info->pluginInit != NULL, return -1, "pluginInit is null %s", name);
ret = info->pluginInit();
INIT_ERROR_CHECK(ret == 0, return -1, "Failed to pluginInit %s", name);
info->state = PLUGIN_STATE_RUNNING;
return 0;
}
return -1;
return 0;
}
int PluginUninstall(const char *name)
......@@ -219,36 +242,50 @@ int PluginUninstall(const char *name)
INIT_ERROR_CHECK(groupNode != NULL && groupNode->data.pluginInfo != NULL,
return 0, "Can not find plugin %s", name);
PluginInfo *info = groupNode->data.pluginInfo;
INIT_ERROR_CHECK(info != NULL, return -1, "Failed to pluginInit %s", name);
INIT_LOGI("PluginUninstall %s %d %p", name, info->state, info->pluginInit);
if (info->state == PLUGIN_STATE_RUNNING) {
INIT_ERROR_CHECK(info->pluginInit != NULL, return -1, "pluginExit is null %s", name);
INIT_ERROR_CHECK(info->pluginExit != NULL, return -1, "pluginExit is null %s", name);
info->pluginExit();
info->state = PLUGIN_STATE_INIT;
}
return 0;
}
static int PluginRegister(const char *name, const char *config, int (*pluginInit)(void), void (*pluginExit)(void))
static PluginInfo *GetPluginInfo(const char *name)
{
INIT_LOGI("PluginRegister %s", name);
INIT_ERROR_CHECK(name != NULL, return -1, "Invalid plugin name");
INIT_ERROR_CHECK(pluginInit != NULL && pluginExit != NULL,
return -1, "Invalid plugin constructor %s", name);
InitServiceSpace();
InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_PLUGINS, name);
if (groupNode == NULL) {
groupNode = AddGroupNode(NODE_TYPE_PLUGINS, name);
INIT_ERROR_CHECK(groupNode != NULL, return -1, "Failed to create group node");
INIT_ERROR_CHECK(groupNode != NULL, return NULL, "Failed to create group node");
}
PluginInfo *info = groupNode->data.pluginInfo;
if (info == NULL) {
info = (PluginInfo *)calloc(1, sizeof(PluginInfo));
INIT_ERROR_CHECK(info != NULL, return -1, "Failed to create module");
INIT_ERROR_CHECK(info != NULL, return NULL, "Failed to create module");
groupNode->data.pluginInfo = info;
info->name = groupNode->name;
info->state = PLUGIN_STATE_INIT;
info->pluginInit = pluginInit;
info->pluginExit = pluginExit;
info->state = 0;
info->startMode = 0;
info->libName = NULL;
}
return info;
}
static int PluginRegister(const char *name, const char *config, int (*pluginInit)(void), void (*pluginExit)(void))
{
INIT_LOGI("PluginRegister %s %p %p", name, pluginInit, pluginExit);
INIT_ERROR_CHECK(name != NULL, return -1, "Invalid plugin name");
INIT_ERROR_CHECK(pluginInit != NULL && pluginExit != NULL,
return -1, "Invalid plugin constructor %s", name);
InitServiceSpace();
PluginInfo *info = GetPluginInfo(name);
INIT_ERROR_CHECK(info != NULL, return -1, "Failed to create group node");
info->state = PLUGIN_STATE_INIT;
info->pluginInit = pluginInit;
info->pluginExit = pluginExit;
// load config
if (config != NULL) {
ParseInitCfg(config, NULL);
......@@ -256,18 +293,70 @@ static int PluginRegister(const char *name, const char *config, int (*pluginInit
return 0;
}
static void PluginCmdInstall(int id, const char *name, int argc, const char **argv)
static int PluginCmdInstall(int id, const char *name, int argc, const char **argv)
{
INIT_ERROR_CHECK(argv != NULL && argc >= 1, return, "Invalid install parameter");
int ret = PluginInstall(argv[0]);
INIT_ERROR_CHECK(ret == 0, return, "Install plugin %s fail", argv[0]);
INIT_ERROR_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
PluginInfo *info = GetPluginInfo(argv[0]);
int ret = 0;
if (info == NULL) {
ret = PluginInstall(argv[0], NULL);
} else {
ret = PluginInstall(argv[0], info->libName);
}
INIT_ERROR_CHECK(ret == 0, return ret, "Install plugin %s fail", argv[0]);
return 0;
}
static void PluginCmdUninstall(int id, const char *name, int argc, const char **argv)
static int PluginCmdUninstall(int id, const char *name, int argc, const char **argv)
{
INIT_ERROR_CHECK(argv != NULL && argc >= 1, return, "Invalid install parameter");
INIT_ERROR_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
int ret = PluginUninstall(argv[0]);
INIT_ERROR_CHECK(ret == 0, return, "Uninstall plugin %s fail", argv[0]);
INIT_ERROR_CHECK(ret == 0, return ret, "Uninstall plugin %s fail", argv[0]);
return 0;
}
static int LoadPluginCfg(void)
{
char *fileBuf = ReadFileToBuf(DEFAULT_PLUGIN_CFG);
INIT_ERROR_CHECK(fileBuf != NULL, return -1, "Failed to read file content %s", DEFAULT_PLUGIN_CFG);
cJSON *root = cJSON_Parse(fileBuf);
INIT_ERROR_CHECK(root != NULL, free(fileBuf);
return -1, "Failed to parse json file %s", DEFAULT_PLUGIN_CFG);
int itemNumber = 0;
cJSON *json = GetArrayItem(root, &itemNumber, "modules");
if (json == NULL) {
free(fileBuf);
return 0;
}
for (int i = 0; i < itemNumber; ++i) {
cJSON *item = cJSON_GetArrayItem(json, i);
char *moduleName = cJSON_GetStringValue(cJSON_GetObjectItem(item, "name"));
if (moduleName == NULL) {
INIT_LOGE("Failed to get plugin info");
continue;
}
PluginInfo *info = GetPluginInfo(moduleName);
if (info == NULL) {
INIT_LOGE("Failed to get plugin for module %s", moduleName);
continue;
}
info->startMode = 0;
char *mode = cJSON_GetStringValue(cJSON_GetObjectItem(item, "start-mode"));
if (mode == NULL || (strcmp(mode, "static") != 0)) {
info->startMode = 1;
}
char *libName = cJSON_GetStringValue(cJSON_GetObjectItem(item, "lib-name"));
if (libName != NULL) {
info->libName = strdup(libName); // do not care error
}
INIT_LOGI("LoadPluginCfg module %s %d libName %s", moduleName, info->startMode, info->libName);
if (info->startMode == 0) {
PluginInstall(moduleName, info->libName);
}
}
free(fileBuf);
return 0;
}
void PluginManagerInit(void)
......@@ -275,6 +364,10 @@ void PluginManagerInit(void)
// "ohos.servicectrl.install"
(void)AddCmdExecutor("install", PluginCmdInstall);
(void)AddCmdExecutor("uninstall", PluginCmdUninstall);
// register interface
SetPluginInterface();
// read cfg and start static plugin
LoadPluginCfg();
}
int SetPluginInterface(void)
......@@ -285,7 +378,7 @@ int SetPluginInterface(void)
}
char *realPath = GetRealPath("/system/lib/libplugin.z.so");
#ifndef STARTUP_INIT_TEST
void* handle = dlopen(realPath, RTLD_LAZY);
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);
......@@ -303,6 +396,8 @@ int SetPluginInterface(void)
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);
......
......@@ -21,6 +21,7 @@
#include "fs_manager/fs_manager.h"
#include "init_log.h"
#include "init_jobs_internal.h"
#include "init_group_manager.h"
#include "init_service.h"
#include "init_service_manager.h"
#include "init_utils.h"
......@@ -64,24 +65,41 @@ static int RBMiscWriteUpdaterMessage(const char *path, const struct RBMiscUpdate
static int RBMiscReadUpdaterMessage(const char *path, struct RBMiscUpdateMessage *boot)
{
char *realPath = GetRealPath(path);
INIT_CHECK_RETURN_VALUE(realPath != NULL, -1);
int ret = 0;
FILE *fp = fopen(realPath, "rb");
free(realPath);
realPath = NULL;
FILE *fp = NULL;
char *realPath = GetRealPath(path);
if (realPath != NULL) {
fp = fopen(realPath, "rb");
free(realPath);
realPath = NULL;
} else {
fp = fopen(path, "rb");
}
if (fp != NULL) {
size_t readLen = fread(boot, 1, sizeof(struct RBMiscUpdateMessage), fp);
(void)fclose(fp);
INIT_ERROR_CHECK(readLen > 0, ret = -1, "Failed to read misc for reboot");
} else {
ret = -1;
INIT_LOGE("Failed to open %s", path);
INIT_LOGE("Failed to open %s errno %d", path, errno);
}
return ret;
}
int GetBootModeFromMisc(void)
{
char miscFile[PATH_MAX] = {0};
int ret = GetBlockDevicePath("/misc", miscFile, PATH_MAX);
INIT_ERROR_CHECK(ret == 0, return -1, "Failed to get misc path");
struct RBMiscUpdateMessage msg;
ret = RBMiscReadUpdaterMessage(miscFile, &msg);
INIT_ERROR_CHECK(ret == 0, return -1, "Failed to get misc info");
if (memcmp(msg.command, "boot_charing", strlen("boot_charing")) == 0) {
return GROUP_CHARING;
}
return 0;
}
static int CheckAndRebootToUpdater(const char *valueData, const char *cmd,
const char *cmdExt, const char *boot)
{
......@@ -190,6 +208,15 @@ int DoFreezeCmd(const char *cmd, const char *opt)
#endif
}
#ifdef INIT_TEST
int DoCharingCmd()
{
// by job to stop service and unmount
DoJobNow("reboot");
return CheckAndRebootToUpdater(NULL, "charing", "charing:", "boot_charing");
}
#endif
struct {
char *cmdName;
int (*doCmd)(const char *cmd, const char *opt);
......@@ -204,6 +231,9 @@ struct {
#endif
{ "suspend", DoSuspendCmd },
{ "freeze", DoFreezeCmd },
#ifdef INIT_TEST
{ "charing", DoCharingCmd }
#endif
};
void ExecReboot(const char *value)
......
......@@ -3,6 +3,7 @@
"name" : "param_watcher",
"path" : ["/system/bin/sa_main", "/system/profile/param_watcher.xml"],
"uid" : "system",
"start-mode" : "boot",
"gid" : ["system", "shell"]
}
]
......
# 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.
import("//base/startup/init_lite/begetd.gni")
import("//build/ohos.gni")
ohos_shared_library("libbootchart") {
sources = [ "bootchart/bootchart.c" ]
include_dirs = [
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/include/param",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/plugin",
"//base/startup/init_lite/interfaces/innerkits/include",
"//third_party/bounds_checking_function/include",
]
deps = [
"//base/startup/init_lite/interfaces/innerkits/plugin:libplugin",
"//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/services/utils:libinit_utils",
"//third_party/bounds_checking_function:libsec_static",
"//third_party/cJSON:cjson_static",
]
part_name = "init"
module_install_dir = "lib/plugin"
}
group("plugin") {
deps = [ ":libbootchart" ]
}
/*
* 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 "bootchart.h"
#include <dirent.h>
#include <stdint.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "init_plugin.h"
#include "init_param.h"
#include "init_utils.h"
#include "plugin_adapter.h"
#include "securec.h"
#define NANO_PRE_JIFFY 10000000
static BootchartCtrl *g_bootchartCtrl = NULL;
static PluginInterface *g_pluginInterface = NULL;
static long long GetJiffies(void)
{
struct timespec time1 = {0};
clock_gettime(CLOCK_MONOTONIC, &time1);
long long jiffies1 = (long long)time1.tv_nsec / NANO_PRE_JIFFY;
long long jiffies2 = (long long)time1.tv_sec * (1000000000 / NANO_PRE_JIFFY); // 1000000000 to nsec
return jiffies1 + jiffies2;
}
char *ReadFileToBuffer(const char *fileName, char *buffer, uint32_t bufferSize)
{
PLUGIN_CHECK(buffer != NULL && fileName != NULL, return NULL, "Invalid param");
int fd = -1;
ssize_t readLen = 0;
do {
buffer[0] = '\0';
errno = 0;
fd = open(fileName, O_RDONLY);
if (fd > 0) {
readLen = read(fd, buffer, bufferSize - 1);
}
PLUGIN_CHECK(readLen >= 0, break, "Failed to read data for %s %d readLen %d", fileName, errno, readLen);
buffer[readLen] = '\0';
} while (0);
if (fd != -1) {
close(fd);
}
return (readLen > 0) ? buffer : NULL;
}
static void BootchartLogHeader(void)
{
char date[32]; // 32 data size
time_t tm = time(NULL);
PLUGIN_CHECK(tm >= 0, return, "Failed to get time");
struct tm *now = localtime(&tm);
PLUGIN_CHECK(now != NULL, return, "Failed to get local time");
size_t size = strftime(date, sizeof(date), "%F %T", now);
PLUGIN_CHECK(size >= 0, return, "Failed to strftime");
struct utsname uts;
if (uname(&uts) == -1) {
return;
}
char release[PARAM_VALUE_LEN_MAX] = {};
uint32_t len = sizeof(release);
if (g_pluginInterface->systemReadParam != NULL) {
(void)g_pluginInterface->systemReadParam("hw_sc.build.os.releasetype", release, &len);
}
char *cmdLine = ReadFileToBuffer("/proc/cmdline", g_bootchartCtrl->buffer, g_bootchartCtrl->bufferSize);
PLUGIN_CHECK(cmdLine != NULL, return, "Failed to open file /data/bootchart/header");
FILE *file = fopen("/data/bootchart/header", "we");
PLUGIN_CHECK(file != NULL, return, "Failed to open file /data/bootchart/header");
(void)fprintf(file, "version = openharmony init\n");
(void)fprintf(file, "title = Boot chart for openharmony (%s)\n", date);
(void)fprintf(file, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
if (strlen(release) > 0) {
(void)fprintf(file, "system.release = %s\n", release);
}
(void)fprintf(file, "system.cpu = %s\n", uts.machine);
(void)fprintf(file, "system.kernel.options = %s\n", cmdLine);
(void)fclose(file);
}
static void BootchartLogFile(FILE *log, const char *procfile)
{
(void)fprintf(log, "%lld\n", GetJiffies());
char *data = ReadFileToBuffer(procfile, g_bootchartCtrl->buffer, g_bootchartCtrl->bufferSize);
if (data != NULL) {
(void)fprintf(log, "%s\n", data);
}
}
static void BootchartLogProcessStat(FILE *log, pid_t pid)
{
static char path[255] = { }; // 255 path length
static char nameBuffer[255] = { }; // 255 path length
int ret = sprintf_s(path, sizeof(path) - 1, "/proc/%d/cmdline", pid);
PLUGIN_CHECK(ret > 0, return, "Failed to format path %d", pid);
path[ret] = '\0';
char *name = ReadFileToBuffer(path, nameBuffer, sizeof(nameBuffer));
// Read process stat line
ret = sprintf_s(path, sizeof(path) - 1, "/proc/%d/stat", pid);
PLUGIN_CHECK(ret > 0, return, "Failed to format path %d", pid);
path[ret] = '\0';
char *stat = ReadFileToBuffer(path, g_bootchartCtrl->buffer, g_bootchartCtrl->bufferSize);
if (stat == NULL) {
return;
}
if (name != NULL && strlen(name) > 0) {
char *end = NULL;
char *start = strstr(stat, "(");
if (start != NULL) {
end = strstr(start, ")");
}
if (end != NULL) {
stat[start - stat + 1] = '\0';
(void)fputs(stat, log);
(void)fputs(name, log);
(void)fputs(end, log);
} else {
(void)fputs(stat, log);
}
} else {
(void)fputs(stat, log);
}
}
static void bootchartLogProcess(FILE *log)
{
(void)fprintf(log, "%lld\n", GetJiffies());
DIR *pDir = opendir("/proc");
PLUGIN_CHECK(pDir != NULL, return, "Read dir /proc failed.%d", errno);
struct dirent *entry;
while ((entry = readdir(pDir)) != NULL) {
pid_t pid = (pid_t)atoi(entry->d_name); // Only process processor
if (pid == 0) {
continue;
}
BootchartLogProcessStat(log, pid);
}
closedir(pDir);
(void)fputc('\n', log);
}
static void *BootchartThreadMain(void *data)
{
PLUGIN_LOGI("bootcharting start");
FILE *statFile = fopen("/data/bootchart/proc_stat.log", "w");
FILE *procFile = fopen("/data/bootchart/proc_ps.log", "w");
FILE *diskFile = fopen("/data/bootchart/proc_diskstats.log", "w");
do {
if (statFile == NULL || procFile == NULL || diskFile == NULL) {
PLUGIN_LOGE("Failed to open file");
break;
}
BootchartLogHeader();
while (1) {
pthread_mutex_lock(&(g_bootchartCtrl->mutex));
struct timespec abstime = {};
struct timeval now = {};
const long timeout = 200; // wait time 200ms
gettimeofday(&now, NULL);
long nsec = now.tv_usec * 1000 + (timeout % 1000) * 1000000; // 1000 unit 1000000 unit nsec
abstime.tv_sec = now.tv_sec + nsec / 1000000000 + timeout / 1000; // 1000 unit 1000000000 unit nsec
abstime.tv_nsec = nsec % 1000000000; // 1000000000 unit nsec
pthread_cond_timedwait(&(g_bootchartCtrl->cond), &(g_bootchartCtrl->mutex), &abstime);
if (g_bootchartCtrl->stop) {
pthread_mutex_unlock(&(g_bootchartCtrl->mutex));
break;
}
pthread_mutex_unlock(&(g_bootchartCtrl->mutex));
PLUGIN_LOGV("bootcharting running");
BootchartLogFile(statFile, "/proc/stat");
BootchartLogFile(diskFile, "/proc/diskstats");
bootchartLogProcess(procFile);
}
} while (0);
if (statFile != NULL) {
(void)fflush(statFile);
(void)fclose(statFile);
}
if (procFile != NULL) {
(void)fflush(procFile);
(void)fclose(procFile);
}
if (diskFile != NULL) {
(void)fflush(diskFile);
(void)fclose(diskFile);
}
PLUGIN_LOGI("bootcharting stop");
return NULL;
}
static void BootchartDestory(void)
{
pthread_mutex_destroy(&(g_bootchartCtrl->mutex));
pthread_cond_destroy(&(g_bootchartCtrl->cond));
free(g_bootchartCtrl);
g_bootchartCtrl = NULL;
}
static int DoBootchartStart(void)
{
if (g_pluginInterface == NULL) {
PLUGIN_LOGI("Invalid bootchart plugin");
return -1;
}
char enable[4] = {}; // 4 enable size
uint32_t size = sizeof(enable);
if (g_pluginInterface->systemReadParam != NULL) {
g_pluginInterface->systemReadParam("init.bootchart.enabled", enable, &size);
}
if (strcmp(enable, "1") != 0) {
PLUGIN_LOGI("Not bootcharting");
return 0;
}
mkdir("/data/bootchart", S_IRWXU | S_IRWXG | S_IRWXO);
if (g_bootchartCtrl != NULL) {
PLUGIN_LOGI("bootcharting has been start");
return 0;
}
int ret = 0;
g_bootchartCtrl = malloc(sizeof(BootchartCtrl));
PLUGIN_CHECK(g_bootchartCtrl != NULL, return -1, "Failed to alloc mem for bootchart");
g_bootchartCtrl->bufferSize = DEFAULT_BUFFER;
ret = pthread_mutex_init(&(g_bootchartCtrl->mutex), NULL);
PLUGIN_CHECK(ret == 0, BootchartDestory();
return -1, "Failed to init mutex");
ret = pthread_cond_init(&(g_bootchartCtrl->cond), NULL);
PLUGIN_CHECK(ret == 0, BootchartDestory();
return -1, "Failed to init cond");
g_bootchartCtrl->stop = 0;
ret = pthread_create(&(g_bootchartCtrl->threadId), NULL, BootchartThreadMain, (void *)g_bootchartCtrl);
PLUGIN_CHECK(ret == 0, BootchartDestory();
return -1, "Failed to init cond");
pthread_mutex_lock(&(g_bootchartCtrl->mutex));
pthread_cond_signal(&(g_bootchartCtrl->cond));
pthread_mutex_unlock(&(g_bootchartCtrl->mutex));
g_bootchartCtrl->start = 1;
return 0;
}
static int DoBootchartStop(void)
{
if (g_bootchartCtrl == NULL || !g_bootchartCtrl->start) {
PLUGIN_LOGI("bootcharting not start");
return 0;
}
pthread_mutex_lock(&(g_bootchartCtrl->mutex));
g_bootchartCtrl->stop = 1;
pthread_cond_signal(&(g_bootchartCtrl->cond));
pthread_mutex_unlock(&(g_bootchartCtrl->mutex));
pthread_join(g_bootchartCtrl->threadId, NULL);
BootchartDestory();
PLUGIN_LOGI("bootcharting stoped");
return 0;
}
static int DoBootchartCmd(int id, const char *name, int argc, const char **argv)
{
PLUGIN_LOGI("DoBootchartCmd argc %d %s", argc, name);
PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
if (strcmp(argv[0], "start") == 0) {
return DoBootchartStart();
} else if (strcmp(argv[0], "stop") == 0) {
return DoBootchartStop();
}
return 0;
}
static PluginCmd g_bootchartCmds[] = {
{"bootchart", DoBootchartCmd, 0},
};
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++) {
g_bootchartCmds[i].index = g_pluginInterface->addCmdExecutor(
g_bootchartCmds[i].name, g_bootchartCmds[i].cmdExecutor);
PLUGIN_LOGI("BootchartInit %d", g_bootchartCmds[i].index);
}
return 0;
}
static void BootchartExit(void)
{
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++) {
g_pluginInterface->removeCmdExecutor(g_bootchartCmds[i].name, g_bootchartCmds[i].index);
}
}
PLUGIN_CONSTRUCTOR(void)
{
g_pluginInterface = GetPluginInterface();
if (g_pluginInterface != NULL && g_pluginInterface->pluginRegister != NULL) {
g_pluginInterface->pluginRegister("bootchart", NULL, BootchartInit, BootchartExit);
}
PLUGIN_LOGI("bootchart pluginInterface %p %p %p",
g_pluginInterface, g_pluginInterface->pluginRegister, GetPluginInterface,
BootchartInit, BootchartExit);
}
\ No newline at end of file
/*
* 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.
*/
#ifndef _PLUGIN_BOOTCHART_H
#define _PLUGIN_BOOTCHART_H
#include <pthread.h>
#include <stdint.h>
#define DEFAULT_BUFFER 2048
typedef struct {
int start;
int stop;
pthread_cond_t cond;
pthread_mutex_t mutex;
pthread_t threadId;
uint32_t bufferSize;
char buffer[DEFAULT_BUFFER];
} BootchartCtrl;
#endif /* _PLUGIN_BOOTCHART_H */
/*
* Copyright (c) 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.
*/
#ifndef PLUGIN_ADAPTER_
#define PLUGIN_ADAPTER_
#include <memory.h>
#include <stdint.h>
#include <stdio.h>
#include "init_log.h"
#include "securec.h"
typedef struct {
char *name;
int (*cmdExecutor)(int id, const char *name, int argc, const char **argv);
int index;
} PluginCmd;
#define READ_DURATION 100000
#define PLUGIN_LOG_FILE "plugintest.log"
#define PLUGIN_LABEL "PLUGIN"
#define PLUGIN_LOGI(fmt, ...) STARTUP_LOGI(PLUGIN_LOG_FILE, PLUGIN_LABEL, fmt, ##__VA_ARGS__)
#define PLUGIN_LOGE(fmt, ...) STARTUP_LOGE(PLUGIN_LOG_FILE, PLUGIN_LABEL, fmt, ##__VA_ARGS__)
#define PLUGIN_LOGV(fmt, ...) STARTUP_LOGV(PLUGIN_LOG_FILE, PLUGIN_LABEL, fmt, ##__VA_ARGS__)
#define PLUGIN_CHECK(ret, exper, ...) \
if (!(ret)) { \
PLUGIN_LOGE(__VA_ARGS__); \
exper; \
}
#define PLUGIN_ONLY_CHECK(ret, exper, ...) \
if (!(ret)) { \
exper; \
}
#endif
\ No newline at end of file
{
"jobs" : [{
"name" : "param:test.randrom.read.start=1",
"condition" : "test.randrom.read.start=1",
"cmds" : [
"param_randrom_write test.randrom.read 100"
]
},
{
"name" : "param:startup.service.ctl.group-test-stage1=running",
"condition" : "startup.service.ctl.group-test-stage1=running",
"cmds" : [
"start group-test-stage2"
]
},
{
"name" : "param:startup.service.ctl.group-test-stage2=running",
"condition" : "startup.service.ctl.group-test-stage2=running",
"cmds" : [
"start group-test-stage3"
]
}
],
"services" : [{
"name" : "group-test-stage1",
"start-mode" : "normal",
"path" : ["/system/bin/begetctl", "init", "group", "test", "group-test-stage1"],
"once" : 1
},
{
"name" : "group-test-stage2",
"start-mode" : "condition",
"path" : ["/system/bin/begetctl", "init", "group", "test", "group-test-stage2"],
"once" : 1
},
{
"name" : "group-test-stage3",
"start-mode" : "condition",
"path" : ["/system/bin/begetctl", "init", "group", "test", "wait", "group-test-stage4"],
"once" : 1
},
{
"name" : "group-test-stage4",
"start-mode" : "condition",
"path" : ["/system/bin/begetctl", "init", "group", "test", "group-test-stage4"],
"once" : 1
}
]
}
......@@ -20,7 +20,9 @@
#include <sys/wait.h>
#include "begetctl.h"
#include "loop_event.h"
#include "plugin_test.h"
#include "service_watcher.h"
#include "shell_utils.h"
#include "sys_param.h"
......@@ -118,15 +120,55 @@ static int32_t BShellParamCmdInstall(BShellHandle shell, int32_t argc, char *arg
return 0;
}
MODULE_CONSTRUCTOR(void)
static int32_t BShellParamCmdDisplay(BShellHandle shell, int32_t argc, char *argv[])
{
CmdInfo infos[] = {
{"read", BShellParamCmdRead, "read system parameter", "read [start | stop]", ""},
{"watcher", BShellParamCmdWatch, "watcher system parameter", "watcher [name]", ""},
{"install", BShellParamCmdInstall, "install plugin", "install [name]", ""},
{"uninstall", BShellParamCmdInstall, "uninstall plugin", "uninstall [name]", ""},
};
for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
BShellEnvRegitsterCmd(GetShellHandle(), &infos[i]);
PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
PLUGIN_LOGV("BShellParamCmdDisplay %s %s", argv[0], argv[1]);
SystemSetParameter("ohos.servicectrl.display", argv[1]);
return 0;
}
void ServiceStatusChangeTest(const char *key, ServiceStatus status)
{
PLUGIN_LOGI("group-test-stage3: wait service %s status: %d", key, status);
if (status == SERVICE_READY || status == SERVICE_STARTED) {
PLUGIN_LOGI("Service %s start work", key);
}
}
static int32_t BShellParamCmdGroupTest(BShellHandle shell, int32_t argc, char *argv[])
{
PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
PLUGIN_LOGI("BShellParamCmdGroupTest %s stage: %s", argv[0], argv[1]);
if (argc > 2 && strcmp(argv[1], "wait") == 0) { // 2 service name index
PLUGIN_LOGI("group-test-stage3: wait service %s", argv[2]); // 2 service name index
ServiceWatchForStatus(argv[2], ServiceStatusChangeTest); // 2 service name index
LE_RunLoop(LE_GetDefaultLoop());
}
return 0;
}
int32_t BShellCmdRegister(BShellHandle shell, int execMode)
{
if (execMode == 0) {
CmdInfo infos[] = {
{"init", BShellParamCmdGroupTest, "init group test", "init group test [stage]", "init group test"},
};
for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
BShellEnvRegitsterCmd(shell, &infos[i]);
}
} else {
CmdInfo infos[] = {
{"display", BShellParamCmdDisplay, "display system service", "display service", "display service"},
{"read", BShellParamCmdRead, "read system parameter", "read [start | stop]", ""},
{"watcher", BShellParamCmdWatch, "watcher system parameter", "watcher [name]", ""},
{"install", BShellParamCmdInstall, "install plugin", "install [name]", ""},
{"uninstall", BShellParamCmdInstall, "uninstall plugin", "uninstall [name]", ""},
{"group", BShellParamCmdGroupTest, "group test", "group test [stage]", "group test"},
};
for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
BShellEnvRegitsterCmd(GetShellHandle(), &infos[i]);
}
}
return 0;
}
......@@ -17,18 +17,19 @@
#include <time.h>
#include "plugin_test.h"
#include "init_param.h"
#include "init_plugin.h"
#define MAX_COUNT 1000
#define TEST_CMD_NAME "param_randrom_write"
static PluginInterface *g_pluginInterface = NULL;
static int g_testCmdIndex = 0;
static void 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_CHECK(argv != NULL && argc >= 1, return, "Invalid install parameter");
PLUGIN_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
PLUGIN_CHECK(g_pluginInterface != NULL && g_pluginInterface->systemWriteParam != NULL,
return, "Invalid install parameter");
return -1, "Invalid install parameter");
PLUGIN_LOGI("PluginParamCmdWriteParam argc %d %s", argc, argv[0]);
int maxCount = MAX_COUNT;
if (argc > 1) {
......@@ -46,6 +47,7 @@ static void PluginParamCmdWriteParam(int id, const char *name, int argc, const c
}
count++;
}
return 0;
}
static int PluginParamTestInit(void)
......
......@@ -23,7 +23,7 @@
#include "securec.h"
#define READ_DURATION 100000
#define PLUGIN_LOG_FILE "plugintest.log"
#define PLUGIN_LOG_FILE "begetctrl.log"
#define PLUGIN_LABEL "PLUGIN"
#define PLUGIN_LOGI(fmt, ...) STARTUP_LOGI(PLUGIN_LOG_FILE, PLUGIN_LABEL, fmt, ##__VA_ARGS__)
#define PLUGIN_LOGE(fmt, ...) STARTUP_LOGE(PLUGIN_LOG_FILE, PLUGIN_LABEL, fmt, ##__VA_ARGS__)
......
......@@ -35,10 +35,11 @@ public:
};
int g_cmdExecId = 0;
void TestCmdExecutor(int id, const char *name, int argc, const char **argv)
int TestCmdExecutor(int id, const char *name, int argc, const char **argv)
{
printf("TestCmdExecutor id %d, name %s \n", id, name);
g_cmdExecId = id;
return 0;
}
HWTEST_F(PluginUnitTest, PluginAddCmd, TestSize.Level1)
......@@ -100,7 +101,7 @@ HWTEST_F(PluginUnitTest, PluginInstallTest, TestSize.Level1)
pluginInterface->pluginRegister(moduleName,
"/home/axw/init_ut/etc/init/plugin_param_test.cfg",
PluginTestInit, PluginTestExit);
PluginInstall(moduleName);
PluginInstall(moduleName, NULL);
PluginUninstall(moduleName);
}
} // namespace init_ut
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册