gpt4 book ai didi

c - mq_open errno 13 权限被拒绝

转载 作者:太空狗 更新时间:2023-10-29 11:08:00 24 4
gpt4 key购买 nike

我在尝试使用 mq_open() 调用创建 posix mq 时遇到了权限问题。我确实合并了此处提到的更改 mq_open Permission denied我查看了类似这样的其他相关帖子 https://groups.google.com/forum/#!topic/comp.unix.programmer/hnTZf6aPpbE但这也指向同一件事。

此外,在尝试编译时我遇到了未识别 mq 调用的错误,在线显示通过在 gcc 中添加 -lrt 进行编译,帖子能够编译,提到它是因为我不完全了解基本原理它并没有通过阅读帖子理解它:)

gcc server_mq.c -lrt -o 服务器

错误编号为 13

天哪,mqd 出了点问题! 权限被拒绝

#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;
}

最佳答案

你不能使用 /tmp/servermq 作为你的名字...

引用ma​​n mq_overvie:

   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.

您很快就会发现这部分相关:

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.

关于c - mq_open errno 13 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35093655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com