sysv_msg.c revision 1.49.10.1 1 1.49.10.1 joerg /* $NetBSD: sysv_msg.c,v 1.49.10.1 2007/10/26 15:48:43 joerg Exp $ */
2 1.26 thorpej
3 1.26 thorpej /*-
4 1.48 ad * Copyright (c) 1999, 2006, 2007 The NetBSD Foundation, Inc.
5 1.26 thorpej * All rights reserved.
6 1.26 thorpej *
7 1.26 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.26 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.48 ad * NASA Ames Research Center, and by Andrew Doran.
10 1.26 thorpej *
11 1.26 thorpej * Redistribution and use in source and binary forms, with or without
12 1.26 thorpej * modification, are permitted provided that the following conditions
13 1.26 thorpej * are met:
14 1.26 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.26 thorpej * notice, this list of conditions and the following disclaimer.
16 1.26 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.26 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.26 thorpej * documentation and/or other materials provided with the distribution.
19 1.26 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.26 thorpej * must display the following acknowledgement:
21 1.26 thorpej * This product includes software developed by the NetBSD
22 1.26 thorpej * Foundation, Inc. and its contributors.
23 1.26 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.26 thorpej * contributors may be used to endorse or promote products derived
25 1.26 thorpej * from this software without specific prior written permission.
26 1.26 thorpej *
27 1.26 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.26 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.26 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.26 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.26 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.26 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.26 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.26 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.26 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.26 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.26 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.26 thorpej */
39 1.9 cgd
40 1.1 cgd /*
41 1.1 cgd * Implementation of SVID messages
42 1.1 cgd *
43 1.26 thorpej * Author: Daniel Boulet
44 1.1 cgd *
45 1.1 cgd * Copyright 1993 Daniel Boulet and RTMX Inc.
46 1.1 cgd *
47 1.1 cgd * This system call was implemented by Daniel Boulet under contract from RTMX.
48 1.1 cgd *
49 1.1 cgd * Redistribution and use in source forms, with and without modification,
50 1.1 cgd * are permitted provided that this entire comment appears intact.
51 1.1 cgd *
52 1.1 cgd * Redistribution in binary form may occur without any restrictions.
53 1.1 cgd * Obviously, it would be nice if you gave credit where credit is due
54 1.1 cgd * but requiring it would be too onerous.
55 1.1 cgd *
56 1.1 cgd * This software is provided ``AS IS'' without any warranties of any kind.
57 1.1 cgd */
58 1.33 lukem
59 1.33 lukem #include <sys/cdefs.h>
60 1.49.10.1 joerg __KERNEL_RCSID(0, "$NetBSD: sysv_msg.c,v 1.49.10.1 2007/10/26 15:48:43 joerg Exp $");
61 1.23 tron
62 1.24 tron #define SYSVMSG
63 1.1 cgd
64 1.2 mycroft #include <sys/param.h>
65 1.2 mycroft #include <sys/kernel.h>
66 1.2 mycroft #include <sys/msg.h>
67 1.29 simonb #include <sys/sysctl.h>
68 1.29 simonb #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
69 1.10 cgd #include <sys/syscallargs.h>
70 1.42 elad #include <sys/kauth.h>
71 1.18 christos
72 1.1 cgd #define MSG_DEBUG
73 1.1 cgd #undef MSG_DEBUG_OK
74 1.1 cgd
75 1.20 christos #ifdef MSG_DEBUG_OK
76 1.21 christos #define MSG_PRINTF(a) printf a
77 1.20 christos #else
78 1.20 christos #define MSG_PRINTF(a)
79 1.20 christos #endif
80 1.20 christos
81 1.36 jdolecek static int nfree_msgmaps; /* # of free map entries */
82 1.36 jdolecek static short free_msgmaps; /* head of linked list of free map entries */
83 1.36 jdolecek static struct __msg *free_msghdrs; /* list of free msg headers */
84 1.36 jdolecek static char *msgpool; /* MSGMAX byte long msg buffer pool */
85 1.36 jdolecek static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
86 1.36 jdolecek static struct __msg *msghdrs; /* MSGTQL msg headers */
87 1.48 ad
88 1.48 ad kmsq_t *msqs; /* MSGMNI msqid_ds struct's */
89 1.48 ad kmutex_t msgmutex; /* subsystem lock */
90 1.1 cgd
91 1.37 junyoung static void msg_freehdr(struct __msg *);
92 1.18 christos
93 1.18 christos void
94 1.40 thorpej msginit(void)
95 1.1 cgd {
96 1.36 jdolecek int i, sz;
97 1.36 jdolecek vaddr_t v;
98 1.1 cgd
99 1.3 mycroft /*
100 1.3 mycroft * msginfo.msgssz should be a power of two for efficiency reasons.
101 1.3 mycroft * It is also pretty silly if msginfo.msgssz is less than 8
102 1.3 mycroft * or greater than about 256 so ...
103 1.3 mycroft */
104 1.1 cgd
105 1.3 mycroft i = 8;
106 1.3 mycroft while (i < 1024 && i != msginfo.msgssz)
107 1.3 mycroft i <<= 1;
108 1.49.10.1 joerg if (i != msginfo.msgssz) {
109 1.49.10.1 joerg panic("msginfo.msgssz = %d, not a small power of 2",
110 1.49.10.1 joerg msginfo.msgssz);
111 1.3 mycroft }
112 1.3 mycroft
113 1.3 mycroft if (msginfo.msgseg > 32767) {
114 1.49.10.1 joerg panic("msginfo.msgseg = %d > 32767", msginfo.msgseg);
115 1.3 mycroft }
116 1.3 mycroft
117 1.36 jdolecek /* Allocate pageable memory for our structures */
118 1.49.10.1 joerg sz = msginfo.msgmax +
119 1.49.10.1 joerg msginfo.msgseg * sizeof(struct msgmap) +
120 1.49.10.1 joerg msginfo.msgtql * sizeof(struct __msg) +
121 1.49.10.1 joerg msginfo.msgmni * sizeof(kmsq_t);
122 1.39 yamt v = uvm_km_alloc(kernel_map, round_page(sz), 0,
123 1.39 yamt UVM_KMF_WIRED|UVM_KMF_ZERO);
124 1.39 yamt if (v == 0)
125 1.36 jdolecek panic("sysv_msg: cannot allocate memory");
126 1.36 jdolecek msgpool = (void *)v;
127 1.49.10.1 joerg msgmaps = (void *)(msgpool + msginfo.msgmax);
128 1.49.10.1 joerg msghdrs = (void *)(msgmaps + msginfo.msgseg);
129 1.49.10.1 joerg msqs = (void *)(msghdrs + msginfo.msgtql);
130 1.49.10.1 joerg
131 1.49.10.1 joerg for (i = 0; i < (msginfo.msgseg - 1); i++)
132 1.49.10.1 joerg msgmaps[i].next = i + 1;
133 1.49.10.1 joerg msgmaps[msginfo.msgseg - 1].next = -1;
134 1.49.10.1 joerg
135 1.3 mycroft free_msgmaps = 0;
136 1.3 mycroft nfree_msgmaps = msginfo.msgseg;
137 1.3 mycroft
138 1.49.10.1 joerg for (i = 0; i < (msginfo.msgtql - 1); i++) {
139 1.3 mycroft msghdrs[i].msg_type = 0;
140 1.49.10.1 joerg msghdrs[i].msg_next = &msghdrs[i + 1];
141 1.49.10.1 joerg }
142 1.49.10.1 joerg i = msginfo.msgtql - 1;
143 1.49.10.1 joerg msghdrs[i].msg_type = 0;
144 1.49.10.1 joerg msghdrs[i].msg_next = NULL;
145 1.3 mycroft free_msghdrs = &msghdrs[0];
146 1.3 mycroft
147 1.4 mycroft for (i = 0; i < msginfo.msgmni; i++) {
148 1.48 ad cv_init(&msqs[i].msq_cv, "msgwait");
149 1.49.10.1 joerg /* Implies entry is available */
150 1.49.10.1 joerg msqs[i].msq_u.msg_qbytes = 0;
151 1.49.10.1 joerg /* Reset to a known value */
152 1.49.10.1 joerg msqs[i].msq_u.msg_perm._seq = 0;
153 1.3 mycroft }
154 1.48 ad
155 1.48 ad mutex_init(&msgmutex, MUTEX_DEFAULT, IPL_NONE);
156 1.1 cgd }
157 1.1 cgd
158 1.3 mycroft static void
159 1.40 thorpej msg_freehdr(struct __msg *msghdr)
160 1.1 cgd {
161 1.48 ad
162 1.48 ad KASSERT(mutex_owned(&msgmutex));
163 1.48 ad
164 1.3 mycroft while (msghdr->msg_ts > 0) {
165 1.3 mycroft short next;
166 1.49.10.1 joerg KASSERT(msghdr->msg_spot >= 0);
167 1.49.10.1 joerg KASSERT(msghdr->msg_spot < msginfo.msgseg);
168 1.49.10.1 joerg
169 1.3 mycroft next = msgmaps[msghdr->msg_spot].next;
170 1.3 mycroft msgmaps[msghdr->msg_spot].next = free_msgmaps;
171 1.3 mycroft free_msgmaps = msghdr->msg_spot;
172 1.5 mycroft nfree_msgmaps++;
173 1.3 mycroft msghdr->msg_spot = next;
174 1.3 mycroft if (msghdr->msg_ts >= msginfo.msgssz)
175 1.3 mycroft msghdr->msg_ts -= msginfo.msgssz;
176 1.3 mycroft else
177 1.3 mycroft msghdr->msg_ts = 0;
178 1.3 mycroft }
179 1.49.10.1 joerg KASSERT(msghdr->msg_spot == -1);
180 1.3 mycroft msghdr->msg_next = free_msghdrs;
181 1.3 mycroft free_msghdrs = msghdr;
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd int
185 1.46 yamt sys___msgctl13(struct lwp *l, void *v, register_t *retval)
186 1.16 thorpej {
187 1.26 thorpej struct sys___msgctl13_args /* {
188 1.10 cgd syscallarg(int) msqid;
189 1.10 cgd syscallarg(int) cmd;
190 1.10 cgd syscallarg(struct msqid_ds *) buf;
191 1.16 thorpej } */ *uap = v;
192 1.26 thorpej struct msqid_ds msqbuf;
193 1.26 thorpej int cmd, error;
194 1.26 thorpej
195 1.26 thorpej cmd = SCARG(uap, cmd);
196 1.26 thorpej
197 1.26 thorpej if (cmd == IPC_SET) {
198 1.26 thorpej error = copyin(SCARG(uap, buf), &msqbuf, sizeof(msqbuf));
199 1.26 thorpej if (error)
200 1.26 thorpej return (error);
201 1.26 thorpej }
202 1.26 thorpej
203 1.44 ad error = msgctl1(l, SCARG(uap, msqid), cmd,
204 1.26 thorpej (cmd == IPC_SET || cmd == IPC_STAT) ? &msqbuf : NULL);
205 1.26 thorpej
206 1.26 thorpej if (error == 0 && cmd == IPC_STAT)
207 1.26 thorpej error = copyout(&msqbuf, SCARG(uap, buf), sizeof(msqbuf));
208 1.26 thorpej
209 1.26 thorpej return (error);
210 1.26 thorpej }
211 1.26 thorpej
212 1.26 thorpej int
213 1.44 ad msgctl1(struct lwp *l, int msqid, int cmd, struct msqid_ds *msqbuf)
214 1.26 thorpej {
215 1.44 ad kauth_cred_t cred = l->l_cred;
216 1.26 thorpej struct msqid_ds *msqptr;
217 1.48 ad kmsq_t *msq;
218 1.26 thorpej int error = 0, ix;
219 1.1 cgd
220 1.26 thorpej MSG_PRINTF(("call to msgctl1(%d, %d)\n", msqid, cmd));
221 1.1 cgd
222 1.26 thorpej ix = IPCID_TO_IX(msqid);
223 1.1 cgd
224 1.48 ad mutex_enter(&msgmutex);
225 1.48 ad
226 1.26 thorpej if (ix < 0 || ix >= msginfo.msgmni) {
227 1.26 thorpej MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", ix,
228 1.20 christos msginfo.msgmni));
229 1.48 ad error = EINVAL;
230 1.48 ad goto unlock;
231 1.3 mycroft }
232 1.1 cgd
233 1.48 ad msq = &msqs[ix];
234 1.48 ad msqptr = &msq->msq_u;
235 1.1 cgd
236 1.3 mycroft if (msqptr->msg_qbytes == 0) {
237 1.20 christos MSG_PRINTF(("no such msqid\n"));
238 1.48 ad error = EINVAL;
239 1.48 ad goto unlock;
240 1.3 mycroft }
241 1.26 thorpej if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqid)) {
242 1.20 christos MSG_PRINTF(("wrong sequence number\n"));
243 1.48 ad error = EINVAL;
244 1.48 ad goto unlock;
245 1.3 mycroft }
246 1.1 cgd
247 1.3 mycroft switch (cmd) {
248 1.3 mycroft case IPC_RMID:
249 1.1 cgd {
250 1.26 thorpej struct __msg *msghdr;
251 1.26 thorpej if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)) != 0)
252 1.48 ad break;
253 1.3 mycroft /* Free the message headers */
254 1.26 thorpej msghdr = msqptr->_msg_first;
255 1.3 mycroft while (msghdr != NULL) {
256 1.26 thorpej struct __msg *msghdr_tmp;
257 1.3 mycroft
258 1.3 mycroft /* Free the segments of each message */
259 1.26 thorpej msqptr->_msg_cbytes -= msghdr->msg_ts;
260 1.5 mycroft msqptr->msg_qnum--;
261 1.3 mycroft msghdr_tmp = msghdr;
262 1.3 mycroft msghdr = msghdr->msg_next;
263 1.3 mycroft msg_freehdr(msghdr_tmp);
264 1.3 mycroft }
265 1.49.10.1 joerg KASSERT(msqptr->_msg_cbytes == 0);
266 1.49.10.1 joerg KASSERT(msqptr->msg_qnum == 0);
267 1.1 cgd
268 1.49.10.1 joerg /* Mark it as free */
269 1.49.10.1 joerg msqptr->msg_qbytes = 0;
270 1.48 ad cv_broadcast(&msq->msq_cv);
271 1.1 cgd }
272 1.3 mycroft break;
273 1.1 cgd
274 1.3 mycroft case IPC_SET:
275 1.26 thorpej if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
276 1.48 ad break;
277 1.44 ad if (msqbuf->msg_qbytes > msqptr->msg_qbytes &&
278 1.47 elad kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
279 1.48 ad NULL) != 0) {
280 1.48 ad error = EPERM;
281 1.48 ad break;
282 1.48 ad }
283 1.26 thorpej if (msqbuf->msg_qbytes > msginfo.msgmnb) {
284 1.26 thorpej MSG_PRINTF(("can't increase msg_qbytes beyond %d "
285 1.26 thorpej "(truncating)\n", msginfo.msgmnb));
286 1.26 thorpej /* silently restrict qbytes to system limit */
287 1.26 thorpej msqbuf->msg_qbytes = msginfo.msgmnb;
288 1.3 mycroft }
289 1.26 thorpej if (msqbuf->msg_qbytes == 0) {
290 1.20 christos MSG_PRINTF(("can't reduce msg_qbytes to 0\n"));
291 1.48 ad error = EINVAL; /* XXX non-standard errno! */
292 1.48 ad break;
293 1.3 mycroft }
294 1.26 thorpej msqptr->msg_perm.uid = msqbuf->msg_perm.uid;
295 1.26 thorpej msqptr->msg_perm.gid = msqbuf->msg_perm.gid;
296 1.3 mycroft msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
297 1.26 thorpej (msqbuf->msg_perm.mode & 0777);
298 1.26 thorpej msqptr->msg_qbytes = msqbuf->msg_qbytes;
299 1.43 kardel msqptr->msg_ctime = time_second;
300 1.3 mycroft break;
301 1.1 cgd
302 1.3 mycroft case IPC_STAT:
303 1.26 thorpej if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
304 1.20 christos MSG_PRINTF(("requester doesn't have read access\n"));
305 1.49 ad break;
306 1.3 mycroft }
307 1.26 thorpej memcpy(msqbuf, msqptr, sizeof(struct msqid_ds));
308 1.3 mycroft break;
309 1.1 cgd
310 1.3 mycroft default:
311 1.20 christos MSG_PRINTF(("invalid command %d\n", cmd));
312 1.48 ad error = EINVAL;
313 1.48 ad break;
314 1.3 mycroft }
315 1.3 mycroft
316 1.49.10.1 joerg unlock:
317 1.48 ad mutex_exit(&msgmutex);
318 1.26 thorpej return (error);
319 1.1 cgd }
320 1.1 cgd
321 1.1 cgd int
322 1.40 thorpej sys_msgget(struct lwp *l, void *v, register_t *retval)
323 1.16 thorpej {
324 1.26 thorpej struct sys_msgget_args /* {
325 1.10 cgd syscallarg(key_t) key;
326 1.10 cgd syscallarg(int) msgflg;
327 1.16 thorpej } */ *uap = v;
328 1.48 ad int msqid, error = 0;
329 1.10 cgd int key = SCARG(uap, key);
330 1.10 cgd int msgflg = SCARG(uap, msgflg);
331 1.44 ad kauth_cred_t cred = l->l_cred;
332 1.26 thorpej struct msqid_ds *msqptr = NULL;
333 1.48 ad kmsq_t *msq;
334 1.48 ad
335 1.48 ad mutex_enter(&msgmutex);
336 1.1 cgd
337 1.20 christos MSG_PRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
338 1.1 cgd
339 1.5 mycroft if (key != IPC_PRIVATE) {
340 1.5 mycroft for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
341 1.48 ad msq = &msqs[msqid];
342 1.48 ad msqptr = &msq->msq_u;
343 1.3 mycroft if (msqptr->msg_qbytes != 0 &&
344 1.26 thorpej msqptr->msg_perm._key == key)
345 1.3 mycroft break;
346 1.3 mycroft }
347 1.3 mycroft if (msqid < msginfo.msgmni) {
348 1.20 christos MSG_PRINTF(("found public key\n"));
349 1.3 mycroft if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
350 1.20 christos MSG_PRINTF(("not exclusive\n"));
351 1.48 ad error = EEXIST;
352 1.48 ad goto unlock;
353 1.3 mycroft }
354 1.26 thorpej if ((error = ipcperm(cred, &msqptr->msg_perm,
355 1.26 thorpej msgflg & 0700 ))) {
356 1.20 christos MSG_PRINTF(("requester doesn't have 0%o access\n",
357 1.20 christos msgflg & 0700));
358 1.48 ad goto unlock;
359 1.3 mycroft }
360 1.5 mycroft goto found;
361 1.3 mycroft }
362 1.1 cgd }
363 1.1 cgd
364 1.20 christos MSG_PRINTF(("need to allocate the msqid_ds\n"));
365 1.5 mycroft if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
366 1.5 mycroft for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
367 1.5 mycroft /*
368 1.5 mycroft * Look for an unallocated and unlocked msqid_ds.
369 1.5 mycroft * msqid_ds's can be locked by msgsnd or msgrcv while
370 1.5 mycroft * they are copying the message in/out. We can't
371 1.5 mycroft * re-use the entry until they release it.
372 1.5 mycroft */
373 1.48 ad msq = &msqs[msqid];
374 1.48 ad msqptr = &msq->msq_u;
375 1.5 mycroft if (msqptr->msg_qbytes == 0 &&
376 1.5 mycroft (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
377 1.5 mycroft break;
378 1.5 mycroft }
379 1.5 mycroft if (msqid == msginfo.msgmni) {
380 1.20 christos MSG_PRINTF(("no more msqid_ds's available\n"));
381 1.48 ad error = ENOSPC;
382 1.48 ad goto unlock;
383 1.5 mycroft }
384 1.20 christos MSG_PRINTF(("msqid %d is available\n", msqid));
385 1.26 thorpej msqptr->msg_perm._key = key;
386 1.42 elad msqptr->msg_perm.cuid = kauth_cred_geteuid(cred);
387 1.42 elad msqptr->msg_perm.uid = kauth_cred_geteuid(cred);
388 1.42 elad msqptr->msg_perm.cgid = kauth_cred_getegid(cred);
389 1.42 elad msqptr->msg_perm.gid = kauth_cred_getegid(cred);
390 1.5 mycroft msqptr->msg_perm.mode = (msgflg & 0777);
391 1.5 mycroft /* Make sure that the returned msqid is unique */
392 1.26 thorpej msqptr->msg_perm._seq++;
393 1.26 thorpej msqptr->_msg_first = NULL;
394 1.26 thorpej msqptr->_msg_last = NULL;
395 1.26 thorpej msqptr->_msg_cbytes = 0;
396 1.5 mycroft msqptr->msg_qnum = 0;
397 1.5 mycroft msqptr->msg_qbytes = msginfo.msgmnb;
398 1.5 mycroft msqptr->msg_lspid = 0;
399 1.5 mycroft msqptr->msg_lrpid = 0;
400 1.5 mycroft msqptr->msg_stime = 0;
401 1.5 mycroft msqptr->msg_rtime = 0;
402 1.43 kardel msqptr->msg_ctime = time_second;
403 1.5 mycroft } else {
404 1.20 christos MSG_PRINTF(("didn't find it and wasn't asked to create it\n"));
405 1.48 ad error = ENOENT;
406 1.48 ad goto unlock;
407 1.1 cgd }
408 1.1 cgd
409 1.49.10.1 joerg found:
410 1.3 mycroft /* Construct the unique msqid */
411 1.3 mycroft *retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
412 1.48 ad
413 1.49.10.1 joerg unlock:
414 1.49.10.1 joerg mutex_exit(&msgmutex);
415 1.48 ad return (error);
416 1.1 cgd }
417 1.1 cgd
418 1.1 cgd int
419 1.46 yamt sys_msgsnd(struct lwp *l, void *v, register_t *retval)
420 1.16 thorpej {
421 1.26 thorpej struct sys_msgsnd_args /* {
422 1.10 cgd syscallarg(int) msqid;
423 1.22 kleink syscallarg(const void *) msgp;
424 1.10 cgd syscallarg(size_t) msgsz;
425 1.10 cgd syscallarg(int) msgflg;
426 1.16 thorpej } */ *uap = v;
427 1.41 cube
428 1.44 ad return msgsnd1(l, SCARG(uap, msqid), SCARG(uap, msgp),
429 1.41 cube SCARG(uap, msgsz), SCARG(uap, msgflg), sizeof(long), copyin);
430 1.41 cube }
431 1.41 cube
432 1.41 cube int
433 1.44 ad msgsnd1(struct lwp *l, int msqidr, const char *user_msgp, size_t msgsz,
434 1.41 cube int msgflg, size_t typesz, copyin_t fetch_type)
435 1.41 cube {
436 1.48 ad int segs_needed, error = 0, msqid;
437 1.44 ad kauth_cred_t cred = l->l_cred;
438 1.26 thorpej struct msqid_ds *msqptr;
439 1.26 thorpej struct __msg *msghdr;
440 1.48 ad kmsq_t *msq;
441 1.3 mycroft short next;
442 1.1 cgd
443 1.34 nathanw MSG_PRINTF(("call to msgsnd(%d, %p, %lld, %d)\n", msqid, user_msgp,
444 1.34 nathanw (long long)msgsz, msgflg));
445 1.1 cgd
446 1.41 cube msqid = IPCID_TO_IX(msqidr);
447 1.1 cgd
448 1.48 ad mutex_enter(&msgmutex);
449 1.48 ad
450 1.3 mycroft if (msqid < 0 || msqid >= msginfo.msgmni) {
451 1.20 christos MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
452 1.20 christos msginfo.msgmni));
453 1.48 ad error = EINVAL;
454 1.48 ad goto unlock;
455 1.3 mycroft }
456 1.1 cgd
457 1.48 ad msq = &msqs[msqid];
458 1.48 ad msqptr = &msq->msq_u;
459 1.48 ad
460 1.3 mycroft if (msqptr->msg_qbytes == 0) {
461 1.20 christos MSG_PRINTF(("no such message queue id\n"));
462 1.48 ad error = EINVAL;
463 1.48 ad goto unlock;
464 1.3 mycroft }
465 1.41 cube if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
466 1.20 christos MSG_PRINTF(("wrong sequence number\n"));
467 1.48 ad error = EINVAL;
468 1.48 ad goto unlock;
469 1.3 mycroft }
470 1.1 cgd
471 1.26 thorpej if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_W))) {
472 1.20 christos MSG_PRINTF(("requester doesn't have write access\n"));
473 1.48 ad goto unlock;
474 1.3 mycroft }
475 1.1 cgd
476 1.3 mycroft segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
477 1.34 nathanw MSG_PRINTF(("msgsz=%lld, msgssz=%d, segs_needed=%d\n",
478 1.34 nathanw (long long)msgsz, msginfo.msgssz, segs_needed));
479 1.3 mycroft for (;;) {
480 1.3 mycroft int need_more_resources = 0;
481 1.1 cgd
482 1.3 mycroft /*
483 1.18 christos * check msgsz [cannot be negative since it is unsigned]
484 1.3 mycroft * (inside this loop in case msg_qbytes changes while we sleep)
485 1.3 mycroft */
486 1.1 cgd
487 1.18 christos if (msgsz > msqptr->msg_qbytes) {
488 1.20 christos MSG_PRINTF(("msgsz > msqptr->msg_qbytes\n"));
489 1.48 ad error = EINVAL;
490 1.48 ad goto unlock;
491 1.3 mycroft }
492 1.1 cgd
493 1.3 mycroft if (msqptr->msg_perm.mode & MSG_LOCKED) {
494 1.20 christos MSG_PRINTF(("msqid is locked\n"));
495 1.3 mycroft need_more_resources = 1;
496 1.3 mycroft }
497 1.26 thorpej if (msgsz + msqptr->_msg_cbytes > msqptr->msg_qbytes) {
498 1.20 christos MSG_PRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
499 1.3 mycroft need_more_resources = 1;
500 1.3 mycroft }
501 1.3 mycroft if (segs_needed > nfree_msgmaps) {
502 1.20 christos MSG_PRINTF(("segs_needed > nfree_msgmaps\n"));
503 1.3 mycroft need_more_resources = 1;
504 1.3 mycroft }
505 1.3 mycroft if (free_msghdrs == NULL) {
506 1.20 christos MSG_PRINTF(("no more msghdrs\n"));
507 1.3 mycroft need_more_resources = 1;
508 1.3 mycroft }
509 1.1 cgd
510 1.3 mycroft if (need_more_resources) {
511 1.3 mycroft int we_own_it;
512 1.1 cgd
513 1.3 mycroft if ((msgflg & IPC_NOWAIT) != 0) {
514 1.26 thorpej MSG_PRINTF(("need more resources but caller "
515 1.26 thorpej "doesn't want to wait\n"));
516 1.48 ad error = EAGAIN;
517 1.48 ad goto unlock;
518 1.3 mycroft }
519 1.1 cgd
520 1.3 mycroft if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
521 1.20 christos MSG_PRINTF(("we don't own the msqid_ds\n"));
522 1.3 mycroft we_own_it = 0;
523 1.3 mycroft } else {
524 1.3 mycroft /* Force later arrivals to wait for our
525 1.3 mycroft request */
526 1.20 christos MSG_PRINTF(("we own the msqid_ds\n"));
527 1.3 mycroft msqptr->msg_perm.mode |= MSG_LOCKED;
528 1.3 mycroft we_own_it = 1;
529 1.3 mycroft }
530 1.20 christos MSG_PRINTF(("goodnight\n"));
531 1.48 ad error = cv_wait_sig(&msq->msq_cv, &msgmutex);
532 1.26 thorpej MSG_PRINTF(("good morning, error=%d\n", error));
533 1.3 mycroft if (we_own_it)
534 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
535 1.26 thorpej if (error != 0) {
536 1.26 thorpej MSG_PRINTF(("msgsnd: interrupted system "
537 1.26 thorpej "call\n"));
538 1.48 ad error = EINTR;
539 1.48 ad goto unlock;
540 1.3 mycroft }
541 1.1 cgd
542 1.3 mycroft /*
543 1.3 mycroft * Make sure that the msq queue still exists
544 1.3 mycroft */
545 1.1 cgd
546 1.3 mycroft if (msqptr->msg_qbytes == 0) {
547 1.20 christos MSG_PRINTF(("msqid deleted\n"));
548 1.48 ad error = EIDRM;
549 1.48 ad goto unlock;
550 1.3 mycroft }
551 1.3 mycroft } else {
552 1.20 christos MSG_PRINTF(("got all the resources that we need\n"));
553 1.3 mycroft break;
554 1.3 mycroft }
555 1.1 cgd }
556 1.1 cgd
557 1.3 mycroft /*
558 1.3 mycroft * We have the resources that we need.
559 1.3 mycroft * Make sure!
560 1.3 mycroft */
561 1.1 cgd
562 1.49.10.1 joerg KASSERT((msqptr->msg_perm.mode & MSG_LOCKED) == 0);
563 1.49.10.1 joerg KASSERT(segs_needed <= nfree_msgmaps);
564 1.49.10.1 joerg KASSERT(msgsz + msqptr->_msg_cbytes <= msqptr->msg_qbytes);
565 1.49.10.1 joerg KASSERT(free_msghdrs != NULL);
566 1.1 cgd
567 1.3 mycroft /*
568 1.3 mycroft * Re-lock the msqid_ds in case we page-fault when copying in the
569 1.3 mycroft * message
570 1.3 mycroft */
571 1.1 cgd
572 1.49.10.1 joerg KASSERT((msqptr->msg_perm.mode & MSG_LOCKED) == 0);
573 1.3 mycroft msqptr->msg_perm.mode |= MSG_LOCKED;
574 1.1 cgd
575 1.3 mycroft /*
576 1.3 mycroft * Allocate a message header
577 1.3 mycroft */
578 1.1 cgd
579 1.3 mycroft msghdr = free_msghdrs;
580 1.3 mycroft free_msghdrs = msghdr->msg_next;
581 1.3 mycroft msghdr->msg_spot = -1;
582 1.3 mycroft msghdr->msg_ts = msgsz;
583 1.1 cgd
584 1.3 mycroft /*
585 1.3 mycroft * Allocate space for the message
586 1.3 mycroft */
587 1.1 cgd
588 1.3 mycroft while (segs_needed > 0) {
589 1.49.10.1 joerg KASSERT(nfree_msgmaps > 0);
590 1.49.10.1 joerg KASSERT(free_msgmaps != -1);
591 1.49.10.1 joerg KASSERT(free_msgmaps < msginfo.msgseg);
592 1.49.10.1 joerg
593 1.3 mycroft next = free_msgmaps;
594 1.20 christos MSG_PRINTF(("allocating segment %d to message\n", next));
595 1.3 mycroft free_msgmaps = msgmaps[next].next;
596 1.5 mycroft nfree_msgmaps--;
597 1.3 mycroft msgmaps[next].next = msghdr->msg_spot;
598 1.3 mycroft msghdr->msg_spot = next;
599 1.5 mycroft segs_needed--;
600 1.1 cgd }
601 1.1 cgd
602 1.3 mycroft /*
603 1.3 mycroft * Copy in the message type
604 1.3 mycroft */
605 1.48 ad mutex_exit(&msgmutex);
606 1.48 ad error = (*fetch_type)(user_msgp, &msghdr->msg_type, typesz);
607 1.48 ad mutex_enter(&msgmutex);
608 1.48 ad if (error != 0) {
609 1.26 thorpej MSG_PRINTF(("error %d copying the message type\n", error));
610 1.3 mycroft msg_freehdr(msghdr);
611 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
612 1.48 ad cv_broadcast(&msq->msq_cv);
613 1.48 ad goto unlock;
614 1.3 mycroft }
615 1.41 cube user_msgp += typesz;
616 1.1 cgd
617 1.3 mycroft /*
618 1.3 mycroft * Validate the message type
619 1.3 mycroft */
620 1.1 cgd
621 1.3 mycroft if (msghdr->msg_type < 1) {
622 1.3 mycroft msg_freehdr(msghdr);
623 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
624 1.48 ad cv_broadcast(&msq->msq_cv);
625 1.34 nathanw MSG_PRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
626 1.48 ad goto unlock;
627 1.3 mycroft }
628 1.1 cgd
629 1.3 mycroft /*
630 1.3 mycroft * Copy in the message body
631 1.3 mycroft */
632 1.3 mycroft
633 1.3 mycroft next = msghdr->msg_spot;
634 1.3 mycroft while (msgsz > 0) {
635 1.3 mycroft size_t tlen;
636 1.49.10.1 joerg KASSERT(next > -1);
637 1.49.10.1 joerg KASSERT(next < msginfo.msgseg);
638 1.49.10.1 joerg
639 1.3 mycroft if (msgsz > msginfo.msgssz)
640 1.3 mycroft tlen = msginfo.msgssz;
641 1.3 mycroft else
642 1.3 mycroft tlen = msgsz;
643 1.48 ad mutex_exit(&msgmutex);
644 1.48 ad error = copyin(user_msgp, &msgpool[next * msginfo.msgssz], tlen);
645 1.48 ad mutex_enter(&msgmutex);
646 1.48 ad if (error != 0) {
647 1.26 thorpej MSG_PRINTF(("error %d copying in message segment\n",
648 1.26 thorpej error));
649 1.3 mycroft msg_freehdr(msghdr);
650 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
651 1.48 ad cv_broadcast(&msq->msq_cv);
652 1.48 ad goto unlock;
653 1.3 mycroft }
654 1.3 mycroft msgsz -= tlen;
655 1.3 mycroft user_msgp += tlen;
656 1.3 mycroft next = msgmaps[next].next;
657 1.1 cgd }
658 1.49.10.1 joerg KASSERT(next == -1);
659 1.1 cgd
660 1.3 mycroft /*
661 1.3 mycroft * We've got the message. Unlock the msqid_ds.
662 1.3 mycroft */
663 1.1 cgd
664 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
665 1.1 cgd
666 1.3 mycroft /*
667 1.3 mycroft * Make sure that the msqid_ds is still allocated.
668 1.3 mycroft */
669 1.1 cgd
670 1.3 mycroft if (msqptr->msg_qbytes == 0) {
671 1.3 mycroft msg_freehdr(msghdr);
672 1.48 ad cv_broadcast(&msq->msq_cv);
673 1.48 ad error = EIDRM;
674 1.48 ad goto unlock;
675 1.3 mycroft }
676 1.3 mycroft
677 1.3 mycroft /*
678 1.3 mycroft * Put the message into the queue
679 1.3 mycroft */
680 1.1 cgd
681 1.26 thorpej if (msqptr->_msg_first == NULL) {
682 1.26 thorpej msqptr->_msg_first = msghdr;
683 1.26 thorpej msqptr->_msg_last = msghdr;
684 1.3 mycroft } else {
685 1.26 thorpej msqptr->_msg_last->msg_next = msghdr;
686 1.26 thorpej msqptr->_msg_last = msghdr;
687 1.3 mycroft }
688 1.26 thorpej msqptr->_msg_last->msg_next = NULL;
689 1.3 mycroft
690 1.26 thorpej msqptr->_msg_cbytes += msghdr->msg_ts;
691 1.5 mycroft msqptr->msg_qnum++;
692 1.44 ad msqptr->msg_lspid = l->l_proc->p_pid;
693 1.43 kardel msqptr->msg_stime = time_second;
694 1.3 mycroft
695 1.48 ad cv_broadcast(&msq->msq_cv);
696 1.48 ad
697 1.49.10.1 joerg unlock:
698 1.49.10.1 joerg mutex_exit(&msgmutex);
699 1.48 ad return error;
700 1.1 cgd }
701 1.1 cgd
702 1.1 cgd int
703 1.40 thorpej sys_msgrcv(struct lwp *l, void *v, register_t *retval)
704 1.16 thorpej {
705 1.27 augustss struct sys_msgrcv_args /* {
706 1.10 cgd syscallarg(int) msqid;
707 1.10 cgd syscallarg(void *) msgp;
708 1.10 cgd syscallarg(size_t) msgsz;
709 1.10 cgd syscallarg(long) msgtyp;
710 1.10 cgd syscallarg(int) msgflg;
711 1.16 thorpej } */ *uap = v;
712 1.41 cube
713 1.44 ad return msgrcv1(l, SCARG(uap, msqid), SCARG(uap, msgp),
714 1.41 cube SCARG(uap, msgsz), SCARG(uap, msgtyp), SCARG(uap, msgflg),
715 1.41 cube sizeof(long), copyout, retval);
716 1.41 cube }
717 1.41 cube
718 1.41 cube int
719 1.44 ad msgrcv1(struct lwp *l, int msqidr, char *user_msgp, size_t msgsz, long msgtyp,
720 1.41 cube int msgflg, size_t typesz, copyout_t put_type, register_t *retval)
721 1.41 cube {
722 1.3 mycroft size_t len;
723 1.44 ad kauth_cred_t cred = l->l_cred;
724 1.27 augustss struct msqid_ds *msqptr;
725 1.27 augustss struct __msg *msghdr;
726 1.48 ad int error = 0, msqid;
727 1.48 ad kmsq_t *msq;
728 1.3 mycroft short next;
729 1.1 cgd
730 1.34 nathanw MSG_PRINTF(("call to msgrcv(%d, %p, %lld, %ld, %d)\n", msqid,
731 1.34 nathanw user_msgp, (long long)msgsz, msgtyp, msgflg));
732 1.1 cgd
733 1.41 cube msqid = IPCID_TO_IX(msqidr);
734 1.1 cgd
735 1.48 ad mutex_enter(&msgmutex);
736 1.48 ad
737 1.3 mycroft if (msqid < 0 || msqid >= msginfo.msgmni) {
738 1.20 christos MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
739 1.20 christos msginfo.msgmni));
740 1.48 ad error = EINVAL;
741 1.48 ad goto unlock;
742 1.3 mycroft }
743 1.1 cgd
744 1.48 ad msq = &msqs[msqid];
745 1.48 ad msqptr = &msq->msq_u;
746 1.48 ad
747 1.3 mycroft if (msqptr->msg_qbytes == 0) {
748 1.20 christos MSG_PRINTF(("no such message queue id\n"));
749 1.48 ad error = EINVAL;
750 1.48 ad goto unlock;
751 1.3 mycroft }
752 1.41 cube if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
753 1.20 christos MSG_PRINTF(("wrong sequence number\n"));
754 1.48 ad error = EINVAL;
755 1.48 ad goto unlock;
756 1.3 mycroft }
757 1.1 cgd
758 1.26 thorpej if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
759 1.20 christos MSG_PRINTF(("requester doesn't have read access\n"));
760 1.48 ad goto unlock;
761 1.3 mycroft }
762 1.1 cgd
763 1.3 mycroft msghdr = NULL;
764 1.3 mycroft while (msghdr == NULL) {
765 1.3 mycroft if (msgtyp == 0) {
766 1.26 thorpej msghdr = msqptr->_msg_first;
767 1.3 mycroft if (msghdr != NULL) {
768 1.3 mycroft if (msgsz < msghdr->msg_ts &&
769 1.3 mycroft (msgflg & MSG_NOERROR) == 0) {
770 1.49.10.1 joerg MSG_PRINTF(("first msg on the queue "
771 1.49.10.1 joerg "is too big (want %lld, got %d)\n",
772 1.34 nathanw (long long)msgsz, msghdr->msg_ts));
773 1.48 ad error = E2BIG;
774 1.48 ad goto unlock;
775 1.3 mycroft }
776 1.26 thorpej if (msqptr->_msg_first == msqptr->_msg_last) {
777 1.26 thorpej msqptr->_msg_first = NULL;
778 1.26 thorpej msqptr->_msg_last = NULL;
779 1.3 mycroft } else {
780 1.26 thorpej msqptr->_msg_first = msghdr->msg_next;
781 1.49.10.1 joerg KASSERT(msqptr->_msg_first != NULL);
782 1.3 mycroft }
783 1.3 mycroft }
784 1.3 mycroft } else {
785 1.26 thorpej struct __msg *previous;
786 1.26 thorpej struct __msg **prev;
787 1.1 cgd
788 1.26 thorpej for (previous = NULL, prev = &msqptr->_msg_first;
789 1.12 mycroft (msghdr = *prev) != NULL;
790 1.12 mycroft previous = msghdr, prev = &msghdr->msg_next) {
791 1.3 mycroft /*
792 1.3 mycroft * Is this message's type an exact match or is
793 1.3 mycroft * this message's type less than or equal to
794 1.3 mycroft * the absolute value of a negative msgtyp?
795 1.3 mycroft * Note that the second half of this test can
796 1.3 mycroft * NEVER be true if msgtyp is positive since
797 1.3 mycroft * msg_type is always positive!
798 1.3 mycroft */
799 1.3 mycroft
800 1.49.10.1 joerg if (msgtyp != msghdr->msg_type &&
801 1.49.10.1 joerg msghdr->msg_type > -msgtyp)
802 1.49.10.1 joerg continue;
803 1.49.10.1 joerg
804 1.49.10.1 joerg MSG_PRINTF(("found message type %ld, requested %ld\n",
805 1.49.10.1 joerg msghdr->msg_type, msgtyp));
806 1.49.10.1 joerg if (msgsz < msghdr->msg_ts &&
807 1.49.10.1 joerg (msgflg & MSG_NOERROR) == 0) {
808 1.49.10.1 joerg MSG_PRINTF(("requested message on the queue "
809 1.49.10.1 joerg "is too big (want %lld, got %d)\n",
810 1.49.10.1 joerg (long long)msgsz, msghdr->msg_ts));
811 1.49.10.1 joerg error = E2BIG;
812 1.49.10.1 joerg goto unlock;
813 1.49.10.1 joerg }
814 1.49.10.1 joerg *prev = msghdr->msg_next;
815 1.49.10.1 joerg if (msghdr != msqptr->_msg_last)
816 1.3 mycroft break;
817 1.49.10.1 joerg if (previous == NULL) {
818 1.49.10.1 joerg KASSERT(prev == &msqptr->_msg_first);
819 1.49.10.1 joerg msqptr->_msg_first = NULL;
820 1.49.10.1 joerg msqptr->_msg_last = NULL;
821 1.49.10.1 joerg } else {
822 1.49.10.1 joerg KASSERT(prev != &msqptr->_msg_first);
823 1.49.10.1 joerg msqptr->_msg_last = previous;
824 1.3 mycroft }
825 1.49.10.1 joerg break;
826 1.3 mycroft }
827 1.1 cgd }
828 1.1 cgd
829 1.3 mycroft /*
830 1.3 mycroft * We've either extracted the msghdr for the appropriate
831 1.3 mycroft * message or there isn't one.
832 1.3 mycroft * If there is one then bail out of this loop.
833 1.3 mycroft */
834 1.3 mycroft if (msghdr != NULL)
835 1.3 mycroft break;
836 1.1 cgd
837 1.1 cgd /*
838 1.3 mycroft * Hmph! No message found. Does the user want to wait?
839 1.1 cgd */
840 1.1 cgd
841 1.3 mycroft if ((msgflg & IPC_NOWAIT) != 0) {
842 1.34 nathanw MSG_PRINTF(("no appropriate message found (msgtyp=%ld)\n",
843 1.20 christos msgtyp));
844 1.48 ad error = ENOMSG;
845 1.48 ad goto unlock;
846 1.3 mycroft }
847 1.1 cgd
848 1.3 mycroft /*
849 1.3 mycroft * Wait for something to happen
850 1.3 mycroft */
851 1.1 cgd
852 1.20 christos MSG_PRINTF(("msgrcv: goodnight\n"));
853 1.48 ad error = cv_wait_sig(&msq->msq_cv, &msgmutex);
854 1.26 thorpej MSG_PRINTF(("msgrcv: good morning (error=%d)\n", error));
855 1.1 cgd
856 1.26 thorpej if (error != 0) {
857 1.26 thorpej MSG_PRINTF(("msgsnd: interrupted system call\n"));
858 1.48 ad error = EINTR;
859 1.48 ad goto unlock;
860 1.3 mycroft }
861 1.1 cgd
862 1.3 mycroft /*
863 1.3 mycroft * Make sure that the msq queue still exists
864 1.3 mycroft */
865 1.1 cgd
866 1.3 mycroft if (msqptr->msg_qbytes == 0 ||
867 1.41 cube msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
868 1.20 christos MSG_PRINTF(("msqid deleted\n"));
869 1.48 ad error = EIDRM;
870 1.48 ad goto unlock;
871 1.3 mycroft }
872 1.1 cgd }
873 1.1 cgd
874 1.3 mycroft /*
875 1.3 mycroft * Return the message to the user.
876 1.3 mycroft *
877 1.3 mycroft * First, do the bookkeeping (before we risk being interrupted).
878 1.3 mycroft */
879 1.1 cgd
880 1.26 thorpej msqptr->_msg_cbytes -= msghdr->msg_ts;
881 1.5 mycroft msqptr->msg_qnum--;
882 1.44 ad msqptr->msg_lrpid = l->l_proc->p_pid;
883 1.43 kardel msqptr->msg_rtime = time_second;
884 1.1 cgd
885 1.3 mycroft /*
886 1.3 mycroft * Make msgsz the actual amount that we'll be returning.
887 1.3 mycroft * Note that this effectively truncates the message if it is too long
888 1.3 mycroft * (since msgsz is never increased).
889 1.3 mycroft */
890 1.1 cgd
891 1.34 nathanw MSG_PRINTF(("found a message, msgsz=%lld, msg_ts=%d\n",
892 1.34 nathanw (long long)msgsz, msghdr->msg_ts));
893 1.3 mycroft if (msgsz > msghdr->msg_ts)
894 1.3 mycroft msgsz = msghdr->msg_ts;
895 1.1 cgd
896 1.3 mycroft /*
897 1.3 mycroft * Return the type to the user.
898 1.3 mycroft */
899 1.48 ad mutex_exit(&msgmutex);
900 1.41 cube error = (*put_type)(&msghdr->msg_type, user_msgp, typesz);
901 1.48 ad mutex_enter(&msgmutex);
902 1.26 thorpej if (error != 0) {
903 1.26 thorpej MSG_PRINTF(("error (%d) copying out message type\n", error));
904 1.3 mycroft msg_freehdr(msghdr);
905 1.48 ad cv_broadcast(&msq->msq_cv);
906 1.48 ad goto unlock;
907 1.3 mycroft }
908 1.41 cube user_msgp += typesz;
909 1.3 mycroft
910 1.3 mycroft /*
911 1.3 mycroft * Return the segments to the user
912 1.3 mycroft */
913 1.1 cgd
914 1.3 mycroft next = msghdr->msg_spot;
915 1.3 mycroft for (len = 0; len < msgsz; len += msginfo.msgssz) {
916 1.3 mycroft size_t tlen;
917 1.49.10.1 joerg KASSERT(next > -1);
918 1.49.10.1 joerg KASSERT(next < msginfo.msgseg);
919 1.3 mycroft
920 1.25 mrg if (msgsz - len > msginfo.msgssz)
921 1.3 mycroft tlen = msginfo.msgssz;
922 1.3 mycroft else
923 1.25 mrg tlen = msgsz - len;
924 1.48 ad mutex_exit(&msgmutex);
925 1.26 thorpej error = copyout(&msgpool[next * msginfo.msgssz],
926 1.3 mycroft user_msgp, tlen);
927 1.48 ad mutex_enter(&msgmutex);
928 1.26 thorpej if (error != 0) {
929 1.20 christos MSG_PRINTF(("error (%d) copying out message segment\n",
930 1.26 thorpej error));
931 1.3 mycroft msg_freehdr(msghdr);
932 1.48 ad cv_broadcast(&msq->msq_cv);
933 1.48 ad goto unlock;
934 1.3 mycroft }
935 1.3 mycroft user_msgp += tlen;
936 1.3 mycroft next = msgmaps[next].next;
937 1.1 cgd }
938 1.1 cgd
939 1.3 mycroft /*
940 1.3 mycroft * Done, return the actual number of bytes copied out.
941 1.3 mycroft */
942 1.1 cgd
943 1.3 mycroft msg_freehdr(msghdr);
944 1.48 ad cv_broadcast(&msq->msq_cv);
945 1.3 mycroft *retval = msgsz;
946 1.48 ad
947 1.49.10.1 joerg unlock:
948 1.49.10.1 joerg mutex_exit(&msgmutex);
949 1.48 ad return error;
950 1.1 cgd }
951