sysv_msg.c revision 88633
150477Speter/* $FreeBSD: head/sys/kern/sysv_msg.c 88633 2001-12-29 07:13:47Z alfred $ */ 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 2259839Speter#include "opt_sysvipc.h" 2359839Speter 242729Sdfr#include <sys/param.h> 252729Sdfr#include <sys/systm.h> 2611626Sbde#include <sys/sysproto.h> 272729Sdfr#include <sys/kernel.h> 282729Sdfr#include <sys/proc.h> 2982607Sdillon#include <sys/lock.h> 3082607Sdillon#include <sys/mutex.h> 312729Sdfr#include <sys/msg.h> 3269449Salfred#include <sys/syscall.h> 3311626Sbde#include <sys/sysent.h> 3459839Speter#include <sys/sysctl.h> 3559839Speter#include <sys/malloc.h> 3668024Srwatson#include <sys/jail.h> 372729Sdfr 3859839Speterstatic MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues"); 3959839Speter 4069449Salfredstatic void msginit __P((void)); 4169449Salfredstatic int msgunload __P((void)); 4269449Salfredstatic int sysvmsg_modload __P((struct module *, int, void *)); 4310358Sjulian 442729Sdfr#define MSG_DEBUG 452729Sdfr#undef MSG_DEBUG_OK 462729Sdfr 4711626Sbdestatic void msg_freehdr __P((struct msg *msghdr)); 482729Sdfr 4911626Sbde/* XXX casting to (sy_call_t *) is bogus, as usual. */ 5012819Sphkstatic sy_call_t *msgcalls[] = { 5111626Sbde (sy_call_t *)msgctl, (sy_call_t *)msgget, 5211626Sbde (sy_call_t *)msgsnd, (sy_call_t *)msgrcv 5311626Sbde}; 542729Sdfr 5559839Speterstruct msg { 5659839Speter struct msg *msg_next; /* next msg in the chain */ 5759839Speter long msg_type; /* type of this message */ 5859839Speter /* >0 -> type of this message */ 5959839Speter /* 0 -> free header */ 6059839Speter u_short msg_ts; /* size of this message */ 6159839Speter short msg_spot; /* location of start of msg in buffer */ 6259839Speter}; 6359839Speter 6459839Speter 6559839Speter#ifndef MSGSSZ 6659839Speter#define MSGSSZ 8 /* Each segment must be 2^N long */ 6759839Speter#endif 6859839Speter#ifndef MSGSEG 6959839Speter#define MSGSEG 2048 /* must be less than 32767 */ 7059839Speter#endif 7159839Speter#define MSGMAX (MSGSSZ*MSGSEG) 7259839Speter#ifndef MSGMNB 7359839Speter#define MSGMNB 2048 /* max # of bytes in a queue */ 7459839Speter#endif 7559839Speter#ifndef MSGMNI 7659839Speter#define MSGMNI 40 7759839Speter#endif 7859839Speter#ifndef MSGTQL 7959839Speter#define MSGTQL 40 8059839Speter#endif 8159839Speter 8259839Speter/* 8359839Speter * Based on the configuration parameters described in an SVR2 (yes, two) 8459839Speter * config(1m) man page. 8559839Speter * 8659839Speter * Each message is broken up and stored in segments that are msgssz bytes 8759839Speter * long. For efficiency reasons, this should be a power of two. Also, 8859839Speter * it doesn't make sense if it is less than 8 or greater than about 256. 8959839Speter * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of 9059839Speter * two between 8 and 1024 inclusive (and panic's if it isn't). 9159839Speter */ 9259839Speterstruct msginfo msginfo = { 9359839Speter MSGMAX, /* max chars in a message */ 9459839Speter MSGMNI, /* # of message queue identifiers */ 9559839Speter MSGMNB, /* max chars in a queue */ 9659839Speter MSGTQL, /* max messages in system */ 9759839Speter MSGSSZ, /* size of a message segment */ 9859839Speter /* (must be small power of 2 greater than 4) */ 9959839Speter MSGSEG /* number of message segments */ 10059839Speter}; 10159839Speter 10259839Speter/* 10359839Speter * macros to convert between msqid_ds's and msqid's. 10459839Speter * (specific to this implementation) 10559839Speter */ 10659839Speter#define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000)) 10759839Speter#define MSQID_IX(id) ((id) & 0xffff) 10859839Speter#define MSQID_SEQ(id) (((id) >> 16) & 0xffff) 10959839Speter 11059839Speter/* 11159839Speter * The rest of this file is specific to this particular implementation. 11259839Speter */ 11359839Speter 11459839Speterstruct msgmap { 11559839Speter short next; /* next segment in buffer */ 11659839Speter /* -1 -> available */ 11759839Speter /* 0..(MSGSEG-1) -> index of next segment */ 11859839Speter}; 11959839Speter 12059839Speter#define MSG_LOCKED 01000 /* Is this msqid_ds locked? */ 12159839Speter 12212819Sphkstatic int nfree_msgmaps; /* # of free map entries */ 12312819Sphkstatic short free_msgmaps; /* head of linked list of free map entries */ 12459839Speterstatic struct msg *free_msghdrs;/* list of free msg headers */ 12559839Speterstatic char *msgpool; /* MSGMAX byte long msg buffer pool */ 12659839Speterstatic struct msgmap *msgmaps; /* MSGSEG msgmap structures */ 12759839Speterstatic struct msg *msghdrs; /* MSGTQL msg headers */ 12859839Speterstatic struct msqid_ds *msqids; /* MSGMNI msqid_ds struct's */ 1292729Sdfr 13059839Speterstatic void 13169449Salfredmsginit() 1322729Sdfr{ 1332729Sdfr register int i; 1342729Sdfr 13583765Smr TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg); 13683765Smr TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz); 13783765Smr msginfo.msgmax = msginfo.msgseg * msginfo.msgssz; 13883765Smr TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni); 13983765Smr 14059839Speter msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK); 14159839Speter if (msgpool == NULL) 14259839Speter panic("msgpool is NULL"); 14359839Speter msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK); 14459839Speter if (msgmaps == NULL) 14559839Speter panic("msgmaps is NULL"); 14659839Speter msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK); 14759839Speter if (msghdrs == NULL) 14859839Speter panic("msghdrs is NULL"); 14959839Speter msqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni, M_MSG, M_WAITOK); 15059839Speter if (msqids == NULL) 15159839Speter panic("msqids is NULL"); 15259839Speter 1532729Sdfr /* 1542729Sdfr * msginfo.msgssz should be a power of two for efficiency reasons. 1552729Sdfr * It is also pretty silly if msginfo.msgssz is less than 8 1562729Sdfr * or greater than about 256 so ... 1572729Sdfr */ 1582729Sdfr 1592729Sdfr i = 8; 1602729Sdfr while (i < 1024 && i != msginfo.msgssz) 1612729Sdfr i <<= 1; 1622729Sdfr if (i != msginfo.msgssz) { 1632729Sdfr printf("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz, 1642729Sdfr msginfo.msgssz); 1652729Sdfr panic("msginfo.msgssz not a small power of 2"); 1662729Sdfr } 1672729Sdfr 1682729Sdfr if (msginfo.msgseg > 32767) { 1692729Sdfr printf("msginfo.msgseg=%d\n", msginfo.msgseg); 1702729Sdfr panic("msginfo.msgseg > 32767"); 1712729Sdfr } 1722729Sdfr 1732729Sdfr if (msgmaps == NULL) 1742729Sdfr panic("msgmaps is NULL"); 1752729Sdfr 1762729Sdfr for (i = 0; i < msginfo.msgseg; i++) { 1772729Sdfr if (i > 0) 1782729Sdfr msgmaps[i-1].next = i; 1792729Sdfr msgmaps[i].next = -1; /* implies entry is available */ 1802729Sdfr } 1812729Sdfr free_msgmaps = 0; 1822729Sdfr nfree_msgmaps = msginfo.msgseg; 1832729Sdfr 1842729Sdfr if (msghdrs == NULL) 1852729Sdfr panic("msghdrs is NULL"); 1862729Sdfr 1872729Sdfr for (i = 0; i < msginfo.msgtql; i++) { 1882729Sdfr msghdrs[i].msg_type = 0; 1892729Sdfr if (i > 0) 1902729Sdfr msghdrs[i-1].msg_next = &msghdrs[i]; 1912729Sdfr msghdrs[i].msg_next = NULL; 1922729Sdfr } 1932729Sdfr free_msghdrs = &msghdrs[0]; 1942729Sdfr 1952729Sdfr if (msqids == NULL) 1962729Sdfr panic("msqids is NULL"); 1972729Sdfr 1982729Sdfr for (i = 0; i < msginfo.msgmni; i++) { 1992729Sdfr msqids[i].msg_qbytes = 0; /* implies entry is available */ 2002729Sdfr msqids[i].msg_perm.seq = 0; /* reset to a known value */ 20166085Speter msqids[i].msg_perm.mode = 0; 2022729Sdfr } 2032729Sdfr} 2042729Sdfr 20569449Salfredstatic int 20669449Salfredmsgunload() 20769449Salfred{ 20869449Salfred struct msqid_ds *msqptr; 20969449Salfred int msqid; 21069449Salfred 21169449Salfred for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 21269449Salfred /* 21369449Salfred * Look for an unallocated and unlocked msqid_ds. 21469449Salfred * msqid_ds's can be locked by msgsnd or msgrcv while 21569449Salfred * they are copying the message in/out. We can't 21669449Salfred * re-use the entry until they release it. 21769449Salfred */ 21869449Salfred msqptr = &msqids[msqid]; 21969449Salfred if (msqptr->msg_qbytes != 0 || 22069449Salfred (msqptr->msg_perm.mode & MSG_LOCKED) != 0) 22169449Salfred break; 22269449Salfred } 22369449Salfred if (msqid != msginfo.msgmni) 22469449Salfred return (EBUSY); 22569449Salfred 22669449Salfred free(msgpool, M_MSG); 22769449Salfred free(msgmaps, M_MSG); 22869449Salfred free(msghdrs, M_MSG); 22969449Salfred free(msqids, M_MSG); 23069449Salfred return (0); 23169449Salfred} 23269449Salfred 23369449Salfred 23469449Salfredstatic int 23569449Salfredsysvmsg_modload(struct module *module, int cmd, void *arg) 23669449Salfred{ 23769449Salfred int error = 0; 23869449Salfred 23969449Salfred switch (cmd) { 24069449Salfred case MOD_LOAD: 24169449Salfred msginit(); 24269449Salfred break; 24369449Salfred case MOD_UNLOAD: 24469449Salfred error = msgunload(); 24569449Salfred break; 24669449Salfred case MOD_SHUTDOWN: 24769449Salfred break; 24869449Salfred default: 24969449Salfred error = EINVAL; 25069449Salfred break; 25169449Salfred } 25269449Salfred return (error); 25369449Salfred} 25469449Salfred 25571038Sdesstatic moduledata_t sysvmsg_mod = { 25671038Sdes "sysvmsg", 25769449Salfred &sysvmsg_modload, 25869449Salfred NULL 25969449Salfred}; 26069449Salfred 26188633SalfredSYSCALL_MODULE_HELPER(msgsys); 26288633SalfredSYSCALL_MODULE_HELPER(msgctl); 26388633SalfredSYSCALL_MODULE_HELPER(msgget); 26488633SalfredSYSCALL_MODULE_HELPER(msgsnd); 26588633SalfredSYSCALL_MODULE_HELPER(msgrcv); 26669449Salfred 26771038SdesDECLARE_MODULE(sysvmsg, sysvmsg_mod, 26869449Salfred SI_SUB_SYSV_MSG, SI_ORDER_FIRST); 26971038SdesMODULE_VERSION(sysvmsg, 1); 27069449Salfred 2712729Sdfr/* 2722729Sdfr * Entry point for all MSG calls 27382607Sdillon * 27482607Sdillon * MPSAFE 2752729Sdfr */ 2762729Sdfrint 27783366Sjulianmsgsys(td, uap) 27883366Sjulian struct thread *td; 27911626Sbde /* XXX actually varargs. */ 28011626Sbde struct msgsys_args /* { 28111626Sbde u_int which; 28211626Sbde int a2; 28311626Sbde int a3; 28411626Sbde int a4; 28511626Sbde int a5; 28611626Sbde int a6; 28711626Sbde } */ *uap; 2882729Sdfr{ 28982607Sdillon int error; 2902729Sdfr 29182607Sdillon mtx_lock(&Giant); 29283366Sjulian if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) { 29382607Sdillon error = ENOSYS; 29482607Sdillon goto done2; 29582607Sdillon } 29682607Sdillon if (uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0])) { 29782607Sdillon error = EINVAL; 29882607Sdillon goto done2; 29982607Sdillon } 30083366Sjulian error = (*msgcalls[uap->which])(td, &uap->a2); 30182607Sdillondone2: 30282607Sdillon mtx_unlock(&Giant); 30382607Sdillon return (error); 3042729Sdfr} 3052729Sdfr 3062729Sdfrstatic void 3072729Sdfrmsg_freehdr(msghdr) 3082729Sdfr struct msg *msghdr; 3092729Sdfr{ 3102729Sdfr while (msghdr->msg_ts > 0) { 3112729Sdfr short next; 3122729Sdfr if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg) 3132729Sdfr panic("msghdr->msg_spot out of range"); 3142729Sdfr next = msgmaps[msghdr->msg_spot].next; 3152729Sdfr msgmaps[msghdr->msg_spot].next = free_msgmaps; 3162729Sdfr free_msgmaps = msghdr->msg_spot; 3172729Sdfr nfree_msgmaps++; 3182729Sdfr msghdr->msg_spot = next; 3192729Sdfr if (msghdr->msg_ts >= msginfo.msgssz) 3202729Sdfr msghdr->msg_ts -= msginfo.msgssz; 3212729Sdfr else 3222729Sdfr msghdr->msg_ts = 0; 3232729Sdfr } 3242729Sdfr if (msghdr->msg_spot != -1) 3252729Sdfr panic("msghdr->msg_spot != -1"); 3262729Sdfr msghdr->msg_next = free_msghdrs; 3272729Sdfr free_msghdrs = msghdr; 3282729Sdfr} 3292729Sdfr 33012866Speter#ifndef _SYS_SYSPROTO_H_ 3312729Sdfrstruct msgctl_args { 3322729Sdfr int msqid; 3332729Sdfr int cmd; 33412866Speter struct msqid_ds *buf; 3352729Sdfr}; 33612866Speter#endif 3372729Sdfr 33882607Sdillon/* 33982607Sdillon * MPSAFE 34082607Sdillon */ 34112866Speterint 34283366Sjulianmsgctl(td, uap) 34383366Sjulian struct thread *td; 3442729Sdfr register struct msgctl_args *uap; 3452729Sdfr{ 3462729Sdfr int msqid = uap->msqid; 3472729Sdfr int cmd = uap->cmd; 34812866Speter struct msqid_ds *user_msqptr = uap->buf; 34982607Sdillon int rval, error; 3502729Sdfr struct msqid_ds msqbuf; 3512729Sdfr register struct msqid_ds *msqptr; 3522729Sdfr 3532729Sdfr#ifdef MSG_DEBUG_OK 3542729Sdfr printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr); 3552729Sdfr#endif 35682607Sdillon mtx_lock(&Giant); 35783366Sjulian if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) { 35882607Sdillon error = ENOSYS; 35982607Sdillon goto done2; 36082607Sdillon } 36168024Srwatson 3622729Sdfr msqid = IPCID_TO_IX(msqid); 3632729Sdfr 3642729Sdfr if (msqid < 0 || msqid >= msginfo.msgmni) { 3652729Sdfr#ifdef MSG_DEBUG_OK 3662729Sdfr printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 3672729Sdfr msginfo.msgmni); 3682729Sdfr#endif 36982607Sdillon error = EINVAL; 37082607Sdillon goto done2; 3712729Sdfr } 3722729Sdfr 3732729Sdfr msqptr = &msqids[msqid]; 3742729Sdfr 3752729Sdfr if (msqptr->msg_qbytes == 0) { 3762729Sdfr#ifdef MSG_DEBUG_OK 3772729Sdfr printf("no such msqid\n"); 3782729Sdfr#endif 37982607Sdillon error = EINVAL; 38082607Sdillon goto done2; 3812729Sdfr } 3822729Sdfr if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 3832729Sdfr#ifdef MSG_DEBUG_OK 3842729Sdfr printf("wrong sequence number\n"); 3852729Sdfr#endif 38682607Sdillon error = EINVAL; 38782607Sdillon goto done2; 3882729Sdfr } 3892729Sdfr 39082607Sdillon error = 0; 3912729Sdfr rval = 0; 3922729Sdfr 3932729Sdfr switch (cmd) { 3942729Sdfr 3952729Sdfr case IPC_RMID: 3962729Sdfr { 3972729Sdfr struct msg *msghdr; 39883366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M))) 39982607Sdillon goto done2; 4002729Sdfr /* Free the message headers */ 4012729Sdfr msghdr = msqptr->msg_first; 4022729Sdfr while (msghdr != NULL) { 4032729Sdfr struct msg *msghdr_tmp; 4042729Sdfr 4052729Sdfr /* Free the segments of each message */ 4062729Sdfr msqptr->msg_cbytes -= msghdr->msg_ts; 4072729Sdfr msqptr->msg_qnum--; 4082729Sdfr msghdr_tmp = msghdr; 4092729Sdfr msghdr = msghdr->msg_next; 4102729Sdfr msg_freehdr(msghdr_tmp); 4112729Sdfr } 4122729Sdfr 4132729Sdfr if (msqptr->msg_cbytes != 0) 4142729Sdfr panic("msg_cbytes is screwed up"); 4152729Sdfr if (msqptr->msg_qnum != 0) 4162729Sdfr panic("msg_qnum is screwed up"); 4172729Sdfr 4182729Sdfr msqptr->msg_qbytes = 0; /* Mark it as free */ 4192729Sdfr 4202729Sdfr wakeup((caddr_t)msqptr); 4212729Sdfr } 4222729Sdfr 4232729Sdfr break; 4242729Sdfr 4252729Sdfr case IPC_SET: 42683366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M))) 42782607Sdillon goto done2; 42882607Sdillon if ((error = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0) 42982607Sdillon goto done2; 43043426Sphk if (msqbuf.msg_qbytes > msqptr->msg_qbytes) { 43183366Sjulian error = suser_td(td); 43282607Sdillon if (error) 43382607Sdillon goto done2; 43443426Sphk } 4352729Sdfr if (msqbuf.msg_qbytes > msginfo.msgmnb) { 4362729Sdfr#ifdef MSG_DEBUG_OK 4372729Sdfr printf("can't increase msg_qbytes beyond %d (truncating)\n", 4382729Sdfr msginfo.msgmnb); 4392729Sdfr#endif 4402729Sdfr msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */ 4412729Sdfr } 4422729Sdfr if (msqbuf.msg_qbytes == 0) { 4432729Sdfr#ifdef MSG_DEBUG_OK 4442729Sdfr printf("can't reduce msg_qbytes to 0\n"); 4452729Sdfr#endif 44682607Sdillon error = EINVAL; /* non-standard errno! */ 44782607Sdillon goto done2; 4482729Sdfr } 4492729Sdfr msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */ 4502729Sdfr msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */ 4512729Sdfr msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) | 4522729Sdfr (msqbuf.msg_perm.mode & 0777); 4532729Sdfr msqptr->msg_qbytes = msqbuf.msg_qbytes; 45434961Sphk msqptr->msg_ctime = time_second; 4552729Sdfr break; 4562729Sdfr 4572729Sdfr case IPC_STAT: 45883366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) { 4592729Sdfr#ifdef MSG_DEBUG_OK 4602729Sdfr printf("requester doesn't have read access\n"); 4612729Sdfr#endif 46282607Sdillon goto done2; 4632729Sdfr } 46482607Sdillon error = copyout((caddr_t)msqptr, user_msqptr, 4652729Sdfr sizeof(struct msqid_ds)); 4662729Sdfr break; 4672729Sdfr 4682729Sdfr default: 4692729Sdfr#ifdef MSG_DEBUG_OK 4702729Sdfr printf("invalid command %d\n", cmd); 4712729Sdfr#endif 47282607Sdillon error = EINVAL; 47382607Sdillon goto done2; 4742729Sdfr } 4752729Sdfr 47682607Sdillon if (error == 0) 47783366Sjulian td->td_retval[0] = rval; 47882607Sdillondone2: 47982607Sdillon mtx_unlock(&Giant); 48082607Sdillon return(error); 4812729Sdfr} 4822729Sdfr 48312866Speter#ifndef _SYS_SYSPROTO_H_ 4842729Sdfrstruct msgget_args { 4852729Sdfr key_t key; 4862729Sdfr int msgflg; 4872729Sdfr}; 48812866Speter#endif 4892729Sdfr 49082607Sdillon/* 49182607Sdillon * MPSAFE 49282607Sdillon */ 49312866Speterint 49483366Sjulianmsgget(td, uap) 49583366Sjulian struct thread *td; 4962729Sdfr register struct msgget_args *uap; 4972729Sdfr{ 49882607Sdillon int msqid, error = 0; 4992729Sdfr int key = uap->key; 5002729Sdfr int msgflg = uap->msgflg; 50183366Sjulian struct ucred *cred = td->td_proc->p_ucred; 5022836Sdg register struct msqid_ds *msqptr = NULL; 5032729Sdfr 5042729Sdfr#ifdef MSG_DEBUG_OK 5052729Sdfr printf("msgget(0x%x, 0%o)\n", key, msgflg); 5062729Sdfr#endif 5072729Sdfr 50882607Sdillon mtx_lock(&Giant); 50983366Sjulian if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) { 51082607Sdillon error = ENOSYS; 51182607Sdillon goto done2; 51282607Sdillon } 51382607Sdillon 5142729Sdfr if (key != IPC_PRIVATE) { 5152729Sdfr for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 5162729Sdfr msqptr = &msqids[msqid]; 5172729Sdfr if (msqptr->msg_qbytes != 0 && 5182729Sdfr msqptr->msg_perm.key == key) 5192729Sdfr break; 5202729Sdfr } 5212729Sdfr if (msqid < msginfo.msgmni) { 5222729Sdfr#ifdef MSG_DEBUG_OK 5232729Sdfr printf("found public key\n"); 5242729Sdfr#endif 5252729Sdfr if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) { 5262729Sdfr#ifdef MSG_DEBUG_OK 5272729Sdfr printf("not exclusive\n"); 5282729Sdfr#endif 52982607Sdillon error = EEXIST; 53082607Sdillon goto done2; 5312729Sdfr } 53283366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, msgflg & 0700 ))) { 5332729Sdfr#ifdef MSG_DEBUG_OK 5342729Sdfr printf("requester doesn't have 0%o access\n", 5352729Sdfr msgflg & 0700); 5362729Sdfr#endif 53782607Sdillon goto done2; 5382729Sdfr } 5392729Sdfr goto found; 5402729Sdfr } 5412729Sdfr } 5422729Sdfr 5432729Sdfr#ifdef MSG_DEBUG_OK 5442729Sdfr printf("need to allocate the msqid_ds\n"); 5452729Sdfr#endif 5462729Sdfr if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) { 5472729Sdfr for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 5482729Sdfr /* 5492729Sdfr * Look for an unallocated and unlocked msqid_ds. 5502729Sdfr * msqid_ds's can be locked by msgsnd or msgrcv while 5512729Sdfr * they are copying the message in/out. We can't 5522729Sdfr * re-use the entry until they release it. 5532729Sdfr */ 5542729Sdfr msqptr = &msqids[msqid]; 5552729Sdfr if (msqptr->msg_qbytes == 0 && 5562729Sdfr (msqptr->msg_perm.mode & MSG_LOCKED) == 0) 5572729Sdfr break; 5582729Sdfr } 5592729Sdfr if (msqid == msginfo.msgmni) { 5602729Sdfr#ifdef MSG_DEBUG_OK 5612729Sdfr printf("no more msqid_ds's available\n"); 5622729Sdfr#endif 56382607Sdillon error = ENOSPC; 56482607Sdillon goto done2; 5652729Sdfr } 5662729Sdfr#ifdef MSG_DEBUG_OK 5672729Sdfr printf("msqid %d is available\n", msqid); 5682729Sdfr#endif 5692729Sdfr msqptr->msg_perm.key = key; 5702729Sdfr msqptr->msg_perm.cuid = cred->cr_uid; 5712729Sdfr msqptr->msg_perm.uid = cred->cr_uid; 5722729Sdfr msqptr->msg_perm.cgid = cred->cr_gid; 5732729Sdfr msqptr->msg_perm.gid = cred->cr_gid; 5742729Sdfr msqptr->msg_perm.mode = (msgflg & 0777); 5752729Sdfr /* Make sure that the returned msqid is unique */ 5762729Sdfr msqptr->msg_perm.seq++; 5772729Sdfr msqptr->msg_first = NULL; 5782729Sdfr msqptr->msg_last = NULL; 5792729Sdfr msqptr->msg_cbytes = 0; 5802729Sdfr msqptr->msg_qnum = 0; 5812729Sdfr msqptr->msg_qbytes = msginfo.msgmnb; 5822729Sdfr msqptr->msg_lspid = 0; 5832729Sdfr msqptr->msg_lrpid = 0; 5842729Sdfr msqptr->msg_stime = 0; 5852729Sdfr msqptr->msg_rtime = 0; 58634961Sphk msqptr->msg_ctime = time_second; 5872729Sdfr } else { 5882729Sdfr#ifdef MSG_DEBUG_OK 5892729Sdfr printf("didn't find it and wasn't asked to create it\n"); 5902729Sdfr#endif 59182607Sdillon error = ENOENT; 59282607Sdillon goto done2; 5932729Sdfr } 5942729Sdfr 5952729Sdfrfound: 5962729Sdfr /* Construct the unique msqid */ 59783366Sjulian td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm); 59882607Sdillondone2: 59982607Sdillon mtx_unlock(&Giant); 60082607Sdillon return (error); 6012729Sdfr} 6022729Sdfr 60312866Speter#ifndef _SYS_SYSPROTO_H_ 6042729Sdfrstruct msgsnd_args { 6052729Sdfr int msqid; 60612866Speter void *msgp; 6072729Sdfr size_t msgsz; 6082729Sdfr int msgflg; 6092729Sdfr}; 61012866Speter#endif 6112729Sdfr 61282607Sdillon/* 61382607Sdillon * MPSAFE 61482607Sdillon */ 61512866Speterint 61683366Sjulianmsgsnd(td, uap) 61783366Sjulian struct thread *td; 6182729Sdfr register struct msgsnd_args *uap; 6192729Sdfr{ 6202729Sdfr int msqid = uap->msqid; 62112866Speter void *user_msgp = uap->msgp; 6222729Sdfr size_t msgsz = uap->msgsz; 6232729Sdfr int msgflg = uap->msgflg; 62482607Sdillon int segs_needed, error = 0; 6252729Sdfr register struct msqid_ds *msqptr; 6262729Sdfr register struct msg *msghdr; 6272729Sdfr short next; 6282729Sdfr 6292729Sdfr#ifdef MSG_DEBUG_OK 6302729Sdfr printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz, 6312729Sdfr msgflg); 6322729Sdfr#endif 63382607Sdillon mtx_lock(&Giant); 63483366Sjulian if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) { 63582607Sdillon error = ENOSYS; 63682607Sdillon goto done2; 63782607Sdillon } 63868024Srwatson 6392729Sdfr msqid = IPCID_TO_IX(msqid); 6402729Sdfr 6412729Sdfr if (msqid < 0 || msqid >= msginfo.msgmni) { 6422729Sdfr#ifdef MSG_DEBUG_OK 6432729Sdfr printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 6442729Sdfr msginfo.msgmni); 6452729Sdfr#endif 64682607Sdillon error = EINVAL; 64782607Sdillon goto done2; 6482729Sdfr } 6492729Sdfr 6502729Sdfr msqptr = &msqids[msqid]; 6512729Sdfr if (msqptr->msg_qbytes == 0) { 6522729Sdfr#ifdef MSG_DEBUG_OK 6532729Sdfr printf("no such message queue id\n"); 6542729Sdfr#endif 65582607Sdillon error = EINVAL; 65682607Sdillon goto done2; 6572729Sdfr } 6582729Sdfr if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 6592729Sdfr#ifdef MSG_DEBUG_OK 6602729Sdfr printf("wrong sequence number\n"); 6612729Sdfr#endif 66282607Sdillon error = EINVAL; 66382607Sdillon goto done2; 6642729Sdfr } 6652729Sdfr 66683366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_W))) { 6672729Sdfr#ifdef MSG_DEBUG_OK 6682729Sdfr printf("requester doesn't have write access\n"); 6692729Sdfr#endif 67082607Sdillon goto done2; 6712729Sdfr } 6722729Sdfr 6732729Sdfr segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz; 6742729Sdfr#ifdef MSG_DEBUG_OK 6752729Sdfr printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz, 6762729Sdfr segs_needed); 6772729Sdfr#endif 6782729Sdfr for (;;) { 6792729Sdfr int need_more_resources = 0; 6802729Sdfr 6812729Sdfr /* 6822729Sdfr * check msgsz 6832729Sdfr * (inside this loop in case msg_qbytes changes while we sleep) 6842729Sdfr */ 6852729Sdfr 6862836Sdg if (msgsz > msqptr->msg_qbytes) { 6872729Sdfr#ifdef MSG_DEBUG_OK 6882729Sdfr printf("msgsz > msqptr->msg_qbytes\n"); 6892729Sdfr#endif 69082607Sdillon error = EINVAL; 69182607Sdillon goto done2; 6922729Sdfr } 6932729Sdfr 6942729Sdfr if (msqptr->msg_perm.mode & MSG_LOCKED) { 6952729Sdfr#ifdef MSG_DEBUG_OK 6962729Sdfr printf("msqid is locked\n"); 6972729Sdfr#endif 6982729Sdfr need_more_resources = 1; 6992729Sdfr } 7002729Sdfr if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) { 7012729Sdfr#ifdef MSG_DEBUG_OK 7022729Sdfr printf("msgsz + msg_cbytes > msg_qbytes\n"); 7032729Sdfr#endif 7042729Sdfr need_more_resources = 1; 7052729Sdfr } 7062729Sdfr if (segs_needed > nfree_msgmaps) { 7072729Sdfr#ifdef MSG_DEBUG_OK 7082729Sdfr printf("segs_needed > nfree_msgmaps\n"); 7092729Sdfr#endif 7102729Sdfr need_more_resources = 1; 7112729Sdfr } 7122729Sdfr if (free_msghdrs == NULL) { 7132729Sdfr#ifdef MSG_DEBUG_OK 7142729Sdfr printf("no more msghdrs\n"); 7152729Sdfr#endif 7162729Sdfr need_more_resources = 1; 7172729Sdfr } 7182729Sdfr 7192729Sdfr if (need_more_resources) { 7202729Sdfr int we_own_it; 7212729Sdfr 7222729Sdfr if ((msgflg & IPC_NOWAIT) != 0) { 7232729Sdfr#ifdef MSG_DEBUG_OK 7242729Sdfr printf("need more resources but caller doesn't want to wait\n"); 7252729Sdfr#endif 72682607Sdillon error = EAGAIN; 72782607Sdillon goto done2; 7282729Sdfr } 7292729Sdfr 7302729Sdfr if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) { 7312729Sdfr#ifdef MSG_DEBUG_OK 7322729Sdfr printf("we don't own the msqid_ds\n"); 7332729Sdfr#endif 7342729Sdfr we_own_it = 0; 7352729Sdfr } else { 7362729Sdfr /* Force later arrivals to wait for our 7372729Sdfr request */ 7382729Sdfr#ifdef MSG_DEBUG_OK 7392729Sdfr printf("we own the msqid_ds\n"); 7402729Sdfr#endif 7412729Sdfr msqptr->msg_perm.mode |= MSG_LOCKED; 7422729Sdfr we_own_it = 1; 7432729Sdfr } 7442729Sdfr#ifdef MSG_DEBUG_OK 7452729Sdfr printf("goodnight\n"); 7462729Sdfr#endif 74782607Sdillon error = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, 7482729Sdfr "msgwait", 0); 7492729Sdfr#ifdef MSG_DEBUG_OK 75082607Sdillon printf("good morning, error=%d\n", error); 7512729Sdfr#endif 7522729Sdfr if (we_own_it) 7532729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 75482607Sdillon if (error != 0) { 7552729Sdfr#ifdef MSG_DEBUG_OK 7562729Sdfr printf("msgsnd: interrupted system call\n"); 7572729Sdfr#endif 75882607Sdillon error = EINTR; 75982607Sdillon goto done2; 7602729Sdfr } 7612729Sdfr 7622729Sdfr /* 7632729Sdfr * Make sure that the msq queue still exists 7642729Sdfr */ 7652729Sdfr 7662729Sdfr if (msqptr->msg_qbytes == 0) { 7672729Sdfr#ifdef MSG_DEBUG_OK 7682729Sdfr printf("msqid deleted\n"); 7692729Sdfr#endif 77082607Sdillon error = EIDRM; 77182607Sdillon goto done2; 7722729Sdfr } 7732729Sdfr 7742729Sdfr } else { 7752729Sdfr#ifdef MSG_DEBUG_OK 7762729Sdfr printf("got all the resources that we need\n"); 7772729Sdfr#endif 7782729Sdfr break; 7792729Sdfr } 7802729Sdfr } 7812729Sdfr 7822729Sdfr /* 7832729Sdfr * We have the resources that we need. 7842729Sdfr * Make sure! 7852729Sdfr */ 7862729Sdfr 7872729Sdfr if (msqptr->msg_perm.mode & MSG_LOCKED) 7882729Sdfr panic("msg_perm.mode & MSG_LOCKED"); 7892729Sdfr if (segs_needed > nfree_msgmaps) 7902729Sdfr panic("segs_needed > nfree_msgmaps"); 7912729Sdfr if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) 7922729Sdfr panic("msgsz + msg_cbytes > msg_qbytes"); 7932729Sdfr if (free_msghdrs == NULL) 7942729Sdfr panic("no more msghdrs"); 7952729Sdfr 7962729Sdfr /* 7972729Sdfr * Re-lock the msqid_ds in case we page-fault when copying in the 7982729Sdfr * message 7992729Sdfr */ 8002729Sdfr 8012729Sdfr if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) 8022729Sdfr panic("msqid_ds is already locked"); 8032729Sdfr msqptr->msg_perm.mode |= MSG_LOCKED; 8042729Sdfr 8052729Sdfr /* 8062729Sdfr * Allocate a message header 8072729Sdfr */ 8082729Sdfr 8092729Sdfr msghdr = free_msghdrs; 8102729Sdfr free_msghdrs = msghdr->msg_next; 8112729Sdfr msghdr->msg_spot = -1; 8122729Sdfr msghdr->msg_ts = msgsz; 8132729Sdfr 8142729Sdfr /* 8152729Sdfr * Allocate space for the message 8162729Sdfr */ 8172729Sdfr 8182729Sdfr while (segs_needed > 0) { 8192729Sdfr if (nfree_msgmaps <= 0) 8202729Sdfr panic("not enough msgmaps"); 8212729Sdfr if (free_msgmaps == -1) 8222729Sdfr panic("nil free_msgmaps"); 8232729Sdfr next = free_msgmaps; 8242729Sdfr if (next <= -1) 8252729Sdfr panic("next too low #1"); 8262729Sdfr if (next >= msginfo.msgseg) 8272729Sdfr panic("next out of range #1"); 8282729Sdfr#ifdef MSG_DEBUG_OK 8292729Sdfr printf("allocating segment %d to message\n", next); 8302729Sdfr#endif 8312729Sdfr free_msgmaps = msgmaps[next].next; 8322729Sdfr nfree_msgmaps--; 8332729Sdfr msgmaps[next].next = msghdr->msg_spot; 8342729Sdfr msghdr->msg_spot = next; 8352729Sdfr segs_needed--; 8362729Sdfr } 8372729Sdfr 8382729Sdfr /* 8392729Sdfr * Copy in the message type 8402729Sdfr */ 8412729Sdfr 84282607Sdillon if ((error = copyin(user_msgp, &msghdr->msg_type, 8432729Sdfr sizeof(msghdr->msg_type))) != 0) { 8442729Sdfr#ifdef MSG_DEBUG_OK 84582607Sdillon printf("error %d copying the message type\n", error); 8462729Sdfr#endif 8472729Sdfr msg_freehdr(msghdr); 8482729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 8492729Sdfr wakeup((caddr_t)msqptr); 85082607Sdillon goto done2; 8512729Sdfr } 85217971Sbde user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type); 8532729Sdfr 8542729Sdfr /* 8552729Sdfr * Validate the message type 8562729Sdfr */ 8572729Sdfr 8582729Sdfr if (msghdr->msg_type < 1) { 8592729Sdfr msg_freehdr(msghdr); 8602729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 8612729Sdfr wakeup((caddr_t)msqptr); 8622729Sdfr#ifdef MSG_DEBUG_OK 8632729Sdfr printf("mtype (%d) < 1\n", msghdr->msg_type); 8642729Sdfr#endif 86582607Sdillon error = EINVAL; 86682607Sdillon goto done2; 8672729Sdfr } 8682729Sdfr 8692729Sdfr /* 8702729Sdfr * Copy in the message body 8712729Sdfr */ 8722729Sdfr 8732729Sdfr next = msghdr->msg_spot; 8742729Sdfr while (msgsz > 0) { 8752729Sdfr size_t tlen; 8762729Sdfr if (msgsz > msginfo.msgssz) 8772729Sdfr tlen = msginfo.msgssz; 8782729Sdfr else 8792729Sdfr tlen = msgsz; 8802729Sdfr if (next <= -1) 8812729Sdfr panic("next too low #2"); 8822729Sdfr if (next >= msginfo.msgseg) 8832729Sdfr panic("next out of range #2"); 88482607Sdillon if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz], 8852729Sdfr tlen)) != 0) { 8862729Sdfr#ifdef MSG_DEBUG_OK 88782607Sdillon printf("error %d copying in message segment\n", error); 8882729Sdfr#endif 8892729Sdfr msg_freehdr(msghdr); 8902729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 8912729Sdfr wakeup((caddr_t)msqptr); 89282607Sdillon goto done2; 8932729Sdfr } 8942729Sdfr msgsz -= tlen; 89517971Sbde user_msgp = (char *)user_msgp + tlen; 8962729Sdfr next = msgmaps[next].next; 8972729Sdfr } 8982729Sdfr if (next != -1) 8992729Sdfr panic("didn't use all the msg segments"); 9002729Sdfr 9012729Sdfr /* 9022729Sdfr * We've got the message. Unlock the msqid_ds. 9032729Sdfr */ 9042729Sdfr 9052729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 9062729Sdfr 9072729Sdfr /* 9082729Sdfr * Make sure that the msqid_ds is still allocated. 9092729Sdfr */ 9102729Sdfr 9112729Sdfr if (msqptr->msg_qbytes == 0) { 9122729Sdfr msg_freehdr(msghdr); 9132729Sdfr wakeup((caddr_t)msqptr); 91482607Sdillon error = EIDRM; 91582607Sdillon goto done2; 9162729Sdfr } 9172729Sdfr 9182729Sdfr /* 9192729Sdfr * Put the message into the queue 9202729Sdfr */ 9212729Sdfr 9222729Sdfr if (msqptr->msg_first == NULL) { 9232729Sdfr msqptr->msg_first = msghdr; 9242729Sdfr msqptr->msg_last = msghdr; 9252729Sdfr } else { 9262729Sdfr msqptr->msg_last->msg_next = msghdr; 9272729Sdfr msqptr->msg_last = msghdr; 9282729Sdfr } 9292729Sdfr msqptr->msg_last->msg_next = NULL; 9302729Sdfr 9312729Sdfr msqptr->msg_cbytes += msghdr->msg_ts; 9322729Sdfr msqptr->msg_qnum++; 93383366Sjulian msqptr->msg_lspid = td->td_proc->p_pid; 93434961Sphk msqptr->msg_stime = time_second; 9352729Sdfr 9362729Sdfr wakeup((caddr_t)msqptr); 93783366Sjulian td->td_retval[0] = 0; 93882607Sdillondone2: 93982607Sdillon mtx_unlock(&Giant); 94082607Sdillon return (error); 9412729Sdfr} 9422729Sdfr 94312866Speter#ifndef _SYS_SYSPROTO_H_ 9442729Sdfrstruct msgrcv_args { 9452729Sdfr int msqid; 9462729Sdfr void *msgp; 9472729Sdfr size_t msgsz; 9482729Sdfr long msgtyp; 9492729Sdfr int msgflg; 9502729Sdfr}; 95112866Speter#endif 9522729Sdfr 95382607Sdillon/* 95482607Sdillon * MPSAFE 95582607Sdillon */ 95612866Speterint 95783366Sjulianmsgrcv(td, uap) 95883366Sjulian struct thread *td; 9592729Sdfr register struct msgrcv_args *uap; 9602729Sdfr{ 9612729Sdfr int msqid = uap->msqid; 9622729Sdfr void *user_msgp = uap->msgp; 9632729Sdfr size_t msgsz = uap->msgsz; 9642729Sdfr long msgtyp = uap->msgtyp; 9652729Sdfr int msgflg = uap->msgflg; 9662729Sdfr size_t len; 9672729Sdfr register struct msqid_ds *msqptr; 9682729Sdfr register struct msg *msghdr; 96982607Sdillon int error = 0; 9702729Sdfr short next; 9712729Sdfr 9722729Sdfr#ifdef MSG_DEBUG_OK 9732729Sdfr printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp, 9742729Sdfr msgsz, msgtyp, msgflg); 9752729Sdfr#endif 9762729Sdfr 97782607Sdillon mtx_lock(&Giant); 97883366Sjulian if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) { 97982607Sdillon error = ENOSYS; 98082607Sdillon goto done2; 98182607Sdillon } 98282607Sdillon 9832729Sdfr msqid = IPCID_TO_IX(msqid); 9842729Sdfr 9852729Sdfr if (msqid < 0 || msqid >= msginfo.msgmni) { 9862729Sdfr#ifdef MSG_DEBUG_OK 9872729Sdfr printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 9882729Sdfr msginfo.msgmni); 9892729Sdfr#endif 99082607Sdillon error = EINVAL; 99182607Sdillon goto done2; 9922729Sdfr } 9932729Sdfr 9942729Sdfr msqptr = &msqids[msqid]; 9952729Sdfr if (msqptr->msg_qbytes == 0) { 9962729Sdfr#ifdef MSG_DEBUG_OK 9972729Sdfr printf("no such message queue id\n"); 9982729Sdfr#endif 99982607Sdillon error = EINVAL; 100082607Sdillon goto done2; 10012729Sdfr } 10022729Sdfr if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 10032729Sdfr#ifdef MSG_DEBUG_OK 10042729Sdfr printf("wrong sequence number\n"); 10052729Sdfr#endif 100682607Sdillon error = EINVAL; 100782607Sdillon goto done2; 10082729Sdfr } 10092729Sdfr 101083366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) { 10112729Sdfr#ifdef MSG_DEBUG_OK 10122729Sdfr printf("requester doesn't have read access\n"); 10132729Sdfr#endif 101482607Sdillon goto done2; 10152729Sdfr } 10162729Sdfr 10172729Sdfr msghdr = NULL; 10182729Sdfr while (msghdr == NULL) { 10192729Sdfr if (msgtyp == 0) { 10202729Sdfr msghdr = msqptr->msg_first; 10212729Sdfr if (msghdr != NULL) { 10222729Sdfr if (msgsz < msghdr->msg_ts && 10232729Sdfr (msgflg & MSG_NOERROR) == 0) { 10242729Sdfr#ifdef MSG_DEBUG_OK 10252729Sdfr printf("first message on the queue is too big (want %d, got %d)\n", 10262729Sdfr msgsz, msghdr->msg_ts); 10272729Sdfr#endif 102882607Sdillon error = E2BIG; 102982607Sdillon goto done2; 10302729Sdfr } 10312729Sdfr if (msqptr->msg_first == msqptr->msg_last) { 10322729Sdfr msqptr->msg_first = NULL; 10332729Sdfr msqptr->msg_last = NULL; 10342729Sdfr } else { 10352729Sdfr msqptr->msg_first = msghdr->msg_next; 10362729Sdfr if (msqptr->msg_first == NULL) 10372729Sdfr panic("msg_first/last screwed up #1"); 10382729Sdfr } 10392729Sdfr } 10402729Sdfr } else { 10412729Sdfr struct msg *previous; 10422729Sdfr struct msg **prev; 10432729Sdfr 10442729Sdfr previous = NULL; 10452729Sdfr prev = &(msqptr->msg_first); 10462729Sdfr while ((msghdr = *prev) != NULL) { 10472729Sdfr /* 10482729Sdfr * Is this message's type an exact match or is 10492729Sdfr * this message's type less than or equal to 10502729Sdfr * the absolute value of a negative msgtyp? 10512729Sdfr * Note that the second half of this test can 10522729Sdfr * NEVER be true if msgtyp is positive since 10532729Sdfr * msg_type is always positive! 10542729Sdfr */ 10552729Sdfr 10562729Sdfr if (msgtyp == msghdr->msg_type || 10572729Sdfr msghdr->msg_type <= -msgtyp) { 10582729Sdfr#ifdef MSG_DEBUG_OK 10592729Sdfr printf("found message type %d, requested %d\n", 10602729Sdfr msghdr->msg_type, msgtyp); 10612729Sdfr#endif 10622729Sdfr if (msgsz < msghdr->msg_ts && 10632729Sdfr (msgflg & MSG_NOERROR) == 0) { 10642729Sdfr#ifdef MSG_DEBUG_OK 10652729Sdfr printf("requested message on the queue is too big (want %d, got %d)\n", 10662729Sdfr msgsz, msghdr->msg_ts); 10672729Sdfr#endif 106882607Sdillon error = E2BIG; 106982607Sdillon goto done2; 10702729Sdfr } 10712729Sdfr *prev = msghdr->msg_next; 10722729Sdfr if (msghdr == msqptr->msg_last) { 10732729Sdfr if (previous == NULL) { 10742729Sdfr if (prev != 10752729Sdfr &msqptr->msg_first) 10762729Sdfr panic("msg_first/last screwed up #2"); 10772729Sdfr msqptr->msg_first = 10782729Sdfr NULL; 10792729Sdfr msqptr->msg_last = 10802729Sdfr NULL; 10812729Sdfr } else { 10822729Sdfr if (prev == 10832729Sdfr &msqptr->msg_first) 10842729Sdfr panic("msg_first/last screwed up #3"); 10852729Sdfr msqptr->msg_last = 10862729Sdfr previous; 10872729Sdfr } 10882729Sdfr } 10892729Sdfr break; 10902729Sdfr } 10912729Sdfr previous = msghdr; 10922729Sdfr prev = &(msghdr->msg_next); 10932729Sdfr } 10942729Sdfr } 10952729Sdfr 10962729Sdfr /* 10972729Sdfr * We've either extracted the msghdr for the appropriate 10982729Sdfr * message or there isn't one. 10992729Sdfr * If there is one then bail out of this loop. 11002729Sdfr */ 11012729Sdfr 11022729Sdfr if (msghdr != NULL) 11032729Sdfr break; 11042729Sdfr 11052729Sdfr /* 11062729Sdfr * Hmph! No message found. Does the user want to wait? 11072729Sdfr */ 11082729Sdfr 11092729Sdfr if ((msgflg & IPC_NOWAIT) != 0) { 11102729Sdfr#ifdef MSG_DEBUG_OK 11112729Sdfr printf("no appropriate message found (msgtyp=%d)\n", 11122729Sdfr msgtyp); 11132729Sdfr#endif 11142729Sdfr /* The SVID says to return ENOMSG. */ 111582607Sdillon error = ENOMSG; 111682607Sdillon goto done2; 11172729Sdfr } 11182729Sdfr 11192729Sdfr /* 11202729Sdfr * Wait for something to happen 11212729Sdfr */ 11222729Sdfr 11232729Sdfr#ifdef MSG_DEBUG_OK 11242729Sdfr printf("msgrcv: goodnight\n"); 11252729Sdfr#endif 112682607Sdillon error = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait", 11272729Sdfr 0); 11282729Sdfr#ifdef MSG_DEBUG_OK 112982607Sdillon printf("msgrcv: good morning (error=%d)\n", error); 11302729Sdfr#endif 11312729Sdfr 113282607Sdillon if (error != 0) { 11332729Sdfr#ifdef MSG_DEBUG_OK 11342729Sdfr printf("msgsnd: interrupted system call\n"); 11352729Sdfr#endif 113682607Sdillon error = EINTR; 113782607Sdillon goto done2; 11382729Sdfr } 11392729Sdfr 11402729Sdfr /* 11412729Sdfr * Make sure that the msq queue still exists 11422729Sdfr */ 11432729Sdfr 11442729Sdfr if (msqptr->msg_qbytes == 0 || 11452729Sdfr msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 11462729Sdfr#ifdef MSG_DEBUG_OK 11472729Sdfr printf("msqid deleted\n"); 11482729Sdfr#endif 114982607Sdillon error = EIDRM; 115082607Sdillon goto done2; 11512729Sdfr } 11522729Sdfr } 11532729Sdfr 11542729Sdfr /* 11552729Sdfr * Return the message to the user. 11562729Sdfr * 11572729Sdfr * First, do the bookkeeping (before we risk being interrupted). 11582729Sdfr */ 11592729Sdfr 11602729Sdfr msqptr->msg_cbytes -= msghdr->msg_ts; 11612729Sdfr msqptr->msg_qnum--; 116283366Sjulian msqptr->msg_lrpid = td->td_proc->p_pid; 116334961Sphk msqptr->msg_rtime = time_second; 11642729Sdfr 11652729Sdfr /* 11662729Sdfr * Make msgsz the actual amount that we'll be returning. 11672729Sdfr * Note that this effectively truncates the message if it is too long 11682729Sdfr * (since msgsz is never increased). 11692729Sdfr */ 11702729Sdfr 11712729Sdfr#ifdef MSG_DEBUG_OK 11722729Sdfr printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz, 11732729Sdfr msghdr->msg_ts); 11742729Sdfr#endif 11752729Sdfr if (msgsz > msghdr->msg_ts) 11762729Sdfr msgsz = msghdr->msg_ts; 11772729Sdfr 11782729Sdfr /* 11792729Sdfr * Return the type to the user. 11802729Sdfr */ 11812729Sdfr 118282607Sdillon error = copyout((caddr_t)&(msghdr->msg_type), user_msgp, 11832729Sdfr sizeof(msghdr->msg_type)); 118482607Sdillon if (error != 0) { 11852729Sdfr#ifdef MSG_DEBUG_OK 118682607Sdillon printf("error (%d) copying out message type\n", error); 11872729Sdfr#endif 11882729Sdfr msg_freehdr(msghdr); 11892729Sdfr wakeup((caddr_t)msqptr); 119082607Sdillon goto done2; 11912729Sdfr } 119217971Sbde user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type); 11932729Sdfr 11942729Sdfr /* 11952729Sdfr * Return the segments to the user 11962729Sdfr */ 11972729Sdfr 11982729Sdfr next = msghdr->msg_spot; 11992729Sdfr for (len = 0; len < msgsz; len += msginfo.msgssz) { 12002729Sdfr size_t tlen; 12012729Sdfr 120245921Ssada if (msgsz - len > msginfo.msgssz) 12032729Sdfr tlen = msginfo.msgssz; 12042729Sdfr else 120545921Ssada tlen = msgsz - len; 12062729Sdfr if (next <= -1) 12072729Sdfr panic("next too low #3"); 12082729Sdfr if (next >= msginfo.msgseg) 12092729Sdfr panic("next out of range #3"); 121082607Sdillon error = copyout((caddr_t)&msgpool[next * msginfo.msgssz], 12112729Sdfr user_msgp, tlen); 121282607Sdillon if (error != 0) { 12132729Sdfr#ifdef MSG_DEBUG_OK 12142729Sdfr printf("error (%d) copying out message segment\n", 121582607Sdillon error); 12162729Sdfr#endif 12172729Sdfr msg_freehdr(msghdr); 12182729Sdfr wakeup((caddr_t)msqptr); 121982607Sdillon goto done2; 12202729Sdfr } 122117971Sbde user_msgp = (char *)user_msgp + tlen; 12222729Sdfr next = msgmaps[next].next; 12232729Sdfr } 12242729Sdfr 12252729Sdfr /* 12262729Sdfr * Done, return the actual number of bytes copied out. 12272729Sdfr */ 12282729Sdfr 12292729Sdfr msg_freehdr(msghdr); 12302729Sdfr wakeup((caddr_t)msqptr); 123183366Sjulian td->td_retval[0] = msgsz; 123282607Sdillondone2: 123382607Sdillon mtx_unlock(&Giant); 123482607Sdillon return (error); 12352729Sdfr} 123677461Sdd 123777461Sddstatic int 123877461Sddsysctl_msqids(SYSCTL_HANDLER_ARGS) 123977461Sdd{ 124077461Sdd 124177461Sdd return (SYSCTL_OUT(req, msqids, 124277461Sdd sizeof(struct msqid_ds) * msginfo.msgmni)); 124377461Sdd} 124477461Sdd 124577461SddSYSCTL_DECL(_kern_ipc); 124677461SddSYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, ""); 124777461SddSYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RD, &msginfo.msgmni, 0, ""); 124877461SddSYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RD, &msginfo.msgmnb, 0, ""); 124977461SddSYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RD, &msginfo.msgtql, 0, ""); 125077461SddSYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RD, &msginfo.msgssz, 0, ""); 125177461SddSYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RD, &msginfo.msgseg, 0, "") 125277461SddSYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD, 125377461Sdd NULL, 0, sysctl_msqids, "", "Message queue IDs"); 1254