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