Home | History | Annotate | Line # | Download | only in kern
sysv_msg.c revision 1.44.4.1
      1 /*	$NetBSD: sysv_msg.c,v 1.44.4.1 2006/09/11 18:47:17 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 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.
     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.1 2006/09/11 18:47:17 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 			return (error);
    284 		if (msqbuf->msg_qbytes > msqptr->msg_qbytes &&
    285 		    kauth_cred_geteuid(cred) != 0)
    286 			return (EPERM);
    287 		if (msqbuf->msg_qbytes > msginfo.msgmnb) {
    288 			MSG_PRINTF(("can't increase msg_qbytes beyond %d "
    289 			    "(truncating)\n", msginfo.msgmnb));
    290 			/* silently restrict qbytes to system limit */
    291 			msqbuf->msg_qbytes = msginfo.msgmnb;
    292 		}
    293 		if (msqbuf->msg_qbytes == 0) {
    294 			MSG_PRINTF(("can't reduce msg_qbytes to 0\n"));
    295 			error = EINVAL;		/* XXX non-standard errno! */
    296 			break;
    297 		}
    298 		msqptr->msg_perm.uid = msqbuf->msg_perm.uid;
    299 		msqptr->msg_perm.gid = msqbuf->msg_perm.gid;
    300 		msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
    301 		    (msqbuf->msg_perm.mode & 0777);
    302 		msqptr->msg_qbytes = msqbuf->msg_qbytes;
    303 		msqptr->msg_ctime = time_second;
    304 		break;
    305 
    306 	case IPC_STAT:
    307 		if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
    308 			MSG_PRINTF(("requester doesn't have read access\n"));
    309 			return (error);
    310 		}
    311 		memcpy(msqbuf, msqptr, sizeof(struct msqid_ds));
    312 		break;
    313 
    314 	default:
    315 		MSG_PRINTF(("invalid command %d\n", cmd));
    316 		error = EINVAL;
    317 		break;
    318 	}
    319 
    320  unlock:
    321 	mutex_exit(&msgmutex);
    322 	return (error);
    323 }
    324 
    325 int
    326 sys_msgget(struct lwp *l, void *v, register_t *retval)
    327 {
    328 	struct sys_msgget_args /* {
    329 		syscallarg(key_t) key;
    330 		syscallarg(int) msgflg;
    331 	} */ *uap = v;
    332 	int msqid, error = 0;
    333 	int key = SCARG(uap, key);
    334 	int msgflg = SCARG(uap, msgflg);
    335 	kauth_cred_t cred = l->l_cred;
    336 	struct msqid_ds *msqptr = NULL;
    337 
    338 	mutex_enter(&msgmutex);
    339 
    340 	MSG_PRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
    341 
    342 	if (key != IPC_PRIVATE) {
    343 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
    344 			msqptr = &msqids[msqid];
    345 			if (msqptr->msg_qbytes != 0 &&
    346 			    msqptr->msg_perm._key == key)
    347 				break;
    348 		}
    349 		if (msqid < msginfo.msgmni) {
    350 			MSG_PRINTF(("found public key\n"));
    351 			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
    352 				MSG_PRINTF(("not exclusive\n"));
    353 				error = EEXIST;
    354 				goto unlock;
    355 			}
    356 			if ((error = ipcperm(cred, &msqptr->msg_perm,
    357 			    msgflg & 0700 ))) {
    358 				MSG_PRINTF(("requester doesn't have 0%o access\n",
    359 				    msgflg & 0700));
    360 				goto unlock;
    361 			}
    362 			goto found;
    363 		}
    364 	}
    365 
    366 	MSG_PRINTF(("need to allocate the msqid_ds\n"));
    367 	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
    368 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
    369 			/*
    370 			 * Look for an unallocated and unlocked msqid_ds.
    371 			 * msqid_ds's can be locked by msgsnd or msgrcv while
    372 			 * they are copying the message in/out.  We can't
    373 			 * re-use the entry until they release it.
    374 			 */
    375 			msqptr = &msqids[msqid];
    376 			if (msqptr->msg_qbytes == 0 &&
    377 			    (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
    378 				break;
    379 		}
    380 		if (msqid == msginfo.msgmni) {
    381 			MSG_PRINTF(("no more msqid_ds's available\n"));
    382 			error = ENOSPC;
    383 			goto unlock;
    384 		}
    385 		MSG_PRINTF(("msqid %d is available\n", msqid));
    386 		msqptr->msg_perm._key = key;
    387 		msqptr->msg_perm.cuid = kauth_cred_geteuid(cred);
    388 		msqptr->msg_perm.uid = kauth_cred_geteuid(cred);
    389 		msqptr->msg_perm.cgid = kauth_cred_getegid(cred);
    390 		msqptr->msg_perm.gid = kauth_cred_getegid(cred);
    391 		msqptr->msg_perm.mode = (msgflg & 0777);
    392 		/* Make sure that the returned msqid is unique */
    393 		msqptr->msg_perm._seq++;
    394 		msqptr->_msg_first = NULL;
    395 		msqptr->_msg_last = NULL;
    396 		msqptr->_msg_cbytes = 0;
    397 		msqptr->msg_qnum = 0;
    398 		msqptr->msg_qbytes = msginfo.msgmnb;
    399 		msqptr->msg_lspid = 0;
    400 		msqptr->msg_lrpid = 0;
    401 		msqptr->msg_stime = 0;
    402 		msqptr->msg_rtime = 0;
    403 		msqptr->msg_ctime = time_second;
    404 	} else {
    405 		MSG_PRINTF(("didn't find it and wasn't asked to create it\n"));
    406 		error = ENOENT;
    407 		goto unlock;
    408 	}
    409 
    410  found:
    411 	/* Construct the unique msqid */
    412 	*retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
    413 
    414  unlock:
    415   	mutex_exit(&msgmutex);
    416 	return (error);
    417 }
    418 
    419 int
    420 sys_msgsnd(struct lwp *l, void *v, register_t *retval)
    421 {
    422 	struct sys_msgsnd_args /* {
    423 		syscallarg(int) msqid;
    424 		syscallarg(const void *) msgp;
    425 		syscallarg(size_t) msgsz;
    426 		syscallarg(int) msgflg;
    427 	} */ *uap = v;
    428 
    429 	return msgsnd1(l, SCARG(uap, msqid), SCARG(uap, msgp),
    430 	    SCARG(uap, msgsz), SCARG(uap, msgflg), sizeof(long), copyin);
    431 }
    432 
    433 int
    434 msgsnd1(struct lwp *l, int msqidr, const char *user_msgp, size_t msgsz,
    435     int msgflg, size_t typesz, copyin_t fetch_type)
    436 {
    437 	int segs_needed, error = 0, msqid;
    438 	kauth_cred_t cred = l->l_cred;
    439 	struct msqid_ds *msqptr;
    440 	struct __msg *msghdr;
    441 	short next;
    442 
    443 	MSG_PRINTF(("call to msgsnd(%d, %p, %lld, %d)\n", msqid, user_msgp,
    444 	    (long long)msgsz, msgflg));
    445 
    446 	msqid = IPCID_TO_IX(msqidr);
    447 
    448 	mutex_enter(&msgmutex);
    449 
    450 	if (msqid < 0 || msqid >= msginfo.msgmni) {
    451 		MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
    452 		    msginfo.msgmni));
    453 		error = EINVAL;
    454 		goto unlock;
    455 	}
    456 
    457 	msqptr = &msqids[msqid];
    458 	if (msqptr->msg_qbytes == 0) {
    459 		MSG_PRINTF(("no such message queue id\n"));
    460 		error = EINVAL;
    461 		goto unlock;
    462 	}
    463 	if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
    464 		MSG_PRINTF(("wrong sequence number\n"));
    465 		error = EINVAL;
    466 		goto unlock;
    467 	}
    468 
    469 	if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_W))) {
    470 		MSG_PRINTF(("requester doesn't have write access\n"));
    471 		goto unlock;
    472 	}
    473 
    474 	segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
    475 	MSG_PRINTF(("msgsz=%lld, msgssz=%d, segs_needed=%d\n",
    476 	    (long long)msgsz, msginfo.msgssz, segs_needed));
    477 	for (;;) {
    478 		int need_more_resources = 0;
    479 
    480 		/*
    481 		 * check msgsz [cannot be negative since it is unsigned]
    482 		 * (inside this loop in case msg_qbytes changes while we sleep)
    483 		 */
    484 
    485 		if (msgsz > msqptr->msg_qbytes) {
    486 			MSG_PRINTF(("msgsz > msqptr->msg_qbytes\n"));
    487 			error = EINVAL;
    488 			goto unlock;
    489 		}
    490 
    491 		if (msqptr->msg_perm.mode & MSG_LOCKED) {
    492 			MSG_PRINTF(("msqid is locked\n"));
    493 			need_more_resources = 1;
    494 		}
    495 		if (msgsz + msqptr->_msg_cbytes > msqptr->msg_qbytes) {
    496 			MSG_PRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
    497 			need_more_resources = 1;
    498 		}
    499 		if (segs_needed > nfree_msgmaps) {
    500 			MSG_PRINTF(("segs_needed > nfree_msgmaps\n"));
    501 			need_more_resources = 1;
    502 		}
    503 		if (free_msghdrs == NULL) {
    504 			MSG_PRINTF(("no more msghdrs\n"));
    505 			need_more_resources = 1;
    506 		}
    507 
    508 		if (need_more_resources) {
    509 			int we_own_it;
    510 
    511 			if ((msgflg & IPC_NOWAIT) != 0) {
    512 				MSG_PRINTF(("need more resources but caller "
    513 				    "doesn't want to wait\n"));
    514 				error = EAGAIN;
    515 				goto unlock;
    516 			}
    517 
    518 			if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
    519 				MSG_PRINTF(("we don't own the msqid_ds\n"));
    520 				we_own_it = 0;
    521 			} else {
    522 				/* Force later arrivals to wait for our
    523 				   request */
    524 				MSG_PRINTF(("we own the msqid_ds\n"));
    525 				msqptr->msg_perm.mode |= MSG_LOCKED;
    526 				we_own_it = 1;
    527 			}
    528 			MSG_PRINTF(("goodnight\n"));
    529 			error = mtsleep(msqptr, (PZERO - 4) | PCATCH,
    530 			    "msgwait", 0, &msgmutex);
    531 			MSG_PRINTF(("good morning, error=%d\n", error));
    532 			if (we_own_it)
    533 				msqptr->msg_perm.mode &= ~MSG_LOCKED;
    534 			if (error != 0) {
    535 				MSG_PRINTF(("msgsnd: interrupted system "
    536 				    "call\n"));
    537 				error = EINTR;
    538 				goto unlock;
    539 			}
    540 
    541 			/*
    542 			 * Make sure that the msq queue still exists
    543 			 */
    544 
    545 			if (msqptr->msg_qbytes == 0) {
    546 				MSG_PRINTF(("msqid deleted\n"));
    547 				error = EIDRM;
    548 				goto unlock;
    549 			}
    550 		} else {
    551 			MSG_PRINTF(("got all the resources that we need\n"));
    552 			break;
    553 		}
    554 	}
    555 
    556 	/*
    557 	 * We have the resources that we need.
    558 	 * Make sure!
    559 	 */
    560 
    561 	if (msqptr->msg_perm.mode & MSG_LOCKED)
    562 		panic("msg_perm.mode & MSG_LOCKED");
    563 	if (segs_needed > nfree_msgmaps)
    564 		panic("segs_needed > nfree_msgmaps");
    565 	if (msgsz + msqptr->_msg_cbytes > msqptr->msg_qbytes)
    566 		panic("msgsz + msg_cbytes > msg_qbytes");
    567 	if (free_msghdrs == NULL)
    568 		panic("no more msghdrs");
    569 
    570 	/*
    571 	 * Re-lock the msqid_ds in case we page-fault when copying in the
    572 	 * message
    573 	 */
    574 
    575 	if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
    576 		panic("msqid_ds is already locked");
    577 	msqptr->msg_perm.mode |= MSG_LOCKED;
    578 
    579 	/*
    580 	 * Allocate a message header
    581 	 */
    582 
    583 	msghdr = free_msghdrs;
    584 	free_msghdrs = msghdr->msg_next;
    585 	msghdr->msg_spot = -1;
    586 	msghdr->msg_ts = msgsz;
    587 
    588 	/*
    589 	 * Allocate space for the message
    590 	 */
    591 
    592 	while (segs_needed > 0) {
    593 		if (nfree_msgmaps <= 0)
    594 			panic("not enough msgmaps");
    595 		if (free_msgmaps == -1)
    596 			panic("nil free_msgmaps");
    597 		next = free_msgmaps;
    598 		if (next <= -1)
    599 			panic("next too low #1");
    600 		if (next >= msginfo.msgseg)
    601 			panic("next out of range #1");
    602 		MSG_PRINTF(("allocating segment %d to message\n", next));
    603 		free_msgmaps = msgmaps[next].next;
    604 		nfree_msgmaps--;
    605 		msgmaps[next].next = msghdr->msg_spot;
    606 		msghdr->msg_spot = next;
    607 		segs_needed--;
    608 	}
    609 
    610 	/*
    611 	 * Copy in the message type
    612 	 */
    613 	mutex_exit(&msgmutex);
    614 	error = (*fetch_type)(user_msgp, &msghdr->msg_type, typesz);
    615 	mutex_enter(&msgmutex);
    616 	if (error != 0) {
    617 		MSG_PRINTF(("error %d copying the message type\n", error));
    618 		msg_freehdr(msghdr);
    619 		msqptr->msg_perm.mode &= ~MSG_LOCKED;
    620 		wakeup(msqptr);
    621 		goto unlock;
    622 	}
    623 	user_msgp += typesz;
    624 
    625 	/*
    626 	 * Validate the message type
    627 	 */
    628 
    629 	if (msghdr->msg_type < 1) {
    630 		msg_freehdr(msghdr);
    631 		msqptr->msg_perm.mode &= ~MSG_LOCKED;
    632 		wakeup(msqptr);
    633 		MSG_PRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
    634 		goto unlock;
    635 	}
    636 
    637 	/*
    638 	 * Copy in the message body
    639 	 */
    640 
    641 	next = msghdr->msg_spot;
    642 	while (msgsz > 0) {
    643 		size_t tlen;
    644 		if (msgsz > msginfo.msgssz)
    645 			tlen = msginfo.msgssz;
    646 		else
    647 			tlen = msgsz;
    648 		if (next <= -1)
    649 			panic("next too low #2");
    650 		if (next >= msginfo.msgseg)
    651 			panic("next out of range #2");
    652 		mutex_exit(&msgmutex);
    653 		error = copyin(user_msgp, &msgpool[next * msginfo.msgssz], tlen);
    654 		mutex_enter(&msgmutex);
    655 		if (error != 0) {
    656 			MSG_PRINTF(("error %d copying in message segment\n",
    657 			    error));
    658 			msg_freehdr(msghdr);
    659 			msqptr->msg_perm.mode &= ~MSG_LOCKED;
    660 			wakeup(msqptr);
    661 			goto unlock;
    662 		}
    663 		msgsz -= tlen;
    664 		user_msgp += tlen;
    665 		next = msgmaps[next].next;
    666 	}
    667 	if (next != -1)
    668 		panic("didn't use all the msg segments");
    669 
    670 	/*
    671 	 * We've got the message.  Unlock the msqid_ds.
    672 	 */
    673 
    674 	msqptr->msg_perm.mode &= ~MSG_LOCKED;
    675 
    676 	/*
    677 	 * Make sure that the msqid_ds is still allocated.
    678 	 */
    679 
    680 	if (msqptr->msg_qbytes == 0) {
    681 		msg_freehdr(msghdr);
    682 		wakeup(msqptr);
    683 		error = EIDRM;
    684 		goto unlock;
    685 	}
    686 
    687 	/*
    688 	 * Put the message into the queue
    689 	 */
    690 
    691 	if (msqptr->_msg_first == NULL) {
    692 		msqptr->_msg_first = msghdr;
    693 		msqptr->_msg_last = msghdr;
    694 	} else {
    695 		msqptr->_msg_last->msg_next = msghdr;
    696 		msqptr->_msg_last = msghdr;
    697 	}
    698 	msqptr->_msg_last->msg_next = NULL;
    699 
    700 	msqptr->_msg_cbytes += msghdr->msg_ts;
    701 	msqptr->msg_qnum++;
    702 	msqptr->msg_lspid = l->l_proc->p_pid;
    703 	msqptr->msg_stime = time_second;
    704 
    705 	wakeup(msqptr);
    706 
    707  unlock:
    708  	mutex_exit(&msgmutex);
    709 	return error;
    710 }
    711 
    712 int
    713 sys_msgrcv(struct lwp *l, void *v, register_t *retval)
    714 {
    715 	struct sys_msgrcv_args /* {
    716 		syscallarg(int) msqid;
    717 		syscallarg(void *) msgp;
    718 		syscallarg(size_t) msgsz;
    719 		syscallarg(long) msgtyp;
    720 		syscallarg(int) msgflg;
    721 	} */ *uap = v;
    722 
    723 	return msgrcv1(l, SCARG(uap, msqid), SCARG(uap, msgp),
    724 	    SCARG(uap, msgsz), SCARG(uap, msgtyp), SCARG(uap, msgflg),
    725 	    sizeof(long), copyout, retval);
    726 }
    727 
    728 int
    729 msgrcv1(struct lwp *l, int msqidr, char *user_msgp, size_t msgsz, long msgtyp,
    730     int msgflg, size_t typesz, copyout_t put_type, register_t *retval)
    731 {
    732 	size_t len;
    733 	kauth_cred_t cred = l->l_cred;
    734 	struct msqid_ds *msqptr;
    735 	struct __msg *msghdr;
    736 	int error = 0, msqid;
    737 	short next;
    738 
    739 	MSG_PRINTF(("call to msgrcv(%d, %p, %lld, %ld, %d)\n", msqid,
    740 	    user_msgp, (long long)msgsz, msgtyp, msgflg));
    741 
    742 	msqid = IPCID_TO_IX(msqidr);
    743 
    744 	mutex_enter(&msgmutex);
    745 
    746 	if (msqid < 0 || msqid >= msginfo.msgmni) {
    747 		MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
    748 		    msginfo.msgmni));
    749 		error = EINVAL;
    750 		goto unlock;
    751 	}
    752 
    753 	msqptr = &msqids[msqid];
    754 	if (msqptr->msg_qbytes == 0) {
    755 		MSG_PRINTF(("no such message queue id\n"));
    756 		error = EINVAL;
    757 		goto unlock;
    758 	}
    759 	if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
    760 		MSG_PRINTF(("wrong sequence number\n"));
    761 		error = EINVAL;
    762 		goto unlock;
    763 	}
    764 
    765 	if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
    766 		MSG_PRINTF(("requester doesn't have read access\n"));
    767 		goto unlock;
    768 	}
    769 
    770 #if 0
    771 	/* cannot happen, msgsz is unsigned */
    772 	if (msgsz < 0) {
    773 		MSG_PRINTF(("msgsz < 0\n"));
    774 		error = EINVAL;
    775 		goto unlock;
    776 	}
    777 #endif
    778 
    779 	msghdr = NULL;
    780 	while (msghdr == NULL) {
    781 		if (msgtyp == 0) {
    782 			msghdr = msqptr->_msg_first;
    783 			if (msghdr != NULL) {
    784 				if (msgsz < msghdr->msg_ts &&
    785 				    (msgflg & MSG_NOERROR) == 0) {
    786 					MSG_PRINTF(("first message on the "
    787 					    "queue is too big "
    788 					    "(want %lld, got %d)\n",
    789 					    (long long)msgsz, msghdr->msg_ts));
    790 					error = E2BIG;
    791 					goto unlock;
    792 				}
    793 				if (msqptr->_msg_first == msqptr->_msg_last) {
    794 					msqptr->_msg_first = NULL;
    795 					msqptr->_msg_last = NULL;
    796 				} else {
    797 					msqptr->_msg_first = msghdr->msg_next;
    798 					if (msqptr->_msg_first == NULL)
    799 						panic("msg_first/last screwed "
    800 						    "up #1");
    801 				}
    802 			}
    803 		} else {
    804 			struct __msg *previous;
    805 			struct __msg **prev;
    806 
    807 			for (previous = NULL, prev = &msqptr->_msg_first;
    808 			     (msghdr = *prev) != NULL;
    809 			     previous = msghdr, prev = &msghdr->msg_next) {
    810 				/*
    811 				 * Is this message's type an exact match or is
    812 				 * this message's type less than or equal to
    813 				 * the absolute value of a negative msgtyp?
    814 				 * Note that the second half of this test can
    815 				 * NEVER be true if msgtyp is positive since
    816 				 * msg_type is always positive!
    817 				 */
    818 
    819 				if (msgtyp == msghdr->msg_type ||
    820 				    msghdr->msg_type <= -msgtyp) {
    821 					MSG_PRINTF(("found message type %ld, "
    822 					    "requested %ld\n",
    823 					    msghdr->msg_type, msgtyp));
    824 					if (msgsz < msghdr->msg_ts &&
    825 					    (msgflg & MSG_NOERROR) == 0) {
    826 						MSG_PRINTF(("requested message "
    827 						    "on the queue is too big "
    828 						    "(want %lld, got %d)\n",
    829 						    (long long)msgsz,
    830 						    msghdr->msg_ts));
    831 						error = E2BIG;
    832 						goto unlock;
    833 					}
    834 					*prev = msghdr->msg_next;
    835 					if (msghdr == msqptr->_msg_last) {
    836 						if (previous == NULL) {
    837 							if (prev !=
    838 							    &msqptr->_msg_first)
    839 								panic("msg_first/last screwed up #2");
    840 							msqptr->_msg_first =
    841 							    NULL;
    842 							msqptr->_msg_last =
    843 							    NULL;
    844 						} else {
    845 							if (prev ==
    846 							    &msqptr->_msg_first)
    847 								panic("msg_first/last screwed up #3");
    848 							msqptr->_msg_last =
    849 							    previous;
    850 						}
    851 					}
    852 					break;
    853 				}
    854 			}
    855 		}
    856 
    857 		/*
    858 		 * We've either extracted the msghdr for the appropriate
    859 		 * message or there isn't one.
    860 		 * If there is one then bail out of this loop.
    861 		 */
    862 
    863 		if (msghdr != NULL)
    864 			break;
    865 
    866 		/*
    867 		 * Hmph!  No message found.  Does the user want to wait?
    868 		 */
    869 
    870 		if ((msgflg & IPC_NOWAIT) != 0) {
    871 			MSG_PRINTF(("no appropriate message found (msgtyp=%ld)\n",
    872 			    msgtyp));
    873 			/* The SVID says to return ENOMSG. */
    874 #ifdef ENOMSG
    875 			error = ENOMSG;
    876 #else
    877 			/* Unfortunately, BSD doesn't define that code yet! */
    878 			error = EAGAIN;
    879 #endif
    880 			goto unlock;
    881 		}
    882 
    883 		/*
    884 		 * Wait for something to happen
    885 		 */
    886 
    887 		MSG_PRINTF(("msgrcv:  goodnight\n"));
    888 		error = mtsleep(msqptr, (PZERO - 4) | PCATCH, "msgwait",
    889 		    0, &msgmutex);
    890 		MSG_PRINTF(("msgrcv: good morning (error=%d)\n", error));
    891 
    892 		if (error != 0) {
    893 			MSG_PRINTF(("msgsnd: interrupted system call\n"));
    894 			error = EINTR;
    895 			goto unlock;
    896 		}
    897 
    898 		/*
    899 		 * Make sure that the msq queue still exists
    900 		 */
    901 
    902 		if (msqptr->msg_qbytes == 0 ||
    903 		    msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
    904 			MSG_PRINTF(("msqid deleted\n"));
    905 			error = EIDRM;
    906 			goto unlock;
    907 		}
    908 	}
    909 
    910 	/*
    911 	 * Return the message to the user.
    912 	 *
    913 	 * First, do the bookkeeping (before we risk being interrupted).
    914 	 */
    915 
    916 	msqptr->_msg_cbytes -= msghdr->msg_ts;
    917 	msqptr->msg_qnum--;
    918 	msqptr->msg_lrpid = l->l_proc->p_pid;
    919 	msqptr->msg_rtime = time_second;
    920 
    921 	/*
    922 	 * Make msgsz the actual amount that we'll be returning.
    923 	 * Note that this effectively truncates the message if it is too long
    924 	 * (since msgsz is never increased).
    925 	 */
    926 
    927 	MSG_PRINTF(("found a message, msgsz=%lld, msg_ts=%d\n",
    928 	    (long long)msgsz, msghdr->msg_ts));
    929 	if (msgsz > msghdr->msg_ts)
    930 		msgsz = msghdr->msg_ts;
    931 
    932 	/*
    933 	 * Return the type to the user.
    934 	 */
    935 	mutex_exit(&msgmutex);
    936 	error = (*put_type)(&msghdr->msg_type, user_msgp, typesz);
    937 	mutex_enter(&msgmutex);
    938 	if (error != 0) {
    939 		MSG_PRINTF(("error (%d) copying out message type\n", error));
    940 		msg_freehdr(msghdr);
    941 		wakeup(msqptr);
    942 		goto unlock;
    943 	}
    944 	user_msgp += typesz;
    945 
    946 	/*
    947 	 * Return the segments to the user
    948 	 */
    949 
    950 	next = msghdr->msg_spot;
    951 	for (len = 0; len < msgsz; len += msginfo.msgssz) {
    952 		size_t tlen;
    953 
    954 		if (msgsz - len > msginfo.msgssz)
    955 			tlen = msginfo.msgssz;
    956 		else
    957 			tlen = msgsz - len;
    958 		if (next <= -1)
    959 			panic("next too low #3");
    960 		if (next >= msginfo.msgseg)
    961 			panic("next out of range #3");
    962 		mutex_exit(&msgmutex);
    963 		error = copyout(&msgpool[next * msginfo.msgssz],
    964 		    user_msgp, tlen);
    965 		mutex_enter(&msgmutex);
    966 		if (error != 0) {
    967 			MSG_PRINTF(("error (%d) copying out message segment\n",
    968 			    error));
    969 			msg_freehdr(msghdr);
    970 			wakeup(msqptr);
    971 			goto unlock;
    972 		}
    973 		user_msgp += tlen;
    974 		next = msgmaps[next].next;
    975 	}
    976 
    977 	/*
    978 	 * Done, return the actual number of bytes copied out.
    979 	 */
    980 
    981 	msg_freehdr(msghdr);
    982 	wakeup(msqptr);
    983 	*retval = msgsz;
    984 
    985  unlock:
    986  	mutex_exit(&msgmutex);
    987 	return error;
    988 }
    989