diff --git a/slsDetectorServers/slsDetectorServer/include/common.h b/slsDetectorServers/slsDetectorServer/include/common.h index 3c2b3fccc..821e28040 100644 --- a/slsDetectorServers/slsDetectorServer/include/common.h +++ b/slsDetectorServers/slsDetectorServer/include/common.h @@ -8,6 +8,10 @@ #include #include +#define TEMP_PROG_FOLDER_NAME "/var/tmp/" +#define TEMP_PROG_FOLDER_NAME_ALL_FILES "/var/tmp/*" +#define TEMP_PROG_FILE_NAME TEMP_PROG_FOLDER_NAME "tmp.rawbin" + enum numberMode { DEC, HEX }; enum PROGRAM_INDEX { PROGRAM_FPGA, PROGRAM_KERNEL, PROGRAM_SERVER }; @@ -49,4 +53,7 @@ int verifyChecksum(char *mess, char *functionType, char *clientChecksum, MD5_CTX *c, char *msg); int setupDetectorServer(char *mess, char *sname); -int writeBinaryFile(char* mess, char* fname, char* buffer, const uint64_t filesize); \ No newline at end of file +int writeBinaryFile(char *mess, char *fname, char *buffer, + const uint64_t filesize, char *errorPrefix); + +int moveBinaryFile(char *mess, char *dest, char *src, char *errorPrefix); diff --git a/slsDetectorServers/slsDetectorServer/include/programViaBlackfin.h b/slsDetectorServers/slsDetectorServer/include/programViaBlackfin.h index afb76acf2..9abe2e678 100644 --- a/slsDetectorServers/slsDetectorServer/include/programViaBlackfin.h +++ b/slsDetectorServers/slsDetectorServer/include/programViaBlackfin.h @@ -8,10 +8,6 @@ #include #include -#define TEMP_PROG_FOLDER_NAME "/var/tmp/" -#define TEMP_PROG_FOLDER_NAME_ALL_FILES "/var/tmp/*" -#define TEMP_PROG_FILE_NAME TEMP_PROG_FOLDER_NAME "tmp.rawbin" - int defineGPIOpins(char *mess); int FPGAdontTouchFlash(char *mess); int FPGATouchFlash(char *mess); @@ -37,4 +33,3 @@ int writeToFlash(char *mess, ssize_t fsize, FILE *flashfd, FILE *srcfd); /** Notify fpga to pick up firmware from flash and wait for status confirmation */ int waitForFPGAtoTouchFlash(char *mess); -int moveBinaryFile(char *mess, char *serverName); diff --git a/slsDetectorServers/slsDetectorServer/src/common.c b/slsDetectorServers/slsDetectorServer/src/common.c index 191fd118e..893c2f41c 100644 --- a/slsDetectorServers/slsDetectorServer/src/common.c +++ b/slsDetectorServers/slsDetectorServer/src/common.c @@ -496,15 +496,15 @@ int setupDetectorServer(char *mess, char *sname) { } int writeBinaryFile(char *mess, char *fname, char *buffer, - const uint64_t filesize) { + const uint64_t filesize, char *errorPrefix) { LOG(logINFO, ("\tWriting Detector Server Binary...\n")); FILE *fp = fopen(fname, "wb"); if (fp == NULL) { sprintf(mess, - "Could not copy detector server. (opening file to write(%s). " - "Maybe it is being used? Try another server name?\n", - fname); + "Could not %s. (opening file to write(%s). " + "Maybe it is being used? Try another name?\n", + errorPrefix, fname); LOG(logERROR, (mess)); return FAIL; } @@ -532,9 +532,9 @@ int writeBinaryFile(char *mess, char *fname, char *buffer, // write if (bytes != (size_t)writeSize) { sprintf(mess, - "Could not copy detector server. Expected to write %lu " + "Could not %s. Expected to write %lu " "bytes, wrote %lu bytes). No space left? \n", - (long unsigned int)filesize, + errorPrefix, (long unsigned int)filesize, (long unsigned int)bytesWritten); LOG(logERROR, (mess)); return FAIL; @@ -545,12 +545,35 @@ int writeBinaryFile(char *mess, char *fname, char *buffer, } printf("\n"); if (fclose(fp) != 0) { - sprintf(mess, - "Could not copy detector server. (closing file pointer)\n"); + sprintf(mess, "Could not %s. (closing file pointer)\n", errorPrefix); LOG(logERROR, (mess)); return FAIL; } - LOG(logINFO, ("\tWritten server binary to %s (%lu bytes)\n", fname, + LOG(logINFO, ("\tWritten binary to %s (%lu bytes)\n", fname, (long unsigned int)bytesWritten)); return OK; -} \ No newline at end of file +} + +int moveBinaryFile(char *mess, char *dest, char *src, char *errorPrefix) { + char cmd[MAX_STR_LENGTH] = {0}; + char retvals[MAX_STR_LENGTH] = {0}; + + // one can move into the current process binary (will not interfere in + // kernel mode) + + char *format = "mv %s %s"; + if (snprintf(cmd, MAX_STR_LENGTH, format, src, dest) >= MAX_STR_LENGTH) { + sprintf(mess, "Could not %s. Command to move binary is too long\n", + errorPrefix); + LOG(logERROR, (mess)); + return FAIL; + } + if (executeCommand(cmd, retvals, logDEBUG1) == FAIL) { + snprintf(mess, MAX_STR_LENGTH, "Could not %s. (moving). %s\n", + errorPrefix, retvals); + LOG(logERROR, (mess)); + return FAIL; + } + LOG(logINFO, ("\tMoved file from %s to %s\n", src, dest)); + return OK; +} diff --git a/slsDetectorServers/slsDetectorServer/src/programViaBlackfin.c b/slsDetectorServers/slsDetectorServer/src/programViaBlackfin.c index afe9f519d..3e5d8538b 100644 --- a/slsDetectorServers/slsDetectorServer/src/programViaBlackfin.c +++ b/slsDetectorServers/slsDetectorServer/src/programViaBlackfin.c @@ -574,26 +574,3 @@ int waitForFPGAtoTouchFlash(char *mess) { LOG(logINFO, ("\tFPGA has picked up the program from flash\n")); return OK; } - -int moveBinaryFile(char *mess, char *serverName) { - char cmd[MAX_STR_LENGTH] = {0}; - char retvals[MAX_STR_LENGTH] = {0}; - - char *format = "mv %s %s"; - if (snprintf(cmd, MAX_STR_LENGTH, format, TEMP_PROG_FILE_NAME, - serverName) >= MAX_STR_LENGTH) { - strcpy(mess, "Could not copy detector server. Command " - "to move server binary is too long\n"); - LOG(logERROR, (mess)); - return FAIL; - } - if (executeCommand(cmd, retvals, logDEBUG1) == FAIL) { - snprintf(mess, MAX_STR_LENGTH, - "Could not copy detector server (moving). %s\n", retvals); - LOG(logERROR, (mess)); - return FAIL; - } - LOG(logINFO, ("\tmoved from temp folder to main with proper name (%s)\n", - serverName)); - return OK; -} diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 5dccd4994..f7b74d004 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -9381,7 +9381,8 @@ void receive_program_via_blackfin(int file_des, enum PROGRAM_INDEX index, totalsize); break; case PROGRAM_SERVER: - ret = moveBinaryFile(mess, serverName); + ret = moveBinaryFile(mess, serverName, TEMP_PROG_FILE_NAME, + "update detector server"); if (ret == OK) { ret = setupDetectorServer(mess, serverName); } @@ -9468,7 +9469,14 @@ void receive_program_default(int file_des, enum PROGRAM_INDEX index, #endif #if defined(GOTTHARD2D) || defined(MYTHEN3D) || defined(EIGERD) case PROGRAM_SERVER: - ret = writeBinaryFile(mess, serverName, src, filesize); + ret = writeBinaryFile(mess, TEMP_PROG_FILE_NAME, src, filesize, + "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 + if (ret == OK) { + ret = moveBinaryFile(mess, serverName, TEMP_PROG_FILE_NAME, + "update detector server"); + } if (ret == OK) { ret = verifyChecksumFromFile(mess, functionType, checksum, serverName);