内容简介
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>
int msgctl(int%uA0msqid,%uA0int%uA0cmd,%uA0struct msqid_ds *buf)
描述
struct msqid_ds { struct ipc_perm msg_perm /* Ownership and permissions time_t msg_stime /* Time of last msgsnd() */ time_t msg_rtime /* Time of last msgrcv() */ time_t msg_ctime /* Time of last change */ unsigned long __msg_cbytes /* Current number of bytes in queue (non-standard) */ msgqnum_t msg_qnum /* Current number of messages in queue */ msglen_t msg_qbytes /* Maximum number of bytes allowed in queue */ pid_t msg_lspid /* PID of last msgsnd() */ pid_t msg_lrpid /* PID of last msgrcv() */ }
The%uA0ipc_perm%uA0structure is defined in <sys/ipc.h> as follows (the highlighted fields are settable using%uA0IPC_SET):
struct ipc_perm { key_t key /* Key supplied to msgget() */ uid_t uid /* Effective UID of owner */ gid_t gid /* Effective GID of owner */ uid_t cuid /* Effective UID of creator */ gid_t cgid /* Effective GID of creator */ unsigned short mode /* Permissions */ unsigned short seq /* Sequence number */ }
Valid values for%uA0cmd%uA0are:
标签 | 描述 | |
---|---|---|
IPC_STAT | ||
%uA0 | Copy information from the kernel data structure associated withmsqid%uA0into the%uA0msqid_ds%uA0structure pointed to by%uA0buf. The caller must have read permission on the message queue. | |
IPC_SET | ||
%uA0 | Write the values of some members of the%uA0msqid_ds%uA0structure pointed to by%uA0buf%uA0to the kernel data structure associated with this message queue, updating also its%uA0msg_ctime%uA0member. The following members of the structure are updated:%uA0msg_qbytes,msg_perm.uid,%uA0msg_perm.gid, and (the least significant 9 bits of)msg_perm.mode. The effective UID of the calling process must match the owner (msg_perm.uid) or creator (msg_perm.cuid) of the message queue, or the caller must be privileged. Appropriate privilege (Linux: the%uA0CAP_IPC_RESOURCEcapability) is required to raise the%uA0msg_qbytes%uA0value beyond the system parameter%uA0MSGMNB. | |
IPC_RMID | ||
%uA0 | Immediately remove the message queue, awakening all waiting reader and writer processes (with an error return and%uA0errno%uA0set to%uA0EIDRM). The calling process must have appropriate privileges or its effective user ID must be either that of the creator or owner of the message queue. | |
IPC_INFO%uA0(Linux specific) | ||
%uA0 |
Returns information about system-wide message queue limits and parameters in the structure pointed to by%uA0buf. This structure is of type%uA0msginfo%uA0(thus, a cast is required), defined in<sys/msg.h>%uA0if the _GNU_SOURCE feature test macro is defined:
|
|
MSG_INFO%uA0(Linux specific) | ||
%uA0 | Returns a%uA0msginfo%uA0structure containing the same information as for%uA0IPC_INFO, except that the following fields are returned with information about system resources consumed by message queues: the%uA0msgpool%uA0field returns the number of message queues that currently exist on the system the%uA0msgmap%uA0field returns the total number of messages in all queues on the system and the%uA0msgtql%uA0field returns the total number of bytes in all messages in all queues on the system. | |
MSG_STAT%uA0(Linux specific) | ||
%uA0 | Returns a%uA0msqid_ds%uA0structure as for%uA0IPC_STAT. However, themsqid%uA0argument is not a queue identifier, but instead an index into the kernel’s internal array that maintains information about all message queues on the system. |
返回值
On success,%uA0IPC_STAT,%uA0IPC_SET, and%uA0IPC_RMID%uA0return 0. A successful%uA0IPC_INFO%uA0orMSG_INFO%uA0operation returns the index of the highest used entry in the kernel’s internal array recording information about all message queues. (This information can be used with repeated%uA0MSG_STAT%uA0operations to obtain information about all queues on the system.) A successful%uA0MSG_STAT%uA0operation returns the identifier of the queue whose index was given in%uA0msqid.On error, -1 is returned with%uA0errno%uA0indicating the error.
错误
On failure,%uA0errno%uA0is set to one of the following:标签 | 描述 |
---|---|
EACCES | The argument%uA0cmd%uA0is equal to%uA0IPC_STAT%uA0or%uA0MSG_STAT, but the calling process does not have read permission on the message queue%uA0msqid, and does not have the%uA0CAP_IPC_OWNERcapability. |
EFAULT | The argument%uA0cmd%uA0has the value%uA0IPC_SET%uA0or%uA0IPC_STAT, but the address pointed to by%uA0buf%uA0isn’t accessible. |
EIDRM | The message queue was removed. |
EINVAL | Invalid value for%uA0cmd%uA0or%uA0msqid. Or: for a%uA0MSG_STAT%uA0operation, the index value specified in%uA0msqid%uA0referred to an array slot that is currently unused. |
EPERM | The argument%uA0cmd%uA0has the value%uA0IPC_SET%uA0or%uA0IPC_RMID, but the effective user ID of the calling process is not the creator (as found in%uA0msg_perm.cuid) or the owner (as found inmsg_perm.uid) of the message queue, and the process is not privileged (Linux: it does not have the%uA0CAP_SYS_ADMINcapability). |
注意
The%uA0IPC_INFO,%uA0MSG_STAT%uA0and%uA0MSG_INFO%uA0operations are used by the%uA0ipcs(8) program to provide information on allocated resources. In the future these may modified or moved to a /proc file system interface.Various fields in the%uA0struct msqid_ds%uA0were shorts under Linux 2.2 and have become longs under Linux 2.4. To take advantage of this, a recompilation under glibc-2.1.91 or later should suffice. (The kernel distinguishes old and new calls by an IPC_64 flag incmd.)