Log the interface of the device
Some checks are pending
Build / build-mac (push) Waiting to run
Build / build-linux (push) Waiting to run
Build / build-win (push) Waiting to run
CodeQL Advanced / Analyze (c-cpp) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run

This commit is contained in:
Andrea Mazzoleni 2026-01-29 16:47:02 +01:00
parent edcec26506
commit 5a5668851d
6 changed files with 125 additions and 36 deletions

View file

@ -527,6 +527,8 @@ static void state_info_log(devinfo_t* devinfo)
log_tag("attr:%s:%s:family:%s\n", devinfo->file, devinfo->name, esc_tag(devinfo->family, esc_buffer));
if (devinfo->model[0])
log_tag("attr:%s:%s:model:%s\n", devinfo->file, devinfo->name, esc_tag(devinfo->model, esc_buffer));
if (devinfo->interf[0])
log_tag("attr:%s:%s:interface:%s\n", devinfo->file, devinfo->name, esc_tag(devinfo->interf, esc_buffer));
if (devinfo->info[INFO_SIZE] != SMART_UNASSIGNED)
log_tag("attr:%s:%s:size:%" PRIu64 "\n", devinfo->file, devinfo->name, devinfo->info[INFO_SIZE]);
if (devinfo->info[INFO_ROTATION_RATE] != SMART_UNASSIGNED)
@ -897,6 +899,7 @@ int devtest(tommy_list* high, tommy_list* low, int operation)
pathprint(entry->serial, sizeof(entry->serial), "FAKE_%s", devinfo->serial);
pathprint(entry->family, sizeof(entry->family), "FAKE_%s", devinfo->family);
pathprint(entry->model, sizeof(entry->model), "FAKE_%s", devinfo->model);
pathprint(entry->interf, sizeof(entry->interf), "FAKE_%s", devinfo->interf);
pathprint(entry->file, sizeof(entry->file), "FAKE_%s", devinfo->file);
pathcpy(entry->name, sizeof(entry->name), devinfo->name);
entry->info[INFO_SIZE] = count * TERA;

View file

@ -2483,7 +2483,7 @@ static int devscan(tommy_list* list)
/**
* Get SMART attributes.
*/
static int devsmart(uint64_t device, const char* name, const char* smartctl, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model)
static int devsmart(uint64_t device, const char* name, const char* smartctl, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model, char* inter)
{
char conv_buf[CONV_MAX];
WCHAR cmd[MAX_PATH + 128];
@ -2517,7 +2517,7 @@ retry:
/* LCOV_EXCL_STOP */
}
if (smartctl_attribute(f, file, name, smart, info, serial, family, model) != 0) {
if (smartctl_attribute(f, file, name, smart, info, serial, family, model, inter) != 0) {
/* LCOV_EXCL_START */
pclose(f);
log_tag("device:%s:%s:shell\n", file, name);
@ -2570,7 +2570,7 @@ retry:
}
static void devattr_property(HANDLE h, char* serial, char* model)
static void devattr_property(HANDLE h, char* serial, char* model, char* inter)
{
STORAGE_PROPERTY_QUERY query = { 0 };
STORAGE_DESCRIPTOR_HEADER header = { 0 };
@ -2606,6 +2606,42 @@ static void devattr_property(HANDLE h, char* serial, char* model)
strtrim(serial);
}
#define BusTypeVirtual 0x0E
#define BusTypeFileBackedVirtual 0x0F
#define BusTypeSpaces 0x10
#define BusTypeNvme 0x11
#define BusTypeSCM 0x12
#define BusTypeUfs 0x13
#define BusTypeNvmeof 0x14
/* always override interface as more detailed than smartctl */
const char* type = 0;
switch ((int)desc->BusType) { /* cast to int to avoid warnings about enum unlisted */
case BusTypeScsi : type = "SCSI"; break;
case BusTypeAtapi : type = "ATAPI"; break;
case BusTypeAta : type = "ATA"; break;
case BusType1394 : type = "FireWire"; break;
case BusTypeSsa : type = "SSA"; break;
case BusTypeFibre : type = "Fibre"; break;
case BusTypeUsb : type = "USB"; break;
case BusTypeRAID : type = "RAID"; break;
case BusTypeiScsi : type = "iSCSI"; break;
case BusTypeSas : type = "SAS"; break;
case BusTypeSata : type = "SATA"; break;
case BusTypeSd : type = "SD"; break;
case BusTypeMmc : type = "MMC"; break;
case BusTypeVirtual : type = "Virtual"; break;
case BusTypeFileBackedVirtual : type = "Virtual"; break;
case BusTypeSpaces : type = "Storage Spaces"; break;
case BusTypeNvme : type = "NVMe"; break;
case BusTypeSCM : type = "SCM"; break;
case BusTypeUfs : type = "UFS"; break;
case BusTypeNvmeof : type = "NVMe"; break;
default :
}
if (type)
snprintf(inter, SMART_MAX, "%s", type);
free(buffer);
}
@ -2640,7 +2676,7 @@ static void devattr_rotational(HANDLE h, uint64_t* rotational)
/**
* Get device attributes.
*/
static void devattr(uint64_t device, const char* name, const char* wfile, uint64_t* info, char* serial, char* family, char* model)
static void devattr(uint64_t device, const char* name, const char* wfile, uint64_t* info, char* serial, char* family, char* model, char* interf)
{
HANDLE h;
wchar_t conv_buf[CONV_MAX];
@ -2666,7 +2702,7 @@ static void devattr(uint64_t device, const char* name, const char* wfile, uint64
devattr_rotational(h, &info[INFO_ROTATION_RATE]);
if (*model == 0 || *serial == 0)
devattr_property(h, serial, model);
devattr_property(h, serial, model, interf);
if (!CloseHandle(h)) {
DWORD error = GetLastError();
@ -2679,7 +2715,7 @@ static void devattr(uint64_t device, const char* name, const char* wfile, uint64
/**
* Get POWER state
*/
static int devprobe(uint64_t device, const char* name, const char* smartctl, int* power, uint64_t* info, char* serial, char* family, char* model)
static int devprobe(uint64_t device, const char* name, const char* smartctl, int* power, uint64_t* info, char* serial, char* family, char* model, char* inter)
{
char conv_buf[CONV_MAX];
WCHAR cmd[MAX_PATH + 128];
@ -2713,7 +2749,7 @@ retry:
/* LCOV_EXCL_STOP */
}
if (smartctl_attribute(f, file, name, 0, info, serial, family, model) != 0) {
if (smartctl_attribute(f, file, name, 0, info, serial, family, model, inter) != 0) {
/* LCOV_EXCL_START */
pclose(f);
log_tag("device:%s:%s:shell\n", file, name);
@ -2865,7 +2901,7 @@ static int devdownifup(uint64_t device, const char* name, const char* smartctl,
{
*power = POWER_UNKNOWN;
if (devprobe(device, name, smartctl, power, 0, 0, 0, 0) != 0)
if (devprobe(device, name, smartctl, power, 0, 0, 0, 0, 0) != 0)
return -1;
if (*power == POWER_ACTIVE)
@ -3013,7 +3049,7 @@ static void* thread_smart(void* arg)
{
devinfo_t* devinfo = arg;
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model) != 0) {
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -3025,7 +3061,7 @@ static void* thread_smart(void* arg)
* smartctl intentionally skips queries on devices in standby mode
* to prevent accidentally spinning them up.
*/
devattr(devinfo->device, devinfo->name, devinfo->wfile, devinfo->info, devinfo->serial, devinfo->family, devinfo->model);
devattr(devinfo->device, devinfo->name, devinfo->wfile, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf);
return 0;
}
@ -3037,7 +3073,7 @@ static void* thread_probe(void* arg)
{
devinfo_t* devinfo = arg;
if (devprobe(devinfo->device, devinfo->name, devinfo->smartctl, &devinfo->power, devinfo->info, devinfo->serial, devinfo->family, devinfo->model) != 0) {
if (devprobe(devinfo->device, devinfo->name, devinfo->smartctl, &devinfo->power, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -3049,7 +3085,7 @@ static void* thread_probe(void* arg)
* smartctl intentionally skips queries on devices in standby mode
* to prevent accidentally spinning them up.
*/
devattr(devinfo->device, devinfo->name, devinfo->wfile, devinfo->info, devinfo->serial, devinfo->family, devinfo->model);
devattr(devinfo->device, devinfo->name, devinfo->wfile, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf);
return 0;
}

View file

@ -530,6 +530,7 @@ struct devinfo_struct {
char serial[SMART_MAX]; /**< Serial number. */
char family[SMART_MAX]; /**< Family. */
char model[SMART_MAX]; /**< Model. */
char interf[SMART_MAX]; /**< Interface of the device: ata, sata, pata, nvme, usb */
int power; /**< POWER mode. */
#if HAVE_THREAD
thread_id_t thread;

View file

@ -1693,7 +1693,7 @@ static int smatch(const char* str, const char* pattern)
return 0;
}
int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model)
int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model, char* inter)
{
unsigned i;
int inside;
@ -1702,6 +1702,7 @@ int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* sm
char dummy_serial[SMART_MAX];
char dummy_family[SMART_MAX];
char dummy_model[SMART_MAX];
char dummy_interface[SMART_MAX];
/* dummy attributes */
if (!smart)
@ -1714,11 +1715,14 @@ int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* sm
family = dummy_family;
if (!model)
model = dummy_model;
if (!inter)
model = dummy_interface;
/* preclear attributes */
*serial = 0;
*family = 0;
*model = 0;
*inter = 0;
for (i = 0; i < SMART_COUNT; ++i)
smart[i] = SMART_UNASSIGNED;
for (i = 0; i < INFO_COUNT; ++i)
@ -1772,31 +1776,67 @@ int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* sm
} else if (sscanf(s, "Serial number: %63s", serial) == 1) {
strtrim(serial);
/* SCSI */
} else if (sscanf(s, "Transport protocol: %63[^\n]", inter) == 1) {
if (strcmp(inter, "Fibre channel (FCP-4)") == 0)
strcpy(inter, "Fibre");
else if (strcmp(inter, "SSA") == 0)
strcpy(inter, "SSA");
else if (strcmp(inter, "IEEE 1394 (SBP-3)") == 0)
strcpy(inter, "FireWire");
else if (strcmp(inter, "RDMA (SRP)") == 0)
strcpy(inter, "SCSI");
else if (strcmp(inter, "iSCSI") == 0)
strcpy(inter, "iSCSI");
else if (strcmp(inter, "SAS (SPL-4)") == 0)
strcpy(inter, "SAS");
else if (strcmp(inter, "ADT") == 0)
strcpy(inter, "SCSI");
else if (strcmp(inter, "ATA (ACS-2)") == 0)
strcpy(inter, "SATA");
else if (strcmp(inter, "UAS") == 0)
strcpy(inter, "USB");
else if (strcmp(inter, "SOP") == 0)
strcpy(inter, "NVMe");
else if (strcmp(inter, "PCIe") == 0)
strcpy(inter, "PCIe");
else
strcpy(inter, "SCSI");
} else if (sscanf(s, "Elements in grown defect list: %" SCNu64, &smart[SMART_REALLOCATED_SECTOR_COUNT]) == 1) {
} else if (sscanf(s, "Current Drive Temperature: %" SCNu64, &smart[SMART_TEMPERATURE_CELSIUS]) == 1) {
} else if (sscanf(s, "Drive Trip Temperature: %" SCNu64, &smart[SMART_AIRFLOW_TEMPERATURE_CELSIUS]) == 1) {
} else if (sscanf(s, "Accumulated start-stop cycles: %" SCNu64, &smart[SMART_START_STOP_COUNT]) == 1) {
} else if (sscanf(s, "Accumulated load-unload cycles: %" SCNu64, &smart[SMART_LOAD_CYCLE_COUNT]) == 1) {
} else if (sscanf(s, " number of hours powered up = %" SCNu64, &smart[SMART_POWER_ON_HOURS]) == 1) { /* note "n" of "number" lower case */
/* ATA */
} else if (sscanf(s, "Serial Number: %63s", serial) == 1) {
} else if (smatch(s, "ID#") == 0) {
inside = 1;
} else if (smatch(s, "No Errors Logged") == 0) { /* ATA */
smart[SMART_ERROR_PROTOCOL] = 0;
} else if (sscanf(s, "ATA Error Count: %" SCNu64, &raw) == 1) { /* ATA */
smart[SMART_ERROR_PROTOCOL] = raw;
} else if (sscanf(s, "Media and Data Integrity Errors: %" SCNu64, &raw) == 1) { /* NVME */
smart[SMART_ERROR_MEDIUM] = raw;
} else if (sscanf(s, "Error Information Log Entries: %" SCNu64, &raw) == 1) { /* NVME */
} else if (sscanf(s, "Non-medium error count: %" SCNu64, &raw) == 1) {
smart[SMART_ERROR_PROTOCOL] = raw;
} else if (sscanf(s, "Medium error count: %" SCNu64, &raw) == 1) {
smart[SMART_ERROR_MEDIUM] = raw;
} else if (sscanf(s, "Non-medium error count: %" SCNu64, &raw) == 1) { /* SCSI */
/* ATA */
} else if (smatch(s, "ATA Version is:") == 0) {
strcpy(inter, "ATA");
} else if (smatch(s, "SATA Version is:") == 0) {
strcpy(inter, "SATA");
} else if (smatch(s, "Transport Type: PCIe") == 0) {
strcpy(inter, "PCIe");
} else if (smatch(s, "Transport Type: Parallel") == 0) {
strcpy(inter, "PATA");
} else if (smatch(s, "No Errors Logged") == 0) {
smart[SMART_ERROR_PROTOCOL] = 0;
} else if (sscanf(s, "ATA Error Count: %" SCNu64, &raw) == 1) {
smart[SMART_ERROR_PROTOCOL] = raw;
/* NVME */
} else if (smatch(s, "NVMe Version:") == 0) {
strcpy(inter, "NVMe");
} else if (sscanf(s, "Media and Data Integrity Errors: %" SCNu64, &raw) == 1) {
smart[SMART_ERROR_MEDIUM] = raw;
} else if (sscanf(s, "Error Information Log Entries: %" SCNu64, &raw) == 1) {
smart[SMART_ERROR_PROTOCOL] = raw;
} else if (sscanf(s, "Percentage Used: %" SCNu64 "%%", &raw) == 1) { /* NVME */
if (raw <= 100)
smart[SMART_WEAR_LEVEL] = raw;
/* Attributes */
} else if (smatch(s, "ID#") == 0) {
inside = 1;
} else if (inside) {
if (sscanf(s, "%u %*s %*s %" SCNu64 " %*s %*s %*s %*s %*s %" SCNu64, &id, &normalized, &raw) != 3) {
log_fatal("Invalid smartctl line '%s'.\n", s);

View file

@ -448,7 +448,7 @@ void malloc_fail(size_t size);
* Read smartctl attributes from a stream.
* Return 0 on success.
*/
int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model);
int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model, char* interf);
/**
* Flush smartctl output from a stream.

View file

@ -1247,7 +1247,7 @@ static int devscan(tommy_list* list)
* Get SMART attributes.
*/
#if HAVE_LINUX_DEVICE
static int devsmart(dev_t device, const char* name, const char* smartctl, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model)
static int devsmart(dev_t device, const char* name, const char* smartctl, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model, char* interface)
{
char cmd[PATH_MAX + 64];
char file[PATH_MAX];
@ -1290,7 +1290,7 @@ static int devsmart(dev_t device, const char* name, const char* smartctl, uint64
/* LCOV_EXCL_STOP */
}
if (smartctl_attribute(f, file, name, smart, info, serial, family, model) != 0) {
if (smartctl_attribute(f, file, name, smart, info, serial, family, model, interface) != 0) {
/* LCOV_EXCL_START */
pclose(f);
log_tag("device:%s:%s:shell\n", file, name);
@ -1401,7 +1401,7 @@ static int devstat(dev_t device, uint64_t* count)
* Get device attributes.
*/
#if HAVE_LINUX_DEVICE
static void devattr(dev_t device, uint64_t* info, char* serial, char* family, char* model)
static void devattr(dev_t device, uint64_t* info, char* serial, char* family, char* model, char* interface)
{
char path[PATH_MAX];
char buf[512];
@ -1469,6 +1469,15 @@ static void devattr(dev_t device, uint64_t* info, char* serial, char* family, ch
}
}
}
/* always override interface if it's usb */
pathprint(path, sizeof(path), "/sys/dev/block/%u:%u", major(device), minor(device));
ret = readlink(path, buf, sizeof(buf));
if (ret > 0 && (unsigned)ret < sizeof(buf)) {
buf[ret] = 0;
if (strstr(buf, "usb") != 0)
strcpy(interface, "USB");
}
}
#endif
@ -1476,7 +1485,7 @@ static void devattr(dev_t device, uint64_t* info, char* serial, char* family, ch
* Get POWER state.
*/
#if HAVE_LINUX_DEVICE
static int devprobe(dev_t device, const char* name, const char* smartctl, int* power, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model)
static int devprobe(dev_t device, const char* name, const char* smartctl, int* power, uint64_t* smart, uint64_t* info, char* serial, char* family, char* model, char* interface)
{
char cmd[PATH_MAX + 64];
char file[PATH_MAX];
@ -1519,7 +1528,7 @@ static int devprobe(dev_t device, const char* name, const char* smartctl, int* p
/* LCOV_EXCL_STOP */
}
if (smartctl_attribute(f, file, name, smart, info, serial, family, model) != 0) {
if (smartctl_attribute(f, file, name, smart, info, serial, family, model, interface) != 0) {
/* LCOV_EXCL_START */
pclose(f);
log_tag("device:%s:%s:shell\n", file, name);
@ -1656,7 +1665,7 @@ static int devdownifup(dev_t device, const char* name, const char* smartctl, int
{
*power = POWER_UNKNOWN;
if (devprobe(device, name, smartctl, power, 0, 0, 0, 0, 0) != 0)
if (devprobe(device, name, smartctl, power, 0, 0, 0, 0, 0, 0) != 0)
return -1;
if (*power == POWER_ACTIVE)
@ -1851,7 +1860,7 @@ static void* thread_smart(void* arg)
#if HAVE_LINUX_DEVICE
devinfo_t* devinfo = arg;
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model) != 0) {
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -1863,7 +1872,7 @@ static void* thread_smart(void* arg)
* smartctl intentionally skips queries on devices in standby mode
* to prevent accidentally spinning them up.
*/
devattr(devinfo->device, devinfo->info, devinfo->serial, devinfo->family, devinfo->model);
devattr(devinfo->device, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf);
return 0;
#else
@ -1880,7 +1889,7 @@ static void* thread_probe(void* arg)
#if HAVE_LINUX_DEVICE
devinfo_t* devinfo = arg;
if (devprobe(devinfo->device, devinfo->name, devinfo->smartctl, &devinfo->power, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model) != 0) {
if (devprobe(devinfo->device, devinfo->name, devinfo->smartctl, &devinfo->power, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -1892,7 +1901,7 @@ static void* thread_probe(void* arg)
* smartctl intentionally skips queries on devices in standby mode
* to prevent accidentally spinning them up.
*/
devattr(devinfo->device, devinfo->info, devinfo->serial, devinfo->family, devinfo->model);
devattr(devinfo->device, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf);
return 0;
#else