mirror of
https://github.com/gnif/LookingGlass.git
synced 2024-12-22 22:01:46 +00:00
[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:
parent
75ec3c0478
commit
d93510e9f2
1 changed files with 76 additions and 2 deletions
|
@ -24,17 +24,20 @@
|
|||
#include "common/stringutils.h"
|
||||
#include "common/thread.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue