120 lines
3.3 KiB
C
120 lines
3.3 KiB
C
/*
|
|
This is a personal academic project. Dear PVS-Studio, please check it.
|
|
PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
*/
|
|
#define _SVID_SOURCE 1
|
|
#define _POSIX_C_SOURCE 200112L
|
|
|
|
#include <sys/shm.h>
|
|
#include <semaphore.h>
|
|
#include <fcntl.h>
|
|
#include <string.h>
|
|
#include "error.h"
|
|
#include <sys/mman.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include "shr_mem.h"
|
|
|
|
sem_t * semEmpty = NULL;
|
|
sem_t * semOpen = NULL;
|
|
|
|
void openSem(char * semName, sem_t ** sem) {
|
|
if (* sem == NULL) {
|
|
if ((* sem = sem_open(semName, O_CREAT | O_RDWR, S_IRWXU, 0)) == SEM_FAILED)
|
|
printSystemError("sem_open() -- ERROR");
|
|
}
|
|
}
|
|
|
|
void closeSem(char * semName, sem_t ** sem) {
|
|
if (sem_close(* sem) < 0)
|
|
printSystemError("sem_close() -- ERROR");
|
|
if (sem_unlink(semName) < 0) {
|
|
if (errno != ENOENT)
|
|
printSystemError("sem_unlink() -- ERROR");
|
|
}
|
|
}
|
|
|
|
int createShm(int totalSize) {
|
|
int fd;
|
|
if ((fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, S_IRWXU)) < 0)
|
|
printSystemError("shm_open() -- ERROR");
|
|
ftruncate(fd, totalSize);
|
|
return fd;
|
|
}
|
|
|
|
void mapShm(char ** shmPointer, int totalSize, int fd) {
|
|
openSem(SEM_OPEN_NAME, &semOpen);
|
|
|
|
if ((* shmPointer = mmap(0, totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
|
|
printSystemError("mmap() -- ERROR");
|
|
|
|
if (sem_post(semOpen) < 0)
|
|
printSystemError("sem_post() -- ERROR");
|
|
}
|
|
|
|
void writeShm(char * shmPointer, char * str, int len) {
|
|
openSem(SEM_EMPTY_NAME, &semEmpty);
|
|
|
|
if (memcpy(shmPointer, str, len) == NULL)
|
|
printError("memcpy() -- ERROR");
|
|
|
|
if (sem_post(semEmpty) < 0)
|
|
printSystemError("sem_post() -- ERROR");
|
|
}
|
|
|
|
void terminateShm(const char * name, int fd, char * shmPointer, int totalSize) {
|
|
if (munmap(shmPointer, totalSize) == -1)
|
|
printSystemError("munmap() -- ERROR");
|
|
if (shm_unlink(name) == -1)
|
|
printSystemError("shm_unlink() -- ERROR");
|
|
if (close(fd) == -1)
|
|
printSystemError("close() -- ERROR");
|
|
closeSem(SEM_OPEN_NAME, &semOpen);
|
|
closeSem(SEM_EMPTY_NAME, &semEmpty);
|
|
}
|
|
|
|
int copyShm(char * destPointer, char * srcPointer, int len) {
|
|
int i;
|
|
for (i = 0; i < len && srcPointer[i] != '\n' && srcPointer[i] != '\0'; i++) {
|
|
destPointer[i] = srcPointer[i];
|
|
}
|
|
destPointer[i++] = '\0';
|
|
|
|
return i;
|
|
}
|
|
|
|
int readShm(char * shmPointer, char * str, int len) {
|
|
openSem(SEM_EMPTY_NAME, &semEmpty);
|
|
|
|
if (sem_wait(semEmpty) < 0)
|
|
printSystemError("sem_wait() -- ERROR");
|
|
|
|
return copyShm(str, shmPointer, len);
|
|
}
|
|
|
|
int openCreatedShm(char ** shmPointer, int totalSize) {
|
|
openSem(SEM_OPEN_NAME, &semOpen);
|
|
if (sem_wait(semOpen) < 0)
|
|
printSystemError("sem_wait() -- ERROR");
|
|
|
|
int fd;
|
|
if ((fd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR)) < 0)
|
|
printSystemError("shm_open() -- ERROR");
|
|
|
|
if ((* shmPointer = mmap(0, totalSize, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED){
|
|
printSystemError("mmap() -- ERROR");
|
|
}
|
|
|
|
return fd;
|
|
}
|
|
|
|
void closeCreatedShm(char * shmPointer, int totalSize, int fd) {
|
|
if (munmap(shmPointer, totalSize) == -1)
|
|
printSystemError("munmap() -- ERROR");
|
|
if (close(fd) < 0)
|
|
printSystemError("close() -- ERROR");
|
|
closeSem(SEM_EMPTY_NAME, &semEmpty);
|
|
closeSem(SEM_OPEN_NAME, &semOpen);
|
|
}
|