From d3c9178f111a64f83eb8803dc7bfbb4ea967f7b5 Mon Sep 17 00:00:00 2001 From: giovannilentini Date: Wed, 18 Nov 2026 14:24:03 +0100 Subject: [PATCH] PhysicalDisk (macOS): adds SMART failing status for ATA disks Ref: #2726 --- CHANGELOG.md | 2 +- .../physicaldisk/physicaldisk_apple.c | 42 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f71e08bd..3d85950a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +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}`. - * On Linux and Windows, ATA disks report the SMART `failing` status as a 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_apple.c b/src/detection/physicaldisk/physicaldisk_apple.c index 295390216..38217bd63 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;