Files
fastfetch/src/detection/physicaldisk/physicaldisk_linux.c
T

186 lines
6.7 KiB
C
Raw Normal View History

2023-12-18 10:43:20 +08:00
#include "physicaldisk.h"
2026-01-06 09:48:38 +08:00
#include "common/io.h"
#include "common/properties.h"
#include "common/stringUtils.h"
2023-12-18 10:43:20 +08:00
#include <ctype.h>
#include <limits.h>
2024-10-12 13:29:58 +08:00
#include <unistd.h>
#include <fcntl.h>
2023-12-18 10:43:20 +08:00
2026-03-29 09:28:49 +08:00
static double detectNvmeTemp(int devfd) {
2024-10-12 13:29:58 +08:00
char pathHwmon[] = "hwmon$/temp1_input";
for (char c = '0'; c <= '9'; c++) // hopefully there's only one digit
{
2024-10-12 13:29:58 +08:00
pathHwmon[strlen("hwmon")] = c;
char buffer[64];
2024-10-18 16:15:48 +08:00
ssize_t size = ffReadFileDataRelative(devfd, pathHwmon, ARRAY_SIZE(buffer), buffer);
2026-03-29 09:28:49 +08:00
if (size > 0) {
buffer[size] = '\0';
double temp = strtod(buffer, NULL);
return temp > 0 && temp < 10000000 /*VMware*/ ? temp / 1000 : FF_PHYSICALDISK_TEMP_UNSET;
}
}
return FF_PHYSICALDISK_TEMP_UNSET;
}
2026-03-29 09:28:49 +08:00
static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOptions* options, FFlist* result) {
2024-10-15 10:29:50 +08:00
int devfd = openat(dfd, "device", O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
2023-12-18 10:43:20 +08:00
2024-10-12 13:29:58 +08:00
FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
2023-12-18 10:43:20 +08:00
2026-03-31 15:56:04 +08:00
if (devfd > 0) {
2026-03-29 09:28:49 +08:00
if (ffAppendFileBufferRelative(devfd, "vendor", &name)) {
2024-10-12 13:29:58 +08:00
ffStrbufTrimRightSpace(&name);
2026-03-29 09:28:49 +08:00
if (name.length > 0) {
2024-10-12 13:29:58 +08:00
ffStrbufAppendC(&name, ' ');
2026-03-29 09:28:49 +08:00
}
2024-10-12 13:29:58 +08:00
}
2023-12-18 10:43:20 +08:00
2024-10-12 13:29:58 +08:00
ffAppendFileBufferRelative(devfd, "model", &name);
ffStrbufTrimRightSpace(&name);
2023-12-18 10:43:20 +08:00
2026-03-29 09:28:49 +08:00
if (name.length == 0) {
2024-10-12 13:29:58 +08:00
ffStrbufSetS(&name, devName);
2026-03-29 09:28:49 +08:00
}
2023-12-18 10:43:20 +08:00
2026-03-29 09:28:49 +08:00
if (ffStrStartsWith(devName, "nvme")) {
2024-10-12 13:29:58 +08:00
int devid, nsid;
2026-03-29 09:28:49 +08:00
if (sscanf(devName, "nvme%dn%d", &devid, &nsid) == 2) {
2024-10-12 13:29:58 +08:00
bool multiNs = nsid > 1;
2026-03-29 09:28:49 +08:00
if (!multiNs) {
2024-10-12 13:29:58 +08:00
char pathSysBlock[32];
2024-10-18 16:15:48 +08:00
snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "/dev/nvme%dn2", devid);
2024-10-12 13:29:58 +08:00
multiNs = access(pathSysBlock, F_OK) == 0;
}
2026-03-29 09:28:49 +08:00
if (multiNs) {
2024-10-12 13:29:58 +08:00
// In Asahi Linux, there are multiple namespaces for the same NVMe drive.
ffStrbufAppendF(&name, " - %d", nsid);
}
2023-12-18 10:43:20 +08:00
}
}
2026-03-29 09:28:49 +08:00
if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
2024-10-12 13:29:58 +08:00
return;
2026-03-29 09:28:49 +08:00
}
2026-03-31 15:56:04 +08:00
} else {
ffStrbufSetS(&name, devName);
2024-10-12 13:29:58 +08:00
}
2023-12-18 10:43:20 +08:00
2024-10-12 13:29:58 +08:00
FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result);
ffStrbufInitMove(&device->name, &name);
ffStrbufInitF(&device->devPath, "/dev/%s", devName);
2026-03-31 15:56:04 +08:00
ffStrbufInit(&device->serial);
ffStrbufInit(&device->revision);
ffStrbufInit(&device->interconnect);
device->type = devfd > 0 ? FF_PHYSICALDISK_TYPE_NONE : FF_PHYSICALDISK_TYPE_VIRTUAL;
device->size = 0;
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
bool isVirtio = false;
if (devfd > 0) {
2026-03-29 09:28:49 +08:00
if (ffStrStartsWith(devName, "nvme")) {
ffStrbufSetStatic(&device->interconnect, "NVMe");
2026-03-29 09:28:49 +08:00
} else if (ffStrStartsWith(devName, "mmcblk")) {
2025-04-05 21:08:06 +08:00
ffStrbufSetStatic(&device->interconnect, "MMC");
2026-03-29 09:28:49 +08:00
} else {
char pathSysDeviceLink[64];
snprintf(pathSysDeviceLink, ARRAY_SIZE(pathSysDeviceLink), "/sys/block/%s/device", devName);
char pathSysDeviceReal[PATH_MAX];
2026-03-29 09:28:49 +08:00
if (realpath(pathSysDeviceLink, pathSysDeviceReal)) {
if (strstr(pathSysDeviceReal, "/usb") != NULL) {
ffStrbufSetStatic(&device->interconnect, "USB");
2026-03-29 09:28:49 +08:00
} else if (strstr(pathSysDeviceReal, "/ata") != NULL) {
ffStrbufSetStatic(&device->interconnect, "ATA");
2026-03-29 09:28:49 +08:00
} else if (strstr(pathSysDeviceReal, "/scsi") != NULL) {
ffStrbufSetStatic(&device->interconnect, "SCSI");
2026-03-29 09:28:49 +08:00
} else if (strstr(pathSysDeviceReal, "/nvme") != NULL) {
ffStrbufSetStatic(&device->interconnect, "NVMe");
2026-03-29 09:28:49 +08:00
} else if (strstr(pathSysDeviceReal, "/virtio") != NULL) {
2026-03-31 15:56:04 +08:00
ffStrbufSetStatic(&device->interconnect, "VirtIO");
isVirtio = true; // VirtIO devices are virtual, but we still want to report it
2026-03-29 09:28:49 +08:00
} else {
if (ffAppendFileBufferRelative(devfd, "transport", &device->interconnect)) {
ffStrbufTrimRightSpace(&device->interconnect);
2026-03-29 09:28:49 +08:00
}
}
2024-10-15 10:29:50 +08:00
}
2023-12-18 10:43:20 +08:00
}
2026-03-31 15:56:04 +08:00
} else {
ffStrbufSetStatic(&device->interconnect, "Virtual");
2024-10-12 13:29:58 +08:00
}
2023-12-18 10:43:20 +08:00
2026-03-31 15:56:04 +08:00
if (devfd > 0 && !isVirtio) {
2024-10-12 13:29:58 +08:00
char isRotationalChar = '1';
2026-03-29 09:28:49 +08:00
if (ffReadFileDataRelative(dfd, "queue/rotational", 1, &isRotationalChar) > 0) {
2024-10-12 13:29:58 +08:00
device->type |= isRotationalChar == '1' ? FF_PHYSICALDISK_TYPE_HDD : FF_PHYSICALDISK_TYPE_SSD;
2026-03-29 09:28:49 +08:00
}
2026-03-31 15:56:04 +08:00
if (ffReadFileBufferRelative(devfd, "serial", &device->serial)) {
ffStrbufTrimSpace(&device->serial);
}
if (ffReadFileBufferRelative(devfd, "firmware_rev", &device->revision) ||
ffReadFileBufferRelative(devfd, "rev", &device->revision)) {
ffStrbufTrimRightSpace(&device->revision);
}
if (options->temp) {
device->temperature = detectNvmeTemp(devfd);
}
2024-10-12 13:29:58 +08:00
}
2023-12-18 10:43:20 +08:00
2024-10-12 13:29:58 +08:00
{
char blkSize[32];
2024-10-18 16:15:48 +08:00
ssize_t fileSize = ffReadFileDataRelative(dfd, "size", ARRAY_SIZE(blkSize) - 1, blkSize);
2026-03-29 09:28:49 +08:00
if (fileSize > 0) {
2024-10-12 13:29:58 +08:00
blkSize[fileSize] = 0;
device->size = (uint64_t) strtoul(blkSize, NULL, 10) * 512;
2026-03-29 09:28:49 +08:00
} else {
2024-10-12 13:29:58 +08:00
device->size = 0;
2026-03-29 09:28:49 +08:00
}
2024-10-12 13:29:58 +08:00
}
2023-12-18 10:43:20 +08:00
2024-10-12 13:29:58 +08:00
{
char removableChar = '0';
2026-03-29 09:28:49 +08:00
if (ffReadFileDataRelative(dfd, "removable", 1, &removableChar) > 0) {
2024-10-12 13:29:58 +08:00
device->type |= removableChar == '1' ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED;
2026-03-29 09:28:49 +08:00
}
2024-10-12 13:29:58 +08:00
}
2023-12-24 22:34:45 +08:00
2024-10-12 13:29:58 +08:00
{
char roChar = '0';
2026-03-29 09:28:49 +08:00
if (ffReadFileDataRelative(dfd, "ro", 1, &roChar) > 0) {
2024-10-12 13:29:58 +08:00
device->type |= roChar == '1' ? FF_PHYSICALDISK_TYPE_READONLY : FF_PHYSICALDISK_TYPE_READWRITE;
2026-03-29 09:28:49 +08:00
}
2024-10-12 13:29:58 +08:00
}
}
2026-03-29 09:28:49 +08:00
const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) {
2024-10-12 13:29:58 +08:00
FF_AUTO_CLOSE_DIR DIR* sysBlockDirp = opendir("/sys/block/");
2026-03-29 09:28:49 +08:00
if (sysBlockDirp == NULL) {
2024-10-12 13:29:58 +08:00
return "opendir(\"/sys/block/\") == NULL";
2026-03-29 09:28:49 +08:00
}
2024-10-12 13:29:58 +08:00
struct dirent* sysBlockEntry;
2026-03-29 09:28:49 +08:00
while ((sysBlockEntry = readdir(sysBlockDirp)) != NULL) {
2024-10-12 13:29:58 +08:00
const char* const devName = sysBlockEntry->d_name;
2026-03-29 09:28:49 +08:00
if (devName[0] == '.') {
2024-10-12 13:29:58 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-10-12 13:29:58 +08:00
2025-01-14 10:05:38 +08:00
char pathSysBlock[sizeof("/sys/block/") + sizeof(sysBlockEntry->d_name)];
2024-10-18 16:15:48 +08:00
snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "/sys/block/%s", devName);
2024-10-12 13:29:58 +08:00
2024-10-15 10:29:50 +08:00
int dfd = openat(dirfd(sysBlockDirp), devName, O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
2026-03-29 09:28:49 +08:00
if (dfd > 0) {
parsePhysicalDisk(dfd, devName, options, result);
}
2023-12-18 10:43:20 +08:00
}
return NULL;
}