using common.c to extract date and using nios.c to check kernel version(a bit specific to nios) and using c api instead of system command to get uname

This commit is contained in:
maliakal_d 2020-09-10 18:41:31 +02:00
parent cf8785ad2e
commit 00f780665f
6 changed files with 74 additions and 85 deletions

View File

@ -1,4 +1,3 @@
#define _GNU_SOURCE // needed for strptime to be at the top
#include "slsDetectorFunctionList.h"
#include "ALTERA_PLL_CYCLONE10.h"
#include "ASIC_Driver.h"
@ -15,10 +14,10 @@
#include <netinet/in.h>
#include <string.h>
#include <time.h>
#include <unistd.h> // usleep
#ifdef VIRTUAL
#include <pthread.h>
#include <time.h>
#endif
// Global variable from slsDetectorServer_funcs
@ -182,45 +181,7 @@ int checkKernelVersion() {
#ifdef VIRTUAL
return OK;
#endif
// extract kernel date string
char output[256];
memset(output, 0, 256);
FILE *sysFile = popen("uname -a | cut -d ' ' -f5-10", "r");
fgets(output, sizeof(output), sysFile);
pclose(sysFile);
// remove end line
output[strlen(output) - 1] = '\0';
// convert kernel date string into time
struct tm kernelDate;
if (NULL == strptime(output, "%a %b %d %H:%M:%S %Z %Y", &kernelDate)) {
LOG(logERROR, ("Could not parse retrieved kernel date, %s\n", output));
return FAIL;
}
time_t t_kernelDate = mktime(&kernelDate);
// convert expected date into time
struct tm expDate;
if (NULL ==
strptime(KERNEL_DATE_VRSN, "%a %b %d %H:%M:%S %Z %Y", &expDate)) {
LOG(logERROR,
("Could not parse expected kernel date, %s\n", KERNEL_DATE_VRSN));
return FAIL;
}
time_t t_expDate = mktime(&expDate);
// compare if kernel time is older than expected time
if (t_kernelDate < t_expDate) {
LOG(logERROR,
("Kernel Version Incompatible (too old)! Expected: %s, Got %s\n",
KERNEL_DATE_VRSN, output));
return FAIL;
}
LOG(logINFOBLUE, ("Kernel Version Compatible: %s [min.: %s]\n", output,
KERNEL_DATE_VRSN));
return OK;
return Nios_checkKernelVersion(KERNEL_DATE_VRSN);
}
int checkType() {

View File

@ -1,4 +1,3 @@
#define _GNU_SOURCE // needed for strptime to be at the top
#include "slsDetectorFunctionList.h"
#include "ALTERA_PLL_CYCLONE10.h"
#include "DAC6571.h"
@ -14,10 +13,10 @@
#include <netinet/in.h>
#include <string.h>
#include <time.h>
#include <unistd.h> // usleep
#ifdef VIRTUAL
#include <pthread.h>
#include <time.h>
#endif
// Global variable from slsDetectorServer_funcs
@ -169,44 +168,7 @@ int checkKernelVersion() {
return OK;
#endif
// extract kernel date string
char output[256];
memset(output, 0, 256);
FILE *sysFile = popen("uname -a | cut -d ' ' -f5-10", "r");
fgets(output, sizeof(output), sysFile);
pclose(sysFile);
// remove end line
output[strlen(output) - 1] = '\0';
// convert kernel date string into time
struct tm kernelDate;
if (NULL == strptime(output, "%a %b %d %H:%M:%S %Z %Y", &kernelDate)) {
LOG(logERROR, ("Could not parse retrieved kernel date, %s\n", output));
return FAIL;
}
time_t t_kernelDate = mktime(&kernelDate);
// convert expected date into time
struct tm expDate;
if (NULL ==
strptime(KERNEL_DATE_VRSN, "%a %b %d %H:%M:%S %Z %Y", &expDate)) {
LOG(logERROR,
("Could not parse expected kernel date, %s\n", KERNEL_DATE_VRSN));
return FAIL;
}
time_t t_expDate = mktime(&expDate);
// compare if kernel time is older than expected time
if (t_kernelDate < t_expDate) {
LOG(logERROR,
("Kernel Version Incompatible (too old)! Expected: %s, Got %s\n",
KERNEL_DATE_VRSN, output));
return FAIL;
}
LOG(logINFOBLUE, ("Kernel Version Compatible: %s [min.: %s]\n", output,
KERNEL_DATE_VRSN));
return OK;
return Nios_checkKernelVersion(KERNEL_DATE_VRSN);
}
int checkType() {

View File

@ -1,6 +1,7 @@
#pragma once
#include <stdio.h>
#include <time.h>
/**
* Convert a value from a range to a different range (eg voltage to dac or vice
@ -16,4 +17,6 @@
int ConvertToDifferentRange(int inputMin, int inputMax, int outputMin,
int outputMax, int inputValue, int *outputValue);
int getAbsPath(char *buf, size_t bufSize, char *fname);
int getAbsPath(char *buf, size_t bufSize, char *fname);
int GetTimeFromString(char *buf, time_t *result);

View File

@ -87,3 +87,7 @@ int mapCSP0(void);
* Get Nios base address
*/
u_int32_t *Nios_getBaseAddress();
/** check kernel version against expected version string (complain if too old)
* @returns OK or FAIL */
int Nios_checkKernelVersion(char *expectedVersion);

View File

@ -1,3 +1,4 @@
#define _GNU_SOURCE // needed for strptime to be at the top
#include "common.h"
#include "clogger.h"
#include "sls_detector_defs.h"
@ -59,4 +60,13 @@ int getAbsPath(char *buf, size_t bufSize, char *fname) {
sprintf(buf, "%s/%s", dir, fname);
LOG(logDEBUG1, ("full path for %s: %s\n", fname, buf));
return OK;
}
int GetTimeFromString(char *buf, time_t *result) {
struct tm t;
if (NULL == strptime(buf, "%a %b %d %H:%M:%S %Z %Y", &t)) {
return FAIL;
}
*result = mktime(&t);
return OK;
}

View File

@ -2,10 +2,13 @@
#include "RegisterDefs.h"
#include "ansi.h"
#include "clogger.h"
#include "common.h"
#include "sls_detector_defs.h"
#include <fcntl.h> // open
#include <sys/mman.h> // mmap
#include <fcntl.h> // open
#include <string.h>
#include <sys/mman.h> // mmap
#include <sys/utsname.h> // uname
/* global variables */
u_int32_t *csp0base = 0;
@ -126,4 +129,50 @@ int mapCSP0(void) {
return OK;
}
u_int32_t *Nios_getBaseAddress() { return csp0base; }
u_int32_t *Nios_getBaseAddress() { return csp0base; }
int Nios_checkKernelVersion(char *expectedVersion) {
// extract kernel date string
struct utsname buf;
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[256];
memset(output, 0, 256);
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;
}