]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/msgop.2
memusage.1, pldd.1, sprof.1, add_key.2, bind.2, bpf.2, clone.2, dup.2, epoll_ctl...
[thirdparty/man-pages.git] / man2 / msgop.2
1 .\" Copyright 1993 Giorgio Ciucci <giorgio@crcc.it>
2 .\" and Copyright 2015 Bill Pemberton <wfp5p@worldbroken.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 16:40:11 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified Mon Jul 10 21:09:59 2000 by aeb
28 .\" Modified 1 Jun 2002, Michael Kerrisk <mtk.manpages@gmail.com>
29 .\" Language clean-ups.
30 .\" Enhanced and corrected information on msg_qbytes, MSGMNB and MSGMAX
31 .\" Added note on restart behavior of msgsnd() and msgrcv()
32 .\" Formatting clean-ups (argument and field names marked as .I
33 .\" instead of .B)
34 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
35 .\" Added notes on capability requirements
36 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" Language and formatting clean-ups
38 .\" Added notes on /proc files
39 .\"
40 .TH MSGOP 2 2020-11-01 "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 .PP
49 .BI "int msgsnd(int " msqid ", const void *" msgp ", size_t " msgsz \
50 ", int " msgflg );
51 .PP
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 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 .PP
71 .in +4n
72 .EX
73 struct msgbuf {
74 long mtype; /* message type, must be > 0 */
75 char mtext[1]; /* message data */
76 };
77 .EE
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 \(bu 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 \(bu
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 .PP
143 A blocked
144 .BR msgsnd ()
145 call may also fail if:
146 .IP \(bu 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 \(bu
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 \(bu 2
169 .I msg_lspid
170 is set to the process ID of the calling process.
171 .IP \(bu
172 .I msg_qnum
173 is incremented by 1.
174 .IP \(bu
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 \(bu 2
221 If
222 .I msgtyp
223 is 0,
224 then the first message in the queue is read.
225 .IP \(bu
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 \(bu
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 .IP
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 .IP
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 \(bu 2
308 A message of the desired type is placed in the queue.
309 .IP \(bu
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 \(bu
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 in the user namespace that governs its IPC namespace.
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 in the user namespace that governs its IPC namespace.
418 .TP
419 .B EFAULT
420 The address pointed to by
421 .I msgp
422 isn't accessible.
423 .TP
424 .B EIDRM
425 While the process was sleeping to receive a message,
426 the message queue was removed.
427 .TP
428 .B EINTR
429 While the process was sleeping to receive a message,
430 the process caught a signal; see
431 .BR signal (7).
432 .TP
433 .B EINVAL
434 .I msqid
435 was invalid, or
436 .I msgsz
437 was less than 0.
438 .TP
439 .BR EINVAL " (since Linux 3.14)"
440 .I msgflg
441 specified
442 .BR MSG_COPY ,
443 but not
444 .BR IPC_NOWAIT .
445 .TP
446 .BR EINVAL " (since Linux 3.14)"
447 .I msgflg
448 specified both
449 .BR MSG_COPY
450 and
451 .BR MSG_EXCEPT .
452 .TP
453 .B ENOMSG
454 .B IPC_NOWAIT
455 was specified in
456 .I msgflg
457 and no message of the requested type existed on the message queue.
458 .TP
459 .B ENOMSG
460 .B IPC_NOWAIT
461 and
462 .B MSG_COPY
463 were specified in
464 .I msgflg
465 and the queue contains less than
466 .I msgtyp
467 messages.
468 .TP
469 .BR ENOSYS " (since Linux 3.8)"
470 Both
471 .B MSG_COPY
472 and
473 .B IPC_NOWAIT
474 were specified in
475 .IR msgflg ,
476 and this kernel was configured without
477 .BR CONFIG_CHECKPOINT_RESTORE .
478 .SH CONFORMING TO
479 POSIX.1-2001, POSIX.1-2008, SVr4.
480 .PP
481 The
482 .B MSG_EXCEPT
483 and
484 .B MSG_COPY
485 flags are Linux-specific;
486 their definitions can be obtained by defining the
487 .B _GNU_SOURCE
488 .\" MSG_COPY since glibc 2.18
489 feature test macro.
490 .SH NOTES
491 The inclusion of
492 .I <sys/types.h>
493 and
494 .I <sys/ipc.h>
495 isn't required on Linux or by any version of POSIX.
496 However,
497 some old implementations required the inclusion of these header files,
498 and the SVID also documented their inclusion.
499 Applications intended to be portable to such old systems may need
500 to include these header files.
501 .\" Like Linux, the FreeBSD man pages still document
502 .\" the inclusion of these header files.
503 .PP
504 The
505 .I msgp
506 argument is declared as \fIstruct msgbuf\ *\fP in
507 glibc 2.0 and 2.1.
508 It is declared as \fIvoid\ *\fP
509 in glibc 2.2 and later, as required by SUSv2 and SUSv3.
510 .PP
511 The following limits on message queue resources affect the
512 .BR msgsnd ()
513 call:
514 .TP
515 .B MSGMAX
516 Maximum size of a message text, in bytes (default value: 8192 bytes).
517 On Linux, this limit can be read and modified via
518 .IR /proc/sys/kernel/msgmax .
519 .TP
520 .B MSGMNB
521 Maximum number of bytes that can be held in a message queue
522 (default value: 16384 bytes).
523 On Linux, this limit can be read and modified via
524 .IR /proc/sys/kernel/msgmnb .
525 A privileged process
526 (Linux: a process with the
527 .B CAP_SYS_RESOURCE
528 capability)
529 can increase the size of a message queue beyond
530 .B MSGMNB
531 using the
532 .BR msgctl (2)
533 .B IPC_SET
534 operation.
535 .PP
536 The implementation has no intrinsic system-wide limits on the
537 number of message headers
538 .RB ( MSGTQL )
539 and the number of bytes in the message pool
540 .RB ( MSGPOOL ).
541 .SH BUGS
542 In Linux 3.13 and earlier,
543 if
544 .BR msgrcv ()
545 was called with the
546 .BR MSG_COPY
547 flag, but without
548 .BR IPC_NOWAIT ,
549 and the message queue contained less than
550 .I msgtyp
551 messages, then the call would block until the next message is written
552 to the queue.
553 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
554 At that point, the call would return a copy of the message,
555 .I regardless
556 of whether that message was at the ordinal position
557 .IR msgtyp .
558 This bug is fixed
559 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
560 in Linux 3.14.
561 .PP
562 Specifying both
563 .B MSG_COPY
564 and
565 .B MSC_EXCEPT
566 in
567 .I msgflg
568 is a logical error (since these flags impose different interpretations on
569 .IR msgtyp ).
570 In Linux 3.13 and earlier,
571 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
572 this error was not diagnosed by
573 .BR msgrcv ().
574 This bug is fixed
575 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
576 in Linux 3.14.
577 .SH EXAMPLES
578 The program below demonstrates the use of
579 .BR msgsnd ()
580 and
581 .BR msgrcv ().
582 .PP
583 The example program is first run with the \fB\-s\fP option to send a
584 message and then run again with the \fB\-r\fP option to receive a
585 message.
586 .PP
587 The following shell session shows a sample run of the program:
588 .PP
589 .in +4n
590 .EX
591 .RB "$" " ./a.out \-s"
592 sent: a message at Wed Mar 4 16:25:45 2015
593
594 .RB "$" " ./a.out \-r"
595 message received: a message at Wed Mar 4 16:25:45 2015
596 .EE
597 .in
598 .SS Program source
599 \&
600 .EX
601 #include <stdio.h>
602 #include <stdlib.h>
603 #include <string.h>
604 #include <time.h>
605 #include <unistd.h>
606 #include <errno.h>
607 #include <sys/types.h>
608 #include <sys/ipc.h>
609 #include <sys/msg.h>
610
611 struct msgbuf {
612 long mtype;
613 char mtext[80];
614 };
615
616 static void
617 usage(char *prog_name, char *msg)
618 {
619 if (msg != NULL)
620 fputs(msg, stderr);
621
622 fprintf(stderr, "Usage: %s [options]\en", prog_name);
623 fprintf(stderr, "Options are:\en");
624 fprintf(stderr, "\-s send message using msgsnd()\en");
625 fprintf(stderr, "\-r read message using msgrcv()\en");
626 fprintf(stderr, "\-t message type (default is 1)\en");
627 fprintf(stderr, "\-k message queue key (default is 1234)\en");
628 exit(EXIT_FAILURE);
629 }
630
631 static void
632 send_msg(int qid, int msgtype)
633 {
634 struct msgbuf msg;
635 time_t t;
636
637 msg.mtype = msgtype;
638
639 time(&t);
640 snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
641 ctime(&t));
642
643 if (msgsnd(qid, &msg, sizeof(msg.mtext),
644 IPC_NOWAIT) == \-1) {
645 perror("msgsnd error");
646 exit(EXIT_FAILURE);
647 }
648 printf("sent: %s\en", msg.mtext);
649 }
650
651 static void
652 get_msg(int qid, int msgtype)
653 {
654 struct msgbuf msg;
655
656 if (msgrcv(qid, &msg, sizeof(msg.mtext), msgtype,
657 MSG_NOERROR | IPC_NOWAIT) == \-1) {
658 if (errno != ENOMSG) {
659 perror("msgrcv");
660 exit(EXIT_FAILURE);
661 }
662 printf("No message available for msgrcv()\en");
663 } else
664 printf("message received: %s\en", msg.mtext);
665 }
666
667 int
668 main(int argc, char *argv[])
669 {
670 int qid, opt;
671 int mode = 0; /* 1 = send, 2 = receive */
672 int msgtype = 1;
673 int msgkey = 1234;
674
675 while ((opt = getopt(argc, argv, "srt:k:")) != \-1) {
676 switch (opt) {
677 case \(aqs\(aq:
678 mode = 1;
679 break;
680 case \(aqr\(aq:
681 mode = 2;
682 break;
683 case \(aqt\(aq:
684 msgtype = atoi(optarg);
685 if (msgtype <= 0)
686 usage(argv[0], "\-t option must be greater than 0\en");
687 break;
688 case \(aqk\(aq:
689 msgkey = atoi(optarg);
690 break;
691 default:
692 usage(argv[0], "Unrecognized option\en");
693 }
694 }
695
696 if (mode == 0)
697 usage(argv[0], "must use either \-s or \-r option\en");
698
699 qid = msgget(msgkey, IPC_CREAT | 0666);
700
701 if (qid == \-1) {
702 perror("msgget");
703 exit(EXIT_FAILURE);
704 }
705
706 if (mode == 2)
707 get_msg(qid, msgtype);
708 else
709 send_msg(qid, msgtype);
710
711 exit(EXIT_SUCCESS);
712 }
713 .EE
714 .SH SEE ALSO
715 .BR msgctl (2),
716 .BR msgget (2),
717 .BR capabilities (7),
718 .BR mq_overview (7),
719 .BR sysvipc (7)