Merge branch 'developer' into gotthard2

This commit is contained in:
maliakal_d 2020-07-17 10:34:46 +02:00
commit f70d28b175
11 changed files with 58 additions and 28 deletions

View File

@ -1,10 +1,9 @@
#pragma once #pragma once
#include "sls_detector_defs.h" #include "sls_detector_defs.h"
#include <semaphore.h>
void sharedMemory_print(); void sharedMemory_print();
int sharedMemory_create(int port); int sharedMemory_create(int port);
void sharedMemory_initialize(); int sharedMemory_initialize();
int sharedMemory_open(int port); int sharedMemory_open(int port);
int sharedMemory_attach(); int sharedMemory_attach();
int sharedMemory_detach(); int sharedMemory_detach();

View File

@ -2,6 +2,7 @@
#include "clogger.h" #include "clogger.h"
#include <errno.h> #include <errno.h>
#include <pthread.h>
#include <string.h> #include <string.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
@ -10,10 +11,11 @@
#define SHM_NAME "sls_server_shared_memory" #define SHM_NAME "sls_server_shared_memory"
#define SHM_VERSION 0x200625 #define SHM_VERSION 0x200625
#define SHM_KEY 5678 #define SHM_KEY 5678
#define MEM_SIZE 128
typedef struct Memory { typedef struct Memory {
int version; int version;
sem_t sem; pthread_mutex_t lock;
enum runStatus scanStatus; // idle, running or error enum runStatus scanStatus; // idle, running or error
int scanStop; int scanStop;
#ifdef VIRTUAL #ifdef VIRTUAL
@ -43,34 +45,46 @@ void sharedMemory_print() {
} }
int sharedMemory_create(int port) { int sharedMemory_create(int port) {
// if sham existed, delete old shm and create again // if shm existed, delete old shm and create again
shmFd = shmFd = shmget(SHM_KEY + port, MEM_SIZE, IPC_CREAT | IPC_EXCL | 0666);
shmget(SHM_KEY + port, sizeof(sharedMem), IPC_CREAT | IPC_EXCL | 0666);
if (shmFd == -1 && errno == EEXIST) { if (shmFd == -1 && errno == EEXIST) {
char cmd[MAX_STR_LENGTH]; // open existing one
memset(cmd, 0, MAX_STR_LENGTH); shmFd = shmget(SHM_KEY + port, MEM_SIZE, IPC_CREAT | 0666);
sprintf(cmd, "ipcrm -M 0x%x", SHM_KEY + port); if (shmFd == -1) {
system(cmd); LOG(logERROR,
LOG(logWARNING, ("c: open existing shared memory (to delete) failed: %s\n",
("Removed old shared memory with id 0x%x\n", SHM_KEY + port)); strerror(errno)));
shmFd = shmget(SHM_KEY + port, sizeof(sharedMem), return FAIL;
IPC_CREAT | IPC_EXCL | 0666); }
// delete existing one
sharedMemory_remove();
LOG(logWARNING, ("Removed old shared memory with id 0x%x (%d)\n",
SHM_KEY + port, SHM_KEY + port));
// create it again with current structure
shmFd = shmget(SHM_KEY + port, MEM_SIZE, IPC_CREAT | IPC_EXCL | 0666);
} }
if (shmFd == -1) { if (shmFd == -1) {
LOG(logERROR, ("Create shared memory failed: %s\n", strerror(errno))); LOG(logERROR, ("Create shared memory failed: %s\n", strerror(errno)));
return FAIL; return FAIL;
} }
LOG(logINFO, ("Shared memory created\n")); LOG(logINFO, ("Shared memory created with key 0x%x\n", SHM_KEY + port));
if (sharedMemory_attach() == FAIL) { if (sharedMemory_attach() == FAIL) {
return FAIL; return FAIL;
} }
sharedMemory_initialize(); if (sharedMemory_initialize() == FAIL) {
return FAIL;
}
return OK; return OK;
} }
void sharedMemory_initialize() { int sharedMemory_initialize() {
shm->version = SHM_VERSION; shm->version = SHM_VERSION;
sem_init(&(shm->sem), 1, 1); if (pthread_mutex_init(&(shm->lock), NULL) != 0) {
LOG(logERROR,
("Failed to initialize pthread lock for shared memory\n"));
return FAIL;
}
shm->scanStatus = IDLE; shm->scanStatus = IDLE;
shm->scanStop = 0; shm->scanStop = 0;
#ifdef VIRTUAL #ifdef VIRTUAL
@ -78,10 +92,11 @@ void sharedMemory_initialize() {
shm->stop = 0; shm->stop = 0;
#endif #endif
LOG(logINFO, ("Shared memory initialized\n")) LOG(logINFO, ("Shared memory initialized\n"))
return OK;
} }
int sharedMemory_open(int port) { int sharedMemory_open(int port) {
shmFd = shmget(SHM_KEY + port, sizeof(sharedMem), 0666); shmFd = shmget(SHM_KEY + port, MEM_SIZE, 0666);
if (shmFd == -1) { if (shmFd == -1) {
LOG(logERROR, ("Open shared memory failed: %s\n", strerror(errno))); LOG(logERROR, ("Open shared memory failed: %s\n", strerror(errno)));
return FAIL; return FAIL;
@ -126,9 +141,9 @@ int sharedMemory_remove() {
return OK; return OK;
} }
void sharedMemory_lock() { sem_wait(&(shm->sem)); } void sharedMemory_lock() { pthread_mutex_lock(&(shm->lock)); }
void sharedMemory_unlock() { sem_post(&(shm->sem)); } void sharedMemory_unlock() { pthread_mutex_unlock(&(shm->lock)); }
#ifdef VIRTUAL #ifdef VIRTUAL
void sharedMemory_setStatus(enum runStatus s) { void sharedMemory_setStatus(enum runStatus s) {

View File

@ -29,6 +29,11 @@ extern int phaseShift;
void error(char *msg) { perror(msg); } void error(char *msg) { perror(msg); }
void sigInterruptHandler(int p) {
sharedMemory_remove();
exit(-1);
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// print version // print version
@ -99,6 +104,17 @@ int main(int argc, char *argv[]) {
// control server // control server
if (isControlServer) { if (isControlServer) {
LOG(logINFOBLUE, ("Control Server [%d]\n", portno)); LOG(logINFOBLUE, ("Control Server [%d]\n", portno));
// Catch signal SIGINT to destroy shm properly
struct sigaction sa;
sa.sa_flags = 0; // no flags
sa.sa_handler = sigInterruptHandler; // handler function
sigemptyset(&sa.sa_mask); // dont block additional signals during
// invocation of handler
if (sigaction(SIGINT, &sa, NULL) == -1) {
LOG(logERROR, ("Could not set handler function for SIGINT"));
}
if (sharedMemory_create(portno) == FAIL) { if (sharedMemory_create(portno) == FAIL) {
return -1; return -1;
} }

View File

@ -3,10 +3,10 @@
#define APILIB 0x200409 #define APILIB 0x200409
#define APIRECEIVER 0x200409 #define APIRECEIVER 0x200409
#define APIGUI 0x200409 #define APIGUI 0x200409
#define APICTB 0x200703 #define APIJUNGFRAU 0x200716
#define APIGOTTHARD 0x200703 #define APICTB 0x200716
#define APIJUNGFRAU 0x200703 #define APIMOENCH 0x200707
#define APIMOENCH 0x200702 #define APIGOTTHARD 0x200716
#define APIEIGER 0x200703 #define APIMYTHEN3 0x200716
#define APIGOTTHARD2 0x200713 #define APIGOTTHARD2 0x200716
#define APIMYTHEN3 0x200713 #define APIEIGER 0x200716