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