]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/msgop.2
msgop.2: Minor tweaks to Bill Pemberton's patch
[thirdparty/man-pages.git] / 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 .TH MSGOP 2 2015-02-21 "Linux" "Linux Programmer's Manual"
40 .SH NAME
41 msgrcv, msgsnd \- System V message queue operations
42 .SH SYNOPSIS
43 .nf
44 .B #include <sys/types.h>
45 .B #include <sys/ipc.h>
46 .B #include <sys/msg.h>
47 .sp
48 .BI "int msgsnd(int " msqid ", const void *" msgp ", size_t " msgsz \
49 ", int " msgflg );
50 .sp
51 .BI "ssize_t msgrcv(int " msqid ", void *" msgp ", size_t " msgsz \
52 ", long " msgtyp ,
53 .BI " int " msgflg );
54 .fi
55 .SH DESCRIPTION
56 The
57 .BR msgsnd ()
58 and
59 .BR msgrcv ()
60 system calls are used, respectively, to send messages to,
61 and receive messages from, a System\ V message queue.
62 The calling process must have write permission on the message queue
63 in order to send a message, and read permission to receive a message.
64 .PP
65 The
66 .I msgp
67 argument is a pointer to a caller-defined structure
68 of the following general form:
69 .in +4n
70 .nf
71
72 struct msgbuf {
73 long mtype; /* message type, must be > 0 */
74 char mtext[1]; /* message data */
75 };
76 .fi
77 .in
78 .PP
79 The
80 .I mtext
81 field is an array (or other structure) whose size is specified by
82 .IR msgsz ,
83 a nonnegative integer value.
84 Messages of zero length (i.e., no
85 .I mtext
86 field) are permitted.
87 The
88 .I mtype
89 field must have a strictly positive integer value.
90 This value can be
91 used by the receiving process for message selection
92 (see the description of
93 .BR msgrcv ()
94 below).
95 .SS msgsnd()
96 The
97 .BR msgsnd ()
98 system call appends a copy of the message pointed to by
99 .I msgp
100 to the message queue whose identifier is specified
101 by
102 .IR msqid .
103 .PP
104 If sufficient space is available in the queue,
105 .BR msgsnd ()
106 succeeds immediately.
107 The queue capacity is governed by the
108 .I msg_qbytes
109 field in the associated data structure for the message queue.
110 During queue creation this field is initialized to
111 .B MSGMNB
112 bytes, but this limit can be modified using
113 .BR msgctl (2).
114 A message queue is considered to be full if either of the following
115 conditions is true:
116 .IP * 2
117 Adding a new message to the queue would cause the total number of bytes
118 in the queue to exceed the queue's maximum size (the
119 .I msg_qbytes
120 field).
121 .IP *
122 Adding another message to the queue would cause the total number of messages
123 in the queue to exceed the queue's maximum size (the
124 .I msg_qbytes
125 field).
126 This check is necessary to prevent an unlimited number of zero-length
127 messages being placed on the queue.
128 Although such messages contain no data,
129 they nevertheless consume (locked) kernel memory.
130 .PP
131 If insufficient space is available in the queue, then the default
132 behavior of
133 .BR msgsnd ()
134 is to block until space becomes available.
135 If
136 .B IPC_NOWAIT
137 is specified in
138 .IR msgflg ,
139 then the call instead fails with the error
140 .BR EAGAIN .
141
142 A blocked
143 .BR msgsnd ()
144 call may also fail if:
145 .IP * 2
146 the queue is removed,
147 in which case the system call fails with
148 .I errno
149 set to
150 .BR EIDRM ;
151 or
152 .IP *
153 a signal is caught, in which case the system call fails
154 with
155 .I errno
156 set to
157 .BR EINTR ; see
158 .BR signal (7).
159 .RB ( msgsnd ()
160 is never automatically restarted after being interrupted by a
161 signal handler, regardless of the setting of the
162 .B SA_RESTART
163 flag when establishing a signal handler.)
164 .PP
165 Upon successful completion the message queue data structure is updated
166 as follows:
167 .IP
168 .I msg_lspid
169 is set to the process ID of the calling process.
170 .IP
171 .I msg_qnum
172 is incremented by 1.
173 .IP
174 .I msg_stime
175 is set to the current time.
176 .SS msgrcv()
177 The
178 .BR msgrcv ()
179 system call removes a message from the queue specified by
180 .I msqid
181 and places it in the buffer
182 pointed to by
183 .IR msgp .
184 .PP
185 The argument
186 .I msgsz
187 specifies the maximum size in bytes for the member
188 .I mtext
189 of the structure pointed to by the
190 .I msgp
191 argument.
192 If the message text has length greater than
193 .IR msgsz ,
194 then the behavior depends on whether
195 .B MSG_NOERROR
196 is specified in
197 .IR msgflg .
198 If
199 .B MSG_NOERROR
200 is specified, then
201 the message text will be truncated (and the truncated part will be
202 lost); if
203 .B MSG_NOERROR
204 is not specified, then
205 the message isn't removed from the queue and
206 the system call fails returning \-1 with
207 .I errno
208 set to
209 .BR E2BIG .
210 .PP
211 Unless
212 .B MSG_COPY
213 is specified in
214 .IR msgflg
215 (see below),
216 the
217 .I msgtyp
218 argument specifies the type of message requested, as follows:
219 .IP * 2
220 If
221 .I msgtyp
222 is 0,
223 then the first message in the queue is read.
224 .IP *
225 If
226 .I msgtyp
227 is greater than 0,
228 then the first message in the queue of type
229 .I msgtyp
230 is read, unless
231 .B MSG_EXCEPT
232 was specified in
233 .IR msgflg ,
234 in which case
235 the first message in the queue of type not equal to
236 .I msgtyp
237 will be read.
238 .IP *
239 If
240 .I msgtyp
241 is less than 0,
242 then the first message in the queue with the lowest type less than or
243 equal to the absolute value of
244 .I msgtyp
245 will be read.
246 .PP
247 The
248 .I msgflg
249 argument is a bit mask constructed by ORing together zero or more
250 of the following flags:
251 .TP
252 .B IPC_NOWAIT
253 Return immediately if no message of the requested type is in the queue.
254 The system call fails with
255 .I errno
256 set to
257 .BR ENOMSG .
258 .TP
259 .BR MSG_COPY " (since Linux 3.8)"
260 .\" commit 4a674f34ba04a002244edaf891b5da7fc1473ae8
261 Nondestructively fetch a copy of the message at the ordinal position
262 in the queue specified by
263 .I msgtyp
264 (messages are considered to be numbered starting at 0).
265
266 This flag must be specified in conjunction with
267 .BR IPC_NOWAIT ,
268 with the result that, if there is no message available at the given position,
269 the call fails immediately with the error
270 .BR ENOMSG .
271 Because they alter the meaning of
272 .I msgtyp
273 in orthogonal ways,
274 .BR MSG_COPY
275 and
276 .BR MSG_EXCEPT
277 may not both be specified in
278 .IR msgflg .
279
280 The
281 .BR MSG_COPY
282 flag was added for the implementation of
283 the kernel checkpoint-restore facility and
284 is available only if the kernel was built with the
285 .B CONFIG_CHECKPOINT_RESTORE
286 option.
287 .TP
288 .B MSG_EXCEPT
289 Used with
290 .I msgtyp
291 greater than 0
292 to read the first message in the queue with message type that differs
293 from
294 .IR msgtyp .
295 .TP
296 .B MSG_NOERROR
297 To truncate the message text if longer than
298 .I msgsz
299 bytes.
300 .PP
301 If no message of the requested type is available and
302 .B IPC_NOWAIT
303 isn't specified in
304 .IR msgflg ,
305 the calling process is blocked until one of the following conditions occurs:
306 .IP * 2
307 A message of the desired type is placed in the queue.
308 .IP *
309 The message queue is removed from the system.
310 In this case, the system call fails with
311 .I errno
312 set to
313 .BR EIDRM .
314 .IP *
315 The calling process catches a signal.
316 In this case, the system call fails with
317 .I errno
318 set to
319 .BR EINTR .
320 .RB ( msgrcv ()
321 is never automatically restarted after being interrupted by a
322 signal handler, regardless of the setting of the
323 .B SA_RESTART
324 flag when establishing a signal handler.)
325 .PP
326 Upon successful completion the message queue data structure is updated
327 as follows:
328 .IP
329 .I msg_lrpid
330 is set to the process ID of the calling process.
331 .IP
332 .I msg_qnum
333 is decremented by 1.
334 .IP
335 .I msg_rtime
336 is set to the current time.
337 .SH RETURN VALUE
338 On failure both functions return \-1
339 with
340 .I errno
341 indicating the error,
342 otherwise
343 .BR msgsnd ()
344 returns 0
345 and
346 .BR msgrcv ()
347 returns the number of bytes actually copied into the
348 .I mtext
349 array.
350 .SH ERRORS
351 When
352 .BR msgsnd ()
353 fails,
354 .I errno
355 will be set to one among the following values:
356 .TP
357 .B EACCES
358 The calling process does not have write permission on the message queue,
359 and does not have the
360 .B CAP_IPC_OWNER
361 capability.
362 .TP
363 .B EAGAIN
364 The message can't be sent due to the
365 .I msg_qbytes
366 limit for the queue and
367 .B IPC_NOWAIT
368 was specified in
369 .IR msgflg .
370 .TP
371 .B EFAULT
372 The address pointed to by
373 .I msgp
374 isn't accessible.
375 .TP
376 .B EIDRM
377 The message queue was removed.
378 .TP
379 .B EINTR
380 Sleeping on a full message queue condition, the process caught a signal.
381 .TP
382 .B EINVAL
383 Invalid
384 .I msqid
385 value, or nonpositive
386 .I mtype
387 value, or
388 invalid
389 .I msgsz
390 value (less than 0 or greater than the system value
391 .BR MSGMAX ).
392 .TP
393 .B ENOMEM
394 The system does not have enough memory to make a copy of the
395 message pointed to by
396 .IR msgp .
397 .PP
398 When
399 .BR msgrcv ()
400 fails,
401 .I errno
402 will be set to one among the following values:
403 .TP
404 .B E2BIG
405 The message text length is greater than
406 .I msgsz
407 and
408 .B MSG_NOERROR
409 isn't specified in
410 .IR msgflg .
411 .TP
412 .B EACCES
413 The calling process does not have read permission on the message queue,
414 and does not have the
415 .B CAP_IPC_OWNER
416 capability.
417 .TP
418 .B EAGAIN
419 No message was available in the queue and
420 .B IPC_NOWAIT
421 was specified in
422 .IR msgflg .
423 .TP
424 .B EFAULT
425 The address pointed to by
426 .I msgp
427 isn't accessible.
428 .TP
429 .B EIDRM
430 While the process was sleeping to receive a message,
431 the message queue was removed.
432 .TP
433 .B EINTR
434 While the process was sleeping to receive a message,
435 the process caught a signal; see
436 .BR signal (7).
437 .TP
438 .B EINVAL
439 .I msgqid
440 was invalid, or
441 .I msgsz
442 was less than 0.
443 .TP
444 .BR EINVAL " (since Linux 3.14)"
445 .I msgflg
446 specified
447 .BR MSG_COPY ,
448 but not
449 .BR IPC_NOWAIT .
450 .TP
451 .BR EINVAL " (since Linux 3.14)"
452 .I msgflg
453 specified both
454 .BR MSG_COPY
455 and
456 .BR MSG_EXCEPT .
457 .TP
458 .B ENOMSG
459 .B IPC_NOWAIT
460 was specified in
461 .I msgflg
462 and no message of the requested type existed on the message queue.
463 .TP
464 .B ENOMSG
465 .B IPC_NOWAIT
466 and
467 .B MSG_COPY
468 were specified in
469 .I msgflg
470 and the queue contains less than
471 .I msgtyp
472 messages.
473 .TP
474 .BR ENOSYS " (since Linux 3.8)"
475 .I MSG_COPY
476 was specified in
477 .IR msgflg ,
478 and this kernel was configured without
479 .BR CONFIG_CHECKPOINT_RESTORE .
480 .SH CONFORMING TO
481 SVr4, POSIX.1-2001.
482
483 The
484 .B MSG_EXCEPT
485 and
486 .B MSG_COPY
487 flags are Linux-specific;
488 their definitions can be obtained by defining the
489 .B _GNU_SOURCE
490 .\" MSG_COPY since glibc 2.18
491 feature test macro.
492 .SH NOTES
493 The inclusion of
494 .I <sys/types.h>
495 and
496 .I <sys/ipc.h>
497 isn't required on Linux or by any version of POSIX.
498 However,
499 some old implementations required the inclusion of these header files,
500 and the SVID also documented their inclusion.
501 Applications intended to be portable to such old systems may need
502 to include these header files.
503 .\" Like Linux, the FreeBSD man pages still document
504 .\" the inclusion of these header files.
505
506 The
507 .I msgp
508 argument is declared as \fIstruct msgbuf\ *\fP in
509 glibc 2.0 and 2.1.
510 It is declared as \fIvoid\ *\fP
511 in glibc 2.2 and later, as required by SUSv2 and SUSv3.
512
513 The following limits on message queue resources affect the
514 .BR msgsnd ()
515 call:
516 .TP
517 .B MSGMAX
518 Maximum size of a message text, in bytes (default value: 8192 bytes).
519 On Linux, this limit can be read and modified via
520 .IR /proc/sys/kernel/msgmax .
521 .TP
522 .B MSGMNB
523 Maximum number of bytes that can be held in a message queue
524 (default value: 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 using the
534 .BR msgctl (2)
535 .B IPC_SET
536 operation.
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 EXAMPLE
580 The program below demonstrates the use of
581 .BR msgsnd ()
582 and
583 .BR msgrcv ().
584
585 The example program is first run with the \fB\-s\fP option to send a
586 message and then run again with the \fB\-r\fP option to receive a
587 message.
588
589 The following shell session shows a sample run of the program:
590 .in +4n
591 .nf
592
593 .RB "$" " ./a.out \-s"
594 sent: a message at Wed Mar 4 16:25:45 2015
595
596 .RB "$" " ./a.out \-r"
597 message received: a message at Wed Mar 4 16:25:45 2015
598
599 .fi
600 .in
601 .SS Program source
602 \&
603 .nf
604 #include <stdio.h>
605 #include <stdlib.h>
606 #include <string.h>
607 #include <time.h>
608 #include <unistd.h>
609 #include <errno.h>
610 #include <sys/types.h>
611 #include <sys/ipc.h>
612 #include <sys/msg.h>
613
614 struct msgbuf {
615 long mtype;
616 char mtext[80];
617 };
618
619 static void
620 usage(char *prog_name, char *msg)
621 {
622 if (msg != NULL)
623 fputs(msg, stderr);
624
625 fprintf(stderr, "Usage: %s [options]\\n", prog_name);
626 fprintf(stderr, "Options are:\\n");
627 fprintf(stderr, "\-s send message using msgsnd()\\n");
628 fprintf(stderr, "\-r read message using msgrcv()\\n");
629 fprintf(stderr, "\-t message type (default is 1)\\n");
630 fprintf(stderr, "\-k message queue key (default is 1234)\\n");
631 exit(EXIT_FAILURE);
632 }
633
634 static void
635 send_msg(int qid, int msgtype)
636 {
637 struct msgbuf msg;
638 time_t t;
639
640 msg.mtype = msgtype;
641
642 time(&t);
643 snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
644 ctime(&t));
645
646 if (msgsnd(qid, (void *) &msg, sizeof(msg.mtext),
647 IPC_NOWAIT) == \-1) {
648 perror("msgsnd error");
649 exit(EXIT_FAILURE);
650 }
651 printf("sent: %s\\n", msg.mtext);
652 }
653
654 static void
655 get_msg(int qid, int msgtype)
656 {
657 struct msgbuf msg;
658
659 if (msgrcv(qid, (void *) &msg, sizeof(msg.mtext), msgtype,
660 MSG_NOERROR | IPC_NOWAIT) == \-1) {
661 if (errno != ENOMSG) {
662 perror("msgrcv");
663 exit(EXIT_FAILURE);
664 }
665 printf("No message available for msgrcv()\\n");
666 } else
667 printf("message received: %s\\n", msg.mtext);
668 }
669
670 int
671 main(int argc, char *argv[])
672 {
673 int qid, opt;
674 int mode = 0; /* 1 = send, 2 = receive */
675 int msgtype = 1;
676 int msgkey = 1234;
677
678 while ((opt = getopt(argc, argv, "srt:k:")) != \-1) {
679 switch (opt) {
680 case \(aqs\(aq:
681 mode = 1;
682 break;
683 case \(aqr\(aq:
684 mode = 2;
685 break;
686 case \(aqt\(aq:
687 msgtype = atoi(optarg);
688 if (msgtype <= 0)
689 usage(argv[0], "\-t option must be greater than 0\\n");
690 break;
691 case \(aqk\(aq:
692 msgkey = atoi(optarg);
693 break;
694 default:
695 usage(argv[0], "Unrecognized option\\n");
696 }
697 }
698
699 if (mode == 0)
700 usage(argv[0], "must use either \-s or \-r option\\n");
701
702 qid = msgget(msgkey, IPC_CREAT | 0666);
703
704 if (qid == \-1) {
705 perror("msgget");
706 exit(EXIT_FAILURE);
707 }
708
709 if (mode == 2)
710 get_msg(qid, msgtype);
711 else
712 send_msg(qid, msgtype);
713
714 exit(EXIT_SUCCESS);
715 }
716 .fi
717 .SH SEE ALSO
718 .BR msgctl (2),
719 .BR msgget (2),
720 .BR capabilities (7),
721 .BR mq_overview (7),
722 .BR svipc (7)