clinuxposixmessage-queue

mq_open err no 13 Permission denied


I am running in to Permission issues while trying to create posix mq with mq_open() call. I did incorporate the change as mentioned here mq_open Permission denied I looked on other relevant post like this https://groups.google.com/forum/#!topic/comp.unix.programmer/hnTZf6aPpbE but that also points to same thing.

Also while trying to compile I was running into error where mq calls were not identified and online it showed to compile by adding -lrt in gcc, post which was able to compile, mentioning it as I am not completely aware about rationale of it and didnt understand it by reading the post :)

gcc server_mq.c -lrt -o server

error number is 13

Oh dear, something went wrong with mqd ! Permission denied

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

#include "client_server.h"

#define PATH "/tmp/servermq"

int main(void)
{
    mqd_t mqd;
    mode_t omask;
    omask = umask(0);
    int flags = O_RDWR | O_CREAT | O_EXCL;
    struct mq_attr attr, *attrp;

    attr.mq_maxmsg = 5;
    attr.mq_msgsize = 1024;

    attrp = &attr;

    mqd = mq_open(PATH, flags, S_IRUSR | S_IWUSR | S_IWGRP, attrp);


    if (mqd == (mqd_t)-1)
    {
        printf("error number is %d \n ",errno);
        printf(" Oh dear, something went wrong with mqd ! %s\n", strerror(errno));
    }

    umask(omask);
    mq_close(mqd);
    mq_unlink(PATH);
    return 0;
}

Solution

  • You can't use /tmp/servermq as your name...

    Quoting man mq_overview (7):

       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.
    

    Also you will soon find this section relevant:

    Mounting the message queue file system
    On Linux, message queues are created in a virtual file system. (Other implementations may also provide such a feature, but the details are likely to differ.) This file system can be mounted (by the superuser) using the following commands:

               # mkdir /dev/mqueue
               # mount -t mqueue none /dev/mqueue
    
           The sticky bit is automatically enabled on the mount directory.