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