msgget message queue always get permission denied


 
Thread Tools Search this Thread
Top Forums Programming msgget message queue always get permission denied
# 1  
Old 09-07-2011
msgget message queue always get permission denied

I want to use msgget() to obtain a message queue between two processes, here is my code:
the first one create the mq, the second one open it and add a message to it. But when I execute the second one, I get permission denied. I've already desperately tried everything I can think of to solve this problem. I even manually change the mode of the mq. But all I get is still permission denied. Somebody please help me.....Smilie
Code:
struct mymsg
{
        long mtype ;
        char data[1000] ;
};

int main()
{
        int msqid ;
        int* nod ;
        struct mymsg msg ;
        struct msqid_ds msgds ;
        key_t key ;
        key = ftok("/home/tefino/Documents/APUE_exercise/IPC/ipc",1) ;
        perror(NULL) ;
        msqid = msgget(key,O_RDWR|IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) ;
        perror(NULL) ;
        msgctl(msqid, IPC_STAT, &msgds) ;
        msgds.msg_perm.mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH ;
        msgctl(msqid, IPC_SET, &msgds) ;
        msgrcv(fd,&msg, 1000, 0,  0) ;
        perror(NULL) ;
        printf("%s\n", msg.data) ;
}

Code:
struct mymsg
{
        long mtype ;
        char data[1000] ;
};

int main()
{
        int msqid ;
        key_t key ;
        struct mymsg msg ;
        key = ftok("/home/tefino/Documents/APUE_exercise/IPC/ipc",1) ;
        gets(msg.data) ;
        msqid = msgget(key, O_WRONLY) ;
        perror(NULL) ;
        struct msqid_ds mds ;
        msgctl(msqid, IPC_STAT, &mds) ;
        mds.msg_perm.mode = S_IWUSR|S_IRUSR ;
        msgctl(msqid, IPC_SET, &mds) ;


        msgsnd(msqid, &msg, 1000, 0) ;
        perror(NULL) ;
}

# 2  
Old 09-07-2011
There are many problems with your code. So let's start with the beginning and move towards a working solution.

First, you should always check the returned code from a system call, It it fails, prints error (e.g. using perror()) and immediately exits. This gives you the opportunity to fix right away the problem instead of continuing with some error condition.

This is for instance the case in your first program with ftok(). If this calls fails, you continue creating a queue with Id -1 (0xffffffff) . As quoted in the man page:
Quote:
The ftok() function uses the identity of the file named by the given pathname (which must refer to an existing, accessible file) and the least significant 8 bits of proj_id [...]
Make sure that you fixed the points mentioned above. If you still have problems, we shall work them on.

Greets,
/Lew
# 3  
Old 09-07-2011
You need to start checking return values. You may not have been noticing that your second program wasn't even opening the queue at all...

The sender program should open it with O_RDWR as well.

You don't need to use the torturous individual flags for permissions when you create the queue. Just ... | 0666 suffices.

---------- Post updated at 09:36 AM ---------- Previous update was at 09:26 AM ----------

Once it's opened, you get 'invalid argument' when sending, because:

Code:
// from man page
           struct msgbuf {
               long mtype;       /* message type, must be > 0 */
               char mtext[1];    /* message data */
           };


Last edited by Corona688; 09-07-2011 at 12:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Permission denied

Trying to get date into the txt file. It says Permission denied. echo $(date +%I:%M:%S_%D) >> /tmp/systemd_suspend_test_err.txt exec 2>> /tmp/systemd_suspend_test_err.txt if ; then # Do the thing you want before suspend here echo "we are suspending $(date +%I:%M:%S_%D)." elif ;... (5 Replies)
Discussion started by: drew77
5 Replies

2. Linux

Permission denied

I am using korn shell When I type in Telnet on cmd line, I get message "cannot execute" How can I get permission to execute command ? In which dir is telnet located ? I looked in /usr/bin dir. but its not there Thanks (1 Reply)
Discussion started by: paramshamnani
1 Replies

3. Shell Programming and Scripting

Permission denied

I created a user so that when he logs in he will be directed to a menu /etc/passwd user1:x:115:1:Support -SysAd:/export/home/user1:/export/home/suppotrmenu/script.sh However when I logged in remotely from another server by ssh user1@1.1.1.1 , it saysexport/home/suppotrmenu/script.sh:... (4 Replies)
Discussion started by: lhareigh890
4 Replies

4. Programming

Permission denied when creating message queue

Hi guys. i have wrote a simple program to test message queue attributes. here it is: #include <stdio.h> #include <stdlib.h> #include <mqueue.h> #include <fcntl.h> #include <string.h> #include <errno.h> #include <sys/stat.h> int main() { struct mq_attr attr; mqd_t mqd; ... (2 Replies)
Discussion started by: majid.merkava
2 Replies

5. Shell Programming and Scripting

Executing expect script giving message as bad interpreter: Permission denied

Hi Gurus, I am new to scripting and needs your help in expect script used for telnet. I wrote a simple script as #!/usr/bin/expect-5.43 -f spawn telnet localhost 2233 expect "password:" send "secret\r" send "i data.cnbc.com\r" send "exit\r" expect eof When I am trying to execute... (2 Replies)
Discussion started by: niks_yv
2 Replies

6. Solaris

Permission denied message for parent directory

Hi All, I have an issue that's eating my head for few days. I would appreciate if anyone could help me out in this to resolve this. In Solaris 8 container I am facing the below issue. As oracle user when I do ls -l in /dboracle mountpoint getting permission denied error messages. $ ls... (3 Replies)
Discussion started by: Sreerag446
3 Replies

7. UNIX for Dummies Questions & Answers

Why do I keep getting .:Permission denied?

I'll start off by saying that I know very little about Unix - however, I do know that I have a .profile file in my home directory, and that I should be able to invoke it by typing . profile. However, when I do this for ANY .filename, I get ".: Permission denied". I'm pretty sure that there is... (12 Replies)
Discussion started by: bbersani
12 Replies

8. Programming

How to limit max no of message in a posix message queue

Hii can anyone pls tell how to limit the max no of message in a posix message queue. I have made changes in proc/sys/fs/mqueue/msg_max But still whenever i try to read the value of max. message in the queue using attr.mq_curmsgs (where struct mq_attr attr) its giving the default value as 10.... (0 Replies)
Discussion started by: mohit3884
0 Replies

9. UNIX for Advanced & Expert Users

Permission denied

Hi, I can not execute a .env file $ . /Data/oracle/d03/mydbora/8.0.6/MYDB.env -bash: /Data/oracle/d03/mydbora/8.0.6/MYDB.env: Permission denied Even if : -rwxrwxrwx 1 oracle dba 2903 Mar 5 2007 /Data/oracle/d03/mydbora/8.0.6/MYDB.env Please help. Many thanks. (1 Reply)
Discussion started by: big123456
1 Replies

10. UNIX for Dummies Questions & Answers

./ Permission Denied.

Could someone tell me why I am getting a permission denied message when I attempt to run this on an out file? Thanks! (8 Replies)
Discussion started by: trouscaillon
8 Replies
Login or Register to Ask a Question