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