Home | History | Annotate | Line # | Download | only in kern
sysv_msg.c revision 1.55.8.1
      1 /*	$NetBSD: sysv_msg.c,v 1.55.8.1 2008/03/29 20:47:01 christos 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.55.8.1 2008/03/29 20:47:01 christos 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___msgctl50(struct lwp *l, const struct sys___msgctl50_args *uap,
    403     register_t *retval)
    404 {
    405 	/* {
    406 		syscallarg(int) msqid;
    407 		syscallarg(int) cmd;
    408 		syscallarg(struct msqid_ds *) buf;
    409 	} */
    410 	struct msqid_ds msqbuf;
    411 	int cmd, error;
    412 
    413 	cmd = SCARG(uap, cmd);
    414 
    415 	if (cmd == IPC_SET) {
    416 		error = copyin(SCARG(uap, buf), &msqbuf, sizeof(msqbuf));
    417 		if (error)
    418 			return (error);
    419 	}
    420 
    421 	error = msgctl1(l, SCARG(uap, msqid), cmd,
    422 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &msqbuf : NULL);
    423 
    424 	if (error == 0 && cmd == IPC_STAT)
    425 		error = copyout(&msqbuf, SCARG(uap, buf), sizeof(msqbuf));
    426 
    427 	return (error);
    428 }
    429 
    430 int
    431 msgctl1(struct lwp *l, int msqid, int cmd, struct msqid_ds *msqbuf)
    432 {
    433 	kauth_cred_t cred = l->l_cred;
    434 	struct msqid_ds *msqptr;
    435 	kmsq_t *msq;
    436 	int error = 0, ix;
    437 
    438 	MSG_PRINTF(("call to msgctl1(%d, %d)\n", msqid, cmd));
    439 
    440 	ix = IPCID_TO_IX(msqid);
    441 
    442 	mutex_enter(&msgmutex);
    443 
    444 	if (ix < 0 || ix >= msginfo.msgmni) {
    445 		MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", ix,
    446 		    msginfo.msgmni));
    447 		error = EINVAL;
    448 		goto unlock;
    449 	}
    450 
    451 	msq = &msqs[ix];
    452 	msqptr = &msq->msq_u;
    453 
    454 	if (msqptr->msg_qbytes == 0) {
    455 		MSG_PRINTF(("no such msqid\n"));
    456 		error = EINVAL;
    457 		goto unlock;
    458 	}
    459 	if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqid)) {
    460 		MSG_PRINTF(("wrong sequence number\n"));
    461 		error = EINVAL;
    462 		goto unlock;
    463 	}
    464 
    465 	switch (cmd) {
    466 	case IPC_RMID:
    467 	{
    468 		struct __msg *msghdr;
    469 		if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)) != 0)
    470 			break;
    471 		/* Free the message headers */
    472 		msghdr = msqptr->_msg_first;
    473 		while (msghdr != NULL) {
    474 			struct __msg *msghdr_tmp;
    475 
    476 			/* Free the segments of each message */
    477 			msqptr->_msg_cbytes -= msghdr->msg_ts;
    478 			msqptr->msg_qnum--;
    479 			msghdr_tmp = msghdr;
    480 			msghdr = msghdr->msg_next;
    481 			msg_freehdr(msghdr_tmp);
    482 		}
    483 		KASSERT(msqptr->_msg_cbytes == 0);
    484 		KASSERT(msqptr->msg_qnum == 0);
    485 
    486 		/* Mark it as free */
    487 		msqptr->msg_qbytes = 0;
    488 		cv_broadcast(&msq->msq_cv);
    489 	}
    490 		break;
    491 
    492 	case IPC_SET:
    493 		if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
    494 			break;
    495 		if (msqbuf->msg_qbytes > msqptr->msg_qbytes &&
    496 		    kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    497 		    NULL) != 0) {
    498 			error = EPERM;
    499 			break;
    500 		}
    501 		if (msqbuf->msg_qbytes > msginfo.msgmnb) {
    502 			MSG_PRINTF(("can't increase msg_qbytes beyond %d "
    503 			    "(truncating)\n", msginfo.msgmnb));
    504 			/* silently restrict qbytes to system limit */
    505 			msqbuf->msg_qbytes = msginfo.msgmnb;
    506 		}
    507 		if (msqbuf->msg_qbytes == 0) {
    508 			MSG_PRINTF(("can't reduce msg_qbytes to 0\n"));
    509 			error = EINVAL;		/* XXX non-standard errno! */
    510 			break;
    511 		}
    512 		msqptr->msg_perm.uid = msqbuf->msg_perm.uid;
    513 		msqptr->msg_perm.gid = msqbuf->msg_perm.gid;
    514 		msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
    515 		    (msqbuf->msg_perm.mode & 0777);
    516 		msqptr->msg_qbytes = msqbuf->msg_qbytes;
    517 		msqptr->msg_ctime = time_second;
    518 		break;
    519 
    520 	case IPC_STAT:
    521 		if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
    522 			MSG_PRINTF(("requester doesn't have read access\n"));
    523 			break;
    524 		}
    525 		memcpy(msqbuf, msqptr, sizeof(struct msqid_ds));
    526 		break;
    527 
    528 	default:
    529 		MSG_PRINTF(("invalid command %d\n", cmd));
    530 		error = EINVAL;
    531 		break;
    532 	}
    533 
    534 unlock:
    535 	mutex_exit(&msgmutex);
    536 	return (error);
    537 }
    538 
    539 int
    540 sys_msgget(struct lwp *l, const struct sys_msgget_args *uap, register_t *retval)
    541 {
    542 	/* {
    543 		syscallarg(key_t) key;
    544 		syscallarg(int) msgflg;
    545 	} */
    546 	int msqid, error = 0;
    547 	int key = SCARG(uap, key);
    548 	int msgflg = SCARG(uap, msgflg);
    549 	kauth_cred_t cred = l->l_cred;
    550 	struct msqid_ds *msqptr = NULL;
    551 	kmsq_t *msq;
    552 
    553 	mutex_enter(&msgmutex);
    554 
    555 	MSG_PRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
    556 
    557 	if (key != IPC_PRIVATE) {
    558 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
    559 			msq = &msqs[msqid];
    560 			msqptr = &msq->msq_u;
    561 			if (msqptr->msg_qbytes != 0 &&
    562 			    msqptr->msg_perm._key == key)
    563 				break;
    564 		}
    565 		if (msqid < msginfo.msgmni) {
    566 			MSG_PRINTF(("found public key\n"));
    567 			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
    568 				MSG_PRINTF(("not exclusive\n"));
    569 				error = EEXIST;
    570 				goto unlock;
    571 			}
    572 			if ((error = ipcperm(cred, &msqptr->msg_perm,
    573 			    msgflg & 0700 ))) {
    574 				MSG_PRINTF(("requester doesn't have 0%o access\n",
    575 				    msgflg & 0700));
    576 				goto unlock;
    577 			}
    578 			goto found;
    579 		}
    580 	}
    581 
    582 	MSG_PRINTF(("need to allocate the msqid_ds\n"));
    583 	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
    584 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
    585 			/*
    586 			 * Look for an unallocated and unlocked msqid_ds.
    587 			 * msqid_ds's can be locked by msgsnd or msgrcv while
    588 			 * they are copying the message in/out.  We can't
    589 			 * re-use the entry until they release it.
    590 			 */
    591 			msq = &msqs[msqid];
    592 			msqptr = &msq->msq_u;
    593 			if (msqptr->msg_qbytes == 0 &&
    594 			    (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
    595 				break;
    596 		}
    597 		if (msqid == msginfo.msgmni) {
    598 			MSG_PRINTF(("no more msqid_ds's available\n"));
    599 			error = ENOSPC;
    600 			goto unlock;
    601 		}
    602 		MSG_PRINTF(("msqid %d is available\n", msqid));
    603 		msqptr->msg_perm._key = key;
    604 		msqptr->msg_perm.cuid = kauth_cred_geteuid(cred);
    605 		msqptr->msg_perm.uid = kauth_cred_geteuid(cred);
    606 		msqptr->msg_perm.cgid = kauth_cred_getegid(cred);
    607 		msqptr->msg_perm.gid = kauth_cred_getegid(cred);
    608 		msqptr->msg_perm.mode = (msgflg & 0777);
    609 		/* Make sure that the returned msqid is unique */
    610 		msqptr->msg_perm._seq++;
    611 		msqptr->_msg_first = NULL;
    612 		msqptr->_msg_last = NULL;
    613 		msqptr->_msg_cbytes = 0;
    614 		msqptr->msg_qnum = 0;
    615 		msqptr->msg_qbytes = msginfo.msgmnb;
    616 		msqptr->msg_lspid = 0;
    617 		msqptr->msg_lrpid = 0;
    618 		msqptr->msg_stime = 0;
    619 		msqptr->msg_rtime = 0;
    620 		msqptr->msg_ctime = time_second;
    621 	} else {
    622 		MSG_PRINTF(("didn't find it and wasn't asked to create it\n"));
    623 		error = ENOENT;
    624 		goto unlock;
    625 	}
    626 
    627 found:
    628 	/* Construct the unique msqid */
    629 	*retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
    630 
    631 unlock:
    632 	mutex_exit(&msgmutex);
    633 	return (error);
    634 }
    635 
    636 int
    637 sys_msgsnd(struct lwp *l, const struct sys_msgsnd_args *uap, register_t *retval)
    638 {
    639 	/* {
    640 		syscallarg(int) msqid;
    641 		syscallarg(const void *) msgp;
    642 		syscallarg(size_t) msgsz;
    643 		syscallarg(int) msgflg;
    644 	} */
    645 
    646 	return msgsnd1(l, SCARG(uap, msqid), SCARG(uap, msgp),
    647 	    SCARG(uap, msgsz), SCARG(uap, msgflg), sizeof(long), copyin);
    648 }
    649 
    650 int
    651 msgsnd1(struct lwp *l, int msqidr, const char *user_msgp, size_t msgsz,
    652     int msgflg, size_t typesz, copyin_t fetch_type)
    653 {
    654 	int segs_needed, error = 0, msqid;
    655 	kauth_cred_t cred = l->l_cred;
    656 	struct msqid_ds *msqptr;
    657 	struct __msg *msghdr;
    658 	kmsq_t *msq;
    659 	short next;
    660 
    661 	MSG_PRINTF(("call to msgsnd(%d, %p, %lld, %d)\n", msqid, user_msgp,
    662 	    (long long)msgsz, msgflg));
    663 restart:
    664 	msqid = IPCID_TO_IX(msqidr);
    665 
    666 	mutex_enter(&msgmutex);
    667 	/* In case of reallocation, we will wait for completion */
    668 	while (__predict_false(msg_realloc_state))
    669 		cv_wait(&msg_realloc_cv, &msgmutex);
    670 
    671 	if (msqid < 0 || msqid >= msginfo.msgmni) {
    672 		MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
    673 		    msginfo.msgmni));
    674 		error = EINVAL;
    675 		goto unlock;
    676 	}
    677 
    678 	msq = &msqs[msqid];
    679 	msqptr = &msq->msq_u;
    680 
    681 	if (msqptr->msg_qbytes == 0) {
    682 		MSG_PRINTF(("no such message queue id\n"));
    683 		error = EINVAL;
    684 		goto unlock;
    685 	}
    686 	if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
    687 		MSG_PRINTF(("wrong sequence number\n"));
    688 		error = EINVAL;
    689 		goto unlock;
    690 	}
    691 
    692 	if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_W))) {
    693 		MSG_PRINTF(("requester doesn't have write access\n"));
    694 		goto unlock;
    695 	}
    696 
    697 	segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
    698 	MSG_PRINTF(("msgsz=%lld, msgssz=%d, segs_needed=%d\n",
    699 	    (long long)msgsz, msginfo.msgssz, segs_needed));
    700 	for (;;) {
    701 		int need_more_resources = 0;
    702 
    703 		/*
    704 		 * check msgsz [cannot be negative since it is unsigned]
    705 		 * (inside this loop in case msg_qbytes changes while we sleep)
    706 		 */
    707 
    708 		if (msgsz > msqptr->msg_qbytes) {
    709 			MSG_PRINTF(("msgsz > msqptr->msg_qbytes\n"));
    710 			error = EINVAL;
    711 			goto unlock;
    712 		}
    713 
    714 		if (msqptr->msg_perm.mode & MSG_LOCKED) {
    715 			MSG_PRINTF(("msqid is locked\n"));
    716 			need_more_resources = 1;
    717 		}
    718 		if (msgsz + msqptr->_msg_cbytes > msqptr->msg_qbytes) {
    719 			MSG_PRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
    720 			need_more_resources = 1;
    721 		}
    722 		if (segs_needed > nfree_msgmaps) {
    723 			MSG_PRINTF(("segs_needed > nfree_msgmaps\n"));
    724 			need_more_resources = 1;
    725 		}
    726 		if (free_msghdrs == NULL) {
    727 			MSG_PRINTF(("no more msghdrs\n"));
    728 			need_more_resources = 1;
    729 		}
    730 
    731 		if (need_more_resources) {
    732 			int we_own_it;
    733 
    734 			if ((msgflg & IPC_NOWAIT) != 0) {
    735 				MSG_PRINTF(("need more resources but caller "
    736 				    "doesn't want to wait\n"));
    737 				error = EAGAIN;
    738 				goto unlock;
    739 			}
    740 
    741 			if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
    742 				MSG_PRINTF(("we don't own the msqid_ds\n"));
    743 				we_own_it = 0;
    744 			} else {
    745 				/* Force later arrivals to wait for our
    746 				   request */
    747 				MSG_PRINTF(("we own the msqid_ds\n"));
    748 				msqptr->msg_perm.mode |= MSG_LOCKED;
    749 				we_own_it = 1;
    750 			}
    751 
    752 			msg_waiters++;
    753 			MSG_PRINTF(("goodnight\n"));
    754 			error = cv_wait_sig(&msq->msq_cv, &msgmutex);
    755 			MSG_PRINTF(("good morning, error=%d\n", error));
    756 			msg_waiters--;
    757 
    758 			if (we_own_it)
    759 				msqptr->msg_perm.mode &= ~MSG_LOCKED;
    760 
    761 			/*
    762 			 * In case of such state, notify reallocator and
    763 			 * restart the call.
    764 			 */
    765 			if (msg_realloc_state) {
    766 				cv_broadcast(&msg_realloc_cv);
    767 				mutex_exit(&msgmutex);
    768 				goto restart;
    769 			}
    770 
    771 			if (error != 0) {
    772 				MSG_PRINTF(("msgsnd: interrupted system "
    773 				    "call\n"));
    774 				error = EINTR;
    775 				goto unlock;
    776 			}
    777 
    778 			/*
    779 			 * Make sure that the msq queue still exists
    780 			 */
    781 
    782 			if (msqptr->msg_qbytes == 0) {
    783 				MSG_PRINTF(("msqid deleted\n"));
    784 				error = EIDRM;
    785 				goto unlock;
    786 			}
    787 		} else {
    788 			MSG_PRINTF(("got all the resources that we need\n"));
    789 			break;
    790 		}
    791 	}
    792 
    793 	/*
    794 	 * We have the resources that we need.
    795 	 * Make sure!
    796 	 */
    797 
    798 	KASSERT((msqptr->msg_perm.mode & MSG_LOCKED) == 0);
    799 	KASSERT(segs_needed <= nfree_msgmaps);
    800 	KASSERT(msgsz + msqptr->_msg_cbytes <= msqptr->msg_qbytes);
    801 	KASSERT(free_msghdrs != NULL);
    802 
    803 	/*
    804 	 * Re-lock the msqid_ds in case we page-fault when copying in the
    805 	 * message
    806 	 */
    807 
    808 	KASSERT((msqptr->msg_perm.mode & MSG_LOCKED) == 0);
    809 	msqptr->msg_perm.mode |= MSG_LOCKED;
    810 
    811 	/*
    812 	 * Allocate a message header
    813 	 */
    814 
    815 	msghdr = free_msghdrs;
    816 	free_msghdrs = msghdr->msg_next;
    817 	msghdr->msg_spot = -1;
    818 	msghdr->msg_ts = msgsz;
    819 
    820 	/*
    821 	 * Allocate space for the message
    822 	 */
    823 
    824 	while (segs_needed > 0) {
    825 		KASSERT(nfree_msgmaps > 0);
    826 		KASSERT(free_msgmaps != -1);
    827 		KASSERT(free_msgmaps < msginfo.msgseg);
    828 
    829 		next = free_msgmaps;
    830 		MSG_PRINTF(("allocating segment %d to message\n", next));
    831 		free_msgmaps = msgmaps[next].next;
    832 		nfree_msgmaps--;
    833 		msgmaps[next].next = msghdr->msg_spot;
    834 		msghdr->msg_spot = next;
    835 		segs_needed--;
    836 	}
    837 
    838 	/*
    839 	 * Copy in the message type
    840 	 */
    841 	mutex_exit(&msgmutex);
    842 	error = (*fetch_type)(user_msgp, &msghdr->msg_type, typesz);
    843 	mutex_enter(&msgmutex);
    844 	if (error != 0) {
    845 		MSG_PRINTF(("error %d copying the message type\n", error));
    846 		msg_freehdr(msghdr);
    847 		msqptr->msg_perm.mode &= ~MSG_LOCKED;
    848 		cv_broadcast(&msq->msq_cv);
    849 		goto unlock;
    850 	}
    851 	user_msgp += typesz;
    852 
    853 	/*
    854 	 * Validate the message type
    855 	 */
    856 
    857 	if (msghdr->msg_type < 1) {
    858 		msg_freehdr(msghdr);
    859 		msqptr->msg_perm.mode &= ~MSG_LOCKED;
    860 		cv_broadcast(&msq->msq_cv);
    861 		MSG_PRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
    862 		goto unlock;
    863 	}
    864 
    865 	/*
    866 	 * Copy in the message body
    867 	 */
    868 
    869 	next = msghdr->msg_spot;
    870 	while (msgsz > 0) {
    871 		size_t tlen;
    872 		KASSERT(next > -1);
    873 		KASSERT(next < msginfo.msgseg);
    874 
    875 		if (msgsz > msginfo.msgssz)
    876 			tlen = msginfo.msgssz;
    877 		else
    878 			tlen = msgsz;
    879 		mutex_exit(&msgmutex);
    880 		error = copyin(user_msgp, &msgpool[next * msginfo.msgssz], tlen);
    881 		mutex_enter(&msgmutex);
    882 		if (error != 0) {
    883 			MSG_PRINTF(("error %d copying in message segment\n",
    884 			    error));
    885 			msg_freehdr(msghdr);
    886 			msqptr->msg_perm.mode &= ~MSG_LOCKED;
    887 			cv_broadcast(&msq->msq_cv);
    888 			goto unlock;
    889 		}
    890 		msgsz -= tlen;
    891 		user_msgp += tlen;
    892 		next = msgmaps[next].next;
    893 	}
    894 	KASSERT(next == -1);
    895 
    896 	/*
    897 	 * We've got the message.  Unlock the msqid_ds.
    898 	 */
    899 
    900 	msqptr->msg_perm.mode &= ~MSG_LOCKED;
    901 
    902 	/*
    903 	 * Make sure that the msqid_ds is still allocated.
    904 	 */
    905 
    906 	if (msqptr->msg_qbytes == 0) {
    907 		msg_freehdr(msghdr);
    908 		cv_broadcast(&msq->msq_cv);
    909 		error = EIDRM;
    910 		goto unlock;
    911 	}
    912 
    913 	/*
    914 	 * Put the message into the queue
    915 	 */
    916 
    917 	if (msqptr->_msg_first == NULL) {
    918 		msqptr->_msg_first = msghdr;
    919 		msqptr->_msg_last = msghdr;
    920 	} else {
    921 		msqptr->_msg_last->msg_next = msghdr;
    922 		msqptr->_msg_last = msghdr;
    923 	}
    924 	msqptr->_msg_last->msg_next = NULL;
    925 
    926 	msqptr->_msg_cbytes += msghdr->msg_ts;
    927 	msqptr->msg_qnum++;
    928 	msqptr->msg_lspid = l->l_proc->p_pid;
    929 	msqptr->msg_stime = time_second;
    930 
    931 	cv_broadcast(&msq->msq_cv);
    932 
    933 unlock:
    934 	mutex_exit(&msgmutex);
    935 	return error;
    936 }
    937 
    938 int
    939 sys_msgrcv(struct lwp *l, const struct sys_msgrcv_args *uap, register_t *retval)
    940 {
    941 	/* {
    942 		syscallarg(int) msqid;
    943 		syscallarg(void *) msgp;
    944 		syscallarg(size_t) msgsz;
    945 		syscallarg(long) msgtyp;
    946 		syscallarg(int) msgflg;
    947 	} */
    948 
    949 	return msgrcv1(l, SCARG(uap, msqid), SCARG(uap, msgp),
    950 	    SCARG(uap, msgsz), SCARG(uap, msgtyp), SCARG(uap, msgflg),
    951 	    sizeof(long), copyout, retval);
    952 }
    953 
    954 int
    955 msgrcv1(struct lwp *l, int msqidr, char *user_msgp, size_t msgsz, long msgtyp,
    956     int msgflg, size_t typesz, copyout_t put_type, register_t *retval)
    957 {
    958 	size_t len;
    959 	kauth_cred_t cred = l->l_cred;
    960 	struct msqid_ds *msqptr;
    961 	struct __msg *msghdr;
    962 	int error = 0, msqid;
    963 	kmsq_t *msq;
    964 	short next;
    965 
    966 	MSG_PRINTF(("call to msgrcv(%d, %p, %lld, %ld, %d)\n", msqid,
    967 	    user_msgp, (long long)msgsz, msgtyp, msgflg));
    968 restart:
    969 	msqid = IPCID_TO_IX(msqidr);
    970 
    971 	mutex_enter(&msgmutex);
    972 	/* In case of reallocation, we will wait for completion */
    973 	while (__predict_false(msg_realloc_state))
    974 		cv_wait(&msg_realloc_cv, &msgmutex);
    975 
    976 	if (msqid < 0 || msqid >= msginfo.msgmni) {
    977 		MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
    978 		    msginfo.msgmni));
    979 		error = EINVAL;
    980 		goto unlock;
    981 	}
    982 
    983 	msq = &msqs[msqid];
    984 	msqptr = &msq->msq_u;
    985 
    986 	if (msqptr->msg_qbytes == 0) {
    987 		MSG_PRINTF(("no such message queue id\n"));
    988 		error = EINVAL;
    989 		goto unlock;
    990 	}
    991 	if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
    992 		MSG_PRINTF(("wrong sequence number\n"));
    993 		error = EINVAL;
    994 		goto unlock;
    995 	}
    996 
    997 	if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
    998 		MSG_PRINTF(("requester doesn't have read access\n"));
    999 		goto unlock;
   1000 	}
   1001 
   1002 	msghdr = NULL;
   1003 	while (msghdr == NULL) {
   1004 		if (msgtyp == 0) {
   1005 			msghdr = msqptr->_msg_first;
   1006 			if (msghdr != NULL) {
   1007 				if (msgsz < msghdr->msg_ts &&
   1008 				    (msgflg & MSG_NOERROR) == 0) {
   1009 					MSG_PRINTF(("first msg on the queue "
   1010 					    "is too big (want %lld, got %d)\n",
   1011 					    (long long)msgsz, msghdr->msg_ts));
   1012 					error = E2BIG;
   1013 					goto unlock;
   1014 				}
   1015 				if (msqptr->_msg_first == msqptr->_msg_last) {
   1016 					msqptr->_msg_first = NULL;
   1017 					msqptr->_msg_last = NULL;
   1018 				} else {
   1019 					msqptr->_msg_first = msghdr->msg_next;
   1020 					KASSERT(msqptr->_msg_first != NULL);
   1021 				}
   1022 			}
   1023 		} else {
   1024 			struct __msg *previous;
   1025 			struct __msg **prev;
   1026 
   1027 			for (previous = NULL, prev = &msqptr->_msg_first;
   1028 			     (msghdr = *prev) != NULL;
   1029 			     previous = msghdr, prev = &msghdr->msg_next) {
   1030 				/*
   1031 				 * Is this message's type an exact match or is
   1032 				 * this message's type less than or equal to
   1033 				 * the absolute value of a negative msgtyp?
   1034 				 * Note that the second half of this test can
   1035 				 * NEVER be true if msgtyp is positive since
   1036 				 * msg_type is always positive!
   1037 				 */
   1038 
   1039 				if (msgtyp != msghdr->msg_type &&
   1040 				    msghdr->msg_type > -msgtyp)
   1041 					continue;
   1042 
   1043 				MSG_PRINTF(("found message type %ld, requested %ld\n",
   1044 				    msghdr->msg_type, msgtyp));
   1045 				if (msgsz < msghdr->msg_ts &&
   1046 				     (msgflg & MSG_NOERROR) == 0) {
   1047 					MSG_PRINTF(("requested message on the queue "
   1048 					    "is too big (want %lld, got %d)\n",
   1049 					    (long long)msgsz, msghdr->msg_ts));
   1050 					error = E2BIG;
   1051 					goto unlock;
   1052 				}
   1053 				*prev = msghdr->msg_next;
   1054 				if (msghdr != msqptr->_msg_last)
   1055 					break;
   1056 				if (previous == NULL) {
   1057 					KASSERT(prev == &msqptr->_msg_first);
   1058 					msqptr->_msg_first = NULL;
   1059 					msqptr->_msg_last = NULL;
   1060 				} else {
   1061 					KASSERT(prev != &msqptr->_msg_first);
   1062 					msqptr->_msg_last = previous;
   1063 				}
   1064 				break;
   1065 			}
   1066 		}
   1067 
   1068 		/*
   1069 		 * We've either extracted the msghdr for the appropriate
   1070 		 * message or there isn't one.
   1071 		 * If there is one then bail out of this loop.
   1072 		 */
   1073 		if (msghdr != NULL)
   1074 			break;
   1075 
   1076 		/*
   1077 		 * Hmph!  No message found.  Does the user want to wait?
   1078 		 */
   1079 
   1080 		if ((msgflg & IPC_NOWAIT) != 0) {
   1081 			MSG_PRINTF(("no appropriate message found (msgtyp=%ld)\n",
   1082 			    msgtyp));
   1083 			error = ENOMSG;
   1084 			goto unlock;
   1085 		}
   1086 
   1087 		/*
   1088 		 * Wait for something to happen
   1089 		 */
   1090 
   1091 		msg_waiters++;
   1092 		MSG_PRINTF(("msgrcv:  goodnight\n"));
   1093 		error = cv_wait_sig(&msq->msq_cv, &msgmutex);
   1094 		MSG_PRINTF(("msgrcv: good morning (error=%d)\n", error));
   1095 		msg_waiters--;
   1096 
   1097 		/*
   1098 		 * In case of such state, notify reallocator and
   1099 		 * restart the call.
   1100 		 */
   1101 		if (msg_realloc_state) {
   1102 			cv_broadcast(&msg_realloc_cv);
   1103 			mutex_exit(&msgmutex);
   1104 			goto restart;
   1105 		}
   1106 
   1107 		if (error != 0) {
   1108 			MSG_PRINTF(("msgsnd: interrupted system call\n"));
   1109 			error = EINTR;
   1110 			goto unlock;
   1111 		}
   1112 
   1113 		/*
   1114 		 * Make sure that the msq queue still exists
   1115 		 */
   1116 
   1117 		if (msqptr->msg_qbytes == 0 ||
   1118 		    msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
   1119 			MSG_PRINTF(("msqid deleted\n"));
   1120 			error = EIDRM;
   1121 			goto unlock;
   1122 		}
   1123 	}
   1124 
   1125 	/*
   1126 	 * Return the message to the user.
   1127 	 *
   1128 	 * First, do the bookkeeping (before we risk being interrupted).
   1129 	 */
   1130 
   1131 	msqptr->_msg_cbytes -= msghdr->msg_ts;
   1132 	msqptr->msg_qnum--;
   1133 	msqptr->msg_lrpid = l->l_proc->p_pid;
   1134 	msqptr->msg_rtime = time_second;
   1135 
   1136 	/*
   1137 	 * Make msgsz the actual amount that we'll be returning.
   1138 	 * Note that this effectively truncates the message if it is too long
   1139 	 * (since msgsz is never increased).
   1140 	 */
   1141 
   1142 	MSG_PRINTF(("found a message, msgsz=%lld, msg_ts=%d\n",
   1143 	    (long long)msgsz, msghdr->msg_ts));
   1144 	if (msgsz > msghdr->msg_ts)
   1145 		msgsz = msghdr->msg_ts;
   1146 
   1147 	/*
   1148 	 * Return the type to the user.
   1149 	 */
   1150 	mutex_exit(&msgmutex);
   1151 	error = (*put_type)(&msghdr->msg_type, user_msgp, typesz);
   1152 	mutex_enter(&msgmutex);
   1153 	if (error != 0) {
   1154 		MSG_PRINTF(("error (%d) copying out message type\n", error));
   1155 		msg_freehdr(msghdr);
   1156 		cv_broadcast(&msq->msq_cv);
   1157 		goto unlock;
   1158 	}
   1159 	user_msgp += typesz;
   1160 
   1161 	/*
   1162 	 * Return the segments to the user
   1163 	 */
   1164 
   1165 	next = msghdr->msg_spot;
   1166 	for (len = 0; len < msgsz; len += msginfo.msgssz) {
   1167 		size_t tlen;
   1168 		KASSERT(next > -1);
   1169 		KASSERT(next < msginfo.msgseg);
   1170 
   1171 		if (msgsz - len > msginfo.msgssz)
   1172 			tlen = msginfo.msgssz;
   1173 		else
   1174 			tlen = msgsz - len;
   1175 		mutex_exit(&msgmutex);
   1176 		error = (*put_type)(&msgpool[next * msginfo.msgssz],
   1177 		    user_msgp, tlen);
   1178 		mutex_enter(&msgmutex);
   1179 		if (error != 0) {
   1180 			MSG_PRINTF(("error (%d) copying out message segment\n",
   1181 			    error));
   1182 			msg_freehdr(msghdr);
   1183 			cv_broadcast(&msq->msq_cv);
   1184 			goto unlock;
   1185 		}
   1186 		user_msgp += tlen;
   1187 		next = msgmaps[next].next;
   1188 	}
   1189 
   1190 	/*
   1191 	 * Done, return the actual number of bytes copied out.
   1192 	 */
   1193 
   1194 	msg_freehdr(msghdr);
   1195 	cv_broadcast(&msq->msq_cv);
   1196 	*retval = msgsz;
   1197 
   1198 unlock:
   1199 	mutex_exit(&msgmutex);
   1200 	return error;
   1201 }
   1202 
   1203 /*
   1204  * Sysctl initialization and nodes.
   1205  */
   1206 
   1207 static int
   1208 sysctl_ipc_msgmni(SYSCTLFN_ARGS)
   1209 {
   1210 	int newsize, error;
   1211 	struct sysctlnode node;
   1212 	node = *rnode;
   1213 	node.sysctl_data = &newsize;
   1214 
   1215 	newsize = msginfo.msgmni;
   1216 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1217 	if (error || newp == NULL)
   1218 		return error;
   1219 
   1220 	sysctl_unlock();
   1221 	error = msgrealloc(newsize, msginfo.msgseg);
   1222 	sysctl_relock();
   1223 	return error;
   1224 }
   1225 
   1226 static int
   1227 sysctl_ipc_msgseg(SYSCTLFN_ARGS)
   1228 {
   1229 	int newsize, error;
   1230 	struct sysctlnode node;
   1231 	node = *rnode;
   1232 	node.sysctl_data = &newsize;
   1233 
   1234 	newsize = msginfo.msgseg;
   1235 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1236 	if (error || newp == NULL)
   1237 		return error;
   1238 
   1239 	sysctl_unlock();
   1240 	error = msgrealloc(msginfo.msgmni, newsize);
   1241 	sysctl_relock();
   1242 	return error;
   1243 }
   1244 
   1245 SYSCTL_SETUP(sysctl_ipc_msg_setup, "sysctl kern.ipc subtree setup")
   1246 {
   1247 	const struct sysctlnode *node = NULL;
   1248 
   1249 	sysctl_createv(clog, 0, NULL, NULL,
   1250 		CTLFLAG_PERMANENT,
   1251 		CTLTYPE_NODE, "kern", NULL,
   1252 		NULL, 0, NULL, 0,
   1253 		CTL_KERN, CTL_EOL);
   1254 	sysctl_createv(clog, 0, NULL, &node,
   1255 		CTLFLAG_PERMANENT,
   1256 		CTLTYPE_NODE, "ipc",
   1257 		SYSCTL_DESCR("SysV IPC options"),
   1258 		NULL, 0, NULL, 0,
   1259 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
   1260 
   1261 	if (node == NULL)
   1262 		return;
   1263 
   1264 	sysctl_createv(clog, 0, &node, NULL,
   1265 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1266 		CTLTYPE_INT, "msgmni",
   1267 		SYSCTL_DESCR("Max number of message queue identifiers"),
   1268 		sysctl_ipc_msgmni, 0, &msginfo.msgmni, 0,
   1269 		CTL_CREATE, CTL_EOL);
   1270 	sysctl_createv(clog, 0, &node, NULL,
   1271 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1272 		CTLTYPE_INT, "msgseg",
   1273 		SYSCTL_DESCR("Max number of number of message segments"),
   1274 		sysctl_ipc_msgseg, 0, &msginfo.msgseg, 0,
   1275 		CTL_CREATE, CTL_EOL);
   1276 }
   1277