diff --git a/src/allocate.c b/src/allocate.c index 0f00688..477b160 100644 --- a/src/allocate.c +++ b/src/allocate.c @@ -146,7 +146,7 @@ if (mSPoolDataTabAddr==NULL){ } if (!gotRessourceSem){ - msgPoolSemIdIntern(ressourceSemId,space->id,selectedPoolIndex); + msgPoolSemIdIntern(ressourceSemId,space->externId,selectedPoolIndex); ressourceSemFd=sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0); if(ressourceSemFd==SEM_FAILED){ NZG_ERROR("sem_open",ressourceSemId); diff --git a/src/free.c b/src/free.c index 92d2e91..8253540 100644 --- a/src/free.c +++ b/src/free.c @@ -48,7 +48,7 @@ int msgFree(msgSpace * space, void * addr){ msgPoolDataTabUnlock(space); // deverouiller semaphore ressource. - msgPoolSemIdIntern(ressourceSemId,space->id,poolIndex); + msgPoolSemIdIntern(ressourceSemId,space->externId,poolIndex); ressourceSemFd = sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0); if (ressourceSemFd == SEM_FAILED){ NZG_ERROR("sem_open",ressourceSemId); diff --git a/src/global.h b/src/global.h index a2c3931..ad6fe15 100644 --- a/src/global.h +++ b/src/global.h @@ -25,6 +25,19 @@ #define SEM_FAILED ((sem_t *)0) #endif + +#ifdef _NZG_REALFILEID +#define DEFAULT_MSGSPACELISTID "/tmp/nzgSpaceList" +#else +#define DEFAULT_MSGSPACELISTID "/nzgSpaceList" +#endif + +#ifdef _NZG_REALFILEID +#define DEFAULT_MSGSPACELISTSEMID "/tmp/nzgSpaceListSem" +#else +#define DEFAULT_MSGSPACELISTSEMID "/nzgSpaceListSem" +#endif + #define NZG_ERROR(func,str) fprintf(stderr,"*** %s: %s***\n%s in %s:%d\n",func,strerror(errno),str,__FILE__,__LINE__); #endif diff --git a/src/ids.h b/src/ids.h index 7cd35d4..219eaa6 100644 --- a/src/ids.h +++ b/src/ids.h @@ -2,17 +2,7 @@ #define _NZG_IDS 1 #include "libnazgul.h" -#ifdef _NZG_REALFILEID -#define msgSpaceListId "/tmp/nzgSpaceList" -#else -#define msgSpaceListId "/nzgSpaceList" -#endif -#ifdef _NZG_REALFILEID -#define msgSpaceListSemId "/tmp/nzgSpaceListSem" -#else -#define msgSpaceListSemId "/nzgSpaceListSem" -#endif /* nzg_ids.c */ diff --git a/src/iface.h b/src/iface.h index fa8b332..4ba1bcf 100644 --- a/src/iface.h +++ b/src/iface.h @@ -7,8 +7,8 @@ typedef enum { true=1, false=0} bool; typedef char msgSpaceId[MSGSPACE_ID_LEN]; -typedef char msgSpaceListId[4*MSGSPACE_ID_LEN]; -typedef char msgSpaceListSemId[4*MSGSPACE_ID_LEN]; +typedef char msgSpaceListId[4*MSGSPACE_ID_LEN]; +typedef char msgSpaceListSemId[4*MSGSPACE_ID_LEN]; typedef char msgSpaceListElemId[4*MSGSPACE_ID_LEN]; typedef char msgPoolDataTabId[4*MSGSPACE_ID_LEN]; diff --git a/src/proto.h b/src/proto.h index a49cfef..48937d7 100644 --- a/src/proto.h +++ b/src/proto.h @@ -97,6 +97,9 @@ int msgSpaceListElemCreate(msgSpaceListElemId listElemId, msgSpace *space); /* spaceListElemLink.c */ /* spaceListElemOpen.c */ void *msgSpaceListElemOpen(msgSpaceListElemId listElemId); +/* spaceListFindId.c */ +int msgSpaceListFindId(msgSpaceId spaceId); +int msgSpaceListElemFindId(msgSpaceListElemId elemId, msgSpaceId spaceId); /* spaceListInit.c */ int msgSpaceListInit(void); /* spaceListLocking.c */ diff --git a/src/spaceCreate.c b/src/spaceCreate.c index 4a10ae6..c88ef35 100644 --- a/src/spaceCreate.c +++ b/src/spaceCreate.c @@ -41,6 +41,22 @@ msgSpace * msgSpaceCreate( if (msgSpaceIdIntern(nzgId,externId) < 0){ return NULL; } + + msgSpaceListInit(); + printf("spaceListInit ok\n"); + msgSpaceListLock(); + printf("spaceListLock ok\n"); + if (msgSpaceListFindId(nzgId) < 1){ + NZG_ERROR("spaceListFindId",nzgId); + // zut, il y a soit une erreur + // soit le msgSpace existe deja + // on quitte + goto ERROR; + } + printf("spaceListFind ok\n"); + msgSpaceListUnlock(); + printf("spaceListUnlock ok\n"); + fprintf(stderr,"Creating msgSpace with id : %s\n",nzgId); mSFd=shm_open( nzgId, @@ -73,7 +89,7 @@ msgSpace * msgSpaceCreate( close(mSFd); /* on remplit la structure */ - strncpy(space->id,externId,MSGSPACE_ID_LEN); + strcpy(space->id,nzgId); space->poolNb=poolNb; space->queueNb=queueNb; space->pid=getpid(); @@ -145,11 +161,17 @@ msgSpace * msgSpaceCreate( msgPoolDataTabClose(space,poolDataTabAddr); /* on ajoute spaceId a la liste des msgSpace connus */ msgSpaceListElemId listElemId; -msgSpaceListInit(); +printf("spaceListInit...\n"); + +printf("ok\n"); msgSpaceListLock(); +printf("spaceListLock...ok\n"); msgSpaceListElemCreate(listElemId,space); +printf("spaceListElemCreate...ok\n"); msgSpaceListAdd(listElemId); +printf("spaceListAdd...ok\n"); msgSpaceListUnlock(); +printf("spaceListUnlock...ok\n"); /* on renvoie un pointeur sur le bon spaceId */ msgPoolDataTabUnlock(space); diff --git a/src/spaceListClose.c b/src/spaceListClose.c index ebd2efc..64864df 100644 --- a/src/spaceListClose.c +++ b/src/spaceListClose.c @@ -2,9 +2,12 @@ #include "ids.h" int msgSpaceListClose(msgSpaceList * list){ - + + msgSpaceListId spaceListId; + strcpy(spaceListId,DEFAULT_MSGSPACELISTID); + if (munmap(list,sizeof(msgSpaceList)) < 0){ - NZG_ERROR("unmap",msgSpaceListId); + NZG_ERROR("unmap",spaceListId); goto ERROR; } diff --git a/src/spaceListElemCreate.c b/src/spaceListElemCreate.c index d7ed2bd..c701fa4 100644 --- a/src/spaceListElemCreate.c +++ b/src/spaceListElemCreate.c @@ -7,7 +7,8 @@ int msgSpaceListElemCreate( msgSpace * space) { - strcpy(listElemId,space->id); + msgSpaceListElemIdIntern(listElemId,space->externId); + printf("Creating ListElem %s",listElemId); int listElemFd; listElemFd=shm_open(listElemId, @@ -25,6 +26,11 @@ int msgSpaceListElemCreate( } close(listElemFd); + msgSpaceListElem * listElem; + listElem=msgSpaceListElemOpen(listElemId); + strcpy(listElem->id,listElemId); + strcpy(listElem->spaceId,space->id); + msgSpaceListElemClose(listElem); return 0; ERROR: return -1; diff --git a/src/spaceListInit.c b/src/spaceListInit.c index 8092958..1353965 100644 --- a/src/spaceListInit.c +++ b/src/spaceListInit.c @@ -1,47 +1,58 @@ #include "libnazgul.h" +#include "ids.h" int msgSpaceListInit(){ - int descMemory; - sem_t * spacesListSemFd; - msgSpaceListSemId spacesListSemId; - msgSpaceListElemId spacesListId; - - /*Creation de semaphore*/ - spacesListSemFd=sem_open(msgSpaceListSemId,O_CREAT|O_EXCL,0666,0); - if(spacesListSemFd==SEM_FAILED){ - NZG_ERROR("sem_open",msgSpaceListSemId); - goto ERROR; - } - - - - /*Fait un segment de memoir partager sur espace de listelement*/ - descMemory=shm_open(msgSpaceListId,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,0600); - if (descMemory == -1){ - descMemory=shm_open(msgSpaceListId,O_RDWR|O_TRUNC,0600); - if (descMemory == -1){ - NZG_ERROR("shm_open",spacesListId); - goto ERROR; - } else { - close(descMemory); - goto EXISTING; - } - } + 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; + } + } - /*Creation de semaphore*/ - spacesListSemFd=sem_open(spacesListSemId,O_CREAT|O_EXCL,0666,0); - if(spacesListSemFd==SEM_FAILED){ - NZG_ERROR("sem_open",spacesListSemId); - goto ERROR; - } - msgSpaceListUnlock(); + /*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); - return 0; + msgSpaceList *list; + list=msgSpaceListOpen(); + strcpy(list->id,spaceListId); + strcpy(list->headId,spaceListId); + strcpy(list->tailId,spaceListId); + msgSpaceListClose(list); + + msgSpaceListUnlock(); + + return 0; ERROR: - return -1; + return -1; EXISTING: - return 0; + return 0; }; diff --git a/src/spaceListLocking.c b/src/spaceListLocking.c index fa7cba3..b6f6dd5 100644 --- a/src/spaceListLocking.c +++ b/src/spaceListLocking.c @@ -4,65 +4,66 @@ // verouille le semaphore de la liste int msgSpaceListLock(){ - sem_t * spacesListSemFd; - msgSpaceListSemId spacesListSemId; + sem_t * msgSpaceListSemFd; + msgSpaceListSemId spaceListSemId; + strcpy(spaceListSemId,DEFAULT_MSGSPACELISTSEMID); - /*Ouverture d'un verrou d'un semafore*/ - //TODO: remplacer par la bonne valeur d'initialisation du - //semaphore - spacesListSemFd=sem_open(spacesListSemId,O_CREAT|O_EXCL,0666,1024); - if(spacesListSemFd==SEM_FAILED){ - NZG_ERROR("sem_open",spacesListSemId); - goto ERROR; - } + /* Ouverture d'un semafore */ + msgSpaceListSemFd=sem_open(spaceListSemId,O_CREAT,0666,1); + if(msgSpaceListSemFd==SEM_FAILED){ + NZG_ERROR("sem_open",spaceListSemId); + goto ERROR; + } - /*Pose d'un semaphore et le verrouille*/ - if(sem_wait(spacesListSemFd)==-1){ - NZG_ERROR("sem_wait",spacesListSemId); - goto ERROR; - } + /* Pose d'un semaphore et le verrouille */ + if(sem_wait(msgSpaceListSemFd)==-1){ + NZG_ERROR("sem_wait",spaceListSemId); + goto ERROR; + } - /* Ferme le semaphore*/ - /* if(sem_close(spacesListSemFd)==-1){ - NZG_ERROR("sem_close",spacesListSemId); - return -1; - }*/ - sem_close(spacesListSemFd); + /* Ferme le semaphore*/ + /* if(sem_close(msgSpaceListSemFd)==-1){ + NZG_ERROR("sem_close",spaceListSemId); + return -1; + }*/ + sem_close(msgSpaceListSemFd); - return 0; + return 0; ERROR: - return -1; + return -1; } // deverouille le semaphore de la liste int msgSpaceListUnlock(){ - sem_t * spacesListSemFd; - msgSpaceListSemId spacesListSemId; - - /*Ouverture dun semaphore*/ - spacesListSemFd=sem_open(spacesListSemId,O_CREAT|O_EXCL,0666,1024); - if(spacesListSemFd==SEM_FAILED){ - NZG_ERROR("sem_open",spacesListSemId); - goto ERROR; - } + sem_t * msgSpaceListSemFd; + msgSpaceListSemId spaceListSemId; + strcpy(spaceListSemId,DEFAULT_MSGSPACELISTSEMID); - /*Relachement du semaphore*/ - if(sem_post(spacesListSemFd)==-1){ - NZG_ERROR("sem_relache",spacesListSemId); - goto ERROR; - } + /*Ouverture dun semaphore*/ + msgSpaceListSemFd=sem_open(spaceListSemId,O_CREAT,0666,0); + if(msgSpaceListSemFd==SEM_FAILED){ + NZG_ERROR("sem_open",spaceListSemId); + goto ERROR; + } - /* Ferme le semaphore - if(sem_close(spacesListSemFd)==-1){ - NZG_ERROR("sem_close",spacesListSemId); - return -1; - }*/ + /*Relachement du semaphore*/ + if(sem_post(msgSpaceListSemFd)==-1){ + NZG_ERROR("sem_relache",spaceListSemId); + goto ERROR; + } - return 0; + + /* Ferme le semaphore */ + if(sem_close(msgSpaceListSemFd)==-1){ + NZG_ERROR("sem_close",spaceListSemId); + goto ERROR; + } + + return 0; ERROR: - return -1; + return -1; }; diff --git a/src/spaceListOpen.c b/src/spaceListOpen.c index 696abdb..da8b1e8 100644 --- a/src/spaceListOpen.c +++ b/src/spaceListOpen.c @@ -5,10 +5,12 @@ void * msgSpaceListOpen(){ int listFd; void * listAddr; - - listFd=shm_open(msgSpaceListId,O_RDWR,SHM_DEFAULT_MODE); + msgSpaceListId spaceListId; + strcpy(spaceListId,DEFAULT_MSGSPACELISTID); + + listFd=shm_open(spaceListId,O_RDWR,SHM_DEFAULT_MODE); if (listFd == -1 ) { - NZG_ERROR("shm_open : msgSpaceList open",msgSpaceListId); + NZG_ERROR("shm_open : msgSpaceList open",spaceListId); goto ERROR; } @@ -19,7 +21,7 @@ void * msgSpaceListOpen(){ listFd, 0); if( listAddr == MAP_FAILED ) { - NZG_ERROR("mmap",msgSpaceListId); + NZG_ERROR("mmap",spaceListId); goto ERROR; }