xref: /titanic_53/usr/src/uts/common/os/msg.c (revision b2eb1770e05377d3b46c691c5721f32915d57f58)
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
5*b2eb1770Sudpa  * Common Development and Distribution License (the "License").
6*b2eb1770Sudpa  * 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 /*
22*b2eb1770Sudpa  * 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  *
407c478bd9Sstevel@tonic-gate  * Control:      project.max-msg-ids (rc_project_msgmni)
417c478bd9Sstevel@tonic-gate  * Description:  Maximum number of message queue ids allowed a project.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  *   When msgget() is used to allocate a message queue, one id is
447c478bd9Sstevel@tonic-gate  *   allocated.  If the id allocation doesn't succeed, msgget() fails
457c478bd9Sstevel@tonic-gate  *   and errno is set to ENOSPC.  Upon successful msgctl(, IPC_RMID)
467c478bd9Sstevel@tonic-gate  *   the id is deallocated.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Control:      process.max-msg-qbytes (rc_process_msgmnb)
497c478bd9Sstevel@tonic-gate  * Description:  Maximum number of bytes of messages on a message queue.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *   When msgget() successfully allocates a message queue, the minimum
527c478bd9Sstevel@tonic-gate  *   enforced value of this limit is used to initialize msg_qbytes.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Control:      process.max-msg-messages (rc_process_msgtql)
557c478bd9Sstevel@tonic-gate  * Description:  Maximum number of messages on a message queue.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  *   When msgget() successfully allocates a message queue, the minimum
587c478bd9Sstevel@tonic-gate  *   enforced value of this limit is used to initialize a per-queue
597c478bd9Sstevel@tonic-gate  *   limit on the number of messages.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <sys/types.h>
637c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
647c478bd9Sstevel@tonic-gate #include <sys/param.h>
657c478bd9Sstevel@tonic-gate #include <sys/cred.h>
667c478bd9Sstevel@tonic-gate #include <sys/user.h>
677c478bd9Sstevel@tonic-gate #include <sys/proc.h>
687c478bd9Sstevel@tonic-gate #include <sys/time.h>
697c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
707c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
717c478bd9Sstevel@tonic-gate #include <sys/msg.h>
727c478bd9Sstevel@tonic-gate #include <sys/msg_impl.h>
737c478bd9Sstevel@tonic-gate #include <sys/list.h>
747c478bd9Sstevel@tonic-gate #include <sys/systm.h>
757c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
767c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
777c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
787c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
797c478bd9Sstevel@tonic-gate #include <sys/errno.h>
807c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
817c478bd9Sstevel@tonic-gate #include <sys/debug.h>
827c478bd9Sstevel@tonic-gate #include <sys/project.h>
837c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
847c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
857c478bd9Sstevel@tonic-gate #include <sys/policy.h>
867c478bd9Sstevel@tonic-gate #include <sys/zone.h>
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #include <c2/audit.h>
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * The following tunables are obsolete.  Though for compatibility we
927c478bd9Sstevel@tonic-gate  * still read and interpret msginfo_msgmnb, msginfo_msgmni, and
937c478bd9Sstevel@tonic-gate  * msginfo_msgtql (see os/project.c and os/rctl_proc.c), the preferred
947c478bd9Sstevel@tonic-gate  * mechanism for administrating the IPC Message facility is through the
957c478bd9Sstevel@tonic-gate  * resource controls described at the top of this file.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate size_t	msginfo_msgmax = 2048;	/* (obsolete) */
987c478bd9Sstevel@tonic-gate size_t	msginfo_msgmnb = 4096;	/* (obsolete) */
997c478bd9Sstevel@tonic-gate int	msginfo_msgmni = 50;	/* (obsolete) */
1007c478bd9Sstevel@tonic-gate int	msginfo_msgtql = 40;	/* (obsolete) */
1017c478bd9Sstevel@tonic-gate int	msginfo_msgssz = 8;	/* (obsolete) */
1027c478bd9Sstevel@tonic-gate int	msginfo_msgmap = 0;	/* (obsolete) */
1037c478bd9Sstevel@tonic-gate ushort_t msginfo_msgseg = 1024;	/* (obsolete) */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_project_msgmni;
1067c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_process_msgmnb;
1077c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_process_msgtql;
1087c478bd9Sstevel@tonic-gate static ipc_service_t *msq_svc;
1097c478bd9Sstevel@tonic-gate static zone_key_t msg_zone_key;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static void msg_dtor(kipc_perm_t *);
1127c478bd9Sstevel@tonic-gate static void msg_rmid(kipc_perm_t *);
1137c478bd9Sstevel@tonic-gate static void msg_remove_zone(zoneid_t, void *);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate static ssize_t msgsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2,
1197c478bd9Sstevel@tonic-gate 	uintptr_t a4, uintptr_t a5);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static struct sysent ipcmsg_sysent = {
1227c478bd9Sstevel@tonic-gate 	6,
1237c478bd9Sstevel@tonic-gate #ifdef	_LP64
1247c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_64RVAL,
1257c478bd9Sstevel@tonic-gate #else
1267c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 	(int (*)())msgsys
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1327c478bd9Sstevel@tonic-gate static ssize32_t msgsys32(int opcode, uint32_t a0, uint32_t a1, uint32_t a2,
1337c478bd9Sstevel@tonic-gate 	uint32_t a4, uint32_t a5);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static struct sysent ipcmsg_sysent32 = {
1367c478bd9Sstevel@tonic-gate 	6,
1377c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1387c478bd9Sstevel@tonic-gate 	(int (*)())msgsys32
1397c478bd9Sstevel@tonic-gate };
1407c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
1437c478bd9Sstevel@tonic-gate 	&mod_syscallops, "System V message facility", &ipcmsg_sysent
1447c478bd9Sstevel@tonic-gate };
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1477c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
1487c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit System V message facility", &ipcmsg_sysent32
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate #endif
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1537c478bd9Sstevel@tonic-gate 	MODREV_1,
1547c478bd9Sstevel@tonic-gate 	&modlsys,
1557c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1567c478bd9Sstevel@tonic-gate 	&modlsys32,
1577c478bd9Sstevel@tonic-gate #endif
1587c478bd9Sstevel@tonic-gate 	NULL
1597c478bd9Sstevel@tonic-gate };
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate int
1637c478bd9Sstevel@tonic-gate _init(void)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	int result;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	msq_svc = ipcs_create("msqids", rc_project_msgmni, sizeof (kmsqid_t),
1687c478bd9Sstevel@tonic-gate 	    msg_dtor, msg_rmid, AT_IPC_MSG,
1697c478bd9Sstevel@tonic-gate 	    offsetof(kproject_data_t, kpd_msgmni));
1707c478bd9Sstevel@tonic-gate 	zone_key_create(&msg_zone_key, NULL, msg_remove_zone, NULL);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if ((result = mod_install(&modlinkage)) == 0)
1737c478bd9Sstevel@tonic-gate 		return (0);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	(void) zone_key_delete(msg_zone_key);
1767c478bd9Sstevel@tonic-gate 	ipcs_destroy(msq_svc);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	return (result);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate int
1827c478bd9Sstevel@tonic-gate _fini(void)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	return (EBUSY);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate int
1887c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static void
1947c478bd9Sstevel@tonic-gate msg_dtor(kipc_perm_t *perm)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	kmsqid_t *qp = (kmsqid_t *)perm;
197*b2eb1770Sudpa 	int		ii;
1987c478bd9Sstevel@tonic-gate 
199*b2eb1770Sudpa 	for (ii = 0; ii < MAX_QNUM_CV; ii++)
200*b2eb1770Sudpa 		ASSERT(qp->msg_rcv_cnt[ii] == 0);
2017c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_snd_cnt == 0);
2027c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_cbytes == 0);
2037c478bd9Sstevel@tonic-gate 	list_destroy(&qp->msg_list);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate #define	msg_hold(mp)	(mp)->msg_copycnt++
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * msg_rele - decrement the reference count on the message.  When count
2117c478bd9Sstevel@tonic-gate  * reaches zero, free message header and contents.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate static void
2147c478bd9Sstevel@tonic-gate msg_rele(struct msg *mp)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	ASSERT(mp->msg_copycnt > 0);
2177c478bd9Sstevel@tonic-gate 	if (mp->msg_copycnt-- == 1) {
2187c478bd9Sstevel@tonic-gate 		if (mp->msg_addr)
2197c478bd9Sstevel@tonic-gate 			kmem_free(mp->msg_addr, mp->msg_size);
2207c478bd9Sstevel@tonic-gate 		kmem_free(mp, sizeof (struct msg));
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * msgunlink - Unlink msg from queue, decrement byte count and wake up anyone
2267c478bd9Sstevel@tonic-gate  * waiting for free bytes on queue.
2277c478bd9Sstevel@tonic-gate  *
2287c478bd9Sstevel@tonic-gate  * Called with queue locked.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate static void
2317c478bd9Sstevel@tonic-gate msgunlink(kmsqid_t *qp, struct msg *mp)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	list_remove(&qp->msg_list, mp);
2347c478bd9Sstevel@tonic-gate 	qp->msg_qnum--;
2357c478bd9Sstevel@tonic-gate 	qp->msg_cbytes -= mp->msg_size;
2367c478bd9Sstevel@tonic-gate 	msg_rele(mp);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/* Wake up waiting writers */
2397c478bd9Sstevel@tonic-gate 	if (qp->msg_snd_cnt)
2407c478bd9Sstevel@tonic-gate 		cv_broadcast(&qp->msg_snd_cv);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate static void
2447c478bd9Sstevel@tonic-gate msg_rmid(kipc_perm_t *perm)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	kmsqid_t *qp = (kmsqid_t *)perm;
2477c478bd9Sstevel@tonic-gate 	struct msg *mp;
248*b2eb1770Sudpa 	int		ii;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	while ((mp = list_head(&qp->msg_list)) != NULL)
2527c478bd9Sstevel@tonic-gate 		msgunlink(qp, mp);
2537c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_cbytes == 0);
2547c478bd9Sstevel@tonic-gate 
255*b2eb1770Sudpa 	for (ii = 0; ii < MAX_QNUM_CV; ii++) {
256*b2eb1770Sudpa 		if (qp->msg_rcv_cnt[ii])
257*b2eb1770Sudpa 			cv_broadcast(&qp->msg_rcv_cv[ii]);
258*b2eb1770Sudpa 	}
2597c478bd9Sstevel@tonic-gate 	if (qp->msg_snd_cnt)
2607c478bd9Sstevel@tonic-gate 		cv_broadcast(&qp->msg_snd_cv);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * msgctl system call.
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * gets q lock (via ipc_lookup), releases before return.
2677c478bd9Sstevel@tonic-gate  * may call users of msg_lock
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate static int
2707c478bd9Sstevel@tonic-gate msgctl(int msgid, int cmd, void *arg)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	STRUCT_DECL(msqid_ds, ds);		/* SVR4 queue work area */
2737c478bd9Sstevel@tonic-gate 	kmsqid_t		*qp;		/* ptr to associated q */
274*b2eb1770Sudpa 	int			error, ii;
2757c478bd9Sstevel@tonic-gate 	struct	cred		*cr;
2767c478bd9Sstevel@tonic-gate 	model_t	mdl = get_udatamodel();
2777c478bd9Sstevel@tonic-gate 	struct msqid_ds64	ds64;
2787c478bd9Sstevel@tonic-gate 	kmutex_t		*lock;
2797c478bd9Sstevel@tonic-gate 	proc_t			*pp = curproc;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	STRUCT_INIT(ds, mdl);
2827c478bd9Sstevel@tonic-gate 	cr = CRED();
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * Perform pre- or non-lookup actions (e.g. copyins, RMID).
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	switch (cmd) {
2887c478bd9Sstevel@tonic-gate 	case IPC_SET:
2897c478bd9Sstevel@tonic-gate 		if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds)))
2907c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2917c478bd9Sstevel@tonic-gate 		break;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	case IPC_SET64:
2947c478bd9Sstevel@tonic-gate 		if (copyin(arg, &ds64, sizeof (struct msqid_ds64)))
2957c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2967c478bd9Sstevel@tonic-gate 		break;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	case IPC_RMID:
2997c478bd9Sstevel@tonic-gate 		if (error = ipc_rmid(msq_svc, msgid, cr))
3007c478bd9Sstevel@tonic-gate 			return (set_errno(error));
3017c478bd9Sstevel@tonic-gate 		return (0);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * get msqid_ds for this msgid
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(msq_svc, msgid, (kipc_perm_t **)&qp)) == NULL)
3087c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	switch (cmd) {
3117c478bd9Sstevel@tonic-gate 	case IPC_SET:
3127c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(ds, msg_qbytes) > qp->msg_qbytes &&
3137c478bd9Sstevel@tonic-gate 		    secpolicy_ipc_config(cr) != 0) {
3147c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3157c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
3167c478bd9Sstevel@tonic-gate 		}
3177c478bd9Sstevel@tonic-gate 		if (error = ipcperm_set(msq_svc, cr, &qp->msg_perm,
3187c478bd9Sstevel@tonic-gate 		    &STRUCT_BUF(ds)->msg_perm, mdl)) {
3197c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3207c478bd9Sstevel@tonic-gate 			return (set_errno(error));
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 		qp->msg_qbytes = STRUCT_FGET(ds, msg_qbytes);
3237c478bd9Sstevel@tonic-gate 		qp->msg_ctime = gethrestime_sec();
3247c478bd9Sstevel@tonic-gate 		break;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	case IPC_STAT:
3277c478bd9Sstevel@tonic-gate 		if (error = ipcperm_access(&qp->msg_perm, MSG_R, cr)) {
3287c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3297c478bd9Sstevel@tonic-gate 			return (set_errno(error));
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 
332*b2eb1770Sudpa 		for (ii = 0; ii < MAX_QNUM_CV; ii++) {
333*b2eb1770Sudpa 			if (qp->msg_rcv_cnt[ii]) {
3347c478bd9Sstevel@tonic-gate 				qp->msg_perm.ipc_mode |= MSG_RWAIT;
335*b2eb1770Sudpa 				break;
336*b2eb1770Sudpa 			}
337*b2eb1770Sudpa 		}
3387c478bd9Sstevel@tonic-gate 		if (qp->msg_snd_cnt)
3397c478bd9Sstevel@tonic-gate 			qp->msg_perm.ipc_mode |= MSG_WWAIT;
3407c478bd9Sstevel@tonic-gate 		ipcperm_stat(&STRUCT_BUF(ds)->msg_perm, &qp->msg_perm, mdl);
3417c478bd9Sstevel@tonic-gate 		qp->msg_perm.ipc_mode &= ~(MSG_RWAIT|MSG_WWAIT);
3427c478bd9Sstevel@tonic-gate 		STRUCT_FSETP(ds, msg_first, NULL); 	/* kernel addr */
3437c478bd9Sstevel@tonic-gate 		STRUCT_FSETP(ds, msg_last, NULL);
3447c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_cbytes, qp->msg_cbytes);
3457c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_qnum, qp->msg_qnum);
3467c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_qbytes, qp->msg_qbytes);
3477c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_lspid, qp->msg_lspid);
3487c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_lrpid, qp->msg_lrpid);
3497c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_stime, qp->msg_stime);
3507c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_rtime, qp->msg_rtime);
3517c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_ctime, qp->msg_ctime);
3527c478bd9Sstevel@tonic-gate 		break;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	case IPC_SET64:
3557c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
3567c478bd9Sstevel@tonic-gate 		if ((ds64.msgx_qbytes > qp->msg_qbytes) &&
3577c478bd9Sstevel@tonic-gate 		    secpolicy_ipc_config(cr) != 0 &&
3587c478bd9Sstevel@tonic-gate 		    rctl_test(rc_process_msgmnb, pp->p_rctls, pp,
3597c478bd9Sstevel@tonic-gate 		    ds64.msgx_qbytes, RCA_SAFE) & RCT_DENY) {
3607c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
3617c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3627c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
3657c478bd9Sstevel@tonic-gate 		if (error = ipcperm_set64(msq_svc, cr, &qp->msg_perm,
3667c478bd9Sstevel@tonic-gate 		    &ds64.msgx_perm)) {
3677c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
3687c478bd9Sstevel@tonic-gate 			return (set_errno(error));
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 		qp->msg_qbytes = ds64.msgx_qbytes;
3717c478bd9Sstevel@tonic-gate 		qp->msg_ctime = gethrestime_sec();
3727c478bd9Sstevel@tonic-gate 		break;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	case IPC_STAT64:
375*b2eb1770Sudpa 		for (ii = 0; ii < MAX_QNUM_CV; ii++) {
376*b2eb1770Sudpa 			if (qp->msg_rcv_cnt[ii]) {
3777c478bd9Sstevel@tonic-gate 				qp->msg_perm.ipc_mode |= MSG_RWAIT;
378*b2eb1770Sudpa 				break;
379*b2eb1770Sudpa 			}
380*b2eb1770Sudpa 		}
3817c478bd9Sstevel@tonic-gate 		if (qp->msg_snd_cnt)
3827c478bd9Sstevel@tonic-gate 			qp->msg_perm.ipc_mode |= MSG_WWAIT;
3837c478bd9Sstevel@tonic-gate 		ipcperm_stat64(&ds64.msgx_perm, &qp->msg_perm);
3847c478bd9Sstevel@tonic-gate 		qp->msg_perm.ipc_mode &= ~(MSG_RWAIT|MSG_WWAIT);
3857c478bd9Sstevel@tonic-gate 		ds64.msgx_cbytes = qp->msg_cbytes;
3867c478bd9Sstevel@tonic-gate 		ds64.msgx_qnum = qp->msg_qnum;
3877c478bd9Sstevel@tonic-gate 		ds64.msgx_qbytes = qp->msg_qbytes;
3887c478bd9Sstevel@tonic-gate 		ds64.msgx_lspid = qp->msg_lspid;
3897c478bd9Sstevel@tonic-gate 		ds64.msgx_lrpid = qp->msg_lrpid;
3907c478bd9Sstevel@tonic-gate 		ds64.msgx_stime = qp->msg_stime;
3917c478bd9Sstevel@tonic-gate 		ds64.msgx_rtime = qp->msg_rtime;
3927c478bd9Sstevel@tonic-gate 		ds64.msgx_ctime = qp->msg_ctime;
3937c478bd9Sstevel@tonic-gate 		break;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	default:
3967c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
3977c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * Do copyout last (after releasing mutex).
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 	switch (cmd) {
4067c478bd9Sstevel@tonic-gate 	case IPC_STAT:
4077c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds)))
4087c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
4097c478bd9Sstevel@tonic-gate 		break;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	case IPC_STAT64:
4127c478bd9Sstevel@tonic-gate 		if (copyout(&ds64, arg, sizeof (struct msqid_ds64)))
4137c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
4147c478bd9Sstevel@tonic-gate 		break;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	return (0);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * Remove all message queues associated with a given zone.  Called by
4227c478bd9Sstevel@tonic-gate  * zone_shutdown when the zone is halted.
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
4257c478bd9Sstevel@tonic-gate static void
4267c478bd9Sstevel@tonic-gate msg_remove_zone(zoneid_t zoneid, void *arg)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	ipc_remove_zone(msq_svc, zoneid);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*
4327c478bd9Sstevel@tonic-gate  * msgget system call.
4337c478bd9Sstevel@tonic-gate  */
4347c478bd9Sstevel@tonic-gate static int
4357c478bd9Sstevel@tonic-gate msgget(key_t key, int msgflg)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;
4387c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
4397c478bd9Sstevel@tonic-gate 	int		id, error;
440*b2eb1770Sudpa 	int		ii;
4417c478bd9Sstevel@tonic-gate 	proc_t		*pp = curproc;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate top:
4447c478bd9Sstevel@tonic-gate 	if (error = ipc_get(msq_svc, key, msgflg, (kipc_perm_t **)&qp, &lock))
4457c478bd9Sstevel@tonic-gate 		return (set_errno(error));
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (IPC_FREE(&qp->msg_perm)) {
4487c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
4497c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		list_create(&qp->msg_list, sizeof (struct msg),
4527c478bd9Sstevel@tonic-gate 		    offsetof(struct msg, msg_node));
4537c478bd9Sstevel@tonic-gate 		qp->msg_qnum = 0;
4547c478bd9Sstevel@tonic-gate 		qp->msg_lspid = qp->msg_lrpid = 0;
4557c478bd9Sstevel@tonic-gate 		qp->msg_stime = qp->msg_rtime = 0;
4567c478bd9Sstevel@tonic-gate 		qp->msg_ctime = gethrestime_sec();
457*b2eb1770Sudpa 		for (ii = 0; ii < MAX_QNUM_CV; ii++)
458*b2eb1770Sudpa 			qp->msg_rcv_cnt[ii] = 0;
459*b2eb1770Sudpa 		qp->msg_snd_cnt = 0;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		if (error = ipc_commit_begin(msq_svc, key, msgflg,
4627c478bd9Sstevel@tonic-gate 		    (kipc_perm_t *)qp)) {
4637c478bd9Sstevel@tonic-gate 			if (error == EAGAIN)
4647c478bd9Sstevel@tonic-gate 				goto top;
4657c478bd9Sstevel@tonic-gate 			return (set_errno(error));
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 		qp->msg_qbytes = rctl_enforced_value(rc_process_msgmnb,
4687c478bd9Sstevel@tonic-gate 		    pp->p_rctls, pp);
4697c478bd9Sstevel@tonic-gate 		qp->msg_qmax = rctl_enforced_value(rc_process_msgtql,
4707c478bd9Sstevel@tonic-gate 		    pp->p_rctls, pp);
4717c478bd9Sstevel@tonic-gate 		lock = ipc_commit_end(msq_svc, &qp->msg_perm);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
4747c478bd9Sstevel@tonic-gate 	if (audit_active)
4757c478bd9Sstevel@tonic-gate 		audit_ipcget(AT_IPC_MSG, (void *)qp);
4767c478bd9Sstevel@tonic-gate #endif
4777c478bd9Sstevel@tonic-gate 	id = qp->msg_perm.ipc_id;
4787c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
4797c478bd9Sstevel@tonic-gate 	return (id);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate  * msgrcv system call.
4847c478bd9Sstevel@tonic-gate  */
4857c478bd9Sstevel@tonic-gate static ssize_t
4867c478bd9Sstevel@tonic-gate msgrcv(int msqid, struct ipcmsgbuf *msgp, size_t msgsz, long msgtyp, int msgflg)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	struct msg	*mp;	/* ptr to msg on q */
4897c478bd9Sstevel@tonic-gate 	struct msg	*smp;	/* ptr to best msg on q */
4907c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;	/* ptr to associated q */
4917c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
4927c478bd9Sstevel@tonic-gate 	size_t		xtsz;	/* transfer byte count */
4937c478bd9Sstevel@tonic-gate 	int		error = 0, copyerror = 0;
4947c478bd9Sstevel@tonic-gate 	int		cvres;
4957c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(ipcmsgbuf, umsgp);
4967c478bd9Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, msg, 1);	/* bump msg send/rcv count */
4997c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(umsgp, mdl, msgp);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL)
5027c478bd9Sstevel@tonic-gate 		return ((ssize_t)set_errno(EINVAL));
5037c478bd9Sstevel@tonic-gate 	ipc_hold(msq_svc, (kipc_perm_t *)qp);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (error = ipcperm_access(&qp->msg_perm, MSG_R, CRED()))
5067c478bd9Sstevel@tonic-gate 		goto msgrcv_out;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate findmsg:
5097c478bd9Sstevel@tonic-gate 	smp = NULL;
5107c478bd9Sstevel@tonic-gate 	mp = list_head(&qp->msg_list);
5117c478bd9Sstevel@tonic-gate 	if (msgtyp == 0) {
5127c478bd9Sstevel@tonic-gate 		smp = mp;
5137c478bd9Sstevel@tonic-gate 	} else {
5147c478bd9Sstevel@tonic-gate 		for (; mp; mp = list_next(&qp->msg_list, mp)) {
5157c478bd9Sstevel@tonic-gate 			if (msgtyp > 0) {
5167c478bd9Sstevel@tonic-gate 				if (msgtyp != mp->msg_type)
5177c478bd9Sstevel@tonic-gate 					continue;
5187c478bd9Sstevel@tonic-gate 				smp = mp;
5197c478bd9Sstevel@tonic-gate 				break;
5207c478bd9Sstevel@tonic-gate 			}
5217c478bd9Sstevel@tonic-gate 			if (mp->msg_type <= -msgtyp) {
5227c478bd9Sstevel@tonic-gate 				if (smp && smp->msg_type <= mp->msg_type)
5237c478bd9Sstevel@tonic-gate 					continue;
5247c478bd9Sstevel@tonic-gate 				smp = mp;
5257c478bd9Sstevel@tonic-gate 			}
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (smp) {
5307c478bd9Sstevel@tonic-gate 		/*
5317c478bd9Sstevel@tonic-gate 		 * Message found.
5327c478bd9Sstevel@tonic-gate 		 */
5337c478bd9Sstevel@tonic-gate 		if ((smp->msg_flags & MSG_RCVCOPY) == 0) {
5347c478bd9Sstevel@tonic-gate 			/*
5357c478bd9Sstevel@tonic-gate 			 * No one else is copying this message. Copy it.
5367c478bd9Sstevel@tonic-gate 			 */
5377c478bd9Sstevel@tonic-gate 			if (msgsz < smp->msg_size) {
5387c478bd9Sstevel@tonic-gate 				if ((msgflg & MSG_NOERROR) == 0) {
5397c478bd9Sstevel@tonic-gate 					error = E2BIG;
5407c478bd9Sstevel@tonic-gate 					goto msgrcv_out;
5417c478bd9Sstevel@tonic-gate 				} else {
5427c478bd9Sstevel@tonic-gate 					xtsz = msgsz;
5437c478bd9Sstevel@tonic-gate 				}
5447c478bd9Sstevel@tonic-gate 			} else {
5457c478bd9Sstevel@tonic-gate 				xtsz = smp->msg_size;
5467c478bd9Sstevel@tonic-gate 			}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 			/*
5497c478bd9Sstevel@tonic-gate 			 * Mark message as being copied out. Release mutex
5507c478bd9Sstevel@tonic-gate 			 * while copying out.
5517c478bd9Sstevel@tonic-gate 			 */
5527c478bd9Sstevel@tonic-gate 			ASSERT((smp->msg_flags & MSG_RCVCOPY) == 0);
5537c478bd9Sstevel@tonic-gate 			smp->msg_flags |= MSG_RCVCOPY;
5547c478bd9Sstevel@tonic-gate 			msg_hold(smp);
5557c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 			if (mdl == DATAMODEL_NATIVE) {
5587c478bd9Sstevel@tonic-gate 				copyerror = copyout(&smp->msg_type, msgp,
5597c478bd9Sstevel@tonic-gate 				    sizeof (smp->msg_type));
5607c478bd9Sstevel@tonic-gate 			} else {
5617c478bd9Sstevel@tonic-gate 				/*
5627c478bd9Sstevel@tonic-gate 				 * 32-bit callers need an imploded msg type.
5637c478bd9Sstevel@tonic-gate 				 */
5647c478bd9Sstevel@tonic-gate 				int32_t	msg_type32 = smp->msg_type;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 				copyerror = copyout(&msg_type32, msgp,
5677c478bd9Sstevel@tonic-gate 				    sizeof (msg_type32));
5687c478bd9Sstevel@tonic-gate 			}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 			if (copyerror == 0 && xtsz)
5717c478bd9Sstevel@tonic-gate 				copyerror = copyout(smp->msg_addr,
5727c478bd9Sstevel@tonic-gate 				    STRUCT_FADDR(umsgp, mtext), xtsz);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 			/*
5757c478bd9Sstevel@tonic-gate 			 * Reclaim mutex, make sure queue still exists,
5767c478bd9Sstevel@tonic-gate 			 * and remove message.
5777c478bd9Sstevel@tonic-gate 			 */
5787c478bd9Sstevel@tonic-gate 			lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id);
5797c478bd9Sstevel@tonic-gate 			ASSERT(smp->msg_flags & MSG_RCVCOPY);
5807c478bd9Sstevel@tonic-gate 			smp->msg_flags &= ~MSG_RCVCOPY;
5817c478bd9Sstevel@tonic-gate 			msg_rele(smp);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 			if (IPC_FREE(&qp->msg_perm)) {
5847c478bd9Sstevel@tonic-gate 				error = EIDRM;
5857c478bd9Sstevel@tonic-gate 				goto msgrcv_out;
5867c478bd9Sstevel@tonic-gate 			}
587*b2eb1770Sudpa 			/*
588*b2eb1770Sudpa 			 * MSG_RCVCOPY was set while we dropped and reaquired
589*b2eb1770Sudpa 			 * the lock. A thread looking for same message type
590*b2eb1770Sudpa 			 * might have entered during that interval and seeing
591*b2eb1770Sudpa 			 * MSG_RCVCOPY set, would have landed up in the sleepq.
592*b2eb1770Sudpa 			 */
593*b2eb1770Sudpa 			cv_broadcast(&qp->msg_rcv_cv[MSG_QNUM(smp->msg_type)]);
594*b2eb1770Sudpa 			cv_broadcast(&qp->msg_rcv_cv[0]);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 			if (copyerror) {
5977c478bd9Sstevel@tonic-gate 				error = EFAULT;
5987c478bd9Sstevel@tonic-gate 				goto msgrcv_out;
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 			qp->msg_lrpid = ttoproc(curthread)->p_pid;
6017c478bd9Sstevel@tonic-gate 			qp->msg_rtime = gethrestime_sec();
6027c478bd9Sstevel@tonic-gate 			msgunlink(qp, smp);
6037c478bd9Sstevel@tonic-gate 			goto msgrcv_out;
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	} else {
6077c478bd9Sstevel@tonic-gate 		/*
6087c478bd9Sstevel@tonic-gate 		 * No message found.
6097c478bd9Sstevel@tonic-gate 		 */
6107c478bd9Sstevel@tonic-gate 		if (msgflg & IPC_NOWAIT) {
6117c478bd9Sstevel@tonic-gate 			error = ENOMSG;
6127c478bd9Sstevel@tonic-gate 			goto msgrcv_out;
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	/* Wait for new message */
617*b2eb1770Sudpa 	qp->msg_rcv_cnt[MSG_QNUM(msgtyp)]++;
618*b2eb1770Sudpa 	cvres = cv_wait_sig(&qp->msg_rcv_cv[MSG_QNUM(msgtyp)], lock);
6197c478bd9Sstevel@tonic-gate 	lock = ipc_relock(msq_svc, qp->msg_perm.ipc_id, lock);
620*b2eb1770Sudpa 	qp->msg_rcv_cnt[MSG_QNUM(msgtyp)]--;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	if (IPC_FREE(&qp->msg_perm)) {
6237c478bd9Sstevel@tonic-gate 		error = EIDRM;
6247c478bd9Sstevel@tonic-gate 		goto msgrcv_out;
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 	if (cvres == 0) {
6277c478bd9Sstevel@tonic-gate 		error = EINTR;
6287c478bd9Sstevel@tonic-gate 		goto msgrcv_out;
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	goto findmsg;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate msgrcv_out:
6347c478bd9Sstevel@tonic-gate 	ipc_rele(msq_svc, (kipc_perm_t *)qp);
6357c478bd9Sstevel@tonic-gate 	if (error)
6367c478bd9Sstevel@tonic-gate 		return ((ssize_t)set_errno(error));
6377c478bd9Sstevel@tonic-gate 	return ((ssize_t)xtsz);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate /*
6417c478bd9Sstevel@tonic-gate  * msgids system call.
6427c478bd9Sstevel@tonic-gate  */
6437c478bd9Sstevel@tonic-gate static int
6447c478bd9Sstevel@tonic-gate msgids(int *buf, uint_t nids, uint_t *pnids)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	int error;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	if (error = ipc_ids(msq_svc, buf, nids, pnids))
6497c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	return (0);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate #define	RND(x)		roundup((x), sizeof (size_t))
6557c478bd9Sstevel@tonic-gate #define	RND32(x)	roundup((x), sizeof (size32_t))
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate  * msgsnap system call.
6597c478bd9Sstevel@tonic-gate  */
6607c478bd9Sstevel@tonic-gate static int
6617c478bd9Sstevel@tonic-gate msgsnap(int msqid, caddr_t buf, size_t bufsz, long msgtyp)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	struct msg	*mp;	/* ptr to msg on q */
6647c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;	/* ptr to associated q */
6657c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
6667c478bd9Sstevel@tonic-gate 	size_t		size;
6677c478bd9Sstevel@tonic-gate 	size_t		nmsg;
6687c478bd9Sstevel@tonic-gate 	struct msg	**snaplist;
6697c478bd9Sstevel@tonic-gate 	int		error, i;
6707c478bd9Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
6717c478bd9Sstevel@tonic-gate 	STRUCT_DECL(msgsnap_head, head);
6727c478bd9Sstevel@tonic-gate 	STRUCT_DECL(msgsnap_mhead, mhead);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	STRUCT_INIT(head, mdl);
6757c478bd9Sstevel@tonic-gate 	STRUCT_INIT(mhead, mdl);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (bufsz < STRUCT_SIZE(head))
6787c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL)
6817c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if (error = ipcperm_access(&qp->msg_perm, MSG_R, CRED())) {
6847c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
6857c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	ipc_hold(msq_svc, (kipc_perm_t *)qp);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/*
6907c478bd9Sstevel@tonic-gate 	 * First compute the required buffer size and
6917c478bd9Sstevel@tonic-gate 	 * the number of messages on the queue.
6927c478bd9Sstevel@tonic-gate 	 */
6937c478bd9Sstevel@tonic-gate 	size = nmsg = 0;
6947c478bd9Sstevel@tonic-gate 	for (mp = list_head(&qp->msg_list); mp;
6957c478bd9Sstevel@tonic-gate 	    mp = list_next(&qp->msg_list, mp)) {
6967c478bd9Sstevel@tonic-gate 		if (msgtyp == 0 ||
6977c478bd9Sstevel@tonic-gate 		    (msgtyp > 0 && msgtyp == mp->msg_type) ||
6987c478bd9Sstevel@tonic-gate 		    (msgtyp < 0 && mp->msg_type <= -msgtyp)) {
6997c478bd9Sstevel@tonic-gate 			nmsg++;
7007c478bd9Sstevel@tonic-gate 			if (mdl == DATAMODEL_NATIVE)
7017c478bd9Sstevel@tonic-gate 				size += RND(mp->msg_size);
7027c478bd9Sstevel@tonic-gate 			else
7037c478bd9Sstevel@tonic-gate 				size += RND32(mp->msg_size);
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	size += STRUCT_SIZE(head) + nmsg * STRUCT_SIZE(mhead);
7087c478bd9Sstevel@tonic-gate 	if (size > bufsz)
7097c478bd9Sstevel@tonic-gate 		nmsg = 0;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (nmsg > 0) {
7127c478bd9Sstevel@tonic-gate 		/*
7137c478bd9Sstevel@tonic-gate 		 * Mark the messages as being copied.
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 		snaplist = (struct msg **)kmem_alloc(nmsg *
7167c478bd9Sstevel@tonic-gate 		    sizeof (struct msg *), KM_SLEEP);
7177c478bd9Sstevel@tonic-gate 		i = 0;
7187c478bd9Sstevel@tonic-gate 		for (mp = list_head(&qp->msg_list); mp;
7197c478bd9Sstevel@tonic-gate 		    mp = list_next(&qp->msg_list, mp)) {
7207c478bd9Sstevel@tonic-gate 			if (msgtyp == 0 ||
7217c478bd9Sstevel@tonic-gate 			    (msgtyp > 0 && msgtyp == mp->msg_type) ||
7227c478bd9Sstevel@tonic-gate 			    (msgtyp < 0 && mp->msg_type <= -msgtyp)) {
7237c478bd9Sstevel@tonic-gate 				msg_hold(mp);
7247c478bd9Sstevel@tonic-gate 				snaplist[i] = mp;
7257c478bd9Sstevel@tonic-gate 				i++;
7267c478bd9Sstevel@tonic-gate 			}
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/*
7327c478bd9Sstevel@tonic-gate 	 * Copy out the buffer header.
7337c478bd9Sstevel@tonic-gate 	 */
7347c478bd9Sstevel@tonic-gate 	STRUCT_FSET(head, msgsnap_size, size);
7357c478bd9Sstevel@tonic-gate 	STRUCT_FSET(head, msgsnap_nmsg, nmsg);
7367c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(head), buf, STRUCT_SIZE(head)))
7377c478bd9Sstevel@tonic-gate 		error = EFAULT;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	buf += STRUCT_SIZE(head);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/*
7427c478bd9Sstevel@tonic-gate 	 * Now copy out the messages one by one.
7437c478bd9Sstevel@tonic-gate 	 */
7447c478bd9Sstevel@tonic-gate 	for (i = 0; i < nmsg; i++) {
7457c478bd9Sstevel@tonic-gate 		mp = snaplist[i];
7467c478bd9Sstevel@tonic-gate 		if (error == 0) {
7477c478bd9Sstevel@tonic-gate 			STRUCT_FSET(mhead, msgsnap_mlen, mp->msg_size);
7487c478bd9Sstevel@tonic-gate 			STRUCT_FSET(mhead, msgsnap_mtype, mp->msg_type);
7497c478bd9Sstevel@tonic-gate 			if (copyout(STRUCT_BUF(mhead), buf, STRUCT_SIZE(mhead)))
7507c478bd9Sstevel@tonic-gate 				error = EFAULT;
7517c478bd9Sstevel@tonic-gate 			buf += STRUCT_SIZE(mhead);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			if (error == 0 &&
7547c478bd9Sstevel@tonic-gate 			    mp->msg_size != 0 &&
7557c478bd9Sstevel@tonic-gate 			    copyout(mp->msg_addr, buf, mp->msg_size))
7567c478bd9Sstevel@tonic-gate 				error = EFAULT;
7577c478bd9Sstevel@tonic-gate 			if (mdl == DATAMODEL_NATIVE)
7587c478bd9Sstevel@tonic-gate 				buf += RND(mp->msg_size);
7597c478bd9Sstevel@tonic-gate 			else
7607c478bd9Sstevel@tonic-gate 				buf += RND32(mp->msg_size);
7617c478bd9Sstevel@tonic-gate 		}
7627c478bd9Sstevel@tonic-gate 		lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id);
7637c478bd9Sstevel@tonic-gate 		msg_rele(mp);
7647c478bd9Sstevel@tonic-gate 		/* Check for msg q deleted or reallocated */
7657c478bd9Sstevel@tonic-gate 		if (IPC_FREE(&qp->msg_perm))
7667c478bd9Sstevel@tonic-gate 			error = EIDRM;
7677c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	(void) ipc_lock(msq_svc, qp->msg_perm.ipc_id);
7717c478bd9Sstevel@tonic-gate 	ipc_rele(msq_svc, (kipc_perm_t *)qp);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (nmsg > 0)
7747c478bd9Sstevel@tonic-gate 		kmem_free(snaplist, nmsg * sizeof (struct msg *));
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	if (error)
7777c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7787c478bd9Sstevel@tonic-gate 	return (0);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate /*
7827c478bd9Sstevel@tonic-gate  * msgsnd system call.
7837c478bd9Sstevel@tonic-gate  */
7847c478bd9Sstevel@tonic-gate static int
7857c478bd9Sstevel@tonic-gate msgsnd(int msqid, struct ipcmsgbuf *msgp, size_t msgsz, int msgflg)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;
7887c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
7897c478bd9Sstevel@tonic-gate 	struct msg	*mp = NULL;
7907c478bd9Sstevel@tonic-gate 	long		type;
7917c478bd9Sstevel@tonic-gate 	int		error = 0;
7927c478bd9Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
7937c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(ipcmsgbuf, umsgp);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, msg, 1);	/* bump msg send/rcv count */
7967c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(umsgp, mdl, msgp);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	if (mdl == DATAMODEL_NATIVE) {
7997c478bd9Sstevel@tonic-gate 		if (copyin(msgp, &type, sizeof (type)))
8007c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
8017c478bd9Sstevel@tonic-gate 	} else {
8027c478bd9Sstevel@tonic-gate 		int32_t	type32;
8037c478bd9Sstevel@tonic-gate 		if (copyin(msgp, &type32, sizeof (type32)))
8047c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
8057c478bd9Sstevel@tonic-gate 		type = type32;
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (type < 1)
8097c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL)
8127c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8137c478bd9Sstevel@tonic-gate 	ipc_hold(msq_svc, (kipc_perm_t *)qp);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	if (msgsz > qp->msg_qbytes) {
8167c478bd9Sstevel@tonic-gate 		error = EINVAL;
8177c478bd9Sstevel@tonic-gate 		goto msgsnd_out;
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (error = ipcperm_access(&qp->msg_perm, MSG_W, CRED()))
8217c478bd9Sstevel@tonic-gate 		goto msgsnd_out;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate top:
8247c478bd9Sstevel@tonic-gate 	/*
8257c478bd9Sstevel@tonic-gate 	 * Allocate space on q, message header, & buffer space.
8267c478bd9Sstevel@tonic-gate 	 */
8277c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_qnum <= qp->msg_qmax);
8287c478bd9Sstevel@tonic-gate 	while ((msgsz > qp->msg_qbytes - qp->msg_cbytes) ||
8297c478bd9Sstevel@tonic-gate 	    (qp->msg_qnum == qp->msg_qmax)) {
8307c478bd9Sstevel@tonic-gate 		int cvres;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		if (msgflg & IPC_NOWAIT) {
8337c478bd9Sstevel@tonic-gate 			error = EAGAIN;
8347c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		qp->msg_snd_cnt++;
8387c478bd9Sstevel@tonic-gate 		cvres = cv_wait_sig(&qp->msg_snd_cv, lock);
8397c478bd9Sstevel@tonic-gate 		lock = ipc_relock(msq_svc, qp->msg_perm.ipc_id, lock);
8407c478bd9Sstevel@tonic-gate 		qp->msg_snd_cnt--;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		if (IPC_FREE(&qp->msg_perm)) {
8437c478bd9Sstevel@tonic-gate 			error = EIDRM;
8447c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 		if (cvres == 0) {
8487c478bd9Sstevel@tonic-gate 			error = EINTR;
8497c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
8547c478bd9Sstevel@tonic-gate 		int failure;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
8577c478bd9Sstevel@tonic-gate 		mp = kmem_zalloc(sizeof (struct msg), KM_SLEEP);
8587c478bd9Sstevel@tonic-gate 		mp->msg_addr = kmem_zalloc(msgsz, KM_SLEEP);
8597c478bd9Sstevel@tonic-gate 		mp->msg_size = msgsz;
8607c478bd9Sstevel@tonic-gate 		mp->msg_copycnt = 1;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		failure = msgsz && (copyin(STRUCT_FADDR(umsgp, mtext),
8637c478bd9Sstevel@tonic-gate 		    mp->msg_addr, msgsz) == -1);
8647c478bd9Sstevel@tonic-gate 		lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id);
8657c478bd9Sstevel@tonic-gate 		if (IPC_FREE(&qp->msg_perm)) {
8667c478bd9Sstevel@tonic-gate 			error = EIDRM;
8677c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 		if (failure) {
8707c478bd9Sstevel@tonic-gate 			error = EFAULT;
8717c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
8727c478bd9Sstevel@tonic-gate 		}
8737c478bd9Sstevel@tonic-gate 		goto top;
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	/*
8777c478bd9Sstevel@tonic-gate 	 * Everything is available, put msg on q.
8787c478bd9Sstevel@tonic-gate 	 */
8797c478bd9Sstevel@tonic-gate 	qp->msg_qnum++;
8807c478bd9Sstevel@tonic-gate 	qp->msg_cbytes += msgsz;
8817c478bd9Sstevel@tonic-gate 	qp->msg_lspid = curproc->p_pid;
8827c478bd9Sstevel@tonic-gate 	qp->msg_stime = gethrestime_sec();
8837c478bd9Sstevel@tonic-gate 	mp->msg_type = type;
8847c478bd9Sstevel@tonic-gate 	mp->msg_flags = 0;
8857c478bd9Sstevel@tonic-gate 	list_insert_tail(&qp->msg_list, mp);
886*b2eb1770Sudpa 	/*
887*b2eb1770Sudpa 	 * For all message type >= 1.
888*b2eb1770Sudpa 	 */
889*b2eb1770Sudpa 	if (qp->msg_rcv_cnt[MSG_QNUM(type)])
890*b2eb1770Sudpa 		cv_broadcast(&qp->msg_rcv_cv[MSG_QNUM(type)]);
891*b2eb1770Sudpa 	/*
892*b2eb1770Sudpa 	 * For all message type < 1.
893*b2eb1770Sudpa 	 */
894*b2eb1770Sudpa 	if (qp->msg_rcv_cnt[0])
895*b2eb1770Sudpa 		cv_broadcast(&qp->msg_rcv_cv[0]);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate msgsnd_out:
8987c478bd9Sstevel@tonic-gate 	ipc_rele(msq_svc, (kipc_perm_t *)qp);	/* drops lock */
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	if (error) {
9017c478bd9Sstevel@tonic-gate 		if (mp)
9027c478bd9Sstevel@tonic-gate 			msg_rele(mp);
9037c478bd9Sstevel@tonic-gate 		return (set_errno(error));
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	return (0);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate /*
9107c478bd9Sstevel@tonic-gate  * msgsys - System entry point for msgctl, msgget, msgrcv, and msgsnd
9117c478bd9Sstevel@tonic-gate  * system calls.
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate static ssize_t
9147c478bd9Sstevel@tonic-gate msgsys(int opcode, uintptr_t a1, uintptr_t a2, uintptr_t a3,
9157c478bd9Sstevel@tonic-gate 	uintptr_t a4, uintptr_t a5)
9167c478bd9Sstevel@tonic-gate {
9177c478bd9Sstevel@tonic-gate 	ssize_t error;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	switch (opcode) {
9207c478bd9Sstevel@tonic-gate 	case MSGGET:
9217c478bd9Sstevel@tonic-gate 		error = msgget((key_t)a1, (int)a2);
9227c478bd9Sstevel@tonic-gate 		break;
9237c478bd9Sstevel@tonic-gate 	case MSGCTL:
9247c478bd9Sstevel@tonic-gate 		error = msgctl((int)a1, (int)a2, (void *)a3);
9257c478bd9Sstevel@tonic-gate 		break;
9267c478bd9Sstevel@tonic-gate 	case MSGRCV:
9277c478bd9Sstevel@tonic-gate 		error = msgrcv((int)a1, (struct ipcmsgbuf *)a2,
9287c478bd9Sstevel@tonic-gate 		    (size_t)a3, (long)a4, (int)a5);
9297c478bd9Sstevel@tonic-gate 		break;
9307c478bd9Sstevel@tonic-gate 	case MSGSND:
9317c478bd9Sstevel@tonic-gate 		error = msgsnd((int)a1, (struct ipcmsgbuf *)a2,
9327c478bd9Sstevel@tonic-gate 		    (size_t)a3, (int)a4);
9337c478bd9Sstevel@tonic-gate 		break;
9347c478bd9Sstevel@tonic-gate 	case MSGIDS:
9357c478bd9Sstevel@tonic-gate 		error = msgids((int *)a1, (uint_t)a2, (uint_t *)a3);
9367c478bd9Sstevel@tonic-gate 		break;
9377c478bd9Sstevel@tonic-gate 	case MSGSNAP:
9387c478bd9Sstevel@tonic-gate 		error = msgsnap((int)a1, (caddr_t)a2, (size_t)a3, (long)a4);
9397c478bd9Sstevel@tonic-gate 		break;
9407c478bd9Sstevel@tonic-gate 	default:
9417c478bd9Sstevel@tonic-gate 		error = set_errno(EINVAL);
9427c478bd9Sstevel@tonic-gate 		break;
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	return (error);
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate  * msgsys32 - System entry point for msgctl, msgget, msgrcv, and msgsnd
9517c478bd9Sstevel@tonic-gate  * system calls for 32-bit callers on LP64 kernel.
9527c478bd9Sstevel@tonic-gate  */
9537c478bd9Sstevel@tonic-gate static ssize32_t
9547c478bd9Sstevel@tonic-gate msgsys32(int opcode, uint32_t a1, uint32_t a2, uint32_t a3,
9557c478bd9Sstevel@tonic-gate 	uint32_t a4, uint32_t a5)
9567c478bd9Sstevel@tonic-gate {
9577c478bd9Sstevel@tonic-gate 	ssize_t error;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	switch (opcode) {
9607c478bd9Sstevel@tonic-gate 	case MSGGET:
9617c478bd9Sstevel@tonic-gate 		error = msgget((key_t)a1, (int)a2);
9627c478bd9Sstevel@tonic-gate 		break;
9637c478bd9Sstevel@tonic-gate 	case MSGCTL:
9647c478bd9Sstevel@tonic-gate 		error = msgctl((int)a1, (int)a2, (void *)(uintptr_t)a3);
9657c478bd9Sstevel@tonic-gate 		break;
9667c478bd9Sstevel@tonic-gate 	case MSGRCV:
9677c478bd9Sstevel@tonic-gate 		error = msgrcv((int)a1, (struct ipcmsgbuf *)(uintptr_t)a2,
9687c478bd9Sstevel@tonic-gate 		    (size_t)a3, (long)(int32_t)a4, (int)a5);
9697c478bd9Sstevel@tonic-gate 		break;
9707c478bd9Sstevel@tonic-gate 	case MSGSND:
9717c478bd9Sstevel@tonic-gate 		error = msgsnd((int)a1, (struct ipcmsgbuf *)(uintptr_t)a2,
9727c478bd9Sstevel@tonic-gate 		    (size_t)(int32_t)a3, (int)a4);
9737c478bd9Sstevel@tonic-gate 		break;
9747c478bd9Sstevel@tonic-gate 	case MSGIDS:
9757c478bd9Sstevel@tonic-gate 		error = msgids((int *)(uintptr_t)a1, (uint_t)a2,
9767c478bd9Sstevel@tonic-gate 		    (uint_t *)(uintptr_t)a3);
9777c478bd9Sstevel@tonic-gate 		break;
9787c478bd9Sstevel@tonic-gate 	case MSGSNAP:
9797c478bd9Sstevel@tonic-gate 		error = msgsnap((int)a1, (caddr_t)(uintptr_t)a2, (size_t)a3,
9807c478bd9Sstevel@tonic-gate 		    (long)(int32_t)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 #endif	/* SYSCALL32_IMPL */
990