mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-12 10:22:12 +02:00
NetIO (Linux): use openat
This commit is contained in:
@@ -4,60 +4,46 @@
|
||||
#include "common/netif/netif.h"
|
||||
#include "util/stringUtils.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <net/if.h>
|
||||
|
||||
static void getData(FFstrbuf* buffer, const char* ifName, bool isDefaultRoute, FFstrbuf* path, FFlist* result)
|
||||
static void getData(FFstrbuf* buffer, const char* ifName, bool isDefaultRoute, int basefd, FFlist* result)
|
||||
{
|
||||
ffStrbufSetF(path, "/sys/class/net/%s/operstate", ifName);
|
||||
if(!ffReadFileBuffer(path->chars, buffer) || !ffStrbufEqualS(buffer, "up\n"))
|
||||
FF_AUTO_CLOSE_FD int dfd = openat(basefd, ifName, O_RDONLY | O_DIRECTORY);
|
||||
if (dfd < 0)
|
||||
return;
|
||||
|
||||
char operstate;
|
||||
if(!ffReadFileDataRelative(dfd, "operstate", 1, &operstate) || operstate != 'u' /* up or unknown */)
|
||||
return;
|
||||
|
||||
FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result);
|
||||
ffStrbufInitS(&counters->name, ifName);
|
||||
counters->defaultRoute = isDefaultRoute;
|
||||
|
||||
ffStrbufSetF(path, "/sys/class/net/%s/statistics/", ifName);
|
||||
uint32_t statLen = path->length;
|
||||
|
||||
ffStrbufAppendS(path, "rx_bytes");
|
||||
if (ffReadFileBuffer(path->chars, buffer))
|
||||
if (ffReadFileBufferRelative(dfd, "statistics/rx_bytes", buffer))
|
||||
counters->rxBytes = ffStrbufToUInt(buffer, 0);
|
||||
ffStrbufSubstrBefore(path, statLen);
|
||||
|
||||
ffStrbufAppendS(path, "tx_bytes");
|
||||
if (ffReadFileBuffer(path->chars, buffer))
|
||||
if (ffReadFileBufferRelative(dfd, "statistics/tx_bytes", buffer))
|
||||
counters->txBytes = ffStrbufToUInt(buffer, 0);
|
||||
ffStrbufSubstrBefore(path, statLen);
|
||||
|
||||
ffStrbufAppendS(path, "rx_packets");
|
||||
if (ffReadFileBuffer(path->chars, buffer))
|
||||
if (ffReadFileBufferRelative(dfd, "statistics/rx_packets", buffer))
|
||||
counters->rxPackets = ffStrbufToUInt(buffer, 0);
|
||||
ffStrbufSubstrBefore(path, statLen);
|
||||
|
||||
ffStrbufAppendS(path, "tx_packets");
|
||||
if (ffReadFileBuffer(path->chars, buffer))
|
||||
if (ffReadFileBufferRelative(dfd, "statistics/tx_packets", buffer))
|
||||
counters->txPackets = ffStrbufToUInt(buffer, 0);
|
||||
ffStrbufSubstrBefore(path, statLen);
|
||||
|
||||
ffStrbufAppendS(path, "rx_errors");
|
||||
if (ffReadFileBuffer(path->chars, buffer))
|
||||
if (ffReadFileBufferRelative(dfd, "statistics/rx_errors", buffer))
|
||||
counters->rxErrors = ffStrbufToUInt(buffer, 0);
|
||||
ffStrbufSubstrBefore(path, statLen);
|
||||
|
||||
ffStrbufAppendS(path, "tx_errors");
|
||||
if (ffReadFileBuffer(path->chars, buffer))
|
||||
if (ffReadFileBufferRelative(dfd, "statistics/tx_errors", buffer))
|
||||
counters->txErrors = ffStrbufToUInt(buffer, 0);
|
||||
ffStrbufSubstrBefore(path, statLen);
|
||||
|
||||
ffStrbufAppendS(path, "rx_dropped");
|
||||
if (ffReadFileBuffer(path->chars, buffer))
|
||||
if (ffReadFileBufferRelative(dfd, "statistics/rx_dropped", buffer))
|
||||
counters->rxDrops = ffStrbufToUInt(buffer, 0);
|
||||
ffStrbufSubstrBefore(path, statLen);
|
||||
|
||||
ffStrbufAppendS(path, "tx_dropped");
|
||||
if (ffReadFileBuffer(path->chars, buffer))
|
||||
if (ffReadFileBufferRelative(dfd, "statistics/tx_dropped", buffer))
|
||||
counters->txDrops = ffStrbufToUInt(buffer, 0);
|
||||
ffStrbufSubstrBefore(path, statLen);
|
||||
}
|
||||
|
||||
const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
|
||||
@@ -65,7 +51,6 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
|
||||
FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/net");
|
||||
if (!dirp) return "opendir(\"/sys/class/net\") == NULL";
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateA(64);
|
||||
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
|
||||
|
||||
const char* defaultRouteIfName = ffNetifGetDefaultRouteIfName();
|
||||
@@ -75,7 +60,7 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
|
||||
if (options->namePrefix.length && strncmp(defaultRouteIfName, options->namePrefix.chars, options->namePrefix.length) != 0)
|
||||
return NULL;
|
||||
|
||||
getData(&buffer, defaultRouteIfName, true, &path, result);
|
||||
getData(&buffer, defaultRouteIfName, true, dirfd(dirp), result);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -89,7 +74,7 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
|
||||
if (options->namePrefix.length && strncmp(ifName, options->namePrefix.chars, options->namePrefix.length) != 0)
|
||||
continue;
|
||||
|
||||
getData(&buffer, ifName, ffStrEquals(ifName, defaultRouteIfName), &path, result);
|
||||
getData(&buffer, ifName, ffStrEquals(ifName, defaultRouteIfName), dirfd(dirp), result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user