Changes.old: tfix
[man-pages.git] / man2 / msgop.2
blob6ffd76b2fe36ffa31f656bc44d37b1d80ae25153
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 (date) "Linux man-pages (unreleased)"
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>
30 .BI "int msgsnd(int " msqid ", const void " msgp [. msgsz "], size_t " msgsz ,
31 .BI "               int " msgflg );
33 .BI "ssize_t msgrcv(int " msqid ", void " msgp [. msgsz "], 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.
47 The
48 .I msgp
49 argument is a pointer to a caller-defined structure
50 of the following general form:
52 .in +4n
53 .EX
54 struct msgbuf {
55     long mtype;       /* message type, must be > 0 */
56     char mtext[1];    /* message data */
58 .EE
59 .in
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
84 .IR msqid .
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] 3
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.
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.
118 .B IPC_NOWAIT
119 is specified in
120 .IR msgflg ,
121 then the call instead fails with the error
122 .BR EAGAIN .
124 A blocked
125 .BR msgsnd ()
126 call may also fail if:
127 .IP \[bu] 3
128 the queue is removed,
129 in which case the system call fails with
130 .I errno
131 set to
132 .BR EIDRM ;
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.)
147 Upon successful completion the message queue data structure is updated
148 as follows:
149 .IP \[bu] 3
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()
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 .
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 .
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 .
193 Unless
194 .B MSG_COPY
195 is specified in
196 .I msgflg
197 (see below),
199 .I msgtyp
200 argument specifies the type of message requested, as follows:
201 .IP \[bu] 3
203 .I msgtyp
204 is 0,
205 then the first message in the queue is read.
206 .IP \[bu]
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]
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.
230 .I msgflg
231 argument is a bit mask constructed by ORing together zero or more
232 of the following flags:
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 .
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).
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
258 .B MSG_EXCEPT
259 may not both be specified in
260 .IR msgflg .
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.
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 .
278 .B MSG_NOERROR
279 To truncate the message text if longer than
280 .I msgsz
281 bytes.
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] 3
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.)
308 Upon successful completion the message queue data structure is updated
309 as follows:
311 .I msg_lrpid
312 is set to the process ID of the calling process.
314 .I msg_qnum
315 is decremented by 1.
317 .I msg_rtime
318 is set to the current time.
319 .SH RETURN VALUE
320 On success,
321 .BR msgsnd ()
322 returns 0
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:
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.
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 .
349 .B EFAULT
350 The address pointed to by
351 .I msgp
352 isn't accessible.
354 .B EIDRM
355 The message queue was removed.
357 .B EINTR
358 Sleeping on a full message queue condition, the process caught a signal.
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 ).
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 .
376 .BR msgrcv ()
377 can fail with the following errors:
379 .B E2BIG
380 The message text length is greater than
381 .I msgsz
383 .B MSG_NOERROR
384 isn't specified in
385 .IR msgflg .
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.
393 .B EFAULT
394 The address pointed to by
395 .I msgp
396 isn't accessible.
398 .B EIDRM
399 While the process was sleeping to receive a message,
400 the message queue was removed.
402 .B EINTR
403 While the process was sleeping to receive a message,
404 the process caught a signal; see
405 .BR signal (7).
407 .B EINVAL
408 .I msqid
409 was invalid, or
410 .I msgsz
411 was less than 0.
413 .BR EINVAL " (since Linux 3.14)"
414 .I msgflg
415 specified
416 .BR MSG_COPY ,
417 but not
418 .BR IPC_NOWAIT .
420 .BR EINVAL " (since Linux 3.14)"
421 .I msgflg
422 specified both
423 .B MSG_COPY
425 .BR MSG_EXCEPT .
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.
433 .B ENOMSG
434 .B IPC_NOWAIT
436 .B MSG_COPY
437 were specified in
438 .I msgflg
439 and the queue contains less than
440 .I msgtyp
441 messages.
443 .BR ENOSYS " (since Linux 3.8)"
444 Both
445 .B MSG_COPY
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 STANDARDS
453 POSIX.1-2008.
456 .B MSG_EXCEPT
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 HISTORY
465 POSIX.1-2001, SVr4.
468 .I msgp
469 argument is declared as \fIstruct msgbuf\ *\fP in
470 glibc 2.0 and 2.1.
471 It is declared as \fIvoid\ *\fP
472 in glibc 2.2 and later, as required by SUSv2 and SUSv3.
473 .SH NOTES
474 The following limits on message queue resources affect the
475 .BR msgsnd ()
476 call:
478 .B MSGMAX
479 Maximum size of a message text, in bytes (default value: 8192 bytes).
480 On Linux, this limit can be read and modified via
481 .IR /proc/sys/kernel/msgmax .
483 .B MSGMNB
484 Maximum number of bytes that can be held in a message queue
485 (default value: 16384 bytes).
486 On Linux, this limit can be read and modified via
487 .IR /proc/sys/kernel/msgmnb .
488 A privileged process
489 (Linux: a process with the
490 .B CAP_SYS_RESOURCE
491 capability)
492 can increase the size of a message queue beyond
493 .B MSGMNB
494 using the
495 .BR msgctl (2)
496 .B IPC_SET
497 operation.
499 The implementation has no intrinsic system-wide limits on the
500 number of message headers
501 .RB ( MSGTQL )
502 and the number of bytes in the message pool
503 .RB ( MSGPOOL ).
504 .SH BUGS
505 In Linux 3.13 and earlier,
507 .BR msgrcv ()
508 was called with the
509 .B MSG_COPY
510 flag, but without
511 .BR IPC_NOWAIT ,
512 and the message queue contained less than
513 .I msgtyp
514 messages, then the call would block until the next message is written
515 to the queue.
516 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
517 At that point, the call would return a copy of the message,
518 .I regardless
519 of whether that message was at the ordinal position
520 .IR msgtyp .
521 This bug is fixed
522 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
523 in Linux 3.14.
525 Specifying both
526 .B MSG_COPY
528 .B MSC_EXCEPT
530 .I msgflg
531 is a logical error (since these flags impose different interpretations on
532 .IR msgtyp ).
533 In Linux 3.13 and earlier,
534 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
535 this error was not diagnosed by
536 .BR msgrcv ().
537 This bug is fixed
538 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
539 in Linux 3.14.
540 .SH EXAMPLES
541 The program below demonstrates the use of
542 .BR msgsnd ()
544 .BR msgrcv ().
546 The example program is first run with the \fB\-s\fP option to send a
547 message and then run again with the \fB\-r\fP option to receive a
548 message.
550 The following shell session shows a sample run of the program:
552 .in +4n
554 .RB "$" " ./a.out \-s"
555 sent: a message at Wed Mar  4 16:25:45 2015
557 .RB "$" " ./a.out \-r"
558 message received: a message at Wed Mar  4 16:25:45 2015
561 .SS Program source
563 .\" SRC BEGIN (msgop.c)
565 #include <errno.h>
566 #include <stdio.h>
567 #include <stdlib.h>
568 #include <sys/ipc.h>
569 #include <sys/msg.h>
570 #include <time.h>
571 #include <unistd.h>
573 struct msgbuf {
574     long mtype;
575     char mtext[80];
578 static void
579 usage(char *prog_name, char *msg)
581     if (msg != NULL)
582         fputs(msg, stderr);
584     fprintf(stderr, "Usage: %s [options]\en", prog_name);
585     fprintf(stderr, "Options are:\en");
586     fprintf(stderr, "\-s        send message using msgsnd()\en");
587     fprintf(stderr, "\-r        read message using msgrcv()\en");
588     fprintf(stderr, "\-t        message type (default is 1)\en");
589     fprintf(stderr, "\-k        message queue key (default is 1234)\en");
590     exit(EXIT_FAILURE);
593 static void
594 send_msg(int qid, int msgtype)
596     time_t         t;
597     struct msgbuf  msg;
599     msg.mtype = msgtype;
601     time(&t);
602     snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
603              ctime(&t));
605     if (msgsnd(qid, &msg, sizeof(msg.mtext),
606                IPC_NOWAIT) == \-1)
607     {
608         perror("msgsnd error");
609         exit(EXIT_FAILURE);
610     }
611     printf("sent: %s\en", msg.mtext);
614 static void
615 get_msg(int qid, int msgtype)
617     struct msgbuf msg;
619     if (msgrcv(qid, &msg, sizeof(msg.mtext), msgtype,
620                MSG_NOERROR | IPC_NOWAIT) == \-1) {
621         if (errno != ENOMSG) {
622             perror("msgrcv");
623             exit(EXIT_FAILURE);
624         }
625         printf("No message available for msgrcv()\en");
626     } else {
627         printf("message received: %s\en", msg.mtext);
628     }
632 main(int argc, char *argv[])
634     int  qid, opt;
635     int  mode = 0;               /* 1 = send, 2 = receive */
636     int  msgtype = 1;
637     int  msgkey = 1234;
639     while ((opt = getopt(argc, argv, "srt:k:")) != \-1) {
640         switch (opt) {
641         case \[aq]s\[aq]:
642             mode = 1;
643             break;
644         case \[aq]r\[aq]:
645             mode = 2;
646             break;
647         case \[aq]t\[aq]:
648             msgtype = atoi(optarg);
649             if (msgtype <= 0)
650                 usage(argv[0], "\-t option must be greater than 0\en");
651             break;
652         case \[aq]k\[aq]:
653             msgkey = atoi(optarg);
654             break;
655         default:
656             usage(argv[0], "Unrecognized option\en");
657         }
658     }
660     if (mode == 0)
661         usage(argv[0], "must use either \-s or \-r option\en");
663     qid = msgget(msgkey, IPC_CREAT | 0666);
665     if (qid == \-1) {
666         perror("msgget");
667         exit(EXIT_FAILURE);
668     }
670     if (mode == 2)
671         get_msg(qid, msgtype);
672     else
673         send_msg(qid, msgtype);
675     exit(EXIT_SUCCESS);
678 .\" SRC END
679 .SH SEE ALSO
680 .BR msgctl (2),
681 .BR msgget (2),
682 .BR capabilities (7),
683 .BR mq_overview (7),
684 .BR sysvipc (7)