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