Anthony O
Anthony O

Reputation: 653

Permission denied when trying to create message queue using POSIX Message Queues

I am creating a message queue with the following snippet by following The Linux Programming Interface.

if((mq_open("/my_message_queue", O_CREAT, O_RDWR, NULL)) == -1) {
    perror("mq creation failed");
}

Running this snippet I get an error: "permission denied". I wanted to check and see if I had created the queue previously and not destroyed it, so I used ipcs. However, ipcs does not show any active message queues. I have never used the POSIX IPC libraries in my development environment before (Ubuntu 18.04). Is some set up I must do to allow my user process to create a message queue? Am I using the API incorrectly?

Upvotes: 2

Views: 3456

Answers (3)

Shawn
Shawn

Reputation: 52364

From the man page:

The oflag argument specifies flags that control the operation of the call. (Definitions of the flags values can be obtained by including <fcntl.h>.) Exactly one of the following must be specified in oflag:

O_RDONLY Open the queue to receive messages only.

O_WRONLY Open the queue to send messages only.

O_RDWR Open the queue to both send and receive messages.

You have none of those three values in your code. Or rather you do, but it's in the mode argument, not the oflag one, where the corresponding number has a completely different meaning. That third argument is the filesystem permission bits used when creating the queue (just like the third argument to open() when creating a new file), not the mode the queue is opened in.

Upvotes: 2

Andrew Henle
Andrew Henle

Reputation: 1

POSIX states :

... The name argument conforms to the construction rules for a pathname, except that the interpretation of <slash> characters other than the leading <slash> character in name is implementation-defined ...

On Linux, the name /my/message_queue is incorrect. Linux requires the name to start with /, but it can contain no other / characters.

Per the Linux mq_overview.7 man page:

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 identified 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.

Upvotes: 0

James K. Lowden
James K. Lowden

Reputation: 7837

EACCES: The queue exists, but the caller does not have permission to open it in the specified mode.

ipcs won't show you queues that you can't read. That it doesn't show you the queue is consistent with mq_open returning a permission error.

TLPI describes mount -t mqueue to a directory of your choosing. You can then use ls(1) on that directory to see the queues, and even interrogate the queues' states by reading the "files" in that directory.

My guess is the queue exists and belongs to root....

Upvotes: 0

Related Questions