From 37c678458879049bb36e361282c7b7dbecd095e3 Mon Sep 17 00:00:00 2001 From: lanxueyuan Date: Wed, 14 Apr 2021 18:53:45 +0800 Subject: [PATCH 01/21] change test case to lv1 --- .../test/unittest/common/cmd_func_test.cpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/services/test/unittest/common/cmd_func_test.cpp b/services/test/unittest/common/cmd_func_test.cpp index 41eb980e8..676403b7e 100644 --- a/services/test/unittest/common/cmd_func_test.cpp +++ b/services/test/unittest/common/cmd_func_test.cpp @@ -135,7 +135,7 @@ public: ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level1) { // do not crash ParseCmdLine(nullptr, nullptr); @@ -147,7 +147,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -175,7 +175,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -193,7 +193,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -234,7 +234,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -266,7 +266,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level1) { // do not crash here DoCmd(nullptr); @@ -278,7 +278,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -297,7 +297,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -341,7 +341,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F732P **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -393,7 +393,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F732P **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -434,7 +434,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F732P **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level1) { CmdLine curCmdLine; @@ -499,7 +499,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level1) { struct stat fileStat = {0}; EXPECT_EQ(0, stat(CFG_FILE.c_str(), &fileStat)); @@ -764,7 +764,7 @@ static void CheckJobs(const cJSON* fileRoot) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level1) { char* fileBuf = ReadFileToBuf(); if (fileBuf == nullptr) { @@ -814,7 +814,7 @@ static void CreateIllegalCfg() * @tc.type: FUNC * @tc.require: AR000F861Q */ -HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -830,7 +830,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000F861Q */ -HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level1) { CmdLine curCmdLine; std::string cmdStr = "loadcfg "; @@ -868,7 +868,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000F861Q */ -HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level1) { CmdLine curCmdLine; std::string cmdStr = "loadcfg "; @@ -916,7 +916,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000F733F */ -HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level1) { // functions do not crash ParseAllJobs(nullptr); @@ -938,7 +938,7 @@ HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000F733F */ -HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level1) { std::string cfgJson = "{\"jobs\":[{\"name\":\"pre-init\",\"cmds\":[\"mkdir " + PRE_INIT_DIR + "\"]},{\"name\":\"init\",\"cmds\":[\"mkdir " + INIT_DIR + -- Gitee From 3900746c958132786b8e6ea932e77e974fe3cecc Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Wed, 21 Apr 2021 22:11:55 +0800 Subject: [PATCH 02/21] updater_start_l1 --- services/BUILD.gn | 138 +++-- services/include/device.h | 33 ++ services/include/list.h | 41 ++ services/src/device.c | 51 ++ services/src/init_cmds.c | 100 +++- services/src/init_service.c | 2 +- services/src/init_service_manager.c | 4 +- services/src/list.c | 36 ++ services/src/main.c | 22 +- services/src/uevent.c | 768 ++++++++++++++++++++++++++++ 10 files changed, 1139 insertions(+), 56 deletions(-) create mode 100755 services/include/device.h create mode 100755 services/include/list.h create mode 100755 services/src/device.c create mode 100755 services/src/list.c create mode 100755 services/src/uevent.c diff --git a/services/BUILD.gn b/services/BUILD.gn index a161e6e22..1fb8e4cba 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -10,60 +10,110 @@ # 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("//build/lite/config/component/lite_component.gni") +if (defined(ohos_lite)) { + import("//build/lite/config/component/lite_component.gni") -lite_component("init_lite") { - features = [ ":init" ] -} + lite_component("init_lite") { + features = [ ":init" ] + } -# feature: init -executable("init") { - defines = [ - "_GNU_SOURCE", #syscall function need this macro definition - ] - sources = [ - "src/init_adapter.c", - "src/init_cmds.c", - "src/init_jobs.c", - "src/init_read_cfg.c", - "src/init_service.c", - "src/init_service_manager.c", - "src/init_signal_handler.c", - "src/main.c", - ] + # feature: init + executable("init") { + defines = [ + "_GNU_SOURCE", #syscall function need this macro definition + "OHOS_LITE=1", + ] + sources = [ + "src/init_adapter.c", + "src/init_cmds.c", + "src/init_jobs.c", + "src/init_read_cfg.c", + "src/init_service.c", + "src/init_service_manager.c", + "src/init_signal_handler.c", + "src/main.c", + ] - include_dirs = [ - "include", - "//third_party/cJSON", - "//third_party/bounds_checking_function/include", - "//base/startup/syspara_lite/interfaces/kits", - ] + include_dirs = [ + "include", + "//third_party/cJSON", + "//third_party/bounds_checking_function/include", + "//base/startup/syspara_lite/interfaces/kits", + ] - cflags = [ "-Wall" ] + cflags = [ "-Wall" ] - deps = [ - "//base/startup/syspara_lite/frameworks/parameter:parameter", - "//build/lite/config/component/cJSON:cjson_shared", - "//third_party/bounds_checking_function:libsec_shared", - ] + deps = [ + "//base/startup/syspara_lite/frameworks/parameter:parameter", + "//build/lite/config/component/cJSON:cjson_shared", + "//third_party/bounds_checking_function:libsec_shared", + ] + ldflags = [] + if (ohos_kernel_type == "liteos_a") { + include_dirs += [ "//kernel/liteos_a/syscall" ] + } + if (ohos_kernel_type == "linux") { + defines += [ "NEED_EXEC_RCS_LINUX" ] + ldflags += [ + "-lm", + "-lpthread", + ] + } + } - ldflags = [] + if (ohos_build_type == "debug") { + group("unittest") { + deps = [ "//base/startup/init_lite/services/test/unittest/common:unittest" ] + } + } +} else { + import("//build/ohos.gni") - if (ohos_kernel_type == "liteos_a") { - include_dirs += [ "//kernel/liteos_a/syscall" ] + ohos_executable("updaterueventd") { + sources = [ + "src/uevent.c", + "src/list.c", + ] + include_dirs = [ + "include", + "//third_party/bounds_checking_function/include", + ] + deps = [ + "//third_party/bounds_checking_function:libsec_static", + ] + install_enable = true + part_name = "updater" } - if (ohos_kernel_type == "linux") { - defines += [ "NEED_EXEC_RCS_LINUX" ] - ldflags += [ - "-lm", - "-lpthread", - ] + ohos_executable("updaterinit") { + sources = [ + "src/main.c", + "src/init_cmds.c", + "src/init_jobs.c", + "src/init_read_cfg.c", + "src/init_adapter.c", + "src/init_service.c", + "src/init_service_manager.c", + "src/init_signal_handler.c", + "src/device.c", + ] + defines = [ "OHOS_LITE=0" ] + include_dirs = [ + "include", + "//third_party/cJSON", + "//third_party/bounds_checking_function/include", + ] + deps = [ + "//third_party/bounds_checking_function:libsec_static", + "//third_party/cJSON:cjson_static", + ] + install_enable = true + part_name = "updater" } -} -if (ohos_build_type == "debug") { - group("unittest") { - deps = [ "//base/startup/init_lite/services/test/unittest/common:unittest" ] + ohos_prebuilt_etc("init.cfg") { + source = "//device/hisilicon/hi3516dv300/updater/init.cfg" + relative_install_dir = "init" + subsystem_name = "updater" } } diff --git a/services/include/device.h b/services/include/device.h new file mode 100755 index 000000000..6739b2bc7 --- /dev/null +++ b/services/include/device.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2020 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 BASE_STARTUP_INITLITE_DEVICE_H +#define BASE_STARTUP_INITLITE_DEVICE_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +void MountBasicFs(); +void CreateDeviceNode(); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +#endif // BASE_STARTUP_INITLITE_DEVICE_H diff --git a/services/include/list.h b/services/include/list.h new file mode 100755 index 000000000..4fa912873 --- /dev/null +++ b/services/include/list.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2020 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 BASE_STARTUP_INITLITE_LIST_H +#define BASE_STARTUP_INITLITE_LIST_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +#include + +struct ListNode { + struct ListNode *next; + struct ListNode *prev; +}; + +void ListInit(struct ListNode *list); +void ListAddTail(struct ListNode *list, struct ListNode *item); +void ListRemove(struct ListNode *item); +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif + +#endif // BASE_STARTUP_INITLITE_LIST_H diff --git a/services/src/device.c b/services/src/device.c new file mode 100755 index 000000000..9411a6a7f --- /dev/null +++ b/services/src/device.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020 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 +#include +#include +#include +#include +#include + +void MountBasicFs() +{ + if (mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755") != 0) { + printf("Mount tmpfs failed. %s\n", strerror(errno)); + } + if (mount("proc", "/proc", "proc", 0, "hidepid=2") != 0) { + printf("Mount procfs failed. %s\n", strerror(errno)); + } + if (mount("sysfs", "/sys", "sysfs", 0, NULL) != 0) { + printf("Mount sysfs failed. %s\n", strerror(errno)); + } +} + +void CreateDeviceNode() +{ + if (mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11)) != 0) { + printf("Create /dev/kmsg device node failed. %s\n", strerror(errno)); + } + if (mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3)) != 0) { + printf("Create /dev/null device node failed. %s\n", strerror(errno)); + } + if (mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8)) != 0) { + printf("Create /dev/random device node failed. %s\n", strerror(errno)); + } + + if (mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9)) != 0) { + printf("Create /dev/urandom device node failed. %s\n", strerror(errno)); + } +} diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index f10833749..6f3f22666 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -24,11 +24,14 @@ #include #include #include - +#if !OHOS_LITE +#include +#include +#include +#endif #include "init_service_manager.h" #include "securec.h" - #define MODE_LEN 4 // for chmod mode, format 0xxx #define DEFAULT_DIR_MODE 0755 // mkdir, default mode #define SPACES_CNT_IN_CMD_MAX 10 // mount, max number of spaces in cmdline @@ -48,6 +51,7 @@ static const char* g_supportedCmds[] = { "chown ", "mount ", "loadcfg ", + "insmod ", }; void ParseCmdLine(const char* cmdStr, CmdLine* resCmd) @@ -172,14 +176,14 @@ static void DoChown(const char* cmdContent) static char* CopySubStr(const char* srcStr, size_t startPos, size_t endPos) { if (endPos <= startPos) { - printf("[Init] DoMount, invalid params<%lu, %lu> for %s.\n", endPos, startPos, srcStr); + printf("[Init] DoMount, invalid params<%zu, %zu> for %s.\n", endPos, startPos, srcStr); return NULL; } size_t mallocLen = endPos - startPos + 1; char* retStr = (char*)malloc(mallocLen); if (retStr == NULL) { - printf("[Init] DoMount, malloc failed! malloc size %lu, for %s.\n", mallocLen, srcStr); + printf("[Init] DoMount, malloc failed! malloc size %zu, for %s.\n", mallocLen, srcStr); return NULL; } @@ -316,6 +320,86 @@ static void DoMount(const char* cmdContent) free(target); } +#if !OHOS_LITE +// format insmod [-f] [options] +static void DoInsmod(const char *cmdContent) +{ +#define OPTIONS_SIZE (128u) + char *p = NULL; + char *line = NULL; + char *restPtr = NULL; + char *fileName = NULL; + int flags = 0; + int fd = -1; + char options[OPTIONS_SIZE] = {0}; + + size_t count = strlen(cmdContent); + if (count > OPTIONS_SIZE) { + printf("[Init], options too long, maybe lost some of options\n"); + } + line = (char *)malloc(count + 1); + if (line == NULL) { + printf("[Init] Allocate memory failed.\n"); + return; + } + if (memcpy_s(line, count, cmdContent, count) != EOK) { + printf("[Init] memcpy failed\n"); + } + + line[count] = '\0'; + + do { + if ((p = strtok_r(line, " ", &restPtr)) == NULL) { + printf("[Init] debug, cannot get filename\n"); + free(line); + return; + } + fileName = p; + printf("[Init] debug, fileName is [%s]\n", fileName); + if ((p = strtok_r(NULL, " ", &restPtr)) == NULL) { + break; + } + if (!strcmp(p, "-f")) { + flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS; + } + } while (0); + + if (flags != 0) { // '-f' option + p = restPtr; // grab all rest of contents. + } else { // no '-f' option, should combine p and resetPtr + if (p != NULL) { + if (restPtr != NULL) { + if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", p, restPtr) == -1) { + return; + } + } else { + if (strncpy_s(options, OPTIONS_SIZE - 1, p, strlen(p)) != 0) { + return; + } + } + } + } + // Open ko files + fd = open(fileName, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); + if (fd < 0) { + printf("[Init] failed to open %s: %d\n", fileName, errno); + goto out; + } + + int rc = syscall(__NR_finit_module, fd, options, flags); + if (rc == -1) { + printf("[Init] finit_module for %s failed: %d\n", fileName, errno); + } +out: + if (fd > 0) { + close(fd); + } + if (line != NULL) { + free(line); + } +} +#endif + static bool CheckValidCfg(const char *path) { size_t cfgCnt = sizeof(g_supportCfg) / sizeof(g_supportCfg[0]); @@ -402,7 +486,13 @@ void DoCmd(const CmdLine* curCmd) DoMount(curCmd->cmdContent); } else if (strncmp(curCmd->name, "loadcfg ", strlen("loadcfg ")) == 0) { DoLoadCfg(curCmd->cmdContent); - } else { + } +#if !OHOS_LITE + else if (strncmp(curCmd->name, "insmod ", strlen("insmod ")) == 0) { + DoInsmod(curCmd->cmdContent); + } +#endif + else { printf("[Init] DoCmd, unknown cmd name %s.\n", curCmd->name); } } diff --git a/services/src/init_service.c b/services/src/init_service.c index fd8504554..1e6e577ab 100644 --- a/services/src/init_service.c +++ b/services/src/init_service.c @@ -64,7 +64,7 @@ static int SetPerms(const Service *service) capHeader.version = _LINUX_CAPABILITY_VERSION_3; capHeader.pid = 0; - struct __user_cap_data_struct capData[CAP_NUM] = {0}; + struct __user_cap_data_struct capData[CAP_NUM] = {}; for (unsigned int i = 0; i < service->servPerm.capsCnt; ++i) { if (service->servPerm.caps[i] == FULL_CAP) { for (int i = 0; i < CAP_NUM; ++i) { diff --git a/services/src/init_service_manager.c b/services/src/init_service_manager.c index d06fec7f1..2a7cbcbbe 100755 --- a/services/src/init_service_manager.c +++ b/services/src/init_service_manager.c @@ -66,7 +66,7 @@ void StartServiceByName(const char* servName) void StopAllServices() { - for (size_t i = 0; i < g_servicesCnt; i++) { + for (int i = 0; i < g_servicesCnt; i++) { if (ServiceStop(&g_services[i]) != SERVICE_SUCCESS) { printf("[Init] StopAllServices, service %s stop failed!\n", g_services[i].name); } @@ -75,7 +75,7 @@ void StopAllServices() void ReapServiceByPID(int pid) { - for (size_t i = 0; i < g_servicesCnt; i++) { + for (int i = 0; i < g_servicesCnt; i++) { if (g_services[i].pid == pid) { if (g_services[i].attribute & SERVICE_ATTR_IMPORTANT) { // important process exit, need to reboot system diff --git a/services/src/list.c b/services/src/list.c new file mode 100755 index 000000000..17e6f468f --- /dev/null +++ b/services/src/list.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020 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 "list.h" + +void ListInit(struct ListNode *node) +{ + node->next = node; + node->prev = node; +} + +void ListAddTail(struct ListNode *head, struct ListNode *item) +{ + item->next = head; + item->prev = head->prev; + head->prev->next = item; + head->prev = item; +} + +void ListRemove(struct ListNode *item) +{ + item->next->prev = item->prev; + item->prev->next = item->next; +} diff --git a/services/src/main.c b/services/src/main.c index 7f1436512..0c116e7d8 100755 --- a/services/src/main.c +++ b/services/src/main.c @@ -27,12 +27,19 @@ #include "init_adapter.h" #include "init_read_cfg.h" #include "init_signal_handler.h" +#if OHOS_LITE #include "parameter.h" +#endif + +#if !OHOS_LITE +#include "device.h" +#endif static const pid_t INIT_PROCESS_PID = 1; static void PrintSysInfo() { +#if OHOS_LITE char* sysInfo = GetVersionId(); if (sysInfo != NULL) { printf("[Init] %s\n", sysInfo); @@ -41,6 +48,7 @@ static void PrintSysInfo() return; } printf("[Init] main, GetVersionId failed!\n"); +#endif } #ifdef OHOS_DEBUG @@ -72,7 +80,13 @@ int main(int argc, char * const argv[]) // 1. print system info PrintSysInfo(); - // 2. signal register +#if !OHOS_LITE + // 2. Mount basic filesystem and create common device node. + MountBasicFs(); + CreateDeviceNode(); +#endif + + // 3. signal register SignalInitModule(); #ifdef OHOS_DEBUG @@ -82,7 +96,7 @@ int main(int argc, char * const argv[]) } #endif // OHOS_DEBUG - // 3. execute rcs + // 4. execute rcs ExecuteRcs(); #ifdef OHOS_DEBUG @@ -92,7 +106,7 @@ int main(int argc, char * const argv[]) } #endif // OHOS_DEBUG - // 4. read configuration file and do jobs + // 5. read configuration file and do jobs InitReadCfg(); #ifdef OHOS_DEBUG @@ -102,7 +116,7 @@ int main(int argc, char * const argv[]) } #endif // OHOS_DEBUG - // 5. keep process alive + // 6. keep process alive #ifdef OHOS_DEBUG printf("[Init] main, time used: sigInfo %ld ms, rcs %ld ms, cfg %ld ms.\n", \ TimeDiffMs(&tmEnter, &tmSysInfo), TimeDiffMs(&tmSysInfo, &tmRcs), TimeDiffMs(&tmRcs, &tmCfg)); diff --git a/services/src/uevent.c b/services/src/uevent.c new file mode 100755 index 000000000..a2b5d222a --- /dev/null +++ b/services/src/uevent.c @@ -0,0 +1,768 @@ +/* + * Copyright (c) 2020 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "list.h" +#include "securec.h" + +int g_ueventFD = -1; + +struct Uevent { + const char *action; + const char *path; + const char *subsystem; + const char *firmware; + const char *partitionName; + const char *deviceName; + int partitionNum; + int major; + int minor; +}; + +struct PlatformNode { + char *name; + char *path; + size_t pathLen; + struct ListNode list; +}; + +static struct ListNode g_platformNames = { + .next = &g_platformNames, + .prev = &g_platformNames, +}; + +const char *TRIGGER = "/dev/.trigger_uevent"; +static void HandleUevent(); + +static int UeventFD() +{ + return g_ueventFD; +} + +static void DoTrigger(DIR *dir) +{ + struct dirent *de = NULL; + int dfd = dirfd(dir); + int fd = openat(dfd, "uevent", O_WRONLY); + if (fd >= 0) { + write(fd, "add\n", 4); + close(fd); + HandleUevent(); + } + + while ((de = readdir(dir)) != NULL) { + DIR *dir2 = NULL; + if (de->d_type != DT_DIR || de->d_name[0] == '.') { + continue; + } + fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); + if (fd < 0) { + continue; + } + + dir2 = fdopendir(fd); + if (dir2 == NULL) { + close(fd); + } else { + DoTrigger(dir2); + closedir(dir2); + } + } +} + +void Trigger(const char *sysPath) +{ + DIR *dir = opendir(sysPath); + if (dir) { + DoTrigger(dir); + closedir(dir); + } +} + +static void RetriggerUevent() +{ + if (access(TRIGGER, F_OK) == 0) { + printf("Skip trigger uevent, alread done\n"); + return; + } + Trigger("/sys/class"); + Trigger("/sys/block"); + Trigger("/sys/devices"); + int fd = open(TRIGGER, O_WRONLY | O_CREAT | O_CLOEXEC, 0000); + if (fd > 0) { + close(fd); + } + printf("Re-trigger uevent done\n"); +} + +static void UeventSockInit() +{ + struct sockaddr_nl addr; + int buffSize = 256 * 1024; + int on = 1; + + if (memset_s(&addr, sizeof(addr), 0, sizeof(addr)) != 0) { + return; + } + addr.nl_family = AF_NETLINK; + addr.nl_pid = getpid(); + addr.nl_groups = 0xffffffff; + + int sockfd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT); + if (sockfd < 0) { + printf("Create socket failed. %d\n", errno); + return; + } + + setsockopt(sockfd, SOL_SOCKET, SO_RCVBUFFORCE, &buffSize, sizeof(buffSize)); + setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); + + if (bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + printf("Bind socket failed. %d\n", errno); + close(sockfd); + return; + } + g_ueventFD = sockfd; + fcntl(g_ueventFD, F_SETFD, FD_CLOEXEC); + fcntl(g_ueventFD, F_SETFL, O_NONBLOCK); + RetriggerUevent(); + return; +} + +ssize_t ReadUevent(int fd, void *buf, size_t len) +{ + struct iovec iov = { buf, len }; + struct sockaddr_nl addr; + char control[CMSG_SPACE(sizeof(struct ucred))]; + uid_t uid = -1; + struct msghdr hdr = { + &addr, + sizeof(addr), + &iov, + 1, + control, + sizeof(control), + 0, + }; + ssize_t n = recvmsg(fd, &hdr, 0); + if (n <= 0) { + return n; + } + struct cmsghdr *cMsg = CMSG_FIRSTHDR(&hdr); + if (cMsg == NULL || cMsg->cmsg_type != SCM_CREDENTIALS) { + goto out; + } + struct ucred *cRed = (struct ucred *)CMSG_DATA(cMsg); + uid = cRed->uid; + if (uid != 0) { + goto out; + } + + if (addr.nl_groups == 0 || addr.nl_pid != 0) { + /* ignoring non-kernel or unicast netlink message */ + goto out; + } + + return n; +out: + bzero(buf, len); + errno = -EIO; + return n; +} + +static void InitUevent(struct Uevent *event) +{ + event->action = ""; + event->path = ""; + event->subsystem = ""; + event->firmware = ""; + event->partitionName = ""; + event->deviceName = ""; + event->partitionNum = -1; +} + +static void ParseUevent(const char *buf, struct Uevent *event) +{ + InitUevent(event); + while (*buf) { + if (strncmp(buf, "ACTION=", 7) == 0) { + buf += 7; + event->action = buf; + } else if (strncmp(buf, "DEVPATH=", 8) == 0) { + buf += 8; + event->path = buf; + } else if (strncmp(buf, "SUBSYSTEM=", 10) == 0) { + buf += 10; + event->subsystem = buf; + } else if (strncmp(buf, "FIRMWARE=", 9) == 0) { + buf += 9; + event->firmware = buf; + } else if (strncmp(buf, "MAJOR=", 6) == 0) { + buf += 6; + event->major = atoi(buf); + } else if (strncmp(buf, "MINOR=", 6) == 0) { + buf += 6; + event->minor = atoi(buf); + } else if (strncmp(buf, "PARTN=", 6) == 0) { + buf += 6; + event->partitionNum = atoi(buf); + } else if (strncmp(buf, "PARTNAME=", 9) == 0) { + buf += 9; + event->partitionName = buf; + } else if (strncmp(buf, "DEVNAME=", 8) == 0) { + buf += 8; + event->deviceName = buf; + } + // Drop reset. + while (*buf++) {} + } +} + +static int MakeDir(const char *path, mode_t mode) +{ + int rc = mkdir(path, mode); + if (rc < 0 && errno != EEXIST) { + printf("Create %s failed. %d\n", path, errno); + } + return rc; +} + +static struct PlatformNode *FindPlatformDevice(const char *path) +{ + size_t pathLen = strlen(path); + struct ListNode *node = NULL; + struct PlatformNode *bus = NULL; + + for (node = (&g_platformNames)->prev; node != &g_platformNames; node = node->prev) { + bus = (struct PlatformNode *)(((char*)(node)) - offsetof(struct PlatformNode, list)); + if ((bus->pathLen < pathLen) && (path[bus->pathLen] == '/') && !strncmp(path, bus->path, bus->pathLen)) { + return bus; + } + } + return NULL; +} + +static void Sanitize(char *s) +{ + const char* accept = + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "_-."; + + if (!s) { + return; + } + + for (; *s; s++) { + s += strspn(s, accept); + if (*s) { + *s = '_'; + } + } +} + +static char **ParsePlatformBlockDevice(const struct Uevent *uevent) +{ + const char *device; + char *slash = NULL; + const char *type; + char linkPath[256]; + int linkNum = 0; + char *p = NULL; + + struct PlatformNode *pDev = FindPlatformDevice(uevent->path); + if (pDev) { + device = pDev->name; + type = "platform"; + } else { + printf("Non platform device.\n"); + return NULL; + } + + char **links = malloc(sizeof(char *) * 4); + if (!links) { + return NULL; + } + if (memset_s(links, sizeof(char *) * 4, 0, sizeof(char *) * 4) != 0) { + return NULL; + } + printf("found %s device %s\n", type, device); + if (snprintf_s(linkPath, sizeof(linkPath), sizeof(linkPath), "/dev/block/%s/%s", type, device) == -1) { + return NULL; + } + if (uevent->partitionName) { + p = strdup(uevent->partitionName); + Sanitize(p); + if (strcmp(uevent->partitionName, p)) { + printf("Linking partition '%s' as '%s'\n", uevent->partitionName, p); + } + if (asprintf(&links[linkNum], "%s/by-name/%s", linkPath, p) > 0) { + linkNum++; + } else { + links[linkNum] = NULL; + } + free(p); + } + if (uevent->partitionNum >= 0) { + if (asprintf(&links[linkNum], "%s/by-num/p%d", linkPath, uevent->partitionNum) > 0) { + linkNum++; + } else { + links[linkNum] = NULL; + } + } + slash = strrchr(uevent->path, '/'); + if (asprintf(&links[linkNum], "%s/%s", linkPath, slash + 1) > 0) { + linkNum++; + } else { + links[linkNum] = NULL; + } + return links; +} + +static void MakeDevice(const char *devpath, const char *path, int block, int major, int minor) +{ + /* Only for super user */ + gid_t gid = 0; + dev_t dev; + mode_t mode = 0600; + mode |= (block ? S_IFBLK : S_IFCHR); + dev = makedev(major, minor); + setegid(gid); + if (mknod(devpath, mode, dev) != 0) { + if (errno != EEXIST) { + printf("Make device node[%d, %d] failed. %d\n", major, minor, errno); + } + } +} + +int MkdirRecursive(const char *pathName, mode_t mode) +{ + char buf[128]; + const char *slash; + const char *p = pathName; + struct stat info; + + while ((slash = strchr(p, '/')) != NULL) { + int width = slash - pathName; + p = slash + 1; + if (width < 0) { + break; + } + if (width == 0) { + continue; + } + if ((unsigned int)width > sizeof(buf) - 1) { + printf("path too long for MkdirRecursive\n"); + return -1; + } + if (memcpy_s(buf, width, pathName, width) != 0) { + return -1; + } + buf[width] = 0; + if (stat(buf, &info) != 0) { + int ret = MakeDir(buf, mode); + if (ret && errno != EEXIST) { + return ret; + } + } + } + int ret = MakeDir(pathName, mode); + if (ret && errno != EEXIST) { + return ret; + } + return 0; +} + +void RemoveLink(const char *oldpath, const char *newpath) +{ + char path[256]; + ssize_t ret = readlink(newpath, path, sizeof(path) - 1); + if (ret <= 0) { + return; + } + path[ret] = 0; + if (!strcmp(path, oldpath)) { + unlink(newpath); + } +} + +static void MakeLink(const char *oldPath, const char *newPath) +{ + char buf[256]; + char *slash = strrchr(newPath, '/'); + if (!slash) { + return; + } + int width = slash - newPath; + if (width <= 0 || width > (int)sizeof(buf) - 1) { + return; + } + if (memcpy_s(buf, sizeof(buf), newPath, width) != 0) { + return; + } + buf[width] = 0; + int ret = MkdirRecursive(buf, 0755); + if (ret) { + printf("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno); + } + ret = symlink(oldPath, newPath); + if (ret && errno != EEXIST) { + printf("Failed to symlink %s to %s: %s (%d)\n", oldPath, newPath, strerror(errno), errno); + } +} + +static void HandleDevice(const char *action, const char *devpath, const char *path, int block, int major, + int minor, char **links) +{ + int i; + if (!strcmp(action, "add")) { + MakeDevice(devpath, path, block, major, minor); + if (links) { + for (i = 0; links[i]; i++) { + MakeLink(devpath, links[i]); + } + } + } + + if (!strcmp(action, "remove")) { + if (links) { + for (i = 0; links[i]; i++) { + RemoveLink(devpath, links[i]); + } + } + unlink(devpath); + } + + if (links) { + for (i = 0; links[i]; i++) { + free(links[i]); + } + free(links); + } +} + +static void HandleBlockDevice(struct Uevent *event) +{ + const char *base = "/dev/block"; + char devpath[96]; + char **links = NULL; + + if (event->major < 0 || event->minor < 0) { + return; + } + const char *name = strrchr(event->path, '/'); + if (name == NULL) { + return; + } + name++; + if (strlen(name) > 64) { // too long + return; + } + if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "%s/%s", base, name) == -1) { + return; + } + MakeDir(base, 0755); + if (!strncmp(event->path, "/devices/", 9)) { + links = ParsePlatformBlockDevice(event); + } + HandleDevice(event->action, devpath, event->path, 1, event->major, event->minor, links); +} + +static void AddPlatformDevice(const char *path) +{ + size_t pathLen = strlen(path); + const char *name = path; + + if (!strncmp(path, "/devices/", 9)) { + name += 9; + if (!strncmp(name, "platform/", 9)) { + name += 9; + } + } + printf("adding platform device %s (%s)\n", name, path); + struct PlatformNode *bus = calloc(1, sizeof(struct PlatformNode)); + bus->path = strdup(path); + bus->pathLen = pathLen; + bus->name = bus->path + (name - path); + ListAddTail(&g_platformNames, &bus->list); +} + +static void RemovePlatformDevice(const char *path) +{ + struct ListNode *node = NULL; + struct PlatformNode *bus = NULL; + + for (node = (&g_platformNames)->prev; node != &g_platformNames; node = node->prev) { + bus = (struct PlatformNode *)(((char*)(node)) - offsetof(struct PlatformNode, list)); + if (!strcmp(path, bus->path)) { + printf("removing platform device %s\n", bus->name); + free(bus->path); + ListRemove(node); + free(bus); + return; + } + } +} + +static void HandlePlatformDevice(const struct Uevent *event) +{ + const char *path = event->path; + if (strcmp(event->action, "add") == 0) { + AddPlatformDevice(path); + } else if (strcmp(event->action, "remove") == 0) { + RemovePlatformDevice(path); + } +} + +static const char *ParseDeviceName(const struct Uevent *uevent, unsigned int len) +{ + /* if it's not a /dev device, nothing else to do */ + if ((uevent->major < 0) || (uevent->minor < 0)) { + return NULL; + } + /* do we have a name? */ + const char *name = strrchr(uevent->path, '/'); + if (!name) { + return NULL; + } + name++; + /* too-long names would overrun our buffer */ + if (strlen(name) > len) { + return NULL; + } + return name; +} + +static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) +{ + char *slash = NULL; + int linkNum = 0; + int width; + + struct PlatformNode *pDev = FindPlatformDevice(uevent->path); + if (!pDev) { + return NULL; + } + + char **links = malloc(sizeof(char *) * 2); + if (!links) { + return NULL; + } + if (memset_s(links, sizeof(char *) * 2, 0, sizeof(char *) * 2) != 0) { + return NULL; + } + + /* skip "/devices/platform/" */ + const char *parent = strchr(uevent->path + pDev->pathLen, '/'); + if (!*parent) { + goto err; + } + + if (!strncmp(parent, "/usb", 4)) { + /* skip root hub name and device. use device interface */ + while (*++parent && *parent != '/') {} + if (*parent) { + while (*++parent && *parent != '/') {} + } + if (!*parent) { + goto err; + } + slash = strchr(++parent, '/'); + if (!slash) { + goto err; + } + width = slash - parent; + if (width <= 0) { + goto err; + } + + if (asprintf(&links[linkNum], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) { + linkNum++; + } else { + links[linkNum] = NULL; + } + mkdir("/dev/usb", 0755); + } else { + goto err; + } + return links; +err: + free(links); + return NULL; +} + +static void HandleGenericDevice(struct Uevent *event) +{ + char *base = NULL; + char devpath[96] = {0}; + char **links = NULL; + + const char *name = ParseDeviceName(event, 64); + if (!name) { + return; + } + if (!strncmp(event->subsystem, "usb", 3)) { + if (!strcmp(event->subsystem, "usb")) { + if (event->deviceName) { + /* + * create device node provided by kernel if present + * see drivers/base/core.c + */ + char *p = devpath; + if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "/dev/%s", event->deviceName) == -1) { + return; + } + /* skip leading /dev/ */ + p += 5; + /* build directories */ + while (*p) { + if (*p == '/') { + *p = 0; + MakeDir(devpath, 0755); + *p = '/'; + } + p++; + } + } else { + /* This imitates the file system that would be created + * if we were using devfs instead. + * Minors are broken up into groups of 128, starting at "001" + */ + int busId = event->minor / 128 + 1; + int deviceId = event->minor % 128 + 1; + /* build directories */ + MakeDir("/dev/bus", 0755); + MakeDir("/dev/bus/usb", 0755); + if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "/dev/bus/usb/%03d", busId) == -1) { + return; + } + MakeDir(devpath, 0755); + if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "/dev/bus/usb/%03d/%03d", busId, + deviceId) == -1) { + return; + } + } + } else { + /* ignore other USB events */ + return; + } + } else if (!strncmp(event->subsystem, "graphics", 8)) { + base = "/dev/graphics/"; + MakeDir(base, 0755); + } else if (!strncmp(event->subsystem, "drm", 3)) { + base = "/dev/dri/"; + MakeDir(base, 0755); + } else if (!strncmp(event->subsystem, "oncrpc", 6)) { + base = "/dev/oncrpc/"; + MakeDir(base, 0755); + } else if (!strncmp(event->subsystem, "adsp", 4)) { + base = "/dev/adsp/"; + MakeDir(base, 0755); + } else if (!strncmp(event->subsystem, "input", 5)) { + base = "/dev/input/"; + MakeDir(base, 0755); + } else if (!strncmp(event->subsystem, "mtd", 3)) { + base = "/dev/mtd/"; + MakeDir(base, 0755); + } else if (!strncmp(event->subsystem, "sound", 5)) { + base = "/dev/snd/"; + MakeDir(base, 0755); + } else if (!strncmp(event->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) { + base = "/dev/log/"; + MakeDir(base, 0755); + name += 4; + } else { + base = "/dev/"; + } + links = GetCharacterDeviceSymlinks(event); + if (!devpath[0]) { + if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "%s%s", base, name) == -1) { + return; + } + } + HandleDevice(event->action, devpath, event->path, 0, + event->major, event->minor, links); +} + +static void HandleDeviceUevent(struct Uevent *event) +{ + if (strcmp(event->action, "add") == 0 || strcmp(event->action, "change") == 0) { + /* Do nothing for now */ + } + if (strncmp(event->subsystem, "block", 5) == 0) { + HandleBlockDevice(event); + } else if (strncmp(event->subsystem, "platform", 8) == 0) { + HandlePlatformDevice(event); + } else { + HandleGenericDevice(event); + } +} + +static void HandleUevent() +{ + char buf[1024 + 2]; + int ret; + struct Uevent event; + while ((ret = ReadUevent(g_ueventFD, buf, 1024)) > 0) { + if (ret >= 1024) { + continue; + } + buf[ret] = '\0'; + buf[ret + 1] = '\0'; + ParseUevent(buf, &event); + HandleDeviceUevent(&event); + } +} + +void UeventInit() +{ + struct pollfd ufd; + UeventSockInit(); + ufd.events = POLLIN; + ufd.fd = UeventFD(); + while (1) { + ufd.revents = 0; + int ret = poll(&ufd, 1, -1); + if (ret <= 0) { + continue; + } + if (ufd.revents & POLLIN) { + HandleUevent(); + } + } + return; +} + +int main(int argc, char **argv) +{ + printf("Uevent demo starting...\n"); + UeventInit(); + return 0; +} -- Gitee From 97d8bd8b373b72406cfcff269dac1b9c72879d95 Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Thu, 22 Apr 2021 14:54:07 +0800 Subject: [PATCH 03/21] modify updater init --- services/BUILD.gn | 3 +-- services/src/init_cmds.c | 8 +++++--- services/src/main.c | 8 ++++---- services/test/unittest/common/BUILD.gn | 4 ++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index 1fb8e4cba..b2b9065f8 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -21,7 +21,7 @@ if (defined(ohos_lite)) { executable("init") { defines = [ "_GNU_SOURCE", #syscall function need this macro definition - "OHOS_LITE=1", + "OHOS_LITE", ] sources = [ "src/init_adapter.c", @@ -97,7 +97,6 @@ if (defined(ohos_lite)) { "src/init_signal_handler.c", "src/device.c", ] - defines = [ "OHOS_LITE=0" ] include_dirs = [ "include", "//third_party/cJSON", diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index 6f3f22666..587d3eaa2 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -24,7 +24,7 @@ #include #include #include -#if !OHOS_LITE +#ifndef OHOS_LITE #include #include #include @@ -320,7 +320,7 @@ static void DoMount(const char* cmdContent) free(target); } -#if !OHOS_LITE +#ifndef OHOS_LITE // format insmod [-f] [options] static void DoInsmod(const char *cmdContent) { @@ -370,10 +370,12 @@ static void DoInsmod(const char *cmdContent) if (p != NULL) { if (restPtr != NULL) { if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", p, restPtr) == -1) { + goto out; return; } } else { if (strncpy_s(options, OPTIONS_SIZE - 1, p, strlen(p)) != 0) { + goto out; return; } } @@ -487,7 +489,7 @@ void DoCmd(const CmdLine* curCmd) } else if (strncmp(curCmd->name, "loadcfg ", strlen("loadcfg ")) == 0) { DoLoadCfg(curCmd->cmdContent); } -#if !OHOS_LITE +#ifndef OHOS_LITE else if (strncmp(curCmd->name, "insmod ", strlen("insmod ")) == 0) { DoInsmod(curCmd->cmdContent); } diff --git a/services/src/main.c b/services/src/main.c index 0c116e7d8..acdc8e373 100755 --- a/services/src/main.c +++ b/services/src/main.c @@ -27,11 +27,11 @@ #include "init_adapter.h" #include "init_read_cfg.h" #include "init_signal_handler.h" -#if OHOS_LITE +#ifdef OHOS_LITE #include "parameter.h" #endif -#if !OHOS_LITE +#ifndef OHOS_LITE #include "device.h" #endif @@ -39,7 +39,7 @@ static const pid_t INIT_PROCESS_PID = 1; static void PrintSysInfo() { -#if OHOS_LITE +#ifdef OHOS_LITE char* sysInfo = GetVersionId(); if (sysInfo != NULL) { printf("[Init] %s\n", sysInfo); @@ -80,7 +80,7 @@ int main(int argc, char * const argv[]) // 1. print system info PrintSysInfo(); -#if !OHOS_LITE +#ifndef OHOS_LITE // 2. Mount basic filesystem and create common device node. MountBasicFs(); CreateDeviceNode(); diff --git a/services/test/unittest/common/BUILD.gn b/services/test/unittest/common/BUILD.gn index 8214e3a4e..e9b94934e 100644 --- a/services/test/unittest/common/BUILD.gn +++ b/services/test/unittest/common/BUILD.gn @@ -21,9 +21,9 @@ unittest("init_test") { "-lpthread", "-lm", ] - + defines = ["OHOS_LITE"] if (storage_type == "emmc") { - defines = [ "USE_EMMC_STORAGE" ] + defines += [ "USE_EMMC_STORAGE" ] } include_dirs = [ "//base/startup/init_lite/services/include" ] -- Gitee From 3494a968934733c7a19bc410a8233b566db116fd Mon Sep 17 00:00:00 2001 From: leon Date: Fri, 23 Apr 2021 15:48:03 +0800 Subject: [PATCH 04/21] Fix coding style --- services/include/list.h | 2 - services/src/device.c | 15 +- services/src/init_adapter.c | 1 + services/src/init_cmds.c | 94 ++--- services/src/uevent.c | 328 ++++++++++-------- .../test/unittest/common/cmd_func_test.cpp | 36 +- 6 files changed, 268 insertions(+), 208 deletions(-) diff --git a/services/include/list.h b/services/include/list.h index 4fa912873..ee2220cc0 100755 --- a/services/include/list.h +++ b/services/include/list.h @@ -22,8 +22,6 @@ extern "C" { #endif #endif -#include - struct ListNode { struct ListNode *next; struct ListNode *prev; diff --git a/services/src/device.c b/services/src/device.c index 9411a6a7f..0a38fe4fd 100755 --- a/services/src/device.c +++ b/services/src/device.c @@ -20,6 +20,13 @@ #include #include +#define DEFAULT_RW_MODE 0666 +#define DEFAULT_NO_AUTHORITY_MODE 0600 +#define DEVICE_ID_THIRD 3 +#define DEVICE_ID_EIGHTH 8 +#define DEVICE_ID_NINTH 9 +#define DEVICE_ID_ELEVNTH 11 + void MountBasicFs() { if (mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755") != 0) { @@ -35,17 +42,17 @@ void MountBasicFs() void CreateDeviceNode() { - if (mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11)) != 0) { + if (mknod("/dev/kmsg", S_IFCHR | DEFAULT_NO_AUTHORITY_MODE, makedev(1, DEVICE_ID_ELEVNTH)) != 0) { printf("Create /dev/kmsg device node failed. %s\n", strerror(errno)); } - if (mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3)) != 0) { + if (mknod("/dev/null", S_IFCHR | DEFAULT_RW_MODE, makedev(1, DEVICE_ID_THIRD)) != 0) { printf("Create /dev/null device node failed. %s\n", strerror(errno)); } - if (mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8)) != 0) { + if (mknod("/dev/random", S_IFCHR | DEFAULT_RW_MODE, makedev(1, DEVICE_ID_EIGHTH)) != 0) { printf("Create /dev/random device node failed. %s\n", strerror(errno)); } - if (mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9)) != 0) { + if (mknod("/dev/urandom", S_IFCHR | DEFAULT_RW_MODE, makedev(1, DEVICE_ID_NINTH)) != 0) { printf("Create /dev/urandom device node failed. %s\n", strerror(errno)); } } diff --git a/services/src/init_adapter.c b/services/src/init_adapter.c index e82cec5aa..34c46408b 100644 --- a/services/src/init_adapter.c +++ b/services/src/init_adapter.c @@ -14,6 +14,7 @@ */ #include "init_adapter.h" + #include #include #include diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index 587d3eaa2..14ddcedbb 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -14,7 +14,6 @@ */ #include "init_cmds.h" - #include #include #include @@ -40,6 +39,7 @@ #define LOADCFG_BUF_SIZE 128 // loadcfg, max buffer for one cmdline #define LOADCFG_MAX_FILE_LEN 51200 // loadcfg, max file size is 50K #define LOADCFG_MAX_LOOP 20 // loadcfg, to prevent to be trapped in infite loop +#define OCTAL_TYPE 8 //8 means octal to decimal static const char *g_supportCfg[] = { "/patch/fstab.cfg", }; @@ -119,7 +119,7 @@ static void DoChmod(const char* cmdContent) } const char* pathBeginStr = cmdContent + MODE_LEN + 1; // after space - mode_t mode = strtoul(cmdContent, NULL, 8); // 8 means octal to decimal + mode_t mode = strtoul(cmdContent, NULL, OCTAL_TYPE); if (mode == 0) { printf("[Init] DoChmod, strtoul failed for %s, er %d.\n", cmdContent, errno); return; @@ -321,17 +321,51 @@ static void DoMount(const char* cmdContent) } #ifndef OHOS_LITE +#define OPTIONS_SIZE (128u) +static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPtr, int flags) +{ + int fd = -1; + char options[OPTIONS_SIZE] = {0}; + + if (flags == 0) { // '-f' option + if (restPtr != NULL && secondPtr != NULL) { // Reset arugments, combine then all. + if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", secondPtr, restPtr) == -1) { + goto out; + } + } else { + if (strncpy_s(options, OPTIONS_SIZE - 1, secondPtr, strlen(secondPtr)) != 0) { + goto out; + } + } + } else { // Only restPtr is option + if (restPtr != NULL) { + strncpy_s(options, OPTIONS_SIZE - 1, restPtr, strlen(restPtr)); + } + } + fd = open(fileName, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); + if (fd < 0) { + printf("[Init] failed to open %s: %d\n", fileName, errno); + goto out; + } + int rc = syscall(__NR_finit_module, fd, options, flags); + if (rc == -1) { + printf("[Init] finit_module for %s failed: %d\n", fileName, errno); + } +out: + if (fd > 0) { + close(fd); + } + return; +} + // format insmod [-f] [options] static void DoInsmod(const char *cmdContent) { -#define OPTIONS_SIZE (128u) char *p = NULL; - char *line = NULL; char *restPtr = NULL; char *fileName = NULL; + char *line = NULL; int flags = 0; - int fd = -1; - char options[OPTIONS_SIZE] = {0}; size_t count = strlen(cmdContent); if (count > OPTIONS_SIZE) { @@ -342,16 +376,15 @@ static void DoInsmod(const char *cmdContent) printf("[Init] Allocate memory failed.\n"); return; } + if (memcpy_s(line, count, cmdContent, count) != EOK) { printf("[Init] memcpy failed\n"); + return; } - line[count] = '\0'; - do { if ((p = strtok_r(line, " ", &restPtr)) == NULL) { printf("[Init] debug, cannot get filename\n"); - free(line); return; } fileName = p; @@ -363,44 +396,13 @@ static void DoInsmod(const char *cmdContent) flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS; } } while (0); - - if (flags != 0) { // '-f' option - p = restPtr; // grab all rest of contents. - } else { // no '-f' option, should combine p and resetPtr - if (p != NULL) { - if (restPtr != NULL) { - if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", p, restPtr) == -1) { - goto out; - return; - } - } else { - if (strncpy_s(options, OPTIONS_SIZE - 1, p, strlen(p)) != 0) { - goto out; - return; - } - } - } - } - // Open ko files - fd = open(fileName, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); - if (fd < 0) { - printf("[Init] failed to open %s: %d\n", fileName, errno); - goto out; - } - - int rc = syscall(__NR_finit_module, fd, options, flags); - if (rc == -1) { - printf("[Init] finit_module for %s failed: %d\n", fileName, errno); - } -out: - if (fd > 0) { - close(fd); - } + DoInsmodInternal(fileName, p, restPtr, flags); if (line != NULL) { free(line); } + return; } -#endif +#endif // OHOS_LITE static bool CheckValidCfg(const char *path) { @@ -488,13 +490,11 @@ void DoCmd(const CmdLine* curCmd) DoMount(curCmd->cmdContent); } else if (strncmp(curCmd->name, "loadcfg ", strlen("loadcfg ")) == 0) { DoLoadCfg(curCmd->cmdContent); - } #ifndef OHOS_LITE - else if (strncmp(curCmd->name, "insmod ", strlen("insmod ")) == 0) { + } else if (strncmp(curCmd->name, "insmod ", strlen("insmod ")) == 0) { DoInsmod(curCmd->cmdContent); - } #endif - else { + } else { printf("[Init] DoCmd, unknown cmd name %s.\n", curCmd->name); } } diff --git a/services/src/uevent.c b/services/src/uevent.c index a2b5d222a..9e91dd6f0 100755 --- a/services/src/uevent.c +++ b/services/src/uevent.c @@ -31,8 +31,57 @@ #include "list.h" #include "securec.h" +#define LINK_NUMBER 4 +#define DEFAULT_DIR_MODE 0755 +#define DEV_DRM 3 +#define DEV_ONCRPC 6 +#define DEV_ADSP 4 +#define DEV_INPUT 5 +#define DEV_MTD 3 +#define DEV_SOUND 5 +#define DEV_MISC 4 +#define DEV_DEFAULT 4 +#define DEV_PLAT_FORM 9 +#define DEV_USB 4 +#define DEV_GRAPHICS 8 +#define EVENT_ACTION 7 +#define EVENT_DEVPATH 8 +#define EVENT_SYSTEM 10 +#define EVENT_FIRMWARE 9 +#define EVENT_MAJOR 6 +#define EVENT_MINOR 6 +#define EVENT_PARTN 6 +#define EVENT_PART_NAME 9 +#define EVENT_DEV_NAME 8 +#define EVENT_BLOCK 5 +#define EVENT_PLAT_FORM 8 +#define TRIGGER_ADDR_SIZE 4 +#define BASE_BUFFER_SIZE 1024 +#define MAX_BUFFER 256 +#define EVENT_MAX_BUFFER 1026 +#define MAX_DEV_PATH 96 +#define MINORS_GROUPS 128 +#define SYS_LINK_NUMBER 2 +#define MAX_DEVICE_LEN 64 +#define DEFAULT_MODE 0000 +#define DEVICE_SKIP 5 +#define HANDLE_DEVICE_USB 3 +#define DEFAULT_NO_AUTHORITY_MODE 0600 + int g_ueventFD = -1; +#define CHECK_RESULT_DONE(ret, do, another) \ + if (ret) { \ + do; \ + } else { \ + another; \ + } + +#define CHECK_RETURN(ret, statement) \ + if (!(ret)) { \ + statement; \ + } + struct Uevent { const char *action; const char *path; @@ -71,7 +120,7 @@ static void DoTrigger(DIR *dir) int dfd = dirfd(dir); int fd = openat(dfd, "uevent", O_WRONLY); if (fd >= 0) { - write(fd, "add\n", 4); + write(fd, "add\n", TRIGGER_ADDR_SIZE); close(fd); HandleUevent(); } @@ -114,7 +163,7 @@ static void RetriggerUevent() Trigger("/sys/class"); Trigger("/sys/block"); Trigger("/sys/devices"); - int fd = open(TRIGGER, O_WRONLY | O_CREAT | O_CLOEXEC, 0000); + int fd = open(TRIGGER, O_WRONLY | O_CREAT | O_CLOEXEC, DEFAULT_MODE); if (fd > 0) { close(fd); } @@ -124,7 +173,7 @@ static void RetriggerUevent() static void UeventSockInit() { struct sockaddr_nl addr; - int buffSize = 256 * 1024; + int buffSize = MAX_BUFFER * BASE_BUFFER_SIZE; int on = 1; if (memset_s(&addr, sizeof(addr), 0, sizeof(addr)) != 0) { @@ -211,32 +260,32 @@ static void ParseUevent(const char *buf, struct Uevent *event) { InitUevent(event); while (*buf) { - if (strncmp(buf, "ACTION=", 7) == 0) { - buf += 7; + if (strncmp(buf, "ACTION=", EVENT_ACTION) == 0) { + buf += EVENT_ACTION; event->action = buf; - } else if (strncmp(buf, "DEVPATH=", 8) == 0) { - buf += 8; + } else if (strncmp(buf, "DEVPATH=", EVENT_DEVPATH) == 0) { + buf += EVENT_DEVPATH; event->path = buf; - } else if (strncmp(buf, "SUBSYSTEM=", 10) == 0) { - buf += 10; + } else if (strncmp(buf, "SUBSYSTEM=", EVENT_SYSTEM) == 0) { + buf += EVENT_SYSTEM; event->subsystem = buf; - } else if (strncmp(buf, "FIRMWARE=", 9) == 0) { - buf += 9; + } else if (strncmp(buf, "FIRMWARE=", EVENT_FIRMWARE) == 0) { + buf += EVENT_FIRMWARE; event->firmware = buf; - } else if (strncmp(buf, "MAJOR=", 6) == 0) { - buf += 6; + } else if (strncmp(buf, "MAJOR=", EVENT_MAJOR) == 0) { + buf += EVENT_MAJOR; event->major = atoi(buf); - } else if (strncmp(buf, "MINOR=", 6) == 0) { - buf += 6; + } else if (strncmp(buf, "MINOR=", EVENT_MINOR) == 0) { + buf += EVENT_MINOR; event->minor = atoi(buf); - } else if (strncmp(buf, "PARTN=", 6) == 0) { - buf += 6; + } else if (strncmp(buf, "PARTN=", EVENT_PARTN) == 0) { + buf += EVENT_PARTN; event->partitionNum = atoi(buf); - } else if (strncmp(buf, "PARTNAME=", 9) == 0) { - buf += 9; + } else if (strncmp(buf, "PARTNAME=", EVENT_PART_NAME) == 0) { + buf += EVENT_PART_NAME; event->partitionName = buf; - } else if (strncmp(buf, "DEVNAME=", 8) == 0) { - buf += 8; + } else if (strncmp(buf, "DEVNAME=", EVENT_DEV_NAME) == 0) { + buf += EVENT_DEV_NAME; event->deviceName = buf; } // Drop reset. @@ -293,24 +342,17 @@ static char **ParsePlatformBlockDevice(const struct Uevent *uevent) const char *device; char *slash = NULL; const char *type; - char linkPath[256]; + char linkPath[MAX_BUFFER]; int linkNum = 0; char *p = NULL; struct PlatformNode *pDev = FindPlatformDevice(uevent->path); - if (pDev) { - device = pDev->name; - type = "platform"; - } else { - printf("Non platform device.\n"); - return NULL; - } - - char **links = malloc(sizeof(char *) * 4); + CHECK_RESULT_DONE(pDev, device = pDev->name; type = "platform", printf("Non platform device.\n"); return NULL); + char **links = malloc(sizeof(char *) * LINK_NUMBER); if (!links) { return NULL; } - if (memset_s(links, sizeof(char *) * 4, 0, sizeof(char *) * 4) != 0) { + if (memset_s(links, sizeof(char *) * LINK_NUMBER, 0, sizeof(char *) * LINK_NUMBER) != 0) { return NULL; } printf("found %s device %s\n", type, device); @@ -351,7 +393,7 @@ static void MakeDevice(const char *devpath, const char *path, int block, int maj /* Only for super user */ gid_t gid = 0; dev_t dev; - mode_t mode = 0600; + mode_t mode = DEFAULT_NO_AUTHORITY_MODE; mode |= (block ? S_IFBLK : S_IFCHR); dev = makedev(major, minor); setegid(gid); @@ -402,7 +444,7 @@ int MkdirRecursive(const char *pathName, mode_t mode) void RemoveLink(const char *oldpath, const char *newpath) { - char path[256]; + char path[MAX_BUFFER]; ssize_t ret = readlink(newpath, path, sizeof(path) - 1); if (ret <= 0) { return; @@ -415,7 +457,7 @@ void RemoveLink(const char *oldpath, const char *newpath) static void MakeLink(const char *oldPath, const char *newPath) { - char buf[256]; + char buf[MAX_BUFFER]; char *slash = strrchr(newPath, '/'); if (!slash) { return; @@ -428,7 +470,7 @@ static void MakeLink(const char *oldPath, const char *newPath) return; } buf[width] = 0; - int ret = MkdirRecursive(buf, 0755); + int ret = MkdirRecursive(buf, DEFAULT_DIR_MODE); if (ret) { printf("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno); } @@ -438,12 +480,11 @@ static void MakeLink(const char *oldPath, const char *newPath) } } -static void HandleDevice(const char *action, const char *devpath, const char *path, int block, int major, - int minor, char **links) +static void HandleDevice(struct Uevent *event, const char *devpath, int block, char **links) { int i; - if (!strcmp(action, "add")) { - MakeDevice(devpath, path, block, major, minor); + if (!strcmp(event->action, "add")) { + MakeDevice(devpath, event->path, block, event->major, event->minor); if (links) { for (i = 0; links[i]; i++) { MakeLink(devpath, links[i]); @@ -451,7 +492,7 @@ static void HandleDevice(const char *action, const char *devpath, const char *pa } } - if (!strcmp(action, "remove")) { + if (!strcmp(event->action, "remove")) { if (links) { for (i = 0; links[i]; i++) { RemoveLink(devpath, links[i]); @@ -471,7 +512,7 @@ static void HandleDevice(const char *action, const char *devpath, const char *pa static void HandleBlockDevice(struct Uevent *event) { const char *base = "/dev/block"; - char devpath[96]; + char devpath[MAX_DEV_PATH]; char **links = NULL; if (event->major < 0 || event->minor < 0) { @@ -482,17 +523,17 @@ static void HandleBlockDevice(struct Uevent *event) return; } name++; - if (strlen(name) > 64) { // too long + if (strlen(name) > MAX_DEVICE_LEN) { // too long return; } if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "%s/%s", base, name) == -1) { return; } - MakeDir(base, 0755); - if (!strncmp(event->path, "/devices/", 9)) { + MakeDir(base, DEFAULT_DIR_MODE); + if (!strncmp(event->path, "/devices/", DEV_PLAT_FORM)) { links = ParsePlatformBlockDevice(event); } - HandleDevice(event->action, devpath, event->path, 1, event->major, event->minor, links); + HandleDevice(event, devpath, 1, links); } static void AddPlatformDevice(const char *path) @@ -500,10 +541,10 @@ static void AddPlatformDevice(const char *path) size_t pathLen = strlen(path); const char *name = path; - if (!strncmp(path, "/devices/", 9)) { - name += 9; - if (!strncmp(name, "platform/", 9)) { - name += 9; + if (!strncmp(path, "/devices/", DEV_PLAT_FORM)) { + name += DEV_PLAT_FORM; + if (!strncmp(name, "platform/", DEV_PLAT_FORM)) { + name += DEV_PLAT_FORM; } } printf("adding platform device %s (%s)\n", name, path); @@ -567,15 +608,11 @@ static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) int width; struct PlatformNode *pDev = FindPlatformDevice(uevent->path); - if (!pDev) { - return NULL; - } + CHECK_RETURN(pDev, return NULL); - char **links = malloc(sizeof(char *) * 2); - if (!links) { - return NULL; - } - if (memset_s(links, sizeof(char *) * 2, 0, sizeof(char *) * 2) != 0) { + char **links = malloc(sizeof(char *) * SYS_LINK_NUMBER); + CHECK_RETURN(links, return NULL); + if (memset_s(links, sizeof(char *) * SYS_LINK_NUMBER, 0, sizeof(char *) * SYS_LINK_NUMBER) != 0) { return NULL; } @@ -585,11 +622,19 @@ static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) goto err; } - if (!strncmp(parent, "/usb", 4)) { + if (!strncmp(parent, "/usb", DEV_USB)) { /* skip root hub name and device. use device interface */ - while (*++parent && *parent != '/') {} + while (*++parent) { + if (*parent == '/') { + break; + } + } if (*parent) { - while (*++parent && *parent != '/') {} + while (*++parent) { + if (*parent == '/') { + break; + } + } } if (!*parent) { goto err; @@ -608,7 +653,7 @@ static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) } else { links[linkNum] = NULL; } - mkdir("/dev/usb", 0755); + mkdir("/dev/usb", DEFAULT_DIR_MODE); } else { goto err; } @@ -618,97 +663,106 @@ err: return NULL; } +static int HandleUsbDevice(const struct Uevent *event, char *devpath, int len) +{ + if (event->deviceName) { + /* + * create device node provided by kernel if present + * see drivers/base/core.c + */ + char *p = devpath; + if (snprintf_s(devpath, len, len, "/dev/%s", event->deviceName) == -1) { + return -1; + } + /* skip leading /dev/ */ + p += DEVICE_SKIP; + /* build directories */ + while (*p) { + if (*p == '/') { + *p = 0; + MakeDir(devpath, DEFAULT_DIR_MODE); + *p = '/'; + } + p++; + } + } else { + /* This imitates the file system that would be created + * if we were using devfs instead. + * Minors are broken up into groups of 128, starting at "001" + */ + int busId = event->minor / MINORS_GROUPS + 1; + int deviceId = event->minor % MINORS_GROUPS + 1; + /* build directories */ + MakeDir("/dev/bus", DEFAULT_DIR_MODE); + MakeDir("/dev/bus/usb", DEFAULT_DIR_MODE); + if (snprintf_s(devpath, len, len, "/dev/bus/usb/%03d", busId) == -1) { + return -1; + } + MakeDir(devpath, DEFAULT_DIR_MODE); + if (snprintf_s(devpath, len, len, "/dev/bus/usb/%03d/%03d", busId, + deviceId) == -1) { + return -1; + } + } + return 0; +} + +static void HandleDeviceEvent(struct Uevent *event, char *devpath, int len, const char *base, const char *name) +{ + char **links = NULL; + links = GetCharacterDeviceSymlinks(event); + if (!devpath[0]) { + if (snprintf_s(devpath, len, len, "%s%s", base, name) == -1) { + printf("[Init] snprintf_s err \n"); + return; + } + } + HandleDevice(event, devpath, 0, links); + return; +} static void HandleGenericDevice(struct Uevent *event) { char *base = NULL; - char devpath[96] = {0}; - char **links = NULL; - - const char *name = ParseDeviceName(event, 64); + char devpath[MAX_DEV_PATH] = {0}; + const char *name = ParseDeviceName(event, MAX_DEVICE_LEN); if (!name) { return; } - if (!strncmp(event->subsystem, "usb", 3)) { + if (!strncmp(event->subsystem, "usb", HANDLE_DEVICE_USB)) { if (!strcmp(event->subsystem, "usb")) { - if (event->deviceName) { - /* - * create device node provided by kernel if present - * see drivers/base/core.c - */ - char *p = devpath; - if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "/dev/%s", event->deviceName) == -1) { - return; - } - /* skip leading /dev/ */ - p += 5; - /* build directories */ - while (*p) { - if (*p == '/') { - *p = 0; - MakeDir(devpath, 0755); - *p = '/'; - } - p++; - } - } else { - /* This imitates the file system that would be created - * if we were using devfs instead. - * Minors are broken up into groups of 128, starting at "001" - */ - int busId = event->minor / 128 + 1; - int deviceId = event->minor % 128 + 1; - /* build directories */ - MakeDir("/dev/bus", 0755); - MakeDir("/dev/bus/usb", 0755); - if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "/dev/bus/usb/%03d", busId) == -1) { - return; - } - MakeDir(devpath, 0755); - if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "/dev/bus/usb/%03d/%03d", busId, - deviceId) == -1) { - return; - } + if (HandleUsbDevice(event, devpath, MAX_DEV_PATH) == -1) { + return; } } else { /* ignore other USB events */ return; } - } else if (!strncmp(event->subsystem, "graphics", 8)) { + } else if (!strncmp(event->subsystem, "graphics", DEV_GRAPHICS)) { base = "/dev/graphics/"; - MakeDir(base, 0755); - } else if (!strncmp(event->subsystem, "drm", 3)) { + MakeDir(base, DEFAULT_DIR_MODE); + } else if (!strncmp(event->subsystem, "drm", DEV_DRM)) { base = "/dev/dri/"; - MakeDir(base, 0755); - } else if (!strncmp(event->subsystem, "oncrpc", 6)) { + MakeDir(base, DEFAULT_DIR_MODE); + } else if (!strncmp(event->subsystem, "oncrpc", DEV_ONCRPC)) { base = "/dev/oncrpc/"; - MakeDir(base, 0755); - } else if (!strncmp(event->subsystem, "adsp", 4)) { + MakeDir(base, DEFAULT_DIR_MODE); + } else if (!strncmp(event->subsystem, "adsp", DEV_ADSP)) { base = "/dev/adsp/"; - MakeDir(base, 0755); - } else if (!strncmp(event->subsystem, "input", 5)) { + MakeDir(base, DEFAULT_DIR_MODE); + } else if (!strncmp(event->subsystem, "input", DEV_INPUT)) { base = "/dev/input/"; - MakeDir(base, 0755); - } else if (!strncmp(event->subsystem, "mtd", 3)) { + MakeDir(base, DEFAULT_DIR_MODE); + } else if (!strncmp(event->subsystem, "mtd", DEV_MTD)) { base = "/dev/mtd/"; - MakeDir(base, 0755); - } else if (!strncmp(event->subsystem, "sound", 5)) { + MakeDir(base, DEFAULT_DIR_MODE); + } else if (!strncmp(event->subsystem, "sound", DEV_SOUND)) { base = "/dev/snd/"; - MakeDir(base, 0755); - } else if (!strncmp(event->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) { - base = "/dev/log/"; - MakeDir(base, 0755); - name += 4; + MakeDir(base, DEFAULT_DIR_MODE); } else { base = "/dev/"; } - links = GetCharacterDeviceSymlinks(event); - if (!devpath[0]) { - if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "%s%s", base, name) == -1) { - return; - } - } - HandleDevice(event->action, devpath, event->path, 0, - event->major, event->minor, links); + HandleDeviceEvent(event, devpath, MAX_DEV_PATH, base, name); + return; } static void HandleDeviceUevent(struct Uevent *event) @@ -716,9 +770,9 @@ static void HandleDeviceUevent(struct Uevent *event) if (strcmp(event->action, "add") == 0 || strcmp(event->action, "change") == 0) { /* Do nothing for now */ } - if (strncmp(event->subsystem, "block", 5) == 0) { + if (strncmp(event->subsystem, "block", EVENT_BLOCK) == 0) { HandleBlockDevice(event); - } else if (strncmp(event->subsystem, "platform", 8) == 0) { + } else if (strncmp(event->subsystem, "platform", EVENT_PLAT_FORM) == 0) { HandlePlatformDevice(event); } else { HandleGenericDevice(event); @@ -727,11 +781,11 @@ static void HandleDeviceUevent(struct Uevent *event) static void HandleUevent() { - char buf[1024 + 2]; + char buf[EVENT_MAX_BUFFER]; int ret; struct Uevent event; - while ((ret = ReadUevent(g_ueventFD, buf, 1024)) > 0) { - if (ret >= 1024) { + while ((ret = ReadUevent(g_ueventFD, buf, BASE_BUFFER_SIZE)) > 0) { + if (ret >= BASE_BUFFER_SIZE) { continue; } buf[ret] = '\0'; @@ -760,7 +814,7 @@ void UeventInit() return; } -int main(int argc, char **argv) +int main(const int argc, const char **argv) { printf("Uevent demo starting...\n"); UeventInit(); diff --git a/services/test/unittest/common/cmd_func_test.cpp b/services/test/unittest/common/cmd_func_test.cpp index 676403b7e..41eb980e8 100644 --- a/services/test/unittest/common/cmd_func_test.cpp +++ b/services/test/unittest/common/cmd_func_test.cpp @@ -135,7 +135,7 @@ public: ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level0) { // do not crash ParseCmdLine(nullptr, nullptr); @@ -147,7 +147,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -175,7 +175,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -193,7 +193,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -234,7 +234,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -266,7 +266,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0) { // do not crash here DoCmd(nullptr); @@ -278,7 +278,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level0) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -297,7 +297,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level0) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -341,7 +341,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F732P **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -393,7 +393,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F732P **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -434,7 +434,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F732P **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0) { CmdLine curCmdLine; @@ -499,7 +499,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level1) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level1) +HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level0) { struct stat fileStat = {0}; EXPECT_EQ(0, stat(CFG_FILE.c_str(), &fileStat)); @@ -764,7 +764,7 @@ static void CheckJobs(const cJSON* fileRoot) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level1) +HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level0) { char* fileBuf = ReadFileToBuf(); if (fileBuf == nullptr) { @@ -814,7 +814,7 @@ static void CreateIllegalCfg() * @tc.type: FUNC * @tc.require: AR000F861Q */ -HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -830,7 +830,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level1) * @tc.type: FUNC * @tc.require: AR000F861Q */ -HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0) { CmdLine curCmdLine; std::string cmdStr = "loadcfg "; @@ -868,7 +868,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level1) * @tc.type: FUNC * @tc.require: AR000F861Q */ -HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0) { CmdLine curCmdLine; std::string cmdStr = "loadcfg "; @@ -916,7 +916,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level1) * @tc.type: FUNC * @tc.require: AR000F733F */ -HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0) { // functions do not crash ParseAllJobs(nullptr); @@ -938,7 +938,7 @@ HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level1) * @tc.type: FUNC * @tc.require: AR000F733F */ -HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level1) +HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level0) { std::string cfgJson = "{\"jobs\":[{\"name\":\"pre-init\",\"cmds\":[\"mkdir " + PRE_INIT_DIR + "\"]},{\"name\":\"init\",\"cmds\":[\"mkdir " + INIT_DIR + -- Gitee From 67c1e1147f7545aca39c97d0941102fdc5c45802 Mon Sep 17 00:00:00 2001 From: leon Date: Fri, 23 Apr 2021 20:12:47 +0800 Subject: [PATCH 05/21] fix coding style --- services/src/init_cmds.c | 6 ++-- services/src/uevent.c | 60 +++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 38 deletions(-) diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index 14ddcedbb..2c78cbf97 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -39,7 +39,7 @@ #define LOADCFG_BUF_SIZE 128 // loadcfg, max buffer for one cmdline #define LOADCFG_MAX_FILE_LEN 51200 // loadcfg, max file size is 50K #define LOADCFG_MAX_LOOP 20 // loadcfg, to prevent to be trapped in infite loop -#define OCTAL_TYPE 8 //8 means octal to decimal +#define OCTAL_TYPE 8 // 8 means octal to decimal static const char *g_supportCfg[] = { "/patch/fstab.cfg", }; @@ -321,7 +321,7 @@ static void DoMount(const char* cmdContent) } #ifndef OHOS_LITE -#define OPTIONS_SIZE (128u) +#define OPTIONS_SIZE 128u static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPtr, int flags) { int fd = -1; @@ -332,7 +332,7 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", secondPtr, restPtr) == -1) { goto out; } - } else { + } else if (secondPtr != NULL) { if (strncpy_s(options, OPTIONS_SIZE - 1, secondPtr, strlen(secondPtr)) != 0) { goto out; } diff --git a/services/src/uevent.c b/services/src/uevent.c index 9e91dd6f0..a75997c60 100755 --- a/services/src/uevent.c +++ b/services/src/uevent.c @@ -70,18 +70,6 @@ int g_ueventFD = -1; -#define CHECK_RESULT_DONE(ret, do, another) \ - if (ret) { \ - do; \ - } else { \ - another; \ - } - -#define CHECK_RETURN(ret, statement) \ - if (!(ret)) { \ - statement; \ - } - struct Uevent { const char *action; const char *path; @@ -106,7 +94,7 @@ static struct ListNode g_platformNames = { .prev = &g_platformNames, }; -const char *TRIGGER = "/dev/.trigger_uevent"; +const char *g_trigger = "/dev/.trigger_uevent"; static void HandleUevent(); static int UeventFD() @@ -156,14 +144,14 @@ void Trigger(const char *sysPath) static void RetriggerUevent() { - if (access(TRIGGER, F_OK) == 0) { + if (access(g_trigger, F_OK) == 0) { printf("Skip trigger uevent, alread done\n"); return; } Trigger("/sys/class"); Trigger("/sys/block"); Trigger("/sys/devices"); - int fd = open(TRIGGER, O_WRONLY | O_CREAT | O_CLOEXEC, DEFAULT_MODE); + int fd = open(g_trigger, O_WRONLY | O_CREAT | O_CLOEXEC, DEFAULT_MODE); if (fd > 0) { close(fd); } @@ -347,15 +335,15 @@ static char **ParsePlatformBlockDevice(const struct Uevent *uevent) char *p = NULL; struct PlatformNode *pDev = FindPlatformDevice(uevent->path); - CHECK_RESULT_DONE(pDev, device = pDev->name; type = "platform", printf("Non platform device.\n"); return NULL); - char **links = malloc(sizeof(char *) * LINK_NUMBER); - if (!links) { + if (!pDev) { return NULL; } - if (memset_s(links, sizeof(char *) * LINK_NUMBER, 0, sizeof(char *) * LINK_NUMBER) != 0) { + device = pDev->name; + type = "platform"; + char **links = calloc(sizeof(char *), LINK_NUMBER); + if (!links) { return NULL; } - printf("found %s device %s\n", type, device); if (snprintf_s(linkPath, sizeof(linkPath), sizeof(linkPath), "/dev/block/%s/%s", type, device) == -1) { return NULL; } @@ -601,6 +589,16 @@ static const char *ParseDeviceName(const struct Uevent *uevent, unsigned int len return name; } +static void FindCharEnd(const char *parent) +{ + while (*++parent) { + if (*parent == '/') { + return; + } + } + return; +} + static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) { char *slash = NULL; @@ -608,11 +606,11 @@ static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) int width; struct PlatformNode *pDev = FindPlatformDevice(uevent->path); - CHECK_RETURN(pDev, return NULL); - - char **links = malloc(sizeof(char *) * SYS_LINK_NUMBER); - CHECK_RETURN(links, return NULL); - if (memset_s(links, sizeof(char *) * SYS_LINK_NUMBER, 0, sizeof(char *) * SYS_LINK_NUMBER) != 0) { + if (!pDev) { + return NULL; + } + char **links = calloc(sizeof(char *), SYS_LINK_NUMBER); + if (!links) { return NULL; } @@ -624,17 +622,9 @@ static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) if (!strncmp(parent, "/usb", DEV_USB)) { /* skip root hub name and device. use device interface */ - while (*++parent) { - if (*parent == '/') { - break; - } - } + FindCharEnd(parent); if (*parent) { - while (*++parent) { - if (*parent == '/') { - break; - } - } + FindCharEnd(parent); } if (!*parent) { goto err; -- Gitee From 365843af6ccf33aeac2ca1f3e776d61d58c23af9 Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 24 Apr 2021 12:12:52 +0800 Subject: [PATCH 06/21] Fix bug about build --- services/test/unittest/common/BUILD.gn | 72 ++++++++++++++------------ 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/services/test/unittest/common/BUILD.gn b/services/test/unittest/common/BUILD.gn index e9b94934e..a692ab44a 100644 --- a/services/test/unittest/common/BUILD.gn +++ b/services/test/unittest/common/BUILD.gn @@ -10,41 +10,49 @@ # 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("//build/lite/config/component/lite_component.gni") -import("//build/lite/config/test.gni") +if (defined(ohos_lite)) { + import("//build/lite/config/component/lite_component.gni") + import("//build/lite/config/test.gni") -unittest("init_test") { - output_extension = "bin" - output_dir = "$root_out_dir/test/unittest/startup" - ldflags = [ - "-lstdc++", - "-lpthread", - "-lm", - ] - defines = ["OHOS_LITE"] - if (storage_type == "emmc") { - defines += [ "USE_EMMC_STORAGE" ] - } + unittest("init_test") { + output_extension = "bin" + output_dir = "$root_out_dir/test/unittest/startup" + ldflags = [ + "-lstdc++", + "-lpthread", + "-lm", + ] + defines = ["OHOS_LITE"] + if (storage_type == "emmc") { + defines += [ "USE_EMMC_STORAGE" ] + } - include_dirs = [ "//base/startup/init_lite/services/include" ] + include_dirs = [ "//base/startup/init_lite/services/include" ] - sources = [ - "//base/startup/init_lite/services/src/init_adapter.c", - "//base/startup/init_lite/services/src/init_cmds.c", - "//base/startup/init_lite/services/src/init_jobs.c", - "//base/startup/init_lite/services/src/init_service.c", - "//base/startup/init_lite/services/src/init_service_manager.c", - "//base/startup/init_lite/services/src/init_signal_handler.c", - "cmd_func_test.cpp", - ] + sources = [ + "//base/startup/init_lite/services/src/init_adapter.c", + "//base/startup/init_lite/services/src/init_cmds.c", + "//base/startup/init_lite/services/src/init_jobs.c", + "//base/startup/init_lite/services/src/init_service.c", + "//base/startup/init_lite/services/src/init_service_manager.c", + "//base/startup/init_lite/services/src/init_signal_handler.c", + "cmd_func_test.cpp", + ] - deps = [ - "//base/startup/syspara_lite/frameworks/parameter:parameter", - "//build/lite/config/component/cJSON:cjson_shared", - "//third_party/bounds_checking_function:libsec_shared", - ] -} + deps = [ + "//base/startup/syspara_lite/frameworks/parameter:parameter", + "//build/lite/config/component/cJSON:cjson_shared", + "//third_party/bounds_checking_function:libsec_shared", + ] + } -group("unittest") { - deps = [ ":init_test" ] + group("unittest") { + deps = [ ":init_test" ] + } +} else { + import("//build/ohos.gni") + sources = [] + group("unittest") { + deps = [] + } } -- Gitee From b4d4dffe8d7914fab7d1a49b2e2faf6d9d0dd9cd Mon Sep 17 00:00:00 2001 From: lanxueyuan Date: Sun, 25 Apr 2021 01:17:23 -0700 Subject: [PATCH 07/21] change param api Change-Id: Iab0fbe089e803dcad60c032f1ccaf73f916728af --- services/src/main.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/services/src/main.c b/services/src/main.c index acdc8e373..40ab42410 100755 --- a/services/src/main.c +++ b/services/src/main.c @@ -40,11 +40,9 @@ static const pid_t INIT_PROCESS_PID = 1; static void PrintSysInfo() { #ifdef OHOS_LITE - char* sysInfo = GetVersionId(); + const char* sysInfo = GetVersionId(); if (sysInfo != NULL) { printf("[Init] %s\n", sysInfo); - free(sysInfo); - sysInfo = NULL; return; } printf("[Init] main, GetVersionId failed!\n"); -- Gitee From 9047e7bed32c10995f7ee70f220ee8b55c084b18 Mon Sep 17 00:00:00 2001 From: lanxueyuan Date: Wed, 28 Apr 2021 10:37:48 +0800 Subject: [PATCH 08/21] change test case to lv1 --- .../test/unittest/common/cmd_func_test.cpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/services/test/unittest/common/cmd_func_test.cpp b/services/test/unittest/common/cmd_func_test.cpp index 41eb980e8..676403b7e 100644 --- a/services/test/unittest/common/cmd_func_test.cpp +++ b/services/test/unittest/common/cmd_func_test.cpp @@ -135,7 +135,7 @@ public: ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level1) { // do not crash ParseCmdLine(nullptr, nullptr); @@ -147,7 +147,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -175,7 +175,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -193,7 +193,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -234,7 +234,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -266,7 +266,7 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level1) { // do not crash here DoCmd(nullptr); @@ -278,7 +278,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -297,7 +297,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733E **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -341,7 +341,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F732P **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -393,7 +393,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F732P **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -434,7 +434,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F732P **/ -HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level1) { CmdLine curCmdLine; @@ -499,7 +499,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level1) { struct stat fileStat = {0}; EXPECT_EQ(0, stat(CFG_FILE.c_str(), &fileStat)); @@ -764,7 +764,7 @@ static void CheckJobs(const cJSON* fileRoot) ** @tc.type: FUNC ** @tc.require: AR000F733F **/ -HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level1) { char* fileBuf = ReadFileToBuf(); if (fileBuf == nullptr) { @@ -814,7 +814,7 @@ static void CreateIllegalCfg() * @tc.type: FUNC * @tc.require: AR000F861Q */ -HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level1) { CmdLine curCmdLine; memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine)); @@ -830,7 +830,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000F861Q */ -HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level1) { CmdLine curCmdLine; std::string cmdStr = "loadcfg "; @@ -868,7 +868,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000F861Q */ -HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level1) { CmdLine curCmdLine; std::string cmdStr = "loadcfg "; @@ -916,7 +916,7 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000F733F */ -HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level1) { // functions do not crash ParseAllJobs(nullptr); @@ -938,7 +938,7 @@ HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000F733F */ -HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level0) +HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level1) { std::string cfgJson = "{\"jobs\":[{\"name\":\"pre-init\",\"cmds\":[\"mkdir " + PRE_INIT_DIR + "\"]},{\"name\":\"init\",\"cmds\":[\"mkdir " + INIT_DIR + -- Gitee From 53cc76edd7a5c71a25b69b8b6bb57ac0b841b4cb Mon Sep 17 00:00:00 2001 From: yangming_ha Date: Wed, 28 Apr 2021 23:59:46 -0700 Subject: [PATCH 09/21] =?UTF-8?q?IssueNo:#I3OU23=20init=E9=80=82=E9=85=8Dl?= =?UTF-8?q?inux=E5=86=85=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/BUILD.gn | 89 +++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index b2b9065f8..eddea8ea5 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -11,14 +11,8 @@ # See the License for the specific language governing permissions and # limitations under the License. if (defined(ohos_lite)) { - import("//build/lite/config/component/lite_component.gni") - - lite_component("init_lite") { - features = [ ":init" ] - } - # feature: init - executable("init") { + executable("init_lite") { defines = [ "_GNU_SOURCE", #syscall function need this macro definition "OHOS_LITE", @@ -58,61 +52,64 @@ if (defined(ohos_lite)) { "-lm", "-lpthread", ] + deps = [ + "//third_party/mksh", + "//third_party/toybox", + ] } } if (ohos_build_type == "debug") { group("unittest") { - deps = [ "//base/startup/init_lite/services/test/unittest/common:unittest" ] + deps = + [ "//base/startup/init_lite/services/test/unittest/common:unittest" ] } } } else { import("//build/ohos.gni") ohos_executable("updaterueventd") { - sources = [ - "src/uevent.c", - "src/list.c", - ] - include_dirs = [ - "include", - "//third_party/bounds_checking_function/include", - ] - deps = [ - "//third_party/bounds_checking_function:libsec_static", - ] - install_enable = true - part_name = "updater" + sources = [ + "src/list.c", + "src/uevent.c", + ] + include_dirs = [ + "include", + "//third_party/bounds_checking_function/include", + ] + deps = [ "//third_party/bounds_checking_function:libsec_static" ] + install_enable = true + part_name = "updater" } ohos_executable("updaterinit") { - sources = [ - "src/main.c", - "src/init_cmds.c", - "src/init_jobs.c", - "src/init_read_cfg.c", - "src/init_adapter.c", - "src/init_service.c", - "src/init_service_manager.c", - "src/init_signal_handler.c", - "src/device.c", - ] - include_dirs = [ - "include", - "//third_party/cJSON", - "//third_party/bounds_checking_function/include", - ] - deps = [ - "//third_party/bounds_checking_function:libsec_static", - "//third_party/cJSON:cjson_static", - ] - install_enable = true - part_name = "updater" + sources = [ + "src/device.c", + "src/init_adapter.c", + "src/init_cmds.c", + "src/init_jobs.c", + "src/init_read_cfg.c", + "src/init_service.c", + "src/init_service_manager.c", + "src/init_signal_handler.c", + "src/main.c", + ] + include_dirs = [ + "include", + "//third_party/cJSON", + "//third_party/bounds_checking_function/include", + ] + deps = [ + "//third_party/bounds_checking_function:libsec_static", + "//third_party/cJSON:cjson_static", + ] + install_enable = true + part_name = "updater" } ohos_prebuilt_etc("init.cfg") { - source = "//device/hisilicon/hi3516dv300/updater/init.cfg" - relative_install_dir = "init" - subsystem_name = "updater" + source = "//device/hisilicon/hi3516dv300/updater/init.cfg" + relative_install_dir = "init" + subsystem_name = "updater" } } -- Gitee From 4b283592484af290c46ae371ac8337e8340e2329 Mon Sep 17 00:00:00 2001 From: yangming_ha Date: Wed, 28 Apr 2021 23:59:46 -0700 Subject: [PATCH 10/21] =?UTF-8?q?IssueNo:#I3OU23=20init=E9=80=82=E9=85=8Dl?= =?UTF-8?q?inux=E5=86=85=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/BUILD.gn | 90 +++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index b2b9065f8..fa7e41372 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -11,14 +11,9 @@ # See the License for the specific language governing permissions and # limitations under the License. if (defined(ohos_lite)) { - import("//build/lite/config/component/lite_component.gni") - - lite_component("init_lite") { - features = [ ":init" ] - } - # feature: init - executable("init") { + executable("init_lite") { + output_name = "init" defines = [ "_GNU_SOURCE", #syscall function need this macro definition "OHOS_LITE", @@ -58,61 +53,64 @@ if (defined(ohos_lite)) { "-lm", "-lpthread", ] + deps = [ + "//third_party/mksh", + "//third_party/toybox", + ] } } if (ohos_build_type == "debug") { group("unittest") { - deps = [ "//base/startup/init_lite/services/test/unittest/common:unittest" ] + deps = + [ "//base/startup/init_lite/services/test/unittest/common:unittest" ] } } } else { import("//build/ohos.gni") ohos_executable("updaterueventd") { - sources = [ - "src/uevent.c", - "src/list.c", - ] - include_dirs = [ - "include", - "//third_party/bounds_checking_function/include", - ] - deps = [ - "//third_party/bounds_checking_function:libsec_static", - ] - install_enable = true - part_name = "updater" + sources = [ + "src/list.c", + "src/uevent.c", + ] + include_dirs = [ + "include", + "//third_party/bounds_checking_function/include", + ] + deps = [ "//third_party/bounds_checking_function:libsec_static" ] + install_enable = true + part_name = "updater" } ohos_executable("updaterinit") { - sources = [ - "src/main.c", - "src/init_cmds.c", - "src/init_jobs.c", - "src/init_read_cfg.c", - "src/init_adapter.c", - "src/init_service.c", - "src/init_service_manager.c", - "src/init_signal_handler.c", - "src/device.c", - ] - include_dirs = [ - "include", - "//third_party/cJSON", - "//third_party/bounds_checking_function/include", - ] - deps = [ - "//third_party/bounds_checking_function:libsec_static", - "//third_party/cJSON:cjson_static", - ] - install_enable = true - part_name = "updater" + sources = [ + "src/device.c", + "src/init_adapter.c", + "src/init_cmds.c", + "src/init_jobs.c", + "src/init_read_cfg.c", + "src/init_service.c", + "src/init_service_manager.c", + "src/init_signal_handler.c", + "src/main.c", + ] + include_dirs = [ + "include", + "//third_party/cJSON", + "//third_party/bounds_checking_function/include", + ] + deps = [ + "//third_party/bounds_checking_function:libsec_static", + "//third_party/cJSON:cjson_static", + ] + install_enable = true + part_name = "updater" } ohos_prebuilt_etc("init.cfg") { - source = "//device/hisilicon/hi3516dv300/updater/init.cfg" - relative_install_dir = "init" - subsystem_name = "updater" + source = "//device/hisilicon/hi3516dv300/updater/init.cfg" + relative_install_dir = "init" + subsystem_name = "updater" } } -- Gitee From 14856393e21f462aaa675d98c5d6b680e3d82552 Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Thu, 29 Apr 2021 19:56:06 +0800 Subject: [PATCH 11/21] updater init --- services/src/device.c | 2 +- services/src/init_cmds.c | 16 +++++++++++---- services/src/uevent.c | 42 ++++++++++++++++++++++++++-------------- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/services/src/device.c b/services/src/device.c index 0a38fe4fd..6d30e9b22 100755 --- a/services/src/device.c +++ b/services/src/device.c @@ -13,12 +13,12 @@ * limitations under the License. */ +#include #include #include #include #include #include -#include #define DEFAULT_RW_MODE 0666 #define DEFAULT_NO_AUTHORITY_MODE 0600 diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index 2c78cbf97..f2d8e3e53 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -14,6 +14,7 @@ */ #include "init_cmds.h" + #include #include #include @@ -24,10 +25,11 @@ #include #include #ifndef OHOS_LITE -#include #include #include +#include #endif + #include "init_service_manager.h" #include "securec.h" @@ -326,7 +328,6 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt { int fd = -1; char options[OPTIONS_SIZE] = {0}; - if (flags == 0) { // '-f' option if (restPtr != NULL && secondPtr != NULL) { // Reset arugments, combine then all. if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", secondPtr, restPtr) == -1) { @@ -339,9 +340,14 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt } } else { // Only restPtr is option if (restPtr != NULL) { - strncpy_s(options, OPTIONS_SIZE - 1, restPtr, strlen(restPtr)); + if (strncpy_s(options, OPTIONS_SIZE - 1, restPtr, strlen(restPtr)) != 0) { + goto out; + } } } + if (!fileName) { + goto out; + } fd = open(fileName, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if (fd < 0) { printf("[Init] failed to open %s: %d\n", fileName, errno); @@ -352,7 +358,7 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt printf("[Init] finit_module for %s failed: %d\n", fileName, errno); } out: - if (fd > 0) { + if (fd >= 0) { close(fd); } return; @@ -379,12 +385,14 @@ static void DoInsmod(const char *cmdContent) if (memcpy_s(line, count, cmdContent, count) != EOK) { printf("[Init] memcpy failed\n"); + free(line); return; } line[count] = '\0'; do { if ((p = strtok_r(line, " ", &restPtr)) == NULL) { printf("[Init] debug, cannot get filename\n"); + free(line); return; } fileName = p; diff --git a/services/src/uevent.c b/services/src/uevent.c index a75997c60..855bda332 100755 --- a/services/src/uevent.c +++ b/services/src/uevent.c @@ -13,21 +13,22 @@ * limitations under the License. */ +#include +#include +#include +#include +#include +#include #include #include #include #include -#include -#include #include #include #include -#include -#include -#include #include -#include -#include +#include + #include "list.h" #include "securec.h" @@ -152,7 +153,7 @@ static void RetriggerUevent() Trigger("/sys/block"); Trigger("/sys/devices"); int fd = open(g_trigger, O_WRONLY | O_CREAT | O_CLOEXEC, DEFAULT_MODE); - if (fd > 0) { + if (fd >= 0) { close(fd); } printf("Re-trigger uevent done\n"); @@ -220,7 +221,7 @@ ssize_t ReadUevent(int fd, void *buf, size_t len) if (uid != 0) { goto out; } - + if (addr.nl_groups == 0 || addr.nl_pid != 0) { /* ignoring non-kernel or unicast netlink message */ goto out; @@ -344,7 +345,8 @@ static char **ParsePlatformBlockDevice(const struct Uevent *uevent) if (!links) { return NULL; } - if (snprintf_s(linkPath, sizeof(linkPath), sizeof(linkPath), "/dev/block/%s/%s", type, device) == -1) { + if (snprintf_s(linkPath, sizeof(linkPath), sizeof(linkPath) - 1, "/dev/block/%s/%s", type, device) == -1) { + free(links); return NULL; } if (uevent->partitionName) { @@ -514,7 +516,7 @@ static void HandleBlockDevice(struct Uevent *event) if (strlen(name) > MAX_DEVICE_LEN) { // too long return; } - if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "%s/%s", base, name) == -1) { + if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath) - 1, "%s/%s", base, name) == -1) { return; } MakeDir(base, DEFAULT_DIR_MODE); @@ -537,10 +539,14 @@ static void AddPlatformDevice(const char *path) } printf("adding platform device %s (%s)\n", name, path); struct PlatformNode *bus = calloc(1, sizeof(struct PlatformNode)); + if (!bus) { + return; + } bus->path = strdup(path); bus->pathLen = pathLen; bus->name = bus->path + (name - path); ListAddTail(&g_platformNames, &bus->list); + return; } static void RemovePlatformDevice(const char *path) @@ -661,7 +667,7 @@ static int HandleUsbDevice(const struct Uevent *event, char *devpath, int len) * see drivers/base/core.c */ char *p = devpath; - if (snprintf_s(devpath, len, len, "/dev/%s", event->deviceName) == -1) { + if (snprintf_s(devpath, len, len - 1, "/dev/%s", event->deviceName) == -1) { return -1; } /* skip leading /dev/ */ @@ -702,13 +708,21 @@ static void HandleDeviceEvent(struct Uevent *event, char *devpath, int len, cons char **links = NULL; links = GetCharacterDeviceSymlinks(event); if (!devpath[0]) { - if (snprintf_s(devpath, len, len, "%s%s", base, name) == -1) { + if (snprintf_s(devpath, len, len - 1, "%s%s", base, name) == -1) { printf("[Init] snprintf_s err \n"); - return; + goto err; } } HandleDevice(event, devpath, 0, links); return; +err: + if (links) { + for (int i = 0; links[i]; i++) { + free(links[i]); + } + free(links); + } + return; } static void HandleGenericDevice(struct Uevent *event) { -- Gitee From 4e73ac48bfa6ea2dac5264c8cb4d1d90f8191924 Mon Sep 17 00:00:00 2001 From: lanxueyuan Date: Thu, 29 Apr 2021 05:02:13 -0700 Subject: [PATCH 12/21] fix code style check Change-Id: Ib2fb33387128fed17166091c907f85f6dd7bf6af --- services/src/device.c | 8 ++++---- services/src/init_cmds.c | 2 +- services/src/uevent.c | 22 +++++++++++----------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/services/src/device.c b/services/src/device.c index 0a38fe4fd..84013a2cb 100755 --- a/services/src/device.c +++ b/services/src/device.c @@ -13,12 +13,12 @@ * limitations under the License. */ -#include -#include -#include +#include #include #include -#include +#include +#include +#include #define DEFAULT_RW_MODE 0666 #define DEFAULT_NO_AUTHORITY_MODE 0600 diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index 2c78cbf97..dd63ca085 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -24,9 +24,9 @@ #include #include #ifndef OHOS_LITE -#include #include #include +#include #endif #include "init_service_manager.h" #include "securec.h" diff --git a/services/src/uevent.c b/services/src/uevent.c index a75997c60..54c023011 100755 --- a/services/src/uevent.c +++ b/services/src/uevent.c @@ -13,21 +13,21 @@ * limitations under the License. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include #include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include -#include #include "list.h" #include "securec.h" -- Gitee From 7d92c42c37d52db2b549d0a65f4d7ca641abd4d7 Mon Sep 17 00:00:00 2001 From: yangming_ha Date: Thu, 6 May 2021 11:09:21 +0800 Subject: [PATCH 13/21] fix linux compile issue --- services/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index fa7e41372..c6224ce1b 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -53,7 +53,7 @@ if (defined(ohos_lite)) { "-lm", "-lpthread", ] - deps = [ + deps += [ "//third_party/mksh", "//third_party/toybox", ] -- Gitee From 7e703f4da0ad2dbb62e3e42ffd44b2a96edf2f27 Mon Sep 17 00:00:00 2001 From: boxi Date: Thu, 29 Apr 2021 14:31:46 +0800 Subject: [PATCH 14/21] feat: Add the init_stage module in startup_init_lite. Add the init_stage module to provide synchronous communication between user mode processes and init process. init stage0-------1fork----> key app |2 |3 (listen)<---------4--------- (notify) |5 init stage1-------6fork----> other app Close #I3OTSU Change-Id: Idec84e90f739475e98347a6a681a6953862b0928 --- initstage/BUILD.gn | 36 +++++++++++++ initstage/include/init_notify.h | 38 ++++++++++++++ initstage/include/init_stage.h | 92 +++++++++++++++++++++++++++++++++ initstage/src/init_stage.c | 67 ++++++++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 initstage/BUILD.gn create mode 100644 initstage/include/init_notify.h create mode 100644 initstage/include/init_stage.h create mode 100644 initstage/src/init_stage.c diff --git a/initstage/BUILD.gn b/initstage/BUILD.gn new file mode 100644 index 000000000..533dc7a9c --- /dev/null +++ b/initstage/BUILD.gn @@ -0,0 +1,36 @@ +# 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. + +import("//build/lite/config/component/lite_component.gni") + +lite_component("initstage") { + features = [ "//base/startup/init_lite/initstage:libinitstage_shared" ] +} + +shared_library("libinitstage_shared") { + sources = [ "src/init_stage.c" ] + cflags = [ "-Wall" ] + include_dirs = [ + "//base/startup/init_lite/initstage/include", + ] + public_deps = [ "//third_party/bounds_checking_function:libsec_shared" ] +} + +static_library("libinitstage_static") { + sources = [ "src/init_stage.c" ] + cflags = [ "-Wall" ] + include_dirs = [ + "//base/startup/init_lite/initstage/include", + ] + public_deps = [ "//third_party/bounds_checking_function:libsec_static" ] +} diff --git a/initstage/include/init_notify.h b/initstage/include/init_notify.h new file mode 100644 index 000000000..95032ad77 --- /dev/null +++ b/initstage/include/init_notify.h @@ -0,0 +1,38 @@ +/* + * 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 BASE_STARTUP_INITLITE_NOTIFY_H +#define BASE_STARTUP_INITLITE_NOTIFY_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +/* + * Notify the event to Init process. + * All processes can call. Usually called by the listened process. + * event: There needs to be a consensus between the listener(init process) and the notifier. + */ +extern int NotifyInit(unsigned short event); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif + +#endif // BASE_STARTUP_INITLITE_NOTIFY_H diff --git a/initstage/include/init_stage.h b/initstage/include/init_stage.h new file mode 100644 index 000000000..09341dc3c --- /dev/null +++ b/initstage/include/init_stage.h @@ -0,0 +1,92 @@ +/* + * 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 BASE_STARTUP_INITLITE_STAGE_H +#define BASE_STARTUP_INITLITE_STAGE_H + +#include +#include + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +typedef enum { + QS_STAGE1 = 1, /* 1: start from stage1, 0 is already called in kernel process */ + QS_STAGE2, /* system init stage No 2 */ + QS_STAGE3, /* system init stage No 3 */ + QS_STAGE_LIMIT +} QuickstartStage; + +typedef enum { + QS_UNREGISTER = QS_STAGE_LIMIT, /* quickstart dev unregister */ + QS_NOTIFY, /* quickstart notify */ + QS_LISTEN, /* quickstart listen */ + QS_CTL_LIMIT +} QuickstartConctrl; + +typedef struct { + unsigned int pid; + unsigned int events; +} QuickstartMask; + +#define QUICKSTART_IOC_MAGIC 'T' +#define QUICKSTART_UNREGISTER _IO(QUICKSTART_IOC_MAGIC, QS_UNREGISTER) +#define QUICKSTART_NOTIFY _IO(QUICKSTART_IOC_MAGIC, QS_NOTIFY) +#define QUICKSTART_LISTEN _IOR(QUICKSTART_IOC_MAGIC, QS_LISTEN, QuickstartMask) +#define QUICKSTART_STAGE(x) _IO(QUICKSTART_IOC_MAGIC, (x)) + +#define QUICKSTART_NODE "/dev/quickstart" + +/* Simple sample Useage: + * INIT PROCESS + * SystemInitStage(QS_STAGE1)----(1)fork----> key APP + * |(2) |(3) + * InitListen<-------------------(4)---------- NotifyInit + * |(5) + * SystemInitStage(QS_STAGE2)----(6)fork---------------------> other APP + * |... + * InitStageFinished + */ + +/* + * Listen the events of a specific pid process by Init process. + * Only be called by Init process. + * eventMask: There needs to be a consensus between the listener and the notifier. + + */ +extern int InitListen(pid_t pid, unsigned short eventMask); + +/* + * Trigger the SystemInit stage. + * Only be called by Init process. + */ +extern int SystemInitStage(QuickstartStage stage); + +/* + * InitStage finished. + * Only be called by Init process. + */ +extern int InitStageFinished(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif + +#endif // BASE_STARTUP_INITLITE_STAGE_H diff --git a/initstage/src/init_stage.c b/initstage/src/init_stage.c new file mode 100644 index 000000000..bffb2697f --- /dev/null +++ b/initstage/src/init_stage.c @@ -0,0 +1,67 @@ +/* + * 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 "init_stage.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int SendCmd(int cmd, unsigned long arg) +{ + int fd = open(QUICKSTART_NODE, O_RDONLY); + if (fd != -1) { + int ret = ioctl(fd, cmd, arg); + if (ret == -1) { + printf("[%s][%d]Err: %s!\n", __FUNCTION__, __LINE__, strerror(errno)); + } + close(fd); + return ret; + } + printf("[%s][%d]Err: %s!\n", __FUNCTION__, __LINE__, strerror(errno)); + return fd; +} + +int InitListen(pid_t pid, unsigned short eventMask) +{ + QuickstartMask listenMask; + listenMask.pid = pid; + listenMask.events = eventMask; + return SendCmd(QUICKSTART_LISTEN, (unsigned long)&listenMask); +} + +int NotifyInit(unsigned short event) +{ + return SendCmd(QUICKSTART_NOTIFY, event); +} + +int SystemInitStage(QuickstartStage stage) +{ + if (stage >= QS_STAGE_LIMIT || stage < QS_STAGE1) { + printf("[%s][%d]Err: the stage(%d) is Not expected!!\n", __FUNCTION__, __LINE__, stage); + return -1; + } + return SendCmd(QUICKSTART_STAGE(stage), 0); +} + +int InitStageFinished(void) +{ + return SendCmd(QUICKSTART_UNREGISTER, 0); +} -- Gitee From 3b1104c54416a08f19e343b4cb3bbe86dcdb681e Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Sat, 8 May 2021 16:37:18 +0800 Subject: [PATCH 15/21] modify for updater --- services/src/init_cmds.c | 23 ++++--- services/src/uevent.c | 133 +++++++++++++++++---------------------- 2 files changed, 72 insertions(+), 84 deletions(-) diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index f2d8e3e53..a79e0fe2f 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -326,38 +326,41 @@ static void DoMount(const char* cmdContent) #define OPTIONS_SIZE 128u static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPtr, int flags) { - int fd = -1; char options[OPTIONS_SIZE] = {0}; if (flags == 0) { // '-f' option if (restPtr != NULL && secondPtr != NULL) { // Reset arugments, combine then all. if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", secondPtr, restPtr) == -1) { - goto out; + return; } } else if (secondPtr != NULL) { if (strncpy_s(options, OPTIONS_SIZE - 1, secondPtr, strlen(secondPtr)) != 0) { - goto out; + return; } } } else { // Only restPtr is option if (restPtr != NULL) { if (strncpy_s(options, OPTIONS_SIZE - 1, restPtr, strlen(restPtr)) != 0) { - goto out; + return; } } } if (!fileName) { - goto out; + return; } - fd = open(fileName, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); + char *realPath = NULL; + realPath = realpath(fileName, realPath); + if (realPath == NULL) { + return; + } + int fd = open(realPath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if (fd < 0) { - printf("[Init] failed to open %s: %d\n", fileName, errno); - goto out; + printf("[Init] failed to open %s: %d\n", realPath, errno); + return; } int rc = syscall(__NR_finit_module, fd, options, flags); if (rc == -1) { - printf("[Init] finit_module for %s failed: %d\n", fileName, errno); + printf("[Init] finit_module for %s failed: %d\n", realPath, errno); } -out: if (fd >= 0) { close(fd); } diff --git a/services/src/uevent.c b/services/src/uevent.c index 3594d19ed..ddb31f485 100755 --- a/services/src/uevent.c +++ b/services/src/uevent.c @@ -102,32 +102,35 @@ static int UeventFD() return g_ueventFD; } -static void DoTrigger(DIR *dir) +static void DoTrigger(DIR *dir, const char *path, int32_t pathLen) { - struct dirent *de = NULL; - int dfd = dirfd(dir); - int fd = openat(dfd, "uevent", O_WRONLY); + if (pathLen < 0) { + return; + } + struct dirent *dirent = NULL; + char ueventPath[MAX_BUFFER]; + if (snprintf_s(ueventPath, sizeof(ueventPath), sizeof(ueventPath) - 1, "%s/uevent", path) == -1) { + return; + } + int fd = open(ueventPath, O_WRONLY); if (fd >= 0) { write(fd, "add\n", TRIGGER_ADDR_SIZE); close(fd); HandleUevent(); } - while ((de = readdir(dir)) != NULL) { - DIR *dir2 = NULL; - if (de->d_type != DT_DIR || de->d_name[0] == '.') { + while ((dirent = readdir(dir)) != NULL) { + if (dirent->d_name[0] == '.' || dirent->d_type != DT_DIR) { continue; } - fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); - if (fd < 0) { + char tmpPath[MAX_BUFFER]; + if (snprintf_s(tmpPath, sizeof(tmpPath), sizeof(tmpPath) - 1, "%s/%s", path, dirent->d_name) == -1) { continue; } - dir2 = fdopendir(fd); - if (dir2 == NULL) { - close(fd); - } else { - DoTrigger(dir2); + DIR *dir2 = opendir(tmpPath); + if (dir2) { + DoTrigger(dir2, tmpPath, strlen(tmpPath)); closedir(dir2); } } @@ -137,7 +140,7 @@ void Trigger(const char *sysPath) { DIR *dir = opendir(sysPath); if (dir) { - DoTrigger(dir); + DoTrigger(dir, sysPath, strlen(sysPath)); closedir(dir); } } @@ -197,7 +200,6 @@ ssize_t ReadUevent(int fd, void *buf, size_t len) struct iovec iov = { buf, len }; struct sockaddr_nl addr; char control[CMSG_SPACE(sizeof(struct ucred))]; - uid_t uid = -1; struct msghdr hdr = { &addr, sizeof(addr), @@ -213,23 +215,24 @@ ssize_t ReadUevent(int fd, void *buf, size_t len) } struct cmsghdr *cMsg = CMSG_FIRSTHDR(&hdr); if (cMsg == NULL || cMsg->cmsg_type != SCM_CREDENTIALS) { - goto out; + bzero(buf, len); + errno = -EIO; + return n; } struct ucred *cRed = (struct ucred *)CMSG_DATA(cMsg); - uid = cRed->uid; - if (uid != 0) { - goto out; + if (cRed->uid != 0) { + bzero(buf, len); + errno = -EIO; + return n; } if (addr.nl_groups == 0 || addr.nl_pid != 0) { /* ignoring non-kernel or unicast netlink message */ - goto out; + bzero(buf, len); + errno = -EIO; + return n; } - return n; -out: - bzero(buf, len); - errno = -EIO; return n; } @@ -305,22 +308,19 @@ static struct PlatformNode *FindPlatformDevice(const char *path) return NULL; } -static void Sanitize(char *s) +static void CheckValidPartitionName(char *partitionName) { - const char* accept = - "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "0123456789" - "_-."; - - if (!s) { + const char* supportPartition = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-"; + if (!partitionName) { return; } - for (; *s; s++) { - s += strspn(s, accept); - if (*s) { - *s = '_'; + for (size_t i = 0; i < strlen(partitionName); i++) { + int len = strspn(partitionName, supportPartition); + partitionName += len; + i = len; + if (*partitionName) { + *partitionName = '_'; } } } @@ -350,7 +350,7 @@ static char **ParsePlatformBlockDevice(const struct Uevent *uevent) } if (uevent->partitionName) { p = strdup(uevent->partitionName); - Sanitize(p); + CheckValidPartitionName(p); if (strcmp(uevent->partitionName, p)) { printf("Linking partition '%s' as '%s'\n", uevent->partitionName, p); } @@ -594,16 +594,6 @@ static const char *ParseDeviceName(const struct Uevent *uevent, unsigned int len return name; } -static void FindCharEnd(const char *parent) -{ - while (*++parent) { - if (*parent == '/') { - return; - } - } - return; -} - static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) { char *slash = NULL; @@ -625,33 +615,28 @@ static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) goto err; } - if (!strncmp(parent, "/usb", DEV_USB)) { - /* skip root hub name and device. use device interface */ - FindCharEnd(parent); - if (*parent) { - FindCharEnd(parent); - } - if (!*parent) { - goto err; - } - slash = strchr(++parent, '/'); - if (!slash) { - goto err; - } - width = slash - parent; - if (width <= 0) { - goto err; - } + if (strncmp(parent, "/usb", DEV_USB)) { + goto err; + } + /* skip root hub name and device. use device interface */ + if (!*parent) { + goto err; + } + slash = strchr(++parent, '/'); + if (!slash) { + goto err; + } + width = slash - parent; + if (width <= 0) { + goto err; + } - if (asprintf(&links[linkNum], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) { - linkNum++; - } else { - links[linkNum] = NULL; - } - mkdir("/dev/usb", DEFAULT_DIR_MODE); + if (asprintf(&links[linkNum], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) { + linkNum++; } else { - goto err; + links[linkNum] = NULL; } + mkdir("/dev/usb", DEFAULT_DIR_MODE); return links; err: free(links); @@ -690,11 +675,11 @@ static int HandleUsbDevice(const struct Uevent *event, char *devpath, int len) /* build directories */ MakeDir("/dev/bus", DEFAULT_DIR_MODE); MakeDir("/dev/bus/usb", DEFAULT_DIR_MODE); - if (snprintf_s(devpath, len, len, "/dev/bus/usb/%03d", busId) == -1) { + if (snprintf_s(devpath, len, len - 1, "/dev/bus/usb/%03d", busId) == -1) { return -1; } MakeDir(devpath, DEFAULT_DIR_MODE); - if (snprintf_s(devpath, len, len, "/dev/bus/usb/%03d/%03d", busId, + if (snprintf_s(devpath, len, len - 1, "/dev/bus/usb/%03d/%03d", busId, deviceId) == -1) { return -1; } -- Gitee From a0aff5e03a465e4d21155c223e4625f3197ff3a7 Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Sat, 8 May 2021 17:33:40 +0800 Subject: [PATCH 16/21] fix codestyle --- services/src/init_cmds.c | 7 +++++-- services/src/uevent.c | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index a79e0fe2f..1c0c79f9e 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -42,6 +42,7 @@ #define LOADCFG_MAX_FILE_LEN 51200 // loadcfg, max file size is 50K #define LOADCFG_MAX_LOOP 20 // loadcfg, to prevent to be trapped in infite loop #define OCTAL_TYPE 8 // 8 means octal to decimal +#define MAX_BUFFER 256 static const char *g_supportCfg[] = { "/patch/fstab.cfg", }; @@ -347,14 +348,15 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt if (!fileName) { return; } - char *realPath = NULL; - realPath = realpath(fileName, realPath); + char *realPath = (char *)calloc(MAX_BUFFER, sizeof(char)); if (realPath == NULL) { return; } + realPath = realpath(fileName, realPath); int fd = open(realPath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if (fd < 0) { printf("[Init] failed to open %s: %d\n", realPath, errno); + free(realPath); return; } int rc = syscall(__NR_finit_module, fd, options, flags); @@ -364,6 +366,7 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt if (fd >= 0) { close(fd); } + free(realPath); return; } diff --git a/services/src/uevent.c b/services/src/uevent.c index ddb31f485..08cf8afe1 100755 --- a/services/src/uevent.c +++ b/services/src/uevent.c @@ -616,7 +616,7 @@ static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent) } if (strncmp(parent, "/usb", DEV_USB)) { - goto err; + goto err; } /* skip root hub name and device. use device interface */ if (!*parent) { -- Gitee From df89fcf6c58ca39e1b32ec5914ddfd7be837bf11 Mon Sep 17 00:00:00 2001 From: boxi Date: Sat, 15 May 2021 10:54:12 +0800 Subject: [PATCH 17/21] feat: Rename initstage to initsync, and apply the module in the init process. 1.Rename initstage to initsync 2.Apply the module in the init process to synchronize the init process with the application process Close #I3RCUF Change-Id: Ibd384485aef411d4d638f0717d2877dbd79a3502 --- {initstage => initsync}/BUILD.gn | 18 ++++++------ {initstage => initsync}/include/init_stage.h | 21 ++++++++------ .../init_stage.c => initsync/src/init_sync.c | 28 ++++++++++++------- .../kits/init_sync.h | 13 ++++++++- services/BUILD.gn | 8 +++++- services/src/init_read_cfg.c | 17 ++++++++++- 6 files changed, 75 insertions(+), 30 deletions(-) rename {initstage => initsync}/BUILD.gn (67%) rename {initstage => initsync}/include/init_stage.h (82%) rename initstage/src/init_stage.c => initsync/src/init_sync.c (65%) rename initstage/include/init_notify.h => interfaces/kits/init_sync.h (73%) diff --git a/initstage/BUILD.gn b/initsync/BUILD.gn similarity index 67% rename from initstage/BUILD.gn rename to initsync/BUILD.gn index 533dc7a9c..86b0ad070 100644 --- a/initstage/BUILD.gn +++ b/initsync/BUILD.gn @@ -13,24 +13,26 @@ import("//build/lite/config/component/lite_component.gni") -lite_component("initstage") { - features = [ "//base/startup/init_lite/initstage:libinitstage_shared" ] +lite_component("initsync") { + features = [ "//base/startup/init_lite/initsync:libinitsync_shared" ] } -shared_library("libinitstage_shared") { - sources = [ "src/init_stage.c" ] +shared_library("libinitsync_shared") { + sources = [ "src/init_sync.c" ] cflags = [ "-Wall" ] include_dirs = [ - "//base/startup/init_lite/initstage/include", + "//base/startup/init_lite/initsync/include", + "//base/startup/init_lite/interfaces/kits", ] public_deps = [ "//third_party/bounds_checking_function:libsec_shared" ] } -static_library("libinitstage_static") { - sources = [ "src/init_stage.c" ] +static_library("libinitsync_static") { + sources = [ "src/init_sync.c" ] cflags = [ "-Wall" ] include_dirs = [ - "//base/startup/init_lite/initstage/include", + "//base/startup/init_lite/initsync/include", + "//base/startup/init_lite/interfaces/kits", ] public_deps = [ "//third_party/bounds_checking_function:libsec_static" ] } diff --git a/initstage/include/init_stage.h b/initsync/include/init_stage.h similarity index 82% rename from initstage/include/init_stage.h rename to initsync/include/init_stage.h index 09341dc3c..7d5b7848b 100644 --- a/initstage/include/init_stage.h +++ b/initsync/include/init_stage.h @@ -16,6 +16,7 @@ #ifndef BASE_STARTUP_INITLITE_STAGE_H #define BASE_STARTUP_INITLITE_STAGE_H +#include "init_sync.h" #include #include @@ -33,21 +34,19 @@ typedef enum { } QuickstartStage; typedef enum { - QS_UNREGISTER = QS_STAGE_LIMIT, /* quickstart dev unregister */ - QS_NOTIFY, /* quickstart notify */ - QS_LISTEN, /* quickstart listen */ + QS_NOTIFY = QS_STAGE_LIMIT, /* quickstart notify */ + QS_LISTEN, /* quickstart listen */ QS_CTL_LIMIT } QuickstartConctrl; typedef struct { - unsigned int pid; unsigned int events; -} QuickstartMask; + unsigned int wait; +} QuickstartListenArgs; #define QUICKSTART_IOC_MAGIC 'T' -#define QUICKSTART_UNREGISTER _IO(QUICKSTART_IOC_MAGIC, QS_UNREGISTER) #define QUICKSTART_NOTIFY _IO(QUICKSTART_IOC_MAGIC, QS_NOTIFY) -#define QUICKSTART_LISTEN _IOR(QUICKSTART_IOC_MAGIC, QS_LISTEN, QuickstartMask) +#define QUICKSTART_LISTEN _IOR(QUICKSTART_IOC_MAGIC, QS_LISTEN, QuickstartListenArgs) #define QUICKSTART_STAGE(x) _IO(QUICKSTART_IOC_MAGIC, (x)) #define QUICKSTART_NODE "/dev/quickstart" @@ -67,9 +66,8 @@ typedef struct { * Listen the events of a specific pid process by Init process. * Only be called by Init process. * eventMask: There needs to be a consensus between the listener and the notifier. - */ -extern int InitListen(pid_t pid, unsigned short eventMask); +extern int InitListen(unsigned long eventMask, unsigned int wait); /* * Trigger the SystemInit stage. @@ -83,6 +81,11 @@ extern int SystemInitStage(QuickstartStage stage); */ extern int InitStageFinished(void); +/* + * Listen event and trigger the SystemInit stage. + * Only be called by Init process. + */ +extern void TriggerStage(unsigned int event, unsigned int wait, QuickstartStage stagelevel); #ifdef __cplusplus #if __cplusplus } diff --git a/initstage/src/init_stage.c b/initsync/src/init_sync.c similarity index 65% rename from initstage/src/init_stage.c rename to initsync/src/init_sync.c index bffb2697f..00f229c3f 100644 --- a/initstage/src/init_stage.c +++ b/initsync/src/init_sync.c @@ -30,24 +30,24 @@ static int SendCmd(int cmd, unsigned long arg) if (fd != -1) { int ret = ioctl(fd, cmd, arg); if (ret == -1) { - printf("[%s][%d]Err: %s!\n", __FUNCTION__, __LINE__, strerror(errno)); + printf("[ERR][%s,%d] %s!\n", __FUNCTION__, __LINE__, strerror(errno)); } close(fd); return ret; } - printf("[%s][%d]Err: %s!\n", __FUNCTION__, __LINE__, strerror(errno)); + printf("[ERR][%s,%d] %s!\n", __FUNCTION__, __LINE__, strerror(errno)); return fd; } -int InitListen(pid_t pid, unsigned short eventMask) +int InitListen(unsigned long eventMask, unsigned int wait) { - QuickstartMask listenMask; - listenMask.pid = pid; - listenMask.events = eventMask; - return SendCmd(QUICKSTART_LISTEN, (unsigned long)&listenMask); + QuickstartListenArgs args; + args.wait = wait; + args.events = eventMask; + return SendCmd(QUICKSTART_LISTEN, (unsigned long)&args); } -int NotifyInit(unsigned short event) +int NotifyInit(unsigned long event) { return SendCmd(QUICKSTART_NOTIFY, event); } @@ -55,13 +55,21 @@ int NotifyInit(unsigned short event) int SystemInitStage(QuickstartStage stage) { if (stage >= QS_STAGE_LIMIT || stage < QS_STAGE1) { - printf("[%s][%d]Err: the stage(%d) is Not expected!!\n", __FUNCTION__, __LINE__, stage); + printf("[ERR][%s,%d] the stage(%d) is not expected!\n", __FUNCTION__, __LINE__, stage); return -1; } return SendCmd(QUICKSTART_STAGE(stage), 0); } +void TriggerStage(unsigned int event, unsigned int wait, QuickstartStage stagelevel) +{ + int ret = InitListen(event, wait); + if (ret != 0 && ret != -1) { + SystemInitStage(stagelevel); + } +} + int InitStageFinished(void) { - return SendCmd(QUICKSTART_UNREGISTER, 0); + return unlink(QUICKSTART_NODE); } diff --git a/initstage/include/init_notify.h b/interfaces/kits/init_sync.h similarity index 73% rename from initstage/include/init_notify.h rename to interfaces/kits/init_sync.h index 95032ad77..ceeefbce3 100644 --- a/initstage/include/init_notify.h +++ b/interfaces/kits/init_sync.h @@ -22,12 +22,23 @@ extern "C" { #endif #endif +#define EVENT1 0xf +#define EVENT1_WAITTIME 10000 // 10s = 10*1000 * 1 tick(1ms) + +#define EVENT2 0xf0 +/* Define EVENT2_WAITTIME 0 means QS_STAGE2 is no used */ +#define EVENT2_WAITTIME 0 + +#define EVENT3 0xf00 +/* Define EVENT3_WAITTIME 0 means QS_STAGE3 is no used */ +#define EVENT3_WAITTIME 0 + /* * Notify the event to Init process. * All processes can call. Usually called by the listened process. * event: There needs to be a consensus between the listener(init process) and the notifier. */ -extern int NotifyInit(unsigned short event); +extern int NotifyInit(unsigned long event); #ifdef __cplusplus #if __cplusplus diff --git a/services/BUILD.gn b/services/BUILD.gn index c6224ce1b..286dadf95 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -40,12 +40,18 @@ if (defined(ohos_lite)) { deps = [ "//base/startup/syspara_lite/frameworks/parameter:parameter", + "//base/startup/init_lite/initsync:initsync", "//build/lite/config/component/cJSON:cjson_shared", "//third_party/bounds_checking_function:libsec_shared", ] ldflags = [] if (ohos_kernel_type == "liteos_a") { - include_dirs += [ "//kernel/liteos_a/syscall" ] + include_dirs += [ + "//kernel/liteos_a/syscall", + "//base/startup/init_lite/interfaces/kits", + "//base/startup/init_lite/initsync/include", + ] + deps += [ "//base/startup/init_lite/initsync:initsync" ] } if (ohos_kernel_type == "linux") { defines += [ "NEED_EXEC_RCS_LINUX" ] diff --git a/services/src/init_read_cfg.c b/services/src/init_read_cfg.c index 11d122a1a..6d7d280fd 100644 --- a/services/src/init_read_cfg.c +++ b/services/src/init_read_cfg.c @@ -27,7 +27,9 @@ #include "init_perms.h" #include "init_service_manager.h" #include "securec.h" - +#ifndef __LINUX__ +#include "init_stage.h" +#endif static const long MAX_JSON_FILE_LEN = 102400; // max init.cfg size 100KB static const int MAX_PATH_ARGS_CNT = 20; // max path and args count @@ -350,8 +352,21 @@ void InitReadCfg() // do jobs DoJob("pre-init"); +#ifndef __LINUX__ + TriggerStage(EVENT1, EVENT1_WAITTIME, QS_STAGE1); +#endif + DoJob("init"); +#ifndef __LINUX__ + TriggerStage(EVENT2, EVENT2_WAITTIME, QS_STAGE2); +#endif + DoJob("post-init"); +#ifndef __LINUX__ + TriggerStage(EVENT3, EVENT3_WAITTIME, QS_STAGE3); + + InitStageFinished(); +#endif ReleaseAllJobs(); } -- Gitee From 509667fca835e3cdb5503c7c0193a86337773e98 Mon Sep 17 00:00:00 2001 From: boxi Date: Mon, 17 May 2021 15:27:56 +0800 Subject: [PATCH 18/21] feat: Rename initstage to initsync, and apply the module in the init process. 1.Rename initstage to initsync 2.Apply the module in the init process to synchronize the init process with the application process Close #I3RCUF --- initsync/src/init_sync.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/initsync/src/init_sync.c b/initsync/src/init_sync.c index 00f229c3f..526394974 100644 --- a/initsync/src/init_sync.c +++ b/initsync/src/init_sync.c @@ -64,7 +64,7 @@ int SystemInitStage(QuickstartStage stage) void TriggerStage(unsigned int event, unsigned int wait, QuickstartStage stagelevel) { int ret = InitListen(event, wait); - if (ret != 0 && ret != -1) { + if (ret != 0) { SystemInitStage(stagelevel); } } -- Gitee From f17b102ab8272765bb102b1f84853b756ea5336f Mon Sep 17 00:00:00 2001 From: lanxueyuan Date: Tue, 18 May 2021 12:04:17 +0800 Subject: [PATCH 19/21] add OHOS_LITE --- services/src/init_read_cfg.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/services/src/init_read_cfg.c b/services/src/init_read_cfg.c index 6d7d280fd..0d2108f3c 100644 --- a/services/src/init_read_cfg.c +++ b/services/src/init_read_cfg.c @@ -28,8 +28,10 @@ #include "init_service_manager.h" #include "securec.h" #ifndef __LINUX__ +#ifdef OHOS_LITE #include "init_stage.h" #endif +#endif static const long MAX_JSON_FILE_LEN = 102400; // max init.cfg size 100KB static const int MAX_PATH_ARGS_CNT = 20; // max path and args count @@ -353,19 +355,25 @@ void InitReadCfg() // do jobs DoJob("pre-init"); #ifndef __LINUX__ +#ifdef OHOS_LITE TriggerStage(EVENT1, EVENT1_WAITTIME, QS_STAGE1); +#endif #endif DoJob("init"); #ifndef __LINUX__ +#ifdef OHOS_LITE TriggerStage(EVENT2, EVENT2_WAITTIME, QS_STAGE2); +#endif #endif DoJob("post-init"); #ifndef __LINUX__ +#ifdef OHOS_LITE TriggerStage(EVENT3, EVENT3_WAITTIME, QS_STAGE3); InitStageFinished(); +#endif #endif ReleaseAllJobs(); } -- Gitee From 265935eb4ffffffb861210f4213c1df5aac984e9 Mon Sep 17 00:00:00 2001 From: lanxueyuan Date: Tue, 18 May 2021 20:14:31 +0800 Subject: [PATCH 20/21] =?UTF-8?q?=E8=A7=84=E8=8C=83=E5=8C=96=E5=A4=B4?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- initsync/src/init_sync.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/initsync/src/init_sync.c b/initsync/src/init_sync.c index 526394974..24aa1cdd9 100644 --- a/initsync/src/init_sync.c +++ b/initsync/src/init_sync.c @@ -14,15 +14,16 @@ */ #include "init_stage.h" -#include -#include -#include -#include + +#include +#include #include #include +#include +#include +#include +#include #include -#include -#include static int SendCmd(int cmd, unsigned long arg) { -- Gitee From a246802e69846c30c6cf1e2a2a9a20238e632b84 Mon Sep 17 00:00:00 2001 From: jason_gitee <9146300+jason_openharmony@user.noreply.gitee.com> Date: Thu, 20 May 2021 11:20:07 +0800 Subject: [PATCH 21/21] update services/src/init_cmds.c. --- services/src/init_cmds.c | 1 + 1 file changed, 1 insertion(+) diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index 1c0c79f9e..b79c56182 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -44,6 +44,7 @@ #define OCTAL_TYPE 8 // 8 means octal to decimal #define MAX_BUFFER 256 static const char *g_supportCfg[] = { + "/etc/patch.cfg", "/patch/fstab.cfg", }; -- Gitee