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