sysv_msg.c revision 8876
18876Srgrimes/* $Id: sysv_msg.c,v 1.3 1994/10/02 17:35:26 phk Exp $ */ 22729Sdfr 32729Sdfr/* 42729Sdfr * Implementation of SVID messages 52729Sdfr * 62729Sdfr * Author: Daniel Boulet 72729Sdfr * 82729Sdfr * Copyright 1993 Daniel Boulet and RTMX Inc. 92729Sdfr * 102729Sdfr * This system call was implemented by Daniel Boulet under contract from RTMX. 112729Sdfr * 122729Sdfr * Redistribution and use in source forms, with and without modification, 132729Sdfr * are permitted provided that this entire comment appears intact. 142729Sdfr * 152729Sdfr * Redistribution in binary form may occur without any restrictions. 162729Sdfr * Obviously, it would be nice if you gave credit where credit is due 172729Sdfr * but requiring it would be too onerous. 182729Sdfr * 192729Sdfr * This software is provided ``AS IS'' without any warranties of any kind. 202729Sdfr */ 212729Sdfr 222729Sdfr#include <sys/param.h> 232729Sdfr#include <sys/systm.h> 242729Sdfr#include <sys/kernel.h> 252729Sdfr#include <sys/proc.h> 262729Sdfr#include <sys/msg.h> 272729Sdfr#include <sys/malloc.h> 282729Sdfr 292729Sdfr#define MSG_DEBUG 302729Sdfr#undef MSG_DEBUG_OK 312729Sdfr 322729Sdfrstatic int msgctl(), msgget(), msgsnd(), msgrcv(); 332729Sdfr 342729Sdfrint (*msgcalls[])() = { msgctl, msgget, msgsnd, msgrcv }; 352729Sdfr 362729Sdfrint nfree_msgmaps; /* # of free map entries */ 372729Sdfrshort free_msgmaps; /* head of linked list of free map entries */ 382729Sdfrstruct msg *free_msghdrs; /* list of free msg headers */ 392729Sdfr 402836Sdgvoid 412729Sdfrmsginit() 422729Sdfr{ 432729Sdfr register int i; 442729Sdfr 452729Sdfr /* 462729Sdfr * msginfo.msgssz should be a power of two for efficiency reasons. 472729Sdfr * It is also pretty silly if msginfo.msgssz is less than 8 482729Sdfr * or greater than about 256 so ... 492729Sdfr */ 502729Sdfr 512729Sdfr i = 8; 522729Sdfr while (i < 1024 && i != msginfo.msgssz) 532729Sdfr i <<= 1; 542729Sdfr if (i != msginfo.msgssz) { 552729Sdfr printf("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz, 562729Sdfr msginfo.msgssz); 572729Sdfr panic("msginfo.msgssz not a small power of 2"); 582729Sdfr } 592729Sdfr 602729Sdfr if (msginfo.msgseg > 32767) { 612729Sdfr printf("msginfo.msgseg=%d\n", msginfo.msgseg); 622729Sdfr panic("msginfo.msgseg > 32767"); 632729Sdfr } 642729Sdfr 652729Sdfr if (msgmaps == NULL) 662729Sdfr panic("msgmaps is NULL"); 672729Sdfr 682729Sdfr for (i = 0; i < msginfo.msgseg; i++) { 692729Sdfr if (i > 0) 702729Sdfr msgmaps[i-1].next = i; 712729Sdfr msgmaps[i].next = -1; /* implies entry is available */ 722729Sdfr } 732729Sdfr free_msgmaps = 0; 742729Sdfr nfree_msgmaps = msginfo.msgseg; 752729Sdfr 762729Sdfr if (msghdrs == NULL) 772729Sdfr panic("msghdrs is NULL"); 782729Sdfr 792729Sdfr for (i = 0; i < msginfo.msgtql; i++) { 802729Sdfr msghdrs[i].msg_type = 0; 812729Sdfr if (i > 0) 822729Sdfr msghdrs[i-1].msg_next = &msghdrs[i]; 832729Sdfr msghdrs[i].msg_next = NULL; 842729Sdfr } 852729Sdfr free_msghdrs = &msghdrs[0]; 862729Sdfr 872729Sdfr if (msqids == NULL) 882729Sdfr panic("msqids is NULL"); 892729Sdfr 902729Sdfr for (i = 0; i < msginfo.msgmni; i++) { 912729Sdfr msqids[i].msg_qbytes = 0; /* implies entry is available */ 922729Sdfr msqids[i].msg_perm.seq = 0; /* reset to a known value */ 932729Sdfr } 942729Sdfr} 952729Sdfr 962729Sdfr/* 972729Sdfr * Entry point for all MSG calls 982729Sdfr */ 992729Sdfr 1002729Sdfrstruct msgsys_args { 1012729Sdfr u_int which; 1022729Sdfr}; 1032729Sdfr 1042729Sdfrint 1052729Sdfrmsgsys(p, uap, retval) 1062729Sdfr struct caller *p; 1072729Sdfr struct msgsys_args *uap; 1082729Sdfr int *retval; 1092729Sdfr{ 1102729Sdfr 1112729Sdfr if (uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0])) 1122729Sdfr return (EINVAL); 1132729Sdfr return ((*msgcalls[uap->which])(p, &uap[1], retval)); 1142729Sdfr} 1152729Sdfr 1162729Sdfrstatic void 1172729Sdfrmsg_freehdr(msghdr) 1182729Sdfr struct msg *msghdr; 1192729Sdfr{ 1202729Sdfr while (msghdr->msg_ts > 0) { 1212729Sdfr short next; 1222729Sdfr if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg) 1232729Sdfr panic("msghdr->msg_spot out of range"); 1242729Sdfr next = msgmaps[msghdr->msg_spot].next; 1252729Sdfr msgmaps[msghdr->msg_spot].next = free_msgmaps; 1262729Sdfr free_msgmaps = msghdr->msg_spot; 1272729Sdfr nfree_msgmaps++; 1282729Sdfr msghdr->msg_spot = next; 1292729Sdfr if (msghdr->msg_ts >= msginfo.msgssz) 1302729Sdfr msghdr->msg_ts -= msginfo.msgssz; 1312729Sdfr else 1322729Sdfr msghdr->msg_ts = 0; 1332729Sdfr } 1342729Sdfr if (msghdr->msg_spot != -1) 1352729Sdfr panic("msghdr->msg_spot != -1"); 1362729Sdfr msghdr->msg_next = free_msghdrs; 1372729Sdfr free_msghdrs = msghdr; 1382729Sdfr} 1392729Sdfr 1402729Sdfrstruct msgctl_args { 1412729Sdfr int msqid; 1422729Sdfr int cmd; 1432729Sdfr struct msqid_ds *user_msqptr; 1442729Sdfr}; 1452729Sdfr 1462729Sdfrint 1472729Sdfrmsgctl(p, uap, retval) 1482729Sdfr struct proc *p; 1492729Sdfr register struct msgctl_args *uap; 1502729Sdfr int *retval; 1512729Sdfr{ 1522729Sdfr int msqid = uap->msqid; 1532729Sdfr int cmd = uap->cmd; 1542729Sdfr struct msqid_ds *user_msqptr = uap->user_msqptr; 1552729Sdfr struct ucred *cred = p->p_ucred; 1563308Sphk int rval, eval; 1572729Sdfr struct msqid_ds msqbuf; 1582729Sdfr register struct msqid_ds *msqptr; 1592729Sdfr 1602729Sdfr#ifdef MSG_DEBUG_OK 1612729Sdfr printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr); 1622729Sdfr#endif 1632729Sdfr 1642729Sdfr msqid = IPCID_TO_IX(msqid); 1652729Sdfr 1662729Sdfr if (msqid < 0 || msqid >= msginfo.msgmni) { 1672729Sdfr#ifdef MSG_DEBUG_OK 1682729Sdfr printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 1692729Sdfr msginfo.msgmni); 1702729Sdfr#endif 1712729Sdfr return(EINVAL); 1722729Sdfr } 1732729Sdfr 1742729Sdfr msqptr = &msqids[msqid]; 1752729Sdfr 1762729Sdfr if (msqptr->msg_qbytes == 0) { 1772729Sdfr#ifdef MSG_DEBUG_OK 1782729Sdfr printf("no such msqid\n"); 1792729Sdfr#endif 1802729Sdfr return(EINVAL); 1812729Sdfr } 1822729Sdfr if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 1832729Sdfr#ifdef MSG_DEBUG_OK 1842729Sdfr printf("wrong sequence number\n"); 1852729Sdfr#endif 1862729Sdfr return(EINVAL); 1872729Sdfr } 1882729Sdfr 1892729Sdfr eval = 0; 1902729Sdfr rval = 0; 1912729Sdfr 1922729Sdfr switch (cmd) { 1932729Sdfr 1942729Sdfr case IPC_RMID: 1952729Sdfr { 1962729Sdfr struct msg *msghdr; 1972729Sdfr if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M))) 1982729Sdfr return(eval); 1992729Sdfr /* Free the message headers */ 2002729Sdfr msghdr = msqptr->msg_first; 2012729Sdfr while (msghdr != NULL) { 2022729Sdfr struct msg *msghdr_tmp; 2032729Sdfr 2042729Sdfr /* Free the segments of each message */ 2052729Sdfr msqptr->msg_cbytes -= msghdr->msg_ts; 2062729Sdfr msqptr->msg_qnum--; 2072729Sdfr msghdr_tmp = msghdr; 2082729Sdfr msghdr = msghdr->msg_next; 2092729Sdfr msg_freehdr(msghdr_tmp); 2102729Sdfr } 2112729Sdfr 2122729Sdfr if (msqptr->msg_cbytes != 0) 2132729Sdfr panic("msg_cbytes is screwed up"); 2142729Sdfr if (msqptr->msg_qnum != 0) 2152729Sdfr panic("msg_qnum is screwed up"); 2162729Sdfr 2172729Sdfr msqptr->msg_qbytes = 0; /* Mark it as free */ 2182729Sdfr 2192729Sdfr wakeup((caddr_t)msqptr); 2202729Sdfr } 2212729Sdfr 2222729Sdfr break; 2232729Sdfr 2242729Sdfr case IPC_SET: 2252729Sdfr if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M))) 2262729Sdfr return(eval); 2272729Sdfr if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0) 2282729Sdfr return(eval); 2292729Sdfr if (msqbuf.msg_qbytes > msqptr->msg_qbytes && cred->cr_uid != 0) 2302729Sdfr return(EPERM); 2312729Sdfr if (msqbuf.msg_qbytes > msginfo.msgmnb) { 2322729Sdfr#ifdef MSG_DEBUG_OK 2332729Sdfr printf("can't increase msg_qbytes beyond %d (truncating)\n", 2342729Sdfr msginfo.msgmnb); 2352729Sdfr#endif 2362729Sdfr msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */ 2372729Sdfr } 2382729Sdfr if (msqbuf.msg_qbytes == 0) { 2392729Sdfr#ifdef MSG_DEBUG_OK 2402729Sdfr printf("can't reduce msg_qbytes to 0\n"); 2412729Sdfr#endif 2422729Sdfr return(EINVAL); /* non-standard errno! */ 2432729Sdfr } 2442729Sdfr msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */ 2452729Sdfr msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */ 2462729Sdfr msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) | 2472729Sdfr (msqbuf.msg_perm.mode & 0777); 2482729Sdfr msqptr->msg_qbytes = msqbuf.msg_qbytes; 2492729Sdfr msqptr->msg_ctime = time.tv_sec; 2502729Sdfr break; 2512729Sdfr 2522729Sdfr case IPC_STAT: 2532729Sdfr if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) { 2542729Sdfr#ifdef MSG_DEBUG_OK 2552729Sdfr printf("requester doesn't have read access\n"); 2562729Sdfr#endif 2572729Sdfr return(eval); 2582729Sdfr } 2592729Sdfr eval = copyout((caddr_t)msqptr, user_msqptr, 2602729Sdfr sizeof(struct msqid_ds)); 2612729Sdfr break; 2622729Sdfr 2632729Sdfr default: 2642729Sdfr#ifdef MSG_DEBUG_OK 2652729Sdfr printf("invalid command %d\n", cmd); 2662729Sdfr#endif 2672729Sdfr return(EINVAL); 2682729Sdfr } 2692729Sdfr 2702729Sdfr if (eval == 0) 2712729Sdfr *retval = rval; 2722729Sdfr return(eval); 2732729Sdfr} 2742729Sdfr 2752729Sdfrstruct msgget_args { 2762729Sdfr key_t key; 2772729Sdfr int msgflg; 2782729Sdfr}; 2792729Sdfr 2802729Sdfrint 2812729Sdfrmsgget(p, uap, retval) 2822729Sdfr struct proc *p; 2832729Sdfr register struct msgget_args *uap; 2842729Sdfr int *retval; 2852729Sdfr{ 2862729Sdfr int msqid, eval; 2872729Sdfr int key = uap->key; 2882729Sdfr int msgflg = uap->msgflg; 2892729Sdfr struct ucred *cred = p->p_ucred; 2902836Sdg register struct msqid_ds *msqptr = NULL; 2912729Sdfr 2922729Sdfr#ifdef MSG_DEBUG_OK 2932729Sdfr printf("msgget(0x%x, 0%o)\n", key, msgflg); 2942729Sdfr#endif 2952729Sdfr 2962729Sdfr if (key != IPC_PRIVATE) { 2972729Sdfr for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 2982729Sdfr msqptr = &msqids[msqid]; 2992729Sdfr if (msqptr->msg_qbytes != 0 && 3002729Sdfr msqptr->msg_perm.key == key) 3012729Sdfr break; 3022729Sdfr } 3032729Sdfr if (msqid < msginfo.msgmni) { 3042729Sdfr#ifdef MSG_DEBUG_OK 3052729Sdfr printf("found public key\n"); 3062729Sdfr#endif 3072729Sdfr if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) { 3082729Sdfr#ifdef MSG_DEBUG_OK 3092729Sdfr printf("not exclusive\n"); 3102729Sdfr#endif 3112729Sdfr return(EEXIST); 3122729Sdfr } 3132729Sdfr if ((eval = ipcperm(cred, &msqptr->msg_perm, msgflg & 0700 ))) { 3142729Sdfr#ifdef MSG_DEBUG_OK 3152729Sdfr printf("requester doesn't have 0%o access\n", 3162729Sdfr msgflg & 0700); 3172729Sdfr#endif 3182729Sdfr return(eval); 3192729Sdfr } 3202729Sdfr goto found; 3212729Sdfr } 3222729Sdfr } 3232729Sdfr 3242729Sdfr#ifdef MSG_DEBUG_OK 3252729Sdfr printf("need to allocate the msqid_ds\n"); 3262729Sdfr#endif 3272729Sdfr if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) { 3282729Sdfr for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 3292729Sdfr /* 3302729Sdfr * Look for an unallocated and unlocked msqid_ds. 3312729Sdfr * msqid_ds's can be locked by msgsnd or msgrcv while 3322729Sdfr * they are copying the message in/out. We can't 3332729Sdfr * re-use the entry until they release it. 3342729Sdfr */ 3352729Sdfr msqptr = &msqids[msqid]; 3362729Sdfr if (msqptr->msg_qbytes == 0 && 3372729Sdfr (msqptr->msg_perm.mode & MSG_LOCKED) == 0) 3382729Sdfr break; 3392729Sdfr } 3402729Sdfr if (msqid == msginfo.msgmni) { 3412729Sdfr#ifdef MSG_DEBUG_OK 3422729Sdfr printf("no more msqid_ds's available\n"); 3432729Sdfr#endif 3448876Srgrimes return(ENOSPC); 3452729Sdfr } 3462729Sdfr#ifdef MSG_DEBUG_OK 3472729Sdfr printf("msqid %d is available\n", msqid); 3482729Sdfr#endif 3492729Sdfr msqptr->msg_perm.key = key; 3502729Sdfr msqptr->msg_perm.cuid = cred->cr_uid; 3512729Sdfr msqptr->msg_perm.uid = cred->cr_uid; 3522729Sdfr msqptr->msg_perm.cgid = cred->cr_gid; 3532729Sdfr msqptr->msg_perm.gid = cred->cr_gid; 3542729Sdfr msqptr->msg_perm.mode = (msgflg & 0777); 3552729Sdfr /* Make sure that the returned msqid is unique */ 3562729Sdfr msqptr->msg_perm.seq++; 3572729Sdfr msqptr->msg_first = NULL; 3582729Sdfr msqptr->msg_last = NULL; 3592729Sdfr msqptr->msg_cbytes = 0; 3602729Sdfr msqptr->msg_qnum = 0; 3612729Sdfr msqptr->msg_qbytes = msginfo.msgmnb; 3622729Sdfr msqptr->msg_lspid = 0; 3632729Sdfr msqptr->msg_lrpid = 0; 3642729Sdfr msqptr->msg_stime = 0; 3652729Sdfr msqptr->msg_rtime = 0; 3662729Sdfr msqptr->msg_ctime = time.tv_sec; 3672729Sdfr } else { 3682729Sdfr#ifdef MSG_DEBUG_OK 3692729Sdfr printf("didn't find it and wasn't asked to create it\n"); 3702729Sdfr#endif 3712729Sdfr return(ENOENT); 3722729Sdfr } 3732729Sdfr 3742729Sdfrfound: 3752729Sdfr /* Construct the unique msqid */ 3762729Sdfr *retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm); 3772729Sdfr return(0); 3782729Sdfr} 3792729Sdfr 3802729Sdfrstruct msgsnd_args { 3812729Sdfr int msqid; 3822729Sdfr void *user_msgp; 3832729Sdfr size_t msgsz; 3842729Sdfr int msgflg; 3852729Sdfr}; 3862729Sdfr 3872729Sdfrint 3882729Sdfrmsgsnd(p, uap, retval) 3892729Sdfr struct proc *p; 3902729Sdfr register struct msgsnd_args *uap; 3912729Sdfr int *retval; 3922729Sdfr{ 3932729Sdfr int msqid = uap->msqid; 3942729Sdfr void *user_msgp = uap->user_msgp; 3952729Sdfr size_t msgsz = uap->msgsz; 3962729Sdfr int msgflg = uap->msgflg; 3972729Sdfr int segs_needed, eval; 3982729Sdfr struct ucred *cred = p->p_ucred; 3992729Sdfr register struct msqid_ds *msqptr; 4002729Sdfr register struct msg *msghdr; 4012729Sdfr short next; 4022729Sdfr 4032729Sdfr#ifdef MSG_DEBUG_OK 4042729Sdfr printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz, 4052729Sdfr msgflg); 4062729Sdfr#endif 4072729Sdfr 4082729Sdfr msqid = IPCID_TO_IX(msqid); 4092729Sdfr 4102729Sdfr if (msqid < 0 || msqid >= msginfo.msgmni) { 4112729Sdfr#ifdef MSG_DEBUG_OK 4122729Sdfr printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 4132729Sdfr msginfo.msgmni); 4142729Sdfr#endif 4152729Sdfr return(EINVAL); 4162729Sdfr } 4172729Sdfr 4182729Sdfr msqptr = &msqids[msqid]; 4192729Sdfr if (msqptr->msg_qbytes == 0) { 4202729Sdfr#ifdef MSG_DEBUG_OK 4212729Sdfr printf("no such message queue id\n"); 4222729Sdfr#endif 4232729Sdfr return(EINVAL); 4242729Sdfr } 4252729Sdfr if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 4262729Sdfr#ifdef MSG_DEBUG_OK 4272729Sdfr printf("wrong sequence number\n"); 4282729Sdfr#endif 4292729Sdfr return(EINVAL); 4302729Sdfr } 4312729Sdfr 4322729Sdfr if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_W))) { 4332729Sdfr#ifdef MSG_DEBUG_OK 4342729Sdfr printf("requester doesn't have write access\n"); 4352729Sdfr#endif 4362729Sdfr return(eval); 4372729Sdfr } 4382729Sdfr 4392729Sdfr segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz; 4402729Sdfr#ifdef MSG_DEBUG_OK 4412729Sdfr printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz, 4422729Sdfr segs_needed); 4432729Sdfr#endif 4442729Sdfr for (;;) { 4452729Sdfr int need_more_resources = 0; 4462729Sdfr 4472729Sdfr /* 4482729Sdfr * check msgsz 4492729Sdfr * (inside this loop in case msg_qbytes changes while we sleep) 4502729Sdfr */ 4512729Sdfr 4522836Sdg if (msgsz > msqptr->msg_qbytes) { 4532729Sdfr#ifdef MSG_DEBUG_OK 4542729Sdfr printf("msgsz > msqptr->msg_qbytes\n"); 4552729Sdfr#endif 4562729Sdfr return(EINVAL); 4572729Sdfr } 4582729Sdfr 4592729Sdfr if (msqptr->msg_perm.mode & MSG_LOCKED) { 4602729Sdfr#ifdef MSG_DEBUG_OK 4612729Sdfr printf("msqid is locked\n"); 4622729Sdfr#endif 4632729Sdfr need_more_resources = 1; 4642729Sdfr } 4652729Sdfr if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) { 4662729Sdfr#ifdef MSG_DEBUG_OK 4672729Sdfr printf("msgsz + msg_cbytes > msg_qbytes\n"); 4682729Sdfr#endif 4692729Sdfr need_more_resources = 1; 4702729Sdfr } 4712729Sdfr if (segs_needed > nfree_msgmaps) { 4722729Sdfr#ifdef MSG_DEBUG_OK 4732729Sdfr printf("segs_needed > nfree_msgmaps\n"); 4742729Sdfr#endif 4752729Sdfr need_more_resources = 1; 4762729Sdfr } 4772729Sdfr if (free_msghdrs == NULL) { 4782729Sdfr#ifdef MSG_DEBUG_OK 4792729Sdfr printf("no more msghdrs\n"); 4802729Sdfr#endif 4812729Sdfr need_more_resources = 1; 4822729Sdfr } 4832729Sdfr 4842729Sdfr if (need_more_resources) { 4852729Sdfr int we_own_it; 4862729Sdfr 4872729Sdfr if ((msgflg & IPC_NOWAIT) != 0) { 4882729Sdfr#ifdef MSG_DEBUG_OK 4892729Sdfr printf("need more resources but caller doesn't want to wait\n"); 4902729Sdfr#endif 4912729Sdfr return(EAGAIN); 4922729Sdfr } 4932729Sdfr 4942729Sdfr if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) { 4952729Sdfr#ifdef MSG_DEBUG_OK 4962729Sdfr printf("we don't own the msqid_ds\n"); 4972729Sdfr#endif 4982729Sdfr we_own_it = 0; 4992729Sdfr } else { 5002729Sdfr /* Force later arrivals to wait for our 5012729Sdfr request */ 5022729Sdfr#ifdef MSG_DEBUG_OK 5032729Sdfr printf("we own the msqid_ds\n"); 5042729Sdfr#endif 5052729Sdfr msqptr->msg_perm.mode |= MSG_LOCKED; 5062729Sdfr we_own_it = 1; 5072729Sdfr } 5082729Sdfr#ifdef MSG_DEBUG_OK 5092729Sdfr printf("goodnight\n"); 5102729Sdfr#endif 5112729Sdfr eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, 5122729Sdfr "msgwait", 0); 5132729Sdfr#ifdef MSG_DEBUG_OK 5142729Sdfr printf("good morning, eval=%d\n", eval); 5152729Sdfr#endif 5162729Sdfr if (we_own_it) 5172729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 5182729Sdfr if (eval != 0) { 5192729Sdfr#ifdef MSG_DEBUG_OK 5202729Sdfr printf("msgsnd: interrupted system call\n"); 5212729Sdfr#endif 5222729Sdfr return(EINTR); 5232729Sdfr } 5242729Sdfr 5252729Sdfr /* 5262729Sdfr * Make sure that the msq queue still exists 5272729Sdfr */ 5282729Sdfr 5292729Sdfr if (msqptr->msg_qbytes == 0) { 5302729Sdfr#ifdef MSG_DEBUG_OK 5312729Sdfr printf("msqid deleted\n"); 5322729Sdfr#endif 5332729Sdfr /* The SVID says to return EIDRM. */ 5342729Sdfr#ifdef EIDRM 5352729Sdfr return(EIDRM); 5362729Sdfr#else 5372729Sdfr /* Unfortunately, BSD doesn't define that code 5382729Sdfr yet! */ 5392729Sdfr return(EINVAL); 5402729Sdfr#endif 5412729Sdfr } 5422729Sdfr 5432729Sdfr } else { 5442729Sdfr#ifdef MSG_DEBUG_OK 5452729Sdfr printf("got all the resources that we need\n"); 5462729Sdfr#endif 5472729Sdfr break; 5482729Sdfr } 5492729Sdfr } 5502729Sdfr 5512729Sdfr /* 5522729Sdfr * We have the resources that we need. 5532729Sdfr * Make sure! 5542729Sdfr */ 5552729Sdfr 5562729Sdfr if (msqptr->msg_perm.mode & MSG_LOCKED) 5572729Sdfr panic("msg_perm.mode & MSG_LOCKED"); 5582729Sdfr if (segs_needed > nfree_msgmaps) 5592729Sdfr panic("segs_needed > nfree_msgmaps"); 5602729Sdfr if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) 5612729Sdfr panic("msgsz + msg_cbytes > msg_qbytes"); 5622729Sdfr if (free_msghdrs == NULL) 5632729Sdfr panic("no more msghdrs"); 5642729Sdfr 5652729Sdfr /* 5662729Sdfr * Re-lock the msqid_ds in case we page-fault when copying in the 5672729Sdfr * message 5682729Sdfr */ 5692729Sdfr 5702729Sdfr if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) 5712729Sdfr panic("msqid_ds is already locked"); 5722729Sdfr msqptr->msg_perm.mode |= MSG_LOCKED; 5732729Sdfr 5742729Sdfr /* 5752729Sdfr * Allocate a message header 5762729Sdfr */ 5772729Sdfr 5782729Sdfr msghdr = free_msghdrs; 5792729Sdfr free_msghdrs = msghdr->msg_next; 5802729Sdfr msghdr->msg_spot = -1; 5812729Sdfr msghdr->msg_ts = msgsz; 5822729Sdfr 5832729Sdfr /* 5842729Sdfr * Allocate space for the message 5852729Sdfr */ 5862729Sdfr 5872729Sdfr while (segs_needed > 0) { 5882729Sdfr if (nfree_msgmaps <= 0) 5892729Sdfr panic("not enough msgmaps"); 5902729Sdfr if (free_msgmaps == -1) 5912729Sdfr panic("nil free_msgmaps"); 5922729Sdfr next = free_msgmaps; 5932729Sdfr if (next <= -1) 5942729Sdfr panic("next too low #1"); 5952729Sdfr if (next >= msginfo.msgseg) 5962729Sdfr panic("next out of range #1"); 5972729Sdfr#ifdef MSG_DEBUG_OK 5982729Sdfr printf("allocating segment %d to message\n", next); 5992729Sdfr#endif 6002729Sdfr free_msgmaps = msgmaps[next].next; 6012729Sdfr nfree_msgmaps--; 6022729Sdfr msgmaps[next].next = msghdr->msg_spot; 6032729Sdfr msghdr->msg_spot = next; 6042729Sdfr segs_needed--; 6052729Sdfr } 6062729Sdfr 6072729Sdfr /* 6082729Sdfr * Copy in the message type 6092729Sdfr */ 6102729Sdfr 6112729Sdfr if ((eval = copyin(user_msgp, &msghdr->msg_type, 6122729Sdfr sizeof(msghdr->msg_type))) != 0) { 6132729Sdfr#ifdef MSG_DEBUG_OK 6142729Sdfr printf("error %d copying the message type\n", eval); 6152729Sdfr#endif 6162729Sdfr msg_freehdr(msghdr); 6172729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 6182729Sdfr wakeup((caddr_t)msqptr); 6192729Sdfr return(eval); 6202729Sdfr } 6212729Sdfr user_msgp += sizeof(msghdr->msg_type); 6222729Sdfr 6232729Sdfr /* 6242729Sdfr * Validate the message type 6252729Sdfr */ 6262729Sdfr 6272729Sdfr if (msghdr->msg_type < 1) { 6282729Sdfr msg_freehdr(msghdr); 6292729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 6302729Sdfr wakeup((caddr_t)msqptr); 6312729Sdfr#ifdef MSG_DEBUG_OK 6322729Sdfr printf("mtype (%d) < 1\n", msghdr->msg_type); 6332729Sdfr#endif 6342729Sdfr return(EINVAL); 6352729Sdfr } 6362729Sdfr 6372729Sdfr /* 6382729Sdfr * Copy in the message body 6392729Sdfr */ 6402729Sdfr 6412729Sdfr next = msghdr->msg_spot; 6422729Sdfr while (msgsz > 0) { 6432729Sdfr size_t tlen; 6442729Sdfr if (msgsz > msginfo.msgssz) 6452729Sdfr tlen = msginfo.msgssz; 6462729Sdfr else 6472729Sdfr tlen = msgsz; 6482729Sdfr if (next <= -1) 6492729Sdfr panic("next too low #2"); 6502729Sdfr if (next >= msginfo.msgseg) 6512729Sdfr panic("next out of range #2"); 6522729Sdfr if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz], 6532729Sdfr tlen)) != 0) { 6542729Sdfr#ifdef MSG_DEBUG_OK 6552729Sdfr printf("error %d copying in message segment\n", eval); 6562729Sdfr#endif 6572729Sdfr msg_freehdr(msghdr); 6582729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 6592729Sdfr wakeup((caddr_t)msqptr); 6602729Sdfr return(eval); 6612729Sdfr } 6622729Sdfr msgsz -= tlen; 6632729Sdfr user_msgp += tlen; 6642729Sdfr next = msgmaps[next].next; 6652729Sdfr } 6662729Sdfr if (next != -1) 6672729Sdfr panic("didn't use all the msg segments"); 6682729Sdfr 6692729Sdfr /* 6702729Sdfr * We've got the message. Unlock the msqid_ds. 6712729Sdfr */ 6722729Sdfr 6732729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 6742729Sdfr 6752729Sdfr /* 6762729Sdfr * Make sure that the msqid_ds is still allocated. 6772729Sdfr */ 6782729Sdfr 6792729Sdfr if (msqptr->msg_qbytes == 0) { 6802729Sdfr msg_freehdr(msghdr); 6812729Sdfr wakeup((caddr_t)msqptr); 6822729Sdfr /* The SVID says to return EIDRM. */ 6832729Sdfr#ifdef EIDRM 6842729Sdfr return(EIDRM); 6852729Sdfr#else 6862729Sdfr /* Unfortunately, BSD doesn't define that code yet! */ 6872729Sdfr return(EINVAL); 6882729Sdfr#endif 6892729Sdfr } 6902729Sdfr 6912729Sdfr /* 6922729Sdfr * Put the message into the queue 6932729Sdfr */ 6942729Sdfr 6952729Sdfr if (msqptr->msg_first == NULL) { 6962729Sdfr msqptr->msg_first = msghdr; 6972729Sdfr msqptr->msg_last = msghdr; 6982729Sdfr } else { 6992729Sdfr msqptr->msg_last->msg_next = msghdr; 7002729Sdfr msqptr->msg_last = msghdr; 7012729Sdfr } 7022729Sdfr msqptr->msg_last->msg_next = NULL; 7032729Sdfr 7042729Sdfr msqptr->msg_cbytes += msghdr->msg_ts; 7052729Sdfr msqptr->msg_qnum++; 7062729Sdfr msqptr->msg_lspid = p->p_pid; 7072729Sdfr msqptr->msg_stime = time.tv_sec; 7082729Sdfr 7092729Sdfr wakeup((caddr_t)msqptr); 7102729Sdfr *retval = 0; 7112729Sdfr return(0); 7122729Sdfr} 7132729Sdfr 7142729Sdfrstruct msgrcv_args { 7152729Sdfr int msqid; 7162729Sdfr void *msgp; 7172729Sdfr size_t msgsz; 7182729Sdfr long msgtyp; 7192729Sdfr int msgflg; 7202729Sdfr}; 7212729Sdfr 7222729Sdfrint 7232729Sdfrmsgrcv(p, uap, retval) 7242729Sdfr struct proc *p; 7252729Sdfr register struct msgrcv_args *uap; 7262729Sdfr int *retval; 7272729Sdfr{ 7282729Sdfr int msqid = uap->msqid; 7292729Sdfr void *user_msgp = uap->msgp; 7302729Sdfr size_t msgsz = uap->msgsz; 7312729Sdfr long msgtyp = uap->msgtyp; 7322729Sdfr int msgflg = uap->msgflg; 7332729Sdfr size_t len; 7342729Sdfr struct ucred *cred = p->p_ucred; 7352729Sdfr register struct msqid_ds *msqptr; 7362729Sdfr register struct msg *msghdr; 7372729Sdfr int eval; 7382729Sdfr short next; 7392729Sdfr 7402729Sdfr#ifdef MSG_DEBUG_OK 7412729Sdfr printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp, 7422729Sdfr msgsz, msgtyp, msgflg); 7432729Sdfr#endif 7442729Sdfr 7452729Sdfr msqid = IPCID_TO_IX(msqid); 7462729Sdfr 7472729Sdfr if (msqid < 0 || msqid >= msginfo.msgmni) { 7482729Sdfr#ifdef MSG_DEBUG_OK 7492729Sdfr printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 7502729Sdfr msginfo.msgmni); 7512729Sdfr#endif 7522729Sdfr return(EINVAL); 7532729Sdfr } 7542729Sdfr 7552729Sdfr msqptr = &msqids[msqid]; 7562729Sdfr if (msqptr->msg_qbytes == 0) { 7572729Sdfr#ifdef MSG_DEBUG_OK 7582729Sdfr printf("no such message queue id\n"); 7592729Sdfr#endif 7602729Sdfr return(EINVAL); 7612729Sdfr } 7622729Sdfr if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 7632729Sdfr#ifdef MSG_DEBUG_OK 7642729Sdfr printf("wrong sequence number\n"); 7652729Sdfr#endif 7662729Sdfr return(EINVAL); 7672729Sdfr } 7682729Sdfr 7692729Sdfr if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) { 7702729Sdfr#ifdef MSG_DEBUG_OK 7712729Sdfr printf("requester doesn't have read access\n"); 7722729Sdfr#endif 7732729Sdfr return(eval); 7742729Sdfr } 7752729Sdfr 7762729Sdfr msghdr = NULL; 7772729Sdfr while (msghdr == NULL) { 7782729Sdfr if (msgtyp == 0) { 7792729Sdfr msghdr = msqptr->msg_first; 7802729Sdfr if (msghdr != NULL) { 7812729Sdfr if (msgsz < msghdr->msg_ts && 7822729Sdfr (msgflg & MSG_NOERROR) == 0) { 7832729Sdfr#ifdef MSG_DEBUG_OK 7842729Sdfr printf("first message on the queue is too big (want %d, got %d)\n", 7852729Sdfr msgsz, msghdr->msg_ts); 7862729Sdfr#endif 7872729Sdfr return(E2BIG); 7882729Sdfr } 7892729Sdfr if (msqptr->msg_first == msqptr->msg_last) { 7902729Sdfr msqptr->msg_first = NULL; 7912729Sdfr msqptr->msg_last = NULL; 7922729Sdfr } else { 7932729Sdfr msqptr->msg_first = msghdr->msg_next; 7942729Sdfr if (msqptr->msg_first == NULL) 7952729Sdfr panic("msg_first/last screwed up #1"); 7962729Sdfr } 7972729Sdfr } 7982729Sdfr } else { 7992729Sdfr struct msg *previous; 8002729Sdfr struct msg **prev; 8012729Sdfr 8022729Sdfr previous = NULL; 8032729Sdfr prev = &(msqptr->msg_first); 8042729Sdfr while ((msghdr = *prev) != NULL) { 8052729Sdfr /* 8062729Sdfr * Is this message's type an exact match or is 8072729Sdfr * this message's type less than or equal to 8082729Sdfr * the absolute value of a negative msgtyp? 8092729Sdfr * Note that the second half of this test can 8102729Sdfr * NEVER be true if msgtyp is positive since 8112729Sdfr * msg_type is always positive! 8122729Sdfr */ 8132729Sdfr 8142729Sdfr if (msgtyp == msghdr->msg_type || 8152729Sdfr msghdr->msg_type <= -msgtyp) { 8162729Sdfr#ifdef MSG_DEBUG_OK 8172729Sdfr printf("found message type %d, requested %d\n", 8182729Sdfr msghdr->msg_type, msgtyp); 8192729Sdfr#endif 8202729Sdfr if (msgsz < msghdr->msg_ts && 8212729Sdfr (msgflg & MSG_NOERROR) == 0) { 8222729Sdfr#ifdef MSG_DEBUG_OK 8232729Sdfr printf("requested message on the queue is too big (want %d, got %d)\n", 8242729Sdfr msgsz, msghdr->msg_ts); 8252729Sdfr#endif 8262729Sdfr return(E2BIG); 8272729Sdfr } 8282729Sdfr *prev = msghdr->msg_next; 8292729Sdfr if (msghdr == msqptr->msg_last) { 8302729Sdfr if (previous == NULL) { 8312729Sdfr if (prev != 8322729Sdfr &msqptr->msg_first) 8332729Sdfr panic("msg_first/last screwed up #2"); 8342729Sdfr msqptr->msg_first = 8352729Sdfr NULL; 8362729Sdfr msqptr->msg_last = 8372729Sdfr NULL; 8382729Sdfr } else { 8392729Sdfr if (prev == 8402729Sdfr &msqptr->msg_first) 8412729Sdfr panic("msg_first/last screwed up #3"); 8422729Sdfr msqptr->msg_last = 8432729Sdfr previous; 8442729Sdfr } 8452729Sdfr } 8462729Sdfr break; 8472729Sdfr } 8482729Sdfr previous = msghdr; 8492729Sdfr prev = &(msghdr->msg_next); 8502729Sdfr } 8512729Sdfr } 8522729Sdfr 8532729Sdfr /* 8542729Sdfr * We've either extracted the msghdr for the appropriate 8552729Sdfr * message or there isn't one. 8562729Sdfr * If there is one then bail out of this loop. 8572729Sdfr */ 8582729Sdfr 8592729Sdfr if (msghdr != NULL) 8602729Sdfr break; 8612729Sdfr 8622729Sdfr /* 8632729Sdfr * Hmph! No message found. Does the user want to wait? 8642729Sdfr */ 8652729Sdfr 8662729Sdfr if ((msgflg & IPC_NOWAIT) != 0) { 8672729Sdfr#ifdef MSG_DEBUG_OK 8682729Sdfr printf("no appropriate message found (msgtyp=%d)\n", 8692729Sdfr msgtyp); 8702729Sdfr#endif 8712729Sdfr /* The SVID says to return ENOMSG. */ 8722729Sdfr#ifdef ENOMSG 8732729Sdfr return(ENOMSG); 8742729Sdfr#else 8752729Sdfr /* Unfortunately, BSD doesn't define that code yet! */ 8762729Sdfr return(EAGAIN); 8772729Sdfr#endif 8782729Sdfr } 8792729Sdfr 8802729Sdfr /* 8812729Sdfr * Wait for something to happen 8822729Sdfr */ 8832729Sdfr 8842729Sdfr#ifdef MSG_DEBUG_OK 8852729Sdfr printf("msgrcv: goodnight\n"); 8862729Sdfr#endif 8872729Sdfr eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait", 8882729Sdfr 0); 8892729Sdfr#ifdef MSG_DEBUG_OK 8902729Sdfr printf("msgrcv: good morning (eval=%d)\n", eval); 8912729Sdfr#endif 8922729Sdfr 8932729Sdfr if (eval != 0) { 8942729Sdfr#ifdef MSG_DEBUG_OK 8952729Sdfr printf("msgsnd: interrupted system call\n"); 8962729Sdfr#endif 8972729Sdfr return(EINTR); 8982729Sdfr } 8992729Sdfr 9002729Sdfr /* 9012729Sdfr * Make sure that the msq queue still exists 9022729Sdfr */ 9032729Sdfr 9042729Sdfr if (msqptr->msg_qbytes == 0 || 9052729Sdfr msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 9062729Sdfr#ifdef MSG_DEBUG_OK 9072729Sdfr printf("msqid deleted\n"); 9082729Sdfr#endif 9092729Sdfr /* The SVID says to return EIDRM. */ 9102729Sdfr#ifdef EIDRM 9112729Sdfr return(EIDRM); 9122729Sdfr#else 9132729Sdfr /* Unfortunately, BSD doesn't define that code yet! */ 9142729Sdfr return(EINVAL); 9152729Sdfr#endif 9162729Sdfr } 9172729Sdfr } 9182729Sdfr 9192729Sdfr /* 9202729Sdfr * Return the message to the user. 9212729Sdfr * 9222729Sdfr * First, do the bookkeeping (before we risk being interrupted). 9232729Sdfr */ 9242729Sdfr 9252729Sdfr msqptr->msg_cbytes -= msghdr->msg_ts; 9262729Sdfr msqptr->msg_qnum--; 9272729Sdfr msqptr->msg_lrpid = p->p_pid; 9282729Sdfr msqptr->msg_rtime = time.tv_sec; 9292729Sdfr 9302729Sdfr /* 9312729Sdfr * Make msgsz the actual amount that we'll be returning. 9322729Sdfr * Note that this effectively truncates the message if it is too long 9332729Sdfr * (since msgsz is never increased). 9342729Sdfr */ 9352729Sdfr 9362729Sdfr#ifdef MSG_DEBUG_OK 9372729Sdfr printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz, 9382729Sdfr msghdr->msg_ts); 9392729Sdfr#endif 9402729Sdfr if (msgsz > msghdr->msg_ts) 9412729Sdfr msgsz = msghdr->msg_ts; 9422729Sdfr 9432729Sdfr /* 9442729Sdfr * Return the type to the user. 9452729Sdfr */ 9462729Sdfr 9472729Sdfr eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp, 9482729Sdfr sizeof(msghdr->msg_type)); 9492729Sdfr if (eval != 0) { 9502729Sdfr#ifdef MSG_DEBUG_OK 9512729Sdfr printf("error (%d) copying out message type\n", eval); 9522729Sdfr#endif 9532729Sdfr msg_freehdr(msghdr); 9542729Sdfr wakeup((caddr_t)msqptr); 9552729Sdfr return(eval); 9562729Sdfr } 9572729Sdfr user_msgp += sizeof(msghdr->msg_type); 9582729Sdfr 9592729Sdfr /* 9602729Sdfr * Return the segments to the user 9612729Sdfr */ 9622729Sdfr 9632729Sdfr next = msghdr->msg_spot; 9642729Sdfr for (len = 0; len < msgsz; len += msginfo.msgssz) { 9652729Sdfr size_t tlen; 9662729Sdfr 9672729Sdfr if (msgsz > msginfo.msgssz) 9682729Sdfr tlen = msginfo.msgssz; 9692729Sdfr else 9702729Sdfr tlen = msgsz; 9712729Sdfr if (next <= -1) 9722729Sdfr panic("next too low #3"); 9732729Sdfr if (next >= msginfo.msgseg) 9742729Sdfr panic("next out of range #3"); 9752729Sdfr eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz], 9762729Sdfr user_msgp, tlen); 9772729Sdfr if (eval != 0) { 9782729Sdfr#ifdef MSG_DEBUG_OK 9792729Sdfr printf("error (%d) copying out message segment\n", 9802729Sdfr eval); 9812729Sdfr#endif 9822729Sdfr msg_freehdr(msghdr); 9832729Sdfr wakeup((caddr_t)msqptr); 9842729Sdfr return(eval); 9852729Sdfr } 9862729Sdfr user_msgp += tlen; 9872729Sdfr next = msgmaps[next].next; 9882729Sdfr } 9892729Sdfr 9902729Sdfr /* 9912729Sdfr * Done, return the actual number of bytes copied out. 9922729Sdfr */ 9932729Sdfr 9942729Sdfr msg_freehdr(msghdr); 9952729Sdfr wakeup((caddr_t)msqptr); 9962729Sdfr *retval = msgsz; 9972729Sdfr return(0); 9982729Sdfr} 999