From 3607a0cef63ed550e7ccd92815df12748a0e44c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E4=BD=B3=E4=BC=9F?= Date: Thu, 21 Aug 2025 15:09:12 +0800 Subject: [PATCH] =?UTF-8?q?Description:=E5=90=8C=E6=AD=A5=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=20IssueNo:https://gitee.com/openharmony/startup=5Fini?= =?UTF-8?q?t/issues/ICU1JT=20Binary=20Source:No=20Signed-off-by:=20songjia?= =?UTF-8?q?wei9=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- device_info/device_info_service.cpp | 10 +-- interfaces/innerkits/fs_manager/BUILD.gn | 42 +++++++-- .../erofs_overlay/erofs_mount_overlay.c | 41 +++++++-- .../erofs_overlay/erofs_remount_overlay.c | 4 +- .../include/erofs_remount_overlay.h | 2 +- interfaces/innerkits/fs_manager/fstab_mount.c | 85 +++++++++++++++---- .../innerkits/fs_manager/libfs_dm/fs_dm.c | 5 +- .../fs_manager/libfs_dm/fs_dm_linear.c | 40 +++++---- .../fs_manager/libfs_dm/fs_dm_snapshot.c | 41 +++++---- .../innerkits/include/fs_manager/fs_manager.h | 12 +++ interfaces/innerkits/include/init_utils.h | 4 - .../init_module_engine/include/bootstage.h | 1 + .../init_module_engine/stub/libinit.stub.json | 2 +- interfaces/innerkits/syspara/param_comm.c | 8 +- interfaces/innerkits/syspara/parameter.c | 1 - .../kits/jskits/src/native_deviceinfo_js.cpp | 68 +++++++-------- services/modules/trace/init_trace.c | 2 +- test/fuzztest/onstop_fuzzer/onstop_fuzzer.cpp | 4 +- test/unittest/init/service_unittest.cpp | 2 + .../ueventd/ueventd_event_unittest.cpp | 14 +++ ueventd/ueventd_device_handler.c | 40 ++++----- 21 files changed, 271 insertions(+), 157 deletions(-) diff --git a/device_info/device_info_service.cpp b/device_info/device_info_service.cpp index 701439180..211d49c5a 100644 --- a/device_info/device_info_service.cpp +++ b/device_info/device_info_service.cpp @@ -107,15 +107,13 @@ int32_t DeviceInfoService::CallbackEnter(uint32_t code) switch (code) { case static_cast(IDeviceInfoIpcCode::COMMAND_GET_UDID): case static_cast(IDeviceInfoIpcCode::COMMAND_GET_SERIAL_I_D): { - if (!CheckPermission("ohos.permission.sec.ACCESS_UDID")) { - return SYSPARAM_PERMISSION_DENIED; - } + bool ret = CheckPermission("ohos.permission.sec.ACCESS_UDID"); + BEGET_CHECK_RETURN_VALUE(ret, SYSPARAM_PERMISSION_DENIED); break; } case static_cast(IDeviceInfoIpcCode::COMMAND_GET_DISK_S_N): { - if (!CheckPermission("ohos.permission.ACCESS_DISK_PHY_INFO")) { - return SYSPARAM_PERMISSION_DENIED; - } + bool ret = CheckPermission("ohos.permission.ACCESS_DISK_PHY_INFO"); + BEGET_CHECK_RETURN_VALUE(ret, SYSPARAM_PERMISSION_DENIED); break; } default: { diff --git a/interfaces/innerkits/fs_manager/BUILD.gn b/interfaces/innerkits/fs_manager/BUILD.gn index feb87956f..5989fa3b9 100755 --- a/interfaces/innerkits/fs_manager/BUILD.gn +++ b/interfaces/innerkits/fs_manager/BUILD.gn @@ -53,11 +53,11 @@ ohos_static_library("libfsmanager_static") { if ((defined(global_parts_info) && defined(global_parts_info.startup_hvb)) || init_startup_feature_erofs_overlay) { - sources += [ + sources += [ "libfs_dm/fs_dm.c", "libfs_dm/fs_dm_linear.c", "libfs_dm/fs_dm_snapshot.c", - ] + ] include_dirs += [ "//base/startup/init/interfaces/innerkits/fs_manager/libfs_dm/include", "//base/startup/init/ueventd/include", @@ -89,6 +89,10 @@ ohos_static_library("libfsmanager_static") { include_dirs += [ "//base/startup/init/interfaces/innerkits/fs_manager/erofs_overlay/include" ] defines += [ "EROFS_OVERLAY" ] + if (build_selinux) { + external_deps += [ "selinux:libselinux" ] + cflags = [ "-DWITH_SELINUX" ] + } } } @@ -118,26 +122,48 @@ ohos_static_library("libfsmanager_static_real") { "cJSON:cjson_static", ] deps = [ "//base/startup/init/services/utils:libinit_utils" ] - if (defined(global_parts_info) && defined(global_parts_info.startup_hvb)) { - sources += [ - "dm_verity/dm_verity.c", + + if ((defined(global_parts_info) && + defined(global_parts_info.startup_hvb)) || + init_startup_feature_erofs_overlay) { + source += [ "libfs_dm/fs_dm.c", "libfs_dm/fs_dm_linear.c", "libfs_dm/fs_dm_snapshot.c", + ] + include_dirs += [ + "//base/startup/init/interfaces/innerkits/fs_manager/libfs_dm/include", + "//base/startup/init/ueventd/include", + ] + defines = [] + deps += [ "//base/startup/init/ueventd:libueventd_ramdisk_static_real" ] + } + + if (defined(global_parts_info) && defined(global_parts_info.startup_hvb)) { + sources += [ + "dm_verity/dm_verity.c", "libfs_hvb/fs_hvb.c", "libfs_hvb/hvb_ops.c", ] include_dirs += [ - "//base/startup/init/interfaces/innerkits/fs_manager/libfs_dm/include", "//base/startup/init/interfaces/innerkits/fs_manager/libfs_hvb/include", "//base/startup/init/interfaces/innerkits/fs_manager/dm_verity/include", - "//base/startup/init/ueventd/include", ] defines = [ "SUPPORT_HVB" ] external_deps += [ "hvb:libhvb_static" ] - deps += [ "//base/startup/init/ueventd:libueventd_ramdisk_static_real" ] + } + + if (init_startup_feature_erofs_overlay) { + sources += [ + "erofs_overlay/erofs_mount_overlay.c", + "erofs_overlay/erofs_overlay_common.c", + "erofs_overlay/erofs_remount_overlay.c", + ] + include_dirs += [ "//base/startup/init/interfaces/innerkits/fs_manager/erofs_overlay/include" ] + + defines += [ "EROFS_OVERLAY" ] } } diff --git a/interfaces/innerkits/fs_manager/erofs_overlay/erofs_mount_overlay.c b/interfaces/innerkits/fs_manager/erofs_overlay/erofs_mount_overlay.c index f74c27edb..7a3601a35 100644 --- a/interfaces/innerkits/fs_manager/erofs_overlay/erofs_mount_overlay.c +++ b/interfaces/innerkits/fs_manager/erofs_overlay/erofs_mount_overlay.c @@ -17,6 +17,9 @@ #include #include #include "securec.h" +#ifdef WITH_SELINUX +#include +#endif #include "init_utils.h" #include "fs_dm.h" #include "switch_root.h" @@ -249,7 +252,6 @@ INIT_STATIC int ConstructLinearTarget(DmVerityTarget *target, const char *dev, u return -1; } target->paras_len = strlen(target->paras); - BEGET_LOGI("dev [%s], linearparas [%s], length [%s]", dev, target->paras, target->paras_len); return 0; } @@ -335,27 +337,42 @@ int MountExt4Device(const char *dev, const char *mnt, bool isFirstMount) char dirExt4[MAX_BUFFER_LEN] = {0}; char dirUpper[MAX_BUFFER_LEN] = {0}; char dirWork[MAX_BUFFER_LEN] = {0}; + +#ifdef WITH_SELINUX + if (isFirstMount) { + const char* fsFileContext = "u:object_r:system_file:s0"; + const char* vendorFileContext = "u:object_r:vendor_file:s0"; + BEGET_LOGI("start to set selinux. mnt:%s", mnt); + if (strcmp(mnt, "/vendor") == 0) { + setfscreatecon(vendorFileContext); + } else { + setfscreatecon(fsFileContext); + } + } +#endif + ret = snprintf_s(dirExt4, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, PREFIX_OVERLAY"%s", mnt); if (ret < 0) { BEGET_LOGE("dirExt4 copy failed errno %d.", errno); - return -1; + goto exit; } ret = snprintf_s(dirUpper, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, PREFIX_OVERLAY"%s"PREFIX_UPPER, mnt); if (ret < 0) { BEGET_LOGE("dirUpper copy failed errno %d.", errno); - return -1; + goto exit; } ret = snprintf_s(dirWork, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, PREFIX_OVERLAY"%s"PREFIX_WORK, mnt); if (ret < 0) { BEGET_LOGE("dirWork copy failed errno %d.", errno); - return -1; + goto exit; } if (mkdir(dirExt4, MODE_MKDIR) && (errno != EEXIST)) { BEGET_LOGE("mkdir %s failed.", dirExt4); - return -1; + ret = -1; + goto exit; } int retryCount = 3; @@ -371,14 +388,23 @@ int MountExt4Device(const char *dev, const char *mnt, bool isFirstMount) if (isFirstMount && mkdir(dirUpper, MODE_MKDIR) && (errno != EEXIST)) { BEGET_LOGE("mkdir dirUpper:%s failed.", dirUpper); - return -1; + ret = -1; + goto exit; } if (isFirstMount && mkdir(dirWork, MODE_MKDIR) && (errno != EEXIST)) { BEGET_LOGE("mkdir dirWork:%s failed.", dirWork); - return -1; + ret = -1; + goto exit; } + ret = 0; +exit: +#ifdef WITH_SELINUX + if (isFirstMount) { + setfscreatecon(NULL); + } +#endif return ret; } @@ -480,5 +506,6 @@ int DoMountOverlayDevice(FstabItem *item) BEGET_LOGE("init ext4 dm dev failed"); return -1; } + BEGET_LOGI("mount overlay device %s on %s success", item->deviceName, item->mountPoint); return rc; } \ No newline at end of file diff --git a/interfaces/innerkits/fs_manager/erofs_overlay/erofs_remount_overlay.c b/interfaces/innerkits/fs_manager/erofs_overlay/erofs_remount_overlay.c index 9b217ede3..ac18241c7 100644 --- a/interfaces/innerkits/fs_manager/erofs_overlay/erofs_remount_overlay.c +++ b/interfaces/innerkits/fs_manager/erofs_overlay/erofs_remount_overlay.c @@ -52,14 +52,14 @@ int GetRemountResult(void) return REMOUNT_FAIL; } -void SetRemountResultFlag() +void SetRemountResultFlag(void) { struct stat st; int ret; int statRet = stat(REMOUNT_RESULT_PATH, &st); if (statRet != 0) { - ret = mkdir(REMOUNT_RESULT_PATH, MODE_MKDIR); + ret = MakeDirRecursive(REMOUNT_RESULT_PATH, MODE_MKDIR); if (ret < 0 && errno != EEXIST) { BEGET_LOGE("mkdir remount path failed errno %d", errno); return; diff --git a/interfaces/innerkits/fs_manager/erofs_overlay/include/erofs_remount_overlay.h b/interfaces/innerkits/fs_manager/erofs_overlay/include/erofs_remount_overlay.h index 8a34cf792..96829636f 100644 --- a/interfaces/innerkits/fs_manager/erofs_overlay/include/erofs_remount_overlay.h +++ b/interfaces/innerkits/fs_manager/erofs_overlay/include/erofs_remount_overlay.h @@ -29,7 +29,7 @@ extern "C" { int GetRemountResult(void); -void SetRemountResultFlag(); +void SetRemountResultFlag(void); int RemountOverlay(void); diff --git a/interfaces/innerkits/fs_manager/fstab_mount.c b/interfaces/innerkits/fs_manager/fstab_mount.c index 078c1817d..801cf1c7a 100755 --- a/interfaces/innerkits/fs_manager/fstab_mount.c +++ b/interfaces/innerkits/fs_manager/fstab_mount.c @@ -27,6 +27,7 @@ #include "fs_manager/fs_manager.h" #include "hookmgr.h" #include "list.h" +#include "init_modulemgr.h" #include "init_utils.h" #include "param/init_param.h" #include "securec.h" @@ -48,14 +49,14 @@ extern "C" { #define FS_MANAGER_BUFFER_SIZE 512 #define BLOCK_SIZE_BUFFER (64) #define RESIZE_BUFFER_SIZE 1024 -#define MAX_GCALLOWNANCE 100 -#define GCALLOWANCE_INCREACE 10 +#define MAX_GC_ALLOWANCE 100 +#define GCALLOWANCE_INCREASE 10 const off_t PARTITION_ACTIVE_SLOT_OFFSET = 1024; const off_t PARTITION_ACTIVE_SLOT_SIZE = 4; int g_bootSlots = -1; int g_currentSlot = -1; -__attribute__((weak)) void InitPostMount(const char *mountPoint, int rc) +__attribute__((weak)) void InitPostMount(int rc, struct FstabItem *fstabItem) { } @@ -454,8 +455,8 @@ INIT_STATIC int MountWithCheckpoint(const char *source, const char *target, cons if (rc != 0 && errno == EBUSY) { rc = 0; } - gcAllowance += GCALLOWANCE_INCREACE; - } while (rc != 0 && errno == EAGAIN && gcAllowance <= MAX_GCALLOWNANCE); + gcAllowance += GCALLOWANCE_INCREASE; + } while (rc != 0 && errno == EAGAIN && gcAllowance <= MAX_GC_ALLOWANCE); return rc; } @@ -510,6 +511,47 @@ int GetCurrentSlot(void) return GetSlotInfoFromBootctrl(PARTITION_ACTIVE_SLOT_OFFSET, PARTITION_ACTIVE_SLOT_SIZE); } +static int GetSlotStatus(void) +{ + static int slotStatus = SLOTSTATUS_INIT; + if (slotStatus != SLOTSTATUS_INIT) { + return slotStatus; + } + slotStatus = SLOTSTATUS_OTHER; + do { + if (GetBootSlots() <= 1) { + BEGET_LOGE("boot slot not need CheckVabMountInfo"); + break; + } + int slot = GetCurrentSlot(); + if (slot <= 0 || slot > MAX_SLOT) { + BEGET_LOGE("slot value %d is invalid", slot); + break; + } + slotStatus = SLOTSTATUS_VAB; + } while (0); + return slotStatus; +} + +static int UpdataAndCheckVabMountInfo(FstabItem *item, int result) +{ + int slot = GetSlotStatus(); + if (slot != SLOTSTATUS_VAB) { + return 0; + } + VabMountInfo mountInfo = { + .deviceName = item == NULL ? NULL : item->deviceName, + .fsType = item == NULL ? NULL : item->fsType, + .result = result, + }; + int ret = HookMgrExecute(GetBootStageHookMgr(), INIT_CHECK_VAB_MOUNTINFO, + item == NULL ? NULL : (void*)&mountInfo, NULL); + if (ret != 0) { + BEGET_LOGE("CheckVabMountInfo failed "); + } + return ret; +} + INIT_STATIC int GetDataWithoutCheckpoint(char *fsSpecificData, size_t fsSpecificDataSize, char *checkpointData, size_t checkpointDataSize) { @@ -542,12 +584,12 @@ INIT_STATIC int GetDataWithoutCheckpoint(char *fsSpecificData, size_t fsSpecific continue; } if (strcmp(checkpointData, "") != 0 && - strncat_s(checkpointData, checkpointDataSize - 1, ",", 1) != EOK) { + strncat_s(checkpointData, checkpointDataSize, ",", 1) != EOK) { BEGET_LOGW("failed to append comma."); rc = -1; break; } - if (strncat_s(checkpointData, checkpointDataSize - 1, p, strlen(p)) != EOK) { + if (strncat_s(checkpointData, checkpointDataSize, p, strlen(p)) != EOK) { BEGET_LOGW("Failed to append mountflags \" %s \", ignore it.", p); rc = -1; break; @@ -642,20 +684,29 @@ static int MountItemByFsType(FstabItem *item) static int ExecCheckpointHook(FstabItem *item) { - HOOK_EXEC_OPTIONS options; - options.flags = TRAVERSE_STOP_WHEN_ERROR; - options.postHook = NULL; - options.preHook = NULL; - int ret = HookMgrExecute(GetBootStageHookMgr(), INIT_DISABLE_CHECKPOINT, (void*)item, &options); - BEGET_LOGI("ExecCheckpointHook ret %d", ret); + int ret = -1; + do { + int slot = GetSlotStatus(); + if (slot != SLOTSTATUS_VAB) { + return 0; + } + + ret = HookMgrExecute(GetBootStageHookMgr(), INIT_DISABLE_CHECKPOINT, (void*)item, NULL); + BEGET_LOGI("ExecCheckpointHook ret %d", ret); + } while (0); + return ret; } + int MountOneItem(FstabItem *item) { if (item == NULL) { return -1; } - + if (!IsSupportedFilesystem(item->fsType)) { + BEGET_LOGW("Unsupported file system \" %s \"", item->fsType); + return 0; + } if (FM_MANAGER_WAIT_ENABLED(item->fsManagerFlags)) { WaitForFile(item->deviceName, WAIT_MAX_SECOND); } @@ -691,11 +742,13 @@ int MountOneItem(FstabItem *item) SwitchRoot("/usr"); } #endif + if (disableCheckpointRet == 0 && rc == 0) { BEGET_LOGI("start health check process"); HookMgrExecute(GetBootStageHookMgr(), INIT_HEALTH_CHECK_ACTIVE, NULL, NULL); } - InitPostMount(item->mountPoint, rc); + UpdataAndCheckVabMountInfo(item, rc); + InitPostMount(rc, item); if (rc != 0) { if (FM_MANAGER_NOFAIL_ENABLED(item->fsManagerFlags)) { BEGET_LOGE("Mount no fail device %s to %s failed, err = %d", item->deviceName, item->mountPoint, errno); @@ -814,7 +867,7 @@ int MountAllWithFstab(const Fstab *fstab, bool required) break; } } - + UpdataAndCheckVabMountInfo(NULL, 0); #ifdef SUPPORT_HVB if (required) HvbDmVerityFinal(); diff --git a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm.c b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm.c index 2caf57bf6..c9528c0ba 100755 --- a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm.c +++ b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm.c @@ -99,7 +99,6 @@ int LoadDmDeviceTable(int fd, const char *devName, DmVerityTarget *target, int d BEGET_LOGE("dmTypeIdx error dmTypeIdx %d", dmTypeIdx); return -1; } - parasTotalSize = DM_ALIGN(sizeof(*io) + sizeof(*ts) + target->paras_len + 1); parasBuf = calloc(1, parasTotalSize); if (parasBuf == NULL) { @@ -362,7 +361,7 @@ int DmGetDeviceName(int fd, const char *devName, char *outDevName, const uint64_ path = calloc(1, pathLen); if (path == NULL) { BEGET_LOGE("calloc path failed"); - return -1; + return rc; } rc = snprintf_s(path, pathLen, pathLen - 1, "%s%d", DM_DEVICE_PATH_PREFIX, devNum); @@ -445,8 +444,6 @@ bool GetDmStatusInfo(const char *name, struct dm_ioctl *io) close(fd); return true; } - - #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_linear.c b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_linear.c index fe97cdee3..e1aceebf5 100644 --- a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_linear.c +++ b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_linear.c @@ -1,17 +1,17 @@ /* -* Copyright (c) 2025 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. -*/ + * Copyright (c) 2025 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 @@ -38,7 +38,6 @@ static int LoadDmLinearDeviceTable(int fd, const char *devName, DmLinearTarget * for (uint32_t index = 0; index < targetNum; index++) { parasTotalSize += target[index].paras_len + 1; } - char *parasBuf = calloc(1, parasTotalSize); BEGET_ERROR_CHECK(parasBuf != NULL, return -1, "error, calloc dm table fail"); io = (struct dm_ioctl *)parasBuf; @@ -51,12 +50,15 @@ static int LoadDmLinearDeviceTable(int fd, const char *devName, DmLinearTarget * uint32_t len = 0; char *paras = NULL; do { - FS_DM_RETURN_ERR_IF_NULL(target[i].paras); + if (target[i].paras == NULL) { + BEGET_LOGE("error, target[%u].paras is NULL", i); + free(parasBuf); + return -1; + } ts = (struct dm_target_spec *)(parasBuf + sizeof(*io) + len); ts->status = 0; ts->sector_start = target[i].start; ts->length = target[i].length; - rc = strcpy_s(ts->target_type, sizeof(ts->target_type), "linear"); BEGET_ERROR_CHECK(rc == EOK, free(parasBuf); return -1, "error %d, cp target type", rc); len += sizeof((*ts)); @@ -97,7 +99,6 @@ int FsDmCreateMultiTargetLinearDevice(const char *devName, char *dmDevPath, uint } int rc = 0; - do { rc = CreateDmDev(fd, devName); if (rc != 0) { @@ -120,10 +121,8 @@ int FsDmCreateMultiTargetLinearDevice(const char *devName, char *dmDevPath, uint BEGET_LOGE("active dm device name failed"); break; } - BEGET_LOGI("fs create linear device success, dev is [%s] dmDevPath is [%s]", devName, dmDevPath); } while (0); - close(fd); return rc; } @@ -146,19 +145,18 @@ int FsDmSwitchToLinearDevice(const char *devName, DmLinearTarget *target, uint32 } int rc = 0; - do { rc = LoadDmLinearDeviceTable(fd, devName, target, targetNum); if (rc != 0) { BEGET_LOGE("load dm device name failed"); break; } + rc = ActiveDmDevice(fd, devName); if (rc != 0) { BEGET_LOGE("active dm device name failed"); break; } - BEGET_LOGI("fs switch linear device success, dev is [%s]", devName); } while (0); diff --git a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_snapshot.c b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_snapshot.c index 8c0fc48c9..591bb4cf8 100644 --- a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_snapshot.c +++ b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_snapshot.c @@ -1,17 +1,18 @@ /* -* Copyright (c) 2025 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. -*/ + * Copyright (c) 2025 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 @@ -35,11 +36,13 @@ int FsDmCreateSnapshotDevice(const char *devName, char *dmDevPath, uint64_t dmDe BEGET_LOGE("argc is null"); return -1; } + int fd = open(DEVICE_MAPPER_PATH, O_RDWR | O_CLOEXEC); if (fd < 0) { BEGET_LOGE("error %d, open %s", errno, DEVICE_MAPPER_PATH); return -1; } + int rc = 0; do { rc = CreateDmDev(fd, devName); @@ -47,23 +50,23 @@ int FsDmCreateSnapshotDevice(const char *devName, char *dmDevPath, uint64_t dmDe BEGET_LOGE("error %d, create dm device fail", rc); break; } + rc = LoadDmDeviceTable(fd, devName, target, SNAPSHOT); if (rc != 0) { BEGET_LOGE("error %d, load device table fail", rc); break; } + rc = ActiveDmDevice(fd, devName); if (rc != 0) { BEGET_LOGE("error %d, active device fail", rc); break; } - rc = DmGetDeviceName(fd, devName, dmDevPath, dmDevPathLen); if (rc != 0) { BEGET_LOGE("get dm device name failed"); break; } - BEGET_LOGI("fs create snapshot device success, dev is [%s] dmDevPath is [%s]", devName, dmDevPath); } while (0); close(fd); @@ -81,6 +84,7 @@ int FsDmSwitchToSnapshotMerge(const char *devName, DmSnapshotTarget *target) BEGET_LOGE("error %d, open %s", errno, DEVICE_MAPPER_PATH); return -1; } + int rc = 0; do { rc = LoadDmDeviceTable(fd, devName, target, SNAPSHOTMERGE); @@ -95,6 +99,7 @@ int FsDmSwitchToSnapshotMerge(const char *devName, DmSnapshotTarget *target) } BEGET_LOGI("fs switch snapshot merge success, dev is %s", devName); } while (0); + close(fd); return rc; } @@ -125,17 +130,17 @@ INIT_STATIC bool ParseStatusText(char *data, StatusInfo *processInfo) BEGET_LOGW("processInfo->error is \"%s\"", processInfo->error); return true; } - BEGET_LOGE("could not parse snapshot processInfo \"%s\": wrong format", processInfo->error); + BEGET_LOGE("could not parse snapshot processInfo: wrong format \"%s\"", processInfo->error); return false; } bool GetDmSnapshotStatus(const char *name, const char *targetType, StatusInfo *processInfo) { - if (name == NULL || targetType == NULL || processInfo == NULL) { + BEGET_LOGI("GetDmSnapshotStatus start, name %s, targetType %s", name, targetType); + if (name == NULL || processInfo == NULL) { BEGET_LOGE("argc is null"); return false; } - BEGET_LOGI("GetDmSnapshotStatus start, name %s, targetType %s", name, targetType); size_t bufferLen = MAX_TABLE_LEN * sizeof(char); int fd = open(DEVICE_MAPPER_PATH, O_RDWR | O_CLOEXEC); BEGET_ERROR_CHECK(fd >= 0, return false, "open error %d", errno); diff --git a/interfaces/innerkits/include/fs_manager/fs_manager.h b/interfaces/innerkits/include/fs_manager/fs_manager.h index 55a5f2d9b..dd437e657 100644 --- a/interfaces/innerkits/include/fs_manager/fs_manager.h +++ b/interfaces/innerkits/include/fs_manager/fs_manager.h @@ -65,6 +65,18 @@ typedef enum MountStatus { MOUNT_MOUNTED = 1, } MountStatus; +typedef enum SlotStatus { + SLOTSTATUS_INIT = 0, + SLOTSTATUS_VAB, + SLOTSTATUS_OTHER, +} SlotStatus; + +typedef struct VabMountInfo { + char *deviceName; + char *fsType; + int result; +} VabMountInfo; + typedef struct FstabItem { char *deviceName; // Block device name char *mountPoint; // Mount point diff --git a/interfaces/innerkits/include/init_utils.h b/interfaces/innerkits/include/init_utils.h index 87f350064..c44bba4e4 100644 --- a/interfaces/innerkits/include/init_utils.h +++ b/interfaces/innerkits/include/init_utils.h @@ -63,10 +63,6 @@ typedef struct { #define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0])) #define BOOT_CMD_LINE STARTUP_INIT_UT_PATH"/proc/cmdline" -#define FIRST_VALUE "First_Value" // 取第一个匹配值 -#define LAST_VALUE "Last_Value" // 取最后一个匹配值 -#define EMPTY_VALUE "EMPTY_VALUE" // 不取任何一个 - #ifndef OHOS_LITE void SetBootCompleted(bool isBootCompleted); bool IsBootCompleted(void); diff --git a/interfaces/innerkits/init_module_engine/include/bootstage.h b/interfaces/innerkits/init_module_engine/include/bootstage.h index c9aa37863..1703df338 100755 --- a/interfaces/innerkits/init_module_engine/include/bootstage.h +++ b/interfaces/innerkits/init_module_engine/include/bootstage.h @@ -34,6 +34,7 @@ enum INIT_BOOTSTAGE { INIT_SNAPSHOT_ACTIVE = 6, INIT_DISABLE_CHECKPOINT = 7, INIT_HEALTH_CHECK_ACTIVE = 8, + INIT_CHECK_VAB_MOUNTINFO = 9, INIT_PRE_PARAM_SERVICE = 10, INIT_VAB_HVBCHECK = 11, INIT_PRE_PARAM_LOAD = 20, diff --git a/interfaces/innerkits/init_module_engine/stub/libinit.stub.json b/interfaces/innerkits/init_module_engine/stub/libinit.stub.json index 69d05327b..0d2704d55 100644 --- a/interfaces/innerkits/init_module_engine/stub/libinit.stub.json +++ b/interfaces/innerkits/init_module_engine/stub/libinit.stub.json @@ -6,7 +6,6 @@ { "name": "LoadPersistParams" }, { "name": "SplitString" }, { "name": "AddCmdExecutor" }, - { "name": "GetServiceByName"}, { "name": "RemoveCmdExecutor" }, { "name": "HookMgrAdd" }, { "name": "HookMgrAddEx" }, @@ -24,6 +23,7 @@ { "name": "StartupLog" }, { "name": "DoJobNow" }, { "name": "GetServiceExtData" }, + { "name": "GetServiceByName"}, { "name": "UpdateMiscMessage" }, { "name": "AddRebootCmdExecutor" }, { "name": "GetBootEventList" }, diff --git a/interfaces/innerkits/syspara/param_comm.c b/interfaces/innerkits/syspara/param_comm.c index 8591bc8d5..c132fcaa2 100644 --- a/interfaces/innerkits/syspara/param_comm.c +++ b/interfaces/innerkits/syspara/param_comm.c @@ -77,7 +77,7 @@ INIT_LOCAL_API int GetParameter_(const char *key, const char *def, char *value, if (def == NULL) { return GetSystemError(ret); } - if (strlen(def) > len) { + if (strlen(def) >= len) { return EC_INVALID; } ret = strcpy_s(value, len, def); @@ -135,11 +135,7 @@ INIT_LOCAL_API const char *GetPropertyAtomic(const char *key, const char **param BEGET_CHECK(res != NULL, return NULL); ret = SystemGetParameter(key, res, &len); - if (ret != 0) { - free(res); - return NULL; - } - + BEGET_CHECK(ret == 0, free(res); return NULL); if (g_propertyGetProcessor != NULL) { res = g_propertyGetProcessor(key, res); } diff --git a/interfaces/innerkits/syspara/parameter.c b/interfaces/innerkits/syspara/parameter.c index e6108cf4a..6d0940407 100644 --- a/interfaces/innerkits/syspara/parameter.c +++ b/interfaces/innerkits/syspara/parameter.c @@ -17,7 +17,6 @@ #include #include -#include #include "param_comm.h" #include "init_param.h" diff --git a/interfaces/kits/jskits/src/native_deviceinfo_js.cpp b/interfaces/kits/jskits/src/native_deviceinfo_js.cpp index e14503e07..bc917fd62 100644 --- a/interfaces/kits/jskits/src/native_deviceinfo_js.cpp +++ b/interfaces/kits/jskits/src/native_deviceinfo_js.cpp @@ -535,6 +535,40 @@ static napi_value GetDiskSN(napi_env env, napi_callback_info info) return napiValue; } +static napi_value CreateDeviceTypes(napi_env env, napi_value exports) +{ + napi_value deviceTypes = nullptr; + napi_value typeDefault = nullptr; + napi_value typePhone = nullptr; + napi_value typeTablet = nullptr; + napi_value type2in1 = nullptr; + napi_value typeTv = nullptr; + napi_value typeWearable = nullptr; + napi_value typeCar = nullptr; + + napi_create_object(env, &deviceTypes); + + napi_create_string_utf8(env, "default", NAPI_AUTO_LENGTH, &typeDefault); + napi_create_string_utf8(env, "phone", NAPI_AUTO_LENGTH, &typePhone); + napi_create_string_utf8(env, "tablet", NAPI_AUTO_LENGTH, &typeTablet); + napi_create_string_utf8(env, "2in1", NAPI_AUTO_LENGTH, &type2in1); + napi_create_string_utf8(env, "tv", NAPI_AUTO_LENGTH, &typeTv); + napi_create_string_utf8(env, "wearable", NAPI_AUTO_LENGTH, &typeWearable); + napi_create_string_utf8(env, "car", NAPI_AUTO_LENGTH, &typeCar); + + napi_set_named_property(env, deviceTypes, "TYPE_DEFAULT", typeDefault); + napi_set_named_property(env, deviceTypes, "TYPE_PHONE", typePhone); + napi_set_named_property(env, deviceTypes, "TYPE_TABLET", typeTablet); + napi_set_named_property(env, deviceTypes, "TYPE_2IN1", type2in1); + napi_set_named_property(env, deviceTypes, "TYPE_TV", typeTv); + napi_set_named_property(env, deviceTypes, "TYPE_WEARABLE", typeWearable); + napi_set_named_property(env, deviceTypes, "TYPE_CAR", typeCar); + + napi_set_named_property(env, exports, "DeviceTypes", deviceTypes); + + return exports; +} + static napi_value EnumLevelClassConstructor(napi_env env, napi_callback_info info) { napi_value thisArg = nullptr; @@ -582,40 +616,6 @@ static napi_value GetPerformanceClass(napi_env env, napi_callback_info info) return napiValue; } -static napi_value CreateDeviceTypes(napi_env env, napi_value exports) -{ - napi_value deviceTypes = nullptr; - napi_value typeDefault = nullptr; - napi_value typePhone = nullptr; - napi_value typeTablet = nullptr; - napi_value type2in1 = nullptr; - napi_value typeTv = nullptr; - napi_value typeWearable = nullptr; - napi_value typeCar = nullptr; - - napi_create_object(env, &deviceTypes); - - napi_create_string_utf8(env, "default", NAPI_AUTO_LENGTH, &typeDefault); - napi_create_string_utf8(env, "phone", NAPI_AUTO_LENGTH, &typePhone); - napi_create_string_utf8(env, "tablet", NAPI_AUTO_LENGTH, &typeTablet); - napi_create_string_utf8(env, "2in1", NAPI_AUTO_LENGTH, &type2in1); - napi_create_string_utf8(env, "tv", NAPI_AUTO_LENGTH, &typeTv); - napi_create_string_utf8(env, "wearable", NAPI_AUTO_LENGTH, &typeWearable); - napi_create_string_utf8(env, "car", NAPI_AUTO_LENGTH, &typeCar); - - napi_set_named_property(env, deviceTypes, "TYPE_DEFAULT", typeDefault); - napi_set_named_property(env, deviceTypes, "TYPE_PHONE", typePhone); - napi_set_named_property(env, deviceTypes, "TYPE_TABLET", typeTablet); - napi_set_named_property(env, deviceTypes, "TYPE_2IN1", type2in1); - napi_set_named_property(env, deviceTypes, "TYPE_TV", typeTv); - napi_set_named_property(env, deviceTypes, "TYPE_WEARABLE", typeWearable); - napi_set_named_property(env, deviceTypes, "TYPE_CAR", typeCar); - - napi_set_named_property(env, exports, "DeviceTypes", deviceTypes); - - return exports; -} - EXTERN_C_START /* * Module init diff --git a/services/modules/trace/init_trace.c b/services/modules/trace/init_trace.c index d6fcc6a62..109ea32a7 100644 --- a/services/modules/trace/init_trace.c +++ b/services/modules/trace/init_trace.c @@ -304,7 +304,7 @@ static void CheckKernelType(bool *isLinux) { struct utsname uts; if (uname(&uts) == -1) { - PLUGIN_LOGE("Kernel type get failed,errno:d", errno); + PLUGIN_LOGE("Kernel type get failed,errno:%d", errno); return; } diff --git a/test/fuzztest/onstop_fuzzer/onstop_fuzzer.cpp b/test/fuzztest/onstop_fuzzer/onstop_fuzzer.cpp index e491fb74e..7ff5dffc0 100644 --- a/test/fuzztest/onstop_fuzzer/onstop_fuzzer.cpp +++ b/test/fuzztest/onstop_fuzzer/onstop_fuzzer.cpp @@ -36,7 +36,7 @@ public: } }; namespace OHOS { -bool FuzzCheckAppWatchPermission(const uint8_t* data, size_t size) + bool FuzzOnstop(const uint8_t* data, size_t size) { std::unique_ptr watcherManager = std::make_unique(0, true); uint32_t id = static_cast(*data); @@ -54,6 +54,6 @@ bool FuzzCheckAppWatchPermission(const uint8_t* data, size_t size) extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::FuzzCheckAppWatchPermission(data, size); + OHOS::FuzzOnstop(data, size); return 0; } \ No newline at end of file diff --git a/test/unittest/init/service_unittest.cpp b/test/unittest/init/service_unittest.cpp index 4edf39540..a37874a40 100644 --- a/test/unittest/init/service_unittest.cpp +++ b/test/unittest/init/service_unittest.cpp @@ -618,6 +618,7 @@ HWTEST_F(ServiceUnitTest, TestKernelPermPermissionError, TestSize.Level1) ASSERT_NE(nullptr, service); int ret = ParseOneService(serviceItem, service); EXPECT_EQ(ret, 0); + EXPECT_TRUE(service->kernelPerms != nullptr); SERVICE_INFO_CTX context; context.serviceName = service->name; @@ -645,6 +646,7 @@ HWTEST_F(ServiceUnitTest, TestKernelPermServiceNameError, TestSize.Level1) ASSERT_NE(nullptr, service); int ret = ParseOneService(serviceItem, service); EXPECT_EQ(ret, 0); + EXPECT_TRUE(service->kernelPerms != nullptr); SERVICE_INFO_CTX context; context.serviceName = "wrongServiceName"; diff --git a/test/unittest/ueventd/ueventd_event_unittest.cpp b/test/unittest/ueventd/ueventd_event_unittest.cpp index 465ca7ac3..3df7f1bcf 100644 --- a/test/unittest/ueventd/ueventd_event_unittest.cpp +++ b/test/unittest/ueventd/ueventd_event_unittest.cpp @@ -532,6 +532,20 @@ HWTEST_F(UeventdEventUnitTest, Init_UeventdEventUnitTest_HandleUsbDevicesWithBus EXPECT_TRUE(exist); } +HWTEST_F(UeventdEventUnitTest, Init_UeventdEventUnitTest_HandleDevicesWithInputEvent001, TestSize.Level1) +{ + struct Uevent uevent = { + .subsystem = "input", + .syspath = "/devices/platform/fingerprint/input", + .deviceName = "event30", + .major = 8, + .minor = 9, + }; + HandleOtherDeviceEvent(&uevent); + auto exist = IsFileExist("/dev/input/event30"); + EXPECT_TRUE(exist); +} + HWTEST_F(UeventdEventUnitTest, Init_UeventdEventUnitTest_Handle001, TestSize.Level1) { char path[] = {"/data/ueventd"}; diff --git a/ueventd/ueventd_device_handler.c b/ueventd/ueventd_device_handler.c index 906b40182..3758cd085 100644 --- a/ueventd/ueventd_device_handler.c +++ b/ueventd/ueventd_device_handler.c @@ -505,29 +505,6 @@ void HandleBlockDeviceEvent(const struct Uevent *uevent) HandleDeviceNode(uevent, deviceNode, isBlock); } -static int SplitUsbDeviceNode(const struct Uevent *uevent, char *deviceNode) -{ - if (uevent->deviceName != NULL) { - if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, "/dev/%s", uevent->deviceName) == -1) { - INIT_LOGE("Make device file for device [%d : %d]", uevent->major, uevent->minor); - return -1; - } - return 0; - } else { - if (uevent->busNum < 0 || uevent->devNum < 0) { - // usb device should always report bus number and device number. - INIT_LOGE("usb device with invalid bus number or device number"); - return -1; - } - if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, - "/dev/bus/usb/%03d/%03d", uevent->busNum, uevent->devNum) == -1) { - INIT_LOGE("Make usb device node for device [%d : %d]", uevent->busNum, uevent->devNum); - return 0; - } - return 0; - } -} - void HandleOtherDeviceEvent(const struct Uevent *uevent) { if (uevent == NULL || uevent->subsystem == NULL || uevent->syspath == NULL) { @@ -561,8 +538,21 @@ void HandleOtherDeviceEvent(const struct Uevent *uevent) // if usb devices report DEVNAME, just create device node. // otherwise, create deviceNode with bus number and device number. if (STRINGEQUAL(uevent->subsystem, "usb")) { - if (SplitUsbDeviceNode(uevent, deviceNode) != 0) { - return; + if (uevent->deviceName != NULL) { + if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, "/dev/%s", uevent->deviceName) == -1) { + INIT_LOGE("Make device file for device [%d : %d]", uevent->major, uevent->minor); + return; + } + } else { + if (uevent->busNum < 0 || uevent->devNum < 0) { + // usb device should always report bus number and device number. + INIT_LOGE("usb device with invalid bus number or device number"); + return; + } + if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, + "/dev/bus/usb/%03d/%03d", uevent->busNum, uevent->devNum) == -1) { + INIT_LOGE("Make usb device node for device [%d : %d]", uevent->busNum, uevent->devNum); + } } } else if (STARTSWITH(uevent->subsystem, "usb")) { // Other usb devies, do not handle it. -- Gitee