Home | History | Annotate | Line # | Download | only in kern
sysv_msg.c revision 1.56.2.4
      1  1.56.2.4     skrll /*	$NetBSD: sysv_msg.c,v 1.56.2.4 2008/10/10 22:34:14 skrll Exp $	*/
      2      1.26   thorpej 
      3      1.26   thorpej /*-
      4      1.48        ad  * Copyright (c) 1999, 2006, 2007 The NetBSD Foundation, Inc.
      5      1.26   thorpej  * All rights reserved.
      6      1.26   thorpej  *
      7      1.26   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.26   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9      1.48        ad  * NASA Ames Research Center, and by Andrew Doran.
     10      1.26   thorpej  *
     11      1.26   thorpej  * Redistribution and use in source and binary forms, with or without
     12      1.26   thorpej  * modification, are permitted provided that the following conditions
     13      1.26   thorpej  * are met:
     14      1.26   thorpej  * 1. Redistributions of source code must retain the above copyright
     15      1.26   thorpej  *    notice, this list of conditions and the following disclaimer.
     16      1.26   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.26   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18      1.26   thorpej  *    documentation and/or other materials provided with the distribution.
     19      1.26   thorpej  *
     20      1.26   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21      1.26   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22      1.26   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23      1.26   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24      1.26   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25      1.26   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26      1.26   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27      1.26   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28      1.26   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29      1.26   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30      1.26   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31      1.26   thorpej  */
     32       1.9       cgd 
     33       1.1       cgd /*
     34       1.1       cgd  * Implementation of SVID messages
     35       1.1       cgd  *
     36      1.26   thorpej  * Author: Daniel Boulet
     37       1.1       cgd  *
     38       1.1       cgd  * Copyright 1993 Daniel Boulet and RTMX Inc.
     39       1.1       cgd  *
     40       1.1       cgd  * This system call was implemented by Daniel Boulet under contract from RTMX.
     41       1.1       cgd  *
     42       1.1       cgd  * Redistribution and use in source forms, with and without modification,
     43       1.1       cgd  * are permitted provided that this entire comment appears intact.
     44       1.1       cgd  *
     45       1.1       cgd  * Redistribution in binary form may occur without any restrictions.
     46       1.1       cgd  * Obviously, it would be nice if you gave credit where credit is due
     47       1.1       cgd  * but requiring it would be too onerous.
     48       1.1       cgd  *
     49       1.1       cgd  * This software is provided ``AS IS'' without any warranties of any kind.
     50       1.1       cgd  */
     51      1.33     lukem 
     52      1.33     lukem #include <sys/cdefs.h>
     53  1.56.2.4     skrll __KERNEL_RCSID(0, "$NetBSD: sysv_msg.c,v 1.56.2.4 2008/10/10 22:34:14 skrll Exp $");
     54      1.23      tron 
     55      1.24      tron #define SYSVMSG
     56       1.1       cgd 
     57       1.2   mycroft #include <sys/param.h>
     58       1.2   mycroft #include <sys/kernel.h>
     59       1.2   mycroft #include <sys/msg.h>
     60      1.29    simonb #include <sys/sysctl.h>
     61      1.29    simonb #include <sys/mount.h>		/* XXX for <sys/syscallargs.h> */
     62      1.10       cgd #include <sys/syscallargs.h>
     63      1.42      elad #include <sys/kauth.h>
     64      1.18  christos 
     65       1.1       cgd #define MSG_DEBUG
     66       1.1       cgd #undef MSG_DEBUG_OK
     67       1.1       cgd 
     68      1.20  christos #ifdef MSG_DEBUG_OK
     69      1.21  christos #define MSG_PRINTF(a)	printf a
     70      1.20  christos #else
     71      1.20  christos #define MSG_PRINTF(a)
     72      1.20  christos #endif
     73      1.20  christos 
     74      1.36  jdolecek static int	nfree_msgmaps;		/* # of free map entries */
     75      1.36  jdolecek static short	free_msgmaps;	/* head of linked list of free map entries */
     76      1.36  jdolecek static struct	__msg *free_msghdrs;	/* list of free msg headers */
     77      1.36  jdolecek static char	*msgpool;		/* MSGMAX byte long msg buffer pool */
     78      1.36  jdolecek static struct	msgmap *msgmaps;	/* MSGSEG msgmap structures */
     79      1.36  jdolecek static struct __msg *msghdrs;		/* MSGTQL msg headers */
     80      1.48        ad 
     81      1.48        ad kmsq_t	*msqs;				/* MSGMNI msqid_ds struct's */
     82      1.48        ad kmutex_t msgmutex;			/* subsystem lock */
     83       1.1       cgd 
     84      1.51     rmind static u_int	msg_waiters = 0;	/* total number of msgrcv waiters */
     85      1.51     rmind static bool	msg_realloc_state;
     86      1.51     rmind static kcondvar_t msg_realloc_cv;
     87      1.51     rmind 
     88      1.37  junyoung static void msg_freehdr(struct __msg *);
     89      1.18  christos 
     90      1.18  christos void
     91      1.40   thorpej msginit(void)
     92       1.1       cgd {
     93      1.36  jdolecek 	int i, sz;
     94      1.36  jdolecek 	vaddr_t v;
     95       1.1       cgd 
     96       1.3   mycroft 	/*
     97       1.3   mycroft 	 * msginfo.msgssz should be a power of two for efficiency reasons.
     98       1.3   mycroft 	 * It is also pretty silly if msginfo.msgssz is less than 8
     99       1.3   mycroft 	 * or greater than about 256 so ...
    100       1.3   mycroft 	 */
    101       1.1       cgd 
    102       1.3   mycroft 	i = 8;
    103       1.3   mycroft 	while (i < 1024 && i != msginfo.msgssz)
    104       1.3   mycroft 		i <<= 1;
    105      1.50     rmind 	if (i != msginfo.msgssz) {
    106      1.50     rmind 		panic("msginfo.msgssz = %d, not a small power of 2",
    107      1.50     rmind 		    msginfo.msgssz);
    108       1.3   mycroft 	}
    109       1.3   mycroft 
    110       1.3   mycroft 	if (msginfo.msgseg > 32767) {
    111      1.50     rmind 		panic("msginfo.msgseg = %d > 32767", msginfo.msgseg);
    112       1.3   mycroft 	}
    113       1.3   mycroft 
    114      1.51     rmind 	/* Allocate the wired memory for our structures */
    115      1.51     rmind 	sz = ALIGN(msginfo.msgmax) +
    116      1.51     rmind 	    ALIGN(msginfo.msgseg * sizeof(struct msgmap)) +
    117      1.51     rmind 	    ALIGN(msginfo.msgtql * sizeof(struct __msg)) +
    118      1.51     rmind 	    ALIGN(msginfo.msgmni * sizeof(kmsq_t));
    119      1.39      yamt 	v = uvm_km_alloc(kernel_map, round_page(sz), 0,
    120      1.39      yamt 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    121      1.39      yamt 	if (v == 0)
    122      1.36  jdolecek 		panic("sysv_msg: cannot allocate memory");
    123      1.36  jdolecek 	msgpool = (void *)v;
    124  1.56.2.4     skrll 	msgmaps = (void *)((uintptr_t)msgpool + ALIGN(msginfo.msgmax));
    125  1.56.2.4     skrll 	msghdrs = (void *)((uintptr_t)msgmaps +
    126  1.56.2.4     skrll 	    ALIGN(msginfo.msgseg * sizeof(struct msgmap)));
    127  1.56.2.4     skrll 	msqs = (void *)((uintptr_t)msghdrs +
    128  1.56.2.4     skrll 	    ALIGN(msginfo.msgtql * sizeof(struct __msg)));
    129      1.50     rmind 
    130      1.50     rmind 	for (i = 0; i < (msginfo.msgseg - 1); i++)
    131      1.50     rmind 		msgmaps[i].next = i + 1;
    132      1.50     rmind 	msgmaps[msginfo.msgseg - 1].next = -1;
    133      1.50     rmind 
    134       1.3   mycroft 	free_msgmaps = 0;
    135       1.3   mycroft 	nfree_msgmaps = msginfo.msgseg;
    136       1.3   mycroft 
    137      1.50     rmind 	for (i = 0; i < (msginfo.msgtql - 1); i++) {
    138       1.3   mycroft 		msghdrs[i].msg_type = 0;
    139      1.50     rmind 		msghdrs[i].msg_next = &msghdrs[i + 1];
    140      1.50     rmind 	}
    141      1.50     rmind 	i = msginfo.msgtql - 1;
    142      1.50     rmind 	msghdrs[i].msg_type = 0;
    143      1.50     rmind 	msghdrs[i].msg_next = NULL;
    144       1.3   mycroft 	free_msghdrs = &msghdrs[0];
    145       1.3   mycroft 
    146       1.4   mycroft 	for (i = 0; i < msginfo.msgmni; i++) {
    147      1.48        ad 		cv_init(&msqs[i].msq_cv, "msgwait");
    148      1.50     rmind 		/* Implies entry is available */
    149      1.50     rmind 		msqs[i].msq_u.msg_qbytes = 0;
    150      1.50     rmind 		/* Reset to a known value */
    151      1.50     rmind 		msqs[i].msq_u.msg_perm._seq = 0;
    152       1.3   mycroft 	}
    153      1.48        ad 
    154      1.48        ad 	mutex_init(&msgmutex, MUTEX_DEFAULT, IPL_NONE);
    155      1.51     rmind 	cv_init(&msg_realloc_cv, "msgrealc");
    156      1.51     rmind 	msg_realloc_state = false;
    157      1.51     rmind }
    158      1.51     rmind 
    159      1.51     rmind static int
    160      1.51     rmind msgrealloc(int newmsgmni, int newmsgseg)
    161      1.51     rmind {
    162      1.51     rmind 	struct msgmap *new_msgmaps;
    163      1.51     rmind 	struct __msg *new_msghdrs, *new_free_msghdrs;
    164      1.51     rmind 	char *old_msgpool, *new_msgpool;
    165      1.51     rmind 	kmsq_t *new_msqs;
    166      1.51     rmind 	vaddr_t v;
    167      1.51     rmind 	int i, sz, msqid, newmsgmax, new_nfree_msgmaps;
    168      1.51     rmind 	short new_free_msgmaps;
    169      1.51     rmind 
    170      1.51     rmind 	if (newmsgmni < 1 || newmsgseg < 1)
    171      1.51     rmind 		return EINVAL;
    172      1.51     rmind 
    173      1.51     rmind 	/* Allocate the wired memory for our structures */
    174      1.51     rmind 	newmsgmax = msginfo.msgssz * newmsgseg;
    175      1.51     rmind 	sz = ALIGN(newmsgmax) +
    176      1.51     rmind 	    ALIGN(newmsgseg * sizeof(struct msgmap)) +
    177      1.51     rmind 	    ALIGN(msginfo.msgtql * sizeof(struct __msg)) +
    178      1.51     rmind 	    ALIGN(newmsgmni * sizeof(kmsq_t));
    179      1.51     rmind 	v = uvm_km_alloc(kernel_map, round_page(sz), 0,
    180      1.51     rmind 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    181      1.51     rmind 	if (v == 0)
    182      1.51     rmind 		return ENOMEM;
    183      1.51     rmind 
    184      1.51     rmind 	mutex_enter(&msgmutex);
    185      1.51     rmind 	if (msg_realloc_state) {
    186      1.51     rmind 		mutex_exit(&msgmutex);
    187      1.51     rmind 		uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
    188      1.51     rmind 		return EBUSY;
    189      1.51     rmind 	}
    190      1.51     rmind 	msg_realloc_state = true;
    191      1.51     rmind 	if (msg_waiters) {
    192      1.51     rmind 		/*
    193      1.51     rmind 		 * Mark reallocation state, wake-up all waiters,
    194      1.51     rmind 		 * and wait while they will all exit.
    195      1.51     rmind 		 */
    196      1.51     rmind 		for (i = 0; i < msginfo.msgmni; i++)
    197      1.51     rmind 			cv_broadcast(&msqs[i].msq_cv);
    198      1.51     rmind 		while (msg_waiters)
    199      1.51     rmind 			cv_wait(&msg_realloc_cv, &msgmutex);
    200      1.51     rmind 	}
    201      1.51     rmind 	old_msgpool = msgpool;
    202      1.51     rmind 
    203      1.51     rmind 	/* We cannot reallocate less memory than we use */
    204      1.51     rmind 	i = 0;
    205      1.51     rmind 	for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
    206      1.51     rmind 		struct msqid_ds *mptr;
    207      1.51     rmind 		kmsq_t *msq;
    208      1.51     rmind 
    209      1.51     rmind 		msq = &msqs[msqid];
    210      1.51     rmind 		mptr = &msq->msq_u;
    211      1.51     rmind 		if (mptr->msg_qbytes || (mptr->msg_perm.mode & MSG_LOCKED))
    212      1.51     rmind 			i = msqid;
    213      1.51     rmind 	}
    214      1.51     rmind 	if (i >= newmsgmni || (msginfo.msgseg - nfree_msgmaps) > newmsgseg) {
    215      1.51     rmind 		mutex_exit(&msgmutex);
    216      1.51     rmind 		uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
    217      1.51     rmind 		return EBUSY;
    218      1.51     rmind 	}
    219      1.51     rmind 
    220      1.51     rmind 	new_msgpool = (void *)v;
    221  1.56.2.4     skrll 	new_msgmaps = (void *)((uintptr_t)new_msgpool + ALIGN(newmsgmax));
    222  1.56.2.4     skrll 	new_msghdrs = (void *)((uintptr_t)new_msgmaps +
    223  1.56.2.4     skrll 	    ALIGN(newmsgseg * sizeof(struct msgmap)));
    224  1.56.2.4     skrll 	new_msqs = (void *)((uintptr_t)new_msghdrs +
    225  1.56.2.4     skrll 	    ALIGN(msginfo.msgtql * sizeof(struct __msg)));
    226      1.51     rmind 
    227      1.51     rmind 	/* Initialize the structures */
    228      1.51     rmind 	for (i = 0; i < (newmsgseg - 1); i++)
    229      1.51     rmind 		new_msgmaps[i].next = i + 1;
    230      1.51     rmind 	new_msgmaps[newmsgseg - 1].next = -1;
    231      1.51     rmind 	new_free_msgmaps = 0;
    232      1.51     rmind 	new_nfree_msgmaps = newmsgseg;
    233      1.51     rmind 
    234      1.51     rmind 	for (i = 0; i < (msginfo.msgtql - 1); i++) {
    235      1.51     rmind 		new_msghdrs[i].msg_type = 0;
    236      1.51     rmind 		new_msghdrs[i].msg_next = &new_msghdrs[i + 1];
    237      1.51     rmind 	}
    238      1.51     rmind 	i = msginfo.msgtql - 1;
    239      1.51     rmind 	new_msghdrs[i].msg_type = 0;
    240      1.51     rmind 	new_msghdrs[i].msg_next = NULL;
    241      1.51     rmind 	new_free_msghdrs = &new_msghdrs[0];
    242      1.51     rmind 
    243      1.51     rmind 	for (i = 0; i < newmsgmni; i++) {
    244      1.51     rmind 		new_msqs[i].msq_u.msg_qbytes = 0;
    245      1.51     rmind 		new_msqs[i].msq_u.msg_perm._seq = 0;
    246      1.51     rmind 		cv_init(&new_msqs[i].msq_cv, "msgwait");
    247      1.51     rmind 	}
    248      1.51     rmind 
    249      1.51     rmind 	/*
    250      1.51     rmind 	 * Copy all message queue identifiers, mesage headers and buffer
    251      1.51     rmind 	 * pools to the new memory location.
    252      1.51     rmind 	 */
    253      1.51     rmind 	for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
    254      1.51     rmind 		struct __msg *nmsghdr, *msghdr, *pmsghdr;
    255      1.51     rmind 		struct msqid_ds *nmptr, *mptr;
    256      1.51     rmind 		kmsq_t *nmsq, *msq;
    257      1.51     rmind 
    258      1.51     rmind 		msq = &msqs[msqid];
    259      1.51     rmind 		mptr = &msq->msq_u;
    260      1.51     rmind 
    261      1.51     rmind 		if (mptr->msg_qbytes == 0 &&
    262      1.51     rmind 		    (mptr->msg_perm.mode & MSG_LOCKED) == 0)
    263      1.51     rmind 			continue;
    264      1.51     rmind 
    265      1.51     rmind 		nmsq = &new_msqs[msqid];
    266      1.51     rmind 		nmptr = &nmsq->msq_u;
    267      1.51     rmind 		memcpy(nmptr, mptr, sizeof(struct msqid_ds));
    268      1.51     rmind 
    269      1.51     rmind 		/*
    270      1.51     rmind 		 * Go through the message headers, and and copy each
    271      1.51     rmind 		 * one by taking the new ones, and thus defragmenting.
    272      1.51     rmind 		 */
    273      1.51     rmind 		nmsghdr = pmsghdr = NULL;
    274      1.51     rmind 		msghdr = mptr->_msg_first;
    275      1.51     rmind 		while (msghdr) {
    276      1.51     rmind 			short nnext = 0, next;
    277      1.51     rmind 			u_short msgsz, segcnt;
    278      1.51     rmind 
    279      1.51     rmind 			/* Take an entry from the new list of free msghdrs */
    280      1.51     rmind 			nmsghdr = new_free_msghdrs;
    281      1.51     rmind 			KASSERT(nmsghdr != NULL);
    282      1.51     rmind 			new_free_msghdrs = nmsghdr->msg_next;
    283      1.51     rmind 
    284      1.51     rmind 			nmsghdr->msg_next = NULL;
    285      1.51     rmind 			if (pmsghdr) {
    286      1.51     rmind 				pmsghdr->msg_next = nmsghdr;
    287      1.51     rmind 			} else {
    288      1.51     rmind 				nmptr->_msg_first = nmsghdr;
    289      1.51     rmind 				pmsghdr = nmsghdr;
    290      1.51     rmind 			}
    291      1.51     rmind 			nmsghdr->msg_ts = msghdr->msg_ts;
    292      1.51     rmind 			nmsghdr->msg_spot = -1;
    293      1.51     rmind 
    294      1.51     rmind 			/* Compute the amount of segments and reserve them */
    295      1.51     rmind 			msgsz = msghdr->msg_ts;
    296      1.51     rmind 			segcnt = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
    297      1.51     rmind 			if (segcnt == 0)
    298      1.51     rmind 				continue;
    299      1.51     rmind 			while (segcnt--) {
    300      1.51     rmind 				nnext = new_free_msgmaps;
    301      1.51     rmind 				new_free_msgmaps = new_msgmaps[nnext].next;
    302      1.51     rmind 				new_nfree_msgmaps--;
    303      1.51     rmind 				new_msgmaps[nnext].next = nmsghdr->msg_spot;
    304      1.51     rmind 				nmsghdr->msg_spot = nnext;
    305      1.51     rmind 			}
    306      1.51     rmind 
    307      1.51     rmind 			/* Copy all segments */
    308      1.51     rmind 			KASSERT(nnext == nmsghdr->msg_spot);
    309      1.51     rmind 			next = msghdr->msg_spot;
    310      1.51     rmind 			while (msgsz > 0) {
    311      1.51     rmind 				size_t tlen;
    312      1.51     rmind 
    313      1.51     rmind 				if (msgsz >= msginfo.msgssz) {
    314      1.51     rmind 					tlen = msginfo.msgssz;
    315      1.51     rmind 					msgsz -= msginfo.msgssz;
    316      1.51     rmind 				} else {
    317      1.51     rmind 					tlen = msgsz;
    318      1.51     rmind 					msgsz = 0;
    319      1.51     rmind 				}
    320      1.51     rmind 
    321      1.51     rmind 				/* Copy the message buffer */
    322      1.51     rmind 				memcpy(&new_msgpool[nnext * msginfo.msgssz],
    323      1.51     rmind 				    &msgpool[next * msginfo.msgssz], tlen);
    324      1.51     rmind 
    325      1.51     rmind 				/* Next entry of the map */
    326      1.51     rmind 				nnext = msgmaps[nnext].next;
    327      1.51     rmind 				next = msgmaps[next].next;
    328      1.51     rmind 			}
    329      1.51     rmind 
    330      1.51     rmind 			/* Next message header */
    331      1.51     rmind 			msghdr = msghdr->msg_next;
    332      1.51     rmind 		}
    333      1.51     rmind 		nmptr->_msg_last = nmsghdr;
    334      1.51     rmind 	}
    335      1.51     rmind 	KASSERT((msginfo.msgseg - nfree_msgmaps) ==
    336      1.51     rmind 	    (newmsgseg - new_nfree_msgmaps));
    337      1.51     rmind 
    338      1.51     rmind 	sz = ALIGN(msginfo.msgmax) +
    339      1.51     rmind 	    ALIGN(msginfo.msgseg * sizeof(struct msgmap)) +
    340      1.51     rmind 	    ALIGN(msginfo.msgtql * sizeof(struct __msg)) +
    341      1.51     rmind 	    ALIGN(msginfo.msgmni * sizeof(kmsq_t));
    342      1.51     rmind 
    343      1.51     rmind 	for (i = 0; i < msginfo.msgmni; i++)
    344      1.51     rmind 		cv_destroy(&msqs[i].msq_cv);
    345      1.51     rmind 
    346      1.51     rmind 	/* Set the pointers and update the new values */
    347      1.51     rmind 	msgpool = new_msgpool;
    348      1.51     rmind 	msgmaps = new_msgmaps;
    349      1.51     rmind 	msghdrs = new_msghdrs;
    350      1.51     rmind 	msqs = new_msqs;
    351      1.51     rmind 
    352      1.51     rmind 	free_msghdrs = new_free_msghdrs;
    353      1.51     rmind 	free_msgmaps = new_free_msgmaps;
    354      1.51     rmind 	nfree_msgmaps = new_nfree_msgmaps;
    355      1.51     rmind 	msginfo.msgmni = newmsgmni;
    356      1.51     rmind 	msginfo.msgseg = newmsgseg;
    357      1.51     rmind 	msginfo.msgmax = newmsgmax;
    358      1.51     rmind 
    359      1.51     rmind 	/* Reallocation completed - notify all waiters, if any */
    360      1.51     rmind 	msg_realloc_state = false;
    361      1.51     rmind 	cv_broadcast(&msg_realloc_cv);
    362      1.51     rmind 	mutex_exit(&msgmutex);
    363      1.51     rmind 
    364      1.51     rmind 	uvm_km_free(kernel_map, (vaddr_t)old_msgpool, sz, UVM_KMF_WIRED);
    365      1.51     rmind 	return 0;
    366       1.1       cgd }
    367       1.1       cgd 
    368       1.3   mycroft static void
    369      1.40   thorpej msg_freehdr(struct __msg *msghdr)
    370       1.1       cgd {
    371      1.48        ad 
    372      1.48        ad 	KASSERT(mutex_owned(&msgmutex));
    373      1.48        ad 
    374       1.3   mycroft 	while (msghdr->msg_ts > 0) {
    375       1.3   mycroft 		short next;
    376      1.50     rmind 		KASSERT(msghdr->msg_spot >= 0);
    377      1.50     rmind 		KASSERT(msghdr->msg_spot < msginfo.msgseg);
    378      1.50     rmind 
    379       1.3   mycroft 		next = msgmaps[msghdr->msg_spot].next;
    380       1.3   mycroft 		msgmaps[msghdr->msg_spot].next = free_msgmaps;
    381       1.3   mycroft 		free_msgmaps = msghdr->msg_spot;
    382       1.5   mycroft 		nfree_msgmaps++;
    383       1.3   mycroft 		msghdr->msg_spot = next;
    384       1.3   mycroft 		if (msghdr->msg_ts >= msginfo.msgssz)
    385       1.3   mycroft 			msghdr->msg_ts -= msginfo.msgssz;
    386       1.3   mycroft 		else
    387       1.3   mycroft 			msghdr->msg_ts = 0;
    388       1.3   mycroft 	}
    389      1.50     rmind 	KASSERT(msghdr->msg_spot == -1);
    390       1.3   mycroft 	msghdr->msg_next = free_msghdrs;
    391       1.3   mycroft 	free_msghdrs = msghdr;
    392       1.1       cgd }
    393       1.1       cgd 
    394       1.1       cgd int
    395      1.54       dsl sys___msgctl13(struct lwp *l, const struct sys___msgctl13_args *uap, register_t *retval)
    396      1.16   thorpej {
    397      1.54       dsl 	/* {
    398      1.10       cgd 		syscallarg(int) msqid;
    399      1.10       cgd 		syscallarg(int) cmd;
    400      1.10       cgd 		syscallarg(struct msqid_ds *) buf;
    401      1.54       dsl 	} */
    402      1.26   thorpej 	struct msqid_ds msqbuf;
    403      1.26   thorpej 	int cmd, error;
    404      1.26   thorpej 
    405      1.26   thorpej 	cmd = SCARG(uap, cmd);
    406      1.26   thorpej 
    407      1.26   thorpej 	if (cmd == IPC_SET) {
    408      1.26   thorpej 		error = copyin(SCARG(uap, buf), &msqbuf, sizeof(msqbuf));
    409      1.26   thorpej 		if (error)
    410      1.26   thorpej 			return (error);
    411      1.26   thorpej 	}
    412      1.26   thorpej 
    413      1.44        ad 	error = msgctl1(l, SCARG(uap, msqid), cmd,
    414      1.26   thorpej 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &msqbuf : NULL);
    415      1.26   thorpej 
    416      1.26   thorpej 	if (error == 0 && cmd == IPC_STAT)
    417      1.26   thorpej 		error = copyout(&msqbuf, SCARG(uap, buf), sizeof(msqbuf));
    418      1.26   thorpej 
    419      1.26   thorpej 	return (error);
    420      1.26   thorpej }
    421      1.26   thorpej 
    422      1.26   thorpej int
    423      1.44        ad msgctl1(struct lwp *l, int msqid, int cmd, struct msqid_ds *msqbuf)
    424      1.26   thorpej {
    425      1.44        ad 	kauth_cred_t cred = l->l_cred;
    426      1.26   thorpej 	struct msqid_ds *msqptr;
    427      1.48        ad 	kmsq_t *msq;
    428      1.26   thorpej 	int error = 0, ix;
    429       1.1       cgd 
    430      1.26   thorpej 	MSG_PRINTF(("call to msgctl1(%d, %d)\n", msqid, cmd));
    431       1.1       cgd 
    432      1.26   thorpej 	ix = IPCID_TO_IX(msqid);
    433       1.1       cgd 
    434      1.48        ad 	mutex_enter(&msgmutex);
    435      1.48        ad 
    436      1.26   thorpej 	if (ix < 0 || ix >= msginfo.msgmni) {
    437      1.26   thorpej 		MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", ix,
    438      1.20  christos 		    msginfo.msgmni));
    439      1.48        ad 		error = EINVAL;
    440      1.48        ad 		goto unlock;
    441       1.3   mycroft 	}
    442       1.1       cgd 
    443      1.48        ad 	msq = &msqs[ix];
    444      1.48        ad 	msqptr = &msq->msq_u;
    445       1.1       cgd 
    446       1.3   mycroft 	if (msqptr->msg_qbytes == 0) {
    447      1.20  christos 		MSG_PRINTF(("no such msqid\n"));
    448      1.48        ad 		error = EINVAL;
    449      1.48        ad 		goto unlock;
    450       1.3   mycroft 	}
    451      1.26   thorpej 	if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqid)) {
    452      1.20  christos 		MSG_PRINTF(("wrong sequence number\n"));
    453      1.48        ad 		error = EINVAL;
    454      1.48        ad 		goto unlock;
    455       1.3   mycroft 	}
    456       1.1       cgd 
    457       1.3   mycroft 	switch (cmd) {
    458       1.3   mycroft 	case IPC_RMID:
    459       1.1       cgd 	{
    460      1.26   thorpej 		struct __msg *msghdr;
    461      1.26   thorpej 		if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)) != 0)
    462      1.48        ad 			break;
    463       1.3   mycroft 		/* Free the message headers */
    464      1.26   thorpej 		msghdr = msqptr->_msg_first;
    465       1.3   mycroft 		while (msghdr != NULL) {
    466      1.26   thorpej 			struct __msg *msghdr_tmp;
    467       1.3   mycroft 
    468       1.3   mycroft 			/* Free the segments of each message */
    469      1.26   thorpej 			msqptr->_msg_cbytes -= msghdr->msg_ts;
    470       1.5   mycroft 			msqptr->msg_qnum--;
    471       1.3   mycroft 			msghdr_tmp = msghdr;
    472       1.3   mycroft 			msghdr = msghdr->msg_next;
    473       1.3   mycroft 			msg_freehdr(msghdr_tmp);
    474       1.3   mycroft 		}
    475      1.50     rmind 		KASSERT(msqptr->_msg_cbytes == 0);
    476      1.50     rmind 		KASSERT(msqptr->msg_qnum == 0);
    477       1.1       cgd 
    478      1.50     rmind 		/* Mark it as free */
    479      1.50     rmind 		msqptr->msg_qbytes = 0;
    480      1.48        ad 		cv_broadcast(&msq->msq_cv);
    481       1.1       cgd 	}
    482       1.3   mycroft 		break;
    483       1.1       cgd 
    484       1.3   mycroft 	case IPC_SET:
    485      1.26   thorpej 		if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
    486      1.48        ad 			break;
    487      1.44        ad 		if (msqbuf->msg_qbytes > msqptr->msg_qbytes &&
    488      1.47      elad 		    kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    489      1.48        ad 		    NULL) != 0) {
    490      1.48        ad 			error = EPERM;
    491      1.48        ad 			break;
    492      1.48        ad 		}
    493      1.26   thorpej 		if (msqbuf->msg_qbytes > msginfo.msgmnb) {
    494      1.26   thorpej 			MSG_PRINTF(("can't increase msg_qbytes beyond %d "
    495      1.26   thorpej 			    "(truncating)\n", msginfo.msgmnb));
    496      1.26   thorpej 			/* silently restrict qbytes to system limit */
    497      1.26   thorpej 			msqbuf->msg_qbytes = msginfo.msgmnb;
    498       1.3   mycroft 		}
    499      1.26   thorpej 		if (msqbuf->msg_qbytes == 0) {
    500      1.20  christos 			MSG_PRINTF(("can't reduce msg_qbytes to 0\n"));
    501      1.48        ad 			error = EINVAL;		/* XXX non-standard errno! */
    502      1.48        ad 			break;
    503       1.3   mycroft 		}
    504      1.26   thorpej 		msqptr->msg_perm.uid = msqbuf->msg_perm.uid;
    505      1.26   thorpej 		msqptr->msg_perm.gid = msqbuf->msg_perm.gid;
    506       1.3   mycroft 		msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
    507      1.26   thorpej 		    (msqbuf->msg_perm.mode & 0777);
    508      1.26   thorpej 		msqptr->msg_qbytes = msqbuf->msg_qbytes;
    509      1.43    kardel 		msqptr->msg_ctime = time_second;
    510       1.3   mycroft 		break;
    511       1.1       cgd 
    512       1.3   mycroft 	case IPC_STAT:
    513      1.26   thorpej 		if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
    514      1.20  christos 			MSG_PRINTF(("requester doesn't have read access\n"));
    515      1.49        ad 			break;
    516       1.3   mycroft 		}
    517      1.26   thorpej 		memcpy(msqbuf, msqptr, sizeof(struct msqid_ds));
    518       1.3   mycroft 		break;
    519       1.1       cgd 
    520       1.3   mycroft 	default:
    521      1.20  christos 		MSG_PRINTF(("invalid command %d\n", cmd));
    522      1.48        ad 		error = EINVAL;
    523      1.48        ad 		break;
    524       1.3   mycroft 	}
    525       1.3   mycroft 
    526      1.50     rmind unlock:
    527      1.48        ad 	mutex_exit(&msgmutex);
    528      1.26   thorpej 	return (error);
    529       1.1       cgd }
    530       1.1       cgd 
    531       1.1       cgd int
    532      1.54       dsl sys_msgget(struct lwp *l, const struct sys_msgget_args *uap, register_t *retval)
    533      1.16   thorpej {
    534      1.54       dsl 	/* {
    535      1.10       cgd 		syscallarg(key_t) key;
    536      1.10       cgd 		syscallarg(int) msgflg;
    537      1.54       dsl 	} */
    538      1.48        ad 	int msqid, error = 0;
    539      1.10       cgd 	int key = SCARG(uap, key);
    540      1.10       cgd 	int msgflg = SCARG(uap, msgflg);
    541      1.44        ad 	kauth_cred_t cred = l->l_cred;
    542      1.26   thorpej 	struct msqid_ds *msqptr = NULL;
    543      1.48        ad 	kmsq_t *msq;
    544      1.48        ad 
    545      1.48        ad 	mutex_enter(&msgmutex);
    546       1.1       cgd 
    547      1.20  christos 	MSG_PRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
    548       1.1       cgd 
    549       1.5   mycroft 	if (key != IPC_PRIVATE) {
    550       1.5   mycroft 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
    551      1.48        ad 			msq = &msqs[msqid];
    552      1.48        ad 			msqptr = &msq->msq_u;
    553       1.3   mycroft 			if (msqptr->msg_qbytes != 0 &&
    554      1.26   thorpej 			    msqptr->msg_perm._key == key)
    555       1.3   mycroft 				break;
    556       1.3   mycroft 		}
    557       1.3   mycroft 		if (msqid < msginfo.msgmni) {
    558      1.20  christos 			MSG_PRINTF(("found public key\n"));
    559       1.3   mycroft 			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
    560      1.20  christos 				MSG_PRINTF(("not exclusive\n"));
    561      1.48        ad 				error = EEXIST;
    562      1.48        ad 				goto unlock;
    563       1.3   mycroft 			}
    564      1.26   thorpej 			if ((error = ipcperm(cred, &msqptr->msg_perm,
    565      1.26   thorpej 			    msgflg & 0700 ))) {
    566      1.20  christos 				MSG_PRINTF(("requester doesn't have 0%o access\n",
    567      1.20  christos 				    msgflg & 0700));
    568      1.48        ad 				goto unlock;
    569       1.3   mycroft 			}
    570       1.5   mycroft 			goto found;
    571       1.3   mycroft 		}
    572       1.1       cgd 	}
    573       1.1       cgd 
    574      1.20  christos 	MSG_PRINTF(("need to allocate the msqid_ds\n"));
    575       1.5   mycroft 	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
    576       1.5   mycroft 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
    577       1.5   mycroft 			/*
    578       1.5   mycroft 			 * Look for an unallocated and unlocked msqid_ds.
    579       1.5   mycroft 			 * msqid_ds's can be locked by msgsnd or msgrcv while
    580       1.5   mycroft 			 * they are copying the message in/out.  We can't
    581       1.5   mycroft 			 * re-use the entry until they release it.
    582       1.5   mycroft 			 */
    583      1.48        ad 			msq = &msqs[msqid];
    584      1.48        ad 			msqptr = &msq->msq_u;
    585       1.5   mycroft 			if (msqptr->msg_qbytes == 0 &&
    586       1.5   mycroft 			    (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
    587       1.5   mycroft 				break;
    588       1.5   mycroft 		}
    589       1.5   mycroft 		if (msqid == msginfo.msgmni) {
    590      1.20  christos 			MSG_PRINTF(("no more msqid_ds's available\n"));
    591      1.48        ad 			error = ENOSPC;
    592      1.48        ad 			goto unlock;
    593       1.5   mycroft 		}
    594      1.20  christos 		MSG_PRINTF(("msqid %d is available\n", msqid));
    595      1.26   thorpej 		msqptr->msg_perm._key = key;
    596      1.42      elad 		msqptr->msg_perm.cuid = kauth_cred_geteuid(cred);
    597      1.42      elad 		msqptr->msg_perm.uid = kauth_cred_geteuid(cred);
    598      1.42      elad 		msqptr->msg_perm.cgid = kauth_cred_getegid(cred);
    599      1.42      elad 		msqptr->msg_perm.gid = kauth_cred_getegid(cred);
    600       1.5   mycroft 		msqptr->msg_perm.mode = (msgflg & 0777);
    601       1.5   mycroft 		/* Make sure that the returned msqid is unique */
    602      1.26   thorpej 		msqptr->msg_perm._seq++;
    603      1.26   thorpej 		msqptr->_msg_first = NULL;
    604      1.26   thorpej 		msqptr->_msg_last = NULL;
    605      1.26   thorpej 		msqptr->_msg_cbytes = 0;
    606       1.5   mycroft 		msqptr->msg_qnum = 0;
    607       1.5   mycroft 		msqptr->msg_qbytes = msginfo.msgmnb;
    608       1.5   mycroft 		msqptr->msg_lspid = 0;
    609       1.5   mycroft 		msqptr->msg_lrpid = 0;
    610       1.5   mycroft 		msqptr->msg_stime = 0;
    611       1.5   mycroft 		msqptr->msg_rtime = 0;
    612      1.43    kardel 		msqptr->msg_ctime = time_second;
    613       1.5   mycroft 	} else {
    614      1.20  christos 		MSG_PRINTF(("didn't find it and wasn't asked to create it\n"));
    615      1.48        ad 		error = ENOENT;
    616      1.48        ad 		goto unlock;
    617       1.1       cgd 	}
    618       1.1       cgd 
    619      1.50     rmind found:
    620       1.3   mycroft 	/* Construct the unique msqid */
    621       1.3   mycroft 	*retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
    622      1.48        ad 
    623      1.50     rmind unlock:
    624      1.50     rmind 	mutex_exit(&msgmutex);
    625      1.48        ad 	return (error);
    626       1.1       cgd }
    627       1.1       cgd 
    628       1.1       cgd int
    629      1.54       dsl sys_msgsnd(struct lwp *l, const struct sys_msgsnd_args *uap, register_t *retval)
    630      1.16   thorpej {
    631      1.54       dsl 	/* {
    632      1.10       cgd 		syscallarg(int) msqid;
    633      1.22    kleink 		syscallarg(const void *) msgp;
    634      1.10       cgd 		syscallarg(size_t) msgsz;
    635      1.10       cgd 		syscallarg(int) msgflg;
    636      1.54       dsl 	} */
    637      1.41      cube 
    638      1.44        ad 	return msgsnd1(l, SCARG(uap, msqid), SCARG(uap, msgp),
    639      1.41      cube 	    SCARG(uap, msgsz), SCARG(uap, msgflg), sizeof(long), copyin);
    640      1.41      cube }
    641      1.41      cube 
    642      1.41      cube int
    643      1.44        ad msgsnd1(struct lwp *l, int msqidr, const char *user_msgp, size_t msgsz,
    644      1.41      cube     int msgflg, size_t typesz, copyin_t fetch_type)
    645      1.41      cube {
    646      1.48        ad 	int segs_needed, error = 0, msqid;
    647      1.44        ad 	kauth_cred_t cred = l->l_cred;
    648      1.26   thorpej 	struct msqid_ds *msqptr;
    649      1.26   thorpej 	struct __msg *msghdr;
    650      1.48        ad 	kmsq_t *msq;
    651       1.3   mycroft 	short next;
    652       1.1       cgd 
    653      1.34   nathanw 	MSG_PRINTF(("call to msgsnd(%d, %p, %lld, %d)\n", msqid, user_msgp,
    654      1.34   nathanw 	    (long long)msgsz, msgflg));
    655      1.53     rmind restart:
    656      1.41      cube 	msqid = IPCID_TO_IX(msqidr);
    657       1.1       cgd 
    658      1.48        ad 	mutex_enter(&msgmutex);
    659      1.53     rmind 	/* In case of reallocation, we will wait for completion */
    660      1.53     rmind 	while (__predict_false(msg_realloc_state))
    661      1.53     rmind 		cv_wait(&msg_realloc_cv, &msgmutex);
    662      1.48        ad 
    663       1.3   mycroft 	if (msqid < 0 || msqid >= msginfo.msgmni) {
    664      1.20  christos 		MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
    665      1.20  christos 		    msginfo.msgmni));
    666      1.48        ad 		error = EINVAL;
    667      1.48        ad 		goto unlock;
    668       1.3   mycroft 	}
    669       1.1       cgd 
    670      1.48        ad 	msq = &msqs[msqid];
    671      1.48        ad 	msqptr = &msq->msq_u;
    672      1.48        ad 
    673       1.3   mycroft 	if (msqptr->msg_qbytes == 0) {
    674      1.20  christos 		MSG_PRINTF(("no such message queue id\n"));
    675      1.48        ad 		error = EINVAL;
    676      1.48        ad 		goto unlock;
    677       1.3   mycroft 	}
    678      1.41      cube 	if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
    679      1.20  christos 		MSG_PRINTF(("wrong sequence number\n"));
    680      1.48        ad 		error = EINVAL;
    681      1.48        ad 		goto unlock;
    682       1.3   mycroft 	}
    683       1.1       cgd 
    684      1.26   thorpej 	if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_W))) {
    685      1.20  christos 		MSG_PRINTF(("requester doesn't have write access\n"));
    686      1.48        ad 		goto unlock;
    687       1.3   mycroft 	}
    688       1.1       cgd 
    689       1.3   mycroft 	segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
    690      1.34   nathanw 	MSG_PRINTF(("msgsz=%lld, msgssz=%d, segs_needed=%d\n",
    691      1.34   nathanw 	    (long long)msgsz, msginfo.msgssz, segs_needed));
    692       1.3   mycroft 	for (;;) {
    693       1.3   mycroft 		int need_more_resources = 0;
    694       1.1       cgd 
    695       1.3   mycroft 		/*
    696      1.18  christos 		 * check msgsz [cannot be negative since it is unsigned]
    697       1.3   mycroft 		 * (inside this loop in case msg_qbytes changes while we sleep)
    698       1.3   mycroft 		 */
    699       1.1       cgd 
    700      1.18  christos 		if (msgsz > msqptr->msg_qbytes) {
    701      1.20  christos 			MSG_PRINTF(("msgsz > msqptr->msg_qbytes\n"));
    702      1.48        ad 			error = EINVAL;
    703      1.48        ad 			goto unlock;
    704       1.3   mycroft 		}
    705       1.1       cgd 
    706       1.3   mycroft 		if (msqptr->msg_perm.mode & MSG_LOCKED) {
    707      1.20  christos 			MSG_PRINTF(("msqid is locked\n"));
    708       1.3   mycroft 			need_more_resources = 1;
    709       1.3   mycroft 		}
    710      1.26   thorpej 		if (msgsz + msqptr->_msg_cbytes > msqptr->msg_qbytes) {
    711      1.20  christos 			MSG_PRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
    712       1.3   mycroft 			need_more_resources = 1;
    713       1.3   mycroft 		}
    714       1.3   mycroft 		if (segs_needed > nfree_msgmaps) {
    715      1.20  christos 			MSG_PRINTF(("segs_needed > nfree_msgmaps\n"));
    716       1.3   mycroft 			need_more_resources = 1;
    717       1.3   mycroft 		}
    718       1.3   mycroft 		if (free_msghdrs == NULL) {
    719      1.20  christos 			MSG_PRINTF(("no more msghdrs\n"));
    720       1.3   mycroft 			need_more_resources = 1;
    721       1.3   mycroft 		}
    722       1.1       cgd 
    723       1.3   mycroft 		if (need_more_resources) {
    724       1.3   mycroft 			int we_own_it;
    725       1.1       cgd 
    726       1.3   mycroft 			if ((msgflg & IPC_NOWAIT) != 0) {
    727      1.26   thorpej 				MSG_PRINTF(("need more resources but caller "
    728      1.26   thorpej 				    "doesn't want to wait\n"));
    729      1.48        ad 				error = EAGAIN;
    730      1.48        ad 				goto unlock;
    731       1.3   mycroft 			}
    732       1.1       cgd 
    733       1.3   mycroft 			if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
    734      1.20  christos 				MSG_PRINTF(("we don't own the msqid_ds\n"));
    735       1.3   mycroft 				we_own_it = 0;
    736       1.3   mycroft 			} else {
    737       1.3   mycroft 				/* Force later arrivals to wait for our
    738       1.3   mycroft 				   request */
    739      1.20  christos 				MSG_PRINTF(("we own the msqid_ds\n"));
    740       1.3   mycroft 				msqptr->msg_perm.mode |= MSG_LOCKED;
    741       1.3   mycroft 				we_own_it = 1;
    742       1.3   mycroft 			}
    743      1.51     rmind 
    744      1.51     rmind 			msg_waiters++;
    745      1.20  christos 			MSG_PRINTF(("goodnight\n"));
    746      1.48        ad 			error = cv_wait_sig(&msq->msq_cv, &msgmutex);
    747      1.26   thorpej 			MSG_PRINTF(("good morning, error=%d\n", error));
    748      1.51     rmind 			msg_waiters--;
    749      1.51     rmind 
    750      1.53     rmind 			if (we_own_it)
    751      1.53     rmind 				msqptr->msg_perm.mode &= ~MSG_LOCKED;
    752      1.53     rmind 
    753      1.53     rmind 			/*
    754      1.53     rmind 			 * In case of such state, notify reallocator and
    755      1.53     rmind 			 * restart the call.
    756      1.53     rmind 			 */
    757      1.53     rmind 			if (msg_realloc_state) {
    758      1.52      yamt 				cv_broadcast(&msg_realloc_cv);
    759      1.53     rmind 				mutex_exit(&msgmutex);
    760      1.53     rmind 				goto restart;
    761      1.53     rmind 			}
    762      1.51     rmind 
    763      1.53     rmind 			if (error != 0) {
    764      1.26   thorpej 				MSG_PRINTF(("msgsnd: interrupted system "
    765      1.26   thorpej 				    "call\n"));
    766      1.48        ad 				error = EINTR;
    767      1.48        ad 				goto unlock;
    768       1.3   mycroft 			}
    769       1.1       cgd 
    770       1.3   mycroft 			/*
    771       1.3   mycroft 			 * Make sure that the msq queue still exists
    772       1.3   mycroft 			 */
    773       1.1       cgd 
    774       1.3   mycroft 			if (msqptr->msg_qbytes == 0) {
    775      1.20  christos 				MSG_PRINTF(("msqid deleted\n"));
    776      1.48        ad 				error = EIDRM;
    777      1.48        ad 				goto unlock;
    778       1.3   mycroft 			}
    779       1.3   mycroft 		} else {
    780      1.20  christos 			MSG_PRINTF(("got all the resources that we need\n"));
    781       1.3   mycroft 			break;
    782       1.3   mycroft 		}
    783       1.1       cgd 	}
    784       1.1       cgd 
    785       1.3   mycroft 	/*
    786       1.3   mycroft 	 * We have the resources that we need.
    787       1.3   mycroft 	 * Make sure!
    788       1.3   mycroft 	 */
    789       1.1       cgd 
    790      1.50     rmind 	KASSERT((msqptr->msg_perm.mode & MSG_LOCKED) == 0);
    791      1.50     rmind 	KASSERT(segs_needed <= nfree_msgmaps);
    792      1.50     rmind 	KASSERT(msgsz + msqptr->_msg_cbytes <= msqptr->msg_qbytes);
    793      1.50     rmind 	KASSERT(free_msghdrs != NULL);
    794       1.1       cgd 
    795       1.3   mycroft 	/*
    796       1.3   mycroft 	 * Re-lock the msqid_ds in case we page-fault when copying in the
    797       1.3   mycroft 	 * message
    798       1.3   mycroft 	 */
    799       1.1       cgd 
    800      1.50     rmind 	KASSERT((msqptr->msg_perm.mode & MSG_LOCKED) == 0);
    801       1.3   mycroft 	msqptr->msg_perm.mode |= MSG_LOCKED;
    802       1.1       cgd 
    803       1.3   mycroft 	/*
    804       1.3   mycroft 	 * Allocate a message header
    805       1.3   mycroft 	 */
    806       1.1       cgd 
    807       1.3   mycroft 	msghdr = free_msghdrs;
    808       1.3   mycroft 	free_msghdrs = msghdr->msg_next;
    809       1.3   mycroft 	msghdr->msg_spot = -1;
    810       1.3   mycroft 	msghdr->msg_ts = msgsz;
    811       1.1       cgd 
    812       1.3   mycroft 	/*
    813       1.3   mycroft 	 * Allocate space for the message
    814       1.3   mycroft 	 */
    815       1.1       cgd 
    816       1.3   mycroft 	while (segs_needed > 0) {
    817      1.50     rmind 		KASSERT(nfree_msgmaps > 0);
    818      1.50     rmind 		KASSERT(free_msgmaps != -1);
    819      1.50     rmind 		KASSERT(free_msgmaps < msginfo.msgseg);
    820      1.50     rmind 
    821       1.3   mycroft 		next = free_msgmaps;
    822      1.20  christos 		MSG_PRINTF(("allocating segment %d to message\n", next));
    823       1.3   mycroft 		free_msgmaps = msgmaps[next].next;
    824       1.5   mycroft 		nfree_msgmaps--;
    825       1.3   mycroft 		msgmaps[next].next = msghdr->msg_spot;
    826       1.3   mycroft 		msghdr->msg_spot = next;
    827       1.5   mycroft 		segs_needed--;
    828       1.1       cgd 	}
    829       1.1       cgd 
    830       1.3   mycroft 	/*
    831       1.3   mycroft 	 * Copy in the message type
    832       1.3   mycroft 	 */
    833      1.48        ad 	mutex_exit(&msgmutex);
    834      1.48        ad 	error = (*fetch_type)(user_msgp, &msghdr->msg_type, typesz);
    835      1.48        ad 	mutex_enter(&msgmutex);
    836      1.48        ad 	if (error != 0) {
    837      1.26   thorpej 		MSG_PRINTF(("error %d copying the message type\n", error));
    838       1.3   mycroft 		msg_freehdr(msghdr);
    839       1.3   mycroft 		msqptr->msg_perm.mode &= ~MSG_LOCKED;
    840      1.48        ad 		cv_broadcast(&msq->msq_cv);
    841      1.48        ad 		goto unlock;
    842       1.3   mycroft 	}
    843      1.41      cube 	user_msgp += typesz;
    844       1.1       cgd 
    845       1.3   mycroft 	/*
    846       1.3   mycroft 	 * Validate the message type
    847       1.3   mycroft 	 */
    848       1.1       cgd 
    849       1.3   mycroft 	if (msghdr->msg_type < 1) {
    850       1.3   mycroft 		msg_freehdr(msghdr);
    851       1.3   mycroft 		msqptr->msg_perm.mode &= ~MSG_LOCKED;
    852      1.48        ad 		cv_broadcast(&msq->msq_cv);
    853      1.34   nathanw 		MSG_PRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
    854  1.56.2.3  wrstuden 		error = EINVAL;
    855      1.48        ad 		goto unlock;
    856       1.3   mycroft 	}
    857       1.1       cgd 
    858       1.3   mycroft 	/*
    859       1.3   mycroft 	 * Copy in the message body
    860       1.3   mycroft 	 */
    861       1.3   mycroft 
    862       1.3   mycroft 	next = msghdr->msg_spot;
    863       1.3   mycroft 	while (msgsz > 0) {
    864       1.3   mycroft 		size_t tlen;
    865      1.50     rmind 		KASSERT(next > -1);
    866      1.50     rmind 		KASSERT(next < msginfo.msgseg);
    867      1.50     rmind 
    868       1.3   mycroft 		if (msgsz > msginfo.msgssz)
    869       1.3   mycroft 			tlen = msginfo.msgssz;
    870       1.3   mycroft 		else
    871       1.3   mycroft 			tlen = msgsz;
    872      1.48        ad 		mutex_exit(&msgmutex);
    873      1.48        ad 		error = copyin(user_msgp, &msgpool[next * msginfo.msgssz], tlen);
    874      1.48        ad 		mutex_enter(&msgmutex);
    875      1.48        ad 		if (error != 0) {
    876      1.26   thorpej 			MSG_PRINTF(("error %d copying in message segment\n",
    877      1.26   thorpej 			    error));
    878       1.3   mycroft 			msg_freehdr(msghdr);
    879       1.3   mycroft 			msqptr->msg_perm.mode &= ~MSG_LOCKED;
    880      1.48        ad 			cv_broadcast(&msq->msq_cv);
    881      1.48        ad 			goto unlock;
    882       1.3   mycroft 		}
    883       1.3   mycroft 		msgsz -= tlen;
    884       1.3   mycroft 		user_msgp += tlen;
    885       1.3   mycroft 		next = msgmaps[next].next;
    886       1.1       cgd 	}
    887      1.50     rmind 	KASSERT(next == -1);
    888       1.1       cgd 
    889       1.3   mycroft 	/*
    890       1.3   mycroft 	 * We've got the message.  Unlock the msqid_ds.
    891       1.3   mycroft 	 */
    892       1.1       cgd 
    893       1.3   mycroft 	msqptr->msg_perm.mode &= ~MSG_LOCKED;
    894       1.1       cgd 
    895       1.3   mycroft 	/*
    896       1.3   mycroft 	 * Make sure that the msqid_ds is still allocated.
    897       1.3   mycroft 	 */
    898       1.1       cgd 
    899       1.3   mycroft 	if (msqptr->msg_qbytes == 0) {
    900       1.3   mycroft 		msg_freehdr(msghdr);
    901      1.48        ad 		cv_broadcast(&msq->msq_cv);
    902      1.48        ad 		error = EIDRM;
    903      1.48        ad 		goto unlock;
    904       1.3   mycroft 	}
    905       1.3   mycroft 
    906       1.3   mycroft 	/*
    907       1.3   mycroft 	 * Put the message into the queue
    908       1.3   mycroft 	 */
    909       1.1       cgd 
    910      1.26   thorpej 	if (msqptr->_msg_first == NULL) {
    911      1.26   thorpej 		msqptr->_msg_first = msghdr;
    912      1.26   thorpej 		msqptr->_msg_last = msghdr;
    913       1.3   mycroft 	} else {
    914      1.26   thorpej 		msqptr->_msg_last->msg_next = msghdr;
    915      1.26   thorpej 		msqptr->_msg_last = msghdr;
    916       1.3   mycroft 	}
    917      1.26   thorpej 	msqptr->_msg_last->msg_next = NULL;
    918       1.3   mycroft 
    919      1.26   thorpej 	msqptr->_msg_cbytes += msghdr->msg_ts;
    920       1.5   mycroft 	msqptr->msg_qnum++;
    921      1.44        ad 	msqptr->msg_lspid = l->l_proc->p_pid;
    922      1.43    kardel 	msqptr->msg_stime = time_second;
    923       1.3   mycroft 
    924      1.48        ad 	cv_broadcast(&msq->msq_cv);
    925      1.48        ad 
    926      1.50     rmind unlock:
    927      1.50     rmind 	mutex_exit(&msgmutex);
    928      1.48        ad 	return error;
    929       1.1       cgd }
    930       1.1       cgd 
    931       1.1       cgd int
    932      1.54       dsl sys_msgrcv(struct lwp *l, const struct sys_msgrcv_args *uap, register_t *retval)
    933      1.16   thorpej {
    934      1.54       dsl 	/* {
    935      1.10       cgd 		syscallarg(int) msqid;
    936      1.10       cgd 		syscallarg(void *) msgp;
    937      1.10       cgd 		syscallarg(size_t) msgsz;
    938      1.10       cgd 		syscallarg(long) msgtyp;
    939      1.10       cgd 		syscallarg(int) msgflg;
    940      1.54       dsl 	} */
    941      1.41      cube 
    942      1.44        ad 	return msgrcv1(l, SCARG(uap, msqid), SCARG(uap, msgp),
    943      1.41      cube 	    SCARG(uap, msgsz), SCARG(uap, msgtyp), SCARG(uap, msgflg),
    944      1.41      cube 	    sizeof(long), copyout, retval);
    945      1.41      cube }
    946      1.41      cube 
    947      1.41      cube int
    948      1.44        ad msgrcv1(struct lwp *l, int msqidr, char *user_msgp, size_t msgsz, long msgtyp,
    949      1.41      cube     int msgflg, size_t typesz, copyout_t put_type, register_t *retval)
    950      1.41      cube {
    951       1.3   mycroft 	size_t len;
    952      1.44        ad 	kauth_cred_t cred = l->l_cred;
    953      1.27  augustss 	struct msqid_ds *msqptr;
    954      1.27  augustss 	struct __msg *msghdr;
    955      1.48        ad 	int error = 0, msqid;
    956      1.48        ad 	kmsq_t *msq;
    957       1.3   mycroft 	short next;
    958       1.1       cgd 
    959      1.34   nathanw 	MSG_PRINTF(("call to msgrcv(%d, %p, %lld, %ld, %d)\n", msqid,
    960      1.34   nathanw 	    user_msgp, (long long)msgsz, msgtyp, msgflg));
    961      1.53     rmind restart:
    962      1.41      cube 	msqid = IPCID_TO_IX(msqidr);
    963       1.1       cgd 
    964      1.48        ad 	mutex_enter(&msgmutex);
    965      1.53     rmind 	/* In case of reallocation, we will wait for completion */
    966      1.53     rmind 	while (__predict_false(msg_realloc_state))
    967      1.53     rmind 		cv_wait(&msg_realloc_cv, &msgmutex);
    968      1.48        ad 
    969       1.3   mycroft 	if (msqid < 0 || msqid >= msginfo.msgmni) {
    970      1.20  christos 		MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
    971      1.20  christos 		    msginfo.msgmni));
    972      1.48        ad 		error = EINVAL;
    973      1.48        ad 		goto unlock;
    974       1.3   mycroft 	}
    975       1.1       cgd 
    976      1.48        ad 	msq = &msqs[msqid];
    977      1.48        ad 	msqptr = &msq->msq_u;
    978      1.48        ad 
    979       1.3   mycroft 	if (msqptr->msg_qbytes == 0) {
    980      1.20  christos 		MSG_PRINTF(("no such message queue id\n"));
    981      1.48        ad 		error = EINVAL;
    982      1.48        ad 		goto unlock;
    983       1.3   mycroft 	}
    984      1.41      cube 	if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
    985      1.20  christos 		MSG_PRINTF(("wrong sequence number\n"));
    986      1.48        ad 		error = EINVAL;
    987      1.48        ad 		goto unlock;
    988       1.3   mycroft 	}
    989       1.1       cgd 
    990      1.26   thorpej 	if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
    991      1.20  christos 		MSG_PRINTF(("requester doesn't have read access\n"));
    992      1.48        ad 		goto unlock;
    993       1.3   mycroft 	}
    994       1.1       cgd 
    995       1.3   mycroft 	msghdr = NULL;
    996       1.3   mycroft 	while (msghdr == NULL) {
    997       1.3   mycroft 		if (msgtyp == 0) {
    998      1.26   thorpej 			msghdr = msqptr->_msg_first;
    999       1.3   mycroft 			if (msghdr != NULL) {
   1000       1.3   mycroft 				if (msgsz < msghdr->msg_ts &&
   1001       1.3   mycroft 				    (msgflg & MSG_NOERROR) == 0) {
   1002      1.50     rmind 					MSG_PRINTF(("first msg on the queue "
   1003      1.50     rmind 					    "is too big (want %lld, got %d)\n",
   1004      1.34   nathanw 					    (long long)msgsz, msghdr->msg_ts));
   1005      1.48        ad 					error = E2BIG;
   1006      1.48        ad 					goto unlock;
   1007       1.3   mycroft 				}
   1008      1.26   thorpej 				if (msqptr->_msg_first == msqptr->_msg_last) {
   1009      1.26   thorpej 					msqptr->_msg_first = NULL;
   1010      1.26   thorpej 					msqptr->_msg_last = NULL;
   1011       1.3   mycroft 				} else {
   1012      1.26   thorpej 					msqptr->_msg_first = msghdr->msg_next;
   1013      1.50     rmind 					KASSERT(msqptr->_msg_first != NULL);
   1014       1.3   mycroft 				}
   1015       1.3   mycroft 			}
   1016       1.3   mycroft 		} else {
   1017      1.26   thorpej 			struct __msg *previous;
   1018      1.26   thorpej 			struct __msg **prev;
   1019       1.1       cgd 
   1020      1.26   thorpej 			for (previous = NULL, prev = &msqptr->_msg_first;
   1021      1.12   mycroft 			     (msghdr = *prev) != NULL;
   1022      1.12   mycroft 			     previous = msghdr, prev = &msghdr->msg_next) {
   1023       1.3   mycroft 				/*
   1024       1.3   mycroft 				 * Is this message's type an exact match or is
   1025       1.3   mycroft 				 * this message's type less than or equal to
   1026       1.3   mycroft 				 * the absolute value of a negative msgtyp?
   1027       1.3   mycroft 				 * Note that the second half of this test can
   1028       1.3   mycroft 				 * NEVER be true if msgtyp is positive since
   1029       1.3   mycroft 				 * msg_type is always positive!
   1030       1.3   mycroft 				 */
   1031       1.3   mycroft 
   1032      1.50     rmind 				if (msgtyp != msghdr->msg_type &&
   1033      1.50     rmind 				    msghdr->msg_type > -msgtyp)
   1034      1.50     rmind 					continue;
   1035      1.50     rmind 
   1036      1.50     rmind 				MSG_PRINTF(("found message type %ld, requested %ld\n",
   1037      1.50     rmind 				    msghdr->msg_type, msgtyp));
   1038      1.50     rmind 				if (msgsz < msghdr->msg_ts &&
   1039      1.50     rmind 				     (msgflg & MSG_NOERROR) == 0) {
   1040      1.50     rmind 					MSG_PRINTF(("requested message on the queue "
   1041      1.50     rmind 					    "is too big (want %lld, got %d)\n",
   1042      1.50     rmind 					    (long long)msgsz, msghdr->msg_ts));
   1043      1.50     rmind 					error = E2BIG;
   1044      1.50     rmind 					goto unlock;
   1045      1.50     rmind 				}
   1046      1.50     rmind 				*prev = msghdr->msg_next;
   1047      1.50     rmind 				if (msghdr != msqptr->_msg_last)
   1048       1.3   mycroft 					break;
   1049      1.50     rmind 				if (previous == NULL) {
   1050      1.50     rmind 					KASSERT(prev == &msqptr->_msg_first);
   1051      1.50     rmind 					msqptr->_msg_first = NULL;
   1052      1.50     rmind 					msqptr->_msg_last = NULL;
   1053      1.50     rmind 				} else {
   1054      1.50     rmind 					KASSERT(prev != &msqptr->_msg_first);
   1055      1.50     rmind 					msqptr->_msg_last = previous;
   1056       1.3   mycroft 				}
   1057      1.50     rmind 				break;
   1058       1.3   mycroft 			}
   1059       1.1       cgd 		}
   1060       1.1       cgd 
   1061       1.3   mycroft 		/*
   1062       1.3   mycroft 		 * We've either extracted the msghdr for the appropriate
   1063       1.3   mycroft 		 * message or there isn't one.
   1064       1.3   mycroft 		 * If there is one then bail out of this loop.
   1065       1.3   mycroft 		 */
   1066       1.3   mycroft 		if (msghdr != NULL)
   1067       1.3   mycroft 			break;
   1068       1.1       cgd 
   1069       1.1       cgd 		/*
   1070       1.3   mycroft 		 * Hmph!  No message found.  Does the user want to wait?
   1071       1.1       cgd 		 */
   1072       1.1       cgd 
   1073       1.3   mycroft 		if ((msgflg & IPC_NOWAIT) != 0) {
   1074      1.34   nathanw 			MSG_PRINTF(("no appropriate message found (msgtyp=%ld)\n",
   1075      1.20  christos 			    msgtyp));
   1076      1.48        ad 			error = ENOMSG;
   1077      1.48        ad 			goto unlock;
   1078       1.3   mycroft 		}
   1079       1.1       cgd 
   1080       1.3   mycroft 		/*
   1081       1.3   mycroft 		 * Wait for something to happen
   1082       1.3   mycroft 		 */
   1083       1.1       cgd 
   1084      1.51     rmind 		msg_waiters++;
   1085      1.20  christos 		MSG_PRINTF(("msgrcv:  goodnight\n"));
   1086      1.48        ad 		error = cv_wait_sig(&msq->msq_cv, &msgmutex);
   1087      1.26   thorpej 		MSG_PRINTF(("msgrcv: good morning (error=%d)\n", error));
   1088      1.51     rmind 		msg_waiters--;
   1089       1.1       cgd 
   1090      1.53     rmind 		/*
   1091      1.53     rmind 		 * In case of such state, notify reallocator and
   1092      1.53     rmind 		 * restart the call.
   1093      1.53     rmind 		 */
   1094      1.53     rmind 		if (msg_realloc_state) {
   1095      1.52      yamt 			cv_broadcast(&msg_realloc_cv);
   1096      1.53     rmind 			mutex_exit(&msgmutex);
   1097      1.53     rmind 			goto restart;
   1098      1.53     rmind 		}
   1099      1.51     rmind 
   1100      1.53     rmind 		if (error != 0) {
   1101      1.26   thorpej 			MSG_PRINTF(("msgsnd: interrupted system call\n"));
   1102      1.48        ad 			error = EINTR;
   1103      1.48        ad 			goto unlock;
   1104       1.3   mycroft 		}
   1105       1.1       cgd 
   1106       1.3   mycroft 		/*
   1107       1.3   mycroft 		 * Make sure that the msq queue still exists
   1108       1.3   mycroft 		 */
   1109       1.1       cgd 
   1110       1.3   mycroft 		if (msqptr->msg_qbytes == 0 ||
   1111      1.41      cube 		    msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
   1112      1.20  christos 			MSG_PRINTF(("msqid deleted\n"));
   1113      1.48        ad 			error = EIDRM;
   1114      1.48        ad 			goto unlock;
   1115       1.3   mycroft 		}
   1116       1.1       cgd 	}
   1117       1.1       cgd 
   1118       1.3   mycroft 	/*
   1119       1.3   mycroft 	 * Return the message to the user.
   1120       1.3   mycroft 	 *
   1121       1.3   mycroft 	 * First, do the bookkeeping (before we risk being interrupted).
   1122       1.3   mycroft 	 */
   1123       1.1       cgd 
   1124      1.26   thorpej 	msqptr->_msg_cbytes -= msghdr->msg_ts;
   1125       1.5   mycroft 	msqptr->msg_qnum--;
   1126      1.44        ad 	msqptr->msg_lrpid = l->l_proc->p_pid;
   1127      1.43    kardel 	msqptr->msg_rtime = time_second;
   1128       1.1       cgd 
   1129       1.3   mycroft 	/*
   1130       1.3   mycroft 	 * Make msgsz the actual amount that we'll be returning.
   1131       1.3   mycroft 	 * Note that this effectively truncates the message if it is too long
   1132       1.3   mycroft 	 * (since msgsz is never increased).
   1133       1.3   mycroft 	 */
   1134       1.1       cgd 
   1135      1.34   nathanw 	MSG_PRINTF(("found a message, msgsz=%lld, msg_ts=%d\n",
   1136      1.34   nathanw 	    (long long)msgsz, msghdr->msg_ts));
   1137       1.3   mycroft 	if (msgsz > msghdr->msg_ts)
   1138       1.3   mycroft 		msgsz = msghdr->msg_ts;
   1139       1.1       cgd 
   1140       1.3   mycroft 	/*
   1141       1.3   mycroft 	 * Return the type to the user.
   1142       1.3   mycroft 	 */
   1143      1.48        ad 	mutex_exit(&msgmutex);
   1144      1.41      cube 	error = (*put_type)(&msghdr->msg_type, user_msgp, typesz);
   1145      1.48        ad 	mutex_enter(&msgmutex);
   1146      1.26   thorpej 	if (error != 0) {
   1147      1.26   thorpej 		MSG_PRINTF(("error (%d) copying out message type\n", error));
   1148       1.3   mycroft 		msg_freehdr(msghdr);
   1149      1.48        ad 		cv_broadcast(&msq->msq_cv);
   1150      1.48        ad 		goto unlock;
   1151       1.3   mycroft 	}
   1152      1.41      cube 	user_msgp += typesz;
   1153       1.3   mycroft 
   1154       1.3   mycroft 	/*
   1155       1.3   mycroft 	 * Return the segments to the user
   1156       1.3   mycroft 	 */
   1157       1.1       cgd 
   1158       1.3   mycroft 	next = msghdr->msg_spot;
   1159       1.3   mycroft 	for (len = 0; len < msgsz; len += msginfo.msgssz) {
   1160       1.3   mycroft 		size_t tlen;
   1161      1.50     rmind 		KASSERT(next > -1);
   1162      1.50     rmind 		KASSERT(next < msginfo.msgseg);
   1163       1.3   mycroft 
   1164      1.25       mrg 		if (msgsz - len > msginfo.msgssz)
   1165       1.3   mycroft 			tlen = msginfo.msgssz;
   1166       1.3   mycroft 		else
   1167      1.25       mrg 			tlen = msgsz - len;
   1168      1.48        ad 		mutex_exit(&msgmutex);
   1169      1.51     rmind 		error = (*put_type)(&msgpool[next * msginfo.msgssz],
   1170       1.3   mycroft 		    user_msgp, tlen);
   1171      1.48        ad 		mutex_enter(&msgmutex);
   1172      1.26   thorpej 		if (error != 0) {
   1173      1.20  christos 			MSG_PRINTF(("error (%d) copying out message segment\n",
   1174      1.26   thorpej 			    error));
   1175       1.3   mycroft 			msg_freehdr(msghdr);
   1176      1.48        ad 			cv_broadcast(&msq->msq_cv);
   1177      1.48        ad 			goto unlock;
   1178       1.3   mycroft 		}
   1179       1.3   mycroft 		user_msgp += tlen;
   1180       1.3   mycroft 		next = msgmaps[next].next;
   1181       1.1       cgd 	}
   1182       1.1       cgd 
   1183       1.3   mycroft 	/*
   1184       1.3   mycroft 	 * Done, return the actual number of bytes copied out.
   1185       1.3   mycroft 	 */
   1186       1.1       cgd 
   1187       1.3   mycroft 	msg_freehdr(msghdr);
   1188      1.48        ad 	cv_broadcast(&msq->msq_cv);
   1189       1.3   mycroft 	*retval = msgsz;
   1190      1.48        ad 
   1191      1.50     rmind unlock:
   1192      1.50     rmind 	mutex_exit(&msgmutex);
   1193      1.48        ad 	return error;
   1194       1.1       cgd }
   1195      1.51     rmind 
   1196      1.51     rmind /*
   1197      1.51     rmind  * Sysctl initialization and nodes.
   1198      1.51     rmind  */
   1199      1.51     rmind 
   1200      1.51     rmind static int
   1201      1.51     rmind sysctl_ipc_msgmni(SYSCTLFN_ARGS)
   1202      1.51     rmind {
   1203      1.51     rmind 	int newsize, error;
   1204      1.51     rmind 	struct sysctlnode node;
   1205      1.51     rmind 	node = *rnode;
   1206      1.51     rmind 	node.sysctl_data = &newsize;
   1207      1.51     rmind 
   1208      1.51     rmind 	newsize = msginfo.msgmni;
   1209      1.51     rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1210      1.51     rmind 	if (error || newp == NULL)
   1211      1.51     rmind 		return error;
   1212      1.51     rmind 
   1213      1.55        ad 	sysctl_unlock();
   1214      1.55        ad 	error = msgrealloc(newsize, msginfo.msgseg);
   1215      1.55        ad 	sysctl_relock();
   1216      1.55        ad 	return error;
   1217      1.51     rmind }
   1218      1.51     rmind 
   1219      1.51     rmind static int
   1220      1.51     rmind sysctl_ipc_msgseg(SYSCTLFN_ARGS)
   1221      1.51     rmind {
   1222      1.51     rmind 	int newsize, error;
   1223      1.51     rmind 	struct sysctlnode node;
   1224      1.51     rmind 	node = *rnode;
   1225      1.51     rmind 	node.sysctl_data = &newsize;
   1226      1.51     rmind 
   1227      1.51     rmind 	newsize = msginfo.msgseg;
   1228      1.51     rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1229      1.51     rmind 	if (error || newp == NULL)
   1230      1.51     rmind 		return error;
   1231      1.51     rmind 
   1232      1.55        ad 	sysctl_unlock();
   1233      1.55        ad 	error = msgrealloc(msginfo.msgmni, newsize);
   1234      1.55        ad 	sysctl_relock();
   1235      1.55        ad 	return error;
   1236      1.51     rmind }
   1237      1.51     rmind 
   1238      1.51     rmind SYSCTL_SETUP(sysctl_ipc_msg_setup, "sysctl kern.ipc subtree setup")
   1239      1.51     rmind {
   1240      1.51     rmind 	const struct sysctlnode *node = NULL;
   1241      1.51     rmind 
   1242      1.51     rmind 	sysctl_createv(clog, 0, NULL, NULL,
   1243      1.51     rmind 		CTLFLAG_PERMANENT,
   1244      1.51     rmind 		CTLTYPE_NODE, "kern", NULL,
   1245      1.51     rmind 		NULL, 0, NULL, 0,
   1246      1.51     rmind 		CTL_KERN, CTL_EOL);
   1247      1.51     rmind 	sysctl_createv(clog, 0, NULL, &node,
   1248      1.51     rmind 		CTLFLAG_PERMANENT,
   1249      1.51     rmind 		CTLTYPE_NODE, "ipc",
   1250      1.51     rmind 		SYSCTL_DESCR("SysV IPC options"),
   1251      1.51     rmind 		NULL, 0, NULL, 0,
   1252      1.51     rmind 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
   1253      1.51     rmind 
   1254      1.51     rmind 	if (node == NULL)
   1255      1.51     rmind 		return;
   1256      1.51     rmind 
   1257      1.51     rmind 	sysctl_createv(clog, 0, &node, NULL,
   1258      1.51     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1259      1.51     rmind 		CTLTYPE_INT, "msgmni",
   1260      1.51     rmind 		SYSCTL_DESCR("Max number of message queue identifiers"),
   1261      1.51     rmind 		sysctl_ipc_msgmni, 0, &msginfo.msgmni, 0,
   1262      1.51     rmind 		CTL_CREATE, CTL_EOL);
   1263      1.51     rmind 	sysctl_createv(clog, 0, &node, NULL,
   1264      1.51     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1265      1.51     rmind 		CTLTYPE_INT, "msgseg",
   1266      1.51     rmind 		SYSCTL_DESCR("Max number of number of message segments"),
   1267      1.51     rmind 		sysctl_ipc_msgseg, 0, &msginfo.msgseg, 0,
   1268      1.51     rmind 		CTL_CREATE, CTL_EOL);
   1269      1.51     rmind }
   1270