OSDN Git Service

b7ee05bc970726c5d16e2da46fb7c0a24d5e0101
[linuxjm/LDP_man-pages.git] / original / man2 / msgop.2
1 .\" Copyright 1993 Giorgio Ciucci <giorgio@crcc.it>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified Tue Oct 22 16:40:11 1996 by Eric S. Raymond <esr@thyrsus.com>
26 .\" Modified Mon Jul 10 21:09:59 2000 by aeb
27 .\" Modified 1 Jun 2002, Michael Kerrisk <mtk.manpages@gmail.com>
28 .\"     Language clean-ups.
29 .\"     Enhanced and corrected information on msg_qbytes, MSGMNB and MSGMAX
30 .\"     Added note on restart behavior of msgsnd() and msgrcv()
31 .\"     Formatting clean-ups (argument and field names marked as .I
32 .\"             instead of .B)
33 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\"     Added notes on capability requirements
35 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
36 .\"     Language and formatting clean-ups
37 .\"     Added notes on /proc files
38 .\" FIXME . Add example programs to this page.
39 .\"
40 .TH MSGOP 2 2014-05-16 "Linux" "Linux Programmer's Manual"
41 .SH NAME
42 msgrcv, msgsnd \- System V message queue operations
43 .SH SYNOPSIS
44 .nf
45 .B #include <sys/types.h>
46 .B #include <sys/ipc.h>
47 .B #include <sys/msg.h>
48 .sp
49 .BI "int msgsnd(int " msqid ", const void *" msgp ", size_t " msgsz \
50 ", int " msgflg );
51 .sp
52 .BI "ssize_t msgrcv(int " msqid ", void *" msgp ", size_t " msgsz \
53 ", long " msgtyp ,
54 .BI "               int " msgflg );
55 .fi
56 .SH DESCRIPTION
57 The
58 .BR msgsnd ()
59 and
60 .BR msgrcv ()
61 system calls are used, respectively, to send messages to,
62 and receive messages from, a System\ V message queue.
63 The calling process must have write permission on the message queue
64 in order to send a message, and read permission to receive a message.
65 .PP
66 The
67 .I msgp
68 argument is a pointer to a caller-defined structure
69 of the following general form:
70 .in +4n
71 .nf
72
73 struct msgbuf {
74     long mtype;       /* message type, must be > 0 */
75     char mtext[1];    /* message data */
76 };
77 .fi
78 .in
79 .PP
80 The
81 .I mtext
82 field is an array (or other structure) whose size is specified by
83 .IR msgsz ,
84 a nonnegative integer value.
85 Messages of zero length (i.e., no
86 .I mtext
87 field) are permitted.
88 The
89 .I mtype
90 field must have a strictly positive integer value.
91 This value can be
92 used by the receiving process for message selection
93 (see the description of
94 .BR msgrcv ()
95 below).
96 .SS msgsnd()
97 The
98 .BR msgsnd ()
99 system call appends a copy of the message pointed to by
100 .I msgp
101 to the message queue whose identifier is specified
102 by
103 .IR msqid .
104 .PP
105 If sufficient space is available in the queue,
106 .BR msgsnd ()
107 succeeds immediately.
108 The queue capacity is governed by the
109 .I msg_qbytes
110 field in the associated data structure for the message queue.
111 During queue creation this field is initialized to
112 .B MSGMNB
113 bytes, but this limit can be modified using
114 .BR msgctl (2).
115 A message queue is considered to be full if either of the following
116 conditions is true:
117 .IP * 2
118 Adding a new message to the queue would cause the total number of bytes
119 in the queue to exceed the queue's maximum size (the
120 .I msg_qbytes
121 field).
122 .IP *
123 Adding another message to the queue would cause the total number of messages
124 in the queue to exceed the queue's maximum size (the
125 .I msg_qbytes
126 field).
127 This check is necessary to prevent an unlimited number of zero-length
128 messages being placed on the queue.
129 Although such messages contain no data,
130 they nevertheless consume (locked) kernel memory.
131 .PP
132 If insufficient space is available in the queue, then the default
133 behavior of
134 .BR msgsnd ()
135 is to block until space becomes available.
136 If
137 .B IPC_NOWAIT
138 is specified in
139 .IR msgflg ,
140 then the call instead fails with the error
141 .BR EAGAIN .
142
143 A blocked
144 .BR msgsnd ()
145 call may also fail if:
146 .IP * 2
147 the queue is removed,
148 in which case the system call fails with
149 .I errno
150 set to
151 .BR EIDRM ;
152 or
153 .IP *
154 a signal is caught, in which case the system call fails
155 with
156 .I errno
157 set to
158 .BR EINTR ; see
159 .BR signal (7).
160 .RB ( msgsnd ()
161 is never automatically restarted after being interrupted by a
162 signal handler, regardless of the setting of the
163 .B SA_RESTART
164 flag when establishing a signal handler.)
165 .PP
166 Upon successful completion the message queue data structure is updated
167 as follows:
168 .IP
169 .I msg_lspid
170 is set to the process ID of the calling process.
171 .IP
172 .I msg_qnum
173 is incremented by 1.
174 .IP
175 .I msg_stime
176 is set to the current time.
177 .SS msgrcv()
178 The
179 .BR msgrcv ()
180 system call removes a message from the queue specified by
181 .I msqid
182 and places it in the buffer
183 pointed to by
184 .IR msgp .
185 .PP
186 The argument
187 .I msgsz
188 specifies the maximum size in bytes for the member
189 .I mtext
190 of the structure pointed to by the
191 .I msgp
192 argument.
193 If the message text has length greater than
194 .IR msgsz ,
195 then the behavior depends on whether
196 .B MSG_NOERROR
197 is specified in
198 .IR msgflg .
199 If
200 .B MSG_NOERROR
201 is specified, then
202 the message text will be truncated (and the truncated part will be
203 lost); if
204 .B MSG_NOERROR
205 is not specified, then
206 the message isn't removed from the queue and
207 the system call fails returning \-1 with
208 .I errno
209 set to
210 .BR E2BIG .
211 .PP
212 Unless
213 .B MSG_COPY
214 is specified in
215 .IR msgflg
216 (see below),
217 the
218 .I msgtyp
219 argument specifies the type of message requested, as follows:
220 .IP * 2
221 If
222 .I msgtyp
223 is 0,
224 then the first message in the queue is read.
225 .IP *
226 If
227 .I msgtyp
228 is greater than 0,
229 then the first message in the queue of type
230 .I msgtyp
231 is read, unless
232 .B MSG_EXCEPT
233 was specified in
234 .IR msgflg ,
235 in which case
236 the first message in the queue of type not equal to
237 .I msgtyp
238 will be read.
239 .IP *
240 If
241 .I msgtyp
242 is less than 0,
243 then the first message in the queue with the lowest type less than or
244 equal to the absolute value of
245 .I msgtyp
246 will be read.
247 .PP
248 The
249 .I msgflg
250 argument is a bit mask constructed by ORing together zero or more
251 of the following flags:
252 .TP
253 .B IPC_NOWAIT
254 Return immediately if no message of the requested type is in the queue.
255 The system call fails with
256 .I errno
257 set to
258 .BR ENOMSG .
259 .TP
260 .BR MSG_COPY " (since Linux 3.8)"
261 .\" commit 4a674f34ba04a002244edaf891b5da7fc1473ae8
262 Nondestructively fetch a copy of the message at the ordinal position
263 in the queue specified by
264 .I msgtyp
265 (messages are considered to be numbered starting at 0).
266
267 This flag must be specified in conjunction with
268 .BR IPC_NOWAIT ,
269 with the result that, if there is no message available at the given position,
270 the call fails immediately with the error
271 .BR ENOMSG .
272 Because they alter the meaning of
273 .I msgtyp
274 in orthogonal ways,
275 .BR MSG_COPY
276 and
277 .BR MSG_EXCEPT
278 may not both be specified in
279 .IR msgflg .
280
281 The
282 .BR MSG_COPY
283 flag was added for the implementation of
284 the kernel checkpoint-restore facility and
285 is available only if the kernel was built with the
286 .B CONFIG_CHECKPOINT_RESTORE
287 option.
288 .TP
289 .B MSG_EXCEPT
290 Used with
291 .I msgtyp
292 greater than 0
293 to read the first message in the queue with message type that differs
294 from
295 .IR msgtyp .
296 .TP
297 .B MSG_NOERROR
298 To truncate the message text if longer than
299 .I msgsz
300 bytes.
301 .PP
302 If no message of the requested type is available and
303 .B IPC_NOWAIT
304 isn't specified in
305 .IR msgflg ,
306 the calling process is blocked until one of the following conditions occurs:
307 .IP * 2
308 A message of the desired type is placed in the queue.
309 .IP *
310 The message queue is removed from the system.
311 In this case, the system call fails with
312 .I errno
313 set to
314 .BR EIDRM .
315 .IP *
316 The calling process catches a signal.
317 In this case, the system call fails with
318 .I errno
319 set to
320 .BR EINTR .
321 .RB ( msgrcv ()
322 is never automatically restarted after being interrupted by a
323 signal handler, regardless of the setting of the
324 .B SA_RESTART
325 flag when establishing a signal handler.)
326 .PP
327 Upon successful completion the message queue data structure is updated
328 as follows:
329 .IP
330 .I msg_lrpid
331 is set to the process ID of the calling process.
332 .IP
333 .I msg_qnum
334 is decremented by 1.
335 .IP
336 .I msg_rtime
337 is set to the current time.
338 .SH RETURN VALUE
339 On failure both functions return \-1
340 with
341 .I errno
342 indicating the error,
343 otherwise
344 .BR msgsnd ()
345 returns 0
346 and
347 .BR msgrcv ()
348 returns the number of bytes actually copied into the
349 .I mtext
350 array.
351 .SH ERRORS
352 When
353 .BR msgsnd ()
354 fails,
355 .I errno
356 will be set to one among the following values:
357 .TP
358 .B EACCES
359 The calling process does not have write permission on the message queue,
360 and does not have the
361 .B CAP_IPC_OWNER
362 capability.
363 .TP
364 .B EAGAIN
365 The message can't be sent due to the
366 .I msg_qbytes
367 limit for the queue and
368 .B IPC_NOWAIT
369 was specified in
370 .IR msgflg .
371 .TP
372 .B EFAULT
373 The address pointed to by
374 .I msgp
375 isn't accessible.
376 .TP
377 .B EIDRM
378 The message queue was removed.
379 .TP
380 .B EINTR
381 Sleeping on a full message queue condition, the process caught a signal.
382 .TP
383 .B EINVAL
384 Invalid
385 .I msqid
386 value, or nonpositive
387 .I mtype
388 value, or
389 invalid
390 .I msgsz
391 value (less than 0 or greater than the system value
392 .BR MSGMAX ).
393 .TP
394 .B ENOMEM
395 The system does not have enough memory to make a copy of the
396 message pointed to by
397 .IR msgp .
398 .PP
399 When
400 .BR msgrcv ()
401 fails,
402 .I errno
403 will be set to one among the following values:
404 .TP
405 .B E2BIG
406 The message text length is greater than
407 .I msgsz
408 and
409 .B MSG_NOERROR
410 isn't specified in
411 .IR msgflg .
412 .TP
413 .B EACCES
414 The calling process does not have read permission on the message queue,
415 and does not have the
416 .B CAP_IPC_OWNER
417 capability.
418 .TP
419 .B EAGAIN
420 No message was available in the queue and
421 .B IPC_NOWAIT
422 was specified in
423 .IR msgflg .
424 .TP
425 .B EFAULT
426 The address pointed to by
427 .I msgp
428 isn't accessible.
429 .TP
430 .B EIDRM
431 While the process was sleeping to receive a message,
432 the message queue was removed.
433 .TP
434 .B EINTR
435 While the process was sleeping to receive a message,
436 the process caught a signal; see
437 .BR signal (7).
438 .TP
439 .B EINVAL
440 .I msgqid
441 was invalid, or
442 .I msgsz
443 was less than 0.
444 .TP
445 .BR EINVAL " (since Linux 3.14)"
446 .I msgflg
447 specified
448 .BR MSG_COPY ,
449 but not
450 .BR IPC_NOWAIT .
451 .TP
452 .BR EINVAL " (since Linux 3.14)"
453 .I msgflg
454 specified both
455 .BR MSG_COPY
456 and
457 .BR MSG_EXCEPT .
458 .TP
459 .B ENOMSG
460 .B IPC_NOWAIT
461 was specified in
462 .I msgflg
463 and no message of the requested type existed on the message queue.
464 .TP
465 .B ENOMSG
466 .B IPC_NOWAIT
467 and
468 .B MSG_COPY
469 were specified in
470 .I msgflg
471 and the queue contains less than
472 .I msgtyp
473 messages.
474 .TP
475 .BR ENOSYS " (since Linux 3.8)"
476 .I MSG_COPY
477 was specified in
478 .IR msgflg ,
479 and this kernel was configured without
480 .BR CONFIG_CHECKPOINT_RESTORE .
481 .SH CONFORMING TO
482 SVr4, POSIX.1-2001.
483
484 The
485 .B MSG_EXCEPT
486 and
487 .B MSG_COPY
488 flags are Linux-specific;
489 their definitions can be obtained by defining the
490 .B _GNU_SOURCE
491 .\" MSG_COPY since glibc 2.18
492 feature test macro.
493 .SH NOTES
494 The inclusion of
495 .I <sys/types.h>
496 and
497 .I <sys/ipc.h>
498 isn't required on Linux or by any version of POSIX.
499 However,
500 some old implementations required the inclusion of these header files,
501 and the SVID also documented their inclusion.
502 Applications intended to be portable to such old systems may need
503 to include these header files.
504 .\" Like Linux, the FreeBSD man pages still document
505 .\" the inclusion of these header files.
506
507 The
508 .I msgp
509 argument is declared as \fIstruct msgbuf\ *\fP with
510 libc4, libc5, glibc 2.0, glibc 2.1.
511 It is declared as \fIvoid\ *\fP
512 with glibc 2.2 and later, as required by SUSv2 and SUSv3.
513
514 The following limits on message queue resources affect the
515 .BR msgsnd ()
516 call:
517 .TP
518 .B MSGMAX
519 Maximum size for a message text: 8192 bytes
520 (on Linux, this limit can be read and modified via
521 .IR /proc/sys/kernel/msgmax ).
522 .TP
523 .B MSGMNB
524 Default maximum size in bytes of a message queue: 16384 bytes
525 (on Linux, this limit can be read and modified via
526 .IR /proc/sys/kernel/msgmnb ).
527 A privileged process
528 (Linux: a process with the
529 .B CAP_SYS_RESOURCE
530 capability)
531 can increase the size of a message queue beyond
532 .B MSGMNB
533 by a
534 .BR msgctl (2)
535 system call.
536 .PP
537 The implementation has no intrinsic system-wide limits on the
538 number of message headers
539 .RB ( MSGTQL )
540 and the number of bytes in the message pool
541 .RB ( MSGPOOL ).
542 .SH BUGS
543 In Linux 3.13 and earlier,
544 if
545 .BR msgrcv ()
546 was called with the
547 .BR MSG_COPY
548 flag, but without
549 .BR IPC_NOWAIT ,
550 and the message queue contained less than
551 .I msgtyp
552 messages, then the call would block until the next message is written
553 to the queue.
554 .\" FIXME http://marc.info/?l=linux-kernel&m=139048542803605&w=2
555 At that point, the call would return a copy of the message,
556 .I regardless
557 of whether that message was at the ordinal position
558 .IR msgtyp .
559 This bug is fixed
560 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
561 in Linux 3.14.
562
563 Specifying both
564 .B MSG_COPY
565 and
566 .B MSC_EXCEPT
567 in
568 .I msgflg
569 is a logical error (since these flags impose different interpretations on
570 .IR msgtyp ).
571 In Linux 3.13 and earlier,
572 .\" FIXME http://marc.info/?l=linux-kernel&m=139048542803605&w=2
573 this error was not diagnosed by
574 .BR msgrcv ().
575 This bug is fixed
576 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
577 in Linux 3.14.
578 .SH SEE ALSO
579 .BR msgctl (2),
580 .BR msgget (2),
581 .BR capabilities (7),
582 .BR mq_overview (7),
583 .BR svipc (7)
584 .SH COLOPHON
585 This page is part of release 3.67 of the Linux
586 .I man-pages
587 project.
588 A description of the project,
589 information about reporting bugs,
590 and the latest version of this page,
591 can be found at
592 \%http://www.kernel.org/doc/man\-pages/.