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

标签 c linux permissions message-queue

我试图设置 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/

相关文章:

c - 内核崩溃后获取 Linux 内核调试信息

c - 原始线程是否执行由 `omp section` 指令定义的程序段之一?

linux - 如何在 Linux 中创建 MP3 的波形图像?

linux - 添加到 Apache 上可用的站点

Android USB枚举

c - "Tournament"的更好算法

c - 使 2 个循环并行运行

linux - 如何让 python 访问 ubuntu 中的系统范围模块?

写入文件时 Python CGI 脚本权限被拒绝

bash - chmod:仅当文件是目录时执行