提交 1b4a5685 编写于 作者: C cheng_jinsong

add shutdown option

Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
上级 eb438eb7
......@@ -29,8 +29,26 @@ extern "C" {
#define DEVICE_CMD_FREEZE "freeze"
#define DEVICE_CMD_RESTORE "restore"
/**
* @brief reboot system
*
* @param option format is: mode[:info]
* mode : command sub key, example: updater, shutdown ...
* info : extend info
* @return int
*/
int DoReboot(const char *cmdContent);
/**
* @brief reboot system
*
* @param mode command sub key, example: updater, shutdown ...
* @param option extend info
*
* @return int
*/
int DoRebootExt(const char *mode, const char *option);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -48,6 +48,7 @@
CachedParameterGetChanged;
CachedParameterDestroy;
DoReboot;
DoRebootExt;
GetControlSocket;
OH_ListAddTail;
OH_ListAddWithOrder;
......
......@@ -30,23 +30,25 @@
#define DOREBOOT_PARAM "reboot.ut"
#endif
static int DoReboot_(const char *option)
static int DoReboot_(const char *mode, const char *option)
{
char value[MAX_REBOOT_OPTION_SIZE];
int ret = 0;
if (option == NULL || strlen(option) == 0) {
ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
ret = SystemSetParameter(DOREBOOT_PARAM, "reboot");
BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set reboot param");
return 0;
if (mode != NULL) {
if ((option != NULL) && (strlen(option) >= 0)) {
ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s:%s", mode, option);
} else {
ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", mode);
}
} else {
if ((option != NULL) && (strlen(option) >= 0)) {
ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", option);
} else {
ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "%s", "reboot");
}
}
int length = strlen(option);
BEGET_ERROR_CHECK(length <= MAX_REBOOT_OPTION_SIZE, return -1,
"Reboot option \" %s \" is too large, overflow", option);
ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", option);
BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed to copy boot option \" %s \"", option);
BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed to format boot mode ");
BEGET_LOGV("Reboot cmd %s", value);
ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
ret = SystemSetParameter(DOREBOOT_PARAM, value);
......@@ -54,7 +56,7 @@ static int DoReboot_(const char *option)
return 0;
}
int DoReboot(const char *option)
static int ExecReboot_(const char *mode, const char *option)
{
// check if param set ok
#ifndef STARTUP_INIT_TEST
......@@ -63,7 +65,7 @@ int DoReboot(const char *option)
const int maxCount = 1;
#endif
int count = 0;
DoReboot_(option);
DoReboot_(mode, option);
while (count < maxCount) {
usleep(100 * 1000); // 100 * 1000 wait 100ms
char result[10] = {0}; // 10 stop len
......@@ -74,8 +76,18 @@ int DoReboot(const char *option)
return 0;
}
count++;
DoReboot_(option);
DoReboot_(mode, option);
}
BEGET_LOGE("Failed to reboot system");
return 0;
}
int DoReboot(const char *option)
{
return ExecReboot_(NULL, option);
}
int DoRebootExt(const char *mode, const char *option)
{
return ExecReboot_(mode, option);
}
\ No newline at end of file
......@@ -49,7 +49,7 @@ MODULE_CONSTRUCTOR(void)
{
CmdInfo infos[] = {
{"reboot", main_cmd, "reboot system", "reboot", ""},
{"reboot", main_cmd, "shutdown system", "reboot shutdown", ""},
{"reboot", main_cmd, "shutdown system", "reboot shutdown[:options]", ""},
{"reboot", main_cmd, "suspend system", "reboot suspend", ""},
{"reboot", main_cmd, "reboot and boot into updater", "reboot updater", ""},
{"reboot", main_cmd, "reboot and boot into updater", "reboot updater[:options]", ""},
......
......@@ -78,8 +78,22 @@ static int DoRebootShutdown(int id, const char *name, int argc, const char **arg
UNUSED(name);
UNUSED(argc);
UNUSED(argv);
PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
// clear misc
(void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
const size_t len = strlen("reboot,");
const char *cmd = strstr(argv[0], "reboot,");
if (cmd != NULL && strlen(cmd) > len) {
PLUGIN_LOGI("DoRebootShutdown argv %s", cmd + len);
// by job to stop service and unmount
DoJobNow("reboot");
#ifndef STARTUP_INIT_TEST
return syscall(__NR_reboot,
LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, cmd + len);
#else
return 0;
#endif
}
return DoRoot_("reboot", RB_POWER_OFF);
}
......@@ -139,8 +153,12 @@ static int DoRebootOther(int id, const char *name, int argc, const char **argv)
const char *cmd = strstr(argv[0], "reboot,");
PLUGIN_CHECK(cmd != NULL, return -1, "Invalid parameter argc %s", argv[0]);
PLUGIN_LOGI("DoRebootOther argv %s", argv[0]);
#ifndef STARTUP_INIT_TEST
return syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, cmd + strlen("reboot,"));
#else
return 0;
#endif
}
static void RebootAdpInit(void)
......
......@@ -145,6 +145,9 @@ MODULE_CONSTRUCTOR(void)
MODULE_DESTRUCTOR(void)
{
if (getpid() != 1) {
return;
}
PLUGIN_LOGI("Selinux adapter plug-in exit now ...");
SelinuxAdpExit();
}
......@@ -52,6 +52,30 @@ ohos_fuzztest("DoRebootFuzzTest") {
defines = [ "STARTUP_INIT_TEST" ]
}
ohos_fuzztest("DoRebootExtFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "//base/startup/init/test/fuzztest/DoRebootExt_fuzzer"
include_dirs = [
"//base/startup/init/interfaces/innerkits/include",
"//base/startup/init/test/fuzztest/utils/include",
]
deps = [
"//base/startup/init/interfaces/innerkits:libbegetutil",
"//third_party/bounds_checking_function:libsec_static",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "DoRebootExt_fuzzer/DoRebootExt_fuzzer.cpp" ]
defines = [ "STARTUP_INIT_TEST" ]
}
ohos_fuzztest("GetControlFileFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "//base/startup/init/test/fuzztest/getcontrolfile_fuzzer"
......
/*
* Copyright (c) 2023 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 "DoRebootExt_fuzzer.h"
#include<string>
#include "init_reboot.h"
namespace OHOS {
bool FuzzDoRebootExt(const uint8_t* data, size_t size)
{
bool result = false;
std::string str(reinterpret_cast<const char*>(data), size);
if (!DoRebootExt(str.c_str(), str.c_str())) {
result = true;
}
return result;
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
/* Run your code on data */
OHOS::FuzzDoRebootExt(data, size);
return 0;
}
/*
* Copyright (c) 2023 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 TEST_FUZZTEST_DOREBOOTEXT_FUZZER_H
#define TEST_FUZZTEST_DOREBOOTEXT_FUZZER_H
#include "fuzz_utils.h"
#define FUZZ_PROJECT_NAME "DoRebootExt_fuzzer"
#endif
/*
* Copyright (c) 2023 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.
*/
FUZZ
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2023 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>100</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>30</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
......@@ -99,6 +99,28 @@ HWTEST_F(InitRebootUnitTest, TestAddRebootCmdNormal, TestSize.Level1)
SystemWriteParam("ohos.startup.powerctrl", "reboot,flashd");
SystemWriteParam("ohos.startup.powerctrl", "reboot,flashd:1000000");
}
HWTEST_F(InitRebootUnitTest, TestRebootCmdExec, TestSize.Level1)
{
PARAM_LOGE("TestRebootCmdExec");
PluginExecCmdByName("reboot", "reboot");
PluginExecCmdByName("reboot.shutdown", "reboot,shutdown");
PluginExecCmdByName("reboot.shutdown", "reboot,shutdown:333333333333");
PluginExecCmdByName("reboot.suspend", "reboot,suspend");
PluginExecCmdByName("reboot.charge", "reboot,charge");
PluginExecCmdByName("reboot.updater", "reboot,updater");
PluginExecCmdByName("reboot.updater", "reboot,updater:2222222");
PluginExecCmdByName("reboot.flashd", "reboot,flashd");
PluginExecCmdByName("reboot.flashd", "reboot,flashd:1000000");
PluginExecCmdByName("reboot.other", "reboot,other");
PARAM_LOGE("TestRebootCmdExec end");
int ret = UpdateMiscMessage("charge:wwwwwwwwwww", "charge", "charge:", "boot_charge");
if (ret == 0) {
ret = GetBootModeFromMisc();
EXPECT_EQ(ret, GROUP_CHARGE);
clearMisc();
}
}
#endif
HWTEST_F(InitRebootUnitTest, TestInitReboot, TestSize.Level1)
......@@ -121,11 +143,7 @@ HWTEST_F(InitRebootUnitTest, TestInitReboot, TestSize.Level1)
ret = DoReboot(DEVICE_CMD_FREEZE);
EXPECT_EQ(ret, 0);
ret = UpdateMiscMessage("charge:wwwwwwwwwww", "charge", "charge:", "boot_charge");
if (ret == 0) {
ret = GetBootModeFromMisc();
EXPECT_EQ(ret, GROUP_CHARGE);
clearMisc();
}
ret = DoRebootExt("shutdown", DEVICE_CMD_FREEZE);
EXPECT_EQ(ret, 0);
}
} // namespace init_ut
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册