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

让人想犯罪 __ 提交于 2019-12-11 06:27:12

问题


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?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/58477463/permission-denied-when-trying-to-create-message-queue-using-posix-message-queues

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!