OSDN Git Service

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