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