c - 尝试使用 POSIX 消息队列创建消息队列时权限被拒绝

标签 c posix ipc

我正在按照 The Linux Programming Interface 使用以下代码片段创建消息队列.

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

运行此代码片段时出现错误:“权限被拒绝”。我想检查一下我之前是否创建了队列并且没有销毁它,所以我使用了 ipcs。但是,ipcs 不显示任何事件消息队列。我以前从未在我的开发环境(Ubuntu 18.04)中使用过 POSIX IPC 库。我是否必须进行一些设置才能允许我的用户进程创建消息队列?我是否错误地使用了 API?

最佳答案

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

您的代码中没有这三个值。或者更确切地说,您这样做,但它在 mode 中参数,而不是 oflag一,其中对应的数字具有完全不同的含义。第三个参数是创建队列时使用的文件系统权限位(就像创建新文件时 open() 的第三个参数一样),而不是打开队列的模式。

关于c - 尝试使用 POSIX 消息队列创建消息队列时权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58477463/

相关文章:

c++ - 我可以使用内存映射文件传递指针吗?

跨 IPC 的 C# 事件

C程序: Binary not found message and project folder empty although i build my project

预处理器后的 C 代码

c - 通过 tkcon 在 .so 文件上运行 C gdb

我们可以为 linux 用户空间程序中的每个信号分配信号处理程序吗?

c - POSIX 计时器和 POSIX 信号处理

c - MessageQueue 名称和消息队列大小

compilation - unistd.h 和 crypt.h 中的 crypt 有什么区别?

c - 将数据从 cygwin 可执行文件 [C] 传输到托管程序 [托管 C++]?