Techie
Techie

Reputation: 305

Not able to open a message queue in read mode (mq_open())

I have a program which creates a message queue and send a message (with mq_send()) to this queue. then I try to open the same message queue to read the message from another process. But mq_open() returns -1.

open_and_write_MQ.c

#include <stdio.h>
#include <mqueue.h>
#include <stdlib.h>
#include <string.h>

#define LEN 50


int main(int argc, char * argv[])
{

    struct mq_attr attr;
    mqd_t fd;
    char buff[LEN];
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = LEN;
    attr.mq_curmsgs = 0;
    memset(buff,0,LEN);
    strcpy(buff,"This is just a test message");
    if(argc < 2)
    {
        printf("\nPlease enter at least one argument\n");
        exit(0);
    }   
    else
    {
        fd = mq_open(argv[1], O_CREAT | O_RDWR, 7777, &attr);  
        if(fd == -1)
        {
            printf("\nCould not create a message queue\n");
            exit(0);
        }
        else
        {
            if(mq_send(fd, buff, sizeof(buff), 1) == -1)
            {
                printf("\nCouldn't write message to the queue\n");
                mq_close(fd);
                mq_unlink(argv[1]);
                exit(0);
            } 
            else
            {
                printf("\n Message written to the queue sucussfully\n");
            }
        }
    }
    mq_close(fd);
    printf("\n waiting for other process to read the MQ\n");
    getchar();
    mq_unlink(argv[1]);
    exit(0);
}

This creates the MQ. Below program tries to read the same MQ. open_and_read_MQ.c

#include <stdio.h>
#include <stdlib.h>
#include <mqueue.h>
#include <string.h>
#include <errno.h>

#define LEN 50

int main(int argc , char *argv[])
{
    mqd_t fd;
    char buff[LEN];
    int sterr;
    memset(buff,0,LEN);
    if(argc < 2)
    {
        printf("\nPlease enter the name of message queue\n");
        exit(0);
    }
    else
    {
        fd = mq_open(argv[1], O_RDONLY);
        if(fd == -1)
        {
            sterr = errno;
            printf("\nCouldn't open the message queue. Error : %s\n",strerror(sterr));
            exit(0);
        }
        else
        {
            if(mq_receive(fd, buff, sizeof(buff), NULL) == -1)
            {
                printf("\nMessage could not be received\n");
                mq_close(fd);
                exit(0);
            }   
            else
            {
                printf("Received Message : %s",buff);
            }
        }
    }
    exit(0);
}

Compilation steps:

$ gcc open_and_read_MQ.c -lrt -o open_and_read_MQ
$ gcc open_and_write_MQ.c -lrt -o open_and_write_MQ

execution steps:

$ ./open_and_write_MQ /new
Message written to the queue sucussfully

waiting for other process to read the MQ

then run below program in some other terminal.

$ ./open_and_read_MQ /new
Couldn't open the message queue. Error : Permission denied

How can I set the permissions of process so that it can read the messages from message queue?

Upvotes: 2

Views: 2951

Answers (1)

Ulfalizer
Ulfalizer

Reputation: 4762

The problem is the line

fd = mq_open(argv[1], O_CREAT | O_RDWR, 7777, &attr);

7777 is not a number specified in octal. Use e.g. 0777 instead (or the S_I* constants described in open(2)). Those permissions might be overly broad though.

Upvotes: 1

Related Questions