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$");
   52 
   53 #include "opt_sysvipc.h"
   54 #include "opt_mac.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/syscall.h>
   67 #include <sys/syscallsubr.h>
   68 #include <sys/sysent.h>
   69 #include <sys/sysctl.h>
   70 #include <sys/malloc.h>
   71 #include <sys/jail.h>
   72 
   73 #include <security/mac/mac_framework.h>
   74 
   75 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
   76 
   77 static void msginit(void);
   78 static int msgunload(void);
   79 static int sysvmsg_modload(struct module *, int, void *);
   80 
   81 #ifdef MSG_DEBUG
   82 #define DPRINTF(a)      printf a
   83 #else
   84 #define DPRINTF(a)
   85 #endif
   86 
   87 static void msg_freehdr(struct msg *msghdr);
   88 
   89 #ifndef MSGSSZ
   90 #define MSGSSZ  8               /* Each segment must be 2^N long */
   91 #endif
   92 #ifndef MSGSEG
   93 #define MSGSEG  2048            /* must be less than 32767 */
   94 #endif
   95 #define MSGMAX  (MSGSSZ*MSGSEG)
   96 #ifndef MSGMNB
   97 #define MSGMNB  2048            /* max # of bytes in a queue */
   98 #endif
   99 #ifndef MSGMNI
  100 #define MSGMNI  40
  101 #endif
  102 #ifndef MSGTQL
  103 #define MSGTQL  40
  104 #endif
  105 
  106 /*
  107  * Based on the configuration parameters described in an SVR2 (yes, two)
  108  * config(1m) man page.
  109  *
  110  * Each message is broken up and stored in segments that are msgssz bytes
  111  * long.  For efficiency reasons, this should be a power of two.  Also,
  112  * it doesn't make sense if it is less than 8 or greater than about 256.
  113  * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
  114  * two between 8 and 1024 inclusive (and panic's if it isn't).
  115  */
  116 struct msginfo msginfo = {
  117                 MSGMAX,         /* max chars in a message */
  118                 MSGMNI,         /* # of message queue identifiers */
  119                 MSGMNB,         /* max chars in a queue */
  120                 MSGTQL,         /* max messages in system */
  121                 MSGSSZ,         /* size of a message segment */
  122                                 /* (must be small power of 2 greater than 4) */
  123                 MSGSEG          /* number of message segments */
  124 };
  125 
  126 /*
  127  * macros to convert between msqid_ds's and msqid's.
  128  * (specific to this implementation)
  129  */
  130 #define MSQID(ix,ds)    ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
  131 #define MSQID_IX(id)    ((id) & 0xffff)
  132 #define MSQID_SEQ(id)   (((id) >> 16) & 0xffff)
  133 
  134 /*
  135  * The rest of this file is specific to this particular implementation.
  136  */
  137 
  138 struct msgmap {
  139         short   next;           /* next segment in buffer */
  140                                 /* -1 -> available */
  141                                 /* 0..(MSGSEG-1) -> index of next segment */
  142 };
  143 
  144 #define MSG_LOCKED      01000   /* Is this msqid_ds locked? */
  145 
  146 static int nfree_msgmaps;       /* # of free map entries */
  147 static short free_msgmaps;      /* head of linked list of free map entries */
  148 static struct msg *free_msghdrs;/* list of free msg headers */
  149 static char *msgpool;           /* MSGMAX byte long msg buffer pool */
  150 static struct msgmap *msgmaps;  /* MSGSEG msgmap structures */
  151 static struct msg *msghdrs;     /* MSGTQL msg headers */
  152 static struct msqid_kernel *msqids;     /* MSGMNI msqid_kernel struct's */
  153 static struct mtx msq_mtx;      /* global mutex for message queues. */
  154 
  155 static void
  156 msginit()
  157 {
  158         register int i;
  159 
  160         TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
  161         TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
  162         msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
  163         TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
  164         TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
  165         TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
  166 
  167         msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
  168         if (msgpool == NULL)
  169                 panic("msgpool is NULL");
  170         msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
  171         if (msgmaps == NULL)
  172                 panic("msgmaps is NULL");
  173         msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
  174         if (msghdrs == NULL)
  175                 panic("msghdrs is NULL");
  176         msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
  177             M_WAITOK);
  178         if (msqids == NULL)
  179                 panic("msqids is NULL");
  180 
  181         /*
  182          * msginfo.msgssz should be a power of two for efficiency reasons.
  183          * It is also pretty silly if msginfo.msgssz is less than 8
  184          * or greater than about 256 so ...
  185          */
  186 
  187         i = 8;
  188         while (i < 1024 && i != msginfo.msgssz)
  189                 i <<= 1;
  190         if (i != msginfo.msgssz) {
  191                 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
  192                     msginfo.msgssz));
  193                 panic("msginfo.msgssz not a small power of 2");
  194         }
  195 
  196         if (msginfo.msgseg > 32767) {
  197                 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
  198                 panic("msginfo.msgseg > 32767");
  199         }
  200 
  201         if (msgmaps == NULL)
  202                 panic("msgmaps is NULL");
  203 
  204         for (i = 0; i < msginfo.msgseg; i++) {
  205                 if (i > 0)
  206                         msgmaps[i-1].next = i;
  207                 msgmaps[i].next = -1;   /* implies entry is available */
  208         }
  209         free_msgmaps = 0;
  210         nfree_msgmaps = msginfo.msgseg;
  211 
  212         if (msghdrs == NULL)
  213                 panic("msghdrs is NULL");
  214 
  215         for (i = 0; i < msginfo.msgtql; i++) {
  216                 msghdrs[i].msg_type = 0;
  217                 if (i > 0)
  218                         msghdrs[i-1].msg_next = &msghdrs[i];
  219                 msghdrs[i].msg_next = NULL;
  220 #ifdef MAC
  221                 mac_init_sysv_msgmsg(&msghdrs[i]);
  222 #endif
  223         }
  224         free_msghdrs = &msghdrs[0];
  225 
  226         if (msqids == NULL)
  227                 panic("msqids is NULL");
  228 
  229         for (i = 0; i < msginfo.msgmni; i++) {
  230                 msqids[i].u.msg_qbytes = 0;     /* implies entry is available */
  231                 msqids[i].u.msg_perm.seq = 0;   /* reset to a known value */
  232                 msqids[i].u.msg_perm.mode = 0;
  233 #ifdef MAC
  234                 mac_init_sysv_msgqueue(&msqids[i]);
  235 #endif
  236         }
  237         mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
  238 }
  239 
  240 static int
  241 msgunload()
  242 {
  243         struct msqid_kernel *msqkptr;
  244         int msqid;
  245 #ifdef MAC
  246         int i;
  247 #endif
  248 
  249         for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
  250                 /*
  251                  * Look for an unallocated and unlocked msqid_ds.
  252                  * msqid_ds's can be locked by msgsnd or msgrcv while
  253                  * they are copying the message in/out.  We can't
  254                  * re-use the entry until they release it.
  255                  */
  256                 msqkptr = &msqids[msqid];
  257                 if (msqkptr->u.msg_qbytes != 0 ||
  258                     (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
  259                         break;
  260         }
  261         if (msqid != msginfo.msgmni)
  262                 return (EBUSY);
  263 
  264 #ifdef MAC
  265         for (i = 0; i < msginfo.msgtql; i++)
  266                 mac_destroy_sysv_msgmsg(&msghdrs[i]);
  267         for (msqid = 0; msqid < msginfo.msgmni; msqid++)
  268                 mac_destroy_sysv_msgqueue(&msqids[msqid]);
  269 #endif
  270         free(msgpool, M_MSG);
  271         free(msgmaps, M_MSG);
  272         free(msghdrs, M_MSG);
  273         free(msqids, M_MSG);
  274         mtx_destroy(&msq_mtx);
  275         return (0);
  276 }
  277 
  278 
  279 static int
  280 sysvmsg_modload(struct module *module, int cmd, void *arg)
  281 {
  282         int error = 0;
  283 
  284         switch (cmd) {
  285         case MOD_LOAD:
  286                 msginit();
  287                 break;
  288         case MOD_UNLOAD:
  289                 error = msgunload();
  290                 break;
  291         case MOD_SHUTDOWN:
  292                 break;
  293         default:
  294                 error = EINVAL;
  295                 break;
  296         }
  297         return (error);
  298 }
  299 
  300 static moduledata_t sysvmsg_mod = {
  301         "sysvmsg",
  302         &sysvmsg_modload,
  303         NULL
  304 };
  305 
  306 SYSCALL_MODULE_HELPER(msgctl);
  307 SYSCALL_MODULE_HELPER(msgget);
  308 SYSCALL_MODULE_HELPER(msgsnd);
  309 SYSCALL_MODULE_HELPER(msgrcv);
  310 
  311 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
  312 MODULE_VERSION(sysvmsg, 1);
  313 
  314 static void
  315 msg_freehdr(msghdr)
  316         struct msg *msghdr;
  317 {
  318         while (msghdr->msg_ts > 0) {
  319                 short next;
  320                 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
  321                         panic("msghdr->msg_spot out of range");
  322                 next = msgmaps[msghdr->msg_spot].next;
  323                 msgmaps[msghdr->msg_spot].next = free_msgmaps;
  324                 free_msgmaps = msghdr->msg_spot;
  325                 nfree_msgmaps++;
  326                 msghdr->msg_spot = next;
  327                 if (msghdr->msg_ts >= msginfo.msgssz)
  328                         msghdr->msg_ts -= msginfo.msgssz;
  329                 else
  330                         msghdr->msg_ts = 0;
  331         }
  332         if (msghdr->msg_spot != -1)
  333                 panic("msghdr->msg_spot != -1");
  334         msghdr->msg_next = free_msghdrs;
  335         free_msghdrs = msghdr;
  336 #ifdef MAC
  337         mac_cleanup_sysv_msgmsg(msghdr);
  338 #endif
  339 }
  340 
  341 #ifndef _SYS_SYSPROTO_H_
  342 struct msgctl_args {
  343         int     msqid;
  344         int     cmd;
  345         struct  msqid_ds *buf;
  346 };
  347 #endif
  348 int
  349 msgctl(td, uap)
  350         struct thread *td;
  351         register struct msgctl_args *uap;
  352 {
  353         int msqid = uap->msqid;
  354         int cmd = uap->cmd;
  355         struct msqid_ds msqbuf;
  356         int error;
  357 
  358         DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
  359         if (cmd == IPC_SET &&
  360             (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
  361                 return (error);
  362         error = kern_msgctl(td, msqid, cmd, &msqbuf);
  363         if (cmd == IPC_STAT && error == 0)
  364                 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
  365         return (error);
  366 }
  367 
  368 int
  369 kern_msgctl(td, msqid, cmd, msqbuf)
  370         struct thread *td;
  371         int msqid;
  372         int cmd;
  373         struct msqid_ds *msqbuf;
  374 {
  375         int rval, error, msqix;
  376         register struct msqid_kernel *msqkptr;
  377 
  378         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  379                 return (ENOSYS);
  380 
  381         msqix = IPCID_TO_IX(msqid);
  382 
  383         if (msqix < 0 || msqix >= msginfo.msgmni) {
  384                 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
  385                     msginfo.msgmni));
  386                 return (EINVAL);
  387         }
  388 
  389         msqkptr = &msqids[msqix];
  390 
  391         mtx_lock(&msq_mtx);
  392         if (msqkptr->u.msg_qbytes == 0) {
  393                 DPRINTF(("no such msqid\n"));
  394                 error = EINVAL;
  395                 goto done2;
  396         }
  397         if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
  398                 DPRINTF(("wrong sequence number\n"));
  399                 error = EINVAL;
  400                 goto done2;
  401         }
  402 #ifdef MAC
  403         error = mac_check_sysv_msqctl(td->td_ucred, msqkptr, cmd);
  404         if (error != 0)
  405                 goto done2;
  406 #endif
  407 
  408         error = 0;
  409         rval = 0;
  410 
  411         switch (cmd) {
  412 
  413         case IPC_RMID:
  414         {
  415                 struct msg *msghdr;
  416                 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
  417                         goto done2;
  418 
  419 #ifdef MAC
  420                 /*
  421                  * Check that the thread has MAC access permissions to
  422                  * individual msghdrs.  Note: We need to do this in a
  423                  * separate loop because the actual loop alters the
  424                  * msq/msghdr info as it progresses, and there is no going
  425                  * back if half the way through we discover that the
  426                  * thread cannot free a certain msghdr.  The msq will get
  427                  * into an inconsistent state.
  428                  */
  429                 for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
  430                     msghdr = msghdr->msg_next) {
  431                         error = mac_check_sysv_msgrmid(td->td_ucred, msghdr);
  432                         if (error != 0)
  433                                 goto done2;
  434                 }
  435 #endif
  436 
  437                 /* Free the message headers */
  438                 msghdr = msqkptr->u.msg_first;
  439                 while (msghdr != NULL) {
  440                         struct msg *msghdr_tmp;
  441 
  442                         /* Free the segments of each message */
  443                         msqkptr->u.msg_cbytes -= msghdr->msg_ts;
  444                         msqkptr->u.msg_qnum--;
  445                         msghdr_tmp = msghdr;
  446                         msghdr = msghdr->msg_next;
  447                         msg_freehdr(msghdr_tmp);
  448                 }
  449 
  450                 if (msqkptr->u.msg_cbytes != 0)
  451                         panic("msg_cbytes is screwed up");
  452                 if (msqkptr->u.msg_qnum != 0)
  453                         panic("msg_qnum is screwed up");
  454 
  455                 msqkptr->u.msg_qbytes = 0;      /* Mark it as free */
  456 
  457 #ifdef MAC
  458                 mac_cleanup_sysv_msgqueue(msqkptr);
  459 #endif
  460 
  461                 wakeup(msqkptr);
  462         }
  463 
  464                 break;
  465 
  466         case IPC_SET:
  467                 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
  468                         goto done2;
  469                 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
  470                         error = priv_check(td, PRIV_IPC_MSGSIZE);
  471                         if (error)
  472                                 goto done2;
  473                 }
  474                 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
  475                         DPRINTF(("can't increase msg_qbytes beyond %d"
  476                             "(truncating)\n", msginfo.msgmnb));
  477                         msqbuf->msg_qbytes = msginfo.msgmnb;    /* silently restrict qbytes to system limit */
  478                 }
  479                 if (msqbuf->msg_qbytes == 0) {
  480                         DPRINTF(("can't reduce msg_qbytes to 0\n"));
  481                         error = EINVAL;         /* non-standard errno! */
  482                         goto done2;
  483                 }
  484                 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
  485                 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
  486                 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
  487                     (msqbuf->msg_perm.mode & 0777);
  488                 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
  489                 msqkptr->u.msg_ctime = time_second;
  490                 break;
  491 
  492         case IPC_STAT:
  493                 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
  494                         DPRINTF(("requester doesn't have read access\n"));
  495                         goto done2;
  496                 }
  497                 *msqbuf = msqkptr->u;
  498                 break;
  499 
  500         default:
  501                 DPRINTF(("invalid command %d\n", cmd));
  502                 error = EINVAL;
  503                 goto done2;
  504         }
  505 
  506         if (error == 0)
  507                 td->td_retval[0] = rval;
  508 done2:
  509         mtx_unlock(&msq_mtx);
  510         return (error);
  511 }
  512 
  513 #ifndef _SYS_SYSPROTO_H_
  514 struct msgget_args {
  515         key_t   key;
  516         int     msgflg;
  517 };
  518 #endif
  519 int
  520 msgget(td, uap)
  521         struct thread *td;
  522         register struct msgget_args *uap;
  523 {
  524         int msqid, error = 0;
  525         int key = uap->key;
  526         int msgflg = uap->msgflg;
  527         struct ucred *cred = td->td_ucred;
  528         register struct msqid_kernel *msqkptr = NULL;
  529 
  530         DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
  531 
  532         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  533                 return (ENOSYS);
  534 
  535         mtx_lock(&msq_mtx);
  536         if (key != IPC_PRIVATE) {
  537                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
  538                         msqkptr = &msqids[msqid];
  539                         if (msqkptr->u.msg_qbytes != 0 &&
  540                             msqkptr->u.msg_perm.key == key)
  541                                 break;
  542                 }
  543                 if (msqid < msginfo.msgmni) {
  544                         DPRINTF(("found public key\n"));
  545                         if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
  546                                 DPRINTF(("not exclusive\n"));
  547                                 error = EEXIST;
  548                                 goto done2;
  549                         }
  550                         if ((error = ipcperm(td, &msqkptr->u.msg_perm,
  551                             msgflg & 0700))) {
  552                                 DPRINTF(("requester doesn't have 0%o access\n",
  553                                     msgflg & 0700));
  554                                 goto done2;
  555                         }
  556 #ifdef MAC
  557                         error = mac_check_sysv_msqget(cred, msqkptr);
  558                         if (error != 0)
  559                                 goto done2;
  560 #endif
  561                         goto found;
  562                 }
  563         }
  564 
  565         DPRINTF(("need to allocate the msqid_ds\n"));
  566         if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
  567                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
  568                         /*
  569                          * Look for an unallocated and unlocked msqid_ds.
  570                          * msqid_ds's can be locked by msgsnd or msgrcv while
  571                          * they are copying the message in/out.  We can't
  572                          * re-use the entry until they release it.
  573                          */
  574                         msqkptr = &msqids[msqid];
  575                         if (msqkptr->u.msg_qbytes == 0 &&
  576                             (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
  577                                 break;
  578                 }
  579                 if (msqid == msginfo.msgmni) {
  580                         DPRINTF(("no more msqid_ds's available\n"));
  581                         error = ENOSPC;
  582                         goto done2;
  583                 }
  584                 DPRINTF(("msqid %d is available\n", msqid));
  585                 msqkptr->u.msg_perm.key = key;
  586                 msqkptr->u.msg_perm.cuid = cred->cr_uid;
  587                 msqkptr->u.msg_perm.uid = cred->cr_uid;
  588                 msqkptr->u.msg_perm.cgid = cred->cr_gid;
  589                 msqkptr->u.msg_perm.gid = cred->cr_gid;
  590                 msqkptr->u.msg_perm.mode = (msgflg & 0777);
  591                 /* Make sure that the returned msqid is unique */
  592                 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
  593                 msqkptr->u.msg_first = NULL;
  594                 msqkptr->u.msg_last = NULL;
  595                 msqkptr->u.msg_cbytes = 0;
  596                 msqkptr->u.msg_qnum = 0;
  597                 msqkptr->u.msg_qbytes = msginfo.msgmnb;
  598                 msqkptr->u.msg_lspid = 0;
  599                 msqkptr->u.msg_lrpid = 0;
  600                 msqkptr->u.msg_stime = 0;
  601                 msqkptr->u.msg_rtime = 0;
  602                 msqkptr->u.msg_ctime = time_second;
  603 #ifdef MAC
  604                 mac_create_sysv_msgqueue(cred, msqkptr);
  605 #endif
  606         } else {
  607                 DPRINTF(("didn't find it and wasn't asked to create it\n"));
  608                 error = ENOENT;
  609                 goto done2;
  610         }
  611 
  612 found:
  613         /* Construct the unique msqid */
  614         td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
  615 done2:
  616         mtx_unlock(&msq_mtx);
  617         return (error);
  618 }
  619 
  620 #ifndef _SYS_SYSPROTO_H_
  621 struct msgsnd_args {
  622         int     msqid;
  623         const void      *msgp;
  624         size_t  msgsz;
  625         int     msgflg;
  626 };
  627 #endif
  628 int
  629 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
  630         struct thread *td;
  631         int msqid;
  632         const void *msgp;       /* XXX msgp is actually mtext. */
  633         size_t msgsz;
  634         int msgflg;
  635         long mtype;
  636 {
  637         int msqix, segs_needed, error = 0;
  638         register struct msqid_kernel *msqkptr;
  639         register struct msg *msghdr;
  640         short next;
  641 
  642         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  643                 return (ENOSYS);
  644 
  645         mtx_lock(&msq_mtx);
  646         msqix = IPCID_TO_IX(msqid);
  647 
  648         if (msqix < 0 || msqix >= msginfo.msgmni) {
  649                 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
  650                     msginfo.msgmni));
  651                 error = EINVAL;
  652                 goto done2;
  653         }
  654 
  655         msqkptr = &msqids[msqix];
  656         if (msqkptr->u.msg_qbytes == 0) {
  657                 DPRINTF(("no such message queue id\n"));
  658                 error = EINVAL;
  659                 goto done2;
  660         }
  661         if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
  662                 DPRINTF(("wrong sequence number\n"));
  663                 error = EINVAL;
  664                 goto done2;
  665         }
  666 
  667         if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
  668                 DPRINTF(("requester doesn't have write access\n"));
  669                 goto done2;
  670         }
  671 
  672 #ifdef MAC
  673         error = mac_check_sysv_msqsnd(td->td_ucred, msqkptr);
  674         if (error != 0)
  675                 goto done2;
  676 #endif
  677 
  678         segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
  679         DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
  680             msginfo.msgssz, segs_needed));
  681         for (;;) {
  682                 int need_more_resources = 0;
  683 
  684                 /*
  685                  * check msgsz
  686                  * (inside this loop in case msg_qbytes changes while we sleep)
  687                  */
  688 
  689                 if (msgsz > msqkptr->u.msg_qbytes) {
  690                         DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
  691                         error = EINVAL;
  692                         goto done2;
  693                 }
  694 
  695                 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
  696                         DPRINTF(("msqid is locked\n"));
  697                         need_more_resources = 1;
  698                 }
  699                 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
  700                         DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
  701                         need_more_resources = 1;
  702                 }
  703                 if (segs_needed > nfree_msgmaps) {
  704                         DPRINTF(("segs_needed > nfree_msgmaps\n"));
  705                         need_more_resources = 1;
  706                 }
  707                 if (free_msghdrs == NULL) {
  708                         DPRINTF(("no more msghdrs\n"));
  709                         need_more_resources = 1;
  710                 }
  711 
  712                 if (need_more_resources) {
  713                         int we_own_it;
  714 
  715                         if ((msgflg & IPC_NOWAIT) != 0) {
  716                                 DPRINTF(("need more resources but caller "
  717                                     "doesn't want to wait\n"));
  718                                 error = EAGAIN;
  719                                 goto done2;
  720                         }
  721 
  722                         if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
  723                                 DPRINTF(("we don't own the msqid_ds\n"));
  724                                 we_own_it = 0;
  725                         } else {
  726                                 /* Force later arrivals to wait for our
  727                                    request */
  728                                 DPRINTF(("we own the msqid_ds\n"));
  729                                 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
  730                                 we_own_it = 1;
  731                         }
  732                         DPRINTF(("msgsnd:  goodnight\n"));
  733                         error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
  734                             "msgsnd", hz);
  735                         DPRINTF(("msgsnd:  good morning, error=%d\n", error));
  736                         if (we_own_it)
  737                                 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
  738                         if (error == EWOULDBLOCK) {
  739                                 DPRINTF(("msgsnd:  timed out\n"));
  740                                 continue;
  741                         }
  742                         if (error != 0) {
  743                                 DPRINTF(("msgsnd:  interrupted system call\n"));
  744                                 error = EINTR;
  745                                 goto done2;
  746                         }
  747 
  748                         /*
  749                          * Make sure that the msq queue still exists
  750                          */
  751 
  752                         if (msqkptr->u.msg_qbytes == 0) {
  753                                 DPRINTF(("msqid deleted\n"));
  754                                 error = EIDRM;
  755                                 goto done2;
  756                         }
  757 
  758                 } else {
  759                         DPRINTF(("got all the resources that we need\n"));
  760                         break;
  761                 }
  762         }
  763 
  764         /*
  765          * We have the resources that we need.
  766          * Make sure!
  767          */
  768 
  769         if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
  770                 panic("msg_perm.mode & MSG_LOCKED");
  771         if (segs_needed > nfree_msgmaps)
  772                 panic("segs_needed > nfree_msgmaps");
  773         if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
  774                 panic("msgsz + msg_cbytes > msg_qbytes");
  775         if (free_msghdrs == NULL)
  776                 panic("no more msghdrs");
  777 
  778         /*
  779          * Re-lock the msqid_ds in case we page-fault when copying in the
  780          * message
  781          */
  782 
  783         if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
  784                 panic("msqid_ds is already locked");
  785         msqkptr->u.msg_perm.mode |= MSG_LOCKED;
  786 
  787         /*
  788          * Allocate a message header
  789          */
  790 
  791         msghdr = free_msghdrs;
  792         free_msghdrs = msghdr->msg_next;
  793         msghdr->msg_spot = -1;
  794         msghdr->msg_ts = msgsz;
  795         msghdr->msg_type = mtype;
  796 #ifdef MAC
  797         /*
  798          * XXXMAC: Should the mac_check_sysv_msgmsq check follow here
  799          * immediately?  Or, should it be checked just before the msg is
  800          * enqueued in the msgq (as it is done now)?
  801          */
  802         mac_create_sysv_msgmsg(td->td_ucred, msqkptr, msghdr);
  803 #endif
  804 
  805         /*
  806          * Allocate space for the message
  807          */
  808 
  809         while (segs_needed > 0) {
  810                 if (nfree_msgmaps <= 0)
  811                         panic("not enough msgmaps");
  812                 if (free_msgmaps == -1)
  813                         panic("nil free_msgmaps");
  814                 next = free_msgmaps;
  815                 if (next <= -1)
  816                         panic("next too low #1");
  817                 if (next >= msginfo.msgseg)
  818                         panic("next out of range #1");
  819                 DPRINTF(("allocating segment %d to message\n", next));
  820                 free_msgmaps = msgmaps[next].next;
  821                 nfree_msgmaps--;
  822                 msgmaps[next].next = msghdr->msg_spot;
  823                 msghdr->msg_spot = next;
  824                 segs_needed--;
  825         }
  826 
  827         /*
  828          * Validate the message type
  829          */
  830 
  831         if (msghdr->msg_type < 1) {
  832                 msg_freehdr(msghdr);
  833                 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
  834                 wakeup(msqkptr);
  835                 DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
  836                 error = EINVAL;
  837                 goto done2;
  838         }
  839 
  840         /*
  841          * Copy in the message body
  842          */
  843 
  844         next = msghdr->msg_spot;
  845         while (msgsz > 0) {
  846                 size_t tlen;
  847                 if (msgsz > msginfo.msgssz)
  848                         tlen = msginfo.msgssz;
  849                 else
  850                         tlen = msgsz;
  851                 if (next <= -1)
  852                         panic("next too low #2");
  853                 if (next >= msginfo.msgseg)
  854                         panic("next out of range #2");
  855                 mtx_unlock(&msq_mtx);
  856                 if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
  857                     tlen)) != 0) {
  858                         mtx_lock(&msq_mtx);
  859                         DPRINTF(("error %d copying in message segment\n",
  860                             error));
  861                         msg_freehdr(msghdr);
  862                         msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
  863                         wakeup(msqkptr);
  864                         goto done2;
  865                 }
  866                 mtx_lock(&msq_mtx);
  867                 msgsz -= tlen;
  868                 msgp = (const char *)msgp + tlen;
  869                 next = msgmaps[next].next;
  870         }
  871         if (next != -1)
  872                 panic("didn't use all the msg segments");
  873 
  874         /*
  875          * We've got the message.  Unlock the msqid_ds.
  876          */
  877 
  878         msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
  879 
  880         /*
  881          * Make sure that the msqid_ds is still allocated.
  882          */
  883 
  884         if (msqkptr->u.msg_qbytes == 0) {
  885                 msg_freehdr(msghdr);
  886                 wakeup(msqkptr);
  887                 error = EIDRM;
  888                 goto done2;
  889         }
  890 
  891 #ifdef MAC
  892         /*
  893          * Note: Since the task/thread allocates the msghdr and usually
  894          * primes it with its own MAC label, for a majority of policies, it
  895          * won't be necessary to check whether the msghdr has access
  896          * permissions to the msgq.  The mac_check_sysv_msqsnd check would
  897          * suffice in that case.  However, this hook may be required where
  898          * individual policies derive a non-identical label for the msghdr
  899          * from the current thread label and may want to check the msghdr
  900          * enqueue permissions, along with read/write permissions to the
  901          * msgq.
  902          */
  903         error = mac_check_sysv_msgmsq(td->td_ucred, msghdr, msqkptr);
  904         if (error != 0) {
  905                 msg_freehdr(msghdr);
  906                 wakeup(msqkptr);
  907                 goto done2;
  908         }
  909 #endif
  910 
  911         /*
  912          * Put the message into the queue
  913          */
  914         if (msqkptr->u.msg_first == NULL) {
  915                 msqkptr->u.msg_first = msghdr;
  916                 msqkptr->u.msg_last = msghdr;
  917         } else {
  918                 msqkptr->u.msg_last->msg_next = msghdr;
  919                 msqkptr->u.msg_last = msghdr;
  920         }
  921         msqkptr->u.msg_last->msg_next = NULL;
  922 
  923         msqkptr->u.msg_cbytes += msghdr->msg_ts;
  924         msqkptr->u.msg_qnum++;
  925         msqkptr->u.msg_lspid = td->td_proc->p_pid;
  926         msqkptr->u.msg_stime = time_second;
  927 
  928         wakeup(msqkptr);
  929         td->td_retval[0] = 0;
  930 done2:
  931         mtx_unlock(&msq_mtx);
  932         return (error);
  933 }
  934 
  935 int
  936 msgsnd(td, uap)
  937         struct thread *td;
  938         register struct msgsnd_args *uap;
  939 {
  940         int error;
  941         long mtype;
  942 
  943         DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
  944             uap->msgsz, uap->msgflg));
  945 
  946         if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
  947                 DPRINTF(("error %d copying the message type\n", error));
  948                 return (error);
  949         }
  950         return (kern_msgsnd(td, uap->msqid,
  951             (const char *)uap->msgp + sizeof(mtype),
  952             uap->msgsz, uap->msgflg, mtype));
  953 }
  954 
  955 #ifndef _SYS_SYSPROTO_H_
  956 struct msgrcv_args {
  957         int     msqid;
  958         void    *msgp;
  959         size_t  msgsz;
  960         long    msgtyp;
  961         int     msgflg;
  962 };
  963 #endif
  964 int
  965 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
  966         struct thread *td;
  967         int msqid;
  968         void *msgp;     /* XXX msgp is actually mtext. */
  969         size_t msgsz;
  970         long msgtyp;
  971         int msgflg;
  972         long *mtype;
  973 {
  974         size_t len;
  975         register struct msqid_kernel *msqkptr;
  976         register struct msg *msghdr;
  977         int msqix, error = 0;
  978         short next;
  979 
  980         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  981                 return (ENOSYS);
  982 
  983         msqix = IPCID_TO_IX(msqid);
  984 
  985         if (msqix < 0 || msqix >= msginfo.msgmni) {
  986                 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
  987                     msginfo.msgmni));
  988                 return (EINVAL);
  989         }
  990 
  991         msqkptr = &msqids[msqix];
  992         mtx_lock(&msq_mtx);
  993         if (msqkptr->u.msg_qbytes == 0) {
  994                 DPRINTF(("no such message queue id\n"));
  995                 error = EINVAL;
  996                 goto done2;
  997         }
  998         if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
  999                 DPRINTF(("wrong sequence number\n"));
 1000                 error = EINVAL;
 1001                 goto done2;
 1002         }
 1003 
 1004         if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
 1005                 DPRINTF(("requester doesn't have read access\n"));
 1006                 goto done2;
 1007         }
 1008 
 1009 #ifdef MAC
 1010         error = mac_check_sysv_msqrcv(td->td_ucred, msqkptr);
 1011         if (error != 0)
 1012                 goto done2;
 1013 #endif
 1014 
 1015         msghdr = NULL;
 1016         while (msghdr == NULL) {
 1017                 if (msgtyp == 0) {
 1018                         msghdr = msqkptr->u.msg_first;
 1019                         if (msghdr != NULL) {
 1020                                 if (msgsz < msghdr->msg_ts &&
 1021                                     (msgflg & MSG_NOERROR) == 0) {
 1022                                         DPRINTF(("first message on the queue "
 1023                                             "is too big (want %zu, got %d)\n",
 1024                                             msgsz, msghdr->msg_ts));
 1025                                         error = E2BIG;
 1026                                         goto done2;
 1027                                 }
 1028 #ifdef MAC
 1029                                 error = mac_check_sysv_msgrcv(td->td_ucred,
 1030                                     msghdr);
 1031                                 if (error != 0)
 1032                                         goto done2;
 1033 #endif
 1034                                 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
 1035                                         msqkptr->u.msg_first = NULL;
 1036                                         msqkptr->u.msg_last = NULL;
 1037                                 } else {
 1038                                         msqkptr->u.msg_first = msghdr->msg_next;
 1039                                         if (msqkptr->u.msg_first == NULL)
 1040                                                 panic("msg_first/last screwed up #1");
 1041                                 }
 1042                         }
 1043                 } else {
 1044                         struct msg *previous;
 1045                         struct msg **prev;
 1046 
 1047                         previous = NULL;
 1048                         prev = &(msqkptr->u.msg_first);
 1049                         while ((msghdr = *prev) != NULL) {
 1050                                 /*
 1051                                  * Is this message's type an exact match or is
 1052                                  * this message's type less than or equal to
 1053                                  * the absolute value of a negative msgtyp?
 1054                                  * Note that the second half of this test can
 1055                                  * NEVER be true if msgtyp is positive since
 1056                                  * msg_type is always positive!
 1057                                  */
 1058 
 1059                                 if (msgtyp == msghdr->msg_type ||
 1060                                     msghdr->msg_type <= -msgtyp) {
 1061                                         DPRINTF(("found message type %ld, "
 1062                                             "requested %ld\n",
 1063                                             msghdr->msg_type, msgtyp));
 1064                                         if (msgsz < msghdr->msg_ts &&
 1065                                             (msgflg & MSG_NOERROR) == 0) {
 1066                                                 DPRINTF(("requested message "
 1067                                                     "on the queue is too big "
 1068                                                     "(want %zu, got %hu)\n",
 1069                                                     msgsz, msghdr->msg_ts));
 1070                                                 error = E2BIG;
 1071                                                 goto done2;
 1072                                         }
 1073 #ifdef MAC
 1074                                         error = mac_check_sysv_msgrcv(
 1075                                             td->td_ucred, msghdr);
 1076                                         if (error != 0)
 1077                                                 goto done2;
 1078 #endif
 1079                                         *prev = msghdr->msg_next;
 1080                                         if (msghdr == msqkptr->u.msg_last) {
 1081                                                 if (previous == NULL) {
 1082                                                         if (prev !=
 1083                                                             &msqkptr->u.msg_first)
 1084                                                                 panic("msg_first/last screwed up #2");
 1085                                                         msqkptr->u.msg_first =
 1086                                                             NULL;
 1087                                                         msqkptr->u.msg_last =
 1088                                                             NULL;
 1089                                                 } else {
 1090                                                         if (prev ==
 1091                                                             &msqkptr->u.msg_first)
 1092                                                                 panic("msg_first/last screwed up #3");
 1093                                                         msqkptr->u.msg_last =
 1094                                                             previous;
 1095                                                 }
 1096                                         }
 1097                                         break;
 1098                                 }
 1099                                 previous = msghdr;
 1100                                 prev = &(msghdr->msg_next);
 1101                         }
 1102                 }
 1103 
 1104                 /*
 1105                  * We've either extracted the msghdr for the appropriate
 1106                  * message or there isn't one.
 1107                  * If there is one then bail out of this loop.
 1108                  */
 1109 
 1110                 if (msghdr != NULL)
 1111                         break;
 1112 
 1113                 /*
 1114                  * Hmph!  No message found.  Does the user want to wait?
 1115                  */
 1116 
 1117                 if ((msgflg & IPC_NOWAIT) != 0) {
 1118                         DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
 1119                             msgtyp));
 1120                         /* The SVID says to return ENOMSG. */
 1121                         error = ENOMSG;
 1122                         goto done2;
 1123                 }
 1124 
 1125                 /*
 1126                  * Wait for something to happen
 1127                  */
 1128 
 1129                 DPRINTF(("msgrcv:  goodnight\n"));
 1130                 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
 1131                     "msgrcv", 0);
 1132                 DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
 1133 
 1134                 if (error != 0) {
 1135                         DPRINTF(("msgrcv:  interrupted system call\n"));
 1136                         error = EINTR;
 1137                         goto done2;
 1138                 }
 1139 
 1140                 /*
 1141                  * Make sure that the msq queue still exists
 1142                  */
 1143 
 1144                 if (msqkptr->u.msg_qbytes == 0 ||
 1145                     msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
 1146                         DPRINTF(("msqid deleted\n"));
 1147                         error = EIDRM;
 1148                         goto done2;
 1149                 }
 1150         }
 1151 
 1152         /*
 1153          * Return the message to the user.
 1154          *
 1155          * First, do the bookkeeping (before we risk being interrupted).
 1156          */
 1157 
 1158         msqkptr->u.msg_cbytes -= msghdr->msg_ts;
 1159         msqkptr->u.msg_qnum--;
 1160         msqkptr->u.msg_lrpid = td->td_proc->p_pid;
 1161         msqkptr->u.msg_rtime = time_second;
 1162 
 1163         /*
 1164          * Make msgsz the actual amount that we'll be returning.
 1165          * Note that this effectively truncates the message if it is too long
 1166          * (since msgsz is never increased).
 1167          */
 1168 
 1169         DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
 1170             msghdr->msg_ts));
 1171         if (msgsz > msghdr->msg_ts)
 1172                 msgsz = msghdr->msg_ts;
 1173         *mtype = msghdr->msg_type;
 1174 
 1175         /*
 1176          * Return the segments to the user
 1177          */
 1178 
 1179         next = msghdr->msg_spot;
 1180         for (len = 0; len < msgsz; len += msginfo.msgssz) {
 1181                 size_t tlen;
 1182 
 1183                 if (msgsz - len > msginfo.msgssz)
 1184                         tlen = msginfo.msgssz;
 1185                 else
 1186                         tlen = msgsz - len;
 1187                 if (next <= -1)
 1188                         panic("next too low #3");
 1189                 if (next >= msginfo.msgseg)
 1190                         panic("next out of range #3");
 1191                 mtx_unlock(&msq_mtx);
 1192                 error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
 1193                 mtx_lock(&msq_mtx);
 1194                 if (error != 0) {
 1195                         DPRINTF(("error (%d) copying out message segment\n",
 1196                             error));
 1197                         msg_freehdr(msghdr);
 1198                         wakeup(msqkptr);
 1199                         goto done2;
 1200                 }
 1201                 msgp = (char *)msgp + tlen;
 1202                 next = msgmaps[next].next;
 1203         }
 1204 
 1205         /*
 1206          * Done, return the actual number of bytes copied out.
 1207          */
 1208 
 1209         msg_freehdr(msghdr);
 1210         wakeup(msqkptr);
 1211         td->td_retval[0] = msgsz;
 1212 done2:
 1213         mtx_unlock(&msq_mtx);
 1214         return (error);
 1215 }
 1216 
 1217 int
 1218 msgrcv(td, uap)
 1219         struct thread *td;
 1220         register struct msgrcv_args *uap;
 1221 {
 1222         int error;
 1223         long mtype;
 1224 
 1225         DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
 1226             uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
 1227 
 1228         if ((error = kern_msgrcv(td, uap->msqid,
 1229             (char *)uap->msgp + sizeof(mtype), uap->msgsz,
 1230             uap->msgtyp, uap->msgflg, &mtype)) != 0)
 1231                 return (error);
 1232         if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
 1233                 DPRINTF(("error %d copying the message type\n", error));
 1234         return (error);
 1235 }
 1236 
 1237 static int
 1238 sysctl_msqids(SYSCTL_HANDLER_ARGS)
 1239 {
 1240 
 1241         return (SYSCTL_OUT(req, msqids,
 1242             sizeof(struct msqid_kernel) * msginfo.msgmni));
 1243 }
 1244 
 1245 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
 1246     "Maximum message size");
 1247 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
 1248     "Number of message queue identifiers");
 1249 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
 1250     "Maximum number of bytes in a queue");
 1251 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
 1252     "Maximum number of messages in the system");
 1253 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
 1254     "Size of a message segment");
 1255 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
 1256     "Number of message segments");
 1257 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
 1258     NULL, 0, sysctl_msqids, "", "Message queue IDs");
 1259 
 1260 SYSCALL_MODULE_HELPER(msgsys);
 1261 SYSCALL_MODULE_HELPER(freebsd7_msgctl);
 1262 
 1263 /* XXX casting to (sy_call_t *) is bogus, as usual. */
 1264 static sy_call_t *msgcalls[] = {
 1265         (sy_call_t *)freebsd7_msgctl, (sy_call_t *)msgget,
 1266         (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
 1267 };
 1268 
 1269 /*
 1270  * Entry point for all MSG calls.
 1271  */
 1272 int
 1273 msgsys(td, uap)
 1274         struct thread *td;
 1275         /* XXX actually varargs. */
 1276         struct msgsys_args /* {
 1277                 int     which;
 1278                 int     a2;
 1279                 int     a3;
 1280                 int     a4;
 1281                 int     a5;
 1282                 int     a6;
 1283         } */ *uap;
 1284 {
 1285         int error;
 1286 
 1287         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
 1288                 return (ENOSYS);
 1289         if (uap->which < 0 ||
 1290             uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
 1291                 return (EINVAL);
 1292         error = (*msgcalls[uap->which])(td, &uap->a2);
 1293         return (error);
 1294 }
 1295 
 1296 #define CP(src, dst, fld)       do { (dst).fld = (src).fld; } while (0)
 1297 
 1298 #ifndef _SYS_SYSPROTO_H_
 1299 struct freebsd7_msgctl_args {
 1300         int     msqid;
 1301         int     cmd;
 1302         struct  msqid_ds_old *buf;
 1303 };
 1304 #endif
 1305 int
 1306 freebsd7_msgctl(td, uap)
 1307         struct thread *td;
 1308         struct freebsd7_msgctl_args *uap;
 1309 {
 1310         struct msqid_ds_old msqold;
 1311         struct msqid_ds msqbuf;
 1312         int error;
 1313 
 1314         DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
 1315             uap->buf));
 1316         if (uap->cmd == IPC_SET) {
 1317                 error = copyin(uap->buf, &msqold, sizeof(msqold));
 1318                 if (error)
 1319                         return (error);
 1320                 ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
 1321                 CP(msqold, msqbuf, msg_first);
 1322                 CP(msqold, msqbuf, msg_last);
 1323                 CP(msqold, msqbuf, msg_cbytes);
 1324                 CP(msqold, msqbuf, msg_qnum);
 1325                 CP(msqold, msqbuf, msg_qbytes);
 1326                 CP(msqold, msqbuf, msg_lspid);
 1327                 CP(msqold, msqbuf, msg_lrpid);
 1328                 CP(msqold, msqbuf, msg_stime);
 1329                 CP(msqold, msqbuf, msg_rtime);
 1330                 CP(msqold, msqbuf, msg_ctime);
 1331         }
 1332         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
 1333         if (error)
 1334                 return (error);
 1335         if (uap->cmd == IPC_STAT) {
 1336                 bzero(&msqold, sizeof(msqold));
 1337                 ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
 1338                 CP(msqbuf, msqold, msg_first);
 1339                 CP(msqbuf, msqold, msg_last);
 1340                 CP(msqbuf, msqold, msg_cbytes);
 1341                 CP(msqbuf, msqold, msg_qnum);
 1342                 CP(msqbuf, msqold, msg_qbytes);
 1343                 CP(msqbuf, msqold, msg_lspid);
 1344                 CP(msqbuf, msqold, msg_lrpid);
 1345                 CP(msqbuf, msqold, msg_stime);
 1346                 CP(msqbuf, msqold, msg_rtime);
 1347                 CP(msqbuf, msqold, msg_ctime);
 1348                 error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
 1349         }
 1350         return (error);
 1351 }
 1352 
 1353 /* ABI compat shim for older kernel modules. */
 1354 #undef kern_msgctl
 1355 
 1356 int     kern_msgctl(struct thread *td, int msqid, int cmd,
 1357             struct msqid_ds_old *buf);
 1358 
 1359 int
 1360 kern_msgctl(td, msqid, cmd, msqold)
 1361         struct thread *td;
 1362         int msqid;
 1363         int cmd;
 1364         struct msqid_ds_old *msqold;
 1365 {
 1366         struct msqid_ds msqbuf;
 1367         int error;
 1368 
 1369         if (cmd == IPC_SET) {
 1370                 ipcperm_old2new(&msqold->msg_perm, &msqbuf.msg_perm);
 1371                 CP(*msqold, msqbuf, msg_first);
 1372                 CP(*msqold, msqbuf, msg_last);
 1373                 CP(*msqold, msqbuf, msg_cbytes);
 1374                 CP(*msqold, msqbuf, msg_qnum);
 1375                 CP(*msqold, msqbuf, msg_qbytes);
 1376                 CP(*msqold, msqbuf, msg_lspid);
 1377                 CP(*msqold, msqbuf, msg_lrpid);
 1378                 CP(*msqold, msqbuf, msg_stime);
 1379                 CP(*msqold, msqbuf, msg_rtime);
 1380                 CP(*msqold, msqbuf, msg_ctime);
 1381         }
 1382         error = kern_new_msgctl(td, msqid, cmd, &msqbuf);
 1383         if (error)
 1384                 return (error);
 1385         if (cmd == IPC_STAT) {
 1386                 bzero(msqold, sizeof(*msqold));
 1387                 ipcperm_new2old(&msqbuf.msg_perm, &msqold->msg_perm);
 1388                 CP(msqbuf, *msqold, msg_first);
 1389                 CP(msqbuf, *msqold, msg_last);
 1390                 CP(msqbuf, *msqold, msg_cbytes);
 1391                 CP(msqbuf, *msqold, msg_qnum);
 1392                 CP(msqbuf, *msqold, msg_qbytes);
 1393                 CP(msqbuf, *msqold, msg_lspid);
 1394                 CP(msqbuf, *msqold, msg_lrpid);
 1395                 CP(msqbuf, *msqold, msg_stime);
 1396                 CP(msqbuf, *msqold, msg_rtime);
 1397                 CP(msqbuf, *msqold, msg_ctime);
 1398         }
 1399         return (error);
 1400 }
 1401 
 1402 #undef CP

Cache object: 0e0f3a8e39c645e09f5d549237e052a7


[ 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.