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