2016-01-29 177 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

你不止歡迎 –