xref: /minix3/lib/libc/sys/msgrcv.2 (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1.\"	$NetBSD: msgrcv.2,v 1.22 2013/07/24 11:54:04 skrll Exp $
2.\"
3.\" Copyright (c) 1995 Frank van der Linden
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"      This product includes software developed for the NetBSD Project
17.\"      by Frank van der Linden
18.\" 4. The name of the author may not be used to endorse or promote products
19.\"    derived from this software without specific prior written permission
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31.\"
32.Dd July 24, 2013
33.Dt MSGRCV 2
34.Os
35.Sh NAME
36.Nm msgrcv
37.Nd receive a message from a message queue
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In sys/msg.h
42.Ft ssize_t
43.Fn msgrcv "int msqid" "void *msgp" "size_t msgsz" "long msgtyp" "int msgflg"
44.Sh DESCRIPTION
45The
46.Fn msgrcv
47function receives a message from the message queue specified in
48.Fa msqid ,
49and places it into the user-defined structure pointed to by
50.Fa msgp .
51This structure must contain a first field of type
52.Sy long
53that will indicate the user-defined type of the message.
54The remaining fields will contain the contents of the message.
55The following is an example of what this user-defined structure might
56look like:
57.Bd -literal
58struct mymsg {
59    long mtype;    /* message type */
60    char mtext[1]; /* body of message */
61};
62.Ed
63.Pp
64.Va mtype
65is an integer greater than 0 that can be used to select messages.
66.Va mtext
67is an array of bytes, with size up to the system limit
68.Dv MSGMAX .
69.Pp
70The value of
71.Fa msgtyp
72has one of the following meanings:
73.Bl -bullet
74.It
75.Fa msgtyp
76is greater than 0.
77The first message of type
78.Fa msgtyp
79will be received.
80.It
81.Fa msgtyp
82is equal to 0.
83The first message on the queue will be received.
84.It
85.Fa msgtyp
86is less than 0.
87The first message of the lowest message type that is
88less than or equal to the absolute value of
89.Fa msgtyp
90will be received.
91.El
92.Pp
93The argument
94.Fa msgsz
95specifies the size in bytes of
96.Va mtext .
97If the received message has a length greater than
98.Fa msgsz
99it will be silently truncated if the
100.Dv MSG_NOERROR
101flag is set in
102.Fa msgflg ,
103otherwise an error will be returned.
104.Pp
105If no matching message is present on the message queue specified by
106.Fa msqid ,
107the behaviour of
108.Fn msgrcv
109depends on whether the
110.Dv IPC_NOWAIT
111flag is set in
112.Fa msgflg
113or not.
114If
115.Dv IPC_NOWAIT
116is set, then
117.Fn msgrcv
118will immediately return a value of \-1 and set
119.Va errno
120to
121.Er EAGAIN .
122If
123.Dv IPC_NOWAIT
124is not set, the calling process will block until:
125.Bl -bullet
126.It
127A message of the requested type becomes available on the message queue.
128.It
129The message queue is removed, in which case \-1 will be returned and
130.Va errno
131set to
132.Er EIDRM .
133.It
134A signal is received and caught.
135\-1 is returned and
136.Va errno
137is set to
138.Er EINTR .
139.El
140.Pp
141If a message is successfully received, the data structure associated with
142.Fa msqid
143is updated as follows:
144.Bl -bullet
145.It
146.Va msg_lrpid
147is set to the pid of the caller.
148.It
149.Va msg_lrtime
150is set to the current time.
151.It
152.Va msg_qnum
153is decremented by 1.
154.El
155.Sh RETURN VALUES
156Upon successful completion,
157.Fn msgrcv
158returns the number of bytes received and placed into the
159.Va mtext
160field of the structure pointed to by
161.Fa msgp .
162Otherwise, \-1 is returned, and
163.Va errno
164set to indicate the error.
165.Sh ERRORS
166.Fn msgrcv
167will fail if:
168.Bl -tag -width Er
169.It Bq Er E2BIG
170A matching message was received, but its size was greater than
171.Fa msgsz
172and the
173.Dv MSG_NOERROR
174flag was not set in
175.Fa msgflg .
176.It Bq Er EACCES
177The calling process does not have read access to the message queue.
178.It Bq Er EAGAIN
179There is no message of the requested type available on the message queue,
180and
181.Dv IPC_NOWAIT
182is set in
183.Fa msgflg .
184.It Bq Er EFAULT
185.Fa msgp
186points to an invalid address.
187.It Bq Er EIDRM
188The message queue identifier
189.Fa msqid
190is removed from the system.
191.It Bq Er EINTR
192The system call was interrupted by the delivery of a signal.
193.It Bq Er EINVAL
194.Fa msqid
195is not a valid message queue identifier
196.Pp
197The message queue was removed while
198.Fn msgrcv
199was waiting for a message of the requested type to become available in it.
200.Pp
201.Fa msgsz
202is greater than
203.Dv SSIZE_MAX .
204.It Bq Er ENOMSG
205The queue does not contain a message of the desired type and
206.Dv IPC_NOWAIT
207is set.
208.El
209.Sh SEE ALSO
210.Xr msgctl 2 ,
211.Xr msgget 2 ,
212.Xr msgsnd 2
213.Sh STANDARDS
214The
215.Nm
216system call conforms to
217.St -xsh5 .
218.Sh HISTORY
219Message queues appeared in the first release of
220.At V .
221