我试图设置 POSIX 消息队列的大小,但似乎不允许。
msgctl()
手册页指出:
IPC_SET 只能由具有适当权限的进程执行, 或者其有效用户 ID 等于与 msqid 关联的 msqid_ds 数据结构中的 msg_perm.cuid 或 msg_perm.uid 的值。只有具有适当权限的进程才能提高 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 */
那么,诀窍是什么?