相关文章推荐
睡不着的豌豆  ·  node.js - Node build ...·  11 月前    · 
迷茫的皮带  ·  App crash on Android ...·  1 年前    · 
爱听歌的机器人  ·  TypeScript ...·  1 年前    · 

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) ;
        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) ;
        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) ;
            
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,

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 */
           struct msgbuf {
               long mtype;       /* message type, must be > 0 */
               char mtext[1];    /* message data */
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 ;... 
Tagged: denied, permission, permission denied, script argument parameter, ubuntu
Discussion started by drew77 and has been viewed 4,230 times.
There has been 5 replies and 4 user thanks in this discussion.
The last reply was by drew77.
"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
Tagged: linux
Discussion started by paramshamnani and has been viewed 5,305 times.
There has been 1 reply in this discussion.
The last reply was by blackrageous.
/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 [email protected] , it saysexport/home/suppotrmenu/script.sh:...
Tagged: shell scripts
Discussion started by lhareigh890 and has been viewed 4,206 times.
There has been 4 replies in this discussion.
The last reply was by alister.
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;
Tagged: message queue, mqueue.h, permission denied, programming
Discussion started by majid.merkava and has been viewed 11,229 times.
There has been 2 replies and 1 user thanks in this discussion.
The last reply was by majid.merkava.
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...
Tagged: shell scripts
Discussion started by niks_yv and has been viewed 10,462 times.
There has been 2 replies in this discussion.
The last reply was by methyl.
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...
Tagged: solaris
Discussion started by Sreerag446 and has been viewed 7,202 times.
There has been 3 replies in this discussion.
The last reply was by Sreerag446.
Tagged: beginners, permission, permission denied, shell script, shell scripting, unix commands, unix scripting, unix scripting basics
Discussion started by bbersani and has been viewed 25,524 times.
There has been 12 replies in this discussion.
The last reply was by brandedfundoo.
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....
Tagged: limit, posix, programming
Discussion started by mohit3884 and has been viewed 9,001 times.
There has been 0 replies in this discussion.
The last reply was by mohit3884.
$ . /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.
Tagged: advanced
Discussion started by big123456 and has been viewed 11,001 times.
There has been 1 reply in this discussion.
The last reply was by Cameron.
Tagged: beginners
Discussion started by trouscaillon and has been viewed 7,611 times.
There has been 8 replies in this discussion.
The last reply was by Neo.