moved verifykernelversion to common

This commit is contained in:
2021-10-29 16:43:48 +02:00
parent 2813cd5ac2
commit 45b3514118
9 changed files with 65 additions and 116 deletions

View File

@ -7,7 +7,8 @@
#include <libgen.h> // dirname
#include <string.h>
#include <unistd.h> // readlink
#include <sys/utsname.h> // uname
#include <unistd.h> // readlink
int ConvertToDifferentRange(int inputMin, int inputMax, int outputMin,
int outputMax, int inputValue, int *outputValue) {
@ -64,7 +65,7 @@ int getAbsPath(char *buf, size_t bufSize, char *fname) {
return OK;
}
int GetTimeFromString(char *buf, time_t *result) {
int getTimeFromString(char *buf, time_t *result) {
// remove timezone as strptime cannot validate timezone despite
// documentation
const char *timezone = {"CEST"};
@ -89,6 +90,52 @@ int GetTimeFromString(char *buf, time_t *result) {
return OK;
}
int validateKernelVersion(char *expectedVersion) {
// extract kernel date string
struct utsname buf = {0};
if (uname(&buf) == -1) {
LOG(logERROR, ("Could not get kernel version\n"));
return FAIL;
}
// remove first word (#version number)
const char *ptr = strchr(buf.version, ' ');
if (ptr == NULL) {
LOG(logERROR, ("Could not parse kernel version\n"));
return FAIL;
}
char output[255];
memset(output, 0, sizeof(output));
strcpy(output, buf.version + (ptr - buf.version + 1));
// convert kernel date string into time
time_t kernelDate;
if (getTimeFromString(output, &kernelDate) == FAIL) {
LOG(logERROR, ("Could not parse retrieved kernel date, %s\n", output));
return FAIL;
}
// convert expected date into time
time_t expDate;
if (getTimeFromString(expectedVersion, &expDate) == FAIL) {
LOG(logERROR,
("Could not parse expected kernel date, %s\n", expectedVersion));
return FAIL;
}
// compare if kernel time is older than expected time
if (kernelDate < expDate) {
LOG(logERROR, ("Kernel Version Incompatible (too old)! Expected: [%s], "
"Got [%s]\n",
expectedVersion, output));
return FAIL;
}
LOG(logINFOBLUE, ("Kernel Version Compatible: %s [min.: %s]\n", output,
expectedVersion));
return OK;
}
void validate(int *ret, char *mess, int arg, int retval, char *modename,
enum numberMode nummode) {
if (*ret == OK && arg != GET_FLAG && retval != arg) {