new exceptions

This commit is contained in:
Erik Frojdh
2019-03-14 16:37:36 +01:00
parent 5235a87e93
commit 9639e480d9
9 changed files with 113 additions and 58 deletions

View File

@ -314,7 +314,7 @@ void multiSlsDetector::initSharedMemory(bool verify) {
FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch "
"(expected 0x"
<< std::hex << MULTI_SHMVERSION << " but got 0x" << multi_shm()->shmversion << std::dec;
throw SharedMemoryException();
throw SharedMemoryError("Shared memory version mismatch!");
}
}
}

View File

@ -12,6 +12,8 @@
#include <cstdlib>
#include <memory>
using sls::RuntimeError;
inline int dummyCallback(detectorData *d, int p, void *) {
std::cout << "got data " << p << std::endl;
return 0;
@ -73,7 +75,7 @@ class multiSlsDetectorClient {
try {
localDet = sls::make_unique<multiSlsDetector>(parser.multi_id(), verify, update);
detPtr = localDet.get();
} catch (const SlsDetectorPackageExceptions &e) {
} catch (const RuntimeError &e) {
/*std::cout << e.GetMessage() << std::endl;*/
return;
} catch (...) {

View File

@ -30,6 +30,8 @@
#include <iostream>
#include <string>
using sls::SharedMemoryError;
template <typename T>
class SharedMemory {
public:
@ -118,21 +120,23 @@ class SharedMemory {
/**
* Create Shared memory and call MapSharedMemory to map it to an address
* throws a SharedMemoryException exception on failure to create, ftruncate or map
* throws a SharedMemoryError exception on failure to create, ftruncate or map
* @param sz of shared memory
*/
void CreateSharedMemory() {
fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) {
FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno);
throw SharedMemoryException();
std::string msg = "Create shared memory " + name + " failed: " + strerror(errno);
FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
}
if (ftruncate(fd, sizeof(T)) < 0) {
FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno);
std::string msg = "Create shared memory " + name + " failed at ftruncate: " + strerror(errno);
FILE_LOG(logERROR) << msg;
close(fd);
RemoveSharedMemory();
throw SharedMemoryException();
throw SharedMemoryError(msg);
}
shared_struct = MapSharedMemory();
@ -141,14 +145,15 @@ class SharedMemory {
/**
* Open existing Shared memory and call MapSharedMemory to map it to an address
* throws a SharedMemoryException exception on failure to open or map
* throws a SharedMemoryError exception on failure to open or map
* @param sz of shared memory
*/
void OpenSharedMemory() {
fd = shm_open(name.c_str(), O_RDWR, 0);
if (fd < 0) {
FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno);
throw SharedMemoryException();
std::string msg = "Open existing shared memory " + name + " failed: " + strerror(errno);
FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
}
shared_struct = MapSharedMemory();
@ -156,14 +161,15 @@ class SharedMemory {
/**
* Unmap shared memory from an address
* throws a SharedMemoryException exception on failure
* throws a SharedMemoryError exception on failure
*/
void UnmapSharedMemory() {
if (shared_struct != nullptr) {
if (munmap(shared_struct, shmSize) < 0) {
FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno);
std::string msg = "Unmapping shared memory " + name +" failed: " + strerror(errno);
FILE_LOG(logERROR) << msg;
close(fd);
throw SharedMemoryException();
throw SharedMemoryError(msg);
}
shared_struct = nullptr;
}
@ -178,8 +184,9 @@ class SharedMemory {
// silent exit if shm did not exist anyway
if (errno == ENOENT)
return;
FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno);
throw SharedMemoryException();
std::string msg = "Free Shared Memory " + name + " Failed: " + strerror(errno);
FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
}
FILE_LOG(logINFO) << "Shared memory deleted " << name;
}
@ -229,10 +236,10 @@ class SharedMemory {
std::string temp = ss.str();
if (temp.length() > NAME_MAX) {
FILE_LOG(logERROR) << "Shared memory initialization failed. " << temp << " has " << temp.length() << " characters. \n"
"Maximum is "
<< NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME;
throw SharedMemoryException();
std::string msg = "Shared memory initialization failed. " + temp + " has " + std::to_string(temp.length()) + " characters. \n"
+ "Maximum is " + std::to_string(NAME_MAX) + ". Change the environment variable " + SHM_ENV_NAME;
FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
}
return temp;
}
@ -246,9 +253,10 @@ class SharedMemory {
T *MapSharedMemory() {
void *addr = mmap(nullptr, sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno);
std::string msg = "Mapping shared memory " + name + " failed: " + strerror(errno);
FILE_LOG(logERROR) << msg;
close(fd);
throw SharedMemoryException();
throw SharedMemoryError(msg);
}
shmSize = sizeof(T);
close(fd);
@ -264,19 +272,20 @@ class SharedMemory {
struct stat sb;
// could not fstat
if (fstat(fd, &sb) < 0) {
FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match "
"(could not fstat): "
<< strerror(errno);
std::string msg = "Could not verify existing shared memory " + name + " size match "
+ "(could not fstat): " + strerror(errno);
FILE_LOG(logERROR) << msg;
close(fd);
throw SharedMemoryException();
throw SharedMemoryError(msg);
}
//size does not match
long unsigned int sz = (long unsigned int)sb.st_size;
if (sz != expectedSize) {
FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match";
FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz;
throw SharedMemoryException();
std::string msg = "Existing shared memory " + name + " size does not match"
+ "Expected " + std::to_string(expectedSize) + ", found " + std::to_string(sz);
FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
return 1;
}
return 0;

View File

@ -22,6 +22,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cassert>
#define DEFAULT_HOSTNAME "localhost"
@ -230,7 +231,7 @@ void slsDetector::initSharedMemory(detectorType type,
"version mismatch "
"(expected 0x"
<< std::hex << SLS_SHMVERSION << " but got 0x" << detector_shm()->shmversion << ")" << std::dec;
throw SharedMemoryException();
throw SharedMemoryError("Shared memory version mismatch (det)");
}
}
}
@ -537,7 +538,7 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b
if (!shm.IsExisting()) {
FILE_LOG(logERROR) << "Shared memory " << shm.GetName() << " does not exist.\n"
"Corrupted Multi Shared memory. Please free shared memory.";
throw SharedMemoryException();
throw SharedMemoryError("Could not read detector type from shared memory");
}
// open, map, verify version
@ -551,7 +552,7 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b
<< std::hex << SLS_SHMVERSION << " but got 0x" << shm()->shmversion << ")" << std::dec;
// unmap and throw
detector_shm.UnmapSharedMemory();
throw SharedMemoryException();
throw SharedMemoryError("Shared memory version mismatch");
}
auto type = shm()->myDetectorType;
return type;