From d93510e9f209f017cb106a3eb6b8cd2d43588d6c Mon Sep 17 00:00:00 2001 From: Quantum Date: Wed, 26 Jan 2022 05:50:56 -0500 Subject: [PATCH] [host] linux: allow getting system version Result is something like: Debian GNU/Linux 11 (bullseye), kernel: Linux 5.14.0-0.bpo.2-amd64 on x86_64 --- host/platform/Linux/src/platform.c | 78 +++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/host/platform/Linux/src/platform.c b/host/platform/Linux/src/platform.c index 2f493bb7..11051816 100644 --- a/host/platform/Linux/src/platform.c +++ b/host/platform/Linux/src/platform.c @@ -24,17 +24,20 @@ #include "common/stringutils.h" #include "common/thread.h" +#include #include #include #include #include #include #include +#include struct app { const char * executable; char * dataPath; + char * osVersion; }; struct app app = { 0 }; @@ -52,6 +55,7 @@ int main(int argc, char * argv[]) int result = app_main(argc, argv); free(app.dataPath); + free(app.osVersion); return result; } @@ -101,10 +105,80 @@ KVMFROS os_getKVMFRType(void) return KVMFR_OS_LINUX; } +static const char * getPrettyName() +{ + static char buffer[256]; + + FILE * fp = fopen("/etc/os-release", "r"); + if (fp == NULL) + { + fp = fopen("/usr/lib/os-release", "r"); + if (fp == NULL) + return NULL; + } + + while (fgets(buffer, sizeof(buffer), fp)) + { + if (strstr(buffer, "PRETTY_NAME")) + { + char * ptr = strchr(buffer, '=') + 1; + while (isspace(*ptr)) + ++ptr; + + size_t len = strlen(ptr); + while (isspace(ptr[len - 1])) + --len; + + if (*ptr == '"' || *ptr == '\'') + { + ++ptr; + len -= 2; + } + + ptr[len] = '\0'; + fclose(fp); + return ptr; + } + + // If a line is too long, skip it. + while (buffer[strlen(buffer) - 1] != '\n') + if (!fgets(buffer, sizeof(buffer), fp)) + goto done; + } + +done: + fclose(fp); + return NULL; +} + const char * os_getOSName(void) { - //TODO - return NULL; + if (app.osVersion) + return app.osVersion; + + const char * pretty = getPrettyName(); + struct utsname utsname; + uname(&utsname); + + if (!pretty) + alloc_sprintf( + &app.osVersion, + "%s %s on %s", + utsname.sysname, + utsname.release, + utsname.machine + ); + else + alloc_sprintf( + &app.osVersion, + "%s, kernel: %s %s on %s", + pretty, + utsname.sysname, + utsname.release, + utsname.machine + ); + + return app.osVersion; } const uint8_t * os_getUUID(void)