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