【问题标题】:Setting size of message queue "not permitted"设置消息队列的大小“不允许”
【发布时间】:2012-05-06 10:26:43
【问题描述】:

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

msgctl() 手册页指出:

IPC_SET 只能由具有适当权限的进程执行 ,其有效用户 ID 等于 msqid_ds 数据结构中的 msg_perm.cuid 或 msg_perm.uid 与 msqid 关联。只有具有适当权限的进程才能 提高 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 */

那么,有什么诀窍呢?

【问题讨论】:

    标签: c linux permissions message-queue


    【解决方案1】:

    来自man msgctl

    需要适当的权限(Linux:CAP_IPC_RESOURCE 功能)才能将 msg_qbytes 值提高到系统参数 MSGMNB 之外。

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

    【讨论】:

    • cat /proc/sys/kernel/msgmnb 给我 65536,msg_qbytes 中的 msgctl(..., IPC_STAT, ...) 也返回的值是多少。已经看到这个我仍然不确定effective user ID [is] equal to the value of msg_perm.cuid or msg_perm.uid in the msqid_ds data structure associated with msqid. 是否是appropriate privileges,因为我引用的手册页中的**or**。 @jpalecek
    • @alk:不。“适当的权限”是 CAP_IPC_RESOURCE 功能。您引用的内容基本上意味着您只能操作队列中的msg_qbytes,但只能在 0-64 kB 范围内。
    猜你喜欢
    • 2017-04-09
    • 2016-03-04
    • 2013-07-13
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多