Phantom Phantom - 2 months ago 32
C Question

POSIX4 messages queues "mq_open: No such file or directory"

I'm trying to use POSIX4 messages queues. So I'm using

mq_open
to create the queue, and for all the options I give to it an
struct mq_attr
, that I fill.

He can't find the queue, while I put the
O_CREATE
flag.

Here is my code (no-indented lines are debug code):

...
/***
* Queues' names
*/
#define GUI_QUEUE "/guiQ"
...
struct mq_attr attrAct; /* Queue parameters */
/***
* Message queue to send action
*/
attrAct.mq_maxmsg=1;
attrAct.mq_msgsize=sizeof(gui_action);
attrAct.mq_flags=0;
attrAct.mq_curmsgs=0;

printf("serveur first sizeof(gui_action) : %lu\tmsgsize : %lu\n", sizeof(gui_action), attrAct.mq_msgsize);
if ((guiQue=mq_open(GUI_QUEUE, O_CREAT | O_NONBLOCK | O_WRONLY
, S_IWUSR | S_IRUSR , &attrAct))!=0) {
perror("mq_open");
exit(EXIT_FAILURE);
}
if (mq_getattr(guiQue, &attrAct)!=0) {
perror("mq_getattr");
}
else {
printf("serveur second sizeof(gui_action) : %lu\tmsgsize : %lu\n", sizeof(gui_action), attrAct.mq_msgsize);
}
struct mq_attr new;
new=attrAct;
new.mq_msgsize=sizeof(gui_action);
printf("serveur third sizeof(gui_action) : %lu\tmsgsize : %lu\n", sizeof(gui_action), new.mq_msgsize);
if (mq_setattr(guiQue, &new, &attrAct)!=0) perror("mq_setattr");
if (mq_getattr(guiQue, &attrAct)!=0) {
perror("mq_getattr");
}
else {
printf("serveur fourth sizeof(gui_action) : %lu\tmsgsize : %lu\n", sizeof(gui_action), attrAct.mq_msgsize);
}
...


And here is the output:

serveur first sizeof(gui_action) : 16 msgsize : 16
mq_open: No such file or directory


What am I doing wrong?

Answer

mq_open returns (mqd_t) -1 on failure and a message queue descriptor on success.

You are mistaking the successful return of mq_open (which, in practice, is an integer >= 0) for failure, and perror is reporting some previous system call's errno.

Comments