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