Debian Codegraph @

upstream/debian-unstable/man2/msgop.2

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