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