From 1caff3b27419f561eae1423bc2099b1d4eb0115e Mon Sep 17 00:00:00 2001 From: sun_fan Date: Fri, 6 Aug 2021 17:06:04 +0800 Subject: [PATCH 1/3] init: init : do reboot cmd delete NoArugment Signed-off-by: sun_fan --- interfaces/innerkits/reboot/init_reboot.c | 15 ++- services/cmds/reboot/init_cmd_reboot.c | 2 +- services/src/init_reboot.c | 136 +++++++++++----------- 3 files changed, 79 insertions(+), 74 deletions(-) mode change 100755 => 100644 interfaces/innerkits/reboot/init_reboot.c mode change 100755 => 100644 services/cmds/reboot/init_cmd_reboot.c diff --git a/interfaces/innerkits/reboot/init_reboot.c b/interfaces/innerkits/reboot/init_reboot.c old mode 100755 new mode 100644 index c45551b..ec74638 --- a/interfaces/innerkits/reboot/init_reboot.c +++ b/interfaces/innerkits/reboot/init_reboot.c @@ -27,12 +27,19 @@ int DoReboot(const char *cmdContent) { char value[MAX_REBOOT_VAUE_SIZE]; - if (cmdContent == NULL) { - INIT_LOGE("DoReboot api error, cmdContent is NULL."); - return -1; + if (cmdContent == NULL || strlen(cmdContent) == 0) { + if (snprintf_s(value, MAX_REBOOT_NAME_SIZE, MAX_REBOOT_NAME_SIZE - 1, "%s", "reboot") < 0) { + INIT_LOGE("DoReboot api error, MAX_REBOOT_NAME_SIZE is not enough"); + return -1; + } + if (SystemSetParameter("sys.powerctrl", value) != 0) { + INIT_LOGE("DoReboot Api SystemSetParameter error"); + return -1; + } + return 0; } int length = strlen(cmdContent); - if (length == 0 || length > MAX_REBOOT_VAUE_SIZE) { + if (length > MAX_REBOOT_VAUE_SIZE) { INIT_LOGE("DoReboot api error, cmdContent = %s, length = %d.", cmdContent, length); return -1; } diff --git a/services/cmds/reboot/init_cmd_reboot.c b/services/cmds/reboot/init_cmd_reboot.c old mode 100755 new mode 100644 index 6e032e7..943021b --- a/services/cmds/reboot/init_cmd_reboot.c +++ b/services/cmds/reboot/init_cmd_reboot.c @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) if (argc == REBOOT_CMD_NUMBER) { ret = DoReboot(argv[1]); } else { - ret = DoReboot("NoArgument"); + ret = DoReboot(NULL); } if (ret != 0) { printf("[reboot command] DoReboot Api return error\n"); diff --git a/services/src/init_reboot.c b/services/src/init_reboot.c index ef09003..cc2aab3 100644 --- a/services/src/init_reboot.c +++ b/services/src/init_reboot.c @@ -129,6 +129,61 @@ static int GetMountStatusForMountPoint(const char *mountPoint) return 0; } +static int UpdateUpdaterStatus(const char *valueData) +{ + const char *miscFile = "/dev/block/platform/soc/10100000.himci.eMMC/by-name/misc"; + struct RBMiscUpdateMessage msg; + bool ret = RBMiscReadUpdaterMessage(miscFile, &msg); + if (!ret) { + INIT_LOGE("RBMiscReadUpdaterMessage error."); + return -1; + } + if (snprintf_s(msg.command, MAX_COMMAND_SIZE, MAX_COMMAND_SIZE - 1, "%s", "boot_updater") == -1) { + INIT_LOGE("RBMiscWriteUpdaterMessage error"); + return -1; + } + if (strlen(valueData) > strlen("updater:") && strncmp(valueData, "updater:", strlen("updater:")) == 0) { + if (snprintf_s(msg.update, MAX_UPDATE_SIZE, MAX_UPDATE_SIZE - 1, "%s", valueData + strlen("updater:")) == -1) { + INIT_LOGE("RBMiscWriteUpdaterMessage error"); + return -1; + } + ret = RBMiscWriteUpdaterMessage(miscFile, &msg); + if (true != ret) { + INIT_LOGE("RBMiscWriteUpdaterMessage error"); + return -1; + } + } else if (strlen(valueData) == strlen("updater") && strncmp(valueData, "updater", strlen("updater")) == 0) { + ret = RBMiscWriteUpdaterMessage(miscFile, &msg); + if (true != ret) { + INIT_LOGE("RBMiscWriteUpdaterMessage error"); + return -1; + } + } else { + return -1; + } + return 0; +} + +static int DoRebootCore(const char *valueData) +{ + if (valueData == NULL) { + reboot(RB_AUTOBOOT); + return 0; + } else if (strncmp(valueData, "shutdown", strlen("shutdown")) == 0) { + reboot(RB_POWER_OFF); + return 0; + } else if (strncmp(valueData, "updater", strlen("updater")) == 0) { + int ret = UpdateUpdaterStatus(valueData); + if (ret == 0) { + reboot(RB_AUTOBOOT); + return 0; + } + } else { + return -1; + } + return 0; +} + void DoReboot(const char *value) { if (value == NULL) { @@ -137,26 +192,26 @@ void DoReboot(const char *value) } INIT_LOGI("DoReboot value = %s", value); - if (strlen(value) > MAX_VALUE_LENGTH) { + if (strlen(value) > MAX_VALUE_LENGTH || strlen(value) < strlen("reboot") || strlen(value) == strlen("reboot,")) { INIT_LOGE("DoReboot reboot value error, value = %s.", value); return; } - const char *valueData = NULL; - if (strncmp(value, "reboot,", strlen("reboot,")) != 0) { + if (strncmp(value, "reboot,", strlen("reboot,")) == 0) { + valueData = value + strlen("reboot,"); + } else if (strlen(value) < strlen("reboot,") && strncmp(value, "reboot", strlen("reboot")) == 0) { + valueData = NULL; + } else { INIT_LOGE("DoReboot reboot value = %s, must started with reboot ,error.", value); return; - } else { - valueData = value + strlen("reboot,"); } - if (strncmp(valueData, "shutdown", strlen("shutdown")) != 0 + if (valueData != NULL + && strncmp(valueData, "shutdown", strlen("shutdown")) != 0 && strncmp(valueData, "updater:", strlen("updater:")) != 0 - && strncmp(valueData, "updater", strlen("updater")) != 0 - && strncmp(valueData, "NoArgument", strlen("NoArgument")) != 0) { + && strncmp(valueData, "updater", strlen("updater")) != 0) { INIT_LOGE("DoReboot value = %s, parameters error.", value); return; } - StopAllServicesBeforeReboot(); if (GetMountStatusForMountPoint("/vendor") != 0) { if (umount("/vendor") != 0) { @@ -168,67 +223,10 @@ void DoReboot(const char *value) INIT_LOGE("DoReboot umount data failed! errno = %d.", errno); } } - // "shutdown" - if (strncmp(valueData, "shutdown", strlen("shutdown")) == 0) { - int ret = reboot(RB_POWER_OFF); - if (ret != 0) { - INIT_LOGE("DoReboot reboot(RB_POWER_OFF) failed! syscall ret %d, err %d.", ret, errno); - } - return; + int ret = DoRebootCore(valueData); + if (ret != 0) { + INIT_LOGE("DoReboot value = %s, error.", value); } - // "updater" or "updater:" - const char *miscFile = "/dev/block/platform/soc/10100000.himci.eMMC/by-name/misc"; - struct RBMiscUpdateMessage msg; - bool ret = RBMiscReadUpdaterMessage(miscFile, &msg); - if(!ret) { - INIT_LOGE("DoReboot RBMiscReadUpdaterMessage error."); - return; - } - const int commandSize = 12; - if (snprintf_s(msg.command, MAX_COMMAND_SIZE, MAX_COMMAND_SIZE - 1, "%s", "boot_updater") == -1) { - INIT_LOGE("DoReboot updater: RBMiscWriteUpdaterMessage error"); - return; - } - msg.command[commandSize] = 0; - - if (strlen(valueData) > strlen("updater:") && strncmp(valueData, "updater:", strlen("updater:")) == 0) { - const char *p = valueData + strlen("updater:"); - if (snprintf_s(msg.update, MAX_UPDATE_SIZE, MAX_UPDATE_SIZE - 1, "%s", p) == -1) { - INIT_LOGE("DoReboot updater: RBMiscWriteUpdaterMessage error"); - return; - } - msg.update[MAX_UPDATE_SIZE - 1] = 0; - ret = RBMiscWriteUpdaterMessage(miscFile, &msg); - if(true != ret) { - INIT_LOGE("DoReboot updater: RBMiscWriteUpdaterMessage error"); - return; - } - ret = reboot(RB_AUTOBOOT); - if (ret != 0) { - INIT_LOGE("DoReboot updater: reboot(RB_AUTOBOOT) failed! syscall ret %d, err %d.", ret, errno); - } - return; - } - if (strlen(valueData) == strlen("updater") && strncmp(valueData, "updater", strlen("updater")) == 0) { - ret = RBMiscWriteUpdaterMessage(miscFile, &msg); - if(true != ret) { - INIT_LOGE("DoReboot updater RBMiscWriteUpdaterMessage error"); - return; - } - ret = reboot(RB_AUTOBOOT); - if (ret != 0) { - INIT_LOGE("DoReboot updater reboot(RB_AUTOBOOT) failed! syscall ret %d, err %d.", ret, errno); - } - return; - } - if (strlen(valueData) == strlen("NoArgument") && strncmp(valueData, "NoArgument", strlen("NoArgument")) == 0) { - ret = reboot(RB_AUTOBOOT); - if (ret != 0) { - INIT_LOGE("DoReboot updater: reboot(RB_AUTOBOOT) failed! syscall ret %d, err %d.", ret, errno); - } - return; - } - INIT_LOGE("DoReboot value = %s, error.", value); return; } From b5a18fbe463db79df165f7d24bc6751c7c917b6e Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Fri, 6 Aug 2021 18:20:42 +0800 Subject: [PATCH 2/3] fix code style Signed-off-by: zhong_ning --- services/src/init_reboot.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/services/src/init_reboot.c b/services/src/init_reboot.c index cc2aab3..76eb0f9 100644 --- a/services/src/init_reboot.c +++ b/services/src/init_reboot.c @@ -148,13 +148,13 @@ static int UpdateUpdaterStatus(const char *valueData) return -1; } ret = RBMiscWriteUpdaterMessage(miscFile, &msg); - if (true != ret) { + if (ret != true) { INIT_LOGE("RBMiscWriteUpdaterMessage error"); return -1; } } else if (strlen(valueData) == strlen("updater") && strncmp(valueData, "updater", strlen("updater")) == 0) { ret = RBMiscWriteUpdaterMessage(miscFile, &msg); - if (true != ret) { + if (ret != true) { INIT_LOGE("RBMiscWriteUpdaterMessage error"); return -1; } @@ -205,10 +205,10 @@ void DoReboot(const char *value) INIT_LOGE("DoReboot reboot value = %s, must started with reboot ,error.", value); return; } - if (valueData != NULL - && strncmp(valueData, "shutdown", strlen("shutdown")) != 0 - && strncmp(valueData, "updater:", strlen("updater:")) != 0 - && strncmp(valueData, "updater", strlen("updater")) != 0) { + if (valueData != NULL && + strncmp(valueData, "shutdown", strlen("shutdown")) != 0 && + strncmp(valueData, "updater:", strlen("updater:")) != 0 && + strncmp(valueData, "updater", strlen("updater")) != 0) { INIT_LOGE("DoReboot value = %s, parameters error.", value); return; } From f11a44fc1343f00bb71a63485a5ded3634089e48 Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Fri, 6 Aug 2021 21:44:05 +0800 Subject: [PATCH 3/3] fix code style Signed-off-by: zhong_ning --- services/src/init_reboot.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/services/src/init_reboot.c b/services/src/init_reboot.c index 76eb0f9..e1b285c 100644 --- a/services/src/init_reboot.c +++ b/services/src/init_reboot.c @@ -205,10 +205,9 @@ void DoReboot(const char *value) INIT_LOGE("DoReboot reboot value = %s, must started with reboot ,error.", value); return; } - if (valueData != NULL && - strncmp(valueData, "shutdown", strlen("shutdown")) != 0 && - strncmp(valueData, "updater:", strlen("updater:")) != 0 && - strncmp(valueData, "updater", strlen("updater")) != 0) { + if (valueData != NULL && strncmp(valueData, "shutdown", strlen("shutdown")) != 0 && + strncmp(valueData, "updater:", strlen("updater:")) != 0 && + strncmp(valueData, "updater", strlen("updater")) != 0) { INIT_LOGE("DoReboot value = %s, parameters error.", value); return; }