update mode added. need to fix why udpatemode get and set not in allowed functions

This commit is contained in:
2021-11-12 17:18:26 +01:00
parent 0ffd30e147
commit eb69d7cb69
13 changed files with 255 additions and 48 deletions

View File

@ -10,9 +10,11 @@
#ifdef VIRTUAL
#define TEMP_PROG_FOLDER_NAME "/tmp/"
#define UPDATE_FILE "/tmp/slsdet_udpate"
#else
#define TEMP_PROG_FOLDER_NAME "/var/tmp/"
#define TEMP_PROG_FOLDER_NAME_ALL_FILES "/var/tmp/*"
#define UPDATE_FILE "udpate.txt"
#endif
#define TEMP_PROG_FILE_NAME TEMP_PROG_FOLDER_NAME "tmp.rawbin"
@ -62,3 +64,6 @@ int writeBinaryFile(char *mess, char *fname, char *buffer,
const uint64_t filesize, char *errorPrefix);
int moveBinaryFile(char *mess, char *dest, char *src, char *errorPrefix);
int createEmptyFile(char *mess, char *fname, char *errorPrefix);
int deleteFile(char *mess, char *fname, char *errorPrefix);

View File

@ -287,4 +287,6 @@ void receive_program_via_blackfin(int file_des, enum PROGRAM_INDEX index,
char *checksum, char *serverName);
void receive_program_default(int file_des, enum PROGRAM_INDEX index,
char *functionType, uint64_t filesize,
char *checksum, char *serverName);
char *checksum, char *serverName);
int get_update_mode(int);
int set_update_mode(int);

View File

@ -434,11 +434,17 @@ int setupDetectorServer(char *mess, char *sname) {
LOG(logINFO, ("\tPermissions modified\n"));
// symbolic link
char linkname[MAX_STR_LENGTH] = {0};
strcpy(linkname, LINKED_SERVER_NAME);
#ifdef VIRTUAL
sprintf(linkname, "%s%s", TEMP_PROG_FOLDER_NAME, LINKED_SERVER_NAME);
#endif
const int fileNameSize = 128;
char linkname[fileNameSize];
if (getAbsPath(linkname, fileNameSize, LINKED_SERVER_NAME) == FAIL) {
sprintf(
mess,
"Could not copy detector server. Could not get abs path of current "
"process\n");
LOG(logERROR, (mess));
return FAIL;
}
if (snprintf(cmd, MAX_STR_LENGTH, "ln -sf %s %s", sname, linkname) >=
MAX_STR_LENGTH) {
strcpy(mess, "Could not copy detector server. Command to "
@ -472,8 +478,7 @@ int setupDetectorServer(char *mess, char *sname) {
// add new link name to /etc/inittab
char *format = "echo 'ttyS0::respawn:/./%s' >> /etc/inittab";
if (snprintf(cmd, MAX_STR_LENGTH, format, LINKED_SERVER_NAME) >=
MAX_STR_LENGTH) {
if (snprintf(cmd, MAX_STR_LENGTH, format, linkname) >= MAX_STR_LENGTH) {
strcpy(mess, "Could not copy detector server. Command "
"to add new server for spawning is too long\n");
LOG(logERROR, (mess));
@ -506,12 +511,23 @@ int writeBinaryFile(char *mess, char *fname, char *buffer,
const uint64_t filesize, char *errorPrefix) {
LOG(logINFO, ("\tWriting Detector Server Binary...\n"));
FILE *fp = fopen(fname, "wb");
const int fileNameSize = 128;
char fullname[fileNameSize];
if (getAbsPath(fullname, fileNameSize, fname) == FAIL) {
sprintf(mess,
"Could not %s. Could not get abs path of current "
"process\n",
errorPrefix);
LOG(logERROR, (mess));
return FAIL;
}
FILE *fp = fopen(fullname, "wb");
if (fp == NULL) {
sprintf(mess,
"Could not %s. (opening file to write(%s). "
"Maybe it is being used? Try another name?\n",
errorPrefix, fname);
errorPrefix, fullname);
LOG(logERROR, (mess));
return FAIL;
}
@ -555,12 +571,13 @@ int writeBinaryFile(char *mess, char *fname, char *buffer,
LOG(logERROR, (mess));
return FAIL;
}
LOG(logINFO, ("\tWritten binary to %s (%lu bytes)\n", fname,
LOG(logINFO, ("\tWritten binary to %s (%lu bytes)\n", fullname,
(long unsigned int)bytesWritten));
return OK;
}
int moveBinaryFile(char *mess, char *dest, char *src, char *errorPrefix) {
char cmd[MAX_STR_LENGTH] = {0};
char retvals[MAX_STR_LENGTH] = {0};
@ -584,3 +601,76 @@ int moveBinaryFile(char *mess, char *dest, char *src, char *errorPrefix) {
LOG(logINFO, ("\tMoved file from %s to %s\n", src, dest));
return OK;
}
int createEmptyFile(char *mess, char *fname, char *errorPrefix) {
const int fileNameSize = 128;
char fullname[fileNameSize];
if (getAbsPath(fullname, fileNameSize, fname) == FAIL) {
sprintf(mess,
"Could not %s. Could not get abs path of current "
"process\n",
errorPrefix);
LOG(logERROR, (mess));
return FAIL;
}
char cmd[MAX_STR_LENGTH] = {0};
char retvals[MAX_STR_LENGTH] = {0};
char *format = "touch %s";
if (snprintf(cmd, MAX_STR_LENGTH, format, fullname) >= MAX_STR_LENGTH) {
sprintf(mess, "Could not %s. Command to create is too long\n",
errorPrefix);
LOG(logERROR, (mess));
return FAIL;
}
if (executeCommand(cmd, retvals, logDEBUG1) == FAIL) {
snprintf(mess, MAX_STR_LENGTH,
"Could not %s. (creating empty file %s): %s\n", errorPrefix,
fullname, retvals);
LOG(logERROR, (mess));
return FAIL;
}
LOG(logINFO, ("\tEmpty file created: %s (%s)\n", fullname, errorPrefix));
return OK;
}
int deleteFile(char *mess, char *fname, char *errorPrefix) {
const int fileNameSize = 128;
char fullname[fileNameSize];
if (getAbsPath(fullname, fileNameSize, fname) == FAIL) {
sprintf(mess,
"Could not %s. Could not get abs path of current "
"process\n",
errorPrefix);
LOG(logERROR, (mess));
return FAIL;
}
if (access(fullname, F_OK) == 0) {
char cmd[MAX_STR_LENGTH] = {0};
char retvals[MAX_STR_LENGTH] = {0};
char *format = "rm %s";
if (snprintf(cmd, MAX_STR_LENGTH, format, fullname) >= MAX_STR_LENGTH) {
sprintf(mess, "Could not %s. Command to delete is too long\n",
errorPrefix);
LOG(logERROR, (mess));
return FAIL;
}
if (executeCommand(cmd, retvals, logDEBUG1) == FAIL) {
snprintf(mess, MAX_STR_LENGTH,
"Could not %s. (deleting file %s). %s\n", errorPrefix,
fullname, retvals);
LOG(logERROR, (mess));
return FAIL;
}
LOG(logINFO, ("\tDeleted file: %s (%s)\n", fullname, errorPrefix));
} else {
LOG(logINFO,
("\tFile does not exist anyway: %s (%s)\n", fullname, errorPrefix));
}
return OK;
}

View File

@ -4,6 +4,7 @@
The port number is passed as an argument */
#include "clogger.h"
#include "common.h"
#include "communication_funcs.h"
#include "sharedMemory.h"
#include "sls/sls_detector_defs.h"
@ -48,6 +49,18 @@ int main(int argc, char *argv[]) {
checkModuleFlag = 1;
int version = 0;
// update flag if update file exists (command line arg overwrites)
const int fileNameSize = 128;
char fname[fileNameSize];
if (getAbsPath(fname, fileNameSize, UPDATE_FILE) == FAIL) {
LOG(logERROR, ("Could not get abs path to check if update file exists. "
"Will try current folder instead.\n"));
strcpy(fname, UPDATE_FILE);
}
if (access(fname, F_OK) == 0) {
updateFlag = 1;
}
// help message
char helpMessage[MAX_STR_LENGTH];
memset(helpMessage, 0, MAX_STR_LENGTH);

View File

@ -95,7 +95,9 @@ int updateModeAllowedFunction(int file_des) {
F_REBOOT_CONTROLLER,
F_GET_KERNEL_VERSION,
F_UPDATE_KERNEL,
F_UPDATE_DETECTOR_SERVER};
F_UPDATE_DETECTOR_SERVER,
F_GET_UPDATE_MODE,
F_SET_UPDATE_MODE};
for (unsigned int i = 0; i < listsize; ++i) {
if ((unsigned int)fnum == list[i]) {
return OK;
@ -457,6 +459,8 @@ void function_table() {
flist[F_GET_KERNEL_VERSION] = &get_kernel_version;
flist[F_UPDATE_KERNEL] = &update_kernel;
flist[F_UPDATE_DETECTOR_SERVER] = &update_detector_server;
flist[F_GET_UPDATE_MODE] = &get_update_mode;
flist[F_SET_UPDATE_MODE] = &set_update_mode;
// check
if (NUM_DET_FUNCTIONS >= RECEIVER_ENUM_START) {
@ -9312,24 +9316,36 @@ int receive_program(int file_des, enum PROGRAM_INDEX index) {
if (index == PROGRAM_SERVER) {
if (receiveData(file_des, serverName, MAX_STR_LENGTH, OTHER) < 0)
return printSocketReadError();
#ifdef VIRTUAL
// writing to a temp folder
{
char temp[MAX_STR_LENGTH] = {0};
sprintf(temp, "%s%s", TEMP_PROG_FOLDER_NAME, serverName);
strcpy(serverName, temp);
}
#endif
LOG(logINFO, ("\tServer Name: %s\n", serverName));
}
// in same folder as current process (will also work for virtual then
// with write permissions)
{
const int fileNameSize = 128;
char fname[fileNameSize];
if (getAbsPath(fname, fileNameSize, serverName) == FAIL) {
ret = FAIL;
sprintf(mess,
"Could not %s. Could not get abs path of current "
"process\n",
functionType);
LOG(logERROR, (mess));
Server_SendResult(file_des, INT32, NULL, 0);
} else {
strcpy(serverName, fname);
}
}
if (ret == OK) {
#if defined(GOTTHARD2D) || defined(MYTHEN3D) || defined(EIGERD)
receive_program_default(file_des, index, functionType, filesize,
checksum, serverName);
receive_program_default(file_des, index, functionType, filesize,
checksum, serverName);
#else
receive_program_via_blackfin(file_des, index, functionType, filesize,
checksum, serverName);
receive_program_via_blackfin(file_des, index, functionType,
filesize, checksum, serverName);
#endif
}
if (ret == OK) {
LOG(logINFOGREEN, ("%s completed successfully\n", functionType));
@ -9527,20 +9543,16 @@ void receive_program_default(int file_des, enum PROGRAM_INDEX index,
"update detector server");
// extra step to write to temp and move to real file as
// fopen will give text busy if opening same name as process name
char dest[MAX_STR_LENGTH] = {0};
sprintf(dest, "%s%s",
(myDetectorType == EIGER ? "/home/root/executables/" : ""),
serverName);
if (ret == OK) {
ret = moveBinaryFile(mess, dest, TEMP_PROG_FILE_NAME,
ret = moveBinaryFile(mess, serverName, TEMP_PROG_FILE_NAME,
"update detector server");
}
if (ret == OK) {
ret = verifyChecksumFromFile(mess, functionType, checksum, dest);
ret = verifyChecksumFromFile(mess, functionType, checksum,
serverName);
}
if (ret == OK) {
ret = setupDetectorServer(mess, dest);
ret = setupDetectorServer(mess, serverName);
}
break;
#endif
@ -9554,4 +9566,42 @@ void receive_program_default(int file_des, enum PROGRAM_INDEX index,
// free resources
free(src);
#endif
}
int get_update_mode(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
int retval = -1;
LOG(logDEBUG1, ("Getting update mode\n"));
retval = updateFlag;
LOG(logDEBUG1, ("update mode retval: %d\n", retval));
return Server_SendResult(file_des, INT32, &retval, sizeof(retval));
}
int set_update_mode(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
int arg = -1;
if (receiveData(file_des, &arg, sizeof(arg), INT32) < 0)
return printSocketReadError();
LOG(logDEBUG1, ("Setting update mode to \n", arg));
switch (arg) {
case 0:
ret = deleteFile(mess, UPDATE_FILE, "unset update mode");
break;
case 1:
ret = createEmptyFile(mess, UPDATE_FILE, "set update mode");
break;
default:
ret = FAIL;
sprintf(mess, "Could not set updatemode. Options: 0 or 1\n");
LOG(logERROR, (mess));
break;
}
return Server_SendResult(file_des, INT32, NULL, 0);
}