From 14d6157901fd5be56858ce17cf31a0fab6074a69 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Fri, 20 Dec 2024 16:38:24 +0800 Subject: [PATCH] PhysicalDisk (Linux): fix interconnect type detection Regression of b5a9d64 Fix #1453 --- .../physicaldisk/physicaldisk_linux.c | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/detection/physicaldisk/physicaldisk_linux.c b/src/detection/physicaldisk/physicaldisk_linux.c index 6a41ada24..ed1fa87be 100644 --- a/src/detection/physicaldisk/physicaldisk_linux.c +++ b/src/detection/physicaldisk/physicaldisk_linux.c @@ -80,24 +80,28 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption { ffStrbufInit(&device->interconnect); - char pathSysDeviceReal[PATH_MAX]; - ssize_t pathLength = readlinkat(dfd, "device", pathSysDeviceReal, ARRAY_SIZE(pathSysDeviceReal) - 1); - if (pathLength > 0) + if (ffStrStartsWith(devName, "nvme")) + ffStrbufSetStatic(&device->interconnect, "NVMe"); + else { - pathSysDeviceReal[pathLength] = '\0'; - - if (strstr(pathSysDeviceReal, "/usb") != NULL) - ffStrbufSetS(&device->interconnect, "USB"); - else if (strstr(pathSysDeviceReal, "/nvme") != NULL) - ffStrbufSetS(&device->interconnect, "NVMe"); - else if (strstr(pathSysDeviceReal, "/ata") != NULL) - ffStrbufSetS(&device->interconnect, "ATA"); - else if (strstr(pathSysDeviceReal, "/scsi") != NULL) - ffStrbufSetS(&device->interconnect, "SCSI"); - else + char pathSysDeviceLink[64]; + snprintf(pathSysDeviceLink, ARRAY_SIZE(pathSysDeviceLink), "/sys/block/%s/device", devName); + char pathSysDeviceReal[PATH_MAX]; + if (realpath(pathSysDeviceLink, pathSysDeviceReal)) { - if (ffAppendFileBufferRelative(devfd, "transport", &device->interconnect)) - ffStrbufTrimRightSpace(&device->interconnect); + if (strstr(pathSysDeviceReal, "/usb") != NULL) + ffStrbufSetStatic(&device->interconnect, "USB"); + else if (strstr(pathSysDeviceReal, "/ata") != NULL) + ffStrbufSetStatic(&device->interconnect, "ATA"); + else if (strstr(pathSysDeviceReal, "/scsi") != NULL) + ffStrbufSetStatic(&device->interconnect, "SCSI"); + else if (strstr(pathSysDeviceReal, "/nvme") != NULL) + ffStrbufSetStatic(&device->interconnect, "NVMe"); + else + { + if (ffAppendFileBufferRelative(devfd, "transport", &device->interconnect)) + ffStrbufTrimRightSpace(&device->interconnect); + } } } }