]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/msgop.2
Many pages: Fix style issues reported by `make lint-groff`
[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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" Modified Tue Oct 22 16:40:11 1996 by Eric S. Raymond <esr@thyrsus.com>
7 .\" Modified Mon Jul 10 21:09:59 2000 by aeb
8 .\" Modified 1 Jun 2002, Michael Kerrisk <mtk.manpages@gmail.com>
9 .\" Language clean-ups.
10 .\" Enhanced and corrected information on msg_qbytes, MSGMNB and MSGMAX
11 .\" Added note on restart behavior of msgsnd() and msgrcv()
12 .\" Formatting clean-ups (argument and field names marked as .I
13 .\" instead of .B)
14 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
15 .\" Added notes on capability requirements
16 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
17 .\" Language and formatting clean-ups
18 .\" Added notes on /proc files
19 .\"
20 .TH MSGOP 2 2021-03-22 "Linux" "Linux Programmer's Manual"
21 .SH NAME
22 msgrcv, msgsnd \- System V message queue operations
23 .SH LIBRARY
24 Standard C library
25 .RI ( libc ", " \-lc )
26 .SH SYNOPSIS
27 .nf
28 .B #include <sys/msg.h>
29 .PP
30 .BI "int msgsnd(int " msqid ", const void *" msgp ", size_t " msgsz \
31 ", int " msgflg );
32 .PP
33 .BI "ssize_t msgrcv(int " msqid ", void *" msgp ", size_t " msgsz \
34 ", long " msgtyp ,
35 .BI " int " msgflg );
36 .fi
37 .SH DESCRIPTION
38 The
39 .BR msgsnd ()
40 and
41 .BR msgrcv ()
42 system calls are used to send messages to,
43 and receive messages from, a System\ V message queue.
44 The calling process must have write permission on the message queue
45 in order to send a message, and read permission to receive a message.
46 .PP
47 The
48 .I msgp
49 argument is a pointer to a caller-defined structure
50 of the following general form:
51 .PP
52 .in +4n
53 .EX
54 struct msgbuf {
55 long mtype; /* message type, must be > 0 */
56 char mtext[1]; /* message data */
57 };
58 .EE
59 .in
60 .PP
61 The
62 .I mtext
63 field is an array (or other structure) whose size is specified by
64 .IR msgsz ,
65 a nonnegative integer value.
66 Messages of zero length (i.e., no
67 .I mtext
68 field) are permitted.
69 The
70 .I mtype
71 field must have a strictly positive integer value.
72 This value can be
73 used by the receiving process for message selection
74 (see the description of
75 .BR msgrcv ()
76 below).
77 .SS msgsnd()
78 The
79 .BR msgsnd ()
80 system call appends a copy of the message pointed to by
81 .I msgp
82 to the message queue whose identifier is specified
83 by
84 .IR msqid .
85 .PP
86 If sufficient space is available in the queue,
87 .BR msgsnd ()
88 succeeds immediately.
89 The queue capacity is governed by the
90 .I msg_qbytes
91 field in the associated data structure for the message queue.
92 During queue creation this field is initialized to
93 .B MSGMNB
94 bytes, but this limit can be modified using
95 .BR msgctl (2).
96 A message queue is considered to be full if either of the following
97 conditions is true:
98 .IP \(bu 2
99 Adding a new message to the queue would cause the total number of bytes
100 in the queue to exceed the queue's maximum size (the
101 .I msg_qbytes
102 field).
103 .IP \(bu
104 Adding another message to the queue would cause the total number of messages
105 in the queue to exceed the queue's maximum size (the
106 .I msg_qbytes
107 field).
108 This check is necessary to prevent an unlimited number of zero-length
109 messages being placed on the queue.
110 Although such messages contain no data,
111 they nevertheless consume (locked) kernel memory.
112 .PP
113 If insufficient space is available in the queue, then the default
114 behavior of
115 .BR msgsnd ()
116 is to block until space becomes available.
117 If
118 .B IPC_NOWAIT
119 is specified in
120 .IR msgflg ,
121 then the call instead fails with the error
122 .BR EAGAIN .
123 .PP
124 A blocked
125 .BR msgsnd ()
126 call may also fail if:
127 .IP \(bu 2
128 the queue is removed,
129 in which case the system call fails with
130 .I errno
131 set to
132 .BR EIDRM ;
133 or
134 .IP \(bu
135 a signal is caught, in which case the system call fails
136 with
137 .I errno
138 set to
139 .BR EINTR ; see
140 .BR signal (7).
141 .RB ( msgsnd ()
142 is never automatically restarted after being interrupted by a
143 signal handler, regardless of the setting of the
144 .B SA_RESTART
145 flag when establishing a signal handler.)
146 .PP
147 Upon successful completion the message queue data structure is updated
148 as follows:
149 .IP \(bu 2
150 .I msg_lspid
151 is set to the process ID of the calling process.
152 .IP \(bu
153 .I msg_qnum
154 is incremented by 1.
155 .IP \(bu
156 .I msg_stime
157 is set to the current time.
158 .SS msgrcv()
159 The
160 .BR msgrcv ()
161 system call removes a message from the queue specified by
162 .I msqid
163 and places it in the buffer
164 pointed to by
165 .IR msgp .
166 .PP
167 The argument
168 .I msgsz
169 specifies the maximum size in bytes for the member
170 .I mtext
171 of the structure pointed to by the
172 .I msgp
173 argument.
174 If the message text has length greater than
175 .IR msgsz ,
176 then the behavior depends on whether
177 .B MSG_NOERROR
178 is specified in
179 .IR msgflg .
180 If
181 .B MSG_NOERROR
182 is specified, then
183 the message text will be truncated (and the truncated part will be
184 lost); if
185 .B MSG_NOERROR
186 is not specified, then
187 the message isn't removed from the queue and
188 the system call fails returning \-1 with
189 .I errno
190 set to
191 .BR E2BIG .
192 .PP
193 Unless
194 .B MSG_COPY
195 is specified in
196 .I msgflg
197 (see below),
198 the
199 .I msgtyp
200 argument specifies the type of message requested, as follows:
201 .IP \(bu 2
202 If
203 .I msgtyp
204 is 0,
205 then the first message in the queue is read.
206 .IP \(bu
207 If
208 .I msgtyp
209 is greater than 0,
210 then the first message in the queue of type
211 .I msgtyp
212 is read, unless
213 .B MSG_EXCEPT
214 was specified in
215 .IR msgflg ,
216 in which case
217 the first message in the queue of type not equal to
218 .I msgtyp
219 will be read.
220 .IP \(bu
221 If
222 .I msgtyp
223 is less than 0,
224 then the first message in the queue with the lowest type less than or
225 equal to the absolute value of
226 .I msgtyp
227 will be read.
228 .PP
229 The
230 .I msgflg
231 argument is a bit mask constructed by ORing together zero or more
232 of the following flags:
233 .TP
234 .B IPC_NOWAIT
235 Return immediately if no message of the requested type is in the queue.
236 The system call fails with
237 .I errno
238 set to
239 .BR ENOMSG .
240 .TP
241 .BR MSG_COPY " (since Linux 3.8)"
242 .\" commit 4a674f34ba04a002244edaf891b5da7fc1473ae8
243 Nondestructively fetch a copy of the message at the ordinal position
244 in the queue specified by
245 .I msgtyp
246 (messages are considered to be numbered starting at 0).
247 .IP
248 This flag must be specified in conjunction with
249 .BR IPC_NOWAIT ,
250 with the result that, if there is no message available at the given position,
251 the call fails immediately with the error
252 .BR ENOMSG .
253 Because they alter the meaning of
254 .I msgtyp
255 in orthogonal ways,
256 .B MSG_COPY
257 and
258 .B MSG_EXCEPT
259 may not both be specified in
260 .IR msgflg .
261 .IP
262 The
263 .B MSG_COPY
264 flag was added for the implementation of
265 the kernel checkpoint-restore facility and
266 is available only if the kernel was built with the
267 .B CONFIG_CHECKPOINT_RESTORE
268 option.
269 .TP
270 .B MSG_EXCEPT
271 Used with
272 .I msgtyp
273 greater than 0
274 to read the first message in the queue with message type that differs
275 from
276 .IR msgtyp .
277 .TP
278 .B MSG_NOERROR
279 To truncate the message text if longer than
280 .I msgsz
281 bytes.
282 .PP
283 If no message of the requested type is available and
284 .B IPC_NOWAIT
285 isn't specified in
286 .IR msgflg ,
287 the calling process is blocked until one of the following conditions occurs:
288 .IP \(bu 2
289 A message of the desired type is placed in the queue.
290 .IP \(bu
291 The message queue is removed from the system.
292 In this case, the system call fails with
293 .I errno
294 set to
295 .BR EIDRM .
296 .IP \(bu
297 The calling process catches a signal.
298 In this case, the system call fails with
299 .I errno
300 set to
301 .BR EINTR .
302 .RB ( msgrcv ()
303 is never automatically restarted after being interrupted by a
304 signal handler, regardless of the setting of the
305 .B SA_RESTART
306 flag when establishing a signal handler.)
307 .PP
308 Upon successful completion the message queue data structure is updated
309 as follows:
310 .IP
311 .I msg_lrpid
312 is set to the process ID of the calling process.
313 .IP
314 .I msg_qnum
315 is decremented by 1.
316 .IP
317 .I msg_rtime
318 is set to the current time.
319 .SH RETURN VALUE
320 On success,
321 .BR msgsnd ()
322 returns 0
323 and
324 .BR msgrcv ()
325 returns the number of bytes actually copied into the
326 .I mtext
327 array.
328 On failure, both functions return \-1, and set
329 .I errno
330 to indicate the error.
331 .SH ERRORS
332 .BR msgsnd ()
333 can fail with the following errors:
334 .TP
335 .B EACCES
336 The calling process does not have write permission on the message queue,
337 and does not have the
338 .B CAP_IPC_OWNER
339 capability in the user namespace that governs its IPC namespace.
340 .TP
341 .B EAGAIN
342 The message can't be sent due to the
343 .I msg_qbytes
344 limit for the queue and
345 .B IPC_NOWAIT
346 was specified in
347 .IR msgflg .
348 .TP
349 .B EFAULT
350 The address pointed to by
351 .I msgp
352 isn't accessible.
353 .TP
354 .B EIDRM
355 The message queue was removed.
356 .TP
357 .B EINTR
358 Sleeping on a full message queue condition, the process caught a signal.
359 .TP
360 .B EINVAL
361 Invalid
362 .I msqid
363 value, or nonpositive
364 .I mtype
365 value, or
366 invalid
367 .I msgsz
368 value (less than 0 or greater than the system value
369 .BR MSGMAX ).
370 .TP
371 .B ENOMEM
372 The system does not have enough memory to make a copy of the
373 message pointed to by
374 .IR msgp .
375 .PP
376 .BR msgrcv ()
377 can fail with the following errors:
378 .TP
379 .B E2BIG
380 The message text length is greater than
381 .I msgsz
382 and
383 .B MSG_NOERROR
384 isn't specified in
385 .IR msgflg .
386 .TP
387 .B EACCES
388 The calling process does not have read permission on the message queue,
389 and does not have the
390 .B CAP_IPC_OWNER
391 capability in the user namespace that governs its IPC namespace.
392 .TP
393 .B EFAULT
394 The address pointed to by
395 .I msgp
396 isn't accessible.
397 .TP
398 .B EIDRM
399 While the process was sleeping to receive a message,
400 the message queue was removed.
401 .TP
402 .B EINTR
403 While the process was sleeping to receive a message,
404 the process caught a signal; see
405 .BR signal (7).
406 .TP
407 .B EINVAL
408 .I msqid
409 was invalid, or
410 .I msgsz
411 was less than 0.
412 .TP
413 .BR EINVAL " (since Linux 3.14)"
414 .I msgflg
415 specified
416 .BR MSG_COPY ,
417 but not
418 .BR IPC_NOWAIT .
419 .TP
420 .BR EINVAL " (since Linux 3.14)"
421 .I msgflg
422 specified both
423 .B MSG_COPY
424 and
425 .BR MSG_EXCEPT .
426 .TP
427 .B ENOMSG
428 .B IPC_NOWAIT
429 was specified in
430 .I msgflg
431 and no message of the requested type existed on the message queue.
432 .TP
433 .B ENOMSG
434 .B IPC_NOWAIT
435 and
436 .B MSG_COPY
437 were specified in
438 .I msgflg
439 and the queue contains less than
440 .I msgtyp
441 messages.
442 .TP
443 .BR ENOSYS " (since Linux 3.8)"
444 Both
445 .B MSG_COPY
446 and
447 .B IPC_NOWAIT
448 were specified in
449 .IR msgflg ,
450 and this kernel was configured without
451 .BR CONFIG_CHECKPOINT_RESTORE .
452 .SH CONFORMING TO
453 POSIX.1-2001, POSIX.1-2008, SVr4.
454 .PP
455 The
456 .B MSG_EXCEPT
457 and
458 .B MSG_COPY
459 flags are Linux-specific;
460 their definitions can be obtained by defining the
461 .B _GNU_SOURCE
462 .\" MSG_COPY since glibc 2.18
463 feature test macro.
464 .SH NOTES
465 The
466 .I msgp
467 argument is declared as \fIstruct msgbuf\ *\fP in
468 glibc 2.0 and 2.1.
469 It is declared as \fIvoid\ *\fP
470 in glibc 2.2 and later, as required by SUSv2 and SUSv3.
471 .PP
472 The following limits on message queue resources affect the
473 .BR msgsnd ()
474 call:
475 .TP
476 .B MSGMAX
477 Maximum size of a message text, in bytes (default value: 8192 bytes).
478 On Linux, this limit can be read and modified via
479 .IR /proc/sys/kernel/msgmax .
480 .TP
481 .B MSGMNB
482 Maximum number of bytes that can be held in a message queue
483 (default value: 16384 bytes).
484 On Linux, this limit can be read and modified via
485 .IR /proc/sys/kernel/msgmnb .
486 A privileged process
487 (Linux: a process with the
488 .B CAP_SYS_RESOURCE
489 capability)
490 can increase the size of a message queue beyond
491 .B MSGMNB
492 using the
493 .BR msgctl (2)
494 .B IPC_SET
495 operation.
496 .PP
497 The implementation has no intrinsic system-wide limits on the
498 number of message headers
499 .RB ( MSGTQL )
500 and the number of bytes in the message pool
501 .RB ( MSGPOOL ).
502 .SH BUGS
503 In Linux 3.13 and earlier,
504 if
505 .BR msgrcv ()
506 was called with the
507 .B MSG_COPY
508 flag, but without
509 .BR IPC_NOWAIT ,
510 and the message queue contained less than
511 .I msgtyp
512 messages, then the call would block until the next message is written
513 to the queue.
514 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
515 At that point, the call would return a copy of the message,
516 .I regardless
517 of whether that message was at the ordinal position
518 .IR msgtyp .
519 This bug is fixed
520 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
521 in Linux 3.14.
522 .PP
523 Specifying both
524 .B MSG_COPY
525 and
526 .B MSC_EXCEPT
527 in
528 .I msgflg
529 is a logical error (since these flags impose different interpretations on
530 .IR msgtyp ).
531 In Linux 3.13 and earlier,
532 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
533 this error was not diagnosed by
534 .BR msgrcv ().
535 This bug is fixed
536 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
537 in Linux 3.14.
538 .SH EXAMPLES
539 The program below demonstrates the use of
540 .BR msgsnd ()
541 and
542 .BR msgrcv ().
543 .PP
544 The example program is first run with the \fB\-s\fP option to send a
545 message and then run again with the \fB\-r\fP option to receive a
546 message.
547 .PP
548 The following shell session shows a sample run of the program:
549 .PP
550 .in +4n
551 .EX
552 .RB "$" " ./a.out \-s"
553 sent: a message at Wed Mar 4 16:25:45 2015
554
555 .RB "$" " ./a.out \-r"
556 message received: a message at Wed Mar 4 16:25:45 2015
557 .EE
558 .in
559 .SS Program source
560 \&
561 .EX
562 #include <stdio.h>
563 #include <stdlib.h>
564 #include <string.h>
565 #include <time.h>
566 #include <unistd.h>
567 #include <errno.h>
568 #include <sys/types.h>
569 #include <sys/ipc.h>
570 #include <sys/msg.h>
571
572 struct msgbuf {
573 long mtype;
574 char mtext[80];
575 };
576
577 static void
578 usage(char *prog_name, char *msg)
579 {
580 if (msg != NULL)
581 fputs(msg, stderr);
582
583 fprintf(stderr, "Usage: %s [options]\en", prog_name);
584 fprintf(stderr, "Options are:\en");
585 fprintf(stderr, "\-s send message using msgsnd()\en");
586 fprintf(stderr, "\-r read message using msgrcv()\en");
587 fprintf(stderr, "\-t message type (default is 1)\en");
588 fprintf(stderr, "\-k message queue key (default is 1234)\en");
589 exit(EXIT_FAILURE);
590 }
591
592 static void
593 send_msg(int qid, int msgtype)
594 {
595 struct msgbuf msg;
596 time_t t;
597
598 msg.mtype = msgtype;
599
600 time(&t);
601 snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
602 ctime(&t));
603
604 if (msgsnd(qid, &msg, sizeof(msg.mtext),
605 IPC_NOWAIT) == \-1) {
606 perror("msgsnd error");
607 exit(EXIT_FAILURE);
608 }
609 printf("sent: %s\en", msg.mtext);
610 }
611
612 static void
613 get_msg(int qid, int msgtype)
614 {
615 struct msgbuf msg;
616
617 if (msgrcv(qid, &msg, sizeof(msg.mtext), msgtype,
618 MSG_NOERROR | IPC_NOWAIT) == \-1) {
619 if (errno != ENOMSG) {
620 perror("msgrcv");
621 exit(EXIT_FAILURE);
622 }
623 printf("No message available for msgrcv()\en");
624 } else
625 printf("message received: %s\en", msg.mtext);
626 }
627
628 int
629 main(int argc, char *argv[])
630 {
631 int qid, opt;
632 int mode = 0; /* 1 = send, 2 = receive */
633 int msgtype = 1;
634 int msgkey = 1234;
635
636 while ((opt = getopt(argc, argv, "srt:k:")) != \-1) {
637 switch (opt) {
638 case \(aqs\(aq:
639 mode = 1;
640 break;
641 case \(aqr\(aq:
642 mode = 2;
643 break;
644 case \(aqt\(aq:
645 msgtype = atoi(optarg);
646 if (msgtype <= 0)
647 usage(argv[0], "\-t option must be greater than 0\en");
648 break;
649 case \(aqk\(aq:
650 msgkey = atoi(optarg);
651 break;
652 default:
653 usage(argv[0], "Unrecognized option\en");
654 }
655 }
656
657 if (mode == 0)
658 usage(argv[0], "must use either \-s or \-r option\en");
659
660 qid = msgget(msgkey, IPC_CREAT | 0666);
661
662 if (qid == \-1) {
663 perror("msgget");
664 exit(EXIT_FAILURE);
665 }
666
667 if (mode == 2)
668 get_msg(qid, msgtype);
669 else
670 send_msg(qid, msgtype);
671
672 exit(EXIT_SUCCESS);
673 }
674 .EE
675 .SH SEE ALSO
676 .BR msgctl (2),
677 .BR msgget (2),
678 .BR capabilities (7),
679 .BR mq_overview (7),
680 .BR sysvipc (7)