l3.libnazgul/src/spaceListInit.c

74 lines
1.6 KiB
C

#include "libnazgul.h"
#include "ids.h"
int msgSpaceListInit()
{
int spaceListFd;
sem_t *spaceListSemFd;
msgSpaceList *list;
msgSpaceListSemId spaceListSemId;
msgSpaceListId spaceListId;
strcpy(spaceListSemId, DEFAULT_MSGSPACELISTSEMID);
strcpy(spaceListId, DEFAULT_MSGSPACELISTID);
/* Creation de semaphore */
spaceListSemFd = sem_open(spaceListSemId, O_CREAT | O_EXCL, 0666, 0);
if (spaceListSemFd == SEM_FAILED) {
spaceListSemFd = sem_open(spaceListSemId, O_CREAT, 0666, 0);
if (spaceListSemFd == SEM_FAILED) {
NZG_ERROR("sem_open", spaceListSemId);
goto ERROR;
}
}
/*Fait un segment de memoir partager sur espace de listelement */
spaceListFd = shm_open(spaceListId,
O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0666);
if (spaceListFd == -1) {
spaceListFd = shm_open(spaceListId, O_RDWR, 0666);
if (spaceListFd == -1) {
NZG_ERROR("shm_open", spaceListId);
goto ERROR;
} else {
close(spaceListFd);
goto EXISTING;
}
}
if (ftruncate(spaceListFd, sizeof(msgSpaceList)) < 0) {
NZG_ERROR("ftruncate", spaceListId);
goto ERROR;
}
close(spaceListFd);
list = msgSpaceListOpen();
if (list == NULL) {
NZG_ERROR("msgSpaceListOpen", "");
goto ERROR;
}
strcpy(list->id, spaceListId);
strcpy(list->headId, spaceListId);
strcpy(list->tailId, spaceListId);
if (msgSpaceListClose(list) < 0) {
NZG_ERROR("msgSpaceListClose", "");
goto ERROR;
}
if (msgSpaceListUnlock() < 0) {
NZG_ERROR("msgSpaceListUnlock", "");
goto ERROR;
}
return 0;
ERROR:
return -1;
EXISTING:
if (msgSpaceListUnlock() < 0) {
NZG_ERROR("msgSpaceListUnlock", "");
goto ERROR;
}
return 0;
};