Stubborn
Stubborn

Reputation: 968

Msgsnd in c: permission denied

I am trying to create a message queue and then send a message to it. Here is what I have tried:

int main(){
    int myMsgQueue;
    struct msgStruct{
        long mtype;
        char mtext[LENGTH];
    };
    struct msgStruct myMsg; 
    myMsg.mtype = (long)getpid();
    strcpy(myMsg.mtext,"Hey there"); //Setting the string of the message

    if((myMsgQueue = msgget(IPC_PRIVATE,IPC_CREAT | IPC_EXCL)) == -1) //Creating the message queue
        errore(__LINE__);
    if(msgsnd(myMsgQueue,&myMsg,sizeof(myMsg) - sizeof(long),0) == -1) //Sending the message
        errore(__LINE__);
    if(msgctl(myMsgQueue,IPC_RMID,0) == -1) //Deleting the message queue
        errore(__LINE__);
}

The function errore simply prints out a string which explains the error using strerror(errno).
However, the code does not seem to work: errore prints "Permission denied" as msgsnd returns -1.
I cannot figure out what is the problem: I am initializing the message queue and an adequate message structure, then creating a message of a type corresponding to the process' pid and of a text corresponding to "Hey there", then sending the message.
What am I missing?

Upvotes: 2

Views: 1029

Answers (1)

amin saffar
amin saffar

Reputation: 2033

Read the man page man page

A message queue identifier exists for the argument key, but operation permission as specified by the low-order 9 bits of msgflg would not be granted;

Upvotes: 1

Related Questions