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