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