xref: /titanic_51/usr/src/uts/common/os/msg.c (revision e50383f4b4a232c1643d12a8374521936b4fed9a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5b2eb1770Sudpa  * Common Development and Distribution License (the "License").
6b2eb1770Sudpa  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b2eb1770Sudpa  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Inter-Process Communication Message Facility.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * See os/ipc.c for a description of common IPC functionality.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Resource controls
387c478bd9Sstevel@tonic-gate  * -----------------
397c478bd9Sstevel@tonic-gate  *
40824c205fSml93401  * Control:      zone.max-msg-ids (rc_zone_msgmni)
41824c205fSml93401  * Description:  Maximum number of message queue ids allowed a zone.
42824c205fSml93401  *
43824c205fSml93401  *   When msgget() is used to allocate a message queue, one id is
44824c205fSml93401  *   allocated.  If the id allocation doesn't succeed, msgget() fails
45824c205fSml93401  *   and errno is set to ENOSPC.  Upon successful msgctl(, IPC_RMID)
46824c205fSml93401  *   the id is deallocated.
47824c205fSml93401  *
487c478bd9Sstevel@tonic-gate  * Control:      project.max-msg-ids (rc_project_msgmni)
497c478bd9Sstevel@tonic-gate  * Description:  Maximum number of message queue ids allowed a project.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *   When msgget() is used to allocate a message queue, one id is
527c478bd9Sstevel@tonic-gate  *   allocated.  If the id allocation doesn't succeed, msgget() fails
537c478bd9Sstevel@tonic-gate  *   and errno is set to ENOSPC.  Upon successful msgctl(, IPC_RMID)
547c478bd9Sstevel@tonic-gate  *   the id is deallocated.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Control:      process.max-msg-qbytes (rc_process_msgmnb)
577c478bd9Sstevel@tonic-gate  * Description:  Maximum number of bytes of messages on a message queue.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  *   When msgget() successfully allocates a message queue, the minimum
607c478bd9Sstevel@tonic-gate  *   enforced value of this limit is used to initialize msg_qbytes.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * Control:      process.max-msg-messages (rc_process_msgtql)
637c478bd9Sstevel@tonic-gate  * Description:  Maximum number of messages on a message queue.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  *   When msgget() successfully allocates a message queue, the minimum
667c478bd9Sstevel@tonic-gate  *   enforced value of this limit is used to initialize a per-queue
677c478bd9Sstevel@tonic-gate  *   limit on the number of messages.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/types.h>
717c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
727c478bd9Sstevel@tonic-gate #include <sys/param.h>
737c478bd9Sstevel@tonic-gate #include <sys/cred.h>
747c478bd9Sstevel@tonic-gate #include <sys/user.h>
757c478bd9Sstevel@tonic-gate #include <sys/proc.h>
767c478bd9Sstevel@tonic-gate #include <sys/time.h>
777c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
787c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
797c478bd9Sstevel@tonic-gate #include <sys/msg.h>
807c478bd9Sstevel@tonic-gate #include <sys/msg_impl.h>
817c478bd9Sstevel@tonic-gate #include <sys/list.h>
827c478bd9Sstevel@tonic-gate #include <sys/systm.h>
837c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
847c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
857c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
867c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
877c478bd9Sstevel@tonic-gate #include <sys/errno.h>
887c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
897c478bd9Sstevel@tonic-gate #include <sys/debug.h>
907c478bd9Sstevel@tonic-gate #include <sys/project.h>
917c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
927c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
937c478bd9Sstevel@tonic-gate #include <sys/policy.h>
947c478bd9Sstevel@tonic-gate #include <sys/zone.h>
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #include <c2/audit.h>
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * The following tunables are obsolete.  Though for compatibility we
1007c478bd9Sstevel@tonic-gate  * still read and interpret msginfo_msgmnb, msginfo_msgmni, and
1017c478bd9Sstevel@tonic-gate  * msginfo_msgtql (see os/project.c and os/rctl_proc.c), the preferred
1027c478bd9Sstevel@tonic-gate  * mechanism for administrating the IPC Message facility is through the
1037c478bd9Sstevel@tonic-gate  * resource controls described at the top of this file.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate size_t	msginfo_msgmax = 2048;	/* (obsolete) */
1067c478bd9Sstevel@tonic-gate size_t	msginfo_msgmnb = 4096;	/* (obsolete) */
1077c478bd9Sstevel@tonic-gate int	msginfo_msgmni = 50;	/* (obsolete) */
1087c478bd9Sstevel@tonic-gate int	msginfo_msgtql = 40;	/* (obsolete) */
1097c478bd9Sstevel@tonic-gate int	msginfo_msgssz = 8;	/* (obsolete) */
1107c478bd9Sstevel@tonic-gate int	msginfo_msgmap = 0;	/* (obsolete) */
1117c478bd9Sstevel@tonic-gate ushort_t msginfo_msgseg = 1024;	/* (obsolete) */
1127c478bd9Sstevel@tonic-gate 
113824c205fSml93401 extern rctl_hndl_t rc_zone_msgmni;
1147c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_project_msgmni;
1157c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_process_msgmnb;
1167c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_process_msgtql;
1177c478bd9Sstevel@tonic-gate static ipc_service_t *msq_svc;
1187c478bd9Sstevel@tonic-gate static zone_key_t msg_zone_key;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static void msg_dtor(kipc_perm_t *);
1217c478bd9Sstevel@tonic-gate static void msg_rmid(kipc_perm_t *);
1227c478bd9Sstevel@tonic-gate static void msg_remove_zone(zoneid_t, void *);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate static ssize_t msgsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2,
1287c478bd9Sstevel@tonic-gate 	uintptr_t a4, uintptr_t a5);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static struct sysent ipcmsg_sysent = {
1317c478bd9Sstevel@tonic-gate 	6,
1327c478bd9Sstevel@tonic-gate #ifdef	_LP64
1337c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_64RVAL,
1347c478bd9Sstevel@tonic-gate #else
1357c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1367c478bd9Sstevel@tonic-gate #endif
1377c478bd9Sstevel@tonic-gate 	(int (*)())msgsys
1387c478bd9Sstevel@tonic-gate };
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1417c478bd9Sstevel@tonic-gate static ssize32_t msgsys32(int opcode, uint32_t a0, uint32_t a1, uint32_t a2,
1427c478bd9Sstevel@tonic-gate 	uint32_t a4, uint32_t a5);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static struct sysent ipcmsg_sysent32 = {
1457c478bd9Sstevel@tonic-gate 	6,
1467c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1477c478bd9Sstevel@tonic-gate 	(int (*)())msgsys32
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
1527c478bd9Sstevel@tonic-gate 	&mod_syscallops, "System V message facility", &ipcmsg_sysent
1537c478bd9Sstevel@tonic-gate };
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1567c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
1577c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit System V message facility", &ipcmsg_sysent32
1587c478bd9Sstevel@tonic-gate };
1597c478bd9Sstevel@tonic-gate #endif
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1627c478bd9Sstevel@tonic-gate 	MODREV_1,
1637c478bd9Sstevel@tonic-gate 	&modlsys,
1647c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1657c478bd9Sstevel@tonic-gate 	&modlsys32,
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate 	NULL
1687c478bd9Sstevel@tonic-gate };
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate int
1727c478bd9Sstevel@tonic-gate _init(void)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	int result;
1757c478bd9Sstevel@tonic-gate 
176824c205fSml93401 	msq_svc = ipcs_create("msqids", rc_project_msgmni, rc_zone_msgmni,
177824c205fSml93401 	    sizeof (kmsqid_t), msg_dtor, msg_rmid, AT_IPC_MSG,
178824c205fSml93401 	    offsetof(ipc_rqty_t, ipcq_msgmni));
1797c478bd9Sstevel@tonic-gate 	zone_key_create(&msg_zone_key, NULL, msg_remove_zone, NULL);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if ((result = mod_install(&modlinkage)) == 0)
1827c478bd9Sstevel@tonic-gate 		return (0);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	(void) zone_key_delete(msg_zone_key);
1857c478bd9Sstevel@tonic-gate 	ipcs_destroy(msq_svc);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (result);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate int
1917c478bd9Sstevel@tonic-gate _fini(void)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	return (EBUSY);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate int
1977c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate static void
2037c478bd9Sstevel@tonic-gate msg_dtor(kipc_perm_t *perm)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	kmsqid_t *qp = (kmsqid_t *)perm;
206b2eb1770Sudpa 	int		ii;
2077c478bd9Sstevel@tonic-gate 
208b2eb1770Sudpa 	for (ii = 0; ii < MAX_QNUM_CV; ii++)
209b2eb1770Sudpa 		ASSERT(qp->msg_rcv_cnt[ii] == 0);
2107c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_snd_cnt == 0);
2117c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_cbytes == 0);
2127c478bd9Sstevel@tonic-gate 	list_destroy(&qp->msg_list);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate #define	msg_hold(mp)	(mp)->msg_copycnt++
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * msg_rele - decrement the reference count on the message.  When count
2207c478bd9Sstevel@tonic-gate  * reaches zero, free message header and contents.
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate static void
2237c478bd9Sstevel@tonic-gate msg_rele(struct msg *mp)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	ASSERT(mp->msg_copycnt > 0);
2267c478bd9Sstevel@tonic-gate 	if (mp->msg_copycnt-- == 1) {
2277c478bd9Sstevel@tonic-gate 		if (mp->msg_addr)
2287c478bd9Sstevel@tonic-gate 			kmem_free(mp->msg_addr, mp->msg_size);
2297c478bd9Sstevel@tonic-gate 		kmem_free(mp, sizeof (struct msg));
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * msgunlink - Unlink msg from queue, decrement byte count and wake up anyone
2357c478bd9Sstevel@tonic-gate  * waiting for free bytes on queue.
2367c478bd9Sstevel@tonic-gate  *
2377c478bd9Sstevel@tonic-gate  * Called with queue locked.
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate static void
2407c478bd9Sstevel@tonic-gate msgunlink(kmsqid_t *qp, struct msg *mp)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	list_remove(&qp->msg_list, mp);
2437c478bd9Sstevel@tonic-gate 	qp->msg_qnum--;
2447c478bd9Sstevel@tonic-gate 	qp->msg_cbytes -= mp->msg_size;
2457c478bd9Sstevel@tonic-gate 	msg_rele(mp);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/* Wake up waiting writers */
2487c478bd9Sstevel@tonic-gate 	if (qp->msg_snd_cnt)
2497c478bd9Sstevel@tonic-gate 		cv_broadcast(&qp->msg_snd_cv);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static void
2537c478bd9Sstevel@tonic-gate msg_rmid(kipc_perm_t *perm)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	kmsqid_t *qp = (kmsqid_t *)perm;
2567c478bd9Sstevel@tonic-gate 	struct msg *mp;
257b2eb1770Sudpa 	int		ii;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	while ((mp = list_head(&qp->msg_list)) != NULL)
2617c478bd9Sstevel@tonic-gate 		msgunlink(qp, mp);
2627c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_cbytes == 0);
2637c478bd9Sstevel@tonic-gate 
264b2eb1770Sudpa 	for (ii = 0; ii < MAX_QNUM_CV; ii++) {
265b2eb1770Sudpa 		if (qp->msg_rcv_cnt[ii])
266b2eb1770Sudpa 			cv_broadcast(&qp->msg_rcv_cv[ii]);
267b2eb1770Sudpa 	}
2687c478bd9Sstevel@tonic-gate 	if (qp->msg_snd_cnt)
2697c478bd9Sstevel@tonic-gate 		cv_broadcast(&qp->msg_snd_cv);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * msgctl system call.
2747c478bd9Sstevel@tonic-gate  *
2757c478bd9Sstevel@tonic-gate  * gets q lock (via ipc_lookup), releases before return.
2767c478bd9Sstevel@tonic-gate  * may call users of msg_lock
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate static int
2797c478bd9Sstevel@tonic-gate msgctl(int msgid, int cmd, void *arg)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	STRUCT_DECL(msqid_ds, ds);		/* SVR4 queue work area */
2827c478bd9Sstevel@tonic-gate 	kmsqid_t		*qp;		/* ptr to associated q */
283b2eb1770Sudpa 	int			error, ii;
2847c478bd9Sstevel@tonic-gate 	struct	cred		*cr;
2857c478bd9Sstevel@tonic-gate 	model_t	mdl = get_udatamodel();
2867c478bd9Sstevel@tonic-gate 	struct msqid_ds64	ds64;
2877c478bd9Sstevel@tonic-gate 	kmutex_t		*lock;
2887c478bd9Sstevel@tonic-gate 	proc_t			*pp = curproc;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	STRUCT_INIT(ds, mdl);
2917c478bd9Sstevel@tonic-gate 	cr = CRED();
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * Perform pre- or non-lookup actions (e.g. copyins, RMID).
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	switch (cmd) {
2977c478bd9Sstevel@tonic-gate 	case IPC_SET:
2987c478bd9Sstevel@tonic-gate 		if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds)))
2997c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3007c478bd9Sstevel@tonic-gate 		break;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	case IPC_SET64:
3037c478bd9Sstevel@tonic-gate 		if (copyin(arg, &ds64, sizeof (struct msqid_ds64)))
3047c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3057c478bd9Sstevel@tonic-gate 		break;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	case IPC_RMID:
3087c478bd9Sstevel@tonic-gate 		if (error = ipc_rmid(msq_svc, msgid, cr))
3097c478bd9Sstevel@tonic-gate 			return (set_errno(error));
3107c478bd9Sstevel@tonic-gate 		return (0);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * get msqid_ds for this msgid
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(msq_svc, msgid, (kipc_perm_t **)&qp)) == NULL)
3177c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	switch (cmd) {
3207c478bd9Sstevel@tonic-gate 	case IPC_SET:
3217c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(ds, msg_qbytes) > qp->msg_qbytes &&
3227c478bd9Sstevel@tonic-gate 		    secpolicy_ipc_config(cr) != 0) {
3237c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3247c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		if (error = ipcperm_set(msq_svc, cr, &qp->msg_perm,
3277c478bd9Sstevel@tonic-gate 		    &STRUCT_BUF(ds)->msg_perm, mdl)) {
3287c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3297c478bd9Sstevel@tonic-gate 			return (set_errno(error));
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 		qp->msg_qbytes = STRUCT_FGET(ds, msg_qbytes);
3327c478bd9Sstevel@tonic-gate 		qp->msg_ctime = gethrestime_sec();
3337c478bd9Sstevel@tonic-gate 		break;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	case IPC_STAT:
3367c478bd9Sstevel@tonic-gate 		if (error = ipcperm_access(&qp->msg_perm, MSG_R, cr)) {
3377c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3387c478bd9Sstevel@tonic-gate 			return (set_errno(error));
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
341b2eb1770Sudpa 		for (ii = 0; ii < MAX_QNUM_CV; ii++) {
342b2eb1770Sudpa 			if (qp->msg_rcv_cnt[ii]) {
3437c478bd9Sstevel@tonic-gate 				qp->msg_perm.ipc_mode |= MSG_RWAIT;
344b2eb1770Sudpa 				break;
345b2eb1770Sudpa 			}
346b2eb1770Sudpa 		}
3477c478bd9Sstevel@tonic-gate 		if (qp->msg_snd_cnt)
3487c478bd9Sstevel@tonic-gate 			qp->msg_perm.ipc_mode |= MSG_WWAIT;
3497c478bd9Sstevel@tonic-gate 		ipcperm_stat(&STRUCT_BUF(ds)->msg_perm, &qp->msg_perm, mdl);
3507c478bd9Sstevel@tonic-gate 		qp->msg_perm.ipc_mode &= ~(MSG_RWAIT|MSG_WWAIT);
3517c478bd9Sstevel@tonic-gate 		STRUCT_FSETP(ds, msg_first, NULL); 	/* kernel addr */
3527c478bd9Sstevel@tonic-gate 		STRUCT_FSETP(ds, msg_last, NULL);
3537c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_cbytes, qp->msg_cbytes);
3547c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_qnum, qp->msg_qnum);
3557c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_qbytes, qp->msg_qbytes);
3567c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_lspid, qp->msg_lspid);
3577c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_lrpid, qp->msg_lrpid);
3587c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_stime, qp->msg_stime);
3597c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_rtime, qp->msg_rtime);
3607c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_ctime, qp->msg_ctime);
3617c478bd9Sstevel@tonic-gate 		break;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	case IPC_SET64:
3647c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
3657c478bd9Sstevel@tonic-gate 		if ((ds64.msgx_qbytes > qp->msg_qbytes) &&
3667c478bd9Sstevel@tonic-gate 		    secpolicy_ipc_config(cr) != 0 &&
3677c478bd9Sstevel@tonic-gate 		    rctl_test(rc_process_msgmnb, pp->p_rctls, pp,
3687c478bd9Sstevel@tonic-gate 		    ds64.msgx_qbytes, RCA_SAFE) & RCT_DENY) {
3697c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
3707c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3717c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
3747c478bd9Sstevel@tonic-gate 		if (error = ipcperm_set64(msq_svc, cr, &qp->msg_perm,
3757c478bd9Sstevel@tonic-gate 		    &ds64.msgx_perm)) {
3767c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3777c478bd9Sstevel@tonic-gate 			return (set_errno(error));
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 		qp->msg_qbytes = ds64.msgx_qbytes;
3807c478bd9Sstevel@tonic-gate 		qp->msg_ctime = gethrestime_sec();
3817c478bd9Sstevel@tonic-gate 		break;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	case IPC_STAT64:
384b2eb1770Sudpa 		for (ii = 0; ii < MAX_QNUM_CV; ii++) {
385b2eb1770Sudpa 			if (qp->msg_rcv_cnt[ii]) {
3867c478bd9Sstevel@tonic-gate 				qp->msg_perm.ipc_mode |= MSG_RWAIT;
387b2eb1770Sudpa 				break;
388b2eb1770Sudpa 			}
389b2eb1770Sudpa 		}
3907c478bd9Sstevel@tonic-gate 		if (qp->msg_snd_cnt)
3917c478bd9Sstevel@tonic-gate 			qp->msg_perm.ipc_mode |= MSG_WWAIT;
3927c478bd9Sstevel@tonic-gate 		ipcperm_stat64(&ds64.msgx_perm, &qp->msg_perm);
3937c478bd9Sstevel@tonic-gate 		qp->msg_perm.ipc_mode &= ~(MSG_RWAIT|MSG_WWAIT);
3947c478bd9Sstevel@tonic-gate 		ds64.msgx_cbytes = qp->msg_cbytes;
3957c478bd9Sstevel@tonic-gate 		ds64.msgx_qnum = qp->msg_qnum;
3967c478bd9Sstevel@tonic-gate 		ds64.msgx_qbytes = qp->msg_qbytes;
3977c478bd9Sstevel@tonic-gate 		ds64.msgx_lspid = qp->msg_lspid;
3987c478bd9Sstevel@tonic-gate 		ds64.msgx_lrpid = qp->msg_lrpid;
3997c478bd9Sstevel@tonic-gate 		ds64.msgx_stime = qp->msg_stime;
4007c478bd9Sstevel@tonic-gate 		ds64.msgx_rtime = qp->msg_rtime;
4017c478bd9Sstevel@tonic-gate 		ds64.msgx_ctime = qp->msg_ctime;
4027c478bd9Sstevel@tonic-gate 		break;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	default:
4057c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
4067c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * Do copyout last (after releasing mutex).
4137c478bd9Sstevel@tonic-gate 	 */
4147c478bd9Sstevel@tonic-gate 	switch (cmd) {
4157c478bd9Sstevel@tonic-gate 	case IPC_STAT:
4167c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds)))
4177c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
4187c478bd9Sstevel@tonic-gate 		break;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	case IPC_STAT64:
4217c478bd9Sstevel@tonic-gate 		if (copyout(&ds64, arg, sizeof (struct msqid_ds64)))
4227c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
4237c478bd9Sstevel@tonic-gate 		break;
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	return (0);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate /*
4307c478bd9Sstevel@tonic-gate  * Remove all message queues associated with a given zone.  Called by
4317c478bd9Sstevel@tonic-gate  * zone_shutdown when the zone is halted.
4327c478bd9Sstevel@tonic-gate  */
4337c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
4347c478bd9Sstevel@tonic-gate static void
4357c478bd9Sstevel@tonic-gate msg_remove_zone(zoneid_t zoneid, void *arg)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	ipc_remove_zone(msq_svc, zoneid);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * msgget system call.
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate static int
4447c478bd9Sstevel@tonic-gate msgget(key_t key, int msgflg)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;
4477c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
4487c478bd9Sstevel@tonic-gate 	int		id, error;
449b2eb1770Sudpa 	int		ii;
4507c478bd9Sstevel@tonic-gate 	proc_t		*pp = curproc;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate top:
4537c478bd9Sstevel@tonic-gate 	if (error = ipc_get(msq_svc, key, msgflg, (kipc_perm_t **)&qp, &lock))
4547c478bd9Sstevel@tonic-gate 		return (set_errno(error));
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if (IPC_FREE(&qp->msg_perm)) {
4577c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
4587c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		list_create(&qp->msg_list, sizeof (struct msg),
4617c478bd9Sstevel@tonic-gate 		    offsetof(struct msg, msg_node));
4627c478bd9Sstevel@tonic-gate 		qp->msg_qnum = 0;
4637c478bd9Sstevel@tonic-gate 		qp->msg_lspid = qp->msg_lrpid = 0;
4647c478bd9Sstevel@tonic-gate 		qp->msg_stime = qp->msg_rtime = 0;
4657c478bd9Sstevel@tonic-gate 		qp->msg_ctime = gethrestime_sec();
466b2eb1770Sudpa 		for (ii = 0; ii < MAX_QNUM_CV; ii++)
467b2eb1770Sudpa 			qp->msg_rcv_cnt[ii] = 0;
468b2eb1770Sudpa 		qp->msg_snd_cnt = 0;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		if (error = ipc_commit_begin(msq_svc, key, msgflg,
4717c478bd9Sstevel@tonic-gate 		    (kipc_perm_t *)qp)) {
4727c478bd9Sstevel@tonic-gate 			if (error == EAGAIN)
4737c478bd9Sstevel@tonic-gate 				goto top;
4747c478bd9Sstevel@tonic-gate 			return (set_errno(error));
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 		qp->msg_qbytes = rctl_enforced_value(rc_process_msgmnb,
4777c478bd9Sstevel@tonic-gate 		    pp->p_rctls, pp);
4787c478bd9Sstevel@tonic-gate 		qp->msg_qmax = rctl_enforced_value(rc_process_msgtql,
4797c478bd9Sstevel@tonic-gate 		    pp->p_rctls, pp);
4807c478bd9Sstevel@tonic-gate 		lock = ipc_commit_end(msq_svc, &qp->msg_perm);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
4837c478bd9Sstevel@tonic-gate 	if (audit_active)
4847c478bd9Sstevel@tonic-gate 		audit_ipcget(AT_IPC_MSG, (void *)qp);
4857c478bd9Sstevel@tonic-gate #endif
4867c478bd9Sstevel@tonic-gate 	id = qp->msg_perm.ipc_id;
4877c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
4887c478bd9Sstevel@tonic-gate 	return (id);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate  * msgrcv system call.
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate static ssize_t
4957c478bd9Sstevel@tonic-gate msgrcv(int msqid, struct ipcmsgbuf *msgp, size_t msgsz, long msgtyp, int msgflg)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	struct msg	*mp;	/* ptr to msg on q */
4987c478bd9Sstevel@tonic-gate 	struct msg	*smp;	/* ptr to best msg on q */
4997c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;	/* ptr to associated q */
5007c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
5017c478bd9Sstevel@tonic-gate 	size_t		xtsz;	/* transfer byte count */
5027c478bd9Sstevel@tonic-gate 	int		error = 0, copyerror = 0;
5037c478bd9Sstevel@tonic-gate 	int		cvres;
5047c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(ipcmsgbuf, umsgp);
5057c478bd9Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, msg, 1);	/* bump msg send/rcv count */
5087c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(umsgp, mdl, msgp);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL)
5117c478bd9Sstevel@tonic-gate 		return ((ssize_t)set_errno(EINVAL));
5127c478bd9Sstevel@tonic-gate 	ipc_hold(msq_svc, (kipc_perm_t *)qp);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (error = ipcperm_access(&qp->msg_perm, MSG_R, CRED()))
5157c478bd9Sstevel@tonic-gate 		goto msgrcv_out;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate findmsg:
5187c478bd9Sstevel@tonic-gate 	smp = NULL;
5197c478bd9Sstevel@tonic-gate 	mp = list_head(&qp->msg_list);
5207c478bd9Sstevel@tonic-gate 	if (msgtyp == 0) {
5217c478bd9Sstevel@tonic-gate 		smp = mp;
5227c478bd9Sstevel@tonic-gate 	} else {
5237c478bd9Sstevel@tonic-gate 		for (; mp; mp = list_next(&qp->msg_list, mp)) {
5247c478bd9Sstevel@tonic-gate 			if (msgtyp > 0) {
5257c478bd9Sstevel@tonic-gate 				if (msgtyp != mp->msg_type)
5267c478bd9Sstevel@tonic-gate 					continue;
5277c478bd9Sstevel@tonic-gate 				smp = mp;
5287c478bd9Sstevel@tonic-gate 				break;
5297c478bd9Sstevel@tonic-gate 			}
5307c478bd9Sstevel@tonic-gate 			if (mp->msg_type <= -msgtyp) {
5317c478bd9Sstevel@tonic-gate 				if (smp && smp->msg_type <= mp->msg_type)
5327c478bd9Sstevel@tonic-gate 					continue;
5337c478bd9Sstevel@tonic-gate 				smp = mp;
5347c478bd9Sstevel@tonic-gate 			}
5357c478bd9Sstevel@tonic-gate 		}
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (smp) {
5397c478bd9Sstevel@tonic-gate 		/*
5407c478bd9Sstevel@tonic-gate 		 * Message found.
5417c478bd9Sstevel@tonic-gate 		 */
5427c478bd9Sstevel@tonic-gate 		if ((smp->msg_flags & MSG_RCVCOPY) == 0) {
5437c478bd9Sstevel@tonic-gate 			/*
5447c478bd9Sstevel@tonic-gate 			 * No one else is copying this message. Copy it.
5457c478bd9Sstevel@tonic-gate 			 */
5467c478bd9Sstevel@tonic-gate 			if (msgsz < smp->msg_size) {
5477c478bd9Sstevel@tonic-gate 				if ((msgflg & MSG_NOERROR) == 0) {
5487c478bd9Sstevel@tonic-gate 					error = E2BIG;
5497c478bd9Sstevel@tonic-gate 					goto msgrcv_out;
5507c478bd9Sstevel@tonic-gate 				} else {
5517c478bd9Sstevel@tonic-gate 					xtsz = msgsz;
5527c478bd9Sstevel@tonic-gate 				}
5537c478bd9Sstevel@tonic-gate 			} else {
5547c478bd9Sstevel@tonic-gate 				xtsz = smp->msg_size;
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 			/*
5587c478bd9Sstevel@tonic-gate 			 * Mark message as being copied out. Release mutex
5597c478bd9Sstevel@tonic-gate 			 * while copying out.
5607c478bd9Sstevel@tonic-gate 			 */
5617c478bd9Sstevel@tonic-gate 			ASSERT((smp->msg_flags & MSG_RCVCOPY) == 0);
5627c478bd9Sstevel@tonic-gate 			smp->msg_flags |= MSG_RCVCOPY;
5637c478bd9Sstevel@tonic-gate 			msg_hold(smp);
5647c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			if (mdl == DATAMODEL_NATIVE) {
5677c478bd9Sstevel@tonic-gate 				copyerror = copyout(&smp->msg_type, msgp,
5687c478bd9Sstevel@tonic-gate 				    sizeof (smp->msg_type));
5697c478bd9Sstevel@tonic-gate 			} else {
5707c478bd9Sstevel@tonic-gate 				/*
5717c478bd9Sstevel@tonic-gate 				 * 32-bit callers need an imploded msg type.
5727c478bd9Sstevel@tonic-gate 				 */
5737c478bd9Sstevel@tonic-gate 				int32_t	msg_type32 = smp->msg_type;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 				copyerror = copyout(&msg_type32, msgp,
5767c478bd9Sstevel@tonic-gate 				    sizeof (msg_type32));
5777c478bd9Sstevel@tonic-gate 			}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 			if (copyerror == 0 && xtsz)
5807c478bd9Sstevel@tonic-gate 				copyerror = copyout(smp->msg_addr,
5817c478bd9Sstevel@tonic-gate 				    STRUCT_FADDR(umsgp, mtext), xtsz);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 			/*
5847c478bd9Sstevel@tonic-gate 			 * Reclaim mutex, make sure queue still exists,
5857c478bd9Sstevel@tonic-gate 			 * and remove message.
5867c478bd9Sstevel@tonic-gate 			 */
5877c478bd9Sstevel@tonic-gate 			lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id);
5887c478bd9Sstevel@tonic-gate 			ASSERT(smp->msg_flags & MSG_RCVCOPY);
5897c478bd9Sstevel@tonic-gate 			smp->msg_flags &= ~MSG_RCVCOPY;
5907c478bd9Sstevel@tonic-gate 			msg_rele(smp);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 			if (IPC_FREE(&qp->msg_perm)) {
5937c478bd9Sstevel@tonic-gate 				error = EIDRM;
5947c478bd9Sstevel@tonic-gate 				goto msgrcv_out;
5957c478bd9Sstevel@tonic-gate 			}
596b2eb1770Sudpa 			/*
597b2eb1770Sudpa 			 * MSG_RCVCOPY was set while we dropped and reaquired
598b2eb1770Sudpa 			 * the lock. A thread looking for same message type
599b2eb1770Sudpa 			 * might have entered during that interval and seeing
600b2eb1770Sudpa 			 * MSG_RCVCOPY set, would have landed up in the sleepq.
601b2eb1770Sudpa 			 */
602b2eb1770Sudpa 			cv_broadcast(&qp->msg_rcv_cv[MSG_QNUM(smp->msg_type)]);
603b2eb1770Sudpa 			cv_broadcast(&qp->msg_rcv_cv[0]);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 			if (copyerror) {
6067c478bd9Sstevel@tonic-gate 				error = EFAULT;
6077c478bd9Sstevel@tonic-gate 				goto msgrcv_out;
6087c478bd9Sstevel@tonic-gate 			}
6097c478bd9Sstevel@tonic-gate 			qp->msg_lrpid = ttoproc(curthread)->p_pid;
6107c478bd9Sstevel@tonic-gate 			qp->msg_rtime = gethrestime_sec();
6117c478bd9Sstevel@tonic-gate 			msgunlink(qp, smp);
6127c478bd9Sstevel@tonic-gate 			goto msgrcv_out;
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	} else {
6167c478bd9Sstevel@tonic-gate 		/*
6177c478bd9Sstevel@tonic-gate 		 * No message found.
6187c478bd9Sstevel@tonic-gate 		 */
6197c478bd9Sstevel@tonic-gate 		if (msgflg & IPC_NOWAIT) {
6207c478bd9Sstevel@tonic-gate 			error = ENOMSG;
6217c478bd9Sstevel@tonic-gate 			goto msgrcv_out;
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/* Wait for new message */
626b2eb1770Sudpa 	qp->msg_rcv_cnt[MSG_QNUM(msgtyp)]++;
627b2eb1770Sudpa 	cvres = cv_wait_sig(&qp->msg_rcv_cv[MSG_QNUM(msgtyp)], lock);
6287c478bd9Sstevel@tonic-gate 	lock = ipc_relock(msq_svc, qp->msg_perm.ipc_id, lock);
629b2eb1770Sudpa 	qp->msg_rcv_cnt[MSG_QNUM(msgtyp)]--;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (IPC_FREE(&qp->msg_perm)) {
6327c478bd9Sstevel@tonic-gate 		error = EIDRM;
6337c478bd9Sstevel@tonic-gate 		goto msgrcv_out;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	if (cvres == 0) {
6367c478bd9Sstevel@tonic-gate 		error = EINTR;
6377c478bd9Sstevel@tonic-gate 		goto msgrcv_out;
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	goto findmsg;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate msgrcv_out:
6437c478bd9Sstevel@tonic-gate 	ipc_rele(msq_svc, (kipc_perm_t *)qp);
6447c478bd9Sstevel@tonic-gate 	if (error)
6457c478bd9Sstevel@tonic-gate 		return ((ssize_t)set_errno(error));
6467c478bd9Sstevel@tonic-gate 	return ((ssize_t)xtsz);
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate /*
6507c478bd9Sstevel@tonic-gate  * msgids system call.
6517c478bd9Sstevel@tonic-gate  */
6527c478bd9Sstevel@tonic-gate static int
6537c478bd9Sstevel@tonic-gate msgids(int *buf, uint_t nids, uint_t *pnids)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	int error;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (error = ipc_ids(msq_svc, buf, nids, pnids))
6587c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	return (0);
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate #define	RND(x)		roundup((x), sizeof (size_t))
6647c478bd9Sstevel@tonic-gate #define	RND32(x)	roundup((x), sizeof (size32_t))
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * msgsnap system call.
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate static int
6707c478bd9Sstevel@tonic-gate msgsnap(int msqid, caddr_t buf, size_t bufsz, long msgtyp)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	struct msg	*mp;	/* ptr to msg on q */
6737c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;	/* ptr to associated q */
6747c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
6757c478bd9Sstevel@tonic-gate 	size_t		size;
6767c478bd9Sstevel@tonic-gate 	size_t		nmsg;
6777c478bd9Sstevel@tonic-gate 	struct msg	**snaplist;
6787c478bd9Sstevel@tonic-gate 	int		error, i;
6797c478bd9Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
6807c478bd9Sstevel@tonic-gate 	STRUCT_DECL(msgsnap_head, head);
6817c478bd9Sstevel@tonic-gate 	STRUCT_DECL(msgsnap_mhead, mhead);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	STRUCT_INIT(head, mdl);
6847c478bd9Sstevel@tonic-gate 	STRUCT_INIT(mhead, mdl);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if (bufsz < STRUCT_SIZE(head))
6877c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL)
6907c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (error = ipcperm_access(&qp->msg_perm, MSG_R, CRED())) {
6937c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
6947c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	ipc_hold(msq_svc, (kipc_perm_t *)qp);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/*
6997c478bd9Sstevel@tonic-gate 	 * First compute the required buffer size and
7007c478bd9Sstevel@tonic-gate 	 * the number of messages on the queue.
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate 	size = nmsg = 0;
7037c478bd9Sstevel@tonic-gate 	for (mp = list_head(&qp->msg_list); mp;
7047c478bd9Sstevel@tonic-gate 	    mp = list_next(&qp->msg_list, mp)) {
7057c478bd9Sstevel@tonic-gate 		if (msgtyp == 0 ||
7067c478bd9Sstevel@tonic-gate 		    (msgtyp > 0 && msgtyp == mp->msg_type) ||
7077c478bd9Sstevel@tonic-gate 		    (msgtyp < 0 && mp->msg_type <= -msgtyp)) {
7087c478bd9Sstevel@tonic-gate 			nmsg++;
7097c478bd9Sstevel@tonic-gate 			if (mdl == DATAMODEL_NATIVE)
7107c478bd9Sstevel@tonic-gate 				size += RND(mp->msg_size);
7117c478bd9Sstevel@tonic-gate 			else
7127c478bd9Sstevel@tonic-gate 				size += RND32(mp->msg_size);
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	size += STRUCT_SIZE(head) + nmsg * STRUCT_SIZE(mhead);
7177c478bd9Sstevel@tonic-gate 	if (size > bufsz)
7187c478bd9Sstevel@tonic-gate 		nmsg = 0;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if (nmsg > 0) {
7217c478bd9Sstevel@tonic-gate 		/*
7227c478bd9Sstevel@tonic-gate 		 * Mark the messages as being copied.
7237c478bd9Sstevel@tonic-gate 		 */
7247c478bd9Sstevel@tonic-gate 		snaplist = (struct msg **)kmem_alloc(nmsg *
7257c478bd9Sstevel@tonic-gate 		    sizeof (struct msg *), KM_SLEEP);
7267c478bd9Sstevel@tonic-gate 		i = 0;
7277c478bd9Sstevel@tonic-gate 		for (mp = list_head(&qp->msg_list); mp;
7287c478bd9Sstevel@tonic-gate 		    mp = list_next(&qp->msg_list, mp)) {
7297c478bd9Sstevel@tonic-gate 			if (msgtyp == 0 ||
7307c478bd9Sstevel@tonic-gate 			    (msgtyp > 0 && msgtyp == mp->msg_type) ||
7317c478bd9Sstevel@tonic-gate 			    (msgtyp < 0 && mp->msg_type <= -msgtyp)) {
7327c478bd9Sstevel@tonic-gate 				msg_hold(mp);
7337c478bd9Sstevel@tonic-gate 				snaplist[i] = mp;
7347c478bd9Sstevel@tonic-gate 				i++;
7357c478bd9Sstevel@tonic-gate 			}
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	/*
7417c478bd9Sstevel@tonic-gate 	 * Copy out the buffer header.
7427c478bd9Sstevel@tonic-gate 	 */
7437c478bd9Sstevel@tonic-gate 	STRUCT_FSET(head, msgsnap_size, size);
7447c478bd9Sstevel@tonic-gate 	STRUCT_FSET(head, msgsnap_nmsg, nmsg);
7457c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(head), buf, STRUCT_SIZE(head)))
7467c478bd9Sstevel@tonic-gate 		error = EFAULT;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	buf += STRUCT_SIZE(head);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	/*
7517c478bd9Sstevel@tonic-gate 	 * Now copy out the messages one by one.
7527c478bd9Sstevel@tonic-gate 	 */
7537c478bd9Sstevel@tonic-gate 	for (i = 0; i < nmsg; i++) {
7547c478bd9Sstevel@tonic-gate 		mp = snaplist[i];
7557c478bd9Sstevel@tonic-gate 		if (error == 0) {
7567c478bd9Sstevel@tonic-gate 			STRUCT_FSET(mhead, msgsnap_mlen, mp->msg_size);
7577c478bd9Sstevel@tonic-gate 			STRUCT_FSET(mhead, msgsnap_mtype, mp->msg_type);
7587c478bd9Sstevel@tonic-gate 			if (copyout(STRUCT_BUF(mhead), buf, STRUCT_SIZE(mhead)))
7597c478bd9Sstevel@tonic-gate 				error = EFAULT;
7607c478bd9Sstevel@tonic-gate 			buf += STRUCT_SIZE(mhead);
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 			if (error == 0 &&
7637c478bd9Sstevel@tonic-gate 			    mp->msg_size != 0 &&
7647c478bd9Sstevel@tonic-gate 			    copyout(mp->msg_addr, buf, mp->msg_size))
7657c478bd9Sstevel@tonic-gate 				error = EFAULT;
7667c478bd9Sstevel@tonic-gate 			if (mdl == DATAMODEL_NATIVE)
7677c478bd9Sstevel@tonic-gate 				buf += RND(mp->msg_size);
7687c478bd9Sstevel@tonic-gate 			else
7697c478bd9Sstevel@tonic-gate 				buf += RND32(mp->msg_size);
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 		lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id);
7727c478bd9Sstevel@tonic-gate 		msg_rele(mp);
7737c478bd9Sstevel@tonic-gate 		/* Check for msg q deleted or reallocated */
7747c478bd9Sstevel@tonic-gate 		if (IPC_FREE(&qp->msg_perm))
7757c478bd9Sstevel@tonic-gate 			error = EIDRM;
7767c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	(void) ipc_lock(msq_svc, qp->msg_perm.ipc_id);
7807c478bd9Sstevel@tonic-gate 	ipc_rele(msq_svc, (kipc_perm_t *)qp);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (nmsg > 0)
7837c478bd9Sstevel@tonic-gate 		kmem_free(snaplist, nmsg * sizeof (struct msg *));
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	if (error)
7867c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7877c478bd9Sstevel@tonic-gate 	return (0);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
790*e50383f4Sdv142724 #define	MSG_PREALLOC_LIMIT 8192
791*e50383f4Sdv142724 
7927c478bd9Sstevel@tonic-gate /*
7937c478bd9Sstevel@tonic-gate  * msgsnd system call.
7947c478bd9Sstevel@tonic-gate  */
7957c478bd9Sstevel@tonic-gate static int
7967c478bd9Sstevel@tonic-gate msgsnd(int msqid, struct ipcmsgbuf *msgp, size_t msgsz, int msgflg)
7977c478bd9Sstevel@tonic-gate {
7987c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;
799*e50383f4Sdv142724 	kmutex_t	*lock = NULL;
8007c478bd9Sstevel@tonic-gate 	struct msg	*mp = NULL;
8017c478bd9Sstevel@tonic-gate 	long		type;
8027c478bd9Sstevel@tonic-gate 	int		error = 0;
8037c478bd9Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
8047c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(ipcmsgbuf, umsgp);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, msg, 1);	/* bump msg send/rcv count */
8077c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(umsgp, mdl, msgp);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	if (mdl == DATAMODEL_NATIVE) {
8107c478bd9Sstevel@tonic-gate 		if (copyin(msgp, &type, sizeof (type)))
8117c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
8127c478bd9Sstevel@tonic-gate 	} else {
8137c478bd9Sstevel@tonic-gate 		int32_t	type32;
8147c478bd9Sstevel@tonic-gate 		if (copyin(msgp, &type32, sizeof (type32)))
8157c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
8167c478bd9Sstevel@tonic-gate 		type = type32;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (type < 1)
8207c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8217c478bd9Sstevel@tonic-gate 
822*e50383f4Sdv142724 	/*
823*e50383f4Sdv142724 	 * We want the value here large enough that most of the
824*e50383f4Sdv142724 	 * the message operations will use the "lockless" path,
825*e50383f4Sdv142724 	 * but small enough that a user can not reserve large
826*e50383f4Sdv142724 	 * chunks of kernel memory unless they have a valid
827*e50383f4Sdv142724 	 * reason to.
828*e50383f4Sdv142724 	 */
829*e50383f4Sdv142724 	if (msgsz <= MSG_PREALLOC_LIMIT) {
830*e50383f4Sdv142724 		/*
831*e50383f4Sdv142724 		 * We are small enough that we can afford to do the
832*e50383f4Sdv142724 		 * allocation now.  This saves dropping the lock
833*e50383f4Sdv142724 		 * and then reacquiring the lock.
834*e50383f4Sdv142724 		 */
835*e50383f4Sdv142724 		mp = kmem_zalloc(sizeof (struct msg), KM_SLEEP);
836*e50383f4Sdv142724 		mp->msg_copycnt = 1;
837*e50383f4Sdv142724 		mp->msg_size = msgsz;
838*e50383f4Sdv142724 		if (msgsz) {
839*e50383f4Sdv142724 			mp->msg_addr = kmem_alloc(msgsz, KM_SLEEP);
840*e50383f4Sdv142724 			if (copyin(STRUCT_FADDR(umsgp, mtext),
841*e50383f4Sdv142724 			    mp->msg_addr, msgsz) == -1) {
842*e50383f4Sdv142724 				error = EFAULT;
843*e50383f4Sdv142724 				goto msgsnd_out;
844*e50383f4Sdv142724 			}
845*e50383f4Sdv142724 		}
846*e50383f4Sdv142724 	}
847*e50383f4Sdv142724 
848*e50383f4Sdv142724 	if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL) {
849*e50383f4Sdv142724 		error = EINVAL;
850*e50383f4Sdv142724 		goto msgsnd_out;
851*e50383f4Sdv142724 	}
852*e50383f4Sdv142724 
8537c478bd9Sstevel@tonic-gate 	ipc_hold(msq_svc, (kipc_perm_t *)qp);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (msgsz > qp->msg_qbytes) {
8567c478bd9Sstevel@tonic-gate 		error = EINVAL;
8577c478bd9Sstevel@tonic-gate 		goto msgsnd_out;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (error = ipcperm_access(&qp->msg_perm, MSG_W, CRED()))
8617c478bd9Sstevel@tonic-gate 		goto msgsnd_out;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate top:
8647c478bd9Sstevel@tonic-gate 	/*
8657c478bd9Sstevel@tonic-gate 	 * Allocate space on q, message header, & buffer space.
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_qnum <= qp->msg_qmax);
8687c478bd9Sstevel@tonic-gate 	while ((msgsz > qp->msg_qbytes - qp->msg_cbytes) ||
8697c478bd9Sstevel@tonic-gate 	    (qp->msg_qnum == qp->msg_qmax)) {
8707c478bd9Sstevel@tonic-gate 		int cvres;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		if (msgflg & IPC_NOWAIT) {
8737c478bd9Sstevel@tonic-gate 			error = EAGAIN;
8747c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 		qp->msg_snd_cnt++;
8787c478bd9Sstevel@tonic-gate 		cvres = cv_wait_sig(&qp->msg_snd_cv, lock);
8797c478bd9Sstevel@tonic-gate 		lock = ipc_relock(msq_svc, qp->msg_perm.ipc_id, lock);
8807c478bd9Sstevel@tonic-gate 		qp->msg_snd_cnt--;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		if (IPC_FREE(&qp->msg_perm)) {
8837c478bd9Sstevel@tonic-gate 			error = EIDRM;
8847c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 		if (cvres == 0) {
8887c478bd9Sstevel@tonic-gate 			error = EINTR;
8897c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
8947c478bd9Sstevel@tonic-gate 		int failure;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
897*e50383f4Sdv142724 		ASSERT(msgsz > 0);
8987c478bd9Sstevel@tonic-gate 		mp = kmem_zalloc(sizeof (struct msg), KM_SLEEP);
899*e50383f4Sdv142724 		mp->msg_addr = kmem_alloc(msgsz, KM_SLEEP);
9007c478bd9Sstevel@tonic-gate 		mp->msg_size = msgsz;
9017c478bd9Sstevel@tonic-gate 		mp->msg_copycnt = 1;
9027c478bd9Sstevel@tonic-gate 
903*e50383f4Sdv142724 		failure = (copyin(STRUCT_FADDR(umsgp, mtext),
9047c478bd9Sstevel@tonic-gate 		    mp->msg_addr, msgsz) == -1);
9057c478bd9Sstevel@tonic-gate 		lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id);
9067c478bd9Sstevel@tonic-gate 		if (IPC_FREE(&qp->msg_perm)) {
9077c478bd9Sstevel@tonic-gate 			error = EIDRM;
9087c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 		if (failure) {
9117c478bd9Sstevel@tonic-gate 			error = EFAULT;
9127c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
9137c478bd9Sstevel@tonic-gate 		}
9147c478bd9Sstevel@tonic-gate 		goto top;
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	/*
9187c478bd9Sstevel@tonic-gate 	 * Everything is available, put msg on q.
9197c478bd9Sstevel@tonic-gate 	 */
9207c478bd9Sstevel@tonic-gate 	qp->msg_qnum++;
9217c478bd9Sstevel@tonic-gate 	qp->msg_cbytes += msgsz;
9227c478bd9Sstevel@tonic-gate 	qp->msg_lspid = curproc->p_pid;
9237c478bd9Sstevel@tonic-gate 	qp->msg_stime = gethrestime_sec();
9247c478bd9Sstevel@tonic-gate 	mp->msg_type = type;
9257c478bd9Sstevel@tonic-gate 	mp->msg_flags = 0;
9267c478bd9Sstevel@tonic-gate 	list_insert_tail(&qp->msg_list, mp);
927b2eb1770Sudpa 	/*
928b2eb1770Sudpa 	 * For all message type >= 1.
929b2eb1770Sudpa 	 */
930b2eb1770Sudpa 	if (qp->msg_rcv_cnt[MSG_QNUM(type)])
931b2eb1770Sudpa 		cv_broadcast(&qp->msg_rcv_cv[MSG_QNUM(type)]);
932b2eb1770Sudpa 	/*
933b2eb1770Sudpa 	 * For all message type < 1.
934b2eb1770Sudpa 	 */
935b2eb1770Sudpa 	if (qp->msg_rcv_cnt[0])
936b2eb1770Sudpa 		cv_broadcast(&qp->msg_rcv_cv[0]);
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate msgsnd_out:
939*e50383f4Sdv142724 	if (lock)
9407c478bd9Sstevel@tonic-gate 		ipc_rele(msq_svc, (kipc_perm_t *)qp);	/* drops lock */
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	if (error) {
9437c478bd9Sstevel@tonic-gate 		if (mp)
9447c478bd9Sstevel@tonic-gate 			msg_rele(mp);
9457c478bd9Sstevel@tonic-gate 		return (set_errno(error));
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	return (0);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate  * msgsys - System entry point for msgctl, msgget, msgrcv, and msgsnd
9537c478bd9Sstevel@tonic-gate  * system calls.
9547c478bd9Sstevel@tonic-gate  */
9557c478bd9Sstevel@tonic-gate static ssize_t
9567c478bd9Sstevel@tonic-gate msgsys(int opcode, uintptr_t a1, uintptr_t a2, uintptr_t a3,
9577c478bd9Sstevel@tonic-gate 	uintptr_t a4, uintptr_t a5)
9587c478bd9Sstevel@tonic-gate {
9597c478bd9Sstevel@tonic-gate 	ssize_t error;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	switch (opcode) {
9627c478bd9Sstevel@tonic-gate 	case MSGGET:
9637c478bd9Sstevel@tonic-gate 		error = msgget((key_t)a1, (int)a2);
9647c478bd9Sstevel@tonic-gate 		break;
9657c478bd9Sstevel@tonic-gate 	case MSGCTL:
9667c478bd9Sstevel@tonic-gate 		error = msgctl((int)a1, (int)a2, (void *)a3);
9677c478bd9Sstevel@tonic-gate 		break;
9687c478bd9Sstevel@tonic-gate 	case MSGRCV:
9697c478bd9Sstevel@tonic-gate 		error = msgrcv((int)a1, (struct ipcmsgbuf *)a2,
9707c478bd9Sstevel@tonic-gate 		    (size_t)a3, (long)a4, (int)a5);
9717c478bd9Sstevel@tonic-gate 		break;
9727c478bd9Sstevel@tonic-gate 	case MSGSND:
9737c478bd9Sstevel@tonic-gate 		error = msgsnd((int)a1, (struct ipcmsgbuf *)a2,
9747c478bd9Sstevel@tonic-gate 		    (size_t)a3, (int)a4);
9757c478bd9Sstevel@tonic-gate 		break;
9767c478bd9Sstevel@tonic-gate 	case MSGIDS:
9777c478bd9Sstevel@tonic-gate 		error = msgids((int *)a1, (uint_t)a2, (uint_t *)a3);
9787c478bd9Sstevel@tonic-gate 		break;
9797c478bd9Sstevel@tonic-gate 	case MSGSNAP:
9807c478bd9Sstevel@tonic-gate 		error = msgsnap((int)a1, (caddr_t)a2, (size_t)a3, (long)a4);
9817c478bd9Sstevel@tonic-gate 		break;
9827c478bd9Sstevel@tonic-gate 	default:
9837c478bd9Sstevel@tonic-gate 		error = set_errno(EINVAL);
9847c478bd9Sstevel@tonic-gate 		break;
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	return (error);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
9917c478bd9Sstevel@tonic-gate /*
9927c478bd9Sstevel@tonic-gate  * msgsys32 - System entry point for msgctl, msgget, msgrcv, and msgsnd
9937c478bd9Sstevel@tonic-gate  * system calls for 32-bit callers on LP64 kernel.
9947c478bd9Sstevel@tonic-gate  */
9957c478bd9Sstevel@tonic-gate static ssize32_t
9967c478bd9Sstevel@tonic-gate msgsys32(int opcode, uint32_t a1, uint32_t a2, uint32_t a3,
9977c478bd9Sstevel@tonic-gate 	uint32_t a4, uint32_t a5)
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	ssize_t error;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	switch (opcode) {
10027c478bd9Sstevel@tonic-gate 	case MSGGET:
10037c478bd9Sstevel@tonic-gate 		error = msgget((key_t)a1, (int)a2);
10047c478bd9Sstevel@tonic-gate 		break;
10057c478bd9Sstevel@tonic-gate 	case MSGCTL:
10067c478bd9Sstevel@tonic-gate 		error = msgctl((int)a1, (int)a2, (void *)(uintptr_t)a3);
10077c478bd9Sstevel@tonic-gate 		break;
10087c478bd9Sstevel@tonic-gate 	case MSGRCV:
10097c478bd9Sstevel@tonic-gate 		error = msgrcv((int)a1, (struct ipcmsgbuf *)(uintptr_t)a2,
10107c478bd9Sstevel@tonic-gate 		    (size_t)a3, (long)(int32_t)a4, (int)a5);
10117c478bd9Sstevel@tonic-gate 		break;
10127c478bd9Sstevel@tonic-gate 	case MSGSND:
10137c478bd9Sstevel@tonic-gate 		error = msgsnd((int)a1, (struct ipcmsgbuf *)(uintptr_t)a2,
10147c478bd9Sstevel@tonic-gate 		    (size_t)(int32_t)a3, (int)a4);
10157c478bd9Sstevel@tonic-gate 		break;
10167c478bd9Sstevel@tonic-gate 	case MSGIDS:
10177c478bd9Sstevel@tonic-gate 		error = msgids((int *)(uintptr_t)a1, (uint_t)a2,
10187c478bd9Sstevel@tonic-gate 		    (uint_t *)(uintptr_t)a3);
10197c478bd9Sstevel@tonic-gate 		break;
10207c478bd9Sstevel@tonic-gate 	case MSGSNAP:
10217c478bd9Sstevel@tonic-gate 		error = msgsnap((int)a1, (caddr_t)(uintptr_t)a2, (size_t)a3,
10227c478bd9Sstevel@tonic-gate 		    (long)(int32_t)a4);
10237c478bd9Sstevel@tonic-gate 		break;
10247c478bd9Sstevel@tonic-gate 	default:
10257c478bd9Sstevel@tonic-gate 		error = set_errno(EINVAL);
10267c478bd9Sstevel@tonic-gate 		break;
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	return (error);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate #endif	/* SYSCALL32_IMPL */
1032