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