open.2: Make it clearer that an FD is an index into the process's FD table
[man-pages.git] / man2 / msgctl.2
blobd42a47d2bef478c94dbc0bf102017c8e5dfe7012
1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright 2004, 2005 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified Sun Feb 18 01:59:29 2001 by Andries E. Brouwer <aeb@cwi.nl>
28 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
29 .\"     Added notes on CAP_IPC_OWNER requirement
30 .\" Modified, 17 Jun 2004, Michael Kerrisk <mtk.manpages@gmail.com>
31 .\"     Added notes on CAP_SYS_ADMIN requirement for IPC_SET and IPC_RMID
32 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
33 .\"     Language and formatting clean-ups
34 .\"     Added msqid_ds and ipc_perm structure definitions
35 .\" 2005-08-02, mtk: Added IPC_INFO, MSG_INFO, MSG_STAT descriptions
36 .\" 2018-03-20, dbueso: Added MSG_STAT_ANY description.
37 .\"
38 .TH MSGCTL 2 2021-03-22 "Linux" "Linux Programmer's Manual"
39 .SH NAME
40 msgctl \- System V message control operations
41 .SH SYNOPSIS
42 .nf
43 .B #include <sys/ipc.h>
44 .B #include <sys/msg.h>
45 .PP
46 .BI "int msgctl(int " msqid ", int " cmd ", struct msqid_ds *" buf );
47 .fi
48 .SH DESCRIPTION
49 .BR msgctl ()
50 performs the control operation specified by
51 .I cmd
52 on the System\ V message queue with identifier
53 .IR msqid .
54 .PP
55 The
56 .I msqid_ds
57 data structure is defined in \fI<sys/msg.h>\fP as follows:
58 .PP
59 .in +4n
60 .EX
61 struct msqid_ds {
62     struct ipc_perm msg_perm;   /* Ownership and permissions */
63     time_t          msg_stime;  /* Time of last msgsnd(2) */
64     time_t          msg_rtime;  /* Time of last msgrcv(2) */
65     time_t          msg_ctime;  /* Time of creation or last
66                                    modification by msgctl() */
67     unsigned long   msg_cbytes; /* # of bytes in queue */
68     msgqnum_t       msg_qnum;   /* # number of messages in queue */
69     msglen_t        msg_qbytes; /* Maximum # of bytes in queue */
70     pid_t           msg_lspid;  /* PID of last msgsnd(2) */
71     pid_t           msg_lrpid;  /* PID of last msgrcv(2) */
73 .EE
74 .in
75 .PP
76 The fields of the
77 .I msgid_ds
78 structure are as follows:
79 .TP 11
80 .I msg_perm
81 This is an
82 .I ipc_perm
83 structure (see below) that specifies the access permissions on the message
84 queue.
85 .TP
86 .I msg_stime
87 Time of the last
88 .BR msgsnd (2)
89 system call.
90 .TP
91 .I msg_rtime
92 Time of the last
93 .BR msgrcv (2)
94 system call.
95 .TP
96 .I msg_ctime
97 Time of creation of queue or time of last
98 .BR msgctl ()
99 .BR IPC_SET
100 operation.
102 .I msg_cbytes
103 Number of bytes in all messages currently on the message queue.
104 This is a nonstandard Linux extension that is not specified in POSIX.
106 .I msg_qnum
107 Number of messages currently on the message queue.
109 .I msg_qbytes
110 Maximum number of bytes of message text allowed on the message
111 queue.
113 .I msg_lspid
114 ID of the process that performed the last
115 .BR msgsnd (2)
116 system call.
118 .I msg_lrpid
119 ID of the process that performed the last
120 .BR msgrcv (2)
121 system call.
124 .I ipc_perm
125 structure is defined as follows
126 (the highlighted fields are settable using
127 .BR IPC_SET ):
129 .in +4n
131 struct ipc_perm {
132     key_t          __key;       /* Key supplied to msgget(2) */
133     uid_t          \fBuid\fP;         /* Effective UID of owner */
134     gid_t          \fBgid\fP;         /* Effective GID of owner */
135     uid_t          cuid;        /* Effective UID of creator */
136     gid_t          cgid;        /* Effective GID of creator */
137     unsigned short \fBmode\fP;        /* Permissions */
138     unsigned short __seq;       /* Sequence number */
143 The least significant 9 bits of the
144 .I mode
145 field of the
146 .I ipc_perm
147 structure define the access permissions for the message queue.
148 The permission bits are as follows:
150 l l.
151 0400    Read by user
152 0200    Write by user
153 0040    Read by group
154 0020    Write by group
155 0004    Read by others
156 0002    Write by others
159 Bits 0100, 0010, and 0001 (the execute bits) are unused by the system.
161 Valid values for
162 .I cmd
163 are:
165 .B IPC_STAT
166 Copy information from the kernel data structure associated with
167 .I msqid
168 into the
169 .I msqid_ds
170 structure pointed to by
171 .IR buf .
172 The caller must have read permission on the message queue.
174 .B IPC_SET
175 Write the values of some members of the
176 .I msqid_ds
177 structure pointed to by
178 .I buf
179 to the kernel data structure associated with this message queue,
180 updating also its
181 .I msg_ctime
182 member.
184 The following members of the structure are updated:
185 .IR msg_qbytes ,
186 .IR msg_perm.uid ,
187 .IR msg_perm.gid ,
188 and (the least significant 9 bits of)
189 .IR msg_perm.mode .
191 The effective UID of the calling process must match the owner
192 .RI ( msg_perm.uid )
193 or creator
194 .RI ( msg_perm.cuid )
195 of the message queue, or the caller must be privileged.
196 Appropriate privilege (Linux: the
197 .B CAP_SYS_RESOURCE
198 capability) is required to raise the
199 .I msg_qbytes
200 value beyond the system parameter
201 .BR MSGMNB .
203 .B IPC_RMID
204 Immediately remove the message queue,
205 awakening all waiting reader and writer processes (with an error
206 return and
207 .I errno
208 set to
209 .BR EIDRM ).
210 The calling process must have appropriate privileges
211 or its effective user ID must be either that of the creator or owner
212 of the message queue.
213 The third argument to
214 .BR msgctl ()
215 is ignored in this case.
217 .BR IPC_INFO " (Linux-specific)"
218 Return information about system-wide message queue limits and
219 parameters in the structure pointed to by
220 .IR buf .
221 This structure is of type
222 .I msginfo
223 (thus, a cast is required),
224 defined in
225 .I <sys/msg.h>
226 if the
227 .B _GNU_SOURCE
228 feature test macro is defined:
230 .in +4n
232 struct msginfo {
233     int msgpool; /* Size in kibibytes of buffer pool
234                     used to hold message data;
235                     unused within kernel */
236     int msgmap;  /* Maximum number of entries in message
237                     map; unused within kernel */
238     int msgmax;  /* Maximum number of bytes that can be
239                     written in a single message */
240     int msgmnb;  /* Maximum number of bytes that can be
241                     written to queue; used to initialize
242                     msg_qbytes during queue creation
243                     (msgget(2)) */
244     int msgmni;  /* Maximum number of message queues */
245     int msgssz;  /* Message segment size;
246                     unused within kernel */
247     int msgtql;  /* Maximum number of messages on all queues
248                     in system; unused within kernel */
249     unsigned short msgseg;
250                  /* Maximum number of segments;
251                     unused within kernel */
257 .IR msgmni ,
258 .IR msgmax ,
260 .I msgmnb
261 settings can be changed via
262 .I /proc
263 files of the same name; see
264 .BR proc (5)
265 for details.
267 .BR MSG_INFO " (Linux-specific)"
268 Return a
269 .I msginfo
270 structure containing the same information as for
271 .BR IPC_INFO ,
272 except that the following fields are returned with information
273 about system resources consumed by message queues: the
274 .I msgpool
275 field returns the number of message queues that currently exist
276 on the system; the
277 .I msgmap
278 field returns the total number of messages in all queues
279 on the system; and the
280 .I msgtql
281 field returns the total number of bytes in all messages
282 in all queues on the system.
284 .BR MSG_STAT " (Linux-specific)"
285 Return a
286 .I msqid_ds
287 structure as for
288 .BR IPC_STAT .
289 However, the
290 .I msqid
291 argument is not a queue identifier, but instead an index into
292 the kernel's internal array that maintains information about
293 all message queues on the system.
295 .BR MSG_STAT_ANY " (Linux-specific, since Linux 4.17)"
296 Return a
297 .I msqid_ds
298 structure as for
299 .BR MSG_STAT .
300 However,
301 .I msg_perm.mode
302 is not checked for read access for
303 .IR msqid
304 meaning that any user can employ this operation (just as any user may read
305 .IR /proc/sysvipc/msg
306 to obtain the same information).
307 .SH RETURN VALUE
308 On success,
309 .BR IPC_STAT ,
310 .BR IPC_SET ,
312 .B IPC_RMID
313 return 0.
314 A successful
315 .B IPC_INFO
317 .B MSG_INFO
318 operation returns the index of the highest used entry in the
319 kernel's internal array recording information about all
320 message queues.
321 (This information can be used with repeated
322 .B MSG_STAT
324 .B MSG_STAT_ANY
325 operations to obtain information about all queues on the system.)
326 A successful
327 .B MSG_STAT
329 .B MSG_STAT_ANY
330 operation returns the identifier of the queue whose index was given in
331 .IR msqid .
333 On failure, \-1 is returned and
334 .I errno
335 is set to indicate the error.
336 .SH ERRORS
338 .B EACCES
339 The argument
340 .I cmd
341 is equal to
342 .B IPC_STAT
344 .BR MSG_STAT ,
345 but the calling process does not have read permission on the message queue
346 .IR msqid ,
347 and does not have the
348 .B CAP_IPC_OWNER
349 capability in the user namespace that governs its IPC namespace.
351 .B EFAULT
352 The argument
353 .I cmd
354 has the value
355 .B IPC_SET
357 .BR IPC_STAT ,
358 but the address pointed to by
359 .I buf
360 isn't accessible.
362 .B EIDRM
363 The message queue was removed.
365 .B EINVAL
366 Invalid value for
367 .I cmd
369 .IR msqid .
370 Or: for a
371 .B MSG_STAT
372 operation, the index value specified in
373 .I msqid
374 referred to an array slot that is currently unused.
376 .B EPERM
377 The argument
378 .I cmd
379 has the value
380 .B IPC_SET
382 .BR IPC_RMID ,
383 but the effective user ID of the calling process is not the creator
384 (as found in
385 .IR msg_perm.cuid )
386 or the owner
387 (as found in
388 .IR msg_perm.uid )
389 of the message queue,
390 and the caller is not privileged (Linux: does not have the
391 .B CAP_SYS_ADMIN
392 capability).
394 .B EPERM
395 An attempt
396 .RB ( IPC_SET )
397 was made to increase
398 .I msg_qbytes
399 beyond the system parameter
400 .BR MSGMNB ,
401 but the caller is not privileged (Linux: does not have the
402 .B CAP_SYS_RESOURCE
403 capability).
404 .SH CONFORMING TO
405 POSIX.1-2001, POSIX.1-2008, SVr4.
406 .\" SVID does not document the EIDRM error condition.
407 .SH NOTES
408 The inclusion of
409 .I <sys/ipc.h>
410 isn't required on Linux or by any version of POSIX.
411 However,
412 some old implementations required the inclusion of this header file,
413 and the SVID also documented its inclusion.
414 Applications intended to be portable to such old systems may need
415 to include this header file.
416 .\" Like Linux, the FreeBSD man pages still document
417 .\" the inclusion of this header file.
420 .BR IPC_INFO ,
421 .BR MSG_STAT ,
423 .B MSG_INFO
424 operations are used by the
425 .BR ipcs (1)
426 program to provide information on allocated resources.
427 In the future these may modified or moved to a
428 .I /proc
429 filesystem interface.
431 Various fields in the \fIstruct msqid_ds\fP were
432 typed as
433 .I short
434 under Linux 2.2
435 and have become
436 .I long
437 under Linux 2.4.
438 To take advantage of this,
439 a recompilation under glibc-2.1.91 or later should suffice.
440 (The kernel distinguishes old and new calls by an
441 .B IPC_64
442 flag in
443 .IR cmd .)
444 .SH SEE ALSO
445 .BR msgget (2),
446 .BR msgrcv (2),
447 .BR msgsnd (2),
448 .BR capabilities (7),
449 .BR mq_overview (7),
450 .BR sysvipc (7)