[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
This commit is contained in:
Quantum 2022-01-26 05:50:56 -05:00 committed by Geoffrey McRae
parent 75ec3c0478
commit d93510e9f2

View File

@ -24,17 +24,20 @@
#include "common/stringutils.h" #include "common/stringutils.h"
#include "common/thread.h" #include "common/thread.h"
#include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <errno.h> #include <errno.h>
#include <pwd.h> #include <pwd.h>
#include <unistd.h> #include <unistd.h>
#include <sys/utsname.h>
struct app struct app
{ {
const char * executable; const char * executable;
char * dataPath; char * dataPath;
char * osVersion;
}; };
struct app app = { 0 }; struct app app = { 0 };
@ -52,6 +55,7 @@ int main(int argc, char * argv[])
int result = app_main(argc, argv); int result = app_main(argc, argv);
free(app.dataPath); free(app.dataPath);
free(app.osVersion);
return result; return result;
} }
@ -101,10 +105,80 @@ KVMFROS os_getKVMFRType(void)
return KVMFR_OS_LINUX; 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) const char * os_getOSName(void)
{ {
//TODO if (app.osVersion)
return NULL; 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) const uint8_t * os_getUUID(void)