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