2004-02-23 21:30:30 +00:00
|
|
|
#include "libnazgul.h"
|
|
|
|
#include "ids.h"
|
|
|
|
|
|
|
|
msgQueue * queueInit(msgSpaceId externId, int queueIdx) {
|
|
|
|
int queueFd;
|
|
|
|
msgQueue * queue;
|
|
|
|
sem_t * semaphore;
|
|
|
|
msgQueueId queueId;
|
|
|
|
|
|
|
|
queue = NULL;
|
|
|
|
|
|
|
|
if(msgQueueIdIntern(queueId, externId, queueIdx) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
queueFd = shm_open(queueId, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, MSGSPACE_DEFAULT_MODE);
|
|
|
|
if(queueFd == -1) {
|
|
|
|
fprintf(stderr, "queueInit : %s initialisation failed: %s\n", queueId, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ftruncate(queueFd, sizeof(* queue)) == -1) {
|
|
|
|
fprintf( stderr, "Queue resizing failed: %s\n",
|
|
|
|
strerror( errno ) );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* on remplit la structure msgQueue */
|
|
|
|
queue->elemCounter = 0;
|
2004-02-24 08:48:48 +00:00
|
|
|
strcpy(queue->headId,queue->id);
|
|
|
|
strcpy(queue->tailId,queue->id);
|
2004-02-23 21:30:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// creation du semaphore
|
|
|
|
semaphore = sem_open(queueId, O_CREAT|O_EXCL, SEM_DEFAULT_MODE, 1);
|
|
|
|
if(semaphore == SEM_FAILED) {
|
|
|
|
NZG_ERROR("sem_open", queueId);
|
|
|
|
goto ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sem_wait(semaphore)==-1){
|
|
|
|
NZG_ERROR("sem_wait",queueId);
|
|
|
|
goto ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* on ferme tout ce qu'il faut */
|
|
|
|
sem_close(semaphore);
|
|
|
|
close(queueFd);
|
|
|
|
|
|
|
|
return queue;
|
|
|
|
ERROR:
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|