The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/sysv_msg.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Implementation of SVID messages
    3  *
    4  * Author:  Daniel Boulet
    5  *
    6  * Copyright 1993 Daniel Boulet and RTMX Inc.
    7  *
    8  * This system call was implemented by Daniel Boulet under contract from RTMX.
    9  *
   10  * Redistribution and use in source forms, with and without modification,
   11  * are permitted provided that this entire comment appears intact.
   12  *
   13  * Redistribution in binary form may occur without any restrictions.
   14  * Obviously, it would be nice if you gave credit where credit is due
   15  * but requiring it would be too onerous.
   16  *
   17  * This software is provided ``AS IS'' without any warranties of any kind.
   18  */
   19 /*-
   20  * Copyright (c) 2003-2005 McAfee, Inc.
   21  * All rights reserved.
   22  *
   23  * This software was developed for the FreeBSD Project in part by McAfee
   24  * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
   25  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
   26  * program.
   27  *
   28  * Redistribution and use in source and binary forms, with or without
   29  * modification, are permitted provided that the following conditions
   30  * are met:
   31  * 1. Redistributions of source code must retain the above copyright
   32  *    notice, this list of conditions and the following disclaimer.
   33  * 2. Redistributions in binary form must reproduce the above copyright
   34  *    notice, this list of conditions and the following disclaimer in the
   35  *    documentation and/or other materials provided with the distribution.
   36  *
   37  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   47  * SUCH DAMAGE.
   48  */
   49 
   50 #include <sys/cdefs.h>
   51 __FBSDID("$FreeBSD: releng/10.2/sys/kern/sysv_msg.c 284665 2015-06-21 06:28:26Z trasz $");
   52 
   53 #include "opt_compat.h"
   54 #include "opt_sysvipc.h"
   55 
   56 #include <sys/param.h>
   57 #include <sys/systm.h>
   58 #include <sys/sysproto.h>
   59 #include <sys/kernel.h>
   60 #include <sys/priv.h>
   61 #include <sys/proc.h>
   62 #include <sys/lock.h>
   63 #include <sys/mutex.h>
   64 #include <sys/module.h>
   65 #include <sys/msg.h>
   66 #include <sys/racct.h>
   67 #include <sys/syscall.h>
   68 #include <sys/syscallsubr.h>
   69 #include <sys/sysent.h>
   70 #include <sys/sysctl.h>
   71 #include <sys/malloc.h>
   72 #include <sys/jail.h>
   73 
   74 #include <security/mac/mac_framework.h>
   75 
   76 FEATURE(sysv_msg, "System V message queues support");
   77 
   78 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
   79 
   80 static int msginit(void);
   81 static int msgunload(void);
   82 static int sysvmsg_modload(struct module *, int, void *);
   83 
   84 
   85 #ifdef MSG_DEBUG
   86 #define DPRINTF(a)      printf a
   87 #else
   88 #define DPRINTF(a)      (void)0
   89 #endif
   90 
   91 static void msg_freehdr(struct msg *msghdr);
   92 
   93 #ifndef MSGSSZ
   94 #define MSGSSZ  8               /* Each segment must be 2^N long */
   95 #endif
   96 #ifndef MSGSEG
   97 #define MSGSEG  2048            /* must be less than 32767 */
   98 #endif
   99 #define MSGMAX  (MSGSSZ*MSGSEG)
  100 #ifndef MSGMNB
  101 #define MSGMNB  2048            /* max # of bytes in a queue */
  102 #endif
  103 #ifndef MSGMNI
  104 #define MSGMNI  40
  105 #endif
  106 #ifndef MSGTQL
  107 #define MSGTQL  40
  108 #endif
  109 
  110 /*
  111  * Based on the configuration parameters described in an SVR2 (yes, two)
  112  * config(1m) man page.
  113  *
  114  * Each message is broken up and stored in segments that are msgssz bytes
  115  * long.  For efficiency reasons, this should be a power of two.  Also,
  116  * it doesn't make sense if it is less than 8 or greater than about 256.
  117  * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
  118  * two between 8 and 1024 inclusive (and panic's if it isn't).
  119  */
  120 struct msginfo msginfo = {
  121                 MSGMAX,         /* max chars in a message */
  122                 MSGMNI,         /* # of message queue identifiers */
  123                 MSGMNB,         /* max chars in a queue */
  124                 MSGTQL,         /* max messages in system */
  125                 MSGSSZ,         /* size of a message segment */
  126                                 /* (must be small power of 2 greater than 4) */
  127                 MSGSEG          /* number of message segments */
  128 };
  129 
  130 /*
  131  * macros to convert between msqid_ds's and msqid's.
  132  * (specific to this implementation)
  133  */
  134 #define MSQID(ix,ds)    ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
  135 #define MSQID_IX(id)    ((id) & 0xffff)
  136 #define MSQID_SEQ(id)   (((id) >> 16) & 0xffff)
  137 
  138 /*
  139  * The rest of this file is specific to this particular implementation.
  140  */
  141 
  142 struct msgmap {
  143         short   next;           /* next segment in buffer */
  144                                 /* -1 -> available */
  145                                 /* 0..(MSGSEG-1) -> index of next segment */
  146 };
  147 
  148 #define MSG_LOCKED      01000   /* Is this msqid_ds locked? */
  149 
  150 static int nfree_msgmaps;       /* # of free map entries */
  151 static short free_msgmaps;      /* head of linked list of free map entries */
  152 static struct msg *free_msghdrs;/* list of free msg headers */
  153 static char *msgpool;           /* MSGMAX byte long msg buffer pool */
  154 static struct msgmap *msgmaps;  /* MSGSEG msgmap structures */
  155 static struct msg *msghdrs;     /* MSGTQL msg headers */
  156 static struct msqid_kernel *msqids;     /* MSGMNI msqid_kernel struct's */
  157 static struct mtx msq_mtx;      /* global mutex for message queues. */
  158 
  159 static struct syscall_helper_data msg_syscalls[] = {
  160         SYSCALL_INIT_HELPER(msgctl),
  161         SYSCALL_INIT_HELPER(msgget),
  162         SYSCALL_INIT_HELPER(msgsnd),
  163         SYSCALL_INIT_HELPER(msgrcv),
  164 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
  165     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
  166         SYSCALL_INIT_HELPER(msgsys),
  167         SYSCALL_INIT_HELPER_COMPAT(freebsd7_msgctl),
  168 #endif
  169         SYSCALL_INIT_LAST
  170 };
  171 
  172 #ifdef COMPAT_FREEBSD32
  173 #include <compat/freebsd32/freebsd32.h>
  174 #include <compat/freebsd32/freebsd32_ipc.h>
  175 #include <compat/freebsd32/freebsd32_proto.h>
  176 #include <compat/freebsd32/freebsd32_signal.h>
  177 #include <compat/freebsd32/freebsd32_syscall.h>
  178 #include <compat/freebsd32/freebsd32_util.h>
  179 
  180 static struct syscall_helper_data msg32_syscalls[] = {
  181         SYSCALL32_INIT_HELPER(freebsd32_msgctl),
  182         SYSCALL32_INIT_HELPER(freebsd32_msgsnd),
  183         SYSCALL32_INIT_HELPER(freebsd32_msgrcv),
  184         SYSCALL32_INIT_HELPER_COMPAT(msgget),
  185         SYSCALL32_INIT_HELPER(freebsd32_msgsys),
  186 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
  187     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
  188         SYSCALL32_INIT_HELPER(freebsd7_freebsd32_msgctl),
  189 #endif
  190         SYSCALL_INIT_LAST
  191 };
  192 #endif
  193 
  194 static int
  195 msginit()
  196 {
  197         int i, error;
  198 
  199         TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
  200         TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
  201         msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
  202         TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
  203         TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
  204         TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
  205 
  206         msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
  207         msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
  208         msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
  209         msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
  210             M_WAITOK);
  211 
  212         /*
  213          * msginfo.msgssz should be a power of two for efficiency reasons.
  214          * It is also pretty silly if msginfo.msgssz is less than 8
  215          * or greater than about 256 so ...
  216          */
  217 
  218         i = 8;
  219         while (i < 1024 && i != msginfo.msgssz)
  220                 i <<= 1;
  221         if (i != msginfo.msgssz) {
  222                 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
  223                     msginfo.msgssz));
  224                 panic("msginfo.msgssz not a small power of 2");
  225         }
  226 
  227         if (msginfo.msgseg > 32767) {
  228                 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
  229                 panic("msginfo.msgseg > 32767");
  230         }
  231 
  232         for (i = 0; i < msginfo.msgseg; i++) {
  233                 if (i > 0)
  234                         msgmaps[i-1].next = i;
  235                 msgmaps[i].next = -1;   /* implies entry is available */
  236         }
  237         free_msgmaps = 0;
  238         nfree_msgmaps = msginfo.msgseg;
  239 
  240         for (i = 0; i < msginfo.msgtql; i++) {
  241                 msghdrs[i].msg_type = 0;
  242                 if (i > 0)
  243                         msghdrs[i-1].msg_next = &msghdrs[i];
  244                 msghdrs[i].msg_next = NULL;
  245 #ifdef MAC
  246                 mac_sysvmsg_init(&msghdrs[i]);
  247 #endif
  248         }
  249         free_msghdrs = &msghdrs[0];
  250 
  251         for (i = 0; i < msginfo.msgmni; i++) {
  252                 msqids[i].u.msg_qbytes = 0;     /* implies entry is available */
  253                 msqids[i].u.msg_perm.seq = 0;   /* reset to a known value */
  254                 msqids[i].u.msg_perm.mode = 0;
  255 #ifdef MAC
  256                 mac_sysvmsq_init(&msqids[i]);
  257 #endif
  258         }
  259         mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
  260 
  261         error = syscall_helper_register(msg_syscalls);
  262         if (error != 0)
  263                 return (error);
  264 #ifdef COMPAT_FREEBSD32
  265         error = syscall32_helper_register(msg32_syscalls);
  266         if (error != 0)
  267                 return (error);
  268 #endif
  269         return (0);
  270 }
  271 
  272 static int
  273 msgunload()
  274 {
  275         struct msqid_kernel *msqkptr;
  276         int msqid;
  277 #ifdef MAC
  278         int i;
  279 #endif
  280 
  281         syscall_helper_unregister(msg_syscalls);
  282 #ifdef COMPAT_FREEBSD32
  283         syscall32_helper_unregister(msg32_syscalls);
  284 #endif
  285 
  286         for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
  287                 /*
  288                  * Look for an unallocated and unlocked msqid_ds.
  289                  * msqid_ds's can be locked by msgsnd or msgrcv while
  290                  * they are copying the message in/out.  We can't
  291                  * re-use the entry until they release it.
  292                  */
  293                 msqkptr = &msqids[msqid];
  294                 if (msqkptr->u.msg_qbytes != 0 ||
  295                     (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
  296                         break;
  297         }
  298         if (msqid != msginfo.msgmni)
  299                 return (EBUSY);
  300 
  301 #ifdef MAC
  302         for (i = 0; i < msginfo.msgtql; i++)
  303                 mac_sysvmsg_destroy(&msghdrs[i]);
  304         for (msqid = 0; msqid < msginfo.msgmni; msqid++)
  305                 mac_sysvmsq_destroy(&msqids[msqid]);
  306 #endif
  307         free(msgpool, M_MSG);
  308         free(msgmaps, M_MSG);
  309         free(msghdrs, M_MSG);
  310         free(msqids, M_MSG);
  311         mtx_destroy(&msq_mtx);
  312         return (0);
  313 }
  314 
  315 
  316 static int
  317 sysvmsg_modload(struct module *module, int cmd, void *arg)
  318 {
  319         int error = 0;
  320 
  321         switch (cmd) {
  322         case MOD_LOAD:
  323                 error = msginit();
  324                 if (error != 0)
  325                         msgunload();
  326                 break;
  327         case MOD_UNLOAD:
  328                 error = msgunload();
  329                 break;
  330         case MOD_SHUTDOWN:
  331                 break;
  332         default:
  333                 error = EINVAL;
  334                 break;
  335         }
  336         return (error);
  337 }
  338 
  339 static moduledata_t sysvmsg_mod = {
  340         "sysvmsg",
  341         &sysvmsg_modload,
  342         NULL
  343 };
  344 
  345 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
  346 MODULE_VERSION(sysvmsg, 1);
  347 
  348 static void
  349 msg_freehdr(msghdr)
  350         struct msg *msghdr;
  351 {
  352         while (msghdr->msg_ts > 0) {
  353                 short next;
  354                 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
  355                         panic("msghdr->msg_spot out of range");
  356                 next = msgmaps[msghdr->msg_spot].next;
  357                 msgmaps[msghdr->msg_spot].next = free_msgmaps;
  358                 free_msgmaps = msghdr->msg_spot;
  359                 nfree_msgmaps++;
  360                 msghdr->msg_spot = next;
  361                 if (msghdr->msg_ts >= msginfo.msgssz)
  362                         msghdr->msg_ts -= msginfo.msgssz;
  363                 else
  364                         msghdr->msg_ts = 0;
  365         }
  366         if (msghdr->msg_spot != -1)
  367                 panic("msghdr->msg_spot != -1");
  368         msghdr->msg_next = free_msghdrs;
  369         free_msghdrs = msghdr;
  370 #ifdef MAC
  371         mac_sysvmsg_cleanup(msghdr);
  372 #endif
  373 }
  374 
  375 #ifndef _SYS_SYSPROTO_H_
  376 struct msgctl_args {
  377         int     msqid;
  378         int     cmd;
  379         struct  msqid_ds *buf;
  380 };
  381 #endif
  382 int
  383 sys_msgctl(td, uap)
  384         struct thread *td;
  385         register struct msgctl_args *uap;
  386 {
  387         int msqid = uap->msqid;
  388         int cmd = uap->cmd;
  389         struct msqid_ds msqbuf;
  390         int error;
  391 
  392         DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
  393         if (cmd == IPC_SET &&
  394             (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
  395                 return (error);
  396         error = kern_msgctl(td, msqid, cmd, &msqbuf);
  397         if (cmd == IPC_STAT && error == 0)
  398                 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
  399         return (error);
  400 }
  401 
  402 int
  403 kern_msgctl(td, msqid, cmd, msqbuf)
  404         struct thread *td;
  405         int msqid;
  406         int cmd;
  407         struct msqid_ds *msqbuf;
  408 {
  409         int rval, error, msqix;
  410         register struct msqid_kernel *msqkptr;
  411 
  412         if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
  413                 return (ENOSYS);
  414 
  415         msqix = IPCID_TO_IX(msqid);
  416 
  417         if (msqix < 0 || msqix >= msginfo.msgmni) {
  418                 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
  419                     msginfo.msgmni));
  420                 return (EINVAL);
  421         }
  422 
  423         msqkptr = &msqids[msqix];
  424 
  425         mtx_lock(&msq_mtx);
  426         if (msqkptr->u.msg_qbytes == 0) {
  427                 DPRINTF(("no such msqid\n"));
  428                 error = EINVAL;
  429                 goto done2;
  430         }
  431         if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
  432                 DPRINTF(("wrong sequence number\n"));
  433                 error = EINVAL;
  434                 goto done2;
  435         }
  436 #ifdef MAC
  437         error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
  438         if (error != 0)
  439                 goto done2;
  440 #endif
  441 
  442         error = 0;
  443         rval = 0;
  444 
  445         switch (cmd) {
  446 
  447         case IPC_RMID:
  448         {
  449                 struct msg *msghdr;
  450                 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
  451                         goto done2;
  452 
  453 #ifdef MAC
  454                 /*
  455                  * Check that the thread has MAC access permissions to
  456                  * individual msghdrs.  Note: We need to do this in a
  457                  * separate loop because the actual loop alters the
  458                  * msq/msghdr info as it progresses, and there is no going
  459                  * back if half the way through we discover that the
  460                  * thread cannot free a certain msghdr.  The msq will get
  461                  * into an inconsistent state.
  462                  */
  463                 for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
  464                     msghdr = msghdr->msg_next) {
  465                         error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
  466                         if (error != 0)
  467                                 goto done2;
  468                 }
  469 #endif
  470 
  471                 racct_sub_cred(msqkptr->cred, RACCT_NMSGQ, 1);
  472                 racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, msqkptr->u.msg_qnum);
  473                 racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msqkptr->u.msg_cbytes);
  474                 crfree(msqkptr->cred);
  475                 msqkptr->cred = NULL;
  476 
  477                 /* Free the message headers */
  478                 msghdr = msqkptr->u.msg_first;
  479                 while (msghdr != NULL) {
  480                         struct msg *msghdr_tmp;
  481 
  482                         /* Free the segments of each message */
  483                         msqkptr->u.msg_cbytes -= msghdr->msg_ts;
  484                         msqkptr->u.msg_qnum--;
  485                         msghdr_tmp = msghdr;
  486                         msghdr = msghdr->msg_next;
  487                         msg_freehdr(msghdr_tmp);
  488                 }
  489 
  490                 if (msqkptr->u.msg_cbytes != 0)
  491                         panic("msg_cbytes is screwed up");
  492                 if (msqkptr->u.msg_qnum != 0)
  493                         panic("msg_qnum is screwed up");
  494 
  495                 msqkptr->u.msg_qbytes = 0;      /* Mark it as free */
  496 
  497 #ifdef MAC
  498                 mac_sysvmsq_cleanup(msqkptr);
  499 #endif
  500 
  501                 wakeup(msqkptr);
  502         }
  503 
  504                 break;
  505 
  506         case IPC_SET:
  507                 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
  508                         goto done2;
  509                 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
  510                         error = priv_check(td, PRIV_IPC_MSGSIZE);
  511                         if (error)
  512                                 goto done2;
  513                 }
  514                 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
  515                         DPRINTF(("can't increase msg_qbytes beyond %d"
  516                             "(truncating)\n", msginfo.msgmnb));
  517                         msqbuf->msg_qbytes = msginfo.msgmnb;    /* silently restrict qbytes to system limit */
  518                 }
  519                 if (msqbuf->msg_qbytes == 0) {
  520                         DPRINTF(("can't reduce msg_qbytes to 0\n"));
  521                         error = EINVAL;         /* non-standard errno! */
  522                         goto done2;
  523                 }
  524                 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
  525                 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
  526                 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
  527                     (msqbuf->msg_perm.mode & 0777);
  528                 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
  529                 msqkptr->u.msg_ctime = time_second;
  530                 break;
  531 
  532         case IPC_STAT:
  533                 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
  534                         DPRINTF(("requester doesn't have read access\n"));
  535                         goto done2;
  536                 }
  537                 *msqbuf = msqkptr->u;
  538                 break;
  539 
  540         default:
  541                 DPRINTF(("invalid command %d\n", cmd));
  542                 error = EINVAL;
  543                 goto done2;
  544         }
  545 
  546         if (error == 0)
  547                 td->td_retval[0] = rval;
  548 done2:
  549         mtx_unlock(&msq_mtx);
  550         return (error);
  551 }
  552 
  553 #ifndef _SYS_SYSPROTO_H_
  554 struct msgget_args {
  555         key_t   key;
  556         int     msgflg;
  557 };
  558 #endif
  559 
  560 int
  561 sys_msgget(td, uap)
  562         struct thread *td;
  563         register struct msgget_args *uap;
  564 {
  565         int msqid, error = 0;
  566         int key = uap->key;
  567         int msgflg = uap->msgflg;
  568         struct ucred *cred = td->td_ucred;
  569         register struct msqid_kernel *msqkptr = NULL;
  570 
  571         DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
  572 
  573         if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
  574                 return (ENOSYS);
  575 
  576         mtx_lock(&msq_mtx);
  577         if (key != IPC_PRIVATE) {
  578                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
  579                         msqkptr = &msqids[msqid];
  580                         if (msqkptr->u.msg_qbytes != 0 &&
  581                             msqkptr->u.msg_perm.key == key)
  582                                 break;
  583                 }
  584                 if (msqid < msginfo.msgmni) {
  585                         DPRINTF(("found public key\n"));
  586                         if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
  587                                 DPRINTF(("not exclusive\n"));
  588                                 error = EEXIST;
  589                                 goto done2;
  590                         }
  591                         if ((error = ipcperm(td, &msqkptr->u.msg_perm,
  592                             msgflg & 0700))) {
  593                                 DPRINTF(("requester doesn't have 0%o access\n",
  594                                     msgflg & 0700));
  595                                 goto done2;
  596                         }
  597 #ifdef MAC
  598                         error = mac_sysvmsq_check_msqget(cred, msqkptr);
  599                         if (error != 0)
  600                                 goto done2;
  601 #endif
  602                         goto found;
  603                 }
  604         }
  605 
  606         DPRINTF(("need to allocate the msqid_ds\n"));
  607         if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
  608                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
  609                         /*
  610                          * Look for an unallocated and unlocked msqid_ds.
  611                          * msqid_ds's can be locked by msgsnd or msgrcv while
  612                          * they are copying the message in/out.  We can't
  613                          * re-use the entry until they release it.
  614                          */
  615                         msqkptr = &msqids[msqid];
  616                         if (msqkptr->u.msg_qbytes == 0 &&
  617                             (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
  618                                 break;
  619                 }
  620                 if (msqid == msginfo.msgmni) {
  621                         DPRINTF(("no more msqid_ds's available\n"));
  622                         error = ENOSPC;
  623                         goto done2;
  624                 }
  625 #ifdef RACCT
  626                 if (racct_enable) {
  627                         PROC_LOCK(td->td_proc);
  628                         error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
  629                         PROC_UNLOCK(td->td_proc);
  630                         if (error != 0) {
  631                                 error = ENOSPC;
  632                                 goto done2;
  633                         }
  634                 }
  635 #endif
  636                 DPRINTF(("msqid %d is available\n", msqid));
  637                 msqkptr->u.msg_perm.key = key;
  638                 msqkptr->u.msg_perm.cuid = cred->cr_uid;
  639                 msqkptr->u.msg_perm.uid = cred->cr_uid;
  640                 msqkptr->u.msg_perm.cgid = cred->cr_gid;
  641                 msqkptr->u.msg_perm.gid = cred->cr_gid;
  642                 msqkptr->u.msg_perm.mode = (msgflg & 0777);
  643                 msqkptr->cred = crhold(cred);
  644                 /* Make sure that the returned msqid is unique */
  645                 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
  646                 msqkptr->u.msg_first = NULL;
  647                 msqkptr->u.msg_last = NULL;
  648                 msqkptr->u.msg_cbytes = 0;
  649                 msqkptr->u.msg_qnum = 0;
  650                 msqkptr->u.msg_qbytes = msginfo.msgmnb;
  651                 msqkptr->u.msg_lspid = 0;
  652                 msqkptr->u.msg_lrpid = 0;
  653                 msqkptr->u.msg_stime = 0;
  654                 msqkptr->u.msg_rtime = 0;
  655                 msqkptr->u.msg_ctime = time_second;
  656 #ifdef MAC
  657                 mac_sysvmsq_create(cred, msqkptr);
  658 #endif
  659         } else {
  660                 DPRINTF(("didn't find it and wasn't asked to create it\n"));
  661                 error = ENOENT;
  662                 goto done2;
  663         }
  664 
  665 found:
  666         /* Construct the unique msqid */
  667         td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
  668 done2:
  669         mtx_unlock(&msq_mtx);
  670         return (error);
  671 }
  672 
  673 #ifndef _SYS_SYSPROTO_H_
  674 struct msgsnd_args {
  675         int     msqid;
  676         const void      *msgp;
  677         size_t  msgsz;
  678         int     msgflg;
  679 };
  680 #endif
  681 int
  682 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
  683         struct thread *td;
  684         int msqid;
  685         const void *msgp;       /* XXX msgp is actually mtext. */
  686         size_t msgsz;
  687         int msgflg;
  688         long mtype;
  689 {
  690         int msqix, segs_needed, error = 0;
  691         register struct msqid_kernel *msqkptr;
  692         register struct msg *msghdr;
  693         short next;
  694 #ifdef RACCT
  695         size_t saved_msgsz;
  696 #endif
  697 
  698         if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
  699                 return (ENOSYS);
  700 
  701         mtx_lock(&msq_mtx);
  702         msqix = IPCID_TO_IX(msqid);
  703 
  704         if (msqix < 0 || msqix >= msginfo.msgmni) {
  705                 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
  706                     msginfo.msgmni));
  707                 error = EINVAL;
  708                 goto done2;
  709         }
  710 
  711         msqkptr = &msqids[msqix];
  712         if (msqkptr->u.msg_qbytes == 0) {
  713                 DPRINTF(("no such message queue id\n"));
  714                 error = EINVAL;
  715                 goto done2;
  716         }
  717         if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
  718                 DPRINTF(("wrong sequence number\n"));
  719                 error = EINVAL;
  720                 goto done2;
  721         }
  722 
  723         if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
  724                 DPRINTF(("requester doesn't have write access\n"));
  725                 goto done2;
  726         }
  727 
  728 #ifdef MAC
  729         error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
  730         if (error != 0)
  731                 goto done2;
  732 #endif
  733 
  734 #ifdef RACCT
  735         if (racct_enable) {
  736                 PROC_LOCK(td->td_proc);
  737                 if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
  738                         PROC_UNLOCK(td->td_proc);
  739                         error = EAGAIN;
  740                         goto done2;
  741                 }
  742                 saved_msgsz = msgsz;
  743                 if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
  744                         racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
  745                         PROC_UNLOCK(td->td_proc);
  746                         error = EAGAIN;
  747                         goto done2;
  748                 }
  749                 PROC_UNLOCK(td->td_proc);
  750         }
  751 #endif
  752 
  753         segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
  754         DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
  755             msginfo.msgssz, segs_needed));
  756         for (;;) {
  757                 int need_more_resources = 0;
  758 
  759                 /*
  760                  * check msgsz
  761                  * (inside this loop in case msg_qbytes changes while we sleep)
  762                  */
  763 
  764                 if (msgsz > msqkptr->u.msg_qbytes) {
  765                         DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
  766                         error = EINVAL;
  767                         goto done3;
  768                 }
  769 
  770                 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
  771                         DPRINTF(("msqid is locked\n"));
  772                         need_more_resources = 1;
  773                 }
  774                 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
  775                         DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
  776                         need_more_resources = 1;
  777                 }
  778                 if (segs_needed > nfree_msgmaps) {
  779                         DPRINTF(("segs_needed > nfree_msgmaps\n"));
  780                         need_more_resources = 1;
  781                 }
  782                 if (free_msghdrs == NULL) {
  783                         DPRINTF(("no more msghdrs\n"));
  784                         need_more_resources = 1;
  785                 }
  786 
  787                 if (need_more_resources) {
  788                         int we_own_it;
  789 
  790                         if ((msgflg & IPC_NOWAIT) != 0) {
  791                                 DPRINTF(("need more resources but caller "
  792                                     "doesn't want to wait\n"));
  793                                 error = EAGAIN;
  794                                 goto done3;
  795                         }
  796 
  797                         if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
  798                                 DPRINTF(("we don't own the msqid_ds\n"));
  799                                 we_own_it = 0;
  800                         } else {
  801                                 /* Force later arrivals to wait for our
  802                                    request */
  803                                 DPRINTF(("we own the msqid_ds\n"));
  804                                 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
  805                                 we_own_it = 1;
  806                         }
  807                         DPRINTF(("msgsnd:  goodnight\n"));
  808                         error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
  809                             "msgsnd", hz);
  810                         DPRINTF(("msgsnd:  good morning, error=%d\n", error));
  811                         if (we_own_it)
  812                                 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
  813                         if (error == EWOULDBLOCK) {
  814                                 DPRINTF(("msgsnd:  timed out\n"));
  815                                 continue;
  816                         }
  817                         if (error != 0) {
  818                                 DPRINTF(("msgsnd:  interrupted system call\n"));
  819                                 error = EINTR;
  820                                 goto done3;
  821                         }
  822 
  823                         /*
  824                          * Make sure that the msq queue still exists
  825                          */
  826 
  827                         if (msqkptr->u.msg_qbytes == 0) {
  828                                 DPRINTF(("msqid deleted\n"));
  829                                 error = EIDRM;
  830                                 goto done3;
  831                         }
  832 
  833                 } else {
  834                         DPRINTF(("got all the resources that we need\n"));
  835                         break;
  836                 }
  837         }
  838 
  839         /*
  840          * We have the resources that we need.
  841          * Make sure!
  842          */
  843 
  844         if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
  845                 panic("msg_perm.mode & MSG_LOCKED");
  846         if (segs_needed > nfree_msgmaps)
  847                 panic("segs_needed > nfree_msgmaps");
  848         if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
  849                 panic("msgsz + msg_cbytes > msg_qbytes");
  850         if (free_msghdrs == NULL)
  851                 panic("no more msghdrs");
  852 
  853         /*
  854          * Re-lock the msqid_ds in case we page-fault when copying in the
  855          * message
  856          */
  857 
  858         if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
  859                 panic("msqid_ds is already locked");
  860         msqkptr->u.msg_perm.mode |= MSG_LOCKED;
  861 
  862         /*
  863          * Allocate a message header
  864          */
  865 
  866         msghdr = free_msghdrs;
  867         free_msghdrs = msghdr->msg_next;
  868         msghdr->msg_spot = -1;
  869         msghdr->msg_ts = msgsz;
  870         msghdr->msg_type = mtype;
  871 #ifdef MAC
  872         /*
  873          * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
  874          * immediately?  Or, should it be checked just before the msg is
  875          * enqueued in the msgq (as it is done now)?
  876          */
  877         mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
  878 #endif
  879 
  880         /*
  881          * Allocate space for the message
  882          */
  883 
  884         while (segs_needed > 0) {
  885                 if (nfree_msgmaps <= 0)
  886                         panic("not enough msgmaps");
  887                 if (free_msgmaps == -1)
  888                         panic("nil free_msgmaps");
  889                 next = free_msgmaps;
  890                 if (next <= -1)
  891                         panic("next too low #1");
  892                 if (next >= msginfo.msgseg)
  893                         panic("next out of range #1");
  894                 DPRINTF(("allocating segment %d to message\n", next));
  895                 free_msgmaps = msgmaps[next].next;
  896                 nfree_msgmaps--;
  897                 msgmaps[next].next = msghdr->msg_spot;
  898                 msghdr->msg_spot = next;
  899                 segs_needed--;
  900         }
  901 
  902         /*
  903          * Validate the message type
  904          */
  905 
  906         if (msghdr->msg_type < 1) {
  907                 msg_freehdr(msghdr);
  908                 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
  909                 wakeup(msqkptr);
  910                 DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
  911                 error = EINVAL;
  912                 goto done3;
  913         }
  914 
  915         /*
  916          * Copy in the message body
  917          */
  918 
  919         next = msghdr->msg_spot;
  920         while (msgsz > 0) {
  921                 size_t tlen;
  922                 if (msgsz > msginfo.msgssz)
  923                         tlen = msginfo.msgssz;
  924                 else
  925                         tlen = msgsz;
  926                 if (next <= -1)
  927                         panic("next too low #2");
  928                 if (next >= msginfo.msgseg)
  929                         panic("next out of range #2");
  930                 mtx_unlock(&msq_mtx);
  931                 if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
  932                     tlen)) != 0) {
  933                         mtx_lock(&msq_mtx);
  934                         DPRINTF(("error %d copying in message segment\n",
  935                             error));
  936                         msg_freehdr(msghdr);
  937                         msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
  938                         wakeup(msqkptr);
  939                         goto done3;
  940                 }
  941                 mtx_lock(&msq_mtx);
  942                 msgsz -= tlen;
  943                 msgp = (const char *)msgp + tlen;
  944                 next = msgmaps[next].next;
  945         }
  946         if (next != -1)
  947                 panic("didn't use all the msg segments");
  948 
  949         /*
  950          * We've got the message.  Unlock the msqid_ds.
  951          */
  952 
  953         msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
  954 
  955         /*
  956          * Make sure that the msqid_ds is still allocated.
  957          */
  958 
  959         if (msqkptr->u.msg_qbytes == 0) {
  960                 msg_freehdr(msghdr);
  961                 wakeup(msqkptr);
  962                 error = EIDRM;
  963                 goto done3;
  964         }
  965 
  966 #ifdef MAC
  967         /*
  968          * Note: Since the task/thread allocates the msghdr and usually
  969          * primes it with its own MAC label, for a majority of policies, it
  970          * won't be necessary to check whether the msghdr has access
  971          * permissions to the msgq.  The mac_sysvmsq_check_msqsnd check would
  972          * suffice in that case.  However, this hook may be required where
  973          * individual policies derive a non-identical label for the msghdr
  974          * from the current thread label and may want to check the msghdr
  975          * enqueue permissions, along with read/write permissions to the
  976          * msgq.
  977          */
  978         error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
  979         if (error != 0) {
  980                 msg_freehdr(msghdr);
  981                 wakeup(msqkptr);
  982                 goto done3;
  983         }
  984 #endif
  985 
  986         /*
  987          * Put the message into the queue
  988          */
  989         if (msqkptr->u.msg_first == NULL) {
  990                 msqkptr->u.msg_first = msghdr;
  991                 msqkptr->u.msg_last = msghdr;
  992         } else {
  993                 msqkptr->u.msg_last->msg_next = msghdr;
  994                 msqkptr->u.msg_last = msghdr;
  995         }
  996         msqkptr->u.msg_last->msg_next = NULL;
  997 
  998         msqkptr->u.msg_cbytes += msghdr->msg_ts;
  999         msqkptr->u.msg_qnum++;
 1000         msqkptr->u.msg_lspid = td->td_proc->p_pid;
 1001         msqkptr->u.msg_stime = time_second;
 1002 
 1003         wakeup(msqkptr);
 1004         td->td_retval[0] = 0;
 1005 done3:
 1006 #ifdef RACCT
 1007         if (racct_enable && error != 0) {
 1008                 PROC_LOCK(td->td_proc);
 1009                 racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
 1010                 racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
 1011                 PROC_UNLOCK(td->td_proc);
 1012         }
 1013 #endif
 1014 done2:
 1015         mtx_unlock(&msq_mtx);
 1016         return (error);
 1017 }
 1018 
 1019 int
 1020 sys_msgsnd(td, uap)
 1021         struct thread *td;
 1022         register struct msgsnd_args *uap;
 1023 {
 1024         int error;
 1025         long mtype;
 1026 
 1027         DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
 1028             uap->msgsz, uap->msgflg));
 1029 
 1030         if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
 1031                 DPRINTF(("error %d copying the message type\n", error));
 1032                 return (error);
 1033         }
 1034         return (kern_msgsnd(td, uap->msqid,
 1035             (const char *)uap->msgp + sizeof(mtype),
 1036             uap->msgsz, uap->msgflg, mtype));
 1037 }
 1038 
 1039 #ifndef _SYS_SYSPROTO_H_
 1040 struct msgrcv_args {
 1041         int     msqid;
 1042         void    *msgp;
 1043         size_t  msgsz;
 1044         long    msgtyp;
 1045         int     msgflg;
 1046 };
 1047 #endif
 1048 int
 1049 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
 1050         struct thread *td;
 1051         int msqid;
 1052         void *msgp;     /* XXX msgp is actually mtext. */
 1053         size_t msgsz;
 1054         long msgtyp;
 1055         int msgflg;
 1056         long *mtype;
 1057 {
 1058         size_t len;
 1059         register struct msqid_kernel *msqkptr;
 1060         register struct msg *msghdr;
 1061         int msqix, error = 0;
 1062         short next;
 1063 
 1064         if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
 1065                 return (ENOSYS);
 1066 
 1067         msqix = IPCID_TO_IX(msqid);
 1068 
 1069         if (msqix < 0 || msqix >= msginfo.msgmni) {
 1070                 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
 1071                     msginfo.msgmni));
 1072                 return (EINVAL);
 1073         }
 1074 
 1075         msqkptr = &msqids[msqix];
 1076         mtx_lock(&msq_mtx);
 1077         if (msqkptr->u.msg_qbytes == 0) {
 1078                 DPRINTF(("no such message queue id\n"));
 1079                 error = EINVAL;
 1080                 goto done2;
 1081         }
 1082         if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
 1083                 DPRINTF(("wrong sequence number\n"));
 1084                 error = EINVAL;
 1085                 goto done2;
 1086         }
 1087 
 1088         if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
 1089                 DPRINTF(("requester doesn't have read access\n"));
 1090                 goto done2;
 1091         }
 1092 
 1093 #ifdef MAC
 1094         error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
 1095         if (error != 0)
 1096                 goto done2;
 1097 #endif
 1098 
 1099         msghdr = NULL;
 1100         while (msghdr == NULL) {
 1101                 if (msgtyp == 0) {
 1102                         msghdr = msqkptr->u.msg_first;
 1103                         if (msghdr != NULL) {
 1104                                 if (msgsz < msghdr->msg_ts &&
 1105                                     (msgflg & MSG_NOERROR) == 0) {
 1106                                         DPRINTF(("first message on the queue "
 1107                                             "is too big (want %zu, got %d)\n",
 1108                                             msgsz, msghdr->msg_ts));
 1109                                         error = E2BIG;
 1110                                         goto done2;
 1111                                 }
 1112 #ifdef MAC
 1113                                 error = mac_sysvmsq_check_msgrcv(td->td_ucred,
 1114                                     msghdr);
 1115                                 if (error != 0)
 1116                                         goto done2;
 1117 #endif
 1118                                 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
 1119                                         msqkptr->u.msg_first = NULL;
 1120                                         msqkptr->u.msg_last = NULL;
 1121                                 } else {
 1122                                         msqkptr->u.msg_first = msghdr->msg_next;
 1123                                         if (msqkptr->u.msg_first == NULL)
 1124                                                 panic("msg_first/last screwed up #1");
 1125                                 }
 1126                         }
 1127                 } else {
 1128                         struct msg *previous;
 1129                         struct msg **prev;
 1130 
 1131                         previous = NULL;
 1132                         prev = &(msqkptr->u.msg_first);
 1133                         while ((msghdr = *prev) != NULL) {
 1134                                 /*
 1135                                  * Is this message's type an exact match or is
 1136                                  * this message's type less than or equal to
 1137                                  * the absolute value of a negative msgtyp?
 1138                                  * Note that the second half of this test can
 1139                                  * NEVER be true if msgtyp is positive since
 1140                                  * msg_type is always positive!
 1141                                  */
 1142 
 1143                                 if (msgtyp == msghdr->msg_type ||
 1144                                     msghdr->msg_type <= -msgtyp) {
 1145                                         DPRINTF(("found message type %ld, "
 1146                                             "requested %ld\n",
 1147                                             msghdr->msg_type, msgtyp));
 1148                                         if (msgsz < msghdr->msg_ts &&
 1149                                             (msgflg & MSG_NOERROR) == 0) {
 1150                                                 DPRINTF(("requested message "
 1151                                                     "on the queue is too big "
 1152                                                     "(want %zu, got %hu)\n",
 1153                                                     msgsz, msghdr->msg_ts));
 1154                                                 error = E2BIG;
 1155                                                 goto done2;
 1156                                         }
 1157 #ifdef MAC
 1158                                         error = mac_sysvmsq_check_msgrcv(
 1159                                             td->td_ucred, msghdr);
 1160                                         if (error != 0)
 1161                                                 goto done2;
 1162 #endif
 1163                                         *prev = msghdr->msg_next;
 1164                                         if (msghdr == msqkptr->u.msg_last) {
 1165                                                 if (previous == NULL) {
 1166                                                         if (prev !=
 1167                                                             &msqkptr->u.msg_first)
 1168                                                                 panic("msg_first/last screwed up #2");
 1169                                                         msqkptr->u.msg_first =
 1170                                                             NULL;
 1171                                                         msqkptr->u.msg_last =
 1172                                                             NULL;
 1173                                                 } else {
 1174                                                         if (prev ==
 1175                                                             &msqkptr->u.msg_first)
 1176                                                                 panic("msg_first/last screwed up #3");
 1177                                                         msqkptr->u.msg_last =
 1178                                                             previous;
 1179                                                 }
 1180                                         }
 1181                                         break;
 1182                                 }
 1183                                 previous = msghdr;
 1184                                 prev = &(msghdr->msg_next);
 1185                         }
 1186                 }
 1187 
 1188                 /*
 1189                  * We've either extracted the msghdr for the appropriate
 1190                  * message or there isn't one.
 1191                  * If there is one then bail out of this loop.
 1192                  */
 1193 
 1194                 if (msghdr != NULL)
 1195                         break;
 1196 
 1197                 /*
 1198                  * Hmph!  No message found.  Does the user want to wait?
 1199                  */
 1200 
 1201                 if ((msgflg & IPC_NOWAIT) != 0) {
 1202                         DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
 1203                             msgtyp));
 1204                         /* The SVID says to return ENOMSG. */
 1205                         error = ENOMSG;
 1206                         goto done2;
 1207                 }
 1208 
 1209                 /*
 1210                  * Wait for something to happen
 1211                  */
 1212 
 1213                 DPRINTF(("msgrcv:  goodnight\n"));
 1214                 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
 1215                     "msgrcv", 0);
 1216                 DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
 1217 
 1218                 if (error != 0) {
 1219                         DPRINTF(("msgrcv:  interrupted system call\n"));
 1220                         error = EINTR;
 1221                         goto done2;
 1222                 }
 1223 
 1224                 /*
 1225                  * Make sure that the msq queue still exists
 1226                  */
 1227 
 1228                 if (msqkptr->u.msg_qbytes == 0 ||
 1229                     msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
 1230                         DPRINTF(("msqid deleted\n"));
 1231                         error = EIDRM;
 1232                         goto done2;
 1233                 }
 1234         }
 1235 
 1236         /*
 1237          * Return the message to the user.
 1238          *
 1239          * First, do the bookkeeping (before we risk being interrupted).
 1240          */
 1241 
 1242         msqkptr->u.msg_cbytes -= msghdr->msg_ts;
 1243         msqkptr->u.msg_qnum--;
 1244         msqkptr->u.msg_lrpid = td->td_proc->p_pid;
 1245         msqkptr->u.msg_rtime = time_second;
 1246 
 1247         racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
 1248         racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
 1249 
 1250         /*
 1251          * Make msgsz the actual amount that we'll be returning.
 1252          * Note that this effectively truncates the message if it is too long
 1253          * (since msgsz is never increased).
 1254          */
 1255 
 1256         DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
 1257             msghdr->msg_ts));
 1258         if (msgsz > msghdr->msg_ts)
 1259                 msgsz = msghdr->msg_ts;
 1260         *mtype = msghdr->msg_type;
 1261 
 1262         /*
 1263          * Return the segments to the user
 1264          */
 1265 
 1266         next = msghdr->msg_spot;
 1267         for (len = 0; len < msgsz; len += msginfo.msgssz) {
 1268                 size_t tlen;
 1269 
 1270                 if (msgsz - len > msginfo.msgssz)
 1271                         tlen = msginfo.msgssz;
 1272                 else
 1273                         tlen = msgsz - len;
 1274                 if (next <= -1)
 1275                         panic("next too low #3");
 1276                 if (next >= msginfo.msgseg)
 1277                         panic("next out of range #3");
 1278                 mtx_unlock(&msq_mtx);
 1279                 error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
 1280                 mtx_lock(&msq_mtx);
 1281                 if (error != 0) {
 1282                         DPRINTF(("error (%d) copying out message segment\n",
 1283                             error));
 1284                         msg_freehdr(msghdr);
 1285                         wakeup(msqkptr);
 1286                         goto done2;
 1287                 }
 1288                 msgp = (char *)msgp + tlen;
 1289                 next = msgmaps[next].next;
 1290         }
 1291 
 1292         /*
 1293          * Done, return the actual number of bytes copied out.
 1294          */
 1295 
 1296         msg_freehdr(msghdr);
 1297         wakeup(msqkptr);
 1298         td->td_retval[0] = msgsz;
 1299 done2:
 1300         mtx_unlock(&msq_mtx);
 1301         return (error);
 1302 }
 1303 
 1304 int
 1305 sys_msgrcv(td, uap)
 1306         struct thread *td;
 1307         register struct msgrcv_args *uap;
 1308 {
 1309         int error;
 1310         long mtype;
 1311 
 1312         DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
 1313             uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
 1314 
 1315         if ((error = kern_msgrcv(td, uap->msqid,
 1316             (char *)uap->msgp + sizeof(mtype), uap->msgsz,
 1317             uap->msgtyp, uap->msgflg, &mtype)) != 0)
 1318                 return (error);
 1319         if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
 1320                 DPRINTF(("error %d copying the message type\n", error));
 1321         return (error);
 1322 }
 1323 
 1324 static int
 1325 sysctl_msqids(SYSCTL_HANDLER_ARGS)
 1326 {
 1327 
 1328         return (SYSCTL_OUT(req, msqids,
 1329             sizeof(struct msqid_kernel) * msginfo.msgmni));
 1330 }
 1331 
 1332 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
 1333     "Maximum message size");
 1334 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
 1335     "Number of message queue identifiers");
 1336 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
 1337     "Maximum number of bytes in a queue");
 1338 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
 1339     "Maximum number of messages in the system");
 1340 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
 1341     "Size of a message segment");
 1342 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
 1343     "Number of message segments");
 1344 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLTYPE_OPAQUE | CTLFLAG_RD,
 1345     NULL, 0, sysctl_msqids, "", "Message queue IDs");
 1346 
 1347 #ifdef COMPAT_FREEBSD32
 1348 int
 1349 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
 1350 {
 1351 
 1352 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 1353     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 1354         switch (uap->which) {
 1355         case 0:
 1356                 return (freebsd7_freebsd32_msgctl(td,
 1357                     (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
 1358         case 2:
 1359                 return (freebsd32_msgsnd(td,
 1360                     (struct freebsd32_msgsnd_args *)&uap->a2));
 1361         case 3:
 1362                 return (freebsd32_msgrcv(td,
 1363                     (struct freebsd32_msgrcv_args *)&uap->a2));
 1364         default:
 1365                 return (sys_msgsys(td, (struct msgsys_args *)uap));
 1366         }
 1367 #else
 1368         return (nosys(td, NULL));
 1369 #endif
 1370 }
 1371 
 1372 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 1373     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 1374 int
 1375 freebsd7_freebsd32_msgctl(struct thread *td,
 1376     struct freebsd7_freebsd32_msgctl_args *uap)
 1377 {
 1378         struct msqid_ds msqbuf;
 1379         struct msqid_ds32_old msqbuf32;
 1380         int error;
 1381 
 1382         if (uap->cmd == IPC_SET) {
 1383                 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
 1384                 if (error)
 1385                         return (error);
 1386                 freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
 1387                 PTRIN_CP(msqbuf32, msqbuf, msg_first);
 1388                 PTRIN_CP(msqbuf32, msqbuf, msg_last);
 1389                 CP(msqbuf32, msqbuf, msg_cbytes);
 1390                 CP(msqbuf32, msqbuf, msg_qnum);
 1391                 CP(msqbuf32, msqbuf, msg_qbytes);
 1392                 CP(msqbuf32, msqbuf, msg_lspid);
 1393                 CP(msqbuf32, msqbuf, msg_lrpid);
 1394                 CP(msqbuf32, msqbuf, msg_stime);
 1395                 CP(msqbuf32, msqbuf, msg_rtime);
 1396                 CP(msqbuf32, msqbuf, msg_ctime);
 1397         }
 1398         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
 1399         if (error)
 1400                 return (error);
 1401         if (uap->cmd == IPC_STAT) {
 1402                 bzero(&msqbuf32, sizeof(msqbuf32));
 1403                 freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
 1404                 PTROUT_CP(msqbuf, msqbuf32, msg_first);
 1405                 PTROUT_CP(msqbuf, msqbuf32, msg_last);
 1406                 CP(msqbuf, msqbuf32, msg_cbytes);
 1407                 CP(msqbuf, msqbuf32, msg_qnum);
 1408                 CP(msqbuf, msqbuf32, msg_qbytes);
 1409                 CP(msqbuf, msqbuf32, msg_lspid);
 1410                 CP(msqbuf, msqbuf32, msg_lrpid);
 1411                 CP(msqbuf, msqbuf32, msg_stime);
 1412                 CP(msqbuf, msqbuf32, msg_rtime);
 1413                 CP(msqbuf, msqbuf32, msg_ctime);
 1414                 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
 1415         }
 1416         return (error);
 1417 }
 1418 #endif
 1419 
 1420 int
 1421 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
 1422 {
 1423         struct msqid_ds msqbuf;
 1424         struct msqid_ds32 msqbuf32;
 1425         int error;
 1426 
 1427         if (uap->cmd == IPC_SET) {
 1428                 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
 1429                 if (error)
 1430                         return (error);
 1431                 freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
 1432                 PTRIN_CP(msqbuf32, msqbuf, msg_first);
 1433                 PTRIN_CP(msqbuf32, msqbuf, msg_last);
 1434                 CP(msqbuf32, msqbuf, msg_cbytes);
 1435                 CP(msqbuf32, msqbuf, msg_qnum);
 1436                 CP(msqbuf32, msqbuf, msg_qbytes);
 1437                 CP(msqbuf32, msqbuf, msg_lspid);
 1438                 CP(msqbuf32, msqbuf, msg_lrpid);
 1439                 CP(msqbuf32, msqbuf, msg_stime);
 1440                 CP(msqbuf32, msqbuf, msg_rtime);
 1441                 CP(msqbuf32, msqbuf, msg_ctime);
 1442         }
 1443         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
 1444         if (error)
 1445                 return (error);
 1446         if (uap->cmd == IPC_STAT) {
 1447                 freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
 1448                 PTROUT_CP(msqbuf, msqbuf32, msg_first);
 1449                 PTROUT_CP(msqbuf, msqbuf32, msg_last);
 1450                 CP(msqbuf, msqbuf32, msg_cbytes);
 1451                 CP(msqbuf, msqbuf32, msg_qnum);
 1452                 CP(msqbuf, msqbuf32, msg_qbytes);
 1453                 CP(msqbuf, msqbuf32, msg_lspid);
 1454                 CP(msqbuf, msqbuf32, msg_lrpid);
 1455                 CP(msqbuf, msqbuf32, msg_stime);
 1456                 CP(msqbuf, msqbuf32, msg_rtime);
 1457                 CP(msqbuf, msqbuf32, msg_ctime);
 1458                 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
 1459         }
 1460         return (error);
 1461 }
 1462 
 1463 int
 1464 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
 1465 {
 1466         const void *msgp;
 1467         long mtype;
 1468         int32_t mtype32;
 1469         int error;
 1470 
 1471         msgp = PTRIN(uap->msgp);
 1472         if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
 1473                 return (error);
 1474         mtype = mtype32;
 1475         return (kern_msgsnd(td, uap->msqid,
 1476             (const char *)msgp + sizeof(mtype32),
 1477             uap->msgsz, uap->msgflg, mtype));
 1478 }
 1479 
 1480 int
 1481 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
 1482 {
 1483         void *msgp;
 1484         long mtype;
 1485         int32_t mtype32;
 1486         int error;
 1487 
 1488         msgp = PTRIN(uap->msgp);
 1489         if ((error = kern_msgrcv(td, uap->msqid,
 1490             (char *)msgp + sizeof(mtype32), uap->msgsz,
 1491             uap->msgtyp, uap->msgflg, &mtype)) != 0)
 1492                 return (error);
 1493         mtype32 = (int32_t)mtype;
 1494         return (copyout(&mtype32, msgp, sizeof(mtype32)));
 1495 }
 1496 #endif
 1497 
 1498 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 1499     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 1500 
 1501 /* XXX casting to (sy_call_t *) is bogus, as usual. */
 1502 static sy_call_t *msgcalls[] = {
 1503         (sy_call_t *)freebsd7_msgctl, (sy_call_t *)sys_msgget,
 1504         (sy_call_t *)sys_msgsnd, (sy_call_t *)sys_msgrcv
 1505 };
 1506 
 1507 /*
 1508  * Entry point for all MSG calls.
 1509  */
 1510 int
 1511 sys_msgsys(td, uap)
 1512         struct thread *td;
 1513         /* XXX actually varargs. */
 1514         struct msgsys_args /* {
 1515                 int     which;
 1516                 int     a2;
 1517                 int     a3;
 1518                 int     a4;
 1519                 int     a5;
 1520                 int     a6;
 1521         } */ *uap;
 1522 {
 1523         int error;
 1524 
 1525         if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
 1526                 return (ENOSYS);
 1527         if (uap->which < 0 ||
 1528             uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
 1529                 return (EINVAL);
 1530         error = (*msgcalls[uap->which])(td, &uap->a2);
 1531         return (error);
 1532 }
 1533 
 1534 #ifndef CP
 1535 #define CP(src, dst, fld)       do { (dst).fld = (src).fld; } while (0)
 1536 #endif
 1537 
 1538 #ifndef _SYS_SYSPROTO_H_
 1539 struct freebsd7_msgctl_args {
 1540         int     msqid;
 1541         int     cmd;
 1542         struct  msqid_ds_old *buf;
 1543 };
 1544 #endif
 1545 int
 1546 freebsd7_msgctl(td, uap)
 1547         struct thread *td;
 1548         struct freebsd7_msgctl_args *uap;
 1549 {
 1550         struct msqid_ds_old msqold;
 1551         struct msqid_ds msqbuf;
 1552         int error;
 1553 
 1554         DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
 1555             uap->buf));
 1556         if (uap->cmd == IPC_SET) {
 1557                 error = copyin(uap->buf, &msqold, sizeof(msqold));
 1558                 if (error)
 1559                         return (error);
 1560                 ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
 1561                 CP(msqold, msqbuf, msg_first);
 1562                 CP(msqold, msqbuf, msg_last);
 1563                 CP(msqold, msqbuf, msg_cbytes);
 1564                 CP(msqold, msqbuf, msg_qnum);
 1565                 CP(msqold, msqbuf, msg_qbytes);
 1566                 CP(msqold, msqbuf, msg_lspid);
 1567                 CP(msqold, msqbuf, msg_lrpid);
 1568                 CP(msqold, msqbuf, msg_stime);
 1569                 CP(msqold, msqbuf, msg_rtime);
 1570                 CP(msqold, msqbuf, msg_ctime);
 1571         }
 1572         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
 1573         if (error)
 1574                 return (error);
 1575         if (uap->cmd == IPC_STAT) {
 1576                 bzero(&msqold, sizeof(msqold));
 1577                 ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
 1578                 CP(msqbuf, msqold, msg_first);
 1579                 CP(msqbuf, msqold, msg_last);
 1580                 CP(msqbuf, msqold, msg_cbytes);
 1581                 CP(msqbuf, msqold, msg_qnum);
 1582                 CP(msqbuf, msqold, msg_qbytes);
 1583                 CP(msqbuf, msqold, msg_lspid);
 1584                 CP(msqbuf, msqold, msg_lrpid);
 1585                 CP(msqbuf, msqold, msg_stime);
 1586                 CP(msqbuf, msqold, msg_rtime);
 1587                 CP(msqbuf, msqold, msg_ctime);
 1588                 error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
 1589         }
 1590         return (error);
 1591 }
 1592 
 1593 #undef CP
 1594 
 1595 #endif  /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
 1596            COMPAT_FREEBSD7 */

Cache object: 93f64fbe71a712989c487ade5791b588


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.