Permission denied when creating message queue

Hi guys.

i have wrote a simple program to test message queue attributes. here it is:

#include <stdio.h>
#include <stdlib.h>
#include <mqueue.h>
#include <fcntl.h>   
#include <string.h>
#include <errno.h>
#include <sys/stat.h>

int main()
{
    struct mq_attr attr;
    mqd_t mqd;
    if ((mqd = mq_open("/tmp/queue.123", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, NULL)) == -1)
    {
        printf("damn it: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }
    mq_getattr(mqd, &attr);
    printf("max message on queue: %ld\n", attr.mq_maxmsg);
    printf("max message size: %ld\n", attr.mq_msgsize);
    printf("current number of message on the queue: %ld\n", attr.mq_curmsgs);
    mq_close(mqd);
    mq_unlink("/tmp/queue.123");
    return EXIT_SUCCESS;
}

but when i run it always it prints:

damn it: Permission denied

I executed it under root user but again this error.
also permissions for /tmp directory is 777.

From man mq_overview:

Message queues are created and opened using mq_open(3);  this  function
       returns  a  message queue descriptor (mqd_t), which is used to refer to
       the open message queue in later calls.  Each message queue  is  identi-
       fied by a name of the form /somename; that is, a null-terminated string
       of up to NAME_MAX (i.e.,  255)  characters  consisting  of  an  initial
       slash,  followed  by one or more characters, none of which are slashes.
       Two processes can operate on the same queue by passing the same name to
       mq_open(3).

Message queues aren't really files. You don't create them under /tmp/.

1 Like

Thank you very much.