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