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