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