WIP shm deletion working on blackfin

This commit is contained in:
2020-07-16 16:00:28 +02:00
parent c67b7aab4d
commit 6136eabee2
3 changed files with 46 additions and 14 deletions

View File

@ -4,7 +4,7 @@
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

@ -10,6 +10,7 @@
#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;
@ -43,34 +44,47 @@ 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, sizeof(sharedMem), IPC_CREAT | IPC_EXCL | 0666); shmget(SHM_KEY + port, MEM_SIZE, 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,
sprintf(cmd, "ipcrm -M 0x%x", SHM_KEY + port); IPC_CREAT | 0666);
system(cmd); if (shmFd == -1) {
LOG(logERROR, ("Create shared memory failed: %s\n", strerror(errno)));
return FAIL;
}
// delete existing one
sharedMemory_remove();
LOG(logWARNING, LOG(logWARNING,
("Removed old shared memory with id 0x%x\n", SHM_KEY + port)); ("Removed old shared memory with id 0x%x (%d)\n", SHM_KEY + port, SHM_KEY + port));
shmFd = shmget(SHM_KEY + port, sizeof(sharedMem),
// create it again with current structure
shmFd = shmget(SHM_KEY + port, MEM_SIZE,
IPC_CREAT | IPC_EXCL | 0666); 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 (sem_init(&(shm->sem), 1, 1)) {
LOG(logERROR, ("Failed to initialize semaphore for shared memory\n"));
LOG(logERROR, ("ERror: %s\n", strerror(errno)));
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;

View File

@ -29,6 +29,12 @@ extern int phaseShift;
void error(char *msg) { perror(msg); } void error(char *msg) { perror(msg); }
void sigInterruptHandler(int p) {
sharedMemory_detach();
sharedMemory_remove();
exit(-1);
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// print version // print version
@ -99,6 +105,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;
} }