sysv_msg.c revision 167211
1139804Simp/*- 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 */ 19140614Srwatson/*- 20140614Srwatson * Copyright (c) 2003-2005 McAfee, Inc. 21140614Srwatson * All rights reserved. 22140614Srwatson * 23140614Srwatson * This software was developed for the FreeBSD Project in part by McAfee 24140614Srwatson * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR 25140614Srwatson * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research 26140614Srwatson * program. 27140614Srwatson * 28140614Srwatson * Redistribution and use in source and binary forms, with or without 29140614Srwatson * modification, are permitted provided that the following conditions 30140614Srwatson * are met: 31140614Srwatson * 1. Redistributions of source code must retain the above copyright 32140614Srwatson * notice, this list of conditions and the following disclaimer. 33140614Srwatson * 2. Redistributions in binary form must reproduce the above copyright 34140614Srwatson * notice, this list of conditions and the following disclaimer in the 35140614Srwatson * documentation and/or other materials provided with the distribution. 36140614Srwatson * 37140614Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 38140614Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39140614Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40140614Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 41140614Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42140614Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43140614Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44140614Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45140614Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46140614Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47140614Srwatson * SUCH DAMAGE. 48140614Srwatson */ 492729Sdfr 50116182Sobrien#include <sys/cdefs.h> 51116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/sysv_msg.c 167211 2007-03-04 22:36:48Z rwatson $"); 52116182Sobrien 5359839Speter#include "opt_sysvipc.h" 54140614Srwatson#include "opt_mac.h" 5559839Speter 562729Sdfr#include <sys/param.h> 572729Sdfr#include <sys/systm.h> 5811626Sbde#include <sys/sysproto.h> 592729Sdfr#include <sys/kernel.h> 60164033Srwatson#include <sys/priv.h> 612729Sdfr#include <sys/proc.h> 6282607Sdillon#include <sys/lock.h> 6382607Sdillon#include <sys/mutex.h> 64129882Sphk#include <sys/module.h> 652729Sdfr#include <sys/msg.h> 6669449Salfred#include <sys/syscall.h> 67140839Ssobomax#include <sys/syscallsubr.h> 6811626Sbde#include <sys/sysent.h> 6959839Speter#include <sys/sysctl.h> 7059839Speter#include <sys/malloc.h> 7168024Srwatson#include <sys/jail.h> 722729Sdfr 73163606Srwatson#include <security/mac/mac_framework.h> 74163606Srwatson 7559839Speterstatic MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues"); 7659839Speter 7792723Salfredstatic void msginit(void); 7892723Salfredstatic int msgunload(void); 7992723Salfredstatic int sysvmsg_modload(struct module *, int, void *); 8010358Sjulian 81100523Salfred#ifdef MSG_DEBUG 82100523Salfred#define DPRINTF(a) printf a 83100523Salfred#else 84100523Salfred#define DPRINTF(a) 85100523Salfred#endif 862729Sdfr 8792723Salfredstatic void msg_freehdr(struct msg *msghdr); 882729Sdfr 8911626Sbde/* XXX casting to (sy_call_t *) is bogus, as usual. */ 9012819Sphkstatic sy_call_t *msgcalls[] = { 9111626Sbde (sy_call_t *)msgctl, (sy_call_t *)msgget, 9211626Sbde (sy_call_t *)msgsnd, (sy_call_t *)msgrcv 9311626Sbde}; 942729Sdfr 9559839Speter#ifndef MSGSSZ 9659839Speter#define MSGSSZ 8 /* Each segment must be 2^N long */ 9759839Speter#endif 9859839Speter#ifndef MSGSEG 9959839Speter#define MSGSEG 2048 /* must be less than 32767 */ 10059839Speter#endif 10159839Speter#define MSGMAX (MSGSSZ*MSGSEG) 10259839Speter#ifndef MSGMNB 10359839Speter#define MSGMNB 2048 /* max # of bytes in a queue */ 10459839Speter#endif 10559839Speter#ifndef MSGMNI 10659839Speter#define MSGMNI 40 10759839Speter#endif 10859839Speter#ifndef MSGTQL 10959839Speter#define MSGTQL 40 11059839Speter#endif 11159839Speter 11259839Speter/* 11359839Speter * Based on the configuration parameters described in an SVR2 (yes, two) 11459839Speter * config(1m) man page. 11559839Speter * 11659839Speter * Each message is broken up and stored in segments that are msgssz bytes 11759839Speter * long. For efficiency reasons, this should be a power of two. Also, 11859839Speter * it doesn't make sense if it is less than 8 or greater than about 256. 11959839Speter * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of 12059839Speter * two between 8 and 1024 inclusive (and panic's if it isn't). 12159839Speter */ 12259839Speterstruct msginfo msginfo = { 12359839Speter MSGMAX, /* max chars in a message */ 12459839Speter MSGMNI, /* # of message queue identifiers */ 12559839Speter MSGMNB, /* max chars in a queue */ 12659839Speter MSGTQL, /* max messages in system */ 12759839Speter MSGSSZ, /* size of a message segment */ 12859839Speter /* (must be small power of 2 greater than 4) */ 12959839Speter MSGSEG /* number of message segments */ 13059839Speter}; 13159839Speter 13259839Speter/* 13359839Speter * macros to convert between msqid_ds's and msqid's. 13459839Speter * (specific to this implementation) 13559839Speter */ 13659839Speter#define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000)) 13759839Speter#define MSQID_IX(id) ((id) & 0xffff) 13859839Speter#define MSQID_SEQ(id) (((id) >> 16) & 0xffff) 13959839Speter 14059839Speter/* 14159839Speter * The rest of this file is specific to this particular implementation. 14259839Speter */ 14359839Speter 14459839Speterstruct msgmap { 14559839Speter short next; /* next segment in buffer */ 14659839Speter /* -1 -> available */ 14759839Speter /* 0..(MSGSEG-1) -> index of next segment */ 14859839Speter}; 14959839Speter 15059839Speter#define MSG_LOCKED 01000 /* Is this msqid_ds locked? */ 15159839Speter 15212819Sphkstatic int nfree_msgmaps; /* # of free map entries */ 15312819Sphkstatic short free_msgmaps; /* head of linked list of free map entries */ 15459839Speterstatic struct msg *free_msghdrs;/* list of free msg headers */ 15559839Speterstatic char *msgpool; /* MSGMAX byte long msg buffer pool */ 15659839Speterstatic struct msgmap *msgmaps; /* MSGSEG msgmap structures */ 15759839Speterstatic struct msg *msghdrs; /* MSGTQL msg headers */ 158137613Srwatsonstatic struct msqid_kernel *msqids; /* MSGMNI msqid_kernel struct's */ 159101772Salfredstatic struct mtx msq_mtx; /* global mutex for message queues. */ 1602729Sdfr 16159839Speterstatic void 16269449Salfredmsginit() 1632729Sdfr{ 1642729Sdfr register int i; 1652729Sdfr 16683765Smr TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg); 16783765Smr TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz); 16883765Smr msginfo.msgmax = msginfo.msgseg * msginfo.msgssz; 16983765Smr TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni); 170139436Srwatson TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb); 171139436Srwatson TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql); 17283765Smr 173111119Simp msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK); 17459839Speter if (msgpool == NULL) 17559839Speter panic("msgpool is NULL"); 176111119Simp msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK); 17759839Speter if (msgmaps == NULL) 17859839Speter panic("msgmaps is NULL"); 179111119Simp msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK); 18059839Speter if (msghdrs == NULL) 18159839Speter panic("msghdrs is NULL"); 182137613Srwatson msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG, 183137613Srwatson M_WAITOK); 18459839Speter if (msqids == NULL) 18559839Speter panic("msqids is NULL"); 18659839Speter 1872729Sdfr /* 1882729Sdfr * msginfo.msgssz should be a power of two for efficiency reasons. 1892729Sdfr * It is also pretty silly if msginfo.msgssz is less than 8 1902729Sdfr * or greater than about 256 so ... 1912729Sdfr */ 1922729Sdfr 1932729Sdfr i = 8; 1942729Sdfr while (i < 1024 && i != msginfo.msgssz) 1952729Sdfr i <<= 1; 1962729Sdfr if (i != msginfo.msgssz) { 197100523Salfred DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz, 198100523Salfred msginfo.msgssz)); 1992729Sdfr panic("msginfo.msgssz not a small power of 2"); 2002729Sdfr } 2012729Sdfr 2022729Sdfr if (msginfo.msgseg > 32767) { 203100523Salfred DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg)); 2042729Sdfr panic("msginfo.msgseg > 32767"); 2052729Sdfr } 2062729Sdfr 2072729Sdfr if (msgmaps == NULL) 2082729Sdfr panic("msgmaps is NULL"); 2092729Sdfr 2102729Sdfr for (i = 0; i < msginfo.msgseg; i++) { 2112729Sdfr if (i > 0) 2122729Sdfr msgmaps[i-1].next = i; 2132729Sdfr msgmaps[i].next = -1; /* implies entry is available */ 2142729Sdfr } 2152729Sdfr free_msgmaps = 0; 2162729Sdfr nfree_msgmaps = msginfo.msgseg; 2172729Sdfr 2182729Sdfr if (msghdrs == NULL) 2192729Sdfr panic("msghdrs is NULL"); 2202729Sdfr 2212729Sdfr for (i = 0; i < msginfo.msgtql; i++) { 2222729Sdfr msghdrs[i].msg_type = 0; 2232729Sdfr if (i > 0) 2242729Sdfr msghdrs[i-1].msg_next = &msghdrs[i]; 2252729Sdfr msghdrs[i].msg_next = NULL; 226140614Srwatson#ifdef MAC 227140614Srwatson mac_init_sysv_msgmsg(&msghdrs[i]); 228140614Srwatson#endif 2292729Sdfr } 2302729Sdfr free_msghdrs = &msghdrs[0]; 2312729Sdfr 2322729Sdfr if (msqids == NULL) 2332729Sdfr panic("msqids is NULL"); 2342729Sdfr 2352729Sdfr for (i = 0; i < msginfo.msgmni; i++) { 236137613Srwatson msqids[i].u.msg_qbytes = 0; /* implies entry is available */ 237137613Srwatson msqids[i].u.msg_perm.seq = 0; /* reset to a known value */ 238137613Srwatson msqids[i].u.msg_perm.mode = 0; 239140614Srwatson#ifdef MAC 240140614Srwatson mac_init_sysv_msgqueue(&msqids[i]); 241140614Srwatson#endif 2422729Sdfr } 243101772Salfred mtx_init(&msq_mtx, "msq", NULL, MTX_DEF); 2442729Sdfr} 2452729Sdfr 24669449Salfredstatic int 24769449Salfredmsgunload() 24869449Salfred{ 249137613Srwatson struct msqid_kernel *msqkptr; 25069449Salfred int msqid; 251140614Srwatson#ifdef MAC 252140614Srwatson int i; 253140614Srwatson#endif 25469449Salfred 25569449Salfred for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 25669449Salfred /* 25769449Salfred * Look for an unallocated and unlocked msqid_ds. 25869449Salfred * msqid_ds's can be locked by msgsnd or msgrcv while 25969449Salfred * they are copying the message in/out. We can't 26069449Salfred * re-use the entry until they release it. 26169449Salfred */ 262137613Srwatson msqkptr = &msqids[msqid]; 263137613Srwatson if (msqkptr->u.msg_qbytes != 0 || 264137613Srwatson (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) 26569449Salfred break; 26669449Salfred } 26769449Salfred if (msqid != msginfo.msgmni) 26869449Salfred return (EBUSY); 26969449Salfred 270140614Srwatson#ifdef MAC 271140614Srwatson for (i = 0; i < msginfo.msgtql; i++) 272140614Srwatson mac_destroy_sysv_msgmsg(&msghdrs[i]); 273140614Srwatson for (msqid = 0; msqid < msginfo.msgmni; msqid++) 274140614Srwatson mac_destroy_sysv_msgqueue(&msqids[msqid]); 275140614Srwatson#endif 27669449Salfred free(msgpool, M_MSG); 27769449Salfred free(msgmaps, M_MSG); 27869449Salfred free(msghdrs, M_MSG); 27969449Salfred free(msqids, M_MSG); 280101772Salfred mtx_destroy(&msq_mtx); 28169449Salfred return (0); 28269449Salfred} 28369449Salfred 28469449Salfred 28569449Salfredstatic int 28669449Salfredsysvmsg_modload(struct module *module, int cmd, void *arg) 28769449Salfred{ 28869449Salfred int error = 0; 28969449Salfred 29069449Salfred switch (cmd) { 29169449Salfred case MOD_LOAD: 29269449Salfred msginit(); 29369449Salfred break; 29469449Salfred case MOD_UNLOAD: 29569449Salfred error = msgunload(); 29669449Salfred break; 29769449Salfred case MOD_SHUTDOWN: 29869449Salfred break; 29969449Salfred default: 30069449Salfred error = EINVAL; 30169449Salfred break; 30269449Salfred } 30369449Salfred return (error); 30469449Salfred} 30569449Salfred 30671038Sdesstatic moduledata_t sysvmsg_mod = { 30771038Sdes "sysvmsg", 30869449Salfred &sysvmsg_modload, 30969449Salfred NULL 31069449Salfred}; 31169449Salfred 31288633SalfredSYSCALL_MODULE_HELPER(msgsys); 31388633SalfredSYSCALL_MODULE_HELPER(msgctl); 31488633SalfredSYSCALL_MODULE_HELPER(msgget); 31588633SalfredSYSCALL_MODULE_HELPER(msgsnd); 31688633SalfredSYSCALL_MODULE_HELPER(msgrcv); 31769449Salfred 31871038SdesDECLARE_MODULE(sysvmsg, sysvmsg_mod, 31969449Salfred SI_SUB_SYSV_MSG, SI_ORDER_FIRST); 32071038SdesMODULE_VERSION(sysvmsg, 1); 32169449Salfred 3222729Sdfr/* 323167211Srwatson * Entry point for all MSG calls. 3242729Sdfr */ 3252729Sdfrint 32683366Sjulianmsgsys(td, uap) 32783366Sjulian struct thread *td; 32811626Sbde /* XXX actually varargs. */ 32911626Sbde struct msgsys_args /* { 330118615Snectar int which; 33111626Sbde int a2; 33211626Sbde int a3; 33311626Sbde int a4; 33411626Sbde int a5; 33511626Sbde int a6; 33611626Sbde } */ *uap; 3372729Sdfr{ 33882607Sdillon int error; 3392729Sdfr 34091703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 34191703Sjhb return (ENOSYS); 342118615Snectar if (uap->which < 0 || 343118615Snectar uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0])) 34491703Sjhb return (EINVAL); 34583366Sjulian error = (*msgcalls[uap->which])(td, &uap->a2); 34682607Sdillon return (error); 3472729Sdfr} 3482729Sdfr 3492729Sdfrstatic void 3502729Sdfrmsg_freehdr(msghdr) 3512729Sdfr struct msg *msghdr; 3522729Sdfr{ 3532729Sdfr while (msghdr->msg_ts > 0) { 3542729Sdfr short next; 3552729Sdfr if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg) 3562729Sdfr panic("msghdr->msg_spot out of range"); 3572729Sdfr next = msgmaps[msghdr->msg_spot].next; 3582729Sdfr msgmaps[msghdr->msg_spot].next = free_msgmaps; 3592729Sdfr free_msgmaps = msghdr->msg_spot; 3602729Sdfr nfree_msgmaps++; 3612729Sdfr msghdr->msg_spot = next; 3622729Sdfr if (msghdr->msg_ts >= msginfo.msgssz) 3632729Sdfr msghdr->msg_ts -= msginfo.msgssz; 3642729Sdfr else 3652729Sdfr msghdr->msg_ts = 0; 3662729Sdfr } 3672729Sdfr if (msghdr->msg_spot != -1) 3682729Sdfr panic("msghdr->msg_spot != -1"); 3692729Sdfr msghdr->msg_next = free_msghdrs; 3702729Sdfr free_msghdrs = msghdr; 371140614Srwatson#ifdef MAC 372140614Srwatson mac_cleanup_sysv_msgmsg(msghdr); 373140614Srwatson#endif 3742729Sdfr} 3752729Sdfr 37612866Speter#ifndef _SYS_SYSPROTO_H_ 3772729Sdfrstruct msgctl_args { 3782729Sdfr int msqid; 3792729Sdfr int cmd; 38012866Speter struct msqid_ds *buf; 3812729Sdfr}; 38212866Speter#endif 3832729Sdfr 38412866Speterint 38583366Sjulianmsgctl(td, uap) 38683366Sjulian struct thread *td; 3872729Sdfr register struct msgctl_args *uap; 3882729Sdfr{ 3892729Sdfr int msqid = uap->msqid; 3902729Sdfr int cmd = uap->cmd; 3912729Sdfr struct msqid_ds msqbuf; 392140839Ssobomax int error; 393140839Ssobomax 394165403Sjkim DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf)); 395140839Ssobomax if (cmd == IPC_SET && 396140839Ssobomax (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0) 397140839Ssobomax return (error); 398141471Sjhb error = kern_msgctl(td, msqid, cmd, &msqbuf); 399140839Ssobomax if (cmd == IPC_STAT && error == 0) 400141471Sjhb error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds)); 401140839Ssobomax return (error); 402140839Ssobomax} 403140839Ssobomax 404140839Ssobomaxint 405141471Sjhbkern_msgctl(td, msqid, cmd, msqbuf) 406140839Ssobomax struct thread *td; 407140839Ssobomax int msqid; 408140839Ssobomax int cmd; 409140839Ssobomax struct msqid_ds *msqbuf; 410140839Ssobomax{ 411140839Ssobomax int rval, error, msqix; 412137613Srwatson register struct msqid_kernel *msqkptr; 4132729Sdfr 41491703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 41591703Sjhb return (ENOSYS); 41691703Sjhb 417140839Ssobomax msqix = IPCID_TO_IX(msqid); 4182729Sdfr 419140839Ssobomax if (msqix < 0 || msqix >= msginfo.msgmni) { 420140839Ssobomax DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix, 421100523Salfred msginfo.msgmni)); 422101772Salfred return (EINVAL); 4232729Sdfr } 4242729Sdfr 425140839Ssobomax msqkptr = &msqids[msqix]; 4262729Sdfr 427101772Salfred mtx_lock(&msq_mtx); 428137613Srwatson if (msqkptr->u.msg_qbytes == 0) { 429100523Salfred DPRINTF(("no such msqid\n")); 43082607Sdillon error = EINVAL; 43182607Sdillon goto done2; 4322729Sdfr } 433140839Ssobomax if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) { 434100523Salfred DPRINTF(("wrong sequence number\n")); 43582607Sdillon error = EINVAL; 43682607Sdillon goto done2; 4372729Sdfr } 438140614Srwatson#ifdef MAC 439140614Srwatson error = mac_check_sysv_msqctl(td->td_ucred, msqkptr, cmd); 440162468Srwatson if (error != 0) 441140614Srwatson goto done2; 442140614Srwatson#endif 4432729Sdfr 44482607Sdillon error = 0; 4452729Sdfr rval = 0; 4462729Sdfr 4472729Sdfr switch (cmd) { 4482729Sdfr 4492729Sdfr case IPC_RMID: 4502729Sdfr { 4512729Sdfr struct msg *msghdr; 452137613Srwatson if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M))) 45382607Sdillon goto done2; 454137613Srwatson 455140614Srwatson#ifdef MAC 456140614Srwatson /* 457140614Srwatson * Check that the thread has MAC access permissions to 458140614Srwatson * individual msghdrs. Note: We need to do this in a 459140614Srwatson * separate loop because the actual loop alters the 460140614Srwatson * msq/msghdr info as it progresses, and there is no going 461140614Srwatson * back if half the way through we discover that the 462140614Srwatson * thread cannot free a certain msghdr. The msq will get 463140614Srwatson * into an inconsistent state. 464140614Srwatson */ 465140614Srwatson for (msghdr = msqkptr->u.msg_first; msghdr != NULL; 466140614Srwatson msghdr = msghdr->msg_next) { 467140614Srwatson error = mac_check_sysv_msgrmid(td->td_ucred, msghdr); 468162468Srwatson if (error != 0) 469140614Srwatson goto done2; 470140614Srwatson } 471140614Srwatson#endif 472140614Srwatson 4732729Sdfr /* Free the message headers */ 474137613Srwatson msghdr = msqkptr->u.msg_first; 4752729Sdfr while (msghdr != NULL) { 4762729Sdfr struct msg *msghdr_tmp; 4772729Sdfr 4782729Sdfr /* Free the segments of each message */ 479137613Srwatson msqkptr->u.msg_cbytes -= msghdr->msg_ts; 480137613Srwatson msqkptr->u.msg_qnum--; 4812729Sdfr msghdr_tmp = msghdr; 4822729Sdfr msghdr = msghdr->msg_next; 4832729Sdfr msg_freehdr(msghdr_tmp); 4842729Sdfr } 4852729Sdfr 486137613Srwatson if (msqkptr->u.msg_cbytes != 0) 4872729Sdfr panic("msg_cbytes is screwed up"); 488137613Srwatson if (msqkptr->u.msg_qnum != 0) 4892729Sdfr panic("msg_qnum is screwed up"); 4902729Sdfr 491137613Srwatson msqkptr->u.msg_qbytes = 0; /* Mark it as free */ 4922729Sdfr 493140614Srwatson#ifdef MAC 494140614Srwatson mac_cleanup_sysv_msgqueue(msqkptr); 495140614Srwatson#endif 496140614Srwatson 497137613Srwatson wakeup(msqkptr); 4982729Sdfr } 4992729Sdfr 5002729Sdfr break; 5012729Sdfr 5022729Sdfr case IPC_SET: 503137613Srwatson if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M))) 50482607Sdillon goto done2; 505140839Ssobomax if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) { 506166829Srwatson error = priv_check_cred(td->td_ucred, 507166829Srwatson PRIV_IPC_MSGSIZE, SUSER_ALLOWJAIL); 50882607Sdillon if (error) 50982607Sdillon goto done2; 51043426Sphk } 511140839Ssobomax if (msqbuf->msg_qbytes > msginfo.msgmnb) { 512100523Salfred DPRINTF(("can't increase msg_qbytes beyond %d" 513100523Salfred "(truncating)\n", msginfo.msgmnb)); 514140839Ssobomax msqbuf->msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */ 5152729Sdfr } 516140839Ssobomax if (msqbuf->msg_qbytes == 0) { 517100523Salfred DPRINTF(("can't reduce msg_qbytes to 0\n")); 51882607Sdillon error = EINVAL; /* non-standard errno! */ 51982607Sdillon goto done2; 5202729Sdfr } 521140839Ssobomax msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */ 522140839Ssobomax msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */ 523137613Srwatson msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) | 524140839Ssobomax (msqbuf->msg_perm.mode & 0777); 525140839Ssobomax msqkptr->u.msg_qbytes = msqbuf->msg_qbytes; 526137613Srwatson msqkptr->u.msg_ctime = time_second; 5272729Sdfr break; 5282729Sdfr 5292729Sdfr case IPC_STAT: 530137613Srwatson if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) { 531100523Salfred DPRINTF(("requester doesn't have read access\n")); 53282607Sdillon goto done2; 5332729Sdfr } 534141471Sjhb *msqbuf = msqkptr->u; 5352729Sdfr break; 5362729Sdfr 5372729Sdfr default: 538100523Salfred DPRINTF(("invalid command %d\n", cmd)); 53982607Sdillon error = EINVAL; 54082607Sdillon goto done2; 5412729Sdfr } 5422729Sdfr 54382607Sdillon if (error == 0) 54483366Sjulian td->td_retval[0] = rval; 54582607Sdillondone2: 546101772Salfred mtx_unlock(&msq_mtx); 547141471Sjhb return (error); 5482729Sdfr} 5492729Sdfr 55012866Speter#ifndef _SYS_SYSPROTO_H_ 5512729Sdfrstruct msgget_args { 5522729Sdfr key_t key; 5532729Sdfr int msgflg; 5542729Sdfr}; 55512866Speter#endif 5562729Sdfr 55712866Speterint 55883366Sjulianmsgget(td, uap) 55983366Sjulian struct thread *td; 5602729Sdfr register struct msgget_args *uap; 5612729Sdfr{ 56282607Sdillon int msqid, error = 0; 5632729Sdfr int key = uap->key; 5642729Sdfr int msgflg = uap->msgflg; 56591703Sjhb struct ucred *cred = td->td_ucred; 566137613Srwatson register struct msqid_kernel *msqkptr = NULL; 5672729Sdfr 568100523Salfred DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg)); 5692729Sdfr 57091703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 57191703Sjhb return (ENOSYS); 57291703Sjhb 573101772Salfred mtx_lock(&msq_mtx); 5742729Sdfr if (key != IPC_PRIVATE) { 5752729Sdfr for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 576137613Srwatson msqkptr = &msqids[msqid]; 577137613Srwatson if (msqkptr->u.msg_qbytes != 0 && 578137613Srwatson msqkptr->u.msg_perm.key == key) 5792729Sdfr break; 5802729Sdfr } 5812729Sdfr if (msqid < msginfo.msgmni) { 582100523Salfred DPRINTF(("found public key\n")); 5832729Sdfr if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) { 584100523Salfred DPRINTF(("not exclusive\n")); 58582607Sdillon error = EEXIST; 58682607Sdillon goto done2; 5872729Sdfr } 588137613Srwatson if ((error = ipcperm(td, &msqkptr->u.msg_perm, 589137613Srwatson msgflg & 0700))) { 590100523Salfred DPRINTF(("requester doesn't have 0%o access\n", 591100523Salfred msgflg & 0700)); 59282607Sdillon goto done2; 5932729Sdfr } 594140614Srwatson#ifdef MAC 595140614Srwatson error = mac_check_sysv_msqget(cred, msqkptr); 596162468Srwatson if (error != 0) 597140614Srwatson goto done2; 598140614Srwatson#endif 5992729Sdfr goto found; 6002729Sdfr } 6012729Sdfr } 6022729Sdfr 603100523Salfred DPRINTF(("need to allocate the msqid_ds\n")); 6042729Sdfr if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) { 6052729Sdfr for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 6062729Sdfr /* 6072729Sdfr * Look for an unallocated and unlocked msqid_ds. 6082729Sdfr * msqid_ds's can be locked by msgsnd or msgrcv while 6092729Sdfr * they are copying the message in/out. We can't 6102729Sdfr * re-use the entry until they release it. 6112729Sdfr */ 612137613Srwatson msqkptr = &msqids[msqid]; 613137613Srwatson if (msqkptr->u.msg_qbytes == 0 && 614137613Srwatson (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0) 6152729Sdfr break; 6162729Sdfr } 6172729Sdfr if (msqid == msginfo.msgmni) { 618100523Salfred DPRINTF(("no more msqid_ds's available\n")); 61982607Sdillon error = ENOSPC; 62082607Sdillon goto done2; 6212729Sdfr } 622100523Salfred DPRINTF(("msqid %d is available\n", msqid)); 623137613Srwatson msqkptr->u.msg_perm.key = key; 624137613Srwatson msqkptr->u.msg_perm.cuid = cred->cr_uid; 625137613Srwatson msqkptr->u.msg_perm.uid = cred->cr_uid; 626137613Srwatson msqkptr->u.msg_perm.cgid = cred->cr_gid; 627137613Srwatson msqkptr->u.msg_perm.gid = cred->cr_gid; 628137613Srwatson msqkptr->u.msg_perm.mode = (msgflg & 0777); 6292729Sdfr /* Make sure that the returned msqid is unique */ 630137613Srwatson msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff; 631137613Srwatson msqkptr->u.msg_first = NULL; 632137613Srwatson msqkptr->u.msg_last = NULL; 633137613Srwatson msqkptr->u.msg_cbytes = 0; 634137613Srwatson msqkptr->u.msg_qnum = 0; 635137613Srwatson msqkptr->u.msg_qbytes = msginfo.msgmnb; 636137613Srwatson msqkptr->u.msg_lspid = 0; 637137613Srwatson msqkptr->u.msg_lrpid = 0; 638137613Srwatson msqkptr->u.msg_stime = 0; 639137613Srwatson msqkptr->u.msg_rtime = 0; 640137613Srwatson msqkptr->u.msg_ctime = time_second; 641140614Srwatson#ifdef MAC 642140614Srwatson mac_create_sysv_msgqueue(cred, msqkptr); 643140614Srwatson#endif 6442729Sdfr } else { 645100523Salfred DPRINTF(("didn't find it and wasn't asked to create it\n")); 64682607Sdillon error = ENOENT; 64782607Sdillon goto done2; 6482729Sdfr } 6492729Sdfr 6502729Sdfrfound: 6512729Sdfr /* Construct the unique msqid */ 652137613Srwatson td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm); 65382607Sdillondone2: 654101772Salfred mtx_unlock(&msq_mtx); 65582607Sdillon return (error); 6562729Sdfr} 6572729Sdfr 65812866Speter#ifndef _SYS_SYSPROTO_H_ 6592729Sdfrstruct msgsnd_args { 6602729Sdfr int msqid; 661109895Salfred const void *msgp; 6622729Sdfr size_t msgsz; 6632729Sdfr int msgflg; 6642729Sdfr}; 66512866Speter#endif 6662729Sdfr 66712866Speterint 668165403Sjkimkern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype) 66983366Sjulian struct thread *td; 670165403Sjkim int msqid; 671165403Sjkim const void *msgp; /* XXX msgp is actually mtext. */ 672165403Sjkim size_t msgsz; 673165403Sjkim int msgflg; 674165403Sjkim long mtype; 6752729Sdfr{ 676165403Sjkim int msqix, segs_needed, error = 0; 677137613Srwatson register struct msqid_kernel *msqkptr; 6782729Sdfr register struct msg *msghdr; 6792729Sdfr short next; 6802729Sdfr 68191703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 68291703Sjhb return (ENOSYS); 68391703Sjhb 684101772Salfred mtx_lock(&msq_mtx); 685165403Sjkim msqix = IPCID_TO_IX(msqid); 6862729Sdfr 687165403Sjkim if (msqix < 0 || msqix >= msginfo.msgmni) { 688165403Sjkim DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix, 689100523Salfred msginfo.msgmni)); 69082607Sdillon error = EINVAL; 69182607Sdillon goto done2; 6922729Sdfr } 6932729Sdfr 694165403Sjkim msqkptr = &msqids[msqix]; 695137613Srwatson if (msqkptr->u.msg_qbytes == 0) { 696100523Salfred DPRINTF(("no such message queue id\n")); 69782607Sdillon error = EINVAL; 69882607Sdillon goto done2; 6992729Sdfr } 700165403Sjkim if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) { 701100523Salfred DPRINTF(("wrong sequence number\n")); 70282607Sdillon error = EINVAL; 70382607Sdillon goto done2; 7042729Sdfr } 7052729Sdfr 706137613Srwatson if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) { 707100523Salfred DPRINTF(("requester doesn't have write access\n")); 70882607Sdillon goto done2; 7092729Sdfr } 7102729Sdfr 711140614Srwatson#ifdef MAC 712140614Srwatson error = mac_check_sysv_msqsnd(td->td_ucred, msqkptr); 713162468Srwatson if (error != 0) 714140614Srwatson goto done2; 715140614Srwatson#endif 716140614Srwatson 7172729Sdfr segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz; 718165403Sjkim DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz, 719165403Sjkim msginfo.msgssz, segs_needed)); 7202729Sdfr for (;;) { 7212729Sdfr int need_more_resources = 0; 7222729Sdfr 7232729Sdfr /* 7242729Sdfr * check msgsz 7252729Sdfr * (inside this loop in case msg_qbytes changes while we sleep) 7262729Sdfr */ 7272729Sdfr 728137613Srwatson if (msgsz > msqkptr->u.msg_qbytes) { 729137613Srwatson DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n")); 73082607Sdillon error = EINVAL; 73182607Sdillon goto done2; 7322729Sdfr } 7332729Sdfr 734137613Srwatson if (msqkptr->u.msg_perm.mode & MSG_LOCKED) { 735100523Salfred DPRINTF(("msqid is locked\n")); 7362729Sdfr need_more_resources = 1; 7372729Sdfr } 738137613Srwatson if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) { 739100523Salfred DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n")); 7402729Sdfr need_more_resources = 1; 7412729Sdfr } 7422729Sdfr if (segs_needed > nfree_msgmaps) { 743100523Salfred DPRINTF(("segs_needed > nfree_msgmaps\n")); 7442729Sdfr need_more_resources = 1; 7452729Sdfr } 7462729Sdfr if (free_msghdrs == NULL) { 747100523Salfred DPRINTF(("no more msghdrs\n")); 7482729Sdfr need_more_resources = 1; 7492729Sdfr } 7502729Sdfr 7512729Sdfr if (need_more_resources) { 7522729Sdfr int we_own_it; 7532729Sdfr 7542729Sdfr if ((msgflg & IPC_NOWAIT) != 0) { 755100523Salfred DPRINTF(("need more resources but caller " 756100523Salfred "doesn't want to wait\n")); 75782607Sdillon error = EAGAIN; 75882607Sdillon goto done2; 7592729Sdfr } 7602729Sdfr 761137613Srwatson if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) { 762100523Salfred DPRINTF(("we don't own the msqid_ds\n")); 7632729Sdfr we_own_it = 0; 7642729Sdfr } else { 7652729Sdfr /* Force later arrivals to wait for our 7662729Sdfr request */ 767100523Salfred DPRINTF(("we own the msqid_ds\n")); 768137613Srwatson msqkptr->u.msg_perm.mode |= MSG_LOCKED; 7692729Sdfr we_own_it = 1; 7702729Sdfr } 771164368Sjkim DPRINTF(("msgsnd: goodnight\n")); 772137613Srwatson error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH, 773164368Sjkim "msgsnd", hz); 774164368Sjkim DPRINTF(("msgsnd: good morning, error=%d\n", error)); 7752729Sdfr if (we_own_it) 776137613Srwatson msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; 777164368Sjkim if (error == EWOULDBLOCK) { 778164368Sjkim DPRINTF(("msgsnd: timed out\n")); 779164368Sjkim continue; 780164368Sjkim } 78182607Sdillon if (error != 0) { 782100523Salfred DPRINTF(("msgsnd: interrupted system call\n")); 78382607Sdillon error = EINTR; 78482607Sdillon goto done2; 7852729Sdfr } 7862729Sdfr 7872729Sdfr /* 7882729Sdfr * Make sure that the msq queue still exists 7892729Sdfr */ 7902729Sdfr 791137613Srwatson if (msqkptr->u.msg_qbytes == 0) { 792100523Salfred DPRINTF(("msqid deleted\n")); 79382607Sdillon error = EIDRM; 79482607Sdillon goto done2; 7952729Sdfr } 7962729Sdfr 7972729Sdfr } else { 798100523Salfred DPRINTF(("got all the resources that we need\n")); 7992729Sdfr break; 8002729Sdfr } 8012729Sdfr } 8022729Sdfr 8032729Sdfr /* 8042729Sdfr * We have the resources that we need. 8052729Sdfr * Make sure! 8062729Sdfr */ 8072729Sdfr 808137613Srwatson if (msqkptr->u.msg_perm.mode & MSG_LOCKED) 8092729Sdfr panic("msg_perm.mode & MSG_LOCKED"); 8102729Sdfr if (segs_needed > nfree_msgmaps) 8112729Sdfr panic("segs_needed > nfree_msgmaps"); 812137613Srwatson if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) 8132729Sdfr panic("msgsz + msg_cbytes > msg_qbytes"); 8142729Sdfr if (free_msghdrs == NULL) 8152729Sdfr panic("no more msghdrs"); 8162729Sdfr 8172729Sdfr /* 8182729Sdfr * Re-lock the msqid_ds in case we page-fault when copying in the 8192729Sdfr * message 8202729Sdfr */ 8212729Sdfr 822137613Srwatson if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) 8232729Sdfr panic("msqid_ds is already locked"); 824137613Srwatson msqkptr->u.msg_perm.mode |= MSG_LOCKED; 8252729Sdfr 8262729Sdfr /* 8272729Sdfr * Allocate a message header 8282729Sdfr */ 8292729Sdfr 8302729Sdfr msghdr = free_msghdrs; 8312729Sdfr free_msghdrs = msghdr->msg_next; 8322729Sdfr msghdr->msg_spot = -1; 8332729Sdfr msghdr->msg_ts = msgsz; 834165403Sjkim msghdr->msg_type = mtype; 835140614Srwatson#ifdef MAC 836140614Srwatson /* 837140614Srwatson * XXXMAC: Should the mac_check_sysv_msgmsq check follow here 838140614Srwatson * immediately? Or, should it be checked just before the msg is 839140614Srwatson * enqueued in the msgq (as it is done now)? 840140614Srwatson */ 841140614Srwatson mac_create_sysv_msgmsg(td->td_ucred, msqkptr, msghdr); 842140614Srwatson#endif 8432729Sdfr 8442729Sdfr /* 8452729Sdfr * Allocate space for the message 8462729Sdfr */ 8472729Sdfr 8482729Sdfr while (segs_needed > 0) { 8492729Sdfr if (nfree_msgmaps <= 0) 8502729Sdfr panic("not enough msgmaps"); 8512729Sdfr if (free_msgmaps == -1) 8522729Sdfr panic("nil free_msgmaps"); 8532729Sdfr next = free_msgmaps; 8542729Sdfr if (next <= -1) 8552729Sdfr panic("next too low #1"); 8562729Sdfr if (next >= msginfo.msgseg) 8572729Sdfr panic("next out of range #1"); 858100523Salfred DPRINTF(("allocating segment %d to message\n", next)); 8592729Sdfr free_msgmaps = msgmaps[next].next; 8602729Sdfr nfree_msgmaps--; 8612729Sdfr msgmaps[next].next = msghdr->msg_spot; 8622729Sdfr msghdr->msg_spot = next; 8632729Sdfr segs_needed--; 8642729Sdfr } 8652729Sdfr 8662729Sdfr /* 8672729Sdfr * Validate the message type 8682729Sdfr */ 8692729Sdfr 8702729Sdfr if (msghdr->msg_type < 1) { 8712729Sdfr msg_freehdr(msghdr); 872137613Srwatson msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; 873137613Srwatson wakeup(msqkptr); 874165403Sjkim DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type)); 87582607Sdillon error = EINVAL; 87682607Sdillon goto done2; 8772729Sdfr } 8782729Sdfr 8792729Sdfr /* 8802729Sdfr * Copy in the message body 8812729Sdfr */ 8822729Sdfr 8832729Sdfr next = msghdr->msg_spot; 8842729Sdfr while (msgsz > 0) { 8852729Sdfr size_t tlen; 8862729Sdfr if (msgsz > msginfo.msgssz) 8872729Sdfr tlen = msginfo.msgssz; 8882729Sdfr else 8892729Sdfr tlen = msgsz; 8902729Sdfr if (next <= -1) 8912729Sdfr panic("next too low #2"); 8922729Sdfr if (next >= msginfo.msgseg) 8932729Sdfr panic("next out of range #2"); 894101772Salfred mtx_unlock(&msq_mtx); 895165403Sjkim if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz], 8962729Sdfr tlen)) != 0) { 897101772Salfred mtx_lock(&msq_mtx); 898100523Salfred DPRINTF(("error %d copying in message segment\n", 899100523Salfred error)); 9002729Sdfr msg_freehdr(msghdr); 901137613Srwatson msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; 902137613Srwatson wakeup(msqkptr); 90382607Sdillon goto done2; 9042729Sdfr } 905101772Salfred mtx_lock(&msq_mtx); 9062729Sdfr msgsz -= tlen; 907165403Sjkim msgp = (const char *)msgp + tlen; 9082729Sdfr next = msgmaps[next].next; 9092729Sdfr } 9102729Sdfr if (next != -1) 9112729Sdfr panic("didn't use all the msg segments"); 9122729Sdfr 9132729Sdfr /* 9142729Sdfr * We've got the message. Unlock the msqid_ds. 9152729Sdfr */ 9162729Sdfr 917137613Srwatson msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; 9182729Sdfr 9192729Sdfr /* 9202729Sdfr * Make sure that the msqid_ds is still allocated. 9212729Sdfr */ 9222729Sdfr 923137613Srwatson if (msqkptr->u.msg_qbytes == 0) { 9242729Sdfr msg_freehdr(msghdr); 925137613Srwatson wakeup(msqkptr); 92682607Sdillon error = EIDRM; 92782607Sdillon goto done2; 9282729Sdfr } 9292729Sdfr 930140614Srwatson#ifdef MAC 9312729Sdfr /* 932140614Srwatson * Note: Since the task/thread allocates the msghdr and usually 933140614Srwatson * primes it with its own MAC label, for a majority of policies, it 934140614Srwatson * won't be necessary to check whether the msghdr has access 935140614Srwatson * permissions to the msgq. The mac_check_sysv_msqsnd check would 936140614Srwatson * suffice in that case. However, this hook may be required where 937140614Srwatson * individual policies derive a non-identical label for the msghdr 938140614Srwatson * from the current thread label and may want to check the msghdr 939140614Srwatson * enqueue permissions, along with read/write permissions to the 940140614Srwatson * msgq. 941140614Srwatson */ 942140614Srwatson error = mac_check_sysv_msgmsq(td->td_ucred, msghdr, msqkptr); 943140614Srwatson if (error != 0) { 944140614Srwatson msg_freehdr(msghdr); 945140614Srwatson wakeup(msqkptr); 946140614Srwatson goto done2; 947140614Srwatson } 948140614Srwatson#endif 949140614Srwatson 950140614Srwatson /* 9512729Sdfr * Put the message into the queue 9522729Sdfr */ 953137613Srwatson if (msqkptr->u.msg_first == NULL) { 954137613Srwatson msqkptr->u.msg_first = msghdr; 955137613Srwatson msqkptr->u.msg_last = msghdr; 9562729Sdfr } else { 957137613Srwatson msqkptr->u.msg_last->msg_next = msghdr; 958137613Srwatson msqkptr->u.msg_last = msghdr; 9592729Sdfr } 960137613Srwatson msqkptr->u.msg_last->msg_next = NULL; 9612729Sdfr 962137613Srwatson msqkptr->u.msg_cbytes += msghdr->msg_ts; 963137613Srwatson msqkptr->u.msg_qnum++; 964137613Srwatson msqkptr->u.msg_lspid = td->td_proc->p_pid; 965137613Srwatson msqkptr->u.msg_stime = time_second; 9662729Sdfr 967137613Srwatson wakeup(msqkptr); 96883366Sjulian td->td_retval[0] = 0; 96982607Sdillondone2: 970101772Salfred mtx_unlock(&msq_mtx); 97182607Sdillon return (error); 9722729Sdfr} 9732729Sdfr 974165403Sjkimint 975165403Sjkimmsgsnd(td, uap) 976165403Sjkim struct thread *td; 977165403Sjkim register struct msgsnd_args *uap; 978165403Sjkim{ 979165403Sjkim int error; 980165403Sjkim long mtype; 981165403Sjkim 982165403Sjkim DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp, 983165403Sjkim uap->msgsz, uap->msgflg)); 984165403Sjkim 985165403Sjkim if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) { 986165403Sjkim DPRINTF(("error %d copying the message type\n", error)); 987165403Sjkim return (error); 988165403Sjkim } 989165403Sjkim return (kern_msgsnd(td, uap->msqid, 990165403Sjkim (const char *)uap->msgp + sizeof(mtype), 991165403Sjkim uap->msgsz, uap->msgflg, mtype)); 992165403Sjkim} 993165403Sjkim 99412866Speter#ifndef _SYS_SYSPROTO_H_ 9952729Sdfrstruct msgrcv_args { 9962729Sdfr int msqid; 9972729Sdfr void *msgp; 9982729Sdfr size_t msgsz; 9992729Sdfr long msgtyp; 10002729Sdfr int msgflg; 10012729Sdfr}; 100212866Speter#endif 10032729Sdfr 100412866Speterint 1005165403Sjkimkern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype) 100683366Sjulian struct thread *td; 1007165403Sjkim int msqid; 1008165403Sjkim void *msgp; /* XXX msgp is actually mtext. */ 1009165403Sjkim size_t msgsz; 1010165403Sjkim long msgtyp; 1011165403Sjkim int msgflg; 1012165403Sjkim long *mtype; 10132729Sdfr{ 10142729Sdfr size_t len; 1015137613Srwatson register struct msqid_kernel *msqkptr; 10162729Sdfr register struct msg *msghdr; 1017165403Sjkim int msqix, error = 0; 10182729Sdfr short next; 10192729Sdfr 102091703Sjhb if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 102191703Sjhb return (ENOSYS); 102291703Sjhb 1023165403Sjkim msqix = IPCID_TO_IX(msqid); 10242729Sdfr 1025165403Sjkim if (msqix < 0 || msqix >= msginfo.msgmni) { 1026165403Sjkim DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix, 1027100523Salfred msginfo.msgmni)); 1028101772Salfred return (EINVAL); 10292729Sdfr } 10302729Sdfr 1031165403Sjkim msqkptr = &msqids[msqix]; 1032101772Salfred mtx_lock(&msq_mtx); 1033137613Srwatson if (msqkptr->u.msg_qbytes == 0) { 1034100523Salfred DPRINTF(("no such message queue id\n")); 103582607Sdillon error = EINVAL; 103682607Sdillon goto done2; 10372729Sdfr } 1038165403Sjkim if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) { 1039100523Salfred DPRINTF(("wrong sequence number\n")); 104082607Sdillon error = EINVAL; 104182607Sdillon goto done2; 10422729Sdfr } 10432729Sdfr 1044137613Srwatson if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) { 1045100523Salfred DPRINTF(("requester doesn't have read access\n")); 104682607Sdillon goto done2; 10472729Sdfr } 10482729Sdfr 1049140614Srwatson#ifdef MAC 1050140614Srwatson error = mac_check_sysv_msqrcv(td->td_ucred, msqkptr); 1051162468Srwatson if (error != 0) 1052140614Srwatson goto done2; 1053140614Srwatson#endif 1054140614Srwatson 10552729Sdfr msghdr = NULL; 10562729Sdfr while (msghdr == NULL) { 10572729Sdfr if (msgtyp == 0) { 1058137613Srwatson msghdr = msqkptr->u.msg_first; 10592729Sdfr if (msghdr != NULL) { 10602729Sdfr if (msgsz < msghdr->msg_ts && 10612729Sdfr (msgflg & MSG_NOERROR) == 0) { 1062100523Salfred DPRINTF(("first message on the queue " 1063165403Sjkim "is too big (want %zu, got %d)\n", 1064100523Salfred msgsz, msghdr->msg_ts)); 106582607Sdillon error = E2BIG; 106682607Sdillon goto done2; 10672729Sdfr } 1068140614Srwatson#ifdef MAC 1069140614Srwatson error = mac_check_sysv_msgrcv(td->td_ucred, 1070140614Srwatson msghdr); 1071162468Srwatson if (error != 0) 1072140614Srwatson goto done2; 1073140614Srwatson#endif 1074137613Srwatson if (msqkptr->u.msg_first == msqkptr->u.msg_last) { 1075137613Srwatson msqkptr->u.msg_first = NULL; 1076137613Srwatson msqkptr->u.msg_last = NULL; 10772729Sdfr } else { 1078137613Srwatson msqkptr->u.msg_first = msghdr->msg_next; 1079137613Srwatson if (msqkptr->u.msg_first == NULL) 10802729Sdfr panic("msg_first/last screwed up #1"); 10812729Sdfr } 10822729Sdfr } 10832729Sdfr } else { 10842729Sdfr struct msg *previous; 10852729Sdfr struct msg **prev; 10862729Sdfr 10872729Sdfr previous = NULL; 1088137613Srwatson prev = &(msqkptr->u.msg_first); 10892729Sdfr while ((msghdr = *prev) != NULL) { 10902729Sdfr /* 10912729Sdfr * Is this message's type an exact match or is 10922729Sdfr * this message's type less than or equal to 10932729Sdfr * the absolute value of a negative msgtyp? 10942729Sdfr * Note that the second half of this test can 10952729Sdfr * NEVER be true if msgtyp is positive since 10962729Sdfr * msg_type is always positive! 10972729Sdfr */ 10982729Sdfr 10992729Sdfr if (msgtyp == msghdr->msg_type || 11002729Sdfr msghdr->msg_type <= -msgtyp) { 1101165403Sjkim DPRINTF(("found message type %ld, " 1102165403Sjkim "requested %ld\n", 1103100523Salfred msghdr->msg_type, msgtyp)); 11042729Sdfr if (msgsz < msghdr->msg_ts && 11052729Sdfr (msgflg & MSG_NOERROR) == 0) { 1106100523Salfred DPRINTF(("requested message " 1107100523Salfred "on the queue is too big " 1108165403Sjkim "(want %zu, got %hu)\n", 1109100523Salfred msgsz, msghdr->msg_ts)); 111082607Sdillon error = E2BIG; 111182607Sdillon goto done2; 11122729Sdfr } 1113140614Srwatson#ifdef MAC 1114140614Srwatson error = mac_check_sysv_msgrcv( 1115140614Srwatson td->td_ucred, msghdr); 1116162468Srwatson if (error != 0) 1117140614Srwatson goto done2; 1118140614Srwatson#endif 11192729Sdfr *prev = msghdr->msg_next; 1120137613Srwatson if (msghdr == msqkptr->u.msg_last) { 11212729Sdfr if (previous == NULL) { 11222729Sdfr if (prev != 1123137613Srwatson &msqkptr->u.msg_first) 11242729Sdfr panic("msg_first/last screwed up #2"); 1125137613Srwatson msqkptr->u.msg_first = 11262729Sdfr NULL; 1127137613Srwatson msqkptr->u.msg_last = 11282729Sdfr NULL; 11292729Sdfr } else { 11302729Sdfr if (prev == 1131137613Srwatson &msqkptr->u.msg_first) 11322729Sdfr panic("msg_first/last screwed up #3"); 1133137613Srwatson msqkptr->u.msg_last = 11342729Sdfr previous; 11352729Sdfr } 11362729Sdfr } 11372729Sdfr break; 11382729Sdfr } 11392729Sdfr previous = msghdr; 11402729Sdfr prev = &(msghdr->msg_next); 11412729Sdfr } 11422729Sdfr } 11432729Sdfr 11442729Sdfr /* 11452729Sdfr * We've either extracted the msghdr for the appropriate 11462729Sdfr * message or there isn't one. 11472729Sdfr * If there is one then bail out of this loop. 11482729Sdfr */ 11492729Sdfr 11502729Sdfr if (msghdr != NULL) 11512729Sdfr break; 11522729Sdfr 11532729Sdfr /* 11542729Sdfr * Hmph! No message found. Does the user want to wait? 11552729Sdfr */ 11562729Sdfr 11572729Sdfr if ((msgflg & IPC_NOWAIT) != 0) { 1158165403Sjkim DPRINTF(("no appropriate message found (msgtyp=%ld)\n", 1159100523Salfred msgtyp)); 11602729Sdfr /* The SVID says to return ENOMSG. */ 116182607Sdillon error = ENOMSG; 116282607Sdillon goto done2; 11632729Sdfr } 11642729Sdfr 11652729Sdfr /* 11662729Sdfr * Wait for something to happen 11672729Sdfr */ 11682729Sdfr 1169100523Salfred DPRINTF(("msgrcv: goodnight\n")); 1170137613Srwatson error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH, 1171164368Sjkim "msgrcv", 0); 1172100523Salfred DPRINTF(("msgrcv: good morning (error=%d)\n", error)); 11732729Sdfr 117482607Sdillon if (error != 0) { 1175164368Sjkim DPRINTF(("msgrcv: interrupted system call\n")); 117682607Sdillon error = EINTR; 117782607Sdillon goto done2; 11782729Sdfr } 11792729Sdfr 11802729Sdfr /* 11812729Sdfr * Make sure that the msq queue still exists 11822729Sdfr */ 11832729Sdfr 1184137613Srwatson if (msqkptr->u.msg_qbytes == 0 || 1185165403Sjkim msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) { 1186100523Salfred DPRINTF(("msqid deleted\n")); 118782607Sdillon error = EIDRM; 118882607Sdillon goto done2; 11892729Sdfr } 11902729Sdfr } 11912729Sdfr 11922729Sdfr /* 11932729Sdfr * Return the message to the user. 11942729Sdfr * 11952729Sdfr * First, do the bookkeeping (before we risk being interrupted). 11962729Sdfr */ 11972729Sdfr 1198137613Srwatson msqkptr->u.msg_cbytes -= msghdr->msg_ts; 1199137613Srwatson msqkptr->u.msg_qnum--; 1200137613Srwatson msqkptr->u.msg_lrpid = td->td_proc->p_pid; 1201137613Srwatson msqkptr->u.msg_rtime = time_second; 12022729Sdfr 12032729Sdfr /* 12042729Sdfr * Make msgsz the actual amount that we'll be returning. 12052729Sdfr * Note that this effectively truncates the message if it is too long 12062729Sdfr * (since msgsz is never increased). 12072729Sdfr */ 12082729Sdfr 1209165403Sjkim DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz, 1210100523Salfred msghdr->msg_ts)); 12112729Sdfr if (msgsz > msghdr->msg_ts) 12122729Sdfr msgsz = msghdr->msg_ts; 1213165403Sjkim *mtype = msghdr->msg_type; 12142729Sdfr 12152729Sdfr /* 12162729Sdfr * Return the segments to the user 12172729Sdfr */ 12182729Sdfr 12192729Sdfr next = msghdr->msg_spot; 12202729Sdfr for (len = 0; len < msgsz; len += msginfo.msgssz) { 12212729Sdfr size_t tlen; 12222729Sdfr 122345921Ssada if (msgsz - len > msginfo.msgssz) 12242729Sdfr tlen = msginfo.msgssz; 12252729Sdfr else 122645921Ssada tlen = msgsz - len; 12272729Sdfr if (next <= -1) 12282729Sdfr panic("next too low #3"); 12292729Sdfr if (next >= msginfo.msgseg) 12302729Sdfr panic("next out of range #3"); 1231101772Salfred mtx_unlock(&msq_mtx); 1232165403Sjkim error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen); 1233101772Salfred mtx_lock(&msq_mtx); 123482607Sdillon if (error != 0) { 1235100523Salfred DPRINTF(("error (%d) copying out message segment\n", 1236100523Salfred error)); 12372729Sdfr msg_freehdr(msghdr); 1238137613Srwatson wakeup(msqkptr); 123982607Sdillon goto done2; 12402729Sdfr } 1241165403Sjkim msgp = (char *)msgp + tlen; 12422729Sdfr next = msgmaps[next].next; 12432729Sdfr } 12442729Sdfr 12452729Sdfr /* 12462729Sdfr * Done, return the actual number of bytes copied out. 12472729Sdfr */ 12482729Sdfr 12492729Sdfr msg_freehdr(msghdr); 1250137613Srwatson wakeup(msqkptr); 125183366Sjulian td->td_retval[0] = msgsz; 125282607Sdillondone2: 1253101772Salfred mtx_unlock(&msq_mtx); 125482607Sdillon return (error); 12552729Sdfr} 125677461Sdd 1257165403Sjkimint 1258165403Sjkimmsgrcv(td, uap) 1259165403Sjkim struct thread *td; 1260165403Sjkim register struct msgrcv_args *uap; 1261165403Sjkim{ 1262165403Sjkim int error; 1263165403Sjkim long mtype; 1264165403Sjkim 1265165403Sjkim DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid, 1266165403Sjkim uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg)); 1267165403Sjkim 1268165403Sjkim if ((error = kern_msgrcv(td, uap->msqid, 1269165403Sjkim (char *)uap->msgp + sizeof(mtype), uap->msgsz, 1270165403Sjkim uap->msgtyp, uap->msgflg, &mtype)) != 0) 1271165403Sjkim return (error); 1272165403Sjkim if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0) 1273165403Sjkim DPRINTF(("error %d copying the message type\n", error)); 1274165403Sjkim return (error); 1275165403Sjkim} 1276165403Sjkim 127777461Sddstatic int 127877461Sddsysctl_msqids(SYSCTL_HANDLER_ARGS) 127977461Sdd{ 128077461Sdd 128177461Sdd return (SYSCTL_OUT(req, msqids, 1282137613Srwatson sizeof(struct msqid_kernel) * msginfo.msgmni)); 128377461Sdd} 128477461Sdd 1285141710ScsjpSYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, 1286141710Scsjp "Maximum message size"); 1287141710ScsjpSYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0, 1288141710Scsjp "Number of message queue identifiers"); 1289141710ScsjpSYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0, 1290141710Scsjp "Maximum number of bytes in a queue"); 1291141710ScsjpSYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0, 1292141710Scsjp "Maximum number of messages in the system"); 1293141710ScsjpSYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0, 1294141710Scsjp "Size of a message segment"); 1295141710ScsjpSYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0, 1296141710Scsjp "Number of message segments"); 129777461SddSYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD, 129877461Sdd NULL, 0, sysctl_msqids, "", "Message queue IDs"); 1299