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