#include <sys/types.h>
|
#include <sys/ipc.h>
|
#include <sys/msg.h>
|
|
int
msgctl (int msqid, int cmd, struct msqid_ds *buf); |
Each message queue has a data structure associated with it, parts of which may be altered by msgctl and parts of which determine the actions of msgctl. The data structure is defined in <sys/msg.h> and contains (amongst others) the following members:
struct msqid_ds {
struct ipc_perm msg_perm; /* msg queue permission bits */
struct msg *msg_first; /* first message in the queue */
struct msg *msg_last; /* last message in the queue */
u_long msg_cbytes; /* number of bytes in use on the queue */
u_long msg_qnum; /* number of msgs in the queue */
u_long msg_qbytes; /* max # of bytes on the queue */
pid_t msg_lspid; /* pid of last msgsnd() */
pid_t msg_lrpid; /* pid of last msgrcv() */
time_t msg_stime; /* time of last msgsnd() */
long msg_pad1;
time_t msg_rtime; /* time of last msgrcv() */
long msg_pad2;
time_t msg_ctime; /* time of last msgctl() */
long msg_pad3;
long msg_pad4[4];
};
The ipc_perm structure used inside the shmid_ds structure is defined in <sys/ipc.h> and looks like this:
struct ipc_perm {
ushort cuid; /* creator user id */
ushort cgid; /* creator group id */
ushort uid; /* user id */
ushort gid; /* group id */
ushort mode; /* r/w permission */
ushort seq; /* sequence # (to generate unique msg/sem/shm id) */
key_t key; /* user specified msg/sem/shm key */
};
The operation to be performed by msgctl is specified in cmd and is one of:
| IPC_STAT | Gather information about the message queue and place it in the structure pointed to by buf. |
| IPC_SET | Set the value of the msg_perm.uid, msg_perm.gid, msg_perm.mode and msg_qbytes fields in the structure associated with msqid. The values are taken from the corresponding fields in the structure pointed to by buf. Values for msg_qbytes that exceed the system limit (MSGMNB from <sys/msg.h>) are silently truncated to that limit. |
| IPC_RMID | Remove the message queue specified by msqid and destroy the data associated with it. |
The permission to read from or write to a message queue (see msgsnd and msgrcv is determined by the msg_perm.mode field in the same way as is done with files (see chmod)
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define MESSAGE_Q_KEY 1000
int main(void)
{
int msq_id, len;
struct {
long mtype;
char mtext[128];
} msg_buf;
/*
* Create a message queue with a given key
*/
if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
printf("Message Q create failed with errno %d\n", errno);
return -1;
}
msg_buf.mtype = 1; /* message identifier */
strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
len = strlen(msg_buf.mtext)+1;
/*
* Put the message in the queue
*/
if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, 0) == -1) {
printf("Message Q send failed with errno %d\n", errno);
}
return 0;
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define MESSAGE_Q_KEY 1000
int main(void)
{
int msq_id;
int msg_len = 128;
int msg_type = 0; /* Any type of message */
struct {
long mtype;
char mtext[128];
} msg_buf;
/*
* Get the message queue id for the given key
*/
if ((msq_id = msgget(MESSAGE_Q_KEY, 0)) == -1) {
printf("Message Q get id failed with errno %d\n", errno);
return -1;
}
/*
* Get the message from the queue
*/
if (msgrcv(msq_id, (struct msgbuf *)&msg_buf, msg_len, msg_type, 0) == -1) {
printf("Message Q recv failed with errno %d\n", errno);
}
/*
* Remove the message queue
*/
if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
printf("Message Q delete failed with errno %d\n", errno);
return -1;
}
return 0;
}
| [EPERM] | |
|
The
cmd
argument
is equal to IPC_SET or IPC_RMID and the caller does
not have the permission to perform this operation
An attempt is made to increase the value of msg_qbytes through IPC_SET but the caller does not have the permission to perform this operation. |
|
| [EACCES] | |
| The command is IPC_STAT and the caller has no read permission for this message queue. | |
| [EINVAL] | |
|
The
msqid
argument
is not a valid message queue identifier.
cmd is not a valid command. |
|
|
© 2005-2007 Nokia |