l3.libnazgul/src/spaceListInit.c
2020-03-03 23:46:32 +01:00

59 lines
1.3 KiB
C

#include "libnazgul.h"
#include "ids.h"
int msgSpaceListInit(){
int spaceListFd;
sem_t * spaceListSemFd;
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,0600);
if (spaceListFd == -1){
spaceListFd=shm_open(spaceListId,O_RDWR|O_TRUNC,0600);
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);
msgSpaceList *list;
list=msgSpaceListOpen();
strcpy(list->id,spaceListId);
strcpy(list->headId,spaceListId);
strcpy(list->tailId,spaceListId);
msgSpaceListClose(list);
msgSpaceListUnlock();
return 0;
ERROR:
return -1;
EXISTING:
return 0;
};