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

Cache object: de4b23c5d3c0c4c0a3c1421069b7ad60


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