l3.libnazgul/src/spaceListInit.c

74 lines
1.6 KiB
C
Raw Normal View History

2004-02-22 22:32:15 +00:00
#include "libnazgul.h"
2004-02-25 21:28:11 +00:00
#include "ids.h"
2004-02-22 22:32:15 +00:00
2020-03-03 23:06:43 +00:00
int msgSpaceListInit()
{
int spaceListFd;
sem_t *spaceListSemFd;
msgSpaceList *list;
2004-02-25 21:28:11 +00:00
2020-03-03 23:06:43 +00:00
msgSpaceListSemId spaceListSemId;
msgSpaceListId spaceListId;
2020-03-03 23:06:43 +00:00
strcpy(spaceListSemId, DEFAULT_MSGSPACELISTSEMID);
strcpy(spaceListId, DEFAULT_MSGSPACELISTID);
2004-02-25 21:28:11 +00:00
2020-03-03 23:06:43 +00:00
/* 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;
}
2004-02-25 22:30:43 +00:00
}
2004-02-25 21:28:11 +00:00
2020-03-03 23:06:43 +00:00
/*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;
}
2004-02-25 21:28:11 +00:00
}
2020-03-03 23:06:43 +00:00
if (ftruncate(spaceListFd, sizeof(msgSpaceList)) < 0) {
NZG_ERROR("ftruncate", spaceListId);
goto ERROR;
}
close(spaceListFd);
2004-02-25 21:28:11 +00:00
2020-03-03 23:06:43 +00:00
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;
}
2004-02-25 22:30:43 +00:00
2020-03-03 23:06:43 +00:00
if (msgSpaceListUnlock() < 0) {
NZG_ERROR("msgSpaceListUnlock", "");
goto ERROR;
}
2004-02-25 21:28:11 +00:00
2020-03-03 23:06:43 +00:00
return 0;
ERROR:
return -1;
EXISTING:
if (msgSpaceListUnlock() < 0) {
NZG_ERROR("msgSpaceListUnlock", "");
goto ERROR;
}
2004-02-25 22:30:43 +00:00
2020-03-03 23:06:43 +00:00
return 0;
2004-02-22 22:32:15 +00:00
};