gpt4 book ai didi

c - 设置消息队列大小 "not permitted"

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:20 57 4
gpt4 key购买 nike

我试图设置 POSIX 消息队列的大小,但似乎不被允许。

msgctl() 手册页指出:

IPC_SET can only be executed by a process with appropriate privileges or that has an effective user ID equal to the value of msg_perm.cuid or msg_perm.uid in the msqid_ds data structure associated with msqid. Only a process with appropriate privileges can raise the value of msg_qbytes.

下面的测试程序返回:

uid: 1324
effective uid: 1324
msgctl(msqid=8028175, IPC_SET, ...) failed
(msg_perm.uid=1324,msg_perm.cuid=1324): Operation not permitted (errno=1)

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

int main(int iArgC, char ** ppszArgv)
{
printf("uid: %u\n", getuid());
printf("effective uid: %u\n", geteuid());

int msqid = msgget(
IPC_PRIVATE,
IPC_CREAT |
S_IRGRP | S_IWGRP |
S_IRUSR | S_IWUSR |
S_IROTH | S_IWOTH);

if (0 > msqid)
{
fprintf(stderr,
"msgget() failed.\n");
return EXIT_FAILURE;
}

{
struct msqid_ds ds = {0};

if (msgctl(
msqid,
IPC_STAT,
&ds))
{
fprintf(stderr,
"msgctl(msqid=%d, IPC_STAT, ...) failed: "
"%s (errno=%d)\n",
msqid,
strerror(errno),
errno);

return EXIT_FAILURE;
}

ds.msg_qbytes = 1024*1024;

if (msgctl(
msqid,
IPC_SET,
&ds))
{
fprintf(stderr,
"msgctl(msqid=%d, IPC_SET, ...) failed "
"(msg_perm.uid=%u,"
"msg_perm.cuid=%u): "
"%s (errno=%d)\n",
msqid,
ds.msg_perm.uid,
ds.msg_perm.cuid,
strerror(errno),
errno);
}

return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

/* EOF */

那么,有什么诀窍呢?

最佳答案

来自 man msgctl :

Appropriate privilege (Linux: the CAP_IPC_RESOURCE capability) is required to raise the msg_qbytes value beyond the system parameter MSGMNB.

在我的系统上,MSGNMB 是 16 kB,远低于您尝试设置的 1 MB。你检查过这个限制了吗? (执行 cat/proc/sys/kernel/msgmnb)

关于c - 设置消息队列大小 "not permitted",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10320705/

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