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();
int sharedMemory_create(int port);
void sharedMemory_initialize();
int sharedMemory_initialize();
int sharedMemory_open(int port);
int sharedMemory_attach();
int sharedMemory_detach();

View File

@ -10,6 +10,7 @@
#define SHM_NAME "sls_server_shared_memory"
#define SHM_VERSION 0x200625
#define SHM_KEY 5678
#define MEM_SIZE 128
typedef struct Memory {
int version;
@ -43,34 +44,47 @@ void sharedMemory_print() {
}
int sharedMemory_create(int port) {
// if sham existed, delete old shm and create again
// if shm existed, delete old shm and create again
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) {
char cmd[MAX_STR_LENGTH];
memset(cmd, 0, MAX_STR_LENGTH);
sprintf(cmd, "ipcrm -M 0x%x", SHM_KEY + port);
system(cmd);
// open existing one
shmFd = shmget(SHM_KEY + port, MEM_SIZE,
IPC_CREAT | 0666);
if (shmFd == -1) {
LOG(logERROR, ("Create shared memory failed: %s\n", strerror(errno)));
return FAIL;
}
// delete existing one
sharedMemory_remove();
LOG(logWARNING,
("Removed old shared memory with id 0x%x\n", SHM_KEY + port));
shmFd = shmget(SHM_KEY + port, sizeof(sharedMem),
("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) {
LOG(logERROR, ("Create shared memory failed: %s\n", strerror(errno)));
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) {
return FAIL;
}
sharedMemory_initialize();
if (sharedMemory_initialize() == FAIL) {
return FAIL;
}
return OK;
}
void sharedMemory_initialize() {
int sharedMemory_initialize() {
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->scanStop = 0;
#ifdef VIRTUAL
@ -78,10 +92,11 @@ void sharedMemory_initialize() {
shm->stop = 0;
#endif
LOG(logINFO, ("Shared memory initialized\n"))
return OK;
}
int sharedMemory_open(int port) {
shmFd = shmget(SHM_KEY + port, sizeof(sharedMem), 0666);
shmFd = shmget(SHM_KEY + port, MEM_SIZE, 0666);
if (shmFd == -1) {
LOG(logERROR, ("Open shared memory failed: %s\n", strerror(errno)));
return FAIL;

View File

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