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