summaryrefslogtreecommitdiffstats
path: root/src/detection/physicaldisk/physicaldisk_linux.c
blob: f8126b96a257e19e2255d2d127cde19786bdcca6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "physicaldisk.h"
#include "common/io.h"
#include "common/properties.h"
#include "common/strutil.h"

#include <ctype.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>

static double detectNvmeTemp(int devfd) {
    char pathHwmon[] = "hwmon$/temp1_input";

    for (char c = '0'; c <= '9'; c++) // hopefully there's only one digit
    {
        pathHwmon[strlen("hwmon")] = c;
        char buffer[64];
        ssize_t size = ffReadFileDataRelative(devfd, pathHwmon, ARRAY_SIZE(buffer), buffer);
        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;
}

static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOptions* options, FFlist* result) {
    uint64_t size = 0;

    {
        char blkSize[32];
        ssize_t fileSize = ffReadFileDataRelative(dfd, "size", ARRAY_SIZE(blkSize) - 1, blkSize);
        if (fileSize > 0) {
            blkSize[fileSize] = 0;
            size = (uint64_t) strtoul(blkSize, NULL, 10) * 512;
        }
    }

    FFPhysicalDiskType type = FF_PHYSICALDISK_TYPE_NONE;
    if (size == 0) {
        if (options->hideType & FF_PHYSICALDISK_TYPE_UNUSED) {
            return;
        }

        type |= FF_PHYSICALDISK_TYPE_UNUSED;
    }

    int devfd = openat(dfd, "device", O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);

    if (devfd < 0) {
        if (options->hideType & FF_PHYSICALDISK_TYPE_VIRTUAL) {
            return;
        }

        type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
    }

    FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();

    if (devfd > 0) {
        if (ffAppendFileBufferRelative(devfd, "vendor", &name)) {
            ffStrbufTrimRightSpace(&name);
            if (name.length > 0) {
                ffStrbufAppendC(&name, ' ');
            }
        }

        ffAppendFileBufferRelative(devfd, "model", &name);
        ffStrbufTrimRightSpace(&name);

        if (name.length == 0) {
            ffStrbufSetS(&name, devName);
        }

        if (ffStrStartsWith(devName, "nvme")) {
            int devid, nsid;
            if (sscanf(devName, "nvme%dn%d", &devid, &nsid) == 2) {
                bool multiNs = nsid > 1;
                if (!multiNs) {
                    char pathSysBlock[32];
                    snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "/dev/nvme%dn2", devid);
                    multiNs = access(pathSysBlock, F_OK) == 0;
                }
                if (multiNs) {
                    // In Asahi Linux, there are multiple namespaces for the same NVMe drive.
                    ffStrbufAppendF(&name, " - %d", nsid);
                }
            }
        }

        if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
            return;
        }
    } else {
        ffStrbufSetS(&name, devName);
    }

    FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result);
    ffStrbufInitMove(&device->name, &name);
    ffStrbufInitF(&device->devPath, "/dev/%s", devName);
    ffStrbufInit(&device->serial);
    ffStrbufInit(&device->revision);
    ffStrbufInit(&device->interconnect);
    device->type = type;
    device->size = size;
    device->temperature = FF_PHYSICALDISK_TEMP_UNSET;

    bool isVirtio = false;
    if (devfd > 0) {
        if (ffStrStartsWith(devName, "nvme")) {
            ffStrbufSetStatic(&device->interconnect, "NVMe");
        } else if (ffStrStartsWith(devName, "mmcblk")) {
            ffStrbufSetStatic(&device->interconnect, "MMC");
        } else {
            char pathSysDeviceLink[64];
            snprintf(pathSysDeviceLink, ARRAY_SIZE(pathSysDeviceLink), "/sys/block/%s/device", devName);
            char pathSysDeviceReal[PATH_MAX];
            if (realpath(pathSysDeviceLink, pathSysDeviceReal)) {
                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 (strstr(pathSysDeviceReal, "/virtio") != NULL) {
                    ffStrbufSetStatic(&device->interconnect, "VirtIO");
                    isVirtio = true; // VirtIO devices are virtual, but we still want to report it
                } else {
                    if (ffAppendFileBufferRelative(devfd, "transport", &device->interconnect)) {
                        ffStrbufTrimRightSpace(&device->interconnect);
                    }
                }
            }
        }
    } else {
        ffStrbufSetStatic(&device->interconnect, "Virtual");
    }

    if (devfd > 0 && !isVirtio) {
        char isRotationalChar = '1';
        if (ffReadFileDataRelative(dfd, "queue/rotational", 1, &isRotationalChar) > 0) {
            device->type |= isRotationalChar == '1' ? FF_PHYSICALDISK_TYPE_HDD : FF_PHYSICALDISK_TYPE_SSD;
        }

        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);
        }
    }

    {
        char removableChar = '0';
        if (ffReadFileDataRelative(dfd, "removable", 1, &removableChar) > 0) {
            device->type |= removableChar == '1' ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED;
        }
    }

    {
        char roChar = '0';
        if (ffReadFileDataRelative(dfd, "ro", 1, &roChar) > 0) {
            device->type |= roChar == '1' ? FF_PHYSICALDISK_TYPE_READONLY : FF_PHYSICALDISK_TYPE_READWRITE;
        }
    }
}

const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) {
    FF_AUTO_CLOSE_DIR DIR* sysBlockDirp = opendir("/sys/block/");
    if (sysBlockDirp == NULL) {
        return "opendir(\"/sys/block/\") == NULL";
    }

    struct dirent* sysBlockEntry;
    while ((sysBlockEntry = readdir(sysBlockDirp)) != NULL) {
        const char* const devName = sysBlockEntry->d_name;

        if (devName[0] == '.') {
            continue;
        }

        char pathSysBlock[sizeof("/sys/block/") + sizeof(sysBlockEntry->d_name)];
        snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "/sys/block/%s", devName);

        int dfd = openat(dirfd(sysBlockDirp), devName, O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
        if (dfd > 0) {
            parsePhysicalDisk(dfd, devName, options, result);
        }
    }

    return NULL;
}