1MSGCTL(2) Linux Programmer's Manual MSGCTL(2)
2
3
4
6 msgctl - System V message control operations
7
9 #include <sys/types.h>
10 #include <sys/ipc.h>
11 #include <sys/msg.h>
12
13 int msgctl(int msqid, int cmd, struct msqid_ds *buf);
14
16 msgctl() performs the control operation specified by cmd on the Sys‐
17 tem V message queue with identifier msqid.
18
19 The msqid_ds data structure is defined in <sys/msg.h> as follows:
20
21 struct msqid_ds {
22 struct ipc_perm msg_perm; /* Ownership and permissions */
23 time_t msg_stime; /* Time of last msgsnd(2) */
24 time_t msg_rtime; /* Time of last msgrcv(2) */
25 time_t msg_ctime; /* Time of last change */
26 unsigned long __msg_cbytes; /* Current number of bytes in
27 queue (nonstandard) */
28 msgqnum_t msg_qnum; /* Current number of messages
29 in queue */
30 msglen_t msg_qbytes; /* Maximum number of bytes
31 allowed in queue */
32 pid_t msg_lspid; /* PID of last msgsnd(2) */
33 pid_t msg_lrpid; /* PID of last msgrcv(2) */
34 };
35
36 The ipc_perm structure is defined as follows (the highlighted fields
37 are settable using IPC_SET):
38
39 struct ipc_perm {
40 key_t __key; /* Key supplied to msgget(2) */
41 uid_t uid; /* Effective UID of owner */
42 gid_t gid; /* Effective GID of owner */
43 uid_t cuid; /* Effective UID of creator */
44 gid_t cgid; /* Effective GID of creator */
45 unsigned short mode; /* Permissions */
46 unsigned short __seq; /* Sequence number */
47 };
48
49 Valid values for cmd are:
50
51 IPC_STAT
52 Copy information from the kernel data structure associated with
53 msqid into the msqid_ds structure pointed to by buf. The caller
54 must have read permission on the message queue.
55
56 IPC_SET
57 Write the values of some members of the msqid_ds structure
58 pointed to by buf to the kernel data structure associated with
59 this message queue, updating also its msg_ctime member. The
60 following members of the structure are updated: msg_qbytes,
61 msg_perm.uid, msg_perm.gid, and (the least significant 9 bits
62 of) msg_perm.mode. The effective UID of the calling process
63 must match the owner (msg_perm.uid) or creator (msg_perm.cuid)
64 of the message queue, or the caller must be privileged. Appro‐
65 priate privilege (Linux: the CAP_SYS_RESOURCE capability) is
66 required to raise the msg_qbytes value beyond the system parame‐
67 ter MSGMNB.
68
69 IPC_RMID
70 Immediately remove the message queue, awakening all waiting
71 reader and writer processes (with an error return and errno set
72 to EIDRM). The calling process must have appropriate privileges
73 or its effective user ID must be either that of the creator or
74 owner of the message queue. The third argument to msgctl() is
75 ignored in this case.
76
77 IPC_INFO (Linux-specific)
78 Return information about system-wide message queue limits and
79 parameters in the structure pointed to by buf. This structure
80 is of type msginfo (thus, a cast is required), defined in
81 <sys/msg.h> if the _GNU_SOURCE feature test macro is defined:
82
83 struct msginfo {
84 int msgpool; /* Size in kibibytes of buffer pool
85 used to hold message data;
86 unused within kernel */
87 int msgmap; /* Maximum number of entries in message
88 map; unused within kernel */
89 int msgmax; /* Maximum number of bytes that can be
90 written in a single message */
91 int msgmnb; /* Maximum number of bytes that can be
92 written to queue; used to initialize
93 msg_qbytes during queue creation
94 (msgget(2)) */
95 int msgmni; /* Maximum number of message queues */
96 int msgssz; /* Message segment size;
97 unused within kernel */
98 int msgtql; /* Maximum number of messages on all queues
99 in system; unused within kernel */
100 unsigned short int msgseg;
101 /* Maximum number of segments;
102 unused within kernel */
103 };
104
105 The msgmni, msgmax, and msgmnb settings can be changed via /proc
106 files of the same name; see proc(5) for details.
107
108 MSG_INFO (Linux-specific)
109 Return a msginfo structure containing the same information as
110 for IPC_INFO, except that the following fields are returned with
111 information about system resources consumed by message queues:
112 the msgpool field returns the number of message queues that cur‐
113 rently exist on the system; the msgmap field returns the total
114 number of messages in all queues on the system; and the msgtql
115 field returns the total number of bytes in all messages in all
116 queues on the system.
117
118 MSG_STAT (Linux-specific)
119 Return a msqid_ds structure as for IPC_STAT. However, the msqid
120 argument is not a queue identifier, but instead an index into
121 the kernel's internal array that maintains information about all
122 message queues on the system.
123
125 On success, IPC_STAT, IPC_SET, and IPC_RMID return 0. A successful
126 IPC_INFO or MSG_INFO operation returns the index of the highest used
127 entry in the kernel's internal array recording information about all
128 message queues. (This information can be used with repeated MSG_STAT
129 operations to obtain information about all queues on the system.) A
130 successful MSG_STAT operation returns the identifier of the queue whose
131 index was given in msqid.
132
133 On error, -1 is returned with errno indicating the error.
134
136 On failure, errno is set to one of the following:
137
138 EACCES The argument cmd is equal to IPC_STAT or MSG_STAT, but the call‐
139 ing process does not have read permission on the message queue
140 msqid, and does not have the CAP_IPC_OWNER capability in the
141 user namespace that governs its IPC namespace.
142
143 EFAULT The argument cmd has the value IPC_SET or IPC_STAT, but the
144 address pointed to by buf isn't accessible.
145
146 EIDRM The message queue was removed.
147
148 EINVAL Invalid value for cmd or msqid. Or: for a MSG_STAT operation,
149 the index value specified in msqid referred to an array slot
150 that is currently unused.
151
152 EPERM The argument cmd has the value IPC_SET or IPC_RMID, but the
153 effective user ID of the calling process is not the creator (as
154 found in msg_perm.cuid) or the owner (as found in msg_perm.uid)
155 of the message queue, and the caller is not privileged (Linux:
156 does not have the CAP_SYS_ADMIN capability).
157
158 EPERM An attempt (IPC_SET) was made to increase msg_qbytes beyond the
159 system parameter MSGMNB, but the caller is not privileged
160 (Linux: does not have the CAP_SYS_RESOURCE capability).
161
163 POSIX.1-2001, POSIX.1-2008, SVr4.
164
166 The inclusion of <sys/types.h> and <sys/ipc.h> isn't required on Linux
167 or by any version of POSIX. However, some old implementations required
168 the inclusion of these header files, and the SVID also documented their
169 inclusion. Applications intended to be portable to such old systems
170 may need to include these header files.
171
172 The IPC_INFO, MSG_STAT and MSG_INFO operations are used by the ipcs(1)
173 program to provide information on allocated resources. In the future
174 these may modified or moved to a /proc filesystem interface.
175
176 Various fields in the struct msqid_ds were typed as short under Linux
177 2.2 and have become long under Linux 2.4. To take advantage of this, a
178 recompilation under glibc-2.1.91 or later should suffice. (The kernel
179 distinguishes old and new calls by an IPC_64 flag in cmd.)
180
182 msgget(2), msgrcv(2), msgsnd(2), capabilities(7), mq_overview(7),
183 svipc(7)
184
186 This page is part of release 4.16 of the Linux man-pages project. A
187 description of the project, information about reporting bugs, and the
188 latest version of this page, can be found at
189 https://www.kernel.org/doc/man-pages/.
190
191
192
193Linux 2017-09-15 MSGCTL(2)