]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/msgop.2
sched_setattr.2: tfix
[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-04-11 "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 .I MSG_COPY
471 was specified in
472 .IR msgflg ,
473 and this kernel was configured without
474 .BR CONFIG_CHECKPOINT_RESTORE .
475 .SH CONFORMING TO
476 POSIX.1-2001, POSIX.1-2008, SVr4.
477 .PP
478 The
479 .B MSG_EXCEPT
480 and
481 .B MSG_COPY
482 flags are Linux-specific;
483 their definitions can be obtained by defining the
484 .B _GNU_SOURCE
485 .\" MSG_COPY since glibc 2.18
486 feature test macro.
487 .SH NOTES
488 The inclusion of
489 .I <sys/types.h>
490 and
491 .I <sys/ipc.h>
492 isn't required on Linux or by any version of POSIX.
493 However,
494 some old implementations required the inclusion of these header files,
495 and the SVID also documented their inclusion.
496 Applications intended to be portable to such old systems may need
497 to include these header files.
498 .\" Like Linux, the FreeBSD man pages still document
499 .\" the inclusion of these header files.
500 .PP
501 The
502 .I msgp
503 argument is declared as \fIstruct msgbuf\ *\fP in
504 glibc 2.0 and 2.1.
505 It is declared as \fIvoid\ *\fP
506 in glibc 2.2 and later, as required by SUSv2 and SUSv3.
507 .PP
508 The following limits on message queue resources affect the
509 .BR msgsnd ()
510 call:
511 .TP
512 .B MSGMAX
513 Maximum size of a message text, in bytes (default value: 8192 bytes).
514 On Linux, this limit can be read and modified via
515 .IR /proc/sys/kernel/msgmax .
516 .TP
517 .B MSGMNB
518 Maximum number of bytes that can be held in a message queue
519 (default value: 16384 bytes).
520 On Linux, this limit can be read and modified via
521 .IR /proc/sys/kernel/msgmnb .
522 A privileged process
523 (Linux: a process with the
524 .B CAP_SYS_RESOURCE
525 capability)
526 can increase the size of a message queue beyond
527 .B MSGMNB
528 using the
529 .BR msgctl (2)
530 .B IPC_SET
531 operation.
532 .PP
533 The implementation has no intrinsic system-wide limits on the
534 number of message headers
535 .RB ( MSGTQL )
536 and the number of bytes in the message pool
537 .RB ( MSGPOOL ).
538 .SH BUGS
539 In Linux 3.13 and earlier,
540 if
541 .BR msgrcv ()
542 was called with the
543 .BR MSG_COPY
544 flag, but without
545 .BR IPC_NOWAIT ,
546 and the message queue contained less than
547 .I msgtyp
548 messages, then the call would block until the next message is written
549 to the queue.
550 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
551 At that point, the call would return a copy of the message,
552 .I regardless
553 of whether that message was at the ordinal position
554 .IR msgtyp .
555 This bug is fixed
556 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
557 in Linux 3.14.
558 .PP
559 Specifying both
560 .B MSG_COPY
561 and
562 .B MSC_EXCEPT
563 in
564 .I msgflg
565 is a logical error (since these flags impose different interpretations on
566 .IR msgtyp ).
567 In Linux 3.13 and earlier,
568 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
569 this error was not diagnosed by
570 .BR msgrcv ().
571 This bug is fixed
572 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
573 in Linux 3.14.
574 .SH EXAMPLE
575 The program below demonstrates the use of
576 .BR msgsnd ()
577 and
578 .BR msgrcv ().
579 .PP
580 The example program is first run with the \fB\-s\fP option to send a
581 message and then run again with the \fB\-r\fP option to receive a
582 message.
583 .PP
584 The following shell session shows a sample run of the program:
585 .PP
586 .in +4n
587 .EX
588 .RB "$" " ./a.out \-s"
589 sent: a message at Wed Mar 4 16:25:45 2015
590
591 .RB "$" " ./a.out \-r"
592 message received: a message at Wed Mar 4 16:25:45 2015
593 .EE
594 .in
595 .SS Program source
596 \&
597 .EX
598 #include <stdio.h>
599 #include <stdlib.h>
600 #include <string.h>
601 #include <time.h>
602 #include <unistd.h>
603 #include <errno.h>
604 #include <sys/types.h>
605 #include <sys/ipc.h>
606 #include <sys/msg.h>
607
608 struct msgbuf {
609 long mtype;
610 char mtext[80];
611 };
612
613 static void
614 usage(char *prog_name, char *msg)
615 {
616 if (msg != NULL)
617 fputs(msg, stderr);
618
619 fprintf(stderr, "Usage: %s [options]\en", prog_name);
620 fprintf(stderr, "Options are:\en");
621 fprintf(stderr, "\-s send message using msgsnd()\en");
622 fprintf(stderr, "\-r read message using msgrcv()\en");
623 fprintf(stderr, "\-t message type (default is 1)\en");
624 fprintf(stderr, "\-k message queue key (default is 1234)\en");
625 exit(EXIT_FAILURE);
626 }
627
628 static void
629 send_msg(int qid, int msgtype)
630 {
631 struct msgbuf msg;
632 time_t t;
633
634 msg.mtype = msgtype;
635
636 time(&t);
637 snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
638 ctime(&t));
639
640 if (msgsnd(qid, (void *) &msg, sizeof(msg.mtext),
641 IPC_NOWAIT) == \-1) {
642 perror("msgsnd error");
643 exit(EXIT_FAILURE);
644 }
645 printf("sent: %s\en", msg.mtext);
646 }
647
648 static void
649 get_msg(int qid, int msgtype)
650 {
651 struct msgbuf msg;
652
653 if (msgrcv(qid, (void *) &msg, sizeof(msg.mtext), msgtype,
654 MSG_NOERROR | IPC_NOWAIT) == \-1) {
655 if (errno != ENOMSG) {
656 perror("msgrcv");
657 exit(EXIT_FAILURE);
658 }
659 printf("No message available for msgrcv()\en");
660 } else
661 printf("message received: %s\en", msg.mtext);
662 }
663
664 int
665 main(int argc, char *argv[])
666 {
667 int qid, opt;
668 int mode = 0; /* 1 = send, 2 = receive */
669 int msgtype = 1;
670 int msgkey = 1234;
671
672 while ((opt = getopt(argc, argv, "srt:k:")) != \-1) {
673 switch (opt) {
674 case \(aqs\(aq:
675 mode = 1;
676 break;
677 case \(aqr\(aq:
678 mode = 2;
679 break;
680 case \(aqt\(aq:
681 msgtype = atoi(optarg);
682 if (msgtype <= 0)
683 usage(argv[0], "\-t option must be greater than 0\en");
684 break;
685 case \(aqk\(aq:
686 msgkey = atoi(optarg);
687 break;
688 default:
689 usage(argv[0], "Unrecognized option\en");
690 }
691 }
692
693 if (mode == 0)
694 usage(argv[0], "must use either \-s or \-r option\en");
695
696 qid = msgget(msgkey, IPC_CREAT | 0666);
697
698 if (qid == \-1) {
699 perror("msgget");
700 exit(EXIT_FAILURE);
701 }
702
703 if (mode == 2)
704 get_msg(qid, msgtype);
705 else
706 send_msg(qid, msgtype);
707
708 exit(EXIT_SUCCESS);
709 }
710 .EE
711 .SH SEE ALSO
712 .BR msgctl (2),
713 .BR msgget (2),
714 .BR capabilities (7),
715 .BR mq_overview (7),
716 .BR sysvipc (7)