sysv_msg.c revision 122201
12729Sdfr/* 22729Sdfr * Implementation of SVID messages 32729Sdfr * 42729Sdfr * Author: Daniel Boulet 52729Sdfr * 62729Sdfr * Copyright 1993 Daniel Boulet and RTMX Inc. 72729Sdfr * 82729Sdfr * This system call was implemented by Daniel Boulet under contract from RTMX. 92729Sdfr * 102729Sdfr * Redistribution and use in source forms, with and without modification, 112729Sdfr * are permitted provided that this entire comment appears intact. 122729Sdfr * 132729Sdfr * Redistribution in binary form may occur without any restrictions. 142729Sdfr * Obviously, it would be nice if you gave credit where credit is due 152729Sdfr * but requiring it would be too onerous. 162729Sdfr * 172729Sdfr * This software is provided ``AS IS'' without any warranties of any kind. 182729Sdfr */ 192729Sdfr 20116182Sobrien#include <sys/cdefs.h> 21116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/sysv_msg.c 122201 2003-11-07 04:47:14Z rwatson $"); 22116182Sobrien 2359839Speter#include "opt_sysvipc.h" 2459839Speter 252729Sdfr#include <sys/param.h> 262729Sdfr#include <sys/systm.h> 2711626Sbde#include <sys/sysproto.h> 282729Sdfr#include <sys/kernel.h> 292729Sdfr#include <sys/proc.h> 3082607Sdillon#include <sys/lock.h> 3182607Sdillon#include <sys/mutex.h> 322729Sdfr#include <sys/msg.h> 3369449Salfred#include <sys/syscall.h> 3411626Sbde#include <sys/sysent.h> 3559839Speter#include <sys/sysctl.h> 3659839Speter#include <sys/malloc.h> 3768024Srwatson#include <sys/jail.h> 382729Sdfr 3959839Speterstatic MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues"); 4059839Speter 4192723Salfredstatic void msginit(void); 4292723Salfredstatic int msgunload(void); 4392723Salfredstatic int sysvmsg_modload(struct module *, int, void *); 4410358Sjulian 45100523Salfred#ifdef MSG_DEBUG 46100523Salfred#define DPRINTF(a) printf a 47100523Salfred#else 48100523Salfred#define DPRINTF(a) 49100523Salfred#endif 502729Sdfr 5192723Salfredstatic void msg_freehdr(struct msg *msghdr); 522729Sdfr 5311626Sbde/* XXX casting to (sy_call_t *) is bogus, as usual. */ 5412819Sphkstatic sy_call_t *msgcalls[] = { 5511626Sbde (sy_call_t *)msgctl, (sy_call_t *)msgget, 5611626Sbde (sy_call_t *)msgsnd, (sy_call_t *)msgrcv 5711626Sbde}; 582729Sdfr 5959839Speterstruct msg { 6059839Speter struct msg *msg_next; /* next msg in the chain */ 6159839Speter long msg_type; /* type of this message */ 6259839Speter /* >0 -> type of this message */ 6359839Speter /* 0 -> free header */ 6459839Speter u_short msg_ts; /* size of this message */ 6559839Speter short msg_spot; /* location of start of msg in buffer */ 6659839Speter}; 6759839Speter 6859839Speter 6959839Speter#ifndef MSGSSZ 7059839Speter#define MSGSSZ 8 /* Each segment must be 2^N long */ 7159839Speter#endif 7259839Speter#ifndef MSGSEG 7359839Speter#define MSGSEG 2048 /* must be less than 32767 */ 7459839Speter#endif 7559839Speter#define MSGMAX (MSGSSZ*MSGSEG) 7659839Speter#ifndef MSGMNB 7759839Speter#define MSGMNB 2048 /* max # of bytes in a queue */ 7859839Speter#endif 7959839Speter#ifndef MSGMNI 8059839Speter#define MSGMNI 40 8159839Speter#endif 8259839Speter#ifndef MSGTQL 8359839Speter#define MSGTQL 40 8459839Speter#endif 8559839Speter 8659839Speter/* 8759839Speter * Based on the configuration parameters described in an SVR2 (yes, two) 8859839Speter * config(1m) man page. 8959839Speter * 9059839Speter * Each message is broken up and stored in segments that are msgssz bytes 9159839Speter * long. For efficiency reasons, this should be a power of two. Also, 9259839Speter * it doesn't make sense if it is less than 8 or greater than about 256. 9359839Speter * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of 9459839Speter * two between 8 and 1024 inclusive (and panic's if it isn't). 9559839Speter */ 9659839Speterstruct msginfo msginfo = { 9759839Speter MSGMAX, /* max chars in a message */ 9859839Speter MSGMNI, /* # of message queue identifiers */ 9959839Speter MSGMNB, /* max chars in a queue */ 10059839Speter MSGTQL, /* max messages in system */ 10159839Speter MSGSSZ, /* size of a message segment */ 10259839Speter /* (must be small power of 2 greater than 4) */ 10359839Speter MSGSEG /* number of message segments */ 10459839Speter}; 10559839Speter 10659839Speter/* 10759839Speter * macros to convert between msqid_ds's and msqid's. 10859839Speter * (specific to this implementation) 10959839Speter */ 11059839Speter#define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000)) 11159839Speter#define MSQID_IX(id) ((id) & 0xffff) 11259839Speter#define MSQID_SEQ(id) (((id) >> 16) & 0xffff) 11359839Speter 11459839Speter/* 11559839Speter * The rest of this file is specific to this particular implementation. 11659839Speter */ 11759839Speter 11859839Speterstruct msgmap { 11959839Speter short next; /* next segment in buffer */ 12059839Speter /* -1 -> available */ 12159839Speter /* 0..(MSGSEG-1) -> index of next segment */ 12259839Speter}; 12359839Speter 12459839Speter#define MSG_LOCKED 01000 /* Is this msqid_ds locked? */ 12559839Speter 12612819Sphkstatic int nfree_msgmaps; /* # of free map entries */ 12712819Sphkstatic short free_msgmaps; /* head of linked list of free map entries */ 12859839Speterstatic struct msg *free_msghdrs;/* list of free msg headers */ 12959839Speterstatic char *msgpool; /* MSGMAX byte long msg buffer pool */ 13059839Speterstatic struct msgmap *msgmaps; /* MSGSEG msgmap structures */ 13159839Speterstatic struct msg *msghdrs; /* MSGTQL msg headers */ 13259839Speterstatic struct msqid_ds *msqids; /* MSGMNI msqid_ds struct's */ 133101772Salfredstatic struct mtx msq_mtx; /* global mutex for message queues. */ 1342729Sdfr 13559839Speterstatic void 13669449Salfredmsginit() 1372729Sdfr{ 1382729Sdfr register int i; 1392729Sdfr 14083765Smr TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg); 14183765Smr TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz); 14283765Smr msginfo.msgmax = msginfo.msgseg * msginfo.msgssz; 14383765Smr TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni); 14483765Smr 145111119Simp msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK); 14659839Speter if (msgpool == NULL) 14759839Speter panic("msgpool is NULL"); 148111119Simp msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK); 14959839Speter if (msgmaps == NULL) 15059839Speter panic("msgmaps is NULL"); 151111119Simp msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK); 15259839Speter if (msghdrs == NULL) 15359839Speter panic("msghdrs is NULL"); 154111119Simp msqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni, M_MSG, M_WAITOK); 15559839Speter if (msqids == NULL) 15659839Speter panic("msqids is NULL"); 15759839Speter 1582729Sdfr /* 1592729Sdfr * msginfo.msgssz should be a power of two for efficiency reasons. 1602729Sdfr * It is also pretty silly if msginfo.msgssz is less than 8 1612729Sdfr * or greater than about 256 so ... 1622729Sdfr */ 1632729Sdfr 1642729Sdfr i = 8; 1652729Sdfr while (i < 1024 && i != msginfo.msgssz) 1662729Sdfr i <<= 1; 1672729Sdfr if (i != msginfo.msgssz) { 168100523Salfred DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz, 169100523Salfred msginfo.msgssz)); 1702729Sdfr panic("msginfo.msgssz not a small power of 2"); 1712729Sdfr } 1722729Sdfr 1732729Sdfr if (msginfo.msgseg > 32767) { 174100523Salfred DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg)); 1752729Sdfr panic("msginfo.msgseg > 32767"); 1762729Sdfr } 1772729Sdfr 1782729Sdfr if (msgmaps == NULL) 1792729Sdfr panic("msgmaps is NULL"); 1802729Sdfr 1812729Sdfr for (i = 0; i < msginfo.msgseg; i++) { 1822729Sdfr if (i > 0) 1832729Sdfr msgmaps[i-1].next = i; 1842729Sdfr msgmaps[i].next = -1; /* implies entry is available */ 1852729Sdfr } 1862729Sdfr free_msgmaps = 0; 1872729Sdfr nfree_msgmaps = msginfo.msgseg; 1882729Sdfr 1892729Sdfr if (msghdrs == NULL) 1902729Sdfr panic("msghdrs is NULL"); 1912729Sdfr 1922729Sdfr for (i = 0; i < msginfo.msgtql; i++) { 1932729Sdfr msghdrs[i].msg_type = 0; 1942729Sdfr if (i > 0) 1952729Sdfr msghdrs[i-1].msg_next = &msghdrs[i]; 1962729Sdfr msghdrs[i].msg_next = NULL; 1972729Sdfr } 1982729Sdfr free_msghdrs = &msghdrs[0]; 1992729Sdfr 2002729Sdfr if (msqids == NULL) 2012729Sdfr panic("msqids is NULL"); 2022729Sdfr 2032729Sdfr for (i = 0; i < msginfo.msgmni; i++) { 2042729Sdfr msqids[i].msg_qbytes = 0; /* implies entry is available */ 2052729Sdfr msqids[i].msg_perm.seq = 0; /* reset to a known value */ 20666085Speter msqids[i].msg_perm.mode = 0; 2072729Sdfr } 208101772Salfred mtx_init(&msq_mtx, "msq", NULL, MTX_DEF); 2092729Sdfr} 2102729Sdfr 21169449Salfredstatic int 21269449Salfredmsgunload() 21369449Salfred{ 21469449Salfred struct msqid_ds *msqptr; 21569449Salfred int msqid; 21669449Salfred 21769449Salfred for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 21869449Salfred /* 21969449Salfred * Look for an unallocated and unlocked msqid_ds. 22069449Salfred * msqid_ds's can be locked by msgsnd or msgrcv while 22169449Salfred * they are copying the message in/out. We can't 22269449Salfred * re-use the entry until they release it. 22369449Salfred */ 22469449Salfred msqptr = &msqids[msqid]; 22569449Salfred if (msqptr->msg_qbytes != 0 || 22669449Salfred (msqptr->msg_perm.mode & MSG_LOCKED) != 0) 22769449Salfred break; 22869449Salfred } 22969449Salfred if (msqid != msginfo.msgmni) 23069449Salfred return (EBUSY); 23169449Salfred 23269449Salfred free(msgpool, M_MSG); 23369449Salfred free(msgmaps, M_MSG); 23469449Salfred free(msghdrs, M_MSG); 23569449Salfred free(msqids, M_MSG); 236101772Salfred mtx_destroy(&msq_mtx); 23769449Salfred return (0); 23869449Salfred} 23969449Salfred 24069449Salfred 24169449Salfredstatic int 24269449Salfredsysvmsg_modload(struct module *module, int cmd, void *arg) 24369449Salfred{ 24469449Salfred int error = 0; 24569449Salfred 24669449Salfred switch (cmd) { 24769449Salfred case MOD_LOAD: 24869449Salfred msginit(); 24969449Salfred break; 25069449Salfred case MOD_UNLOAD: 25169449Salfred error = msgunload(); 25269449Salfred break; 25369449Salfred case MOD_SHUTDOWN: 25469449Salfred break; 25569449Salfred default: 25669449Salfred error = EINVAL; 25769449Salfred break; 25869449Salfred } 25969449Salfred return (error); 26069449Salfred} 26169449Salfred 26271038Sdesstatic moduledata_t sysvmsg_mod = { 26371038Sdes "sysvmsg", 26469449Salfred &sysvmsg_modload, 26569449Salfred NULL 26669449Salfred}; 26769449Salfred 26888633SalfredSYSCALL_MODULE_HELPER(msgsys); 26988633SalfredSYSCALL_MODULE_HELPER(msgctl); 27088633SalfredSYSCALL_MODULE_HELPER(msgget); 27188633SalfredSYSCALL_MODULE_HELPER(msgsnd); 27288633SalfredSYSCALL_MODULE_HELPER(msgrcv); 27369449Salfred 27471038SdesDECLARE_MODULE(sysvmsg, sysvmsg_mod, 27569449Salfred SI_SUB_SYSV_MSG, SI_ORDER_FIRST); 27671038SdesMODULE_VERSION(sysvmsg, 1); 27769449Salfred 2782729Sdfr/* 2792729Sdfr * Entry point for all MSG calls 28082607Sdillon * 28182607Sdillon * MPSAFE 2822729Sdfr */ 2832729Sdfrint 28483366Sjulianmsgsys(td, uap) 28583366Sjulian struct thread *td; 28611626Sbde /* XXX actually varargs. */ 28711626Sbde struct msgsys_args /* { 288118615Snectar int which; 28911626Sbde int a2; 29011626Sbde int a3; 29111626Sbde int a4; 29211626Sbde int a5; 29311626Sbde int a6; 29411626Sbde } */ *uap; 2952729Sdfr{ 29682607Sdillon int error; 2972729Sdfr 29891703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 29991703Sjhb return (ENOSYS); 300118615Snectar if (uap->which < 0 || 301118615Snectar uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0])) 30291703Sjhb return (EINVAL); 30383366Sjulian error = (*msgcalls[uap->which])(td, &uap->a2); 30482607Sdillon return (error); 3052729Sdfr} 3062729Sdfr 3072729Sdfrstatic void 3082729Sdfrmsg_freehdr(msghdr) 3092729Sdfr struct msg *msghdr; 3102729Sdfr{ 3112729Sdfr while (msghdr->msg_ts > 0) { 3122729Sdfr short next; 3132729Sdfr if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg) 3142729Sdfr panic("msghdr->msg_spot out of range"); 3152729Sdfr next = msgmaps[msghdr->msg_spot].next; 3162729Sdfr msgmaps[msghdr->msg_spot].next = free_msgmaps; 3172729Sdfr free_msgmaps = msghdr->msg_spot; 3182729Sdfr nfree_msgmaps++; 3192729Sdfr msghdr->msg_spot = next; 3202729Sdfr if (msghdr->msg_ts >= msginfo.msgssz) 3212729Sdfr msghdr->msg_ts -= msginfo.msgssz; 3222729Sdfr else 3232729Sdfr msghdr->msg_ts = 0; 3242729Sdfr } 3252729Sdfr if (msghdr->msg_spot != -1) 3262729Sdfr panic("msghdr->msg_spot != -1"); 3272729Sdfr msghdr->msg_next = free_msghdrs; 3282729Sdfr free_msghdrs = msghdr; 3292729Sdfr} 3302729Sdfr 33112866Speter#ifndef _SYS_SYSPROTO_H_ 3322729Sdfrstruct msgctl_args { 3332729Sdfr int msqid; 3342729Sdfr int cmd; 33512866Speter struct msqid_ds *buf; 3362729Sdfr}; 33712866Speter#endif 3382729Sdfr 33982607Sdillon/* 34082607Sdillon * MPSAFE 34182607Sdillon */ 34212866Speterint 34383366Sjulianmsgctl(td, uap) 34483366Sjulian struct thread *td; 3452729Sdfr register struct msgctl_args *uap; 3462729Sdfr{ 3472729Sdfr int msqid = uap->msqid; 3482729Sdfr int cmd = uap->cmd; 34912866Speter struct msqid_ds *user_msqptr = uap->buf; 35082607Sdillon int rval, error; 3512729Sdfr struct msqid_ds msqbuf; 3522729Sdfr register struct msqid_ds *msqptr; 3532729Sdfr 354100523Salfred DPRINTF(("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr)); 35591703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 35691703Sjhb return (ENOSYS); 35791703Sjhb 3582729Sdfr msqid = IPCID_TO_IX(msqid); 3592729Sdfr 3602729Sdfr if (msqid < 0 || msqid >= msginfo.msgmni) { 361100523Salfred DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 362100523Salfred msginfo.msgmni)); 363101772Salfred return (EINVAL); 3642729Sdfr } 365101772Salfred if (cmd == IPC_SET && 366101772Salfred (error = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0) 367101772Salfred return (error); 3682729Sdfr 3692729Sdfr msqptr = &msqids[msqid]; 3702729Sdfr 371101772Salfred mtx_lock(&msq_mtx); 3722729Sdfr if (msqptr->msg_qbytes == 0) { 373100523Salfred DPRINTF(("no such msqid\n")); 37482607Sdillon error = EINVAL; 37582607Sdillon goto done2; 3762729Sdfr } 3772729Sdfr if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 378100523Salfred DPRINTF(("wrong sequence number\n")); 37982607Sdillon error = EINVAL; 38082607Sdillon goto done2; 3812729Sdfr } 3822729Sdfr 38382607Sdillon error = 0; 3842729Sdfr rval = 0; 3852729Sdfr 3862729Sdfr switch (cmd) { 3872729Sdfr 3882729Sdfr case IPC_RMID: 3892729Sdfr { 3902729Sdfr struct msg *msghdr; 39183366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M))) 39282607Sdillon goto done2; 3932729Sdfr /* Free the message headers */ 3942729Sdfr msghdr = msqptr->msg_first; 3952729Sdfr while (msghdr != NULL) { 3962729Sdfr struct msg *msghdr_tmp; 3972729Sdfr 3982729Sdfr /* Free the segments of each message */ 3992729Sdfr msqptr->msg_cbytes -= msghdr->msg_ts; 4002729Sdfr msqptr->msg_qnum--; 4012729Sdfr msghdr_tmp = msghdr; 4022729Sdfr msghdr = msghdr->msg_next; 4032729Sdfr msg_freehdr(msghdr_tmp); 4042729Sdfr } 4052729Sdfr 4062729Sdfr if (msqptr->msg_cbytes != 0) 4072729Sdfr panic("msg_cbytes is screwed up"); 4082729Sdfr if (msqptr->msg_qnum != 0) 4092729Sdfr panic("msg_qnum is screwed up"); 4102729Sdfr 4112729Sdfr msqptr->msg_qbytes = 0; /* Mark it as free */ 4122729Sdfr 413100511Salfred wakeup(msqptr); 4142729Sdfr } 4152729Sdfr 4162729Sdfr break; 4172729Sdfr 4182729Sdfr case IPC_SET: 41983366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M))) 42082607Sdillon goto done2; 42143426Sphk if (msqbuf.msg_qbytes > msqptr->msg_qbytes) { 42293593Sjhb error = suser(td); 42382607Sdillon if (error) 42482607Sdillon goto done2; 42543426Sphk } 4262729Sdfr if (msqbuf.msg_qbytes > msginfo.msgmnb) { 427100523Salfred DPRINTF(("can't increase msg_qbytes beyond %d" 428100523Salfred "(truncating)\n", msginfo.msgmnb)); 4292729Sdfr msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */ 4302729Sdfr } 4312729Sdfr if (msqbuf.msg_qbytes == 0) { 432100523Salfred DPRINTF(("can't reduce msg_qbytes to 0\n")); 43382607Sdillon error = EINVAL; /* non-standard errno! */ 43482607Sdillon goto done2; 4352729Sdfr } 4362729Sdfr msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */ 4372729Sdfr msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */ 4382729Sdfr msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) | 4392729Sdfr (msqbuf.msg_perm.mode & 0777); 4402729Sdfr msqptr->msg_qbytes = msqbuf.msg_qbytes; 44134961Sphk msqptr->msg_ctime = time_second; 4422729Sdfr break; 4432729Sdfr 4442729Sdfr case IPC_STAT: 44583366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) { 446100523Salfred DPRINTF(("requester doesn't have read access\n")); 44782607Sdillon goto done2; 4482729Sdfr } 4492729Sdfr break; 4502729Sdfr 4512729Sdfr default: 452100523Salfred DPRINTF(("invalid command %d\n", cmd)); 45382607Sdillon error = EINVAL; 45482607Sdillon goto done2; 4552729Sdfr } 4562729Sdfr 45782607Sdillon if (error == 0) 45883366Sjulian td->td_retval[0] = rval; 45982607Sdillondone2: 460101772Salfred mtx_unlock(&msq_mtx); 461101772Salfred if (cmd == IPC_STAT && error == 0) 462101772Salfred error = copyout(msqptr, user_msqptr, sizeof(struct msqid_ds)); 46382607Sdillon return(error); 4642729Sdfr} 4652729Sdfr 46612866Speter#ifndef _SYS_SYSPROTO_H_ 4672729Sdfrstruct msgget_args { 4682729Sdfr key_t key; 4692729Sdfr int msgflg; 4702729Sdfr}; 47112866Speter#endif 4722729Sdfr 47382607Sdillon/* 47482607Sdillon * MPSAFE 47582607Sdillon */ 47612866Speterint 47783366Sjulianmsgget(td, uap) 47883366Sjulian struct thread *td; 4792729Sdfr register struct msgget_args *uap; 4802729Sdfr{ 48182607Sdillon int msqid, error = 0; 4822729Sdfr int key = uap->key; 4832729Sdfr int msgflg = uap->msgflg; 48491703Sjhb struct ucred *cred = td->td_ucred; 4852836Sdg register struct msqid_ds *msqptr = NULL; 4862729Sdfr 487100523Salfred DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg)); 4882729Sdfr 48991703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 49091703Sjhb return (ENOSYS); 49191703Sjhb 492101772Salfred mtx_lock(&msq_mtx); 4932729Sdfr if (key != IPC_PRIVATE) { 4942729Sdfr for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 4952729Sdfr msqptr = &msqids[msqid]; 4962729Sdfr if (msqptr->msg_qbytes != 0 && 4972729Sdfr msqptr->msg_perm.key == key) 4982729Sdfr break; 4992729Sdfr } 5002729Sdfr if (msqid < msginfo.msgmni) { 501100523Salfred DPRINTF(("found public key\n")); 5022729Sdfr if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) { 503100523Salfred DPRINTF(("not exclusive\n")); 50482607Sdillon error = EEXIST; 50582607Sdillon goto done2; 5062729Sdfr } 507122201Srwatson if ((error = ipcperm(td, &msqptr->msg_perm, msgflg & 0700))) { 508100523Salfred DPRINTF(("requester doesn't have 0%o access\n", 509100523Salfred msgflg & 0700)); 51082607Sdillon goto done2; 5112729Sdfr } 5122729Sdfr goto found; 5132729Sdfr } 5142729Sdfr } 5152729Sdfr 516100523Salfred DPRINTF(("need to allocate the msqid_ds\n")); 5172729Sdfr if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) { 5182729Sdfr for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 5192729Sdfr /* 5202729Sdfr * Look for an unallocated and unlocked msqid_ds. 5212729Sdfr * msqid_ds's can be locked by msgsnd or msgrcv while 5222729Sdfr * they are copying the message in/out. We can't 5232729Sdfr * re-use the entry until they release it. 5242729Sdfr */ 5252729Sdfr msqptr = &msqids[msqid]; 5262729Sdfr if (msqptr->msg_qbytes == 0 && 5272729Sdfr (msqptr->msg_perm.mode & MSG_LOCKED) == 0) 5282729Sdfr break; 5292729Sdfr } 5302729Sdfr if (msqid == msginfo.msgmni) { 531100523Salfred DPRINTF(("no more msqid_ds's available\n")); 53282607Sdillon error = ENOSPC; 53382607Sdillon goto done2; 5342729Sdfr } 535100523Salfred DPRINTF(("msqid %d is available\n", msqid)); 5362729Sdfr msqptr->msg_perm.key = key; 5372729Sdfr msqptr->msg_perm.cuid = cred->cr_uid; 5382729Sdfr msqptr->msg_perm.uid = cred->cr_uid; 5392729Sdfr msqptr->msg_perm.cgid = cred->cr_gid; 5402729Sdfr msqptr->msg_perm.gid = cred->cr_gid; 5412729Sdfr msqptr->msg_perm.mode = (msgflg & 0777); 5422729Sdfr /* Make sure that the returned msqid is unique */ 543107896Smaxim msqptr->msg_perm.seq = (msqptr->msg_perm.seq + 1) & 0x7fff; 5442729Sdfr msqptr->msg_first = NULL; 5452729Sdfr msqptr->msg_last = NULL; 5462729Sdfr msqptr->msg_cbytes = 0; 5472729Sdfr msqptr->msg_qnum = 0; 5482729Sdfr msqptr->msg_qbytes = msginfo.msgmnb; 5492729Sdfr msqptr->msg_lspid = 0; 5502729Sdfr msqptr->msg_lrpid = 0; 5512729Sdfr msqptr->msg_stime = 0; 5522729Sdfr msqptr->msg_rtime = 0; 55334961Sphk msqptr->msg_ctime = time_second; 5542729Sdfr } else { 555100523Salfred DPRINTF(("didn't find it and wasn't asked to create it\n")); 55682607Sdillon error = ENOENT; 55782607Sdillon goto done2; 5582729Sdfr } 5592729Sdfr 5602729Sdfrfound: 5612729Sdfr /* Construct the unique msqid */ 56283366Sjulian td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm); 56382607Sdillondone2: 564101772Salfred mtx_unlock(&msq_mtx); 56582607Sdillon return (error); 5662729Sdfr} 5672729Sdfr 56812866Speter#ifndef _SYS_SYSPROTO_H_ 5692729Sdfrstruct msgsnd_args { 5702729Sdfr int msqid; 571109895Salfred const void *msgp; 5722729Sdfr size_t msgsz; 5732729Sdfr int msgflg; 5742729Sdfr}; 57512866Speter#endif 5762729Sdfr 57782607Sdillon/* 57882607Sdillon * MPSAFE 57982607Sdillon */ 58012866Speterint 58183366Sjulianmsgsnd(td, uap) 58283366Sjulian struct thread *td; 5832729Sdfr register struct msgsnd_args *uap; 5842729Sdfr{ 5852729Sdfr int msqid = uap->msqid; 586109895Salfred const void *user_msgp = uap->msgp; 5872729Sdfr size_t msgsz = uap->msgsz; 5882729Sdfr int msgflg = uap->msgflg; 58982607Sdillon int segs_needed, error = 0; 5902729Sdfr register struct msqid_ds *msqptr; 5912729Sdfr register struct msg *msghdr; 5922729Sdfr short next; 5932729Sdfr 594100523Salfred DPRINTF(("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz, 595100523Salfred msgflg)); 59691703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 59791703Sjhb return (ENOSYS); 59891703Sjhb 599101772Salfred mtx_lock(&msq_mtx); 6002729Sdfr msqid = IPCID_TO_IX(msqid); 6012729Sdfr 6022729Sdfr if (msqid < 0 || msqid >= msginfo.msgmni) { 603100523Salfred DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 604100523Salfred msginfo.msgmni)); 60582607Sdillon error = EINVAL; 60682607Sdillon goto done2; 6072729Sdfr } 6082729Sdfr 6092729Sdfr msqptr = &msqids[msqid]; 6102729Sdfr if (msqptr->msg_qbytes == 0) { 611100523Salfred DPRINTF(("no such message queue id\n")); 61282607Sdillon error = EINVAL; 61382607Sdillon goto done2; 6142729Sdfr } 6152729Sdfr if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 616100523Salfred DPRINTF(("wrong sequence number\n")); 61782607Sdillon error = EINVAL; 61882607Sdillon goto done2; 6192729Sdfr } 6202729Sdfr 62183366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_W))) { 622100523Salfred DPRINTF(("requester doesn't have write access\n")); 62382607Sdillon goto done2; 6242729Sdfr } 6252729Sdfr 6262729Sdfr segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz; 627100523Salfred DPRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz, 628100523Salfred segs_needed)); 6292729Sdfr for (;;) { 6302729Sdfr int need_more_resources = 0; 6312729Sdfr 6322729Sdfr /* 6332729Sdfr * check msgsz 6342729Sdfr * (inside this loop in case msg_qbytes changes while we sleep) 6352729Sdfr */ 6362729Sdfr 6372836Sdg if (msgsz > msqptr->msg_qbytes) { 638100523Salfred DPRINTF(("msgsz > msqptr->msg_qbytes\n")); 63982607Sdillon error = EINVAL; 64082607Sdillon goto done2; 6412729Sdfr } 6422729Sdfr 6432729Sdfr if (msqptr->msg_perm.mode & MSG_LOCKED) { 644100523Salfred DPRINTF(("msqid is locked\n")); 6452729Sdfr need_more_resources = 1; 6462729Sdfr } 6472729Sdfr if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) { 648100523Salfred DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n")); 6492729Sdfr need_more_resources = 1; 6502729Sdfr } 6512729Sdfr if (segs_needed > nfree_msgmaps) { 652100523Salfred DPRINTF(("segs_needed > nfree_msgmaps\n")); 6532729Sdfr need_more_resources = 1; 6542729Sdfr } 6552729Sdfr if (free_msghdrs == NULL) { 656100523Salfred DPRINTF(("no more msghdrs\n")); 6572729Sdfr need_more_resources = 1; 6582729Sdfr } 6592729Sdfr 6602729Sdfr if (need_more_resources) { 6612729Sdfr int we_own_it; 6622729Sdfr 6632729Sdfr if ((msgflg & IPC_NOWAIT) != 0) { 664100523Salfred DPRINTF(("need more resources but caller " 665100523Salfred "doesn't want to wait\n")); 66682607Sdillon error = EAGAIN; 66782607Sdillon goto done2; 6682729Sdfr } 6692729Sdfr 6702729Sdfr if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) { 671100523Salfred DPRINTF(("we don't own the msqid_ds\n")); 6722729Sdfr we_own_it = 0; 6732729Sdfr } else { 6742729Sdfr /* Force later arrivals to wait for our 6752729Sdfr request */ 676100523Salfred DPRINTF(("we own the msqid_ds\n")); 6772729Sdfr msqptr->msg_perm.mode |= MSG_LOCKED; 6782729Sdfr we_own_it = 1; 6792729Sdfr } 680100523Salfred DPRINTF(("goodnight\n")); 681101772Salfred error = msleep(msqptr, &msq_mtx, (PZERO - 4) | PCATCH, 6822729Sdfr "msgwait", 0); 683100523Salfred DPRINTF(("good morning, error=%d\n", error)); 6842729Sdfr if (we_own_it) 6852729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 68682607Sdillon if (error != 0) { 687100523Salfred DPRINTF(("msgsnd: interrupted system call\n")); 68882607Sdillon error = EINTR; 68982607Sdillon goto done2; 6902729Sdfr } 6912729Sdfr 6922729Sdfr /* 6932729Sdfr * Make sure that the msq queue still exists 6942729Sdfr */ 6952729Sdfr 6962729Sdfr if (msqptr->msg_qbytes == 0) { 697100523Salfred DPRINTF(("msqid deleted\n")); 69882607Sdillon error = EIDRM; 69982607Sdillon goto done2; 7002729Sdfr } 7012729Sdfr 7022729Sdfr } else { 703100523Salfred DPRINTF(("got all the resources that we need\n")); 7042729Sdfr break; 7052729Sdfr } 7062729Sdfr } 7072729Sdfr 7082729Sdfr /* 7092729Sdfr * We have the resources that we need. 7102729Sdfr * Make sure! 7112729Sdfr */ 7122729Sdfr 7132729Sdfr if (msqptr->msg_perm.mode & MSG_LOCKED) 7142729Sdfr panic("msg_perm.mode & MSG_LOCKED"); 7152729Sdfr if (segs_needed > nfree_msgmaps) 7162729Sdfr panic("segs_needed > nfree_msgmaps"); 7172729Sdfr if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) 7182729Sdfr panic("msgsz + msg_cbytes > msg_qbytes"); 7192729Sdfr if (free_msghdrs == NULL) 7202729Sdfr panic("no more msghdrs"); 7212729Sdfr 7222729Sdfr /* 7232729Sdfr * Re-lock the msqid_ds in case we page-fault when copying in the 7242729Sdfr * message 7252729Sdfr */ 7262729Sdfr 7272729Sdfr if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) 7282729Sdfr panic("msqid_ds is already locked"); 7292729Sdfr msqptr->msg_perm.mode |= MSG_LOCKED; 7302729Sdfr 7312729Sdfr /* 7322729Sdfr * Allocate a message header 7332729Sdfr */ 7342729Sdfr 7352729Sdfr msghdr = free_msghdrs; 7362729Sdfr free_msghdrs = msghdr->msg_next; 7372729Sdfr msghdr->msg_spot = -1; 7382729Sdfr msghdr->msg_ts = msgsz; 7392729Sdfr 7402729Sdfr /* 7412729Sdfr * Allocate space for the message 7422729Sdfr */ 7432729Sdfr 7442729Sdfr while (segs_needed > 0) { 7452729Sdfr if (nfree_msgmaps <= 0) 7462729Sdfr panic("not enough msgmaps"); 7472729Sdfr if (free_msgmaps == -1) 7482729Sdfr panic("nil free_msgmaps"); 7492729Sdfr next = free_msgmaps; 7502729Sdfr if (next <= -1) 7512729Sdfr panic("next too low #1"); 7522729Sdfr if (next >= msginfo.msgseg) 7532729Sdfr panic("next out of range #1"); 754100523Salfred DPRINTF(("allocating segment %d to message\n", next)); 7552729Sdfr free_msgmaps = msgmaps[next].next; 7562729Sdfr nfree_msgmaps--; 7572729Sdfr msgmaps[next].next = msghdr->msg_spot; 7582729Sdfr msghdr->msg_spot = next; 7592729Sdfr segs_needed--; 7602729Sdfr } 7612729Sdfr 7622729Sdfr /* 7632729Sdfr * Copy in the message type 7642729Sdfr */ 7652729Sdfr 766101772Salfred mtx_unlock(&msq_mtx); 76782607Sdillon if ((error = copyin(user_msgp, &msghdr->msg_type, 7682729Sdfr sizeof(msghdr->msg_type))) != 0) { 769101772Salfred mtx_lock(&msq_mtx); 770100523Salfred DPRINTF(("error %d copying the message type\n", error)); 7712729Sdfr msg_freehdr(msghdr); 7722729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 773100511Salfred wakeup(msqptr); 77482607Sdillon goto done2; 7752729Sdfr } 776101772Salfred mtx_lock(&msq_mtx); 777109906Salfred user_msgp = (const char *)user_msgp + sizeof(msghdr->msg_type); 7782729Sdfr 7792729Sdfr /* 7802729Sdfr * Validate the message type 7812729Sdfr */ 7822729Sdfr 7832729Sdfr if (msghdr->msg_type < 1) { 7842729Sdfr msg_freehdr(msghdr); 7852729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 786100511Salfred wakeup(msqptr); 787100523Salfred DPRINTF(("mtype (%d) < 1\n", msghdr->msg_type)); 78882607Sdillon error = EINVAL; 78982607Sdillon goto done2; 7902729Sdfr } 7912729Sdfr 7922729Sdfr /* 7932729Sdfr * Copy in the message body 7942729Sdfr */ 7952729Sdfr 7962729Sdfr next = msghdr->msg_spot; 7972729Sdfr while (msgsz > 0) { 7982729Sdfr size_t tlen; 7992729Sdfr if (msgsz > msginfo.msgssz) 8002729Sdfr tlen = msginfo.msgssz; 8012729Sdfr else 8022729Sdfr tlen = msgsz; 8032729Sdfr if (next <= -1) 8042729Sdfr panic("next too low #2"); 8052729Sdfr if (next >= msginfo.msgseg) 8062729Sdfr panic("next out of range #2"); 807101772Salfred mtx_unlock(&msq_mtx); 80882607Sdillon if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz], 8092729Sdfr tlen)) != 0) { 810101772Salfred mtx_lock(&msq_mtx); 811100523Salfred DPRINTF(("error %d copying in message segment\n", 812100523Salfred error)); 8132729Sdfr msg_freehdr(msghdr); 8142729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 815100511Salfred wakeup(msqptr); 81682607Sdillon goto done2; 8172729Sdfr } 818101772Salfred mtx_lock(&msq_mtx); 8192729Sdfr msgsz -= tlen; 820109906Salfred user_msgp = (const char *)user_msgp + tlen; 8212729Sdfr next = msgmaps[next].next; 8222729Sdfr } 8232729Sdfr if (next != -1) 8242729Sdfr panic("didn't use all the msg segments"); 8252729Sdfr 8262729Sdfr /* 8272729Sdfr * We've got the message. Unlock the msqid_ds. 8282729Sdfr */ 8292729Sdfr 8302729Sdfr msqptr->msg_perm.mode &= ~MSG_LOCKED; 8312729Sdfr 8322729Sdfr /* 8332729Sdfr * Make sure that the msqid_ds is still allocated. 8342729Sdfr */ 8352729Sdfr 8362729Sdfr if (msqptr->msg_qbytes == 0) { 8372729Sdfr msg_freehdr(msghdr); 838100511Salfred wakeup(msqptr); 83982607Sdillon error = EIDRM; 84082607Sdillon goto done2; 8412729Sdfr } 8422729Sdfr 8432729Sdfr /* 8442729Sdfr * Put the message into the queue 8452729Sdfr */ 8462729Sdfr 8472729Sdfr if (msqptr->msg_first == NULL) { 8482729Sdfr msqptr->msg_first = msghdr; 8492729Sdfr msqptr->msg_last = msghdr; 8502729Sdfr } else { 8512729Sdfr msqptr->msg_last->msg_next = msghdr; 8522729Sdfr msqptr->msg_last = msghdr; 8532729Sdfr } 8542729Sdfr msqptr->msg_last->msg_next = NULL; 8552729Sdfr 8562729Sdfr msqptr->msg_cbytes += msghdr->msg_ts; 8572729Sdfr msqptr->msg_qnum++; 85883366Sjulian msqptr->msg_lspid = td->td_proc->p_pid; 85934961Sphk msqptr->msg_stime = time_second; 8602729Sdfr 861100511Salfred wakeup(msqptr); 86283366Sjulian td->td_retval[0] = 0; 86382607Sdillondone2: 864101772Salfred mtx_unlock(&msq_mtx); 86582607Sdillon return (error); 8662729Sdfr} 8672729Sdfr 86812866Speter#ifndef _SYS_SYSPROTO_H_ 8692729Sdfrstruct msgrcv_args { 8702729Sdfr int msqid; 8712729Sdfr void *msgp; 8722729Sdfr size_t msgsz; 8732729Sdfr long msgtyp; 8742729Sdfr int msgflg; 8752729Sdfr}; 87612866Speter#endif 8772729Sdfr 87882607Sdillon/* 87982607Sdillon * MPSAFE 88082607Sdillon */ 88112866Speterint 88283366Sjulianmsgrcv(td, uap) 88383366Sjulian struct thread *td; 8842729Sdfr register struct msgrcv_args *uap; 8852729Sdfr{ 8862729Sdfr int msqid = uap->msqid; 8872729Sdfr void *user_msgp = uap->msgp; 8882729Sdfr size_t msgsz = uap->msgsz; 8892729Sdfr long msgtyp = uap->msgtyp; 8902729Sdfr int msgflg = uap->msgflg; 8912729Sdfr size_t len; 8922729Sdfr register struct msqid_ds *msqptr; 8932729Sdfr register struct msg *msghdr; 89482607Sdillon int error = 0; 8952729Sdfr short next; 8962729Sdfr 897100523Salfred DPRINTF(("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp, 898100523Salfred msgsz, msgtyp, msgflg)); 8992729Sdfr 90091703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 90191703Sjhb return (ENOSYS); 90291703Sjhb 9032729Sdfr msqid = IPCID_TO_IX(msqid); 9042729Sdfr 9052729Sdfr if (msqid < 0 || msqid >= msginfo.msgmni) { 906100523Salfred DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 907100523Salfred msginfo.msgmni)); 908101772Salfred return (EINVAL); 9092729Sdfr } 9102729Sdfr 9112729Sdfr msqptr = &msqids[msqid]; 912101772Salfred mtx_lock(&msq_mtx); 9132729Sdfr if (msqptr->msg_qbytes == 0) { 914100523Salfred DPRINTF(("no such message queue id\n")); 91582607Sdillon error = EINVAL; 91682607Sdillon goto done2; 9172729Sdfr } 9182729Sdfr if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 919100523Salfred DPRINTF(("wrong sequence number\n")); 92082607Sdillon error = EINVAL; 92182607Sdillon goto done2; 9222729Sdfr } 9232729Sdfr 92483366Sjulian if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) { 925100523Salfred DPRINTF(("requester doesn't have read access\n")); 92682607Sdillon goto done2; 9272729Sdfr } 9282729Sdfr 9292729Sdfr msghdr = NULL; 9302729Sdfr while (msghdr == NULL) { 9312729Sdfr if (msgtyp == 0) { 9322729Sdfr msghdr = msqptr->msg_first; 9332729Sdfr if (msghdr != NULL) { 9342729Sdfr if (msgsz < msghdr->msg_ts && 9352729Sdfr (msgflg & MSG_NOERROR) == 0) { 936100523Salfred DPRINTF(("first message on the queue " 937100523Salfred "is too big (want %d, got %d)\n", 938100523Salfred msgsz, msghdr->msg_ts)); 93982607Sdillon error = E2BIG; 94082607Sdillon goto done2; 9412729Sdfr } 9422729Sdfr if (msqptr->msg_first == msqptr->msg_last) { 9432729Sdfr msqptr->msg_first = NULL; 9442729Sdfr msqptr->msg_last = NULL; 9452729Sdfr } else { 9462729Sdfr msqptr->msg_first = msghdr->msg_next; 9472729Sdfr if (msqptr->msg_first == NULL) 9482729Sdfr panic("msg_first/last screwed up #1"); 9492729Sdfr } 9502729Sdfr } 9512729Sdfr } else { 9522729Sdfr struct msg *previous; 9532729Sdfr struct msg **prev; 9542729Sdfr 9552729Sdfr previous = NULL; 9562729Sdfr prev = &(msqptr->msg_first); 9572729Sdfr while ((msghdr = *prev) != NULL) { 9582729Sdfr /* 9592729Sdfr * Is this message's type an exact match or is 9602729Sdfr * this message's type less than or equal to 9612729Sdfr * the absolute value of a negative msgtyp? 9622729Sdfr * Note that the second half of this test can 9632729Sdfr * NEVER be true if msgtyp is positive since 9642729Sdfr * msg_type is always positive! 9652729Sdfr */ 9662729Sdfr 9672729Sdfr if (msgtyp == msghdr->msg_type || 9682729Sdfr msghdr->msg_type <= -msgtyp) { 969100523Salfred DPRINTF(("found message type %d, " 970100523Salfred "requested %d\n", 971100523Salfred msghdr->msg_type, msgtyp)); 9722729Sdfr if (msgsz < msghdr->msg_ts && 9732729Sdfr (msgflg & MSG_NOERROR) == 0) { 974100523Salfred DPRINTF(("requested message " 975100523Salfred "on the queue is too big " 976100523Salfred "(want %d, got %d)\n", 977100523Salfred msgsz, msghdr->msg_ts)); 97882607Sdillon error = E2BIG; 97982607Sdillon goto done2; 9802729Sdfr } 9812729Sdfr *prev = msghdr->msg_next; 9822729Sdfr if (msghdr == msqptr->msg_last) { 9832729Sdfr if (previous == NULL) { 9842729Sdfr if (prev != 9852729Sdfr &msqptr->msg_first) 9862729Sdfr panic("msg_first/last screwed up #2"); 9872729Sdfr msqptr->msg_first = 9882729Sdfr NULL; 9892729Sdfr msqptr->msg_last = 9902729Sdfr NULL; 9912729Sdfr } else { 9922729Sdfr if (prev == 9932729Sdfr &msqptr->msg_first) 9942729Sdfr panic("msg_first/last screwed up #3"); 9952729Sdfr msqptr->msg_last = 9962729Sdfr previous; 9972729Sdfr } 9982729Sdfr } 9992729Sdfr break; 10002729Sdfr } 10012729Sdfr previous = msghdr; 10022729Sdfr prev = &(msghdr->msg_next); 10032729Sdfr } 10042729Sdfr } 10052729Sdfr 10062729Sdfr /* 10072729Sdfr * We've either extracted the msghdr for the appropriate 10082729Sdfr * message or there isn't one. 10092729Sdfr * If there is one then bail out of this loop. 10102729Sdfr */ 10112729Sdfr 10122729Sdfr if (msghdr != NULL) 10132729Sdfr break; 10142729Sdfr 10152729Sdfr /* 10162729Sdfr * Hmph! No message found. Does the user want to wait? 10172729Sdfr */ 10182729Sdfr 10192729Sdfr if ((msgflg & IPC_NOWAIT) != 0) { 1020100523Salfred DPRINTF(("no appropriate message found (msgtyp=%d)\n", 1021100523Salfred msgtyp)); 10222729Sdfr /* The SVID says to return ENOMSG. */ 102382607Sdillon error = ENOMSG; 102482607Sdillon goto done2; 10252729Sdfr } 10262729Sdfr 10272729Sdfr /* 10282729Sdfr * Wait for something to happen 10292729Sdfr */ 10302729Sdfr 1031100523Salfred DPRINTF(("msgrcv: goodnight\n")); 1032101772Salfred error = msleep(msqptr, &msq_mtx, (PZERO - 4) | PCATCH, 1033101772Salfred "msgwait", 0); 1034100523Salfred DPRINTF(("msgrcv: good morning (error=%d)\n", error)); 10352729Sdfr 103682607Sdillon if (error != 0) { 1037100523Salfred DPRINTF(("msgsnd: interrupted system call\n")); 103882607Sdillon error = EINTR; 103982607Sdillon goto done2; 10402729Sdfr } 10412729Sdfr 10422729Sdfr /* 10432729Sdfr * Make sure that the msq queue still exists 10442729Sdfr */ 10452729Sdfr 10462729Sdfr if (msqptr->msg_qbytes == 0 || 10472729Sdfr msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 1048100523Salfred DPRINTF(("msqid deleted\n")); 104982607Sdillon error = EIDRM; 105082607Sdillon goto done2; 10512729Sdfr } 10522729Sdfr } 10532729Sdfr 10542729Sdfr /* 10552729Sdfr * Return the message to the user. 10562729Sdfr * 10572729Sdfr * First, do the bookkeeping (before we risk being interrupted). 10582729Sdfr */ 10592729Sdfr 10602729Sdfr msqptr->msg_cbytes -= msghdr->msg_ts; 10612729Sdfr msqptr->msg_qnum--; 106283366Sjulian msqptr->msg_lrpid = td->td_proc->p_pid; 106334961Sphk msqptr->msg_rtime = time_second; 10642729Sdfr 10652729Sdfr /* 10662729Sdfr * Make msgsz the actual amount that we'll be returning. 10672729Sdfr * Note that this effectively truncates the message if it is too long 10682729Sdfr * (since msgsz is never increased). 10692729Sdfr */ 10702729Sdfr 1071100523Salfred DPRINTF(("found a message, msgsz=%d, msg_ts=%d\n", msgsz, 1072100523Salfred msghdr->msg_ts)); 10732729Sdfr if (msgsz > msghdr->msg_ts) 10742729Sdfr msgsz = msghdr->msg_ts; 10752729Sdfr 10762729Sdfr /* 10772729Sdfr * Return the type to the user. 10782729Sdfr */ 10792729Sdfr 1080101772Salfred mtx_unlock(&msq_mtx); 1081100511Salfred error = copyout(&(msghdr->msg_type), user_msgp, 10822729Sdfr sizeof(msghdr->msg_type)); 1083101772Salfred mtx_lock(&msq_mtx); 108482607Sdillon if (error != 0) { 1085100523Salfred DPRINTF(("error (%d) copying out message type\n", error)); 10862729Sdfr msg_freehdr(msghdr); 1087100511Salfred wakeup(msqptr); 108882607Sdillon goto done2; 10892729Sdfr } 109017971Sbde user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type); 10912729Sdfr 10922729Sdfr /* 10932729Sdfr * Return the segments to the user 10942729Sdfr */ 10952729Sdfr 10962729Sdfr next = msghdr->msg_spot; 10972729Sdfr for (len = 0; len < msgsz; len += msginfo.msgssz) { 10982729Sdfr size_t tlen; 10992729Sdfr 110045921Ssada if (msgsz - len > msginfo.msgssz) 11012729Sdfr tlen = msginfo.msgssz; 11022729Sdfr else 110345921Ssada tlen = msgsz - len; 11042729Sdfr if (next <= -1) 11052729Sdfr panic("next too low #3"); 11062729Sdfr if (next >= msginfo.msgseg) 11072729Sdfr panic("next out of range #3"); 1108101772Salfred mtx_unlock(&msq_mtx); 1109100511Salfred error = copyout(&msgpool[next * msginfo.msgssz], 11102729Sdfr user_msgp, tlen); 1111101772Salfred mtx_lock(&msq_mtx); 111282607Sdillon if (error != 0) { 1113100523Salfred DPRINTF(("error (%d) copying out message segment\n", 1114100523Salfred error)); 11152729Sdfr msg_freehdr(msghdr); 1116100511Salfred wakeup(msqptr); 111782607Sdillon goto done2; 11182729Sdfr } 111917971Sbde user_msgp = (char *)user_msgp + tlen; 11202729Sdfr next = msgmaps[next].next; 11212729Sdfr } 11222729Sdfr 11232729Sdfr /* 11242729Sdfr * Done, return the actual number of bytes copied out. 11252729Sdfr */ 11262729Sdfr 11272729Sdfr msg_freehdr(msghdr); 1128100511Salfred wakeup(msqptr); 112983366Sjulian td->td_retval[0] = msgsz; 113082607Sdillondone2: 1131101772Salfred mtx_unlock(&msq_mtx); 113282607Sdillon return (error); 11332729Sdfr} 113477461Sdd 113577461Sddstatic int 113677461Sddsysctl_msqids(SYSCTL_HANDLER_ARGS) 113777461Sdd{ 113877461Sdd 113977461Sdd return (SYSCTL_OUT(req, msqids, 114077461Sdd sizeof(struct msqid_ds) * msginfo.msgmni)); 114177461Sdd} 114277461Sdd 114377461SddSYSCTL_DECL(_kern_ipc); 114477461SddSYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, ""); 1145121307SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0, ""); 114677461SddSYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RD, &msginfo.msgmnb, 0, ""); 114777461SddSYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RD, &msginfo.msgtql, 0, ""); 1148121307SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0, ""); 1149121307SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0, ""); 115077461SddSYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD, 115177461Sdd NULL, 0, sysctl_msqids, "", "Message queue IDs"); 1152