Merge pull request #533 from cgzones/os-release

SysArchMeter: read os-release instead of running lsb-release
This commit is contained in:
Nathan Scott 2021-03-01 18:38:53 +11:00 committed by GitHub
commit 7b48fec59a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 60 additions and 46 deletions

View File

@ -9,7 +9,6 @@ in the source distribution for its full text.
#include "SysArchMeter.h" #include "SysArchMeter.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include "XUtils.h" #include "XUtils.h"
@ -17,59 +16,74 @@ in the source distribution for its full text.
static const int SysArchMeter_attributes[] = {HOSTNAME}; static const int SysArchMeter_attributes[] = {HOSTNAME};
static void SysArchMeter_updateValues(Meter* this, char* buffer, size_t size) { static void parseOSRelease(char* buffer, size_t bufferLen) {
static struct utsname uname_info; FILE* stream = fopen("/etc/os-release", "r");
static int uname_result; if (!stream) {
static char distro[3][64] = { {'\0'}, {'\0'}, {'\0'} }; stream = fopen("/usr/lib/os-release", "r");
static bool loaded_data = false; if (!stream) {
xSnprintf(buffer, bufferLen, "Unknown Distro");
return;
}
}
(void)this; char name[64] = {'\0'};
char version[64] = {'\0'};
if(!loaded_data) { char lineBuffer[256];
uname_result = uname(&uname_info); while (fgets(lineBuffer, sizeof(lineBuffer), stream)) {
FILE* fp = popen("lsb_release --id --release --codename", "r"); if (String_startsWith(lineBuffer, "PRETTY_NAME=\"")) {
if(fp) { const char* start = lineBuffer + strlen("PRETTY_NAME=\"");
char line[96] = {'\0'}; const char* stop = strrchr(lineBuffer, '"');
size_t n = 0; if (!stop || stop <= start)
continue;
while(fgets(line, sizeof(line), fp)) { String_safeStrncpy(buffer, start, MINIMUM(bufferLen, (size_t)(stop - start + 1)));
n = strcspn(line, ":"); fclose(stream);
if(n > 0 && (n + 1) < strlen(line)) { return;
char* value = String_trim(&line[n + 1]); }
line[n] = '\0'; if (String_startsWith(lineBuffer, "NAME=\"")) {
const char* start = lineBuffer + strlen("NAME=\"");
if(String_eq(value, "n/a")) { const char* stop = strrchr(lineBuffer, '"');
free(value); if (!stop || stop <= start)
continue;
String_safeStrncpy(name, start, MINIMUM(sizeof(name), (size_t)(stop - start + 1)));
continue; 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);
if(String_eq(line, "Distributor ID")) snprintf(buffer, bufferLen, "%s%s%s", name[0] ? name : "", name[0] && version[0] ? " " : "", version);
snprintf(distro[0], sizeof(distro[0]), "%s", value); }
else if(String_eq(line, "Release"))
snprintf(distro[1], sizeof(distro[1]), "%s", value);
else if(String_eq(line, "Codename"))
snprintf(distro[2], sizeof(distro[2]), "%s", value);
free(value); static void SysArchMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) {
} static char savedString[128] = {'\0'};
} static bool loaded_data = false;
if(!distro[0][0])
snprintf(distro[0], sizeof(distro[0]), "Unknown"); if (!loaded_data) {
pclose(fp); 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);
} }
loaded_data = true; loaded_data = true;
} }
if(uname_result == 0) { String_safeStrncpy(buffer, savedString, size);
if (distro[1][0] && distro[2][0])
snprintf(buffer, size, "%s %s [%s] / %s %s (%s)", uname_info.sysname, uname_info.release, uname_info.machine, distro[0], distro[1], distro[2]);
else if(distro[1][0])
snprintf(buffer, size, "%s %s [%s] / %s %s", uname_info.sysname, uname_info.release, uname_info.machine, distro[0], distro[1]);
else
snprintf(buffer, size, "%s %s [%s]", uname_info.sysname, uname_info.release, uname_info.machine);
} else {
snprintf(buffer, size, "Unknown");
}
} }
const MeterClass SysArchMeter_class = { const MeterClass SysArchMeter_class = {