diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ff57fdab7..3d85950a74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Features: * Added opt-in drive health detection for NVMe disks, via udisks2 on Linux (no root required), NVMe SMART on macOS and the NVMe health log on Windows. (#2726, PhysicalDisk, Linux / macOS / Windows) * Enable it with `"health": true`. The default output appends `Health <100 - percentage used>%` and any critical warnings. * New format args: `{health}`, `{percent-used}`, `{available-spare}` and `{critical-warning}`. + * ATA disks report the SMART `failing` status as a critical warning. Changes: * Improved PCI GPU name resolution by checking udev's hardware database and the NVIDIA driver's procfs before falling back to pci.ids. (GPU, Linux) diff --git a/src/detection/physicaldisk/physicaldisk.h b/src/detection/physicaldisk/physicaldisk.h index 7520c97121..3d561f8e5f 100644 --- a/src/detection/physicaldisk/physicaldisk.h +++ b/src/detection/physicaldisk/physicaldisk.h @@ -13,6 +13,7 @@ typedef enum FFPhysicalDiskWarning: uint8_t { FF_PHYSICALDISK_WARNING_READONLY = 1 << 3, FF_PHYSICALDISK_WARNING_BACKUP = 1 << 4, FF_PHYSICALDISK_WARNING_PMR_READONLY = 1 << 5, + FF_PHYSICALDISK_WARNING_FAILING = 1 << 6, FF_PHYSICALDISK_WARNING_UNSET = UINT8_MAX, } FFPhysicalDiskWarning; static_assert(sizeof(FFPhysicalDiskWarning) == sizeof(uint8_t), ""); diff --git a/src/detection/physicaldisk/physicaldisk_apple.c b/src/detection/physicaldisk/physicaldisk_apple.c index 2953902168..38217bd639 100644 --- a/src/detection/physicaldisk/physicaldisk_apple.c +++ b/src/detection/physicaldisk/physicaldisk_apple.c @@ -8,18 +8,17 @@ #include #include #include +#include #ifdef MAC_OS_X_VERSION_10_15 #include #endif -#ifdef MAC_OS_X_VERSION_10_15 static inline void wrapIoDestroyPlugInInterface(IOCFPlugInInterface*** pluginInf) { assert(pluginInf); if (*pluginInf) { IODestroyPlugInInterface(*pluginInf); } } -#endif static const char* detectNvmeSmart(io_service_t entryPhysical, FFPhysicalDiskResult* device, FFPhysicalDiskOptions* options) { #ifdef MAC_OS_X_VERSION_10_15 @@ -59,6 +58,38 @@ static const char* detectNvmeSmart(io_service_t entryPhysical, FFPhysicalDiskRes #endif } +static const char* detectAtaSmart(io_service_t entryPhysical, FFPhysicalDiskResult* device) { + [[gnu::cleanup(wrapIoDestroyPlugInInterface)]] IOCFPlugInInterface** pluginInf = nullptr; + int32_t score; + if (IOCreatePlugInInterfaceForService(entryPhysical, kIOATASMARTUserClientTypeID, kIOCFPlugInInterfaceID, &pluginInf, &score) != kIOReturnSuccess) { + FF_DEBUG("IOCreatePlugInInterfaceForService(kIOATASMARTUserClientTypeID) failed"); + return "IOCreatePlugInInterfaceForService() failed"; + } + + IOATASMARTInterface** smartInf = nullptr; + if ((*pluginInf)->QueryInterface(pluginInf, CFUUIDGetUUIDBytes(kIOATASMARTInterfaceID), (LPVOID) &smartInf) != kIOReturnSuccess) { + FF_DEBUG("QueryInterface(kIOATASMARTInterfaceID) failed"); + return "QueryInterface() failed"; + } + + Boolean exceeded = false; + const char* error = nullptr; + if ((*smartInf)->SMARTReturnStatus(smartInf, &exceeded) == kIOReturnSuccess) { + if (device->criticalWarning == FF_PHYSICALDISK_WARNING_UNSET) { + device->criticalWarning = FF_PHYSICALDISK_WARNING_NONE; + } + if (exceeded) { + device->criticalWarning |= FF_PHYSICALDISK_WARNING_FAILING; + } + } else { + FF_DEBUG("SMARTReturnStatus() failed"); + error = "SMARTReturnStatus() failed"; + } + + (*pluginInf)->Release(smartInf); + return error; +} + const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) { FF_IOOBJECT_AUTO_RELEASE io_iterator_t iterator = 0; if (IOServiceGetMatchingServices(MACH_PORT_NULL, IOServiceMatching(kIOBlockStorageDriverClass), &iterator) != KERN_SUCCESS) { @@ -200,6 +231,13 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) } } #endif + + if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && options->health) { + FF_CFTYPE_AUTO_RELEASE CFBooleanRef ataSMARTCapable = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertySMARTCapableKey), nullptr, kNilOptions); + if (ataSMARTCapable && CFBooleanGetValue(ataSMARTCapable)) { + detectAtaSmart(entryPhysical, device); + } + } } return nullptr; diff --git a/src/detection/physicaldisk/physicaldisk_linux.c b/src/detection/physicaldisk/physicaldisk_linux.c index 9db2a0cb9f..549b9d5b93 100644 --- a/src/detection/physicaldisk/physicaldisk_linux.c +++ b/src/detection/physicaldisk/physicaldisk_linux.c @@ -14,6 +14,7 @@ #define FF_UDISKS_BUS "org.freedesktop.UDisks2" #define FF_UDISKS_NVME_CONTROLLER "org.freedesktop.UDisks2.NVMe.Controller" + #define FF_UDISKS_DRIVE_ATA "org.freedesktop.UDisks2.Drive.Ata" static bool hasUDisks(FFDBusData* dbus) { DBusMessage* reply = ffDBusGetMethodReply(dbus, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameHasOwner", FF_UDISKS_BUS, nullptr); @@ -80,7 +81,7 @@ static void parseSmartAttributes(FFDBusData* dbus, DBusMessage* reply, FFPhysica DBusMessageIter entryIter; dbus->lib->ffdbus_message_iter_recurse(&dictIter, &entryIter); if (dbus->lib->ffdbus_message_iter_get_arg_type(&entryIter) != DBUS_TYPE_STRING) { - return; + break; } const char* key; @@ -102,43 +103,81 @@ static void parseSmartAttributes(FFDBusData* dbus, DBusMessage* reply, FFPhysica } } -static void detectNvmeHealth(FFDBusData* dbus, const char* devName, FFPhysicalDiskResult* device) { - char blockPath[128]; - snprintf(blockPath, ARRAY_SIZE(blockPath), "/org/freedesktop/UDisks2/block_devices/%s", devName); - - FF_STRBUF_AUTO_DESTROY drivePath = ffStrbufCreate(); - if (!ffDBusGetPropertyString(dbus, FF_UDISKS_BUS, blockPath, "org.freedesktop.UDisks2.Block", "Drive", &drivePath) || drivePath.length <= 1) { - FF_DEBUG("Failed to get udisks drive of %s", devName); - return; - } - +static void detectNvmeHealth(FFDBusData* dbus, const char* drivePath, FFPhysicalDiskResult* device) { uint64_t smartUpdated = 0; - if (!ffDBusGetPropertyUint(dbus, FF_UDISKS_BUS, drivePath.chars, FF_UDISKS_NVME_CONTROLLER, "SmartUpdated", &smartUpdated)) { - FF_DEBUG("%s has no NVMe controller interface", drivePath.chars); + if (!ffDBusGetPropertyUint(dbus, FF_UDISKS_BUS, drivePath, FF_UDISKS_NVME_CONTROLLER, "SmartUpdated", &smartUpdated)) { + FF_DEBUG("%s has no NVMe controller interface", drivePath); return; } if (smartUpdated == 0) { - FF_DEBUG("udisks has not collected SMART data of %s yet", drivePath.chars); + FF_DEBUG("udisks has not collected SMART data of %s yet", drivePath); return; } - DBusMessage* reply = ffDBusGetProperty(dbus, FF_UDISKS_BUS, drivePath.chars, FF_UDISKS_NVME_CONTROLLER, "SmartCriticalWarning"); + DBusMessage* reply = ffDBusGetProperty(dbus, FF_UDISKS_BUS, drivePath, FF_UDISKS_NVME_CONTROLLER, "SmartCriticalWarning"); if (reply == nullptr) { - FF_DEBUG("Failed to get SmartCriticalWarning of %s", drivePath.chars); + FF_DEBUG("Failed to get SmartCriticalWarning of %s", drivePath); return; } device->criticalWarning = parseCriticalWarning(dbus, reply); dbus->lib->ffdbus_message_unref(reply); - reply = ffDBusGetMethodReplyEmptyDict(dbus, FF_UDISKS_BUS, drivePath.chars, FF_UDISKS_NVME_CONTROLLER, "SmartGetAttributes"); + reply = ffDBusGetMethodReplyEmptyDict(dbus, FF_UDISKS_BUS, drivePath, FF_UDISKS_NVME_CONTROLLER, "SmartGetAttributes"); if (reply == nullptr) { - FF_DEBUG("SmartGetAttributes failed for %s", drivePath.chars); + FF_DEBUG("SmartGetAttributes failed for %s", drivePath); return; } parseSmartAttributes(dbus, reply, device); dbus->lib->ffdbus_message_unref(reply); } +static void detectAtaHealth(FFDBusData* dbus, const char* drivePath, FFPhysicalDiskResult* device) { + DBusMessage* reply = ffDBusGetAllProperties(dbus, FF_UDISKS_BUS, drivePath, FF_UDISKS_DRIVE_ATA); + if (reply == nullptr) { + FF_DEBUG("%s has no ATA interface", drivePath); + return; + } + + bool smartFailing = false; + uint64_t smartUpdated = 0; + DBusMessageIter iter; + if (dbus->lib->ffdbus_message_iter_init(reply, &iter) && dbus->lib->ffdbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_ARRAY) { + DBusMessageIter dictIter; + dbus->lib->ffdbus_message_iter_recurse(&iter, &dictIter); + while (dbus->lib->ffdbus_message_iter_get_arg_type(&dictIter) == DBUS_TYPE_DICT_ENTRY) { + DBusMessageIter entryIter; + dbus->lib->ffdbus_message_iter_recurse(&dictIter, &entryIter); + if (dbus->lib->ffdbus_message_iter_get_arg_type(&entryIter) != DBUS_TYPE_STRING) { + break; + } + + const char* key; + dbus->lib->ffdbus_message_iter_get_basic(&entryIter, &key); + dbus->lib->ffdbus_message_iter_next(&entryIter); + + if (ffStrEquals(key, "SmartUpdated")) { + ffDBusGetUint(dbus, &entryIter, &smartUpdated); + } else if (ffStrEquals(key, "SmartFailing")) { + ffDBusGetBool(dbus, &entryIter, &smartFailing); + } + + dbus->lib->ffdbus_message_iter_next(&dictIter); + } + } + dbus->lib->ffdbus_message_unref(reply); + + if (smartUpdated == 0) { + FF_DEBUG("udisks has not collected SMART data of %s yet", drivePath); + return; + } + if (device->criticalWarning == FF_PHYSICALDISK_WARNING_UNSET) { + device->criticalWarning = FF_PHYSICALDISK_WARNING_NONE; + } + if (smartFailing) { + device->criticalWarning |= FF_PHYSICALDISK_WARNING_FAILING; + } +} + static void detectHealth(FFlist* result) { FF_DBUS_AUTO_DESTROY_DATA FFDBusData dbus = {}; const char* error = ffDBusLoadData(DBUS_BUS_SYSTEM, &dbus); @@ -152,8 +191,23 @@ static void detectHealth(FFlist* result) { } FF_LIST_FOR_EACH (FFPhysicalDiskResult, device, *result) { + if (device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) { + continue; + } + + char blockPath[128]; + snprintf(blockPath, ARRAY_SIZE(blockPath), "/org/freedesktop/UDisks2/block_devices/%s", device->devPath.chars + strlen("/dev/")); + + FF_STRBUF_AUTO_DESTROY drivePath = ffStrbufCreate(); + if (!ffDBusGetPropertyString(&dbus, FF_UDISKS_BUS, blockPath, "org.freedesktop.UDisks2.Block", "Drive", &drivePath) || drivePath.length <= 1) { + FF_DEBUG("Failed to get udisks drive of %s", device->devPath.chars); + continue; + } + if (ffStrbufStartsWithS(&device->devPath, "/dev/nvme")) { - detectNvmeHealth(&dbus, device->devPath.chars + strlen("/dev/"), device); + detectNvmeHealth(&dbus, drivePath.chars, device); + } else { + detectAtaHealth(&dbus, drivePath.chars, device); } } } diff --git a/src/detection/physicaldisk/physicaldisk_windows.c b/src/detection/physicaldisk/physicaldisk_windows.c index 64b0482321..440ea382fd 100644 --- a/src/detection/physicaldisk/physicaldisk_windows.c +++ b/src/detection/physicaldisk/physicaldisk_windows.c @@ -340,6 +340,32 @@ static const char* detectPhysicalDisk(const char* physicalType, const wchar_t* s } } + if (options->health && sdd && !(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && (sdd->BusType == BusTypeAta || sdd->BusType == BusTypeSata)) { + STORAGE_PREDICT_FAILURE spf = {}; + if (DeviceIoControl( + hDevice, + IOCTL_STORAGE_PREDICT_FAILURE, + nullptr, + 0, + &spf, + sizeof(spf), + &retSize, + nullptr)) { + if (retSize >= sizeof(spf)) { + if (device->criticalWarning == FF_PHYSICALDISK_WARNING_UNSET) { + device->criticalWarning = FF_PHYSICALDISK_WARNING_NONE; + } + if (spf.PredictFailure) { + device->criticalWarning |= FF_PHYSICALDISK_WARNING_FAILING; + } + } else { + FF_DEBUG("ATA failure prediction reply too short: %u bytes", (unsigned) retSize); + } + } else { + FF_DEBUG("Failed to query ATA failure prediction: %s", ffDebugWin32Error(GetLastError())); + } + } + return nullptr; } diff --git a/src/modules/physicaldisk/physicaldisk.c b/src/modules/physicaldisk/physicaldisk.c index 8a951cc1ef..3ae14dfa20 100644 --- a/src/modules/physicaldisk/physicaldisk.c +++ b/src/modules/physicaldisk/physicaldisk.c @@ -17,6 +17,7 @@ static const char* const warningNames[] = { "read-only", "backup failed", "pmr read-only", + "failing", }; static inline double getHealth(const FFPhysicalDiskResult* dev) {