2016-01-29 146 views
2

我在尝试使用mq_open()调用创建posix mq时遇到了权限问题。我确实纳入了这里提到的变化mq_open Permission denied我看过其他相关帖子,如https://groups.google.com/forum/#!topic/comp.unix.programmer/hnTZf6aPpbE,但也指向相同的东西。mq_open err no 13权限被拒绝

另外,当试图编译时,我遇到了错误,其中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; 
} 

回答

3

不能使用/tmp/servermq您的姓名......

报价人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. 

而且你很快就会发现这相关章节:

挂载消息队列文件系统
在Linux上,消息队列是在虚拟文件系统中创建的。使用下面的命令(其他 实现还可以提供这样的功能,但细节 可能不同。)该文件系统可以被安装(由超级用户) :

  # mkdir /dev/mqueue 
      # mount -t mqueue none /dev/mqueue 

     The sticky bit is automatically enabled on the mount directory. 
+0

感谢ü感谢ü ...花了3小时调试,并在网上寻找,而不是阅读男人:(......真的很感谢帮助!!! – oneday

+0

你不止欢迎 –