summaryrefslogtreecommitdiffstats
path: root/SysArchMeter.c
diff options
context:
space:
mode:
authorNathan Scott <nathans@redhat.com>2021-03-02 15:58:11 +1100
committerNathan Scott <nathans@redhat.com>2021-03-04 13:40:11 +1100
commit5b50ae3aa3a418f3f84ff2fdb172ab447753814f (patch)
tree17fcbba67e6935b2da236dc8dba4ee7fbfab4177 /SysArchMeter.c
parent2328e52403dfa52d122a9f7ccbd365beaedb8c9f (diff)
Separate display from sampling in SysArch and Hostname Meters
Several of our newer meters have merged coding concerns in terms of extracting values and displaying those values. This commit rectifies that for the SysArch and Hostname meters, allowing use of this code with alternative front/back ends. The SysArch code is also refined to detect whether the platform has an os-release file at all and/or the sys/utsname.h header via configure.ac.
Diffstat (limited to 'SysArchMeter.c')
-rw-r--r--SysArchMeter.c79
1 files changed, 5 insertions, 74 deletions
diff --git a/SysArchMeter.c b/SysArchMeter.c
index cc962630..c368e944 100644
--- a/SysArchMeter.c
+++ b/SysArchMeter.c
@@ -6,90 +6,21 @@ in the source distribution for its full text.
*/
#include "config.h" // IWYU pragma: keep
+#include "Platform.h"
#include "SysArchMeter.h"
-#include <stdio.h>
-#include <sys/utsname.h>
-
#include "XUtils.h"
static const int SysArchMeter_attributes[] = {HOSTNAME};
-static void parseOSRelease(char* buffer, size_t bufferLen) {
- FILE* stream = fopen("/etc/os-release", "r");
- if (!stream) {
- stream = fopen("/usr/lib/os-release", "r");
- if (!stream) {
- xSnprintf(buffer, bufferLen, "Unknown Distro");
- return;
- }
- }
-
- char name[64] = {'\0'};
- char version[64] = {'\0'};
- char lineBuffer[256];
- while (fgets(lineBuffer, sizeof(lineBuffer), stream)) {
- if (String_startsWith(lineBuffer, "PRETTY_NAME=\"")) {
- const char* start = lineBuffer + strlen("PRETTY_NAME=\"");
- const char* stop = strrchr(lineBuffer, '"');
- if (!stop || stop <= start)
- continue;
- String_safeStrncpy(buffer, start, MINIMUM(bufferLen, (size_t)(stop - start + 1)));
- fclose(stream);
- return;
- }
- if (String_startsWith(lineBuffer, "NAME=\"")) {
- const char* start = lineBuffer + strlen("NAME=\"");
- const char* stop = strrchr(lineBuffer, '"');
- if (!stop || stop <= start)
- continue;
- String_safeStrncpy(name, start, MINIMUM(sizeof(name), (size_t)(stop - start + 1)));
- continue;
- }
- if (String_startsWith(lineBuffer, "VERSION=\"")) {
- const char* start = lineBuffer + strlen("VERSION=\"");
- const char* stop = strrchr(lineBuffer, '"');
- if (!stop || stop <= start)
- continue;
- String_safeStrncpy(version, start, MINIMUM(sizeof(version), (size_t)(stop - start + 1)));
- continue;
- }
- }
- fclose(stream);
-
- snprintf(buffer, bufferLen, "%s%s%s", name[0] ? name : "", name[0] && version[0] ? " " : "", version);
-}
-
static void SysArchMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) {
- static char savedString[
- /* uname structure fields - manpages recommend sizeof */
- sizeof(((struct utsname*)0)->sysname) +
- sizeof(((struct utsname*)0)->release) +
- sizeof(((struct utsname*)0)->machine) +
- 16/*markup*/ +
- 128/*distro*/] = {'\0'};
- static bool loaded_data = false;
-
- if (!loaded_data) {
- struct utsname uname_info;
- int uname_result = uname(&uname_info);
-
- char distro[128];
- parseOSRelease(distro, sizeof(distro));
-
- if (uname_result == 0) {
- size_t written = xSnprintf(savedString, sizeof(savedString), "%s %s [%s]", uname_info.sysname, uname_info.release, uname_info.machine);
- if (!String_contains_i(savedString, distro) && sizeof(savedString) > written)
- snprintf(savedString + written, sizeof(savedString) - written, " @ %s", distro);
- } else {
- snprintf(savedString, sizeof(savedString), "%s", distro);
- }
+ static char* string;
- loaded_data = true;
- }
+ if (string == NULL)
+ Platform_getRelease(&string);
- String_safeStrncpy(buffer, savedString, size);
+ String_safeStrncpy(buffer, string, size);
}
const MeterClass SysArchMeter_class = {

© 2014-2024 Faster IT GmbH | imprint | privacy policy