xref: /titanic_44/usr/src/uts/common/os/streamio.c (revision 301ce41f42acb0fca24b2abbaddde9bdb71ab449)
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
58ec5a142Sedp  * Common Development and Distribution License (the "License").
68ec5a142Sedp  * 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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
2651dd2c77Svk199839  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/proc.h>
397c478bd9Sstevel@tonic-gate #include <sys/cred.h>
407c478bd9Sstevel@tonic-gate #include <sys/user.h>
417c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437c478bd9Sstevel@tonic-gate #include <sys/stream.h>
447c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
457c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
467c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
477c478bd9Sstevel@tonic-gate #include <sys/var.h>
487c478bd9Sstevel@tonic-gate #include <sys/poll.h>
497c478bd9Sstevel@tonic-gate #include <sys/termio.h>
507c478bd9Sstevel@tonic-gate #include <sys/ttold.h>
517c478bd9Sstevel@tonic-gate #include <sys/systm.h>
527c478bd9Sstevel@tonic-gate #include <sys/uio.h>
537c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
547c478bd9Sstevel@tonic-gate #include <sys/sad.h>
55f4b3ec61Sdh155122 #include <sys/netstack.h>
567c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
577c478bd9Sstevel@tonic-gate #include <sys/jioctl.h>
587c478bd9Sstevel@tonic-gate #include <sys/procset.h>
597c478bd9Sstevel@tonic-gate #include <sys/session.h>
607c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
617c478bd9Sstevel@tonic-gate #include <sys/filio.h>
627c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
637c478bd9Sstevel@tonic-gate #include <sys/debug.h>
647c478bd9Sstevel@tonic-gate #include <sys/strredir.h>
657c478bd9Sstevel@tonic-gate #include <sys/fs/fifonode.h>
667c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
677c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
687c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
697c478bd9Sstevel@tonic-gate #include <sys/project.h>
707c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
717c478bd9Sstevel@tonic-gate #include <sys/msio.h>
727c478bd9Sstevel@tonic-gate #include <sys/tty.h>
737c478bd9Sstevel@tonic-gate #include <sys/ptyvar.h>
747c478bd9Sstevel@tonic-gate #include <sys/vuid_event.h>
757c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
767c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
777c478bd9Sstevel@tonic-gate #include <sys/sunldi_impl.h>
787c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
797c478bd9Sstevel@tonic-gate #include <sys/policy.h>
80d62bc4baSyz147064 #include <sys/dld.h>
81f4b3ec61Sdh155122 #include <sys/zone.h>
827c478bd9Sstevel@tonic-gate 
839acbbeafSnn35248 /*
849acbbeafSnn35248  * This define helps improve the readability of streams code while
859acbbeafSnn35248  * still maintaining a very old streams performance enhancement.  The
869acbbeafSnn35248  * performance enhancement basically involved having all callers
879acbbeafSnn35248  * of straccess() perform the first check that straccess() will do
889acbbeafSnn35248  * locally before actually calling straccess().  (There by reducing
899acbbeafSnn35248  * the number of unnecessary calls to straccess().)
909acbbeafSnn35248  */
919acbbeafSnn35248 #define	i_straccess(x, y)	((stp->sd_sidp == NULL) ? 0 : \
929acbbeafSnn35248 				    (stp->sd_vnode->v_type == VFIFO) ? 0 : \
939acbbeafSnn35248 				    straccess((x), (y)))
949acbbeafSnn35248 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * what is mblk_pull_len?
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * If a streams message consists of many short messages,
997c478bd9Sstevel@tonic-gate  * a performance degradation occurs from copyout overhead.
1007c478bd9Sstevel@tonic-gate  * To decrease the per mblk overhead, messages that are
1017c478bd9Sstevel@tonic-gate  * likely to consist of many small mblks are pulled up into
1027c478bd9Sstevel@tonic-gate  * one continuous chunk of memory.
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  * To avoid the processing overhead of examining every
1057c478bd9Sstevel@tonic-gate  * mblk, a quick heuristic is used. If the first mblk in
1067c478bd9Sstevel@tonic-gate  * the message is shorter than mblk_pull_len, it is likely
1077c478bd9Sstevel@tonic-gate  * that the rest of the mblk will be short.
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * This heuristic was decided upon after performance tests
1107c478bd9Sstevel@tonic-gate  * indicated that anything more complex slowed down the main
1117c478bd9Sstevel@tonic-gate  * code path.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate #define	MBLK_PULL_LEN 64
1147c478bd9Sstevel@tonic-gate uint32_t mblk_pull_len = MBLK_PULL_LEN;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * The sgttyb_handling flag controls the handling of the old BSD
1187c478bd9Sstevel@tonic-gate  * TIOCGETP, TIOCSETP, and TIOCSETN ioctls as follows:
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * 0 - Emit no warnings at all and retain old, broken behavior.
1217c478bd9Sstevel@tonic-gate  * 1 - Emit no warnings and silently handle new semantics.
1227c478bd9Sstevel@tonic-gate  * 2 - Send cmn_err(CE_NOTE) when either TIOCSETP or TIOCSETN is used
1237c478bd9Sstevel@tonic-gate  *     (once per system invocation).  Handle with new semantics.
1247c478bd9Sstevel@tonic-gate  * 3 - Send SIGSYS when any TIOCGETP, TIOCSETP, or TIOCSETN call is
1257c478bd9Sstevel@tonic-gate  *     made (so that offenders drop core and are easy to debug).
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  * The "new semantics" are that TIOCGETP returns B38400 for
1287c478bd9Sstevel@tonic-gate  * sg_[io]speed if the corresponding value is over B38400, and that
1297c478bd9Sstevel@tonic-gate  * TIOCSET[PN] accept B38400 in these cases to mean "retain current
1307c478bd9Sstevel@tonic-gate  * bit rate."
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate int sgttyb_handling = 1;
1337c478bd9Sstevel@tonic-gate static boolean_t sgttyb_complaint;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /* don't push drcompat module by default on Style-2 streams */
1367c478bd9Sstevel@tonic-gate static int push_drcompat = 0;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * id value used to distinguish between different ioctl messages
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate static uint32_t ioc_id;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static void putback(struct stdata *, queue_t *, mblk_t *, int);
1447c478bd9Sstevel@tonic-gate static void strcleanall(struct vnode *);
1457c478bd9Sstevel@tonic-gate static int strwsrv(queue_t *);
146ca9327a6Smeem static int strdocmd(struct stdata *, struct strcmd *, cred_t *);
14717169044Sbrutus static void struioainit(queue_t *, sodirect_t *, uio_t *);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * qinit and module_info structures for stream head read and write queues
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate struct module_info strm_info = { 0, "strrhead", 0, INFPSZ, STRHIGH, STRLOW };
1537c478bd9Sstevel@tonic-gate struct module_info stwm_info = { 0, "strwhead", 0, 0, 0, 0 };
1547c478bd9Sstevel@tonic-gate struct qinit strdata = { strrput, NULL, NULL, NULL, NULL, &strm_info };
1557c478bd9Sstevel@tonic-gate struct qinit stwdata = { NULL, strwsrv, NULL, NULL, NULL, &stwm_info };
1567c478bd9Sstevel@tonic-gate struct module_info fiform_info = { 0, "fifostrrhead", 0, PIPE_BUF, FIFOHIWAT,
1577c478bd9Sstevel@tonic-gate     FIFOLOWAT };
1587c478bd9Sstevel@tonic-gate struct module_info fifowm_info = { 0, "fifostrwhead", 0, 0, 0, 0 };
1597c478bd9Sstevel@tonic-gate struct qinit fifo_strdata = { strrput, NULL, NULL, NULL, NULL, &fiform_info };
1607c478bd9Sstevel@tonic-gate struct qinit fifo_stwdata = { NULL, strwsrv, NULL, NULL, NULL, &fifowm_info };
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate extern kmutex_t	strresources;	/* protects global resources */
1637c478bd9Sstevel@tonic-gate extern kmutex_t muxifier;	/* single-threads multiplexor creation */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static boolean_t msghasdata(mblk_t *bp);
1667c478bd9Sstevel@tonic-gate #define	msgnodata(bp) (!msghasdata(bp))
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Stream head locking notes:
1707c478bd9Sstevel@tonic-gate  *	There are four monitors associated with the stream head:
1717c478bd9Sstevel@tonic-gate  *	1. v_stream monitor: in stropen() and strclose() v_lock
1727c478bd9Sstevel@tonic-gate  *		is held while the association of vnode and stream
1737c478bd9Sstevel@tonic-gate  *		head is established or tested for.
1747c478bd9Sstevel@tonic-gate  *	2. open/close/push/pop monitor: sd_lock is held while each
1757c478bd9Sstevel@tonic-gate  *		thread bids for exclusive access to this monitor
1767c478bd9Sstevel@tonic-gate  *		for opening or closing a stream.  In addition, this
1777c478bd9Sstevel@tonic-gate  *		monitor is entered during pushes and pops.  This
1787c478bd9Sstevel@tonic-gate  *		guarantees that during plumbing operations there
1797c478bd9Sstevel@tonic-gate  *		is only one thread trying to change the plumbing.
1807c478bd9Sstevel@tonic-gate  *		Any other threads present in the stream are only
1817c478bd9Sstevel@tonic-gate  *		using the plumbing.
1827c478bd9Sstevel@tonic-gate  *	3. read/write monitor: in the case of read, a thread holds
1837c478bd9Sstevel@tonic-gate  *		sd_lock while trying to get data from the stream
1847c478bd9Sstevel@tonic-gate  *		head queue.  if there is none to fulfill a read
1857c478bd9Sstevel@tonic-gate  *		request, it sets RSLEEP and calls cv_wait_sig() down
1867c478bd9Sstevel@tonic-gate  *		in strwaitq() to await the arrival of new data.
1877c478bd9Sstevel@tonic-gate  *		when new data arrives in strrput(), sd_lock is acquired
1887c478bd9Sstevel@tonic-gate  *		before testing for RSLEEP and calling cv_broadcast().
1897c478bd9Sstevel@tonic-gate  *		the behavior of strwrite(), strwsrv(), and WSLEEP
1907c478bd9Sstevel@tonic-gate  *		mirror this.
1917c478bd9Sstevel@tonic-gate  *	4. ioctl monitor: sd_lock is gotten to ensure that only one
1927c478bd9Sstevel@tonic-gate  *		thread is doing an ioctl at a time.
19317169044Sbrutus  *
19417169044Sbrutus  * Note, for sodirect case 3. is extended to (*sodirect_t.sod_enqueue)()
19517169044Sbrutus  * call-back from below, further the sodirect support is for code paths
19617169044Sbrutus  * called via kstgetmsg(), all other code paths ASSERT() that sodirect
19717169044Sbrutus  * uioa generated mblk_t's (i.e. DBLK_UIOA) aren't processed.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static int
2017c478bd9Sstevel@tonic-gate push_mod(queue_t *qp, dev_t *devp, struct stdata *stp, const char *name,
202f4b3ec61Sdh155122     int anchor, cred_t *crp, uint_t anchor_zoneid)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	int error;
2057c478bd9Sstevel@tonic-gate 	fmodsw_impl_t *fp;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STRHUP|STRDERR|STWRERR)) {
2087c478bd9Sstevel@tonic-gate 		error = (stp->sd_flag & STRHUP) ? ENXIO : EIO;
2097c478bd9Sstevel@tonic-gate 		return (error);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	if (stp->sd_pushcnt >= nstrpush) {
2127c478bd9Sstevel@tonic-gate 		return (EINVAL);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if ((fp = fmodsw_find(name, FMODSW_HOLD | FMODSW_LOAD)) == NULL) {
2167c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STREOPENFAIL;
2177c478bd9Sstevel@tonic-gate 		return (EINVAL);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/*
2217c478bd9Sstevel@tonic-gate 	 * push new module and call its open routine via qattach
2227c478bd9Sstevel@tonic-gate 	 */
2237c478bd9Sstevel@tonic-gate 	if ((error = qattach(qp, devp, 0, crp, fp, B_FALSE)) != 0)
2247c478bd9Sstevel@tonic-gate 		return (error);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Check to see if caller wants a STREAMS anchor
2287c478bd9Sstevel@tonic-gate 	 * put at this place in the stream, and add if so.
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
231f4b3ec61Sdh155122 	if (anchor == stp->sd_pushcnt) {
2327c478bd9Sstevel@tonic-gate 		stp->sd_anchor = stp->sd_pushcnt;
233f4b3ec61Sdh155122 		stp->sd_anchorzone = anchor_zoneid;
234f4b3ec61Sdh155122 	}
2357c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	return (0);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * Open a stream device.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate int
2447c478bd9Sstevel@tonic-gate stropen(vnode_t *vp, dev_t *devp, int flag, cred_t *crp)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	struct stdata *stp;
2477c478bd9Sstevel@tonic-gate 	queue_t *qp;
2487c478bd9Sstevel@tonic-gate 	int s;
249d62bc4baSyz147064 	dev_t dummydev, savedev;
2507c478bd9Sstevel@tonic-gate 	struct autopush *ap;
251d62bc4baSyz147064 	struct dlautopush dlap;
2527c478bd9Sstevel@tonic-gate 	int error = 0;
2537c478bd9Sstevel@tonic-gate 	ssize_t	rmin, rmax;
2547c478bd9Sstevel@tonic-gate 	int cloneopen;
2557c478bd9Sstevel@tonic-gate 	queue_t *brq;
2567c478bd9Sstevel@tonic-gate 	major_t major;
257f4b3ec61Sdh155122 	str_stack_t *ss;
258f4b3ec61Sdh155122 	zoneid_t zoneid;
259f4b3ec61Sdh155122 	uint_t anchor;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (audit_active)
2627c478bd9Sstevel@tonic-gate 		audit_stropen(vp, devp, flag, crp);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * If the stream already exists, wait for any open in progress
2667c478bd9Sstevel@tonic-gate 	 * to complete, then call the open function of each module and
2677c478bd9Sstevel@tonic-gate 	 * driver in the stream.  Otherwise create the stream.
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_STROPEN, "stropen:%p", vp);
2707c478bd9Sstevel@tonic-gate retry:
2717c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
2727c478bd9Sstevel@tonic-gate 	if ((stp = vp->v_stream) != NULL) {
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		/*
2757c478bd9Sstevel@tonic-gate 		 * Waiting for stream to be created to device
2767c478bd9Sstevel@tonic-gate 		 * due to another open.
2777c478bd9Sstevel@tonic-gate 		 */
2787c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		if (STRMATED(stp)) {
2817c478bd9Sstevel@tonic-gate 			struct stdata *strmatep = stp->sd_mate;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			STRLOCKMATES(stp);
2847c478bd9Sstevel@tonic-gate 			if (strmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
2857c478bd9Sstevel@tonic-gate 				if (flag & (FNDELAY|FNONBLOCK)) {
2867c478bd9Sstevel@tonic-gate 					error = EAGAIN;
2877c478bd9Sstevel@tonic-gate 					mutex_exit(&strmatep->sd_lock);
2887c478bd9Sstevel@tonic-gate 					goto ckreturn;
2897c478bd9Sstevel@tonic-gate 				}
2907c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
2917c478bd9Sstevel@tonic-gate 				if (!cv_wait_sig(&strmatep->sd_monitor,
2927c478bd9Sstevel@tonic-gate 				    &strmatep->sd_lock)) {
2937c478bd9Sstevel@tonic-gate 					error = EINTR;
2947c478bd9Sstevel@tonic-gate 					mutex_exit(&strmatep->sd_lock);
2957c478bd9Sstevel@tonic-gate 					mutex_enter(&stp->sd_lock);
2967c478bd9Sstevel@tonic-gate 					goto ckreturn;
2977c478bd9Sstevel@tonic-gate 				}
2987c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
2997c478bd9Sstevel@tonic-gate 				goto retry;
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
3027c478bd9Sstevel@tonic-gate 				if (flag & (FNDELAY|FNONBLOCK)) {
3037c478bd9Sstevel@tonic-gate 					error = EAGAIN;
3047c478bd9Sstevel@tonic-gate 					mutex_exit(&strmatep->sd_lock);
3057c478bd9Sstevel@tonic-gate 					goto ckreturn;
3067c478bd9Sstevel@tonic-gate 				}
3077c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
308d3e55dcdSgww 				if (!cv_wait_sig(&stp->sd_monitor,
309d3e55dcdSgww 				    &stp->sd_lock)) {
3107c478bd9Sstevel@tonic-gate 					error = EINTR;
3117c478bd9Sstevel@tonic-gate 					goto ckreturn;
3127c478bd9Sstevel@tonic-gate 				}
3137c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
3147c478bd9Sstevel@tonic-gate 				goto retry;
3157c478bd9Sstevel@tonic-gate 			}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STRDERR|STWRERR)) {
3187c478bd9Sstevel@tonic-gate 				error = EIO;
3197c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
3207c478bd9Sstevel@tonic-gate 				goto ckreturn;
3217c478bd9Sstevel@tonic-gate 			}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STWOPEN;
3247c478bd9Sstevel@tonic-gate 			STRUNLOCKMATES(stp);
3257c478bd9Sstevel@tonic-gate 		} else {
3267c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
3277c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
3287c478bd9Sstevel@tonic-gate 				if (flag & (FNDELAY|FNONBLOCK)) {
3297c478bd9Sstevel@tonic-gate 					error = EAGAIN;
3307c478bd9Sstevel@tonic-gate 					goto ckreturn;
3317c478bd9Sstevel@tonic-gate 				}
332d3e55dcdSgww 				if (!cv_wait_sig(&stp->sd_monitor,
333d3e55dcdSgww 				    &stp->sd_lock)) {
3347c478bd9Sstevel@tonic-gate 					error = EINTR;
3357c478bd9Sstevel@tonic-gate 					goto ckreturn;
3367c478bd9Sstevel@tonic-gate 				}
3377c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
3387c478bd9Sstevel@tonic-gate 				goto retry;  /* could be clone! */
3397c478bd9Sstevel@tonic-gate 			}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STRDERR|STWRERR)) {
3427c478bd9Sstevel@tonic-gate 				error = EIO;
3437c478bd9Sstevel@tonic-gate 				goto ckreturn;
3447c478bd9Sstevel@tonic-gate 			}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STWOPEN;
3477c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		/*
3517c478bd9Sstevel@tonic-gate 		 * Open all modules and devices down stream to notify
3527c478bd9Sstevel@tonic-gate 		 * that another user is streaming.  For modules, set the
3537c478bd9Sstevel@tonic-gate 		 * last argument to MODOPEN and do not pass any open flags.
3547c478bd9Sstevel@tonic-gate 		 * Ignore dummydev since this is not the first open.
3557c478bd9Sstevel@tonic-gate 		 */
3567c478bd9Sstevel@tonic-gate 		claimstr(stp->sd_wrq);
3577c478bd9Sstevel@tonic-gate 		qp = stp->sd_wrq;
3587c478bd9Sstevel@tonic-gate 		while (_SAMESTR(qp)) {
3597c478bd9Sstevel@tonic-gate 			qp = qp->q_next;
3607c478bd9Sstevel@tonic-gate 			if ((error = qreopen(_RD(qp), devp, flag, crp)) != 0)
3617c478bd9Sstevel@tonic-gate 				break;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		releasestr(stp->sd_wrq);
3647c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
3657c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~(STRHUP|STWOPEN|STRDERR|STWRERR);
3667c478bd9Sstevel@tonic-gate 		stp->sd_rerror = 0;
3677c478bd9Sstevel@tonic-gate 		stp->sd_werror = 0;
3687c478bd9Sstevel@tonic-gate ckreturn:
3697c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);
3707c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
3717c478bd9Sstevel@tonic-gate 		return (error);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * This vnode isn't streaming.  SPECFS already
3767c478bd9Sstevel@tonic-gate 	 * checked for multiple vnodes pointing to the
3777c478bd9Sstevel@tonic-gate 	 * same stream, so create a stream to the driver.
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	qp = allocq();
3807c478bd9Sstevel@tonic-gate 	stp = shalloc(qp);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Initialize stream head.  shalloc() has given us
3847c478bd9Sstevel@tonic-gate 	 * exclusive access, and we have the vnode locked;
3857c478bd9Sstevel@tonic-gate 	 * we can do whatever we want with stp.
3867c478bd9Sstevel@tonic-gate 	 */
3877c478bd9Sstevel@tonic-gate 	stp->sd_flag = STWOPEN;
3887c478bd9Sstevel@tonic-gate 	stp->sd_siglist = NULL;
3897c478bd9Sstevel@tonic-gate 	stp->sd_pollist.ph_list = NULL;
3907c478bd9Sstevel@tonic-gate 	stp->sd_sigflags = 0;
3917c478bd9Sstevel@tonic-gate 	stp->sd_mark = NULL;
3927c478bd9Sstevel@tonic-gate 	stp->sd_closetime = STRTIMOUT;
3937c478bd9Sstevel@tonic-gate 	stp->sd_sidp = NULL;
3947c478bd9Sstevel@tonic-gate 	stp->sd_pgidp = NULL;
3957c478bd9Sstevel@tonic-gate 	stp->sd_vnode = vp;
3967c478bd9Sstevel@tonic-gate 	stp->sd_rerror = 0;
3977c478bd9Sstevel@tonic-gate 	stp->sd_werror = 0;
3987c478bd9Sstevel@tonic-gate 	stp->sd_wroff = 0;
399c28749e9Skais 	stp->sd_tail = 0;
4007c478bd9Sstevel@tonic-gate 	stp->sd_iocblk = NULL;
401ca9327a6Smeem 	stp->sd_cmdblk = NULL;
4027c478bd9Sstevel@tonic-gate 	stp->sd_pushcnt = 0;
4037c478bd9Sstevel@tonic-gate 	stp->sd_qn_minpsz = 0;
4047c478bd9Sstevel@tonic-gate 	stp->sd_qn_maxpsz = INFPSZ - 1;	/* used to check for initialization */
4057c478bd9Sstevel@tonic-gate 	stp->sd_maxblk = INFPSZ;
40617169044Sbrutus 	stp->sd_sodirect = NULL;
4077c478bd9Sstevel@tonic-gate 	qp->q_ptr = _WR(qp)->q_ptr = stp;
4087c478bd9Sstevel@tonic-gate 	STREAM(qp) = STREAM(_WR(qp)) = stp;
4097c478bd9Sstevel@tonic-gate 	vp->v_stream = stp;
4107c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
4117c478bd9Sstevel@tonic-gate 	if (vp->v_type == VFIFO) {
4127c478bd9Sstevel@tonic-gate 		stp->sd_flag |= OLDNDELAY;
4137c478bd9Sstevel@tonic-gate 		/*
4147c478bd9Sstevel@tonic-gate 		 * This means, both for pipes and fifos
4157c478bd9Sstevel@tonic-gate 		 * strwrite will send SIGPIPE if the other
4167c478bd9Sstevel@tonic-gate 		 * end is closed. For putmsg it depends
4177c478bd9Sstevel@tonic-gate 		 * on whether it is a XPG4_2 application
4187c478bd9Sstevel@tonic-gate 		 * or not
4197c478bd9Sstevel@tonic-gate 		 */
4207c478bd9Sstevel@tonic-gate 		stp->sd_wput_opt = SW_SIGPIPE;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		/* setq might sleep in kmem_alloc - avoid holding locks. */
4237c478bd9Sstevel@tonic-gate 		setq(qp, &fifo_strdata, &fifo_stwdata, NULL, QMTSAFE,
4247c478bd9Sstevel@tonic-gate 		    SQ_CI|SQ_CO, B_FALSE);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		set_qend(qp);
427ad1660d0Smeem 		stp->sd_strtab = fifo_getinfo();
4287c478bd9Sstevel@tonic-gate 		_WR(qp)->q_nfsrv = _WR(qp);
4297c478bd9Sstevel@tonic-gate 		qp->q_nfsrv = qp;
4307c478bd9Sstevel@tonic-gate 		/*
4317c478bd9Sstevel@tonic-gate 		 * Wake up others that are waiting for stream to be created.
4327c478bd9Sstevel@tonic-gate 		 */
4337c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
4347c478bd9Sstevel@tonic-gate 		/*
4357c478bd9Sstevel@tonic-gate 		 * nothing is be pushed on stream yet, so
4367c478bd9Sstevel@tonic-gate 		 * optimized stream head packetsizes are just that
4377c478bd9Sstevel@tonic-gate 		 * of the read queue
4387c478bd9Sstevel@tonic-gate 		 */
4397c478bd9Sstevel@tonic-gate 		stp->sd_qn_minpsz = qp->q_minpsz;
4407c478bd9Sstevel@tonic-gate 		stp->sd_qn_maxpsz = qp->q_maxpsz;
4417c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STWOPEN;
4427c478bd9Sstevel@tonic-gate 		goto fifo_opendone;
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	/* setq might sleep in kmem_alloc - avoid holding locks. */
4457c478bd9Sstevel@tonic-gate 	setq(qp, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_FALSE);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	set_qend(qp);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * Open driver and create stream to it (via qattach).
4517c478bd9Sstevel@tonic-gate 	 */
452d62bc4baSyz147064 	savedev = *devp;
4537c478bd9Sstevel@tonic-gate 	cloneopen = (getmajor(*devp) == clone_major);
4547c478bd9Sstevel@tonic-gate 	if ((error = qattach(qp, devp, flag, crp, NULL, B_FALSE)) != 0) {
4557c478bd9Sstevel@tonic-gate 		mutex_enter(&vp->v_lock);
4567c478bd9Sstevel@tonic-gate 		vp->v_stream = NULL;
4577c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
4587c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
4597c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);
4607c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
4617c478bd9Sstevel@tonic-gate 		freeq(_RD(qp));
4627c478bd9Sstevel@tonic-gate 		shfree(stp);
4637c478bd9Sstevel@tonic-gate 		return (error);
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * Set sd_strtab after open in order to handle clonable drivers
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 	stp->sd_strtab = STREAMSTAB(getmajor(*devp));
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Historical note: dummydev used to be be prior to the initial
4727c478bd9Sstevel@tonic-gate 	 * open (via qattach above), which made the value seen
4737c478bd9Sstevel@tonic-gate 	 * inconsistent between an I_PUSH and an autopush of a module.
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	dummydev = *devp;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * For clone open of old style (Q not associated) network driver,
4797c478bd9Sstevel@tonic-gate 	 * push DRMODNAME module to handle DL_ATTACH/DL_DETACH
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	brq = _RD(_WR(qp)->q_next);
4827c478bd9Sstevel@tonic-gate 	major = getmajor(*devp);
4837c478bd9Sstevel@tonic-gate 	if (push_drcompat && cloneopen && NETWORK_DRV(major) &&
4847c478bd9Sstevel@tonic-gate 	    ((brq->q_flag & _QASSOCIATED) == 0)) {
485f4b3ec61Sdh155122 		if (push_mod(qp, &dummydev, stp, DRMODNAME, 0, crp, 0) != 0)
4867c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot push " DRMODNAME
4877c478bd9Sstevel@tonic-gate 			    " streams module");
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
490d62bc4baSyz147064 	if (!NETWORK_DRV(major)) {
491d62bc4baSyz147064 		savedev = *devp;
492d62bc4baSyz147064 	} else {
4937c478bd9Sstevel@tonic-gate 		/*
494d62bc4baSyz147064 		 * For network devices, process differently based on the
495d62bc4baSyz147064 		 * return value from dld_autopush():
496d62bc4baSyz147064 		 *
497d62bc4baSyz147064 		 *   0: the passed-in device points to a GLDv3 datalink with
498d62bc4baSyz147064 		 *   per-link autopush configuration; use that configuration
499d62bc4baSyz147064 		 *   and ignore any per-driver autopush configuration.
500d62bc4baSyz147064 		 *
501d62bc4baSyz147064 		 *   1: the passed-in device points to a physical GLDv3
502d62bc4baSyz147064 		 *   datalink without per-link autopush configuration.  The
503d62bc4baSyz147064 		 *   passed in device was changed to refer to the actual
504d62bc4baSyz147064 		 *   physical device (if it's not already); we use that new
505d62bc4baSyz147064 		 *   device to look up any per-driver autopush configuration.
506d62bc4baSyz147064 		 *
507d62bc4baSyz147064 		 *   -1: neither of the above cases applied; use the initial
508d62bc4baSyz147064 		 *   device to look up any per-driver autopush configuration.
509d62bc4baSyz147064 		 */
510d62bc4baSyz147064 		switch (dld_autopush(&savedev, &dlap)) {
511d62bc4baSyz147064 		case 0:
512d62bc4baSyz147064 			zoneid = crgetzoneid(crp);
513d62bc4baSyz147064 			for (s = 0; s < dlap.dap_npush; s++) {
514d62bc4baSyz147064 				error = push_mod(qp, &dummydev, stp,
515d62bc4baSyz147064 				    dlap.dap_aplist[s], dlap.dap_anchor, crp,
516d62bc4baSyz147064 				    zoneid);
517d62bc4baSyz147064 				if (error != 0)
518d62bc4baSyz147064 					break;
519d62bc4baSyz147064 			}
520d62bc4baSyz147064 			goto opendone;
521d62bc4baSyz147064 		case 1:
522d62bc4baSyz147064 			break;
523d62bc4baSyz147064 		case -1:
524d62bc4baSyz147064 			savedev = *devp;
525d62bc4baSyz147064 			break;
526d62bc4baSyz147064 		}
527d62bc4baSyz147064 	}
528d62bc4baSyz147064 	/*
529d62bc4baSyz147064 	 * Find the autopush configuration based on "savedev". Start with the
530d62bc4baSyz147064 	 * global zone. If not found check in the local zone.
5317c478bd9Sstevel@tonic-gate 	 */
532f4b3ec61Sdh155122 	zoneid = GLOBAL_ZONEID;
533f4b3ec61Sdh155122 retryap:
534f4b3ec61Sdh155122 	ss = netstack_find_by_stackid(zoneid_to_netstackid(zoneid))->
535f4b3ec61Sdh155122 	    netstack_str;
536d62bc4baSyz147064 	if ((ap = sad_ap_find_by_dev(savedev, ss)) == NULL) {
537f4b3ec61Sdh155122 		netstack_rele(ss->ss_netstack);
538f4b3ec61Sdh155122 		if (zoneid == GLOBAL_ZONEID) {
539f4b3ec61Sdh155122 			/*
540f4b3ec61Sdh155122 			 * None found. Also look in the zone's autopush table.
541f4b3ec61Sdh155122 			 */
542f4b3ec61Sdh155122 			zoneid = crgetzoneid(crp);
543f4b3ec61Sdh155122 			if (zoneid != GLOBAL_ZONEID)
544f4b3ec61Sdh155122 				goto retryap;
545f4b3ec61Sdh155122 		}
5467c478bd9Sstevel@tonic-gate 		goto opendone;
547f4b3ec61Sdh155122 	}
548f4b3ec61Sdh155122 	anchor = ap->ap_anchor;
549f4b3ec61Sdh155122 	zoneid = crgetzoneid(crp);
5507c478bd9Sstevel@tonic-gate 	for (s = 0; s < ap->ap_npush; s++) {
5517c478bd9Sstevel@tonic-gate 		error = push_mod(qp, &dummydev, stp, ap->ap_list[s],
552f4b3ec61Sdh155122 		    anchor, crp, zoneid);
5537c478bd9Sstevel@tonic-gate 		if (error != 0)
5547c478bd9Sstevel@tonic-gate 			break;
5557c478bd9Sstevel@tonic-gate 	}
556f4b3ec61Sdh155122 	sad_ap_rele(ap, ss);
557f4b3ec61Sdh155122 	netstack_rele(ss->ss_netstack);
5587c478bd9Sstevel@tonic-gate 
559d62bc4baSyz147064 opendone:
560d62bc4baSyz147064 
5617c478bd9Sstevel@tonic-gate 	/*
5627c478bd9Sstevel@tonic-gate 	 * let specfs know that open failed part way through
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	if (error) {
5657c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
5667c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STREOPENFAIL;
5677c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/*
5717c478bd9Sstevel@tonic-gate 	 * Wake up others that are waiting for stream to be created.
5727c478bd9Sstevel@tonic-gate 	 */
5737c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
5747c478bd9Sstevel@tonic-gate 	stp->sd_flag &= ~STWOPEN;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/*
5777c478bd9Sstevel@tonic-gate 	 * As a performance concern we are caching the values of
5787c478bd9Sstevel@tonic-gate 	 * q_minpsz and q_maxpsz of the module below the stream
5797c478bd9Sstevel@tonic-gate 	 * head in the stream head.
5807c478bd9Sstevel@tonic-gate 	 */
5817c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(stp->sd_wrq->q_next));
5827c478bd9Sstevel@tonic-gate 	rmin = stp->sd_wrq->q_next->q_minpsz;
5837c478bd9Sstevel@tonic-gate 	rmax = stp->sd_wrq->q_next->q_maxpsz;
5847c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(stp->sd_wrq->q_next));
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* do this processing here as a performance concern */
5877c478bd9Sstevel@tonic-gate 	if (strmsgsz != 0) {
5887c478bd9Sstevel@tonic-gate 		if (rmax == INFPSZ)
5897c478bd9Sstevel@tonic-gate 			rmax = strmsgsz;
5907c478bd9Sstevel@tonic-gate 		else
5917c478bd9Sstevel@tonic-gate 			rmax = MIN(strmsgsz, rmax);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(stp->sd_wrq));
5957c478bd9Sstevel@tonic-gate 	stp->sd_qn_minpsz = rmin;
5967c478bd9Sstevel@tonic-gate 	stp->sd_qn_maxpsz = rmax;
5977c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(stp->sd_wrq));
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate fifo_opendone:
6007c478bd9Sstevel@tonic-gate 	cv_broadcast(&stp->sd_monitor);
6017c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
6027c478bd9Sstevel@tonic-gate 	return (error);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate static int strsink(queue_t *, mblk_t *);
6067c478bd9Sstevel@tonic-gate static struct qinit deadrend = {
6077c478bd9Sstevel@tonic-gate 	strsink, NULL, NULL, NULL, NULL, &strm_info, NULL
6087c478bd9Sstevel@tonic-gate };
6097c478bd9Sstevel@tonic-gate static struct qinit deadwend = {
6107c478bd9Sstevel@tonic-gate 	NULL, NULL, NULL, NULL, NULL, &stwm_info, NULL
6117c478bd9Sstevel@tonic-gate };
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate  * Close a stream.
6157c478bd9Sstevel@tonic-gate  * This is called from closef() on the last close of an open stream.
6167c478bd9Sstevel@tonic-gate  * Strclean() will already have removed the siglist and pollist
6177c478bd9Sstevel@tonic-gate  * information, so all that remains is to remove all multiplexor links
6187c478bd9Sstevel@tonic-gate  * for the stream, pop all the modules (and the driver), and free the
6197c478bd9Sstevel@tonic-gate  * stream structure.
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate int
6237c478bd9Sstevel@tonic-gate strclose(struct vnode *vp, int flag, cred_t *crp)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	struct stdata *stp;
6267c478bd9Sstevel@tonic-gate 	queue_t *qp;
6277c478bd9Sstevel@tonic-gate 	int rval;
6287c478bd9Sstevel@tonic-gate 	int freestp = 1;
6297c478bd9Sstevel@tonic-gate 	queue_t *rmq;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (audit_active)
6327c478bd9Sstevel@tonic-gate 		audit_strclose(vp, flag, crp);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
6357c478bd9Sstevel@tonic-gate 	    TR_STRCLOSE, "strclose:%p", vp);
6367c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
6397c478bd9Sstevel@tonic-gate 	ASSERT(!(stp->sd_flag & STPLEX));
6407c478bd9Sstevel@tonic-gate 	qp = stp->sd_wrq;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * Needed so that strpoll will return non-zero for this fd.
6447c478bd9Sstevel@tonic-gate 	 * Note that with POLLNOERR STRHUP does still cause POLLHUP.
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
6477c478bd9Sstevel@tonic-gate 	stp->sd_flag |= STRHUP;
6487c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * If the registered process or process group did not have an
6527c478bd9Sstevel@tonic-gate 	 * open instance of this stream then strclean would not be
6537c478bd9Sstevel@tonic-gate 	 * called. Thus at the time of closing all remaining siglist entries
6547c478bd9Sstevel@tonic-gate 	 * are removed.
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 	if (stp->sd_siglist != NULL)
6577c478bd9Sstevel@tonic-gate 		strcleanall(vp);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_siglist == NULL);
6607c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_sigflags == 0);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	if (STRMATED(stp)) {
6637c478bd9Sstevel@tonic-gate 		struct stdata *strmatep = stp->sd_mate;
6647c478bd9Sstevel@tonic-gate 		int waited = 1;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		STRLOCKMATES(stp);
6677c478bd9Sstevel@tonic-gate 		while (waited) {
6687c478bd9Sstevel@tonic-gate 			waited = 0;
6697c478bd9Sstevel@tonic-gate 			while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
6707c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
6717c478bd9Sstevel@tonic-gate 				cv_wait(&stp->sd_monitor, &stp->sd_lock);
6727c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
6737c478bd9Sstevel@tonic-gate 				STRLOCKMATES(stp);
6747c478bd9Sstevel@tonic-gate 				waited = 1;
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 			while (strmatep->sd_flag &
6777c478bd9Sstevel@tonic-gate 			    (STWOPEN|STRCLOSE|STRPLUMB)) {
6787c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
6797c478bd9Sstevel@tonic-gate 				cv_wait(&strmatep->sd_monitor,
6807c478bd9Sstevel@tonic-gate 				    &strmatep->sd_lock);
6817c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
6827c478bd9Sstevel@tonic-gate 				STRLOCKMATES(stp);
6837c478bd9Sstevel@tonic-gate 				waited = 1;
6847c478bd9Sstevel@tonic-gate 			}
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRCLOSE;
6877c478bd9Sstevel@tonic-gate 		STRUNLOCKMATES(stp);
6887c478bd9Sstevel@tonic-gate 	} else {
6897c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
6907c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRCLOSE;
6917c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_first == NULL);	/* No more delayed write */
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/* Check if an I_LINK was ever done on this stream */
6977c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & STRHASLINKS) {
698f4b3ec61Sdh155122 		netstack_t *ns;
699f4b3ec61Sdh155122 		str_stack_t *ss;
700f4b3ec61Sdh155122 
701f4b3ec61Sdh155122 		ns = netstack_find_by_cred(crp);
702f4b3ec61Sdh155122 		ASSERT(ns != NULL);
703f4b3ec61Sdh155122 		ss = ns->netstack_str;
704f4b3ec61Sdh155122 		ASSERT(ss != NULL);
705f4b3ec61Sdh155122 
706f4b3ec61Sdh155122 		(void) munlinkall(stp, LINKCLOSE|LINKNORMAL, crp, &rval, ss);
707f4b3ec61Sdh155122 		netstack_rele(ss->ss_netstack);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	while (_SAMESTR(qp)) {
7117c478bd9Sstevel@tonic-gate 		/*
7127c478bd9Sstevel@tonic-gate 		 * Holding sd_lock prevents q_next from changing in
7137c478bd9Sstevel@tonic-gate 		 * this stream.
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
7167c478bd9Sstevel@tonic-gate 		if (!(flag & (FNDELAY|FNONBLOCK)) && (stp->sd_closetime > 0)) {
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 			/*
7197c478bd9Sstevel@tonic-gate 			 * sleep until awakened by strwsrv() or timeout
7207c478bd9Sstevel@tonic-gate 			 */
7217c478bd9Sstevel@tonic-gate 			for (;;) {
7227c478bd9Sstevel@tonic-gate 				mutex_enter(QLOCK(qp->q_next));
7237c478bd9Sstevel@tonic-gate 				if (!(qp->q_next->q_mblkcnt)) {
7247c478bd9Sstevel@tonic-gate 					mutex_exit(QLOCK(qp->q_next));
7257c478bd9Sstevel@tonic-gate 					break;
7267c478bd9Sstevel@tonic-gate 				}
7277c478bd9Sstevel@tonic-gate 				stp->sd_flag |= WSLEEP;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 				/* ensure strwsrv gets enabled */
7307c478bd9Sstevel@tonic-gate 				qp->q_next->q_flag |= QWANTW;
7317c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(qp->q_next));
7327c478bd9Sstevel@tonic-gate 				/* get out if we timed out or recv'd a signal */
7337c478bd9Sstevel@tonic-gate 				if (str_cv_wait(&qp->q_wait, &stp->sd_lock,
7347c478bd9Sstevel@tonic-gate 				    stp->sd_closetime, 0) <= 0) {
7357c478bd9Sstevel@tonic-gate 					break;
7367c478bd9Sstevel@tonic-gate 				}
7377c478bd9Sstevel@tonic-gate 			}
7387c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~WSLEEP;
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		rmq = qp->q_next;
7437c478bd9Sstevel@tonic-gate 		if (rmq->q_flag & QISDRV) {
7447c478bd9Sstevel@tonic-gate 			ASSERT(!_SAMESTR(rmq));
7457c478bd9Sstevel@tonic-gate 			wait_sq_svc(_RD(qp)->q_syncq);
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		qdetach(_RD(rmq), 1, flag, crp, B_FALSE);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7518ec5a142Sedp 	/*
7528ec5a142Sedp 	 * Since we call pollwakeup in close() now, the poll list should
7538ec5a142Sedp 	 * be empty in most cases. The only exception is the layered devices
7548ec5a142Sedp 	 * (e.g. the console drivers with redirection modules pushed on top
7558ec5a142Sedp 	 * of it).  We have to do this after calling qdetach() because
7568ec5a142Sedp 	 * the redirection module won't have torn down the console
7578ec5a142Sedp 	 * redirection until after qdetach() has been invoked.
7588ec5a142Sedp 	 */
7598ec5a142Sedp 	if (stp->sd_pollist.ph_list != NULL) {
7608ec5a142Sedp 		pollwakeup(&stp->sd_pollist, POLLERR);
7618ec5a142Sedp 		pollhead_clean(&stp->sd_pollist);
7628ec5a142Sedp 	}
7638ec5a142Sedp 	ASSERT(stp->sd_pollist.ph_list == NULL);
7648ec5a142Sedp 	ASSERT(stp->sd_sidp == NULL);
7658ec5a142Sedp 	ASSERT(stp->sd_pgidp == NULL);
7668ec5a142Sedp 
7677c478bd9Sstevel@tonic-gate 	/* Prevent qenable from re-enabling the stream head queue */
7687c478bd9Sstevel@tonic-gate 	disable_svc(_RD(qp));
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	/*
7717c478bd9Sstevel@tonic-gate 	 * Wait until service procedure of each queue is
7727c478bd9Sstevel@tonic-gate 	 * run, if QINSERVICE is set.
7737c478bd9Sstevel@tonic-gate 	 */
7747c478bd9Sstevel@tonic-gate 	wait_svc(_RD(qp));
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/*
7777c478bd9Sstevel@tonic-gate 	 * Now, flush both queues.
7787c478bd9Sstevel@tonic-gate 	 */
7797c478bd9Sstevel@tonic-gate 	flushq(_RD(qp), FLUSHALL);
7807c478bd9Sstevel@tonic-gate 	flushq(qp, FLUSHALL);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	/*
7837c478bd9Sstevel@tonic-gate 	 * If the write queue of the stream head is pointing to a
7847c478bd9Sstevel@tonic-gate 	 * read queue, we have a twisted stream.  If the read queue
7857c478bd9Sstevel@tonic-gate 	 * is alive, convert the stream head queues into a dead end.
7867c478bd9Sstevel@tonic-gate 	 * If the read queue is dead, free the dead pair.
7877c478bd9Sstevel@tonic-gate 	 */
7887c478bd9Sstevel@tonic-gate 	if (qp->q_next && !_SAMESTR(qp)) {
7897c478bd9Sstevel@tonic-gate 		if (qp->q_next->q_qinfo == &deadrend) {	/* half-closed pipe */
7907c478bd9Sstevel@tonic-gate 			flushq(qp->q_next, FLUSHALL); /* ensure no message */
7917c478bd9Sstevel@tonic-gate 			shfree(qp->q_next->q_stream);
7927c478bd9Sstevel@tonic-gate 			freeq(qp->q_next);
7937c478bd9Sstevel@tonic-gate 			freeq(_RD(qp));
7947c478bd9Sstevel@tonic-gate 		} else if (qp->q_next == _RD(qp)) {	/* fifo */
7957c478bd9Sstevel@tonic-gate 			freeq(_RD(qp));
7967c478bd9Sstevel@tonic-gate 		} else {				/* pipe */
7977c478bd9Sstevel@tonic-gate 			freestp = 0;
7987c478bd9Sstevel@tonic-gate 			/*
7997c478bd9Sstevel@tonic-gate 			 * The q_info pointers are never accessed when
8007c478bd9Sstevel@tonic-gate 			 * SQLOCK is held.
8017c478bd9Sstevel@tonic-gate 			 */
8027c478bd9Sstevel@tonic-gate 			ASSERT(qp->q_syncq == _RD(qp)->q_syncq);
8037c478bd9Sstevel@tonic-gate 			mutex_enter(SQLOCK(qp->q_syncq));
8047c478bd9Sstevel@tonic-gate 			qp->q_qinfo = &deadwend;
8057c478bd9Sstevel@tonic-gate 			_RD(qp)->q_qinfo = &deadrend;
8067c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(qp->q_syncq));
8077c478bd9Sstevel@tonic-gate 		}
8087c478bd9Sstevel@tonic-gate 	} else {
8097c478bd9Sstevel@tonic-gate 		freeq(_RD(qp)); /* free stream head queue pair */
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
8137c478bd9Sstevel@tonic-gate 	if (stp->sd_iocblk) {
8147c478bd9Sstevel@tonic-gate 		if (stp->sd_iocblk != (mblk_t *)-1) {
8157c478bd9Sstevel@tonic-gate 			freemsg(stp->sd_iocblk);
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = NULL;
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 	stp->sd_vnode = NULL;
8207c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
8217c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
8227c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
823ca9327a6Smeem 	freemsg(stp->sd_cmdblk);
824ca9327a6Smeem 	stp->sd_cmdblk = NULL;
8257c478bd9Sstevel@tonic-gate 	stp->sd_flag &= ~STRCLOSE;
8267c478bd9Sstevel@tonic-gate 	cv_broadcast(&stp->sd_monitor);
8277c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if (freestp)
8307c478bd9Sstevel@tonic-gate 		shfree(stp);
8317c478bd9Sstevel@tonic-gate 	return (0);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate static int
8357c478bd9Sstevel@tonic-gate strsink(queue_t *q, mblk_t *bp)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	struct copyresp *resp;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	switch (bp->b_datap->db_type) {
8407c478bd9Sstevel@tonic-gate 	case M_FLUSH:
8417c478bd9Sstevel@tonic-gate 		if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
8427c478bd9Sstevel@tonic-gate 			*bp->b_rptr &= ~FLUSHR;
8437c478bd9Sstevel@tonic-gate 			bp->b_flag |= MSGNOLOOP;
8447c478bd9Sstevel@tonic-gate 			/*
8457c478bd9Sstevel@tonic-gate 			 * Protect against the driver passing up
8467c478bd9Sstevel@tonic-gate 			 * messages after it has done a qprocsoff.
8477c478bd9Sstevel@tonic-gate 			 */
8487c478bd9Sstevel@tonic-gate 			if (_OTHERQ(q)->q_next == NULL)
8497c478bd9Sstevel@tonic-gate 				freemsg(bp);
8507c478bd9Sstevel@tonic-gate 			else
8517c478bd9Sstevel@tonic-gate 				qreply(q, bp);
8527c478bd9Sstevel@tonic-gate 		} else {
8537c478bd9Sstevel@tonic-gate 			freemsg(bp);
8547c478bd9Sstevel@tonic-gate 		}
8557c478bd9Sstevel@tonic-gate 		break;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	case M_COPYIN:
8587c478bd9Sstevel@tonic-gate 	case M_COPYOUT:
8597c478bd9Sstevel@tonic-gate 		if (bp->b_cont) {
8607c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
8617c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCDATA;
8647c478bd9Sstevel@tonic-gate 		bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
8657c478bd9Sstevel@tonic-gate 		resp = (struct copyresp *)bp->b_rptr;
8667c478bd9Sstevel@tonic-gate 		resp->cp_rval = (caddr_t)1;	/* failure */
8677c478bd9Sstevel@tonic-gate 		/*
8687c478bd9Sstevel@tonic-gate 		 * Protect against the driver passing up
8697c478bd9Sstevel@tonic-gate 		 * messages after it has done a qprocsoff.
8707c478bd9Sstevel@tonic-gate 		 */
8717c478bd9Sstevel@tonic-gate 		if (_OTHERQ(q)->q_next == NULL)
8727c478bd9Sstevel@tonic-gate 			freemsg(bp);
8737c478bd9Sstevel@tonic-gate 		else
8747c478bd9Sstevel@tonic-gate 			qreply(q, bp);
8757c478bd9Sstevel@tonic-gate 		break;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	case M_IOCTL:
8787c478bd9Sstevel@tonic-gate 		if (bp->b_cont) {
8797c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
8807c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCNAK;
8837c478bd9Sstevel@tonic-gate 		/*
8847c478bd9Sstevel@tonic-gate 		 * Protect against the driver passing up
8857c478bd9Sstevel@tonic-gate 		 * messages after it has done a qprocsoff.
8867c478bd9Sstevel@tonic-gate 		 */
8877c478bd9Sstevel@tonic-gate 		if (_OTHERQ(q)->q_next == NULL)
8887c478bd9Sstevel@tonic-gate 			freemsg(bp);
8897c478bd9Sstevel@tonic-gate 		else
8907c478bd9Sstevel@tonic-gate 			qreply(q, bp);
8917c478bd9Sstevel@tonic-gate 		break;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	default:
8947c478bd9Sstevel@tonic-gate 		freemsg(bp);
8957c478bd9Sstevel@tonic-gate 		break;
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	return (0);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate  * Clean up after a process when it closes a stream.  This is called
9037c478bd9Sstevel@tonic-gate  * from closef for all closes, whereas strclose is called only for the
9047c478bd9Sstevel@tonic-gate  * last close on a stream.  The siglist is scanned for entries for the
9057c478bd9Sstevel@tonic-gate  * current process, and these are removed.
9067c478bd9Sstevel@tonic-gate  */
9077c478bd9Sstevel@tonic-gate void
9087c478bd9Sstevel@tonic-gate strclean(struct vnode *vp)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	strsig_t *ssp, *pssp, *tssp;
9117c478bd9Sstevel@tonic-gate 	stdata_t *stp;
9127c478bd9Sstevel@tonic-gate 	int update = 0;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
9157c478bd9Sstevel@tonic-gate 	    TR_STRCLEAN, "strclean:%p", vp);
9167c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
9177c478bd9Sstevel@tonic-gate 	pssp = NULL;
9187c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
9197c478bd9Sstevel@tonic-gate 	ssp = stp->sd_siglist;
9207c478bd9Sstevel@tonic-gate 	while (ssp) {
9217c478bd9Sstevel@tonic-gate 		if (ssp->ss_pidp == curproc->p_pidp) {
9227c478bd9Sstevel@tonic-gate 			tssp = ssp->ss_next;
9237c478bd9Sstevel@tonic-gate 			if (pssp)
9247c478bd9Sstevel@tonic-gate 				pssp->ss_next = tssp;
9257c478bd9Sstevel@tonic-gate 			else
9267c478bd9Sstevel@tonic-gate 				stp->sd_siglist = tssp;
9277c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
9287c478bd9Sstevel@tonic-gate 			PID_RELE(ssp->ss_pidp);
9297c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
9307c478bd9Sstevel@tonic-gate 			kmem_free(ssp, sizeof (strsig_t));
9317c478bd9Sstevel@tonic-gate 			update = 1;
9327c478bd9Sstevel@tonic-gate 			ssp = tssp;
9337c478bd9Sstevel@tonic-gate 		} else {
9347c478bd9Sstevel@tonic-gate 			pssp = ssp;
9357c478bd9Sstevel@tonic-gate 			ssp = ssp->ss_next;
9367c478bd9Sstevel@tonic-gate 		}
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 	if (update) {
9397c478bd9Sstevel@tonic-gate 		stp->sd_sigflags = 0;
9407c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
9417c478bd9Sstevel@tonic-gate 			stp->sd_sigflags |= ssp->ss_events;
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * Used on the last close to remove any remaining items on the siglist.
9487c478bd9Sstevel@tonic-gate  * These could be present on the siglist due to I_ESETSIG calls that
9497c478bd9Sstevel@tonic-gate  * use process groups or processed that do not have an open file descriptor
9507c478bd9Sstevel@tonic-gate  * for this stream (Such entries would not be removed by strclean).
9517c478bd9Sstevel@tonic-gate  */
9527c478bd9Sstevel@tonic-gate static void
9537c478bd9Sstevel@tonic-gate strcleanall(struct vnode *vp)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 	strsig_t *ssp, *nssp;
9567c478bd9Sstevel@tonic-gate 	stdata_t *stp;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
9597c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
9607c478bd9Sstevel@tonic-gate 	ssp = stp->sd_siglist;
9617c478bd9Sstevel@tonic-gate 	stp->sd_siglist = NULL;
9627c478bd9Sstevel@tonic-gate 	while (ssp) {
9637c478bd9Sstevel@tonic-gate 		nssp = ssp->ss_next;
9647c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
9657c478bd9Sstevel@tonic-gate 		PID_RELE(ssp->ss_pidp);
9667c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
9677c478bd9Sstevel@tonic-gate 		kmem_free(ssp, sizeof (strsig_t));
9687c478bd9Sstevel@tonic-gate 		ssp = nssp;
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 	stp->sd_sigflags = 0;
9717c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate /*
9757c478bd9Sstevel@tonic-gate  * Retrieve the next message from the logical stream head read queue
9767c478bd9Sstevel@tonic-gate  * using either rwnext (if sync stream) or getq_noenab.
9777c478bd9Sstevel@tonic-gate  * It is the callers responsibility to call qbackenable after
9787c478bd9Sstevel@tonic-gate  * it is finished with the message. The caller should not call
9797c478bd9Sstevel@tonic-gate  * qbackenable until after any putback calls to avoid spurious backenabling.
98017169044Sbrutus  *
98117169044Sbrutus  * Also, handle uioa initialization and process any DBLK_UIOA flaged messages.
9827c478bd9Sstevel@tonic-gate  */
9837c478bd9Sstevel@tonic-gate mblk_t *
9847c478bd9Sstevel@tonic-gate strget(struct stdata *stp, queue_t *q, struct uio *uiop, int first,
9857c478bd9Sstevel@tonic-gate     int *errorp)
9867c478bd9Sstevel@tonic-gate {
98717169044Sbrutus 	sodirect_t *sodp = stp->sd_sodirect;
9887c478bd9Sstevel@tonic-gate 	mblk_t *bp;
9897c478bd9Sstevel@tonic-gate 	int error;
990*301ce41fSja97890 	ssize_t rbytes = 0;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	/* Holding sd_lock prevents the read queue from changing  */
993*301ce41fSja97890 	ASSERT(MUTEX_HELD(&stp->sd_lock));
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	if (uiop != NULL && stp->sd_struiordq != NULL &&
9967c478bd9Sstevel@tonic-gate 	    q->q_first == NULL &&
9977c478bd9Sstevel@tonic-gate 	    (!first || (stp->sd_wakeq & RSLEEP))) {
9987c478bd9Sstevel@tonic-gate 		/*
9997c478bd9Sstevel@tonic-gate 		 * Stream supports rwnext() for the read side.
10007c478bd9Sstevel@tonic-gate 		 * If this is the first time we're called by e.g. strread
10017c478bd9Sstevel@tonic-gate 		 * only do the downcall if there is a deferred wakeup
10027c478bd9Sstevel@tonic-gate 		 * (registered in sd_wakeq).
10037c478bd9Sstevel@tonic-gate 		 */
10047c478bd9Sstevel@tonic-gate 		struiod_t uiod;
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 		if (first)
10077c478bd9Sstevel@tonic-gate 			stp->sd_wakeq &= ~RSLEEP;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		(void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
10107c478bd9Sstevel@tonic-gate 		    sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
10117c478bd9Sstevel@tonic-gate 		uiod.d_mp = 0;
10127c478bd9Sstevel@tonic-gate 		/*
10137c478bd9Sstevel@tonic-gate 		 * Mark that a thread is in rwnext on the read side
10147c478bd9Sstevel@tonic-gate 		 * to prevent strrput from nacking ioctls immediately.
10157c478bd9Sstevel@tonic-gate 		 * When the last concurrent rwnext returns
10167c478bd9Sstevel@tonic-gate 		 * the ioctls are nack'ed.
10177c478bd9Sstevel@tonic-gate 		 */
10187c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&stp->sd_lock));
10197c478bd9Sstevel@tonic-gate 		stp->sd_struiodnak++;
10207c478bd9Sstevel@tonic-gate 		/*
10217c478bd9Sstevel@tonic-gate 		 * Note: rwnext will drop sd_lock.
10227c478bd9Sstevel@tonic-gate 		 */
10237c478bd9Sstevel@tonic-gate 		error = rwnext(q, &uiod);
10247c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
10257c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
10267c478bd9Sstevel@tonic-gate 		stp->sd_struiodnak--;
10277c478bd9Sstevel@tonic-gate 		while (stp->sd_struiodnak == 0 &&
10287c478bd9Sstevel@tonic-gate 		    ((bp = stp->sd_struionak) != NULL)) {
10297c478bd9Sstevel@tonic-gate 			stp->sd_struionak = bp->b_next;
10307c478bd9Sstevel@tonic-gate 			bp->b_next = NULL;
10317c478bd9Sstevel@tonic-gate 			bp->b_datap->db_type = M_IOCNAK;
10327c478bd9Sstevel@tonic-gate 			/*
10337c478bd9Sstevel@tonic-gate 			 * Protect against the driver passing up
10347c478bd9Sstevel@tonic-gate 			 * messages after it has done a qprocsoff.
10357c478bd9Sstevel@tonic-gate 			 */
10367c478bd9Sstevel@tonic-gate 			if (_OTHERQ(q)->q_next == NULL)
10377c478bd9Sstevel@tonic-gate 				freemsg(bp);
10387c478bd9Sstevel@tonic-gate 			else {
10397c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
10407c478bd9Sstevel@tonic-gate 				qreply(q, bp);
10417c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
10427c478bd9Sstevel@tonic-gate 			}
10437c478bd9Sstevel@tonic-gate 		}
10447c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&stp->sd_lock));
10457c478bd9Sstevel@tonic-gate 		if (error == 0 || error == EWOULDBLOCK) {
10467c478bd9Sstevel@tonic-gate 			if ((bp = uiod.d_mp) != NULL) {
10477c478bd9Sstevel@tonic-gate 				*errorp = 0;
10487c478bd9Sstevel@tonic-gate 				ASSERT(MUTEX_HELD(&stp->sd_lock));
10497c478bd9Sstevel@tonic-gate 				return (bp);
10507c478bd9Sstevel@tonic-gate 			}
10517c478bd9Sstevel@tonic-gate 			error = 0;
10527c478bd9Sstevel@tonic-gate 		} else if (error == EINVAL) {
10537c478bd9Sstevel@tonic-gate 			/*
10547c478bd9Sstevel@tonic-gate 			 * The stream plumbing must have
10557c478bd9Sstevel@tonic-gate 			 * changed while we were away, so
10567c478bd9Sstevel@tonic-gate 			 * just turn off rwnext()s.
10577c478bd9Sstevel@tonic-gate 			 */
10587c478bd9Sstevel@tonic-gate 			error = 0;
10597c478bd9Sstevel@tonic-gate 		} else if (error == EBUSY) {
10607c478bd9Sstevel@tonic-gate 			/*
10617c478bd9Sstevel@tonic-gate 			 * The module might have data in transit using putnext
10627c478bd9Sstevel@tonic-gate 			 * Fall back on waiting + getq.
10637c478bd9Sstevel@tonic-gate 			 */
10647c478bd9Sstevel@tonic-gate 			error = 0;
10657c478bd9Sstevel@tonic-gate 		} else {
10667c478bd9Sstevel@tonic-gate 			*errorp = error;
10677c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(&stp->sd_lock));
10687c478bd9Sstevel@tonic-gate 			return (NULL);
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 		/*
10717c478bd9Sstevel@tonic-gate 		 * Try a getq in case a rwnext() generated mblk
10727c478bd9Sstevel@tonic-gate 		 * has bubbled up via strrput().
10737c478bd9Sstevel@tonic-gate 		 */
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 	*errorp = 0;
10767c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
1077*301ce41fSja97890 
107817169044Sbrutus 	if (sodp != NULL && (sodp->sod_state & SOD_ENABLED) &&
107917169044Sbrutus 	    (sodp->sod_uioa.uioa_state & UIOA_INIT)) {
108017169044Sbrutus 		/*
108117169044Sbrutus 		 * First kstrgetmsg() call for an uioa_t so if any
108217169044Sbrutus 		 * queued mblk_t's need to consume them before uioa
108317169044Sbrutus 		 * from below can occur.
108417169044Sbrutus 		 */
108517169044Sbrutus 		sodp->sod_uioa.uioa_state &= UIOA_CLR;
108617169044Sbrutus 		sodp->sod_uioa.uioa_state |= UIOA_ENABLED;
108717169044Sbrutus 		if (q->q_first != NULL) {
108817169044Sbrutus 			struioainit(q, sodp, uiop);
108917169044Sbrutus 		}
1090*301ce41fSja97890 	} else {
1091*301ce41fSja97890 		/*
1092*301ce41fSja97890 		 * If we have a valid uio, try and use this as a guide for how
1093*301ce41fSja97890 		 * many bytes to retrieve from the queue via getq_noenab().
1094*301ce41fSja97890 		 * Doing this can avoid unneccesary counting of overlong
1095*301ce41fSja97890 		 * messages in putback(). We currently only do this for sockets.
1096*301ce41fSja97890 		 */
1097*301ce41fSja97890 		if ((uiop != NULL) && (stp->sd_vnode->v_type == VSOCK))
1098*301ce41fSja97890 			rbytes = uiop->uio_resid;
109917169044Sbrutus 	}
110017169044Sbrutus 
1101*301ce41fSja97890 	bp = getq_noenab(q, rbytes);
110217169044Sbrutus 	if (bp != NULL && (bp->b_datap->db_flags & DBLK_UIOA)) {
110317169044Sbrutus 		/*
110417169044Sbrutus 		 * A uioa flaged mblk_t chain, already uio processed,
110517169044Sbrutus 		 * add it to the sodirect uioa pending free list.
110617169044Sbrutus 		 *
110717169044Sbrutus 		 * Note, a b_cont chain headed by a DBLK_UIOA enable
110817169044Sbrutus 		 * mblk_t must have all mblk_t(s) DBLK_UIOA enabled.
110917169044Sbrutus 		 */
111017169044Sbrutus 		mblk_t	*bpt = sodp->sod_uioaft;
111117169044Sbrutus 
111217169044Sbrutus 		ASSERT(sodp != NULL);
111317169044Sbrutus 
111417169044Sbrutus 		/*
111517169044Sbrutus 		 * Add first mblk_t of "bp" chain to current sodirect uioa
111617169044Sbrutus 		 * free list tail mblk_t, if any, else empty list so new head.
111717169044Sbrutus 		 */
111817169044Sbrutus 		if (bpt == NULL)
111917169044Sbrutus 			sodp->sod_uioafh = bp;
112017169044Sbrutus 		else
112117169044Sbrutus 			bpt->b_cont = bp;
112217169044Sbrutus 
112317169044Sbrutus 		/*
112417169044Sbrutus 		 * Walk mblk_t "bp" chain to find tail and adjust rptr of
112517169044Sbrutus 		 * each to reflect that uioamove() has consumed all data.
112617169044Sbrutus 		 */
112717169044Sbrutus 		bpt = bp;
112817169044Sbrutus 		for (;;) {
112917169044Sbrutus 			bpt->b_rptr = bpt->b_wptr;
113017169044Sbrutus 			if (bpt->b_cont == NULL)
113117169044Sbrutus 				break;
113217169044Sbrutus 			bpt = bpt->b_cont;
113317169044Sbrutus 
113417169044Sbrutus 			ASSERT(bpt->b_datap->db_flags & DBLK_UIOA);
113517169044Sbrutus 		}
113617169044Sbrutus 		/* New sodirect uioa free list tail */
113717169044Sbrutus 		sodp->sod_uioaft = bpt;
113817169044Sbrutus 
113917169044Sbrutus 		/* Only 1 strget() with data returned per uioa_t */
114017169044Sbrutus 		if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) {
114117169044Sbrutus 			sodp->sod_uioa.uioa_state &= UIOA_CLR;
114217169044Sbrutus 			sodp->sod_uioa.uioa_state |= UIOA_FINI;
114317169044Sbrutus 		}
114417169044Sbrutus 	}
114517169044Sbrutus 
114617169044Sbrutus 	return (bp);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*
11507c478bd9Sstevel@tonic-gate  * Copy out the message pointed to by `bp' into the uio pointed to by `uiop'.
11517c478bd9Sstevel@tonic-gate  * If the message does not fit in the uio the remainder of it is returned;
11527c478bd9Sstevel@tonic-gate  * otherwise NULL is returned.  Any embedded zero-length mblk_t's are
11537c478bd9Sstevel@tonic-gate  * consumed, even if uio_resid reaches zero.  On error, `*errorp' is set to
11547c478bd9Sstevel@tonic-gate  * the error code, the message is consumed, and NULL is returned.
11557c478bd9Sstevel@tonic-gate  */
11567c478bd9Sstevel@tonic-gate static mblk_t *
11577c478bd9Sstevel@tonic-gate struiocopyout(mblk_t *bp, struct uio *uiop, int *errorp)
11587c478bd9Sstevel@tonic-gate {
11597c478bd9Sstevel@tonic-gate 	int error;
11607c478bd9Sstevel@tonic-gate 	ptrdiff_t n;
11617c478bd9Sstevel@tonic-gate 	mblk_t *nbp;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_wptr >= bp->b_rptr);
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	do {
116617169044Sbrutus 		ASSERT(!(bp->b_datap->db_flags & DBLK_UIOA));
116717169044Sbrutus 
11687c478bd9Sstevel@tonic-gate 		if ((n = MIN(uiop->uio_resid, MBLKL(bp))) != 0) {
11697c478bd9Sstevel@tonic-gate 			ASSERT(n > 0);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 			error = uiomove(bp->b_rptr, n, UIO_READ, uiop);
11727c478bd9Sstevel@tonic-gate 			if (error != 0) {
11737c478bd9Sstevel@tonic-gate 				freemsg(bp);
11747c478bd9Sstevel@tonic-gate 				*errorp = error;
11757c478bd9Sstevel@tonic-gate 				return (NULL);
11767c478bd9Sstevel@tonic-gate 			}
11777c478bd9Sstevel@tonic-gate 		}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 		bp->b_rptr += n;
11807c478bd9Sstevel@tonic-gate 		while (bp != NULL && (bp->b_rptr >= bp->b_wptr)) {
11817c478bd9Sstevel@tonic-gate 			nbp = bp;
11827c478bd9Sstevel@tonic-gate 			bp = bp->b_cont;
11837c478bd9Sstevel@tonic-gate 			freeb(nbp);
11847c478bd9Sstevel@tonic-gate 		}
11857c478bd9Sstevel@tonic-gate 	} while (bp != NULL && uiop->uio_resid > 0);
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	*errorp = 0;
11887c478bd9Sstevel@tonic-gate 	return (bp);
11897c478bd9Sstevel@tonic-gate }
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate /*
11927c478bd9Sstevel@tonic-gate  * Read a stream according to the mode flags in sd_flag:
11937c478bd9Sstevel@tonic-gate  *
11947c478bd9Sstevel@tonic-gate  * (default mode)		- Byte stream, msg boundaries are ignored
11957c478bd9Sstevel@tonic-gate  * RD_MSGDIS (msg discard)	- Read on msg boundaries and throw away
11967c478bd9Sstevel@tonic-gate  *				any data remaining in msg
11977c478bd9Sstevel@tonic-gate  * RD_MSGNODIS (msg non-discard) - Read on msg boundaries and put back
11987c478bd9Sstevel@tonic-gate  *				any remaining data on head of read queue
11997c478bd9Sstevel@tonic-gate  *
12007c478bd9Sstevel@tonic-gate  * Consume readable messages on the front of the queue until
12017c478bd9Sstevel@tonic-gate  * ttolwp(curthread)->lwp_count
12027c478bd9Sstevel@tonic-gate  * is satisfied, the readable messages are exhausted, or a message
12037c478bd9Sstevel@tonic-gate  * boundary is reached in a message mode.  If no data was read and
12047c478bd9Sstevel@tonic-gate  * the stream was not opened with the NDELAY flag, block until data arrives.
12057c478bd9Sstevel@tonic-gate  * Otherwise return the data read and update the count.
12067c478bd9Sstevel@tonic-gate  *
12077c478bd9Sstevel@tonic-gate  * In default mode a 0 length message signifies end-of-file and terminates
12087c478bd9Sstevel@tonic-gate  * a read in progress.  The 0 length message is removed from the queue
12097c478bd9Sstevel@tonic-gate  * only if it is the only message read (no data is read).
12107c478bd9Sstevel@tonic-gate  *
12117c478bd9Sstevel@tonic-gate  * An attempt to read an M_PROTO or M_PCPROTO message results in an
12127c478bd9Sstevel@tonic-gate  * EBADMSG error return, unless either RD_PROTDAT or RD_PROTDIS are set.
12137c478bd9Sstevel@tonic-gate  * If RD_PROTDAT is set, M_PROTO and M_PCPROTO messages are read as data.
12147c478bd9Sstevel@tonic-gate  * If RD_PROTDIS is set, the M_PROTO and M_PCPROTO parts of the message
12157c478bd9Sstevel@tonic-gate  * are unlinked from and M_DATA blocks in the message, the protos are
12167c478bd9Sstevel@tonic-gate  * thrown away, and the data is read.
12177c478bd9Sstevel@tonic-gate  */
12187c478bd9Sstevel@tonic-gate /* ARGSUSED */
12197c478bd9Sstevel@tonic-gate int
12207c478bd9Sstevel@tonic-gate strread(struct vnode *vp, struct uio *uiop, cred_t *crp)
12217c478bd9Sstevel@tonic-gate {
12227c478bd9Sstevel@tonic-gate 	struct stdata *stp;
12237c478bd9Sstevel@tonic-gate 	mblk_t *bp, *nbp;
12247c478bd9Sstevel@tonic-gate 	queue_t *q;
12257c478bd9Sstevel@tonic-gate 	int error = 0;
12267c478bd9Sstevel@tonic-gate 	uint_t old_sd_flag;
12277c478bd9Sstevel@tonic-gate 	int first;
12287c478bd9Sstevel@tonic-gate 	char rflg;
12297c478bd9Sstevel@tonic-gate 	uint_t mark;		/* Contains MSG*MARK and _LASTMARK */
12307c478bd9Sstevel@tonic-gate #define	_LASTMARK	0x8000	/* Distinct from MSG*MARK */
12317c478bd9Sstevel@tonic-gate 	short delim;
12327c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
12337c478bd9Sstevel@tonic-gate 	char waitflag;
12347c478bd9Sstevel@tonic-gate 	unsigned char type;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
12377c478bd9Sstevel@tonic-gate 	    TR_STRREAD_ENTER, "strread:%p", vp);
12387c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
12397c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
12429acbbeafSnn35248 
12439acbbeafSnn35248 	if ((error = i_straccess(stp, JCREAD)) != 0) {
12449acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
12459acbbeafSnn35248 		return (error);
12469acbbeafSnn35248 	}
12479acbbeafSnn35248 
12487c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STRDERR|STPLEX)) {
12497c478bd9Sstevel@tonic-gate 		error = strgeterr(stp, STRDERR|STPLEX, 0);
12507c478bd9Sstevel@tonic-gate 		if (error != 0) {
12517c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
12527c478bd9Sstevel@tonic-gate 			return (error);
12537c478bd9Sstevel@tonic-gate 		}
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	/*
12577c478bd9Sstevel@tonic-gate 	 * Loop terminates when uiop->uio_resid == 0.
12587c478bd9Sstevel@tonic-gate 	 */
12597c478bd9Sstevel@tonic-gate 	rflg = 0;
12607c478bd9Sstevel@tonic-gate 	waitflag = READWAIT;
12617c478bd9Sstevel@tonic-gate 	q = _RD(stp->sd_wrq);
12627c478bd9Sstevel@tonic-gate 	for (;;) {
12637c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&stp->sd_lock));
12647c478bd9Sstevel@tonic-gate 		old_sd_flag = stp->sd_flag;
12657c478bd9Sstevel@tonic-gate 		mark = 0;
12667c478bd9Sstevel@tonic-gate 		delim = 0;
12677c478bd9Sstevel@tonic-gate 		first = 1;
12687c478bd9Sstevel@tonic-gate 		while ((bp = strget(stp, q, uiop, first, &error)) == NULL) {
12697c478bd9Sstevel@tonic-gate 			int done = 0;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(&stp->sd_lock));
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 			if (error != 0)
12747c478bd9Sstevel@tonic-gate 				goto oops;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STRHUP|STREOF)) {
12777c478bd9Sstevel@tonic-gate 				goto oops;
12787c478bd9Sstevel@tonic-gate 			}
12797c478bd9Sstevel@tonic-gate 			if (rflg && !(stp->sd_flag & STRDELIM)) {
12807c478bd9Sstevel@tonic-gate 				goto oops;
12817c478bd9Sstevel@tonic-gate 			}
12827c478bd9Sstevel@tonic-gate 			/*
12837c478bd9Sstevel@tonic-gate 			 * If a read(fd,buf,0) has been done, there is no
12847c478bd9Sstevel@tonic-gate 			 * need to sleep. We always have zero bytes to
12857c478bd9Sstevel@tonic-gate 			 * return.
12867c478bd9Sstevel@tonic-gate 			 */
12877c478bd9Sstevel@tonic-gate 			if (uiop->uio_resid == 0) {
12887c478bd9Sstevel@tonic-gate 				goto oops;
12897c478bd9Sstevel@tonic-gate 			}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 			qbackenable(q, 0);
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_WAIT,
12947c478bd9Sstevel@tonic-gate 			    "strread calls strwaitq:%p, %p, %p",
12957c478bd9Sstevel@tonic-gate 			    vp, uiop, crp);
12967c478bd9Sstevel@tonic-gate 			if ((error = strwaitq(stp, waitflag, uiop->uio_resid,
12977c478bd9Sstevel@tonic-gate 			    uiop->uio_fmode, -1, &done)) != 0 || done) {
12987c478bd9Sstevel@tonic-gate 				TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_DONE,
12997c478bd9Sstevel@tonic-gate 				    "strread error or done:%p, %p, %p",
13007c478bd9Sstevel@tonic-gate 				    vp, uiop, crp);
13017c478bd9Sstevel@tonic-gate 				if ((uiop->uio_fmode & FNDELAY) &&
13027c478bd9Sstevel@tonic-gate 				    (stp->sd_flag & OLDNDELAY) &&
13037c478bd9Sstevel@tonic-gate 				    (error == EAGAIN))
13047c478bd9Sstevel@tonic-gate 					error = 0;
13057c478bd9Sstevel@tonic-gate 				goto oops;
13067c478bd9Sstevel@tonic-gate 			}
13077c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_AWAKE,
13087c478bd9Sstevel@tonic-gate 			    "strread awakes:%p, %p, %p", vp, uiop, crp);
13099acbbeafSnn35248 			if ((error = i_straccess(stp, JCREAD)) != 0) {
13109acbbeafSnn35248 				goto oops;
13117c478bd9Sstevel@tonic-gate 			}
13127c478bd9Sstevel@tonic-gate 			first = 0;
13137c478bd9Sstevel@tonic-gate 		}
131417169044Sbrutus 
13157c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&stp->sd_lock));
13167c478bd9Sstevel@tonic-gate 		ASSERT(bp);
131717169044Sbrutus 		ASSERT(!(bp->b_datap->db_flags & DBLK_UIOA));
13187c478bd9Sstevel@tonic-gate 		pri = bp->b_band;
13197c478bd9Sstevel@tonic-gate 		/*
13207c478bd9Sstevel@tonic-gate 		 * Extract any mark information. If the message is not
13217c478bd9Sstevel@tonic-gate 		 * completely consumed this information will be put in the mblk
13227c478bd9Sstevel@tonic-gate 		 * that is putback.
13237c478bd9Sstevel@tonic-gate 		 * If MSGMARKNEXT is set and the message is completely consumed
13247c478bd9Sstevel@tonic-gate 		 * the STRATMARK flag will be set below. Likewise, if
13257c478bd9Sstevel@tonic-gate 		 * MSGNOTMARKNEXT is set and the message is
13267c478bd9Sstevel@tonic-gate 		 * completely consumed STRNOTATMARK will be set.
13277c478bd9Sstevel@tonic-gate 		 *
13287c478bd9Sstevel@tonic-gate 		 * For some unknown reason strread only breaks the read at the
13297c478bd9Sstevel@tonic-gate 		 * last mark.
13307c478bd9Sstevel@tonic-gate 		 */
13317c478bd9Sstevel@tonic-gate 		mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
13327c478bd9Sstevel@tonic-gate 		ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
13337c478bd9Sstevel@tonic-gate 		    (MSGMARKNEXT|MSGNOTMARKNEXT));
13347c478bd9Sstevel@tonic-gate 		if (mark != 0 && bp == stp->sd_mark) {
13357c478bd9Sstevel@tonic-gate 			if (rflg) {
13367c478bd9Sstevel@tonic-gate 				putback(stp, q, bp, pri);
13377c478bd9Sstevel@tonic-gate 				goto oops;
13387c478bd9Sstevel@tonic-gate 			}
13397c478bd9Sstevel@tonic-gate 			mark |= _LASTMARK;
13407c478bd9Sstevel@tonic-gate 			stp->sd_mark = NULL;
13417c478bd9Sstevel@tonic-gate 		}
13427c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STRDELIM) && (bp->b_flag & MSGDELIM))
13437c478bd9Sstevel@tonic-gate 			delim = 1;
13447c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 		if (STREAM_NEEDSERVICE(stp))
13477c478bd9Sstevel@tonic-gate 			stream_runservice(stp);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 		type = bp->b_datap->db_type;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 		switch (type) {
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 		case M_DATA:
13547c478bd9Sstevel@tonic-gate ismdata:
13557c478bd9Sstevel@tonic-gate 			if (msgnodata(bp)) {
13567c478bd9Sstevel@tonic-gate 				if (mark || delim) {
13577c478bd9Sstevel@tonic-gate 					freemsg(bp);
13587c478bd9Sstevel@tonic-gate 				} else if (rflg) {
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 					/*
13617c478bd9Sstevel@tonic-gate 					 * If already read data put zero
13627c478bd9Sstevel@tonic-gate 					 * length message back on queue else
13637c478bd9Sstevel@tonic-gate 					 * free msg and return 0.
13647c478bd9Sstevel@tonic-gate 					 */
13657c478bd9Sstevel@tonic-gate 					bp->b_band = pri;
13667c478bd9Sstevel@tonic-gate 					mutex_enter(&stp->sd_lock);
13677c478bd9Sstevel@tonic-gate 					putback(stp, q, bp, pri);
13687c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
13697c478bd9Sstevel@tonic-gate 				} else {
13707c478bd9Sstevel@tonic-gate 					freemsg(bp);
13717c478bd9Sstevel@tonic-gate 				}
13727c478bd9Sstevel@tonic-gate 				error =  0;
13737c478bd9Sstevel@tonic-gate 				goto oops1;
13747c478bd9Sstevel@tonic-gate 			}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 			rflg = 1;
13777c478bd9Sstevel@tonic-gate 			waitflag |= NOINTR;
13787c478bd9Sstevel@tonic-gate 			bp = struiocopyout(bp, uiop, &error);
13797c478bd9Sstevel@tonic-gate 			if (error != 0)
13807c478bd9Sstevel@tonic-gate 				goto oops1;
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
13837c478bd9Sstevel@tonic-gate 			if (bp) {
13847c478bd9Sstevel@tonic-gate 				/*
13857c478bd9Sstevel@tonic-gate 				 * Have remaining data in message.
13867c478bd9Sstevel@tonic-gate 				 * Free msg if in discard mode.
13877c478bd9Sstevel@tonic-gate 				 */
13887c478bd9Sstevel@tonic-gate 				if (stp->sd_read_opt & RD_MSGDIS) {
13897c478bd9Sstevel@tonic-gate 					freemsg(bp);
13907c478bd9Sstevel@tonic-gate 				} else {
13917c478bd9Sstevel@tonic-gate 					bp->b_band = pri;
13927c478bd9Sstevel@tonic-gate 					if ((mark & _LASTMARK) &&
13937c478bd9Sstevel@tonic-gate 					    (stp->sd_mark == NULL))
13947c478bd9Sstevel@tonic-gate 						stp->sd_mark = bp;
13957c478bd9Sstevel@tonic-gate 					bp->b_flag |= mark & ~_LASTMARK;
13967c478bd9Sstevel@tonic-gate 					if (delim)
13977c478bd9Sstevel@tonic-gate 						bp->b_flag |= MSGDELIM;
13987c478bd9Sstevel@tonic-gate 					if (msgnodata(bp))
13997c478bd9Sstevel@tonic-gate 						freemsg(bp);
14007c478bd9Sstevel@tonic-gate 					else
14017c478bd9Sstevel@tonic-gate 						putback(stp, q, bp, pri);
14027c478bd9Sstevel@tonic-gate 				}
14037c478bd9Sstevel@tonic-gate 			} else {
14047c478bd9Sstevel@tonic-gate 				/*
14057c478bd9Sstevel@tonic-gate 				 * Consumed the complete message.
14067c478bd9Sstevel@tonic-gate 				 * Move the MSG*MARKNEXT information
14077c478bd9Sstevel@tonic-gate 				 * to the stream head just in case
14087c478bd9Sstevel@tonic-gate 				 * the read queue becomes empty.
14097c478bd9Sstevel@tonic-gate 				 *
14107c478bd9Sstevel@tonic-gate 				 * If the stream head was at the mark
14117c478bd9Sstevel@tonic-gate 				 * (STRATMARK) before we dropped sd_lock above
14127c478bd9Sstevel@tonic-gate 				 * and some data was consumed then we have
14137c478bd9Sstevel@tonic-gate 				 * moved past the mark thus STRATMARK is
14147c478bd9Sstevel@tonic-gate 				 * cleared. However, if a message arrived in
14157c478bd9Sstevel@tonic-gate 				 * strrput during the copyout above causing
14167c478bd9Sstevel@tonic-gate 				 * STRATMARK to be set we can not clear that
14177c478bd9Sstevel@tonic-gate 				 * flag.
14187c478bd9Sstevel@tonic-gate 				 */
14197c478bd9Sstevel@tonic-gate 				if (mark &
14207c478bd9Sstevel@tonic-gate 				    (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
14217c478bd9Sstevel@tonic-gate 					if (mark & MSGMARKNEXT) {
14227c478bd9Sstevel@tonic-gate 						stp->sd_flag &= ~STRNOTATMARK;
14237c478bd9Sstevel@tonic-gate 						stp->sd_flag |= STRATMARK;
14247c478bd9Sstevel@tonic-gate 					} else if (mark & MSGNOTMARKNEXT) {
14257c478bd9Sstevel@tonic-gate 						stp->sd_flag &= ~STRATMARK;
14267c478bd9Sstevel@tonic-gate 						stp->sd_flag |= STRNOTATMARK;
14277c478bd9Sstevel@tonic-gate 					} else {
14287c478bd9Sstevel@tonic-gate 						stp->sd_flag &=
14297c478bd9Sstevel@tonic-gate 						    ~(STRATMARK|STRNOTATMARK);
14307c478bd9Sstevel@tonic-gate 					}
14317c478bd9Sstevel@tonic-gate 				} else if (rflg && (old_sd_flag & STRATMARK)) {
14327c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRATMARK;
14337c478bd9Sstevel@tonic-gate 				}
14347c478bd9Sstevel@tonic-gate 			}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 			/*
14377c478bd9Sstevel@tonic-gate 			 * Check for signal messages at the front of the read
14387c478bd9Sstevel@tonic-gate 			 * queue and generate the signal(s) if appropriate.
14397c478bd9Sstevel@tonic-gate 			 * The only signal that can be on queue is M_SIG at
14407c478bd9Sstevel@tonic-gate 			 * this point.
14417c478bd9Sstevel@tonic-gate 			 */
14427c478bd9Sstevel@tonic-gate 			while ((((bp = q->q_first)) != NULL) &&
14437c478bd9Sstevel@tonic-gate 			    (bp->b_datap->db_type == M_SIG)) {
1444*301ce41fSja97890 				bp = getq_noenab(q, 0);
14457c478bd9Sstevel@tonic-gate 				/*
14467c478bd9Sstevel@tonic-gate 				 * sd_lock is held so the content of the
14477c478bd9Sstevel@tonic-gate 				 * read queue can not change.
14487c478bd9Sstevel@tonic-gate 				 */
14497c478bd9Sstevel@tonic-gate 				ASSERT(bp != NULL &&
14507c478bd9Sstevel@tonic-gate 				    bp->b_datap->db_type == M_SIG);
14517c478bd9Sstevel@tonic-gate 				strsignal_nolock(stp, *bp->b_rptr,
14527c478bd9Sstevel@tonic-gate 				    (int32_t)bp->b_band);
14537c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
14547c478bd9Sstevel@tonic-gate 				freemsg(bp);
14557c478bd9Sstevel@tonic-gate 				if (STREAM_NEEDSERVICE(stp))
14567c478bd9Sstevel@tonic-gate 					stream_runservice(stp);
14577c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
14587c478bd9Sstevel@tonic-gate 			}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 			if ((uiop->uio_resid == 0) || (mark & _LASTMARK) ||
14617c478bd9Sstevel@tonic-gate 			    delim ||
14627c478bd9Sstevel@tonic-gate 			    (stp->sd_read_opt & (RD_MSGDIS|RD_MSGNODIS))) {
14637c478bd9Sstevel@tonic-gate 				goto oops;
14647c478bd9Sstevel@tonic-gate 			}
14657c478bd9Sstevel@tonic-gate 			continue;
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 		case M_SIG:
14687c478bd9Sstevel@tonic-gate 			strsignal(stp, *bp->b_rptr, (int32_t)bp->b_band);
14697c478bd9Sstevel@tonic-gate 			freemsg(bp);
14707c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
14717c478bd9Sstevel@tonic-gate 			continue;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 		case M_PROTO:
14747c478bd9Sstevel@tonic-gate 		case M_PCPROTO:
14757c478bd9Sstevel@tonic-gate 			/*
14767c478bd9Sstevel@tonic-gate 			 * Only data messages are readable.
14777c478bd9Sstevel@tonic-gate 			 * Any others generate an error, unless
14787c478bd9Sstevel@tonic-gate 			 * RD_PROTDIS or RD_PROTDAT is set.
14797c478bd9Sstevel@tonic-gate 			 */
14807c478bd9Sstevel@tonic-gate 			if (stp->sd_read_opt & RD_PROTDAT) {
14817c478bd9Sstevel@tonic-gate 				for (nbp = bp; nbp; nbp = nbp->b_next) {
1482d3e55dcdSgww 					if ((nbp->b_datap->db_type ==
1483d3e55dcdSgww 					    M_PROTO) ||
1484d3e55dcdSgww 					    (nbp->b_datap->db_type ==
1485d3e55dcdSgww 					    M_PCPROTO)) {
14867c478bd9Sstevel@tonic-gate 						nbp->b_datap->db_type = M_DATA;
1487d3e55dcdSgww 					} else {
14887c478bd9Sstevel@tonic-gate 						break;
14897c478bd9Sstevel@tonic-gate 					}
1490d3e55dcdSgww 				}
14917c478bd9Sstevel@tonic-gate 				/*
14927c478bd9Sstevel@tonic-gate 				 * clear stream head hi pri flag based on
14937c478bd9Sstevel@tonic-gate 				 * first message
14947c478bd9Sstevel@tonic-gate 				 */
14957c478bd9Sstevel@tonic-gate 				if (type == M_PCPROTO) {
14967c478bd9Sstevel@tonic-gate 					mutex_enter(&stp->sd_lock);
14977c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
14987c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
14997c478bd9Sstevel@tonic-gate 				}
15007c478bd9Sstevel@tonic-gate 				goto ismdata;
15017c478bd9Sstevel@tonic-gate 			} else if (stp->sd_read_opt & RD_PROTDIS) {
15027c478bd9Sstevel@tonic-gate 				/*
15037c478bd9Sstevel@tonic-gate 				 * discard non-data messages
15047c478bd9Sstevel@tonic-gate 				 */
15057c478bd9Sstevel@tonic-gate 				while (bp &&
15067c478bd9Sstevel@tonic-gate 				    ((bp->b_datap->db_type == M_PROTO) ||
15077c478bd9Sstevel@tonic-gate 				    (bp->b_datap->db_type == M_PCPROTO))) {
15087c478bd9Sstevel@tonic-gate 					nbp = unlinkb(bp);
15097c478bd9Sstevel@tonic-gate 					freeb(bp);
15107c478bd9Sstevel@tonic-gate 					bp = nbp;
15117c478bd9Sstevel@tonic-gate 				}
15127c478bd9Sstevel@tonic-gate 				/*
15137c478bd9Sstevel@tonic-gate 				 * clear stream head hi pri flag based on
15147c478bd9Sstevel@tonic-gate 				 * first message
15157c478bd9Sstevel@tonic-gate 				 */
15167c478bd9Sstevel@tonic-gate 				if (type == M_PCPROTO) {
15177c478bd9Sstevel@tonic-gate 					mutex_enter(&stp->sd_lock);
15187c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
15197c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
15207c478bd9Sstevel@tonic-gate 				}
15217c478bd9Sstevel@tonic-gate 				if (bp) {
15227c478bd9Sstevel@tonic-gate 					bp->b_band = pri;
15237c478bd9Sstevel@tonic-gate 					goto ismdata;
15247c478bd9Sstevel@tonic-gate 				} else {
15257c478bd9Sstevel@tonic-gate 					break;
15267c478bd9Sstevel@tonic-gate 				}
15277c478bd9Sstevel@tonic-gate 			}
15287c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
15297c478bd9Sstevel@tonic-gate 		case M_PASSFP:
15307c478bd9Sstevel@tonic-gate 			if ((bp->b_datap->db_type == M_PASSFP) &&
15317c478bd9Sstevel@tonic-gate 			    (stp->sd_read_opt & RD_PROTDIS)) {
15327c478bd9Sstevel@tonic-gate 				freemsg(bp);
15337c478bd9Sstevel@tonic-gate 				break;
15347c478bd9Sstevel@tonic-gate 			}
15357c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
15367c478bd9Sstevel@tonic-gate 			putback(stp, q, bp, pri);
15377c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
15387c478bd9Sstevel@tonic-gate 			if (rflg == 0)
15397c478bd9Sstevel@tonic-gate 				error = EBADMSG;
15407c478bd9Sstevel@tonic-gate 			goto oops1;
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 		default:
15437c478bd9Sstevel@tonic-gate 			/*
15447c478bd9Sstevel@tonic-gate 			 * Garbage on stream head read queue.
15457c478bd9Sstevel@tonic-gate 			 */
15467c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "bad %x found at stream head\n",
15477c478bd9Sstevel@tonic-gate 			    bp->b_datap->db_type);
15487c478bd9Sstevel@tonic-gate 			freemsg(bp);
15497c478bd9Sstevel@tonic-gate 			goto oops1;
15507c478bd9Sstevel@tonic-gate 		}
15517c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate oops:
15547c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
15557c478bd9Sstevel@tonic-gate oops1:
15567c478bd9Sstevel@tonic-gate 	qbackenable(q, pri);
15577c478bd9Sstevel@tonic-gate 	return (error);
15587c478bd9Sstevel@tonic-gate #undef	_LASTMARK
15597c478bd9Sstevel@tonic-gate }
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate /*
15627c478bd9Sstevel@tonic-gate  * Default processing of M_PROTO/M_PCPROTO messages.
15637c478bd9Sstevel@tonic-gate  * Determine which wakeups and signals are needed.
15647c478bd9Sstevel@tonic-gate  * This can be replaced by a user-specified procedure for kernel users
15657c478bd9Sstevel@tonic-gate  * of STREAMS.
15667c478bd9Sstevel@tonic-gate  */
15677c478bd9Sstevel@tonic-gate /* ARGSUSED */
15687c478bd9Sstevel@tonic-gate mblk_t *
15697c478bd9Sstevel@tonic-gate strrput_proto(vnode_t *vp, mblk_t *mp,
15707c478bd9Sstevel@tonic-gate     strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
15717c478bd9Sstevel@tonic-gate     strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
15727c478bd9Sstevel@tonic-gate {
15737c478bd9Sstevel@tonic-gate 	*wakeups = RSLEEP;
15747c478bd9Sstevel@tonic-gate 	*allmsgsigs = 0;
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
15777c478bd9Sstevel@tonic-gate 	case M_PROTO:
15787c478bd9Sstevel@tonic-gate 		if (mp->b_band == 0) {
15797c478bd9Sstevel@tonic-gate 			*firstmsgsigs = S_INPUT | S_RDNORM;
15807c478bd9Sstevel@tonic-gate 			*pollwakeups = POLLIN | POLLRDNORM;
15817c478bd9Sstevel@tonic-gate 		} else {
15827c478bd9Sstevel@tonic-gate 			*firstmsgsigs = S_INPUT | S_RDBAND;
15837c478bd9Sstevel@tonic-gate 			*pollwakeups = POLLIN | POLLRDBAND;
15847c478bd9Sstevel@tonic-gate 		}
15857c478bd9Sstevel@tonic-gate 		break;
15867c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
15877c478bd9Sstevel@tonic-gate 		*firstmsgsigs = S_HIPRI;
15887c478bd9Sstevel@tonic-gate 		*pollwakeups = POLLPRI;
15897c478bd9Sstevel@tonic-gate 		break;
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 	return (mp);
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate /*
15957c478bd9Sstevel@tonic-gate  * Default processing of everything but M_DATA, M_PROTO, M_PCPROTO and
15967c478bd9Sstevel@tonic-gate  * M_PASSFP messages.
15977c478bd9Sstevel@tonic-gate  * Determine which wakeups and signals are needed.
15987c478bd9Sstevel@tonic-gate  * This can be replaced by a user-specified procedure for kernel users
15997c478bd9Sstevel@tonic-gate  * of STREAMS.
16007c478bd9Sstevel@tonic-gate  */
16017c478bd9Sstevel@tonic-gate /* ARGSUSED */
16027c478bd9Sstevel@tonic-gate mblk_t *
16037c478bd9Sstevel@tonic-gate strrput_misc(vnode_t *vp, mblk_t *mp,
16047c478bd9Sstevel@tonic-gate     strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
16057c478bd9Sstevel@tonic-gate     strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate 	*wakeups = 0;
16087c478bd9Sstevel@tonic-gate 	*firstmsgsigs = 0;
16097c478bd9Sstevel@tonic-gate 	*allmsgsigs = 0;
16107c478bd9Sstevel@tonic-gate 	*pollwakeups = 0;
16117c478bd9Sstevel@tonic-gate 	return (mp);
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate /*
16157c478bd9Sstevel@tonic-gate  * Stream read put procedure.  Called from downstream driver/module
16167c478bd9Sstevel@tonic-gate  * with messages for the stream head.  Data, protocol, and in-stream
16177c478bd9Sstevel@tonic-gate  * signal messages are placed on the queue, others are handled directly.
16187c478bd9Sstevel@tonic-gate  */
16197c478bd9Sstevel@tonic-gate int
16207c478bd9Sstevel@tonic-gate strrput(queue_t *q, mblk_t *bp)
16217c478bd9Sstevel@tonic-gate {
16227c478bd9Sstevel@tonic-gate 	struct stdata	*stp;
16237c478bd9Sstevel@tonic-gate 	ulong_t		rput_opt;
16247c478bd9Sstevel@tonic-gate 	strwakeup_t	wakeups;
16257c478bd9Sstevel@tonic-gate 	strsigset_t	firstmsgsigs;	/* Signals if first message on queue */
16267c478bd9Sstevel@tonic-gate 	strsigset_t	allmsgsigs;	/* Signals for all messages */
16277c478bd9Sstevel@tonic-gate 	strsigset_t	signals;	/* Signals events to generate */
16287c478bd9Sstevel@tonic-gate 	strpollset_t	pollwakeups;
16297c478bd9Sstevel@tonic-gate 	mblk_t		*nextbp;
16307c478bd9Sstevel@tonic-gate 	uchar_t		band = 0;
16317c478bd9Sstevel@tonic-gate 	int		hipri_sig;
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	stp = (struct stdata *)q->q_ptr;
16347c478bd9Sstevel@tonic-gate 	/*
16357c478bd9Sstevel@tonic-gate 	 * Use rput_opt for optimized access to the SR_ flags except
16367c478bd9Sstevel@tonic-gate 	 * SR_POLLIN. That flag has to be checked under sd_lock since it
16377c478bd9Sstevel@tonic-gate 	 * is modified by strpoll().
16387c478bd9Sstevel@tonic-gate 	 */
16397c478bd9Sstevel@tonic-gate 	rput_opt = stp->sd_rput_opt;
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	ASSERT(qclaimed(q));
16427c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_ENTER,
16437c478bd9Sstevel@tonic-gate 	    "strrput called with message type:q %p bp %p", q, bp);
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	/*
16467c478bd9Sstevel@tonic-gate 	 * Perform initial processing and pass to the parameterized functions.
16477c478bd9Sstevel@tonic-gate 	 */
16487c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_next == NULL);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	switch (bp->b_datap->db_type) {
16517c478bd9Sstevel@tonic-gate 	case M_DATA:
16527c478bd9Sstevel@tonic-gate 		/*
16537c478bd9Sstevel@tonic-gate 		 * sockfs is the only consumer of STREOF and when it is set,
16547c478bd9Sstevel@tonic-gate 		 * it implies that the receiver is not interested in receiving
16557c478bd9Sstevel@tonic-gate 		 * any more data, hence the mblk is freed to prevent unnecessary
16567c478bd9Sstevel@tonic-gate 		 * message queueing at the stream head.
16577c478bd9Sstevel@tonic-gate 		 */
16587c478bd9Sstevel@tonic-gate 		if (stp->sd_flag == STREOF) {
16597c478bd9Sstevel@tonic-gate 			freemsg(bp);
16607c478bd9Sstevel@tonic-gate 			return (0);
16617c478bd9Sstevel@tonic-gate 		}
16627c478bd9Sstevel@tonic-gate 		if ((rput_opt & SR_IGN_ZEROLEN) &&
16637c478bd9Sstevel@tonic-gate 		    bp->b_rptr == bp->b_wptr && msgnodata(bp)) {
16647c478bd9Sstevel@tonic-gate 			/*
16657c478bd9Sstevel@tonic-gate 			 * Ignore zero-length M_DATA messages. These might be
16667c478bd9Sstevel@tonic-gate 			 * generated by some transports.
16677c478bd9Sstevel@tonic-gate 			 * The zero-length M_DATA messages, even if they
16687c478bd9Sstevel@tonic-gate 			 * are ignored, should effect the atmark tracking and
16697c478bd9Sstevel@tonic-gate 			 * should wake up a thread sleeping in strwaitmark.
16707c478bd9Sstevel@tonic-gate 			 */
16717c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
16727c478bd9Sstevel@tonic-gate 			if (bp->b_flag & MSGMARKNEXT) {
16737c478bd9Sstevel@tonic-gate 				/*
16747c478bd9Sstevel@tonic-gate 				 * Record the position of the mark either
16757c478bd9Sstevel@tonic-gate 				 * in q_last or in STRATMARK.
16767c478bd9Sstevel@tonic-gate 				 */
16777c478bd9Sstevel@tonic-gate 				if (q->q_last != NULL) {
16787c478bd9Sstevel@tonic-gate 					q->q_last->b_flag &= ~MSGNOTMARKNEXT;
16797c478bd9Sstevel@tonic-gate 					q->q_last->b_flag |= MSGMARKNEXT;
16807c478bd9Sstevel@tonic-gate 				} else {
16817c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRNOTATMARK;
16827c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRATMARK;
16837c478bd9Sstevel@tonic-gate 				}
16847c478bd9Sstevel@tonic-gate 			} else if (bp->b_flag & MSGNOTMARKNEXT) {
16857c478bd9Sstevel@tonic-gate 				/*
16867c478bd9Sstevel@tonic-gate 				 * Record that this is not the position of
16877c478bd9Sstevel@tonic-gate 				 * the mark either in q_last or in
16887c478bd9Sstevel@tonic-gate 				 * STRNOTATMARK.
16897c478bd9Sstevel@tonic-gate 				 */
16907c478bd9Sstevel@tonic-gate 				if (q->q_last != NULL) {
16917c478bd9Sstevel@tonic-gate 					q->q_last->b_flag &= ~MSGMARKNEXT;
16927c478bd9Sstevel@tonic-gate 					q->q_last->b_flag |= MSGNOTMARKNEXT;
16937c478bd9Sstevel@tonic-gate 				} else {
16947c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRATMARK;
16957c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRNOTATMARK;
16967c478bd9Sstevel@tonic-gate 				}
16977c478bd9Sstevel@tonic-gate 			}
16987c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & RSLEEP) {
16997c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~RSLEEP;
17007c478bd9Sstevel@tonic-gate 				cv_broadcast(&q->q_wait);
17017c478bd9Sstevel@tonic-gate 			}
17027c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
17037c478bd9Sstevel@tonic-gate 			freemsg(bp);
17047c478bd9Sstevel@tonic-gate 			return (0);
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 		wakeups = RSLEEP;
17077c478bd9Sstevel@tonic-gate 		if (bp->b_band == 0) {
17087c478bd9Sstevel@tonic-gate 			firstmsgsigs = S_INPUT | S_RDNORM;
17097c478bd9Sstevel@tonic-gate 			pollwakeups = POLLIN | POLLRDNORM;
17107c478bd9Sstevel@tonic-gate 		} else {
17117c478bd9Sstevel@tonic-gate 			firstmsgsigs = S_INPUT | S_RDBAND;
17127c478bd9Sstevel@tonic-gate 			pollwakeups = POLLIN | POLLRDBAND;
17137c478bd9Sstevel@tonic-gate 		}
17147c478bd9Sstevel@tonic-gate 		if (rput_opt & SR_SIGALLDATA)
17157c478bd9Sstevel@tonic-gate 			allmsgsigs = firstmsgsigs;
17167c478bd9Sstevel@tonic-gate 		else
17177c478bd9Sstevel@tonic-gate 			allmsgsigs = 0;
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
17207c478bd9Sstevel@tonic-gate 		if ((rput_opt & SR_CONSOL_DATA) &&
17213584396bSja97890 		    (q->q_last != NULL) &&
17227c478bd9Sstevel@tonic-gate 		    (bp->b_flag & (MSGMARK|MSGDELIM)) == 0) {
17237c478bd9Sstevel@tonic-gate 			/*
17243584396bSja97890 			 * Consolidate an M_DATA message onto an M_DATA,
17257c478bd9Sstevel@tonic-gate 			 * M_PROTO, or M_PCPROTO by merging it with q_last.
17267c478bd9Sstevel@tonic-gate 			 * The consolidation does not take place if
17277c478bd9Sstevel@tonic-gate 			 * the old message is marked with either of the
17287c478bd9Sstevel@tonic-gate 			 * marks or the delim flag or if the new
17297c478bd9Sstevel@tonic-gate 			 * message is marked with MSGMARK. The MSGMARK
17307c478bd9Sstevel@tonic-gate 			 * check is needed to handle the odd semantics of
17317c478bd9Sstevel@tonic-gate 			 * MSGMARK where essentially the whole message
17327c478bd9Sstevel@tonic-gate 			 * is to be treated as marked.
17337c478bd9Sstevel@tonic-gate 			 * Carry any MSGMARKNEXT  and MSGNOTMARKNEXT from the
17347c478bd9Sstevel@tonic-gate 			 * new message to the front of the b_cont chain.
17357c478bd9Sstevel@tonic-gate 			 */
17363584396bSja97890 			mblk_t *lbp = q->q_last;
17373584396bSja97890 			unsigned char db_type = lbp->b_datap->db_type;
17387c478bd9Sstevel@tonic-gate 
17393584396bSja97890 			if ((db_type == M_DATA || db_type == M_PROTO ||
17403584396bSja97890 			    db_type == M_PCPROTO) &&
17413584396bSja97890 			    !(lbp->b_flag & (MSGDELIM|MSGMARK|MSGMARKNEXT))) {
17427c478bd9Sstevel@tonic-gate 				rmvq_noenab(q, lbp);
17437c478bd9Sstevel@tonic-gate 				/*
17447c478bd9Sstevel@tonic-gate 				 * The first message in the b_cont list
17457c478bd9Sstevel@tonic-gate 				 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
17467c478bd9Sstevel@tonic-gate 				 * We need to handle the case where we
17473584396bSja97890 				 * are appending:
17487c478bd9Sstevel@tonic-gate 				 *
17497c478bd9Sstevel@tonic-gate 				 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
17507c478bd9Sstevel@tonic-gate 				 * 2) a MSGMARKNEXT to a plain message.
17517c478bd9Sstevel@tonic-gate 				 * 3) a MSGNOTMARKNEXT to a plain message
17527c478bd9Sstevel@tonic-gate 				 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
17537c478bd9Sstevel@tonic-gate 				 *    message.
17547c478bd9Sstevel@tonic-gate 				 *
17557c478bd9Sstevel@tonic-gate 				 * Thus we never append a MSGMARKNEXT or
17567c478bd9Sstevel@tonic-gate 				 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
17577c478bd9Sstevel@tonic-gate 				 */
17587c478bd9Sstevel@tonic-gate 				if (bp->b_flag & MSGMARKNEXT) {
17597c478bd9Sstevel@tonic-gate 					lbp->b_flag |= MSGMARKNEXT;
17607c478bd9Sstevel@tonic-gate 					lbp->b_flag &= ~MSGNOTMARKNEXT;
17617c478bd9Sstevel@tonic-gate 					bp->b_flag &= ~MSGMARKNEXT;
17627c478bd9Sstevel@tonic-gate 				} else if (bp->b_flag & MSGNOTMARKNEXT) {
17637c478bd9Sstevel@tonic-gate 					lbp->b_flag |= MSGNOTMARKNEXT;
17647c478bd9Sstevel@tonic-gate 					bp->b_flag &= ~MSGNOTMARKNEXT;
17657c478bd9Sstevel@tonic-gate 				}
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 				linkb(lbp, bp);
17687c478bd9Sstevel@tonic-gate 				bp = lbp;
17697c478bd9Sstevel@tonic-gate 				/*
17707c478bd9Sstevel@tonic-gate 				 * The new message logically isn't the first
17717c478bd9Sstevel@tonic-gate 				 * even though the q_first check below thinks
17727c478bd9Sstevel@tonic-gate 				 * it is. Clear the firstmsgsigs to make it
17737c478bd9Sstevel@tonic-gate 				 * not appear to be first.
17747c478bd9Sstevel@tonic-gate 				 */
17757c478bd9Sstevel@tonic-gate 				firstmsgsigs = 0;
17767c478bd9Sstevel@tonic-gate 			}
17777c478bd9Sstevel@tonic-gate 		}
17787c478bd9Sstevel@tonic-gate 		break;
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	case M_PASSFP:
17817c478bd9Sstevel@tonic-gate 		wakeups = RSLEEP;
17827c478bd9Sstevel@tonic-gate 		allmsgsigs = 0;
17837c478bd9Sstevel@tonic-gate 		if (bp->b_band == 0) {
17847c478bd9Sstevel@tonic-gate 			firstmsgsigs = S_INPUT | S_RDNORM;
17857c478bd9Sstevel@tonic-gate 			pollwakeups = POLLIN | POLLRDNORM;
17867c478bd9Sstevel@tonic-gate 		} else {
17877c478bd9Sstevel@tonic-gate 			firstmsgsigs = S_INPUT | S_RDBAND;
17887c478bd9Sstevel@tonic-gate 			pollwakeups = POLLIN | POLLRDBAND;
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
17917c478bd9Sstevel@tonic-gate 		break;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	case M_PROTO:
17947c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
17957c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_rprotofunc != NULL);
17967c478bd9Sstevel@tonic-gate 		bp = (stp->sd_rprotofunc)(stp->sd_vnode, bp,
17977c478bd9Sstevel@tonic-gate 		    &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
17987c478bd9Sstevel@tonic-gate #define	ALLSIG	(S_INPUT|S_HIPRI|S_OUTPUT|S_MSG|S_ERROR|S_HANGUP|S_RDNORM|\
17997c478bd9Sstevel@tonic-gate 		S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)
18007c478bd9Sstevel@tonic-gate #define	ALLPOLL	(POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLWRNORM|POLLRDBAND|\
18017c478bd9Sstevel@tonic-gate 		POLLWRBAND)
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 		ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
18047c478bd9Sstevel@tonic-gate 		ASSERT((firstmsgsigs & ~ALLSIG) == 0);
18057c478bd9Sstevel@tonic-gate 		ASSERT((allmsgsigs & ~ALLSIG) == 0);
18067c478bd9Sstevel@tonic-gate 		ASSERT((pollwakeups & ~ALLPOLL) == 0);
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
18097c478bd9Sstevel@tonic-gate 		break;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	default:
18127c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_rmiscfunc != NULL);
18137c478bd9Sstevel@tonic-gate 		bp = (stp->sd_rmiscfunc)(stp->sd_vnode, bp,
18147c478bd9Sstevel@tonic-gate 		    &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
18157c478bd9Sstevel@tonic-gate 		ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
18167c478bd9Sstevel@tonic-gate 		ASSERT((firstmsgsigs & ~ALLSIG) == 0);
18177c478bd9Sstevel@tonic-gate 		ASSERT((allmsgsigs & ~ALLSIG) == 0);
18187c478bd9Sstevel@tonic-gate 		ASSERT((pollwakeups & ~ALLPOLL) == 0);
18197c478bd9Sstevel@tonic-gate #undef	ALLSIG
18207c478bd9Sstevel@tonic-gate #undef	ALLPOLL
18217c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
18227c478bd9Sstevel@tonic-gate 		break;
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/* By default generate superset of signals */
18277c478bd9Sstevel@tonic-gate 	signals = (firstmsgsigs | allmsgsigs);
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	/*
18307c478bd9Sstevel@tonic-gate 	 * The  proto and misc functions can return multiple messages
18317c478bd9Sstevel@tonic-gate 	 * as a b_next chain. Such messages are processed separately.
18327c478bd9Sstevel@tonic-gate 	 */
18337c478bd9Sstevel@tonic-gate one_more:
18347c478bd9Sstevel@tonic-gate 	hipri_sig = 0;
18357c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
18367c478bd9Sstevel@tonic-gate 		nextbp = NULL;
18377c478bd9Sstevel@tonic-gate 	} else {
18387c478bd9Sstevel@tonic-gate 		nextbp = bp->b_next;
18397c478bd9Sstevel@tonic-gate 		bp->b_next = NULL;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 		switch (bp->b_datap->db_type) {
18427c478bd9Sstevel@tonic-gate 		case M_PCPROTO:
18437c478bd9Sstevel@tonic-gate 			/*
18447c478bd9Sstevel@tonic-gate 			 * Only one priority protocol message is allowed at the
18457c478bd9Sstevel@tonic-gate 			 * stream head at a time.
18467c478bd9Sstevel@tonic-gate 			 */
18477c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & STRPRI) {
18487c478bd9Sstevel@tonic-gate 				TRACE_0(TR_FAC_STREAMS_FR, TR_STRRPUT_PROTERR,
18497c478bd9Sstevel@tonic-gate 				    "M_PCPROTO already at head");
18507c478bd9Sstevel@tonic-gate 				freemsg(bp);
18517c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
18527c478bd9Sstevel@tonic-gate 				goto done;
18537c478bd9Sstevel@tonic-gate 			}
18547c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRPRI;
18557c478bd9Sstevel@tonic-gate 			hipri_sig = 1;
18567c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
18577c478bd9Sstevel@tonic-gate 		case M_DATA:
18587c478bd9Sstevel@tonic-gate 		case M_PROTO:
18597c478bd9Sstevel@tonic-gate 		case M_PASSFP:
18607c478bd9Sstevel@tonic-gate 			band = bp->b_band;
18617c478bd9Sstevel@tonic-gate 			/*
18627c478bd9Sstevel@tonic-gate 			 * Marking doesn't work well when messages
18637c478bd9Sstevel@tonic-gate 			 * are marked in more than one band.  We only
18647c478bd9Sstevel@tonic-gate 			 * remember the last message received, even if
18657c478bd9Sstevel@tonic-gate 			 * it is placed on the queue ahead of other
18667c478bd9Sstevel@tonic-gate 			 * marked messages.
18677c478bd9Sstevel@tonic-gate 			 */
18687c478bd9Sstevel@tonic-gate 			if (bp->b_flag & MSGMARK)
18697c478bd9Sstevel@tonic-gate 				stp->sd_mark = bp;
18707c478bd9Sstevel@tonic-gate 			(void) putq(q, bp);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 			/*
18737c478bd9Sstevel@tonic-gate 			 * If message is a PCPROTO message, always use
18747c478bd9Sstevel@tonic-gate 			 * firstmsgsigs to determine if a signal should be
18757c478bd9Sstevel@tonic-gate 			 * sent as strrput is the only place to send
18767c478bd9Sstevel@tonic-gate 			 * signals for PCPROTO. Other messages are based on
18777c478bd9Sstevel@tonic-gate 			 * the STRGETINPROG flag. The flag determines if
18787c478bd9Sstevel@tonic-gate 			 * strrput or (k)strgetmsg will be responsible for
18797c478bd9Sstevel@tonic-gate 			 * sending the signals, in the firstmsgsigs case.
18807c478bd9Sstevel@tonic-gate 			 */
18817c478bd9Sstevel@tonic-gate 			if ((hipri_sig == 1) ||
18827c478bd9Sstevel@tonic-gate 			    (((stp->sd_flag & STRGETINPROG) == 0) &&
18837c478bd9Sstevel@tonic-gate 			    (q->q_first == bp)))
18847c478bd9Sstevel@tonic-gate 				signals = (firstmsgsigs | allmsgsigs);
18857c478bd9Sstevel@tonic-gate 			else
18867c478bd9Sstevel@tonic-gate 				signals = allmsgsigs;
18877c478bd9Sstevel@tonic-gate 			break;
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 		default:
18907c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
18917c478bd9Sstevel@tonic-gate 			(void) strrput_nondata(q, bp);
18927c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
18937c478bd9Sstevel@tonic-gate 			break;
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 	}
18967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
18977c478bd9Sstevel@tonic-gate 	/*
18987c478bd9Sstevel@tonic-gate 	 * Wake sleeping read/getmsg and cancel deferred wakeup
18997c478bd9Sstevel@tonic-gate 	 */
19007c478bd9Sstevel@tonic-gate 	if (wakeups & RSLEEP)
19017c478bd9Sstevel@tonic-gate 		stp->sd_wakeq &= ~RSLEEP;
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	wakeups &= stp->sd_flag;
19047c478bd9Sstevel@tonic-gate 	if (wakeups & RSLEEP) {
19057c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~RSLEEP;
19067c478bd9Sstevel@tonic-gate 		cv_broadcast(&q->q_wait);
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 	if (wakeups & WSLEEP) {
19097c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~WSLEEP;
19107c478bd9Sstevel@tonic-gate 		cv_broadcast(&_WR(q)->q_wait);
19117c478bd9Sstevel@tonic-gate 	}
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	if (pollwakeups != 0) {
19147c478bd9Sstevel@tonic-gate 		if (pollwakeups == (POLLIN | POLLRDNORM)) {
19157c478bd9Sstevel@tonic-gate 			/*
19167c478bd9Sstevel@tonic-gate 			 * Can't use rput_opt since it was not
19177c478bd9Sstevel@tonic-gate 			 * read when sd_lock was held and SR_POLLIN is changed
19187c478bd9Sstevel@tonic-gate 			 * by strpoll() under sd_lock.
19197c478bd9Sstevel@tonic-gate 			 */
19207c478bd9Sstevel@tonic-gate 			if (!(stp->sd_rput_opt & SR_POLLIN))
19217c478bd9Sstevel@tonic-gate 				goto no_pollwake;
19227c478bd9Sstevel@tonic-gate 			stp->sd_rput_opt &= ~SR_POLLIN;
19237c478bd9Sstevel@tonic-gate 		}
19247c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
19257c478bd9Sstevel@tonic-gate 		pollwakeup(&stp->sd_pollist, pollwakeups);
19267c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
19277c478bd9Sstevel@tonic-gate 	}
19287c478bd9Sstevel@tonic-gate no_pollwake:
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	/*
19317c478bd9Sstevel@tonic-gate 	 * strsendsig can handle multiple signals with a
19327c478bd9Sstevel@tonic-gate 	 * single call.
19337c478bd9Sstevel@tonic-gate 	 */
19347c478bd9Sstevel@tonic-gate 	if (stp->sd_sigflags & signals)
19357c478bd9Sstevel@tonic-gate 		strsendsig(stp->sd_siglist, signals, band, 0);
19367c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate done:
19407c478bd9Sstevel@tonic-gate 	if (nextbp == NULL)
19417c478bd9Sstevel@tonic-gate 		return (0);
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	/*
19447c478bd9Sstevel@tonic-gate 	 * Any signals were handled the first time.
19457c478bd9Sstevel@tonic-gate 	 * Wakeups and pollwakeups are redone to avoid any race
19467c478bd9Sstevel@tonic-gate 	 * conditions - all the messages are not queued until the
19477c478bd9Sstevel@tonic-gate 	 * last message has been processed by strrput.
19487c478bd9Sstevel@tonic-gate 	 */
19497c478bd9Sstevel@tonic-gate 	bp = nextbp;
19507c478bd9Sstevel@tonic-gate 	signals = firstmsgsigs = allmsgsigs = 0;
19517c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
19527c478bd9Sstevel@tonic-gate 	goto one_more;
19537c478bd9Sstevel@tonic-gate }
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate static void
19567c478bd9Sstevel@tonic-gate log_dupioc(queue_t *rq, mblk_t *bp)
19577c478bd9Sstevel@tonic-gate {
19587c478bd9Sstevel@tonic-gate 	queue_t *wq, *qp;
19597c478bd9Sstevel@tonic-gate 	char *modnames, *mnp, *dname;
19607c478bd9Sstevel@tonic-gate 	size_t maxmodstr;
19617c478bd9Sstevel@tonic-gate 	boolean_t islast;
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	/*
19647c478bd9Sstevel@tonic-gate 	 * Allocate a buffer large enough to hold the names of nstrpush modules
19657c478bd9Sstevel@tonic-gate 	 * and one driver, with spaces between and NUL terminator.  If we can't
19667c478bd9Sstevel@tonic-gate 	 * get memory, then we'll just log the driver name.
19677c478bd9Sstevel@tonic-gate 	 */
19687c478bd9Sstevel@tonic-gate 	maxmodstr = nstrpush * (FMNAMESZ + 1);
19697c478bd9Sstevel@tonic-gate 	mnp = modnames = kmem_alloc(maxmodstr, KM_NOSLEEP);
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	/* march down write side to print log message down to the driver */
19727c478bd9Sstevel@tonic-gate 	wq = WR(rq);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	/* make sure q_next doesn't shift around while we're grabbing data */
19757c478bd9Sstevel@tonic-gate 	claimstr(wq);
19767c478bd9Sstevel@tonic-gate 	qp = wq->q_next;
19777c478bd9Sstevel@tonic-gate 	do {
19787c478bd9Sstevel@tonic-gate 		if ((dname = qp->q_qinfo->qi_minfo->mi_idname) == NULL)
19797c478bd9Sstevel@tonic-gate 			dname = "?";
19807c478bd9Sstevel@tonic-gate 		islast = !SAMESTR(qp) || qp->q_next == NULL;
19817c478bd9Sstevel@tonic-gate 		if (modnames == NULL) {
19827c478bd9Sstevel@tonic-gate 			/*
19837c478bd9Sstevel@tonic-gate 			 * If we don't have memory, then get the driver name in
19847c478bd9Sstevel@tonic-gate 			 * the log where we can see it.  Note that memory
19857c478bd9Sstevel@tonic-gate 			 * pressure is a possible cause of these sorts of bugs.
19867c478bd9Sstevel@tonic-gate 			 */
19877c478bd9Sstevel@tonic-gate 			if (islast) {
19887c478bd9Sstevel@tonic-gate 				modnames = dname;
19897c478bd9Sstevel@tonic-gate 				maxmodstr = 0;
19907c478bd9Sstevel@tonic-gate 			}
19917c478bd9Sstevel@tonic-gate 		} else {
19927c478bd9Sstevel@tonic-gate 			mnp += snprintf(mnp, FMNAMESZ + 1, "%s", dname);
19937c478bd9Sstevel@tonic-gate 			if (!islast)
19947c478bd9Sstevel@tonic-gate 				*mnp++ = ' ';
19957c478bd9Sstevel@tonic-gate 		}
19967c478bd9Sstevel@tonic-gate 		qp = qp->q_next;
19977c478bd9Sstevel@tonic-gate 	} while (!islast);
19987c478bd9Sstevel@tonic-gate 	releasestr(wq);
19997c478bd9Sstevel@tonic-gate 	/* Cannot happen unless stream head is corrupt. */
20007c478bd9Sstevel@tonic-gate 	ASSERT(modnames != NULL);
20017c478bd9Sstevel@tonic-gate 	(void) strlog(rq->q_qinfo->qi_minfo->mi_idnum, 0, 1,
20027c478bd9Sstevel@tonic-gate 	    SL_CONSOLE|SL_TRACE|SL_ERROR,
20037c478bd9Sstevel@tonic-gate 	    "Warning: stream %p received duplicate %X M_IOC%s; module list: %s",
20047c478bd9Sstevel@tonic-gate 	    rq->q_ptr, ((struct iocblk *)bp->b_rptr)->ioc_cmd,
20057c478bd9Sstevel@tonic-gate 	    (DB_TYPE(bp) == M_IOCACK ? "ACK" : "NAK"), modnames);
20067c478bd9Sstevel@tonic-gate 	if (maxmodstr != 0)
20077c478bd9Sstevel@tonic-gate 		kmem_free(modnames, maxmodstr);
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate int
20117c478bd9Sstevel@tonic-gate strrput_nondata(queue_t *q, mblk_t *bp)
20127c478bd9Sstevel@tonic-gate {
20137c478bd9Sstevel@tonic-gate 	struct stdata *stp;
20147c478bd9Sstevel@tonic-gate 	struct iocblk *iocbp;
20157c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
20167c478bd9Sstevel@tonic-gate 	struct copyreq *reqp;
20177c478bd9Sstevel@tonic-gate 	struct copyresp *resp;
20187c478bd9Sstevel@tonic-gate 	unsigned char bpri;
20197c478bd9Sstevel@tonic-gate 	unsigned char  flushed_already = 0;
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	stp = (struct stdata *)q->q_ptr;
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	ASSERT(!(stp->sd_flag & STPLEX));
20247c478bd9Sstevel@tonic-gate 	ASSERT(qclaimed(q));
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	switch (bp->b_datap->db_type) {
20277c478bd9Sstevel@tonic-gate 	case M_ERROR:
20287c478bd9Sstevel@tonic-gate 		/*
20297c478bd9Sstevel@tonic-gate 		 * An error has occurred downstream, the errno is in the first
20307c478bd9Sstevel@tonic-gate 		 * bytes of the message.
20317c478bd9Sstevel@tonic-gate 		 */
20327c478bd9Sstevel@tonic-gate 		if ((bp->b_wptr - bp->b_rptr) == 2) {	/* New flavor */
20337c478bd9Sstevel@tonic-gate 			unsigned char rw = 0;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
20367c478bd9Sstevel@tonic-gate 			if (*bp->b_rptr != NOERROR) {	/* read error */
20377c478bd9Sstevel@tonic-gate 				if (*bp->b_rptr != 0) {
20387c478bd9Sstevel@tonic-gate 					if (stp->sd_flag & STRDERR)
20397c478bd9Sstevel@tonic-gate 						flushed_already |= FLUSHR;
20407c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRDERR;
20417c478bd9Sstevel@tonic-gate 					rw |= FLUSHR;
20427c478bd9Sstevel@tonic-gate 				} else {
20437c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRDERR;
20447c478bd9Sstevel@tonic-gate 				}
20457c478bd9Sstevel@tonic-gate 				stp->sd_rerror = *bp->b_rptr;
20467c478bd9Sstevel@tonic-gate 			}
20477c478bd9Sstevel@tonic-gate 			bp->b_rptr++;
20487c478bd9Sstevel@tonic-gate 			if (*bp->b_rptr != NOERROR) {	/* write error */
20497c478bd9Sstevel@tonic-gate 				if (*bp->b_rptr != 0) {
20507c478bd9Sstevel@tonic-gate 					if (stp->sd_flag & STWRERR)
20517c478bd9Sstevel@tonic-gate 						flushed_already |= FLUSHW;
20527c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STWRERR;
20537c478bd9Sstevel@tonic-gate 					rw |= FLUSHW;
20547c478bd9Sstevel@tonic-gate 				} else {
20557c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STWRERR;
20567c478bd9Sstevel@tonic-gate 				}
20577c478bd9Sstevel@tonic-gate 				stp->sd_werror = *bp->b_rptr;
20587c478bd9Sstevel@tonic-gate 			}
20597c478bd9Sstevel@tonic-gate 			if (rw) {
20607c478bd9Sstevel@tonic-gate 				TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_WAKE,
20617c478bd9Sstevel@tonic-gate 				    "strrput cv_broadcast:q %p, bp %p",
20627c478bd9Sstevel@tonic-gate 				    q, bp);
20637c478bd9Sstevel@tonic-gate 				cv_broadcast(&q->q_wait); /* readers */
20647c478bd9Sstevel@tonic-gate 				cv_broadcast(&_WR(q)->q_wait); /* writers */
20657c478bd9Sstevel@tonic-gate 				cv_broadcast(&stp->sd_monitor); /* ioctllers */
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
20687c478bd9Sstevel@tonic-gate 				pollwakeup(&stp->sd_pollist, POLLERR);
20697c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 				if (stp->sd_sigflags & S_ERROR)
20727c478bd9Sstevel@tonic-gate 					strsendsig(stp->sd_siglist, S_ERROR, 0,
20737c478bd9Sstevel@tonic-gate 					    ((rw & FLUSHR) ? stp->sd_rerror :
20747c478bd9Sstevel@tonic-gate 					    stp->sd_werror));
20757c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
20767c478bd9Sstevel@tonic-gate 				/*
20777c478bd9Sstevel@tonic-gate 				 * Send the M_FLUSH only
20787c478bd9Sstevel@tonic-gate 				 * for the first M_ERROR
20797c478bd9Sstevel@tonic-gate 				 * message on the stream
20807c478bd9Sstevel@tonic-gate 				 */
20817c478bd9Sstevel@tonic-gate 				if (flushed_already == rw) {
20827c478bd9Sstevel@tonic-gate 					freemsg(bp);
20837c478bd9Sstevel@tonic-gate 					return (0);
20847c478bd9Sstevel@tonic-gate 				}
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 				bp->b_datap->db_type = M_FLUSH;
20877c478bd9Sstevel@tonic-gate 				*bp->b_rptr = rw;
20887c478bd9Sstevel@tonic-gate 				bp->b_wptr = bp->b_rptr + 1;
20897c478bd9Sstevel@tonic-gate 				/*
20907c478bd9Sstevel@tonic-gate 				 * Protect against the driver
20917c478bd9Sstevel@tonic-gate 				 * passing up messages after
20927c478bd9Sstevel@tonic-gate 				 * it has done a qprocsoff
20937c478bd9Sstevel@tonic-gate 				 */
20947c478bd9Sstevel@tonic-gate 				if (_OTHERQ(q)->q_next == NULL)
20957c478bd9Sstevel@tonic-gate 					freemsg(bp);
20967c478bd9Sstevel@tonic-gate 				else
20977c478bd9Sstevel@tonic-gate 					qreply(q, bp);
20987c478bd9Sstevel@tonic-gate 				return (0);
20997c478bd9Sstevel@tonic-gate 			} else
21007c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
21017c478bd9Sstevel@tonic-gate 		} else if (*bp->b_rptr != 0) {		/* Old flavor */
21027c478bd9Sstevel@tonic-gate 				if (stp->sd_flag & (STRDERR|STWRERR))
21037c478bd9Sstevel@tonic-gate 					flushed_already = FLUSHRW;
21047c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
21057c478bd9Sstevel@tonic-gate 				stp->sd_flag |= (STRDERR|STWRERR);
21067c478bd9Sstevel@tonic-gate 				stp->sd_rerror = *bp->b_rptr;
21077c478bd9Sstevel@tonic-gate 				stp->sd_werror = *bp->b_rptr;
21087c478bd9Sstevel@tonic-gate 				TRACE_2(TR_FAC_STREAMS_FR,
21097c478bd9Sstevel@tonic-gate 				    TR_STRRPUT_WAKE2,
21107c478bd9Sstevel@tonic-gate 				    "strrput wakeup #2:q %p, bp %p", q, bp);
21117c478bd9Sstevel@tonic-gate 				cv_broadcast(&q->q_wait); /* the readers */
21127c478bd9Sstevel@tonic-gate 				cv_broadcast(&_WR(q)->q_wait); /* the writers */
21137c478bd9Sstevel@tonic-gate 				cv_broadcast(&stp->sd_monitor); /* ioctllers */
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
21167c478bd9Sstevel@tonic-gate 				pollwakeup(&stp->sd_pollist, POLLERR);
21177c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 				if (stp->sd_sigflags & S_ERROR)
21207c478bd9Sstevel@tonic-gate 					strsendsig(stp->sd_siglist, S_ERROR, 0,
21217c478bd9Sstevel@tonic-gate 					    (stp->sd_werror ? stp->sd_werror :
21227c478bd9Sstevel@tonic-gate 					    stp->sd_rerror));
21237c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 				/*
21267c478bd9Sstevel@tonic-gate 				 * Send the M_FLUSH only
21277c478bd9Sstevel@tonic-gate 				 * for the first M_ERROR
21287c478bd9Sstevel@tonic-gate 				 * message on the stream
21297c478bd9Sstevel@tonic-gate 				 */
21307c478bd9Sstevel@tonic-gate 				if (flushed_already != FLUSHRW) {
21317c478bd9Sstevel@tonic-gate 					bp->b_datap->db_type = M_FLUSH;
21327c478bd9Sstevel@tonic-gate 					*bp->b_rptr = FLUSHRW;
21337c478bd9Sstevel@tonic-gate 					/*
21347c478bd9Sstevel@tonic-gate 					 * Protect against the driver passing up
21357c478bd9Sstevel@tonic-gate 					 * messages after it has done a
21367c478bd9Sstevel@tonic-gate 					 * qprocsoff.
21377c478bd9Sstevel@tonic-gate 					 */
21387c478bd9Sstevel@tonic-gate 				if (_OTHERQ(q)->q_next == NULL)
21397c478bd9Sstevel@tonic-gate 					freemsg(bp);
21407c478bd9Sstevel@tonic-gate 				else
21417c478bd9Sstevel@tonic-gate 					qreply(q, bp);
21427c478bd9Sstevel@tonic-gate 				return (0);
21437c478bd9Sstevel@tonic-gate 				}
21447c478bd9Sstevel@tonic-gate 		}
21457c478bd9Sstevel@tonic-gate 		freemsg(bp);
21467c478bd9Sstevel@tonic-gate 		return (0);
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	case M_HANGUP:
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 		freemsg(bp);
21517c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
21527c478bd9Sstevel@tonic-gate 		stp->sd_werror = ENXIO;
21537c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRHUP;
21547c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~(WSLEEP|RSLEEP);
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 		/*
21577c478bd9Sstevel@tonic-gate 		 * send signal if controlling tty
21587c478bd9Sstevel@tonic-gate 		 */
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp) {
21617c478bd9Sstevel@tonic-gate 			prsignal(stp->sd_sidp, SIGHUP);
21627c478bd9Sstevel@tonic-gate 			if (stp->sd_sidp != stp->sd_pgidp)
21637c478bd9Sstevel@tonic-gate 				pgsignal(stp->sd_pgidp, SIGTSTP);
21647c478bd9Sstevel@tonic-gate 		}
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 		/*
21677c478bd9Sstevel@tonic-gate 		 * wake up read, write, and exception pollers and
21687c478bd9Sstevel@tonic-gate 		 * reset wakeup mechanism.
21697c478bd9Sstevel@tonic-gate 		 */
21707c478bd9Sstevel@tonic-gate 		cv_broadcast(&q->q_wait);	/* the readers */
21717c478bd9Sstevel@tonic-gate 		cv_broadcast(&_WR(q)->q_wait);	/* the writers */
21727c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);	/* the ioctllers */
21737c478bd9Sstevel@tonic-gate 		strhup(stp);
21749acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
21757c478bd9Sstevel@tonic-gate 		return (0);
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	case M_UNHANGUP:
21787c478bd9Sstevel@tonic-gate 		freemsg(bp);
21797c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
21807c478bd9Sstevel@tonic-gate 		stp->sd_werror = 0;
21817c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STRHUP;
21827c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
21837c478bd9Sstevel@tonic-gate 		return (0);
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	case M_SIG:
21867c478bd9Sstevel@tonic-gate 		/*
21877c478bd9Sstevel@tonic-gate 		 * Someone downstream wants to post a signal.  The
21887c478bd9Sstevel@tonic-gate 		 * signal to post is contained in the first byte of the
21897c478bd9Sstevel@tonic-gate 		 * message.  If the message would go on the front of
21907c478bd9Sstevel@tonic-gate 		 * the queue, send a signal to the process group
21917c478bd9Sstevel@tonic-gate 		 * (if not SIGPOLL) or to the siglist processes
21927c478bd9Sstevel@tonic-gate 		 * (SIGPOLL).  If something is already on the queue,
21937c478bd9Sstevel@tonic-gate 		 * OR if we are delivering a delayed suspend (*sigh*
21947c478bd9Sstevel@tonic-gate 		 * another "tty" hack) and there's no one sleeping already,
21957c478bd9Sstevel@tonic-gate 		 * just enqueue the message.
21967c478bd9Sstevel@tonic-gate 		 */
21977c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
21987c478bd9Sstevel@tonic-gate 		if (q->q_first || (*bp->b_rptr == SIGTSTP &&
21997c478bd9Sstevel@tonic-gate 		    !(stp->sd_flag & RSLEEP))) {
22007c478bd9Sstevel@tonic-gate 			(void) putq(q, bp);
22017c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
22027c478bd9Sstevel@tonic-gate 			return (0);
22037c478bd9Sstevel@tonic-gate 		}
22047c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
22057c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	case M_PCSIG:
22087c478bd9Sstevel@tonic-gate 		/*
22097c478bd9Sstevel@tonic-gate 		 * Don't enqueue, just post the signal.
22107c478bd9Sstevel@tonic-gate 		 */
22117c478bd9Sstevel@tonic-gate 		strsignal(stp, *bp->b_rptr, 0L);
22127c478bd9Sstevel@tonic-gate 		freemsg(bp);
22137c478bd9Sstevel@tonic-gate 		return (0);
22147c478bd9Sstevel@tonic-gate 
2215ca9327a6Smeem 	case M_CMD:
2216ca9327a6Smeem 		if (MBLKL(bp) != sizeof (cmdblk_t)) {
2217ca9327a6Smeem 			freemsg(bp);
2218ca9327a6Smeem 			return (0);
2219ca9327a6Smeem 		}
2220ca9327a6Smeem 
2221ca9327a6Smeem 		mutex_enter(&stp->sd_lock);
2222ca9327a6Smeem 		if (stp->sd_flag & STRCMDWAIT) {
2223ca9327a6Smeem 			ASSERT(stp->sd_cmdblk == NULL);
2224ca9327a6Smeem 			stp->sd_cmdblk = bp;
2225ca9327a6Smeem 			cv_broadcast(&stp->sd_monitor);
2226ca9327a6Smeem 			mutex_exit(&stp->sd_lock);
2227ca9327a6Smeem 		} else {
2228ca9327a6Smeem 			mutex_exit(&stp->sd_lock);
2229ca9327a6Smeem 			freemsg(bp);
2230ca9327a6Smeem 		}
2231ca9327a6Smeem 		return (0);
2232ca9327a6Smeem 
22337c478bd9Sstevel@tonic-gate 	case M_FLUSH:
22347c478bd9Sstevel@tonic-gate 		/*
22357c478bd9Sstevel@tonic-gate 		 * Flush queues.  The indication of which queues to flush
22367c478bd9Sstevel@tonic-gate 		 * is in the first byte of the message.  If the read queue
22377c478bd9Sstevel@tonic-gate 		 * is specified, then flush it.  If FLUSHBAND is set, just
22387c478bd9Sstevel@tonic-gate 		 * flush the band specified by the second byte of the message.
22397c478bd9Sstevel@tonic-gate 		 *
22407c478bd9Sstevel@tonic-gate 		 * If a module has issued a M_SETOPT to not flush hi
22417c478bd9Sstevel@tonic-gate 		 * priority messages off of the stream head, then pass this
22427c478bd9Sstevel@tonic-gate 		 * flag into the flushq code to preserve such messages.
22437c478bd9Sstevel@tonic-gate 		 */
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 		if (*bp->b_rptr & FLUSHR) {
22467c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
22477c478bd9Sstevel@tonic-gate 			if (*bp->b_rptr & FLUSHBAND) {
22487c478bd9Sstevel@tonic-gate 				ASSERT((bp->b_wptr - bp->b_rptr) >= 2);
22497c478bd9Sstevel@tonic-gate 				flushband(q, *(bp->b_rptr + 1), FLUSHALL);
22507c478bd9Sstevel@tonic-gate 			} else
22517c478bd9Sstevel@tonic-gate 				flushq_common(q, FLUSHALL,
22527c478bd9Sstevel@tonic-gate 				    stp->sd_read_opt & RFLUSHPCPROT);
22537c478bd9Sstevel@tonic-gate 			if ((q->q_first == NULL) ||
22547c478bd9Sstevel@tonic-gate 			    (q->q_first->b_datap->db_type < QPCTL))
22557c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
22567c478bd9Sstevel@tonic-gate 			else {
22577c478bd9Sstevel@tonic-gate 				ASSERT(stp->sd_flag & STRPRI);
22587c478bd9Sstevel@tonic-gate 			}
22597c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
22607c478bd9Sstevel@tonic-gate 		}
22617c478bd9Sstevel@tonic-gate 		if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
22627c478bd9Sstevel@tonic-gate 			*bp->b_rptr &= ~FLUSHR;
22637c478bd9Sstevel@tonic-gate 			bp->b_flag |= MSGNOLOOP;
22647c478bd9Sstevel@tonic-gate 			/*
22657c478bd9Sstevel@tonic-gate 			 * Protect against the driver passing up
22667c478bd9Sstevel@tonic-gate 			 * messages after it has done a qprocsoff.
22677c478bd9Sstevel@tonic-gate 			 */
22687c478bd9Sstevel@tonic-gate 			if (_OTHERQ(q)->q_next == NULL)
22697c478bd9Sstevel@tonic-gate 				freemsg(bp);
22707c478bd9Sstevel@tonic-gate 			else
22717c478bd9Sstevel@tonic-gate 				qreply(q, bp);
22727c478bd9Sstevel@tonic-gate 			return (0);
22737c478bd9Sstevel@tonic-gate 		}
22747c478bd9Sstevel@tonic-gate 		freemsg(bp);
22757c478bd9Sstevel@tonic-gate 		return (0);
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	case M_IOCACK:
22787c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
22797c478bd9Sstevel@tonic-gate 		iocbp = (struct iocblk *)bp->b_rptr;
22807c478bd9Sstevel@tonic-gate 		/*
22817c478bd9Sstevel@tonic-gate 		 * If not waiting for ACK or NAK then just free msg.
22827c478bd9Sstevel@tonic-gate 		 * If incorrect id sequence number then just free msg.
22837c478bd9Sstevel@tonic-gate 		 * If already have ACK or NAK for user then this is a
22847c478bd9Sstevel@tonic-gate 		 *    duplicate, display a warning and free the msg.
22857c478bd9Sstevel@tonic-gate 		 */
22867c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
22877c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
22887c478bd9Sstevel@tonic-gate 		    (stp->sd_iocid != iocbp->ioc_id)) {
22897c478bd9Sstevel@tonic-gate 			/*
22907c478bd9Sstevel@tonic-gate 			 * If the ACK/NAK is a dup, display a message
22917c478bd9Sstevel@tonic-gate 			 * Dup is when sd_iocid == ioc_id, and
22927c478bd9Sstevel@tonic-gate 			 * sd_iocblk == <valid ptr> or -1 (the former
22937c478bd9Sstevel@tonic-gate 			 * is when an ioctl has been put on the stream
22947c478bd9Sstevel@tonic-gate 			 * head, but has not yet been consumed, the
22957c478bd9Sstevel@tonic-gate 			 * later is when it has been consumed).
22967c478bd9Sstevel@tonic-gate 			 */
22977c478bd9Sstevel@tonic-gate 			if ((stp->sd_iocid == iocbp->ioc_id) &&
22987c478bd9Sstevel@tonic-gate 			    (stp->sd_iocblk != NULL)) {
22997c478bd9Sstevel@tonic-gate 				log_dupioc(q, bp);
23007c478bd9Sstevel@tonic-gate 			}
23017c478bd9Sstevel@tonic-gate 			freemsg(bp);
23027c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
23037c478bd9Sstevel@tonic-gate 			return (0);
23047c478bd9Sstevel@tonic-gate 		}
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 		/*
23077c478bd9Sstevel@tonic-gate 		 * Assign ACK or NAK to user and wake up.
23087c478bd9Sstevel@tonic-gate 		 */
23097c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = bp;
23107c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);
23117c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
23127c478bd9Sstevel@tonic-gate 		return (0);
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	case M_COPYIN:
23157c478bd9Sstevel@tonic-gate 	case M_COPYOUT:
23167c478bd9Sstevel@tonic-gate 		reqp = (struct copyreq *)bp->b_rptr;
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 		/*
23197c478bd9Sstevel@tonic-gate 		 * If not waiting for ACK or NAK then just fail request.
23207c478bd9Sstevel@tonic-gate 		 * If already have ACK, NAK, or copy request, then just
23217c478bd9Sstevel@tonic-gate 		 * fail request.
23227c478bd9Sstevel@tonic-gate 		 * If incorrect id sequence number then just fail request.
23237c478bd9Sstevel@tonic-gate 		 */
23247c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
23257c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
23267c478bd9Sstevel@tonic-gate 		    (stp->sd_iocid != reqp->cq_id)) {
23277c478bd9Sstevel@tonic-gate 			if (bp->b_cont) {
23287c478bd9Sstevel@tonic-gate 				freemsg(bp->b_cont);
23297c478bd9Sstevel@tonic-gate 				bp->b_cont = NULL;
23307c478bd9Sstevel@tonic-gate 			}
23317c478bd9Sstevel@tonic-gate 			bp->b_datap->db_type = M_IOCDATA;
23327c478bd9Sstevel@tonic-gate 			bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
23337c478bd9Sstevel@tonic-gate 			resp = (struct copyresp *)bp->b_rptr;
23347c478bd9Sstevel@tonic-gate 			resp->cp_rval = (caddr_t)1;	/* failure */
23357c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
23367c478bd9Sstevel@tonic-gate 			putnext(stp->sd_wrq, bp);
23377c478bd9Sstevel@tonic-gate 			return (0);
23387c478bd9Sstevel@tonic-gate 		}
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 		/*
23417c478bd9Sstevel@tonic-gate 		 * Assign copy request to user and wake up.
23427c478bd9Sstevel@tonic-gate 		 */
23437c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = bp;
23447c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);
23457c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
23467c478bd9Sstevel@tonic-gate 		return (0);
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	case M_SETOPTS:
23497c478bd9Sstevel@tonic-gate 		/*
23507c478bd9Sstevel@tonic-gate 		 * Set stream head options (read option, write offset,
23517c478bd9Sstevel@tonic-gate 		 * min/max packet size, and/or high/low water marks for
23527c478bd9Sstevel@tonic-gate 		 * the read side only).
23537c478bd9Sstevel@tonic-gate 		 */
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 		bpri = 0;
23567c478bd9Sstevel@tonic-gate 		sop = (struct stroptions *)bp->b_rptr;
23577c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
23587c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_READOPT) {
23597c478bd9Sstevel@tonic-gate 			switch (sop->so_readopt & RMODEMASK) {
23607c478bd9Sstevel@tonic-gate 			case RNORM:
23617c478bd9Sstevel@tonic-gate 				stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
23627c478bd9Sstevel@tonic-gate 				break;
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 			case RMSGD:
23657c478bd9Sstevel@tonic-gate 				stp->sd_read_opt =
23667c478bd9Sstevel@tonic-gate 				    ((stp->sd_read_opt & ~RD_MSGNODIS) |
23677c478bd9Sstevel@tonic-gate 				    RD_MSGDIS);
23687c478bd9Sstevel@tonic-gate 				break;
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 			case RMSGN:
23717c478bd9Sstevel@tonic-gate 				stp->sd_read_opt =
23727c478bd9Sstevel@tonic-gate 				    ((stp->sd_read_opt & ~RD_MSGDIS) |
23737c478bd9Sstevel@tonic-gate 				    RD_MSGNODIS);
23747c478bd9Sstevel@tonic-gate 				break;
23757c478bd9Sstevel@tonic-gate 			}
23767c478bd9Sstevel@tonic-gate 			switch (sop->so_readopt & RPROTMASK) {
23777c478bd9Sstevel@tonic-gate 			case RPROTNORM:
23787c478bd9Sstevel@tonic-gate 				stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
23797c478bd9Sstevel@tonic-gate 				break;
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 			case RPROTDAT:
23827c478bd9Sstevel@tonic-gate 				stp->sd_read_opt =
23837c478bd9Sstevel@tonic-gate 				    ((stp->sd_read_opt & ~RD_PROTDIS) |
23847c478bd9Sstevel@tonic-gate 				    RD_PROTDAT);
23857c478bd9Sstevel@tonic-gate 				break;
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 			case RPROTDIS:
23887c478bd9Sstevel@tonic-gate 				stp->sd_read_opt =
23897c478bd9Sstevel@tonic-gate 				    ((stp->sd_read_opt & ~RD_PROTDAT) |
23907c478bd9Sstevel@tonic-gate 				    RD_PROTDIS);
23917c478bd9Sstevel@tonic-gate 				break;
23927c478bd9Sstevel@tonic-gate 			}
23937c478bd9Sstevel@tonic-gate 			switch (sop->so_readopt & RFLUSHMASK) {
23947c478bd9Sstevel@tonic-gate 			case RFLUSHPCPROT:
23957c478bd9Sstevel@tonic-gate 				/*
23967c478bd9Sstevel@tonic-gate 				 * This sets the stream head to NOT flush
23977c478bd9Sstevel@tonic-gate 				 * M_PCPROTO messages.
23987c478bd9Sstevel@tonic-gate 				 */
23997c478bd9Sstevel@tonic-gate 				stp->sd_read_opt |= RFLUSHPCPROT;
24007c478bd9Sstevel@tonic-gate 				break;
24017c478bd9Sstevel@tonic-gate 			}
24027c478bd9Sstevel@tonic-gate 		}
24037c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_ERROPT) {
24047c478bd9Sstevel@tonic-gate 			switch (sop->so_erropt & RERRMASK) {
24057c478bd9Sstevel@tonic-gate 			case RERRNORM:
24067c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRDERRNONPERSIST;
24077c478bd9Sstevel@tonic-gate 				break;
24087c478bd9Sstevel@tonic-gate 			case RERRNONPERSIST:
24097c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRDERRNONPERSIST;
24107c478bd9Sstevel@tonic-gate 				break;
24117c478bd9Sstevel@tonic-gate 			}
24127c478bd9Sstevel@tonic-gate 			switch (sop->so_erropt & WERRMASK) {
24137c478bd9Sstevel@tonic-gate 			case WERRNORM:
24147c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STWRERRNONPERSIST;
24157c478bd9Sstevel@tonic-gate 				break;
24167c478bd9Sstevel@tonic-gate 			case WERRNONPERSIST:
24177c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STWRERRNONPERSIST;
24187c478bd9Sstevel@tonic-gate 				break;
24197c478bd9Sstevel@tonic-gate 			}
24207c478bd9Sstevel@tonic-gate 		}
24217c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_COPYOPT) {
24227c478bd9Sstevel@tonic-gate 			if (sop->so_copyopt & ZCVMSAFE) {
24237c478bd9Sstevel@tonic-gate 				stp->sd_copyflag |= STZCVMSAFE;
24247c478bd9Sstevel@tonic-gate 				stp->sd_copyflag &= ~STZCVMUNSAFE;
24257c478bd9Sstevel@tonic-gate 			} else if (sop->so_copyopt & ZCVMUNSAFE) {
24267c478bd9Sstevel@tonic-gate 				stp->sd_copyflag |= STZCVMUNSAFE;
24277c478bd9Sstevel@tonic-gate 				stp->sd_copyflag &= ~STZCVMSAFE;
24287c478bd9Sstevel@tonic-gate 			}
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 			if (sop->so_copyopt & COPYCACHED) {
24317c478bd9Sstevel@tonic-gate 				stp->sd_copyflag |= STRCOPYCACHED;
24327c478bd9Sstevel@tonic-gate 			}
24337c478bd9Sstevel@tonic-gate 		}
24347c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_WROFF)
24357c478bd9Sstevel@tonic-gate 			stp->sd_wroff = sop->so_wroff;
2436c28749e9Skais 		if (sop->so_flags & SO_TAIL)
2437c28749e9Skais 			stp->sd_tail = sop->so_tail;
24387c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MINPSZ)
24397c478bd9Sstevel@tonic-gate 			q->q_minpsz = sop->so_minpsz;
24407c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MAXPSZ)
24417c478bd9Sstevel@tonic-gate 			q->q_maxpsz = sop->so_maxpsz;
24427c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MAXBLK)
24437c478bd9Sstevel@tonic-gate 			stp->sd_maxblk = sop->so_maxblk;
24447c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_HIWAT) {
24457c478bd9Sstevel@tonic-gate 			if (sop->so_flags & SO_BAND) {
2446d3e55dcdSgww 				if (strqset(q, QHIWAT,
2447d3e55dcdSgww 				    sop->so_band, sop->so_hiwat)) {
2448d3e55dcdSgww 					cmn_err(CE_WARN, "strrput: could not "
2449d3e55dcdSgww 					    "allocate qband\n");
2450d3e55dcdSgww 				} else {
24517c478bd9Sstevel@tonic-gate 					bpri = sop->so_band;
2452d3e55dcdSgww 				}
24537c478bd9Sstevel@tonic-gate 			} else {
24547c478bd9Sstevel@tonic-gate 				q->q_hiwat = sop->so_hiwat;
24557c478bd9Sstevel@tonic-gate 			}
24567c478bd9Sstevel@tonic-gate 		}
24577c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_LOWAT) {
24587c478bd9Sstevel@tonic-gate 			if (sop->so_flags & SO_BAND) {
2459d3e55dcdSgww 				if (strqset(q, QLOWAT,
2460d3e55dcdSgww 				    sop->so_band, sop->so_lowat)) {
2461d3e55dcdSgww 					cmn_err(CE_WARN, "strrput: could not "
2462d3e55dcdSgww 					    "allocate qband\n");
2463d3e55dcdSgww 				} else {
24647c478bd9Sstevel@tonic-gate 					bpri = sop->so_band;
2465d3e55dcdSgww 				}
24667c478bd9Sstevel@tonic-gate 			} else {
24677c478bd9Sstevel@tonic-gate 				q->q_lowat = sop->so_lowat;
24687c478bd9Sstevel@tonic-gate 			}
24697c478bd9Sstevel@tonic-gate 		}
24707c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MREADON)
24717c478bd9Sstevel@tonic-gate 			stp->sd_flag |= SNDMREAD;
24727c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MREADOFF)
24737c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~SNDMREAD;
24747c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_NDELON)
24757c478bd9Sstevel@tonic-gate 			stp->sd_flag |= OLDNDELAY;
24767c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_NDELOFF)
24777c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~OLDNDELAY;
24787c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_ISTTY)
24797c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRISTTY;
24807c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_ISNTTY)
24817c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRISTTY;
24827c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_TOSTOP)
24837c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRTOSTOP;
24847c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_TONSTOP)
24857c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRTOSTOP;
24867c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_DELIM)
24877c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRDELIM;
24887c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_NODELIM)
24897c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRDELIM;
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
24927c478bd9Sstevel@tonic-gate 		freemsg(bp);
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 		/* Check backenable in case the water marks changed */
24957c478bd9Sstevel@tonic-gate 		qbackenable(q, bpri);
24967c478bd9Sstevel@tonic-gate 		return (0);
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 	/*
24997c478bd9Sstevel@tonic-gate 	 * The following set of cases deal with situations where two stream
25007c478bd9Sstevel@tonic-gate 	 * heads are connected to each other (twisted streams).  These messages
25017c478bd9Sstevel@tonic-gate 	 * have no meaning at the stream head.
25027c478bd9Sstevel@tonic-gate 	 */
25037c478bd9Sstevel@tonic-gate 	case M_BREAK:
25047c478bd9Sstevel@tonic-gate 	case M_CTL:
25057c478bd9Sstevel@tonic-gate 	case M_DELAY:
25067c478bd9Sstevel@tonic-gate 	case M_START:
25077c478bd9Sstevel@tonic-gate 	case M_STOP:
25087c478bd9Sstevel@tonic-gate 	case M_IOCDATA:
25097c478bd9Sstevel@tonic-gate 	case M_STARTI:
25107c478bd9Sstevel@tonic-gate 	case M_STOPI:
25117c478bd9Sstevel@tonic-gate 		freemsg(bp);
25127c478bd9Sstevel@tonic-gate 		return (0);
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	case M_IOCTL:
25157c478bd9Sstevel@tonic-gate 		/*
25167c478bd9Sstevel@tonic-gate 		 * Always NAK this condition
25177c478bd9Sstevel@tonic-gate 		 * (makes no sense)
25187c478bd9Sstevel@tonic-gate 		 * If there is one or more threads in the read side
25197c478bd9Sstevel@tonic-gate 		 * rwnext we have to defer the nacking until that thread
25207c478bd9Sstevel@tonic-gate 		 * returns (in strget).
25217c478bd9Sstevel@tonic-gate 		 */
25227c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
25237c478bd9Sstevel@tonic-gate 		if (stp->sd_struiodnak != 0) {
25247c478bd9Sstevel@tonic-gate 			/*
25257c478bd9Sstevel@tonic-gate 			 * Defer NAK to the streamhead. Queue at the end
25267c478bd9Sstevel@tonic-gate 			 * the list.
25277c478bd9Sstevel@tonic-gate 			 */
25287c478bd9Sstevel@tonic-gate 			mblk_t *mp = stp->sd_struionak;
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 			while (mp && mp->b_next)
25317c478bd9Sstevel@tonic-gate 				mp = mp->b_next;
25327c478bd9Sstevel@tonic-gate 			if (mp)
25337c478bd9Sstevel@tonic-gate 				mp->b_next = bp;
25347c478bd9Sstevel@tonic-gate 			else
25357c478bd9Sstevel@tonic-gate 				stp->sd_struionak = bp;
25367c478bd9Sstevel@tonic-gate 			bp->b_next = NULL;
25377c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
25387c478bd9Sstevel@tonic-gate 			return (0);
25397c478bd9Sstevel@tonic-gate 		}
25407c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCNAK;
25437c478bd9Sstevel@tonic-gate 		/*
25447c478bd9Sstevel@tonic-gate 		 * Protect against the driver passing up
25457c478bd9Sstevel@tonic-gate 		 * messages after it has done a qprocsoff.
25467c478bd9Sstevel@tonic-gate 		 */
25477c478bd9Sstevel@tonic-gate 		if (_OTHERQ(q)->q_next == NULL)
25487c478bd9Sstevel@tonic-gate 			freemsg(bp);
25497c478bd9Sstevel@tonic-gate 		else
25507c478bd9Sstevel@tonic-gate 			qreply(q, bp);
25517c478bd9Sstevel@tonic-gate 		return (0);
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	default:
25547c478bd9Sstevel@tonic-gate #ifdef DEBUG
25557c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
25567c478bd9Sstevel@tonic-gate 		    "bad message type %x received at stream head\n",
25577c478bd9Sstevel@tonic-gate 		    bp->b_datap->db_type);
25587c478bd9Sstevel@tonic-gate #endif
25597c478bd9Sstevel@tonic-gate 		freemsg(bp);
25607c478bd9Sstevel@tonic-gate 		return (0);
25617c478bd9Sstevel@tonic-gate 	}
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
25647c478bd9Sstevel@tonic-gate }
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate /*
25677c478bd9Sstevel@tonic-gate  * Check if the stream pointed to by `stp' can be written to, and return an
25687c478bd9Sstevel@tonic-gate  * error code if not.  If `eiohup' is set, then return EIO if STRHUP is set.
25697c478bd9Sstevel@tonic-gate  * If `sigpipeok' is set and the SW_SIGPIPE option is enabled on the stream,
25707c478bd9Sstevel@tonic-gate  * then always return EPIPE and send a SIGPIPE to the invoking thread.
25717c478bd9Sstevel@tonic-gate  */
25727c478bd9Sstevel@tonic-gate static int
25737c478bd9Sstevel@tonic-gate strwriteable(struct stdata *stp, boolean_t eiohup, boolean_t sigpipeok)
25747c478bd9Sstevel@tonic-gate {
25757c478bd9Sstevel@tonic-gate 	int error;
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	/*
25807c478bd9Sstevel@tonic-gate 	 * For modem support, POSIX states that on writes, EIO should
25817c478bd9Sstevel@tonic-gate 	 * be returned if the stream has been hung up.
25827c478bd9Sstevel@tonic-gate 	 */
25837c478bd9Sstevel@tonic-gate 	if (eiohup && (stp->sd_flag & (STPLEX|STRHUP)) == STRHUP)
25847c478bd9Sstevel@tonic-gate 		error = EIO;
25857c478bd9Sstevel@tonic-gate 	else
25867c478bd9Sstevel@tonic-gate 		error = strgeterr(stp, STRHUP|STPLEX|STWRERR, 0);
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate 	if (error != 0) {
25897c478bd9Sstevel@tonic-gate 		if (!(stp->sd_flag & STPLEX) &&
25907c478bd9Sstevel@tonic-gate 		    (stp->sd_wput_opt & SW_SIGPIPE) && sigpipeok) {
25917c478bd9Sstevel@tonic-gate 			tsignal(curthread, SIGPIPE);
25927c478bd9Sstevel@tonic-gate 			error = EPIPE;
25937c478bd9Sstevel@tonic-gate 		}
25947c478bd9Sstevel@tonic-gate 	}
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 	return (error);
25977c478bd9Sstevel@tonic-gate }
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate /*
26007c478bd9Sstevel@tonic-gate  * Copyin and send data down a stream.
26017c478bd9Sstevel@tonic-gate  * The caller will allocate and copyin any control part that precedes the
26027c478bd9Sstevel@tonic-gate  * message and pass than in as mctl.
26037c478bd9Sstevel@tonic-gate  *
26047c478bd9Sstevel@tonic-gate  * Caller should *not* hold sd_lock.
26057c478bd9Sstevel@tonic-gate  * When EWOULDBLOCK is returned the caller has to redo the canputnext
26067c478bd9Sstevel@tonic-gate  * under sd_lock in order to avoid missing a backenabling wakeup.
26077c478bd9Sstevel@tonic-gate  *
26087c478bd9Sstevel@tonic-gate  * Use iosize = -1 to not send any M_DATA. iosize = 0 sends zero-length M_DATA.
26097c478bd9Sstevel@tonic-gate  *
26107c478bd9Sstevel@tonic-gate  * Set MSG_IGNFLOW in flags to ignore flow control for hipri messages.
26117c478bd9Sstevel@tonic-gate  * For sync streams we can only ignore flow control by reverting to using
26127c478bd9Sstevel@tonic-gate  * putnext.
26137c478bd9Sstevel@tonic-gate  *
26147c478bd9Sstevel@tonic-gate  * If sd_maxblk is less than *iosize this routine might return without
26157c478bd9Sstevel@tonic-gate  * transferring all of *iosize. In all cases, on return *iosize will contain
26167c478bd9Sstevel@tonic-gate  * the amount of data that was transferred.
26177c478bd9Sstevel@tonic-gate  */
26187c478bd9Sstevel@tonic-gate static int
26197c478bd9Sstevel@tonic-gate strput(struct stdata *stp, mblk_t *mctl, struct uio *uiop, ssize_t *iosize,
26207c478bd9Sstevel@tonic-gate     int b_flag, int pri, int flags)
26217c478bd9Sstevel@tonic-gate {
26227c478bd9Sstevel@tonic-gate 	struiod_t uiod;
26237c478bd9Sstevel@tonic-gate 	mblk_t *mp;
26247c478bd9Sstevel@tonic-gate 	queue_t *wqp = stp->sd_wrq;
26257c478bd9Sstevel@tonic-gate 	int error = 0;
26267c478bd9Sstevel@tonic-gate 	ssize_t count = *iosize;
26277c478bd9Sstevel@tonic-gate 	cred_t *cr;
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	if (uiop != NULL && count >= 0)
26327c478bd9Sstevel@tonic-gate 		flags |= stp->sd_struiowrq ? STRUIO_POSTPONE : 0;
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	if (!(flags & STRUIO_POSTPONE)) {
26357c478bd9Sstevel@tonic-gate 		/*
26367c478bd9Sstevel@tonic-gate 		 * Use regular canputnext, strmakedata, putnext sequence.
26377c478bd9Sstevel@tonic-gate 		 */
26387c478bd9Sstevel@tonic-gate 		if (pri == 0) {
26397c478bd9Sstevel@tonic-gate 			if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
26407c478bd9Sstevel@tonic-gate 				freemsg(mctl);
26417c478bd9Sstevel@tonic-gate 				return (EWOULDBLOCK);
26427c478bd9Sstevel@tonic-gate 			}
26437c478bd9Sstevel@tonic-gate 		} else {
26447c478bd9Sstevel@tonic-gate 			if (!(flags & MSG_IGNFLOW) && !bcanputnext(wqp, pri)) {
26457c478bd9Sstevel@tonic-gate 				freemsg(mctl);
26467c478bd9Sstevel@tonic-gate 				return (EWOULDBLOCK);
26477c478bd9Sstevel@tonic-gate 			}
26487c478bd9Sstevel@tonic-gate 		}
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 		if ((error = strmakedata(iosize, uiop, stp, flags,
26517c478bd9Sstevel@tonic-gate 		    &mp)) != 0) {
26527c478bd9Sstevel@tonic-gate 			freemsg(mctl);
26537c478bd9Sstevel@tonic-gate 			/*
26547c478bd9Sstevel@tonic-gate 			 * need to change return code to ENOMEM
26557c478bd9Sstevel@tonic-gate 			 * so that this is not confused with
26567c478bd9Sstevel@tonic-gate 			 * flow control, EAGAIN.
26577c478bd9Sstevel@tonic-gate 			 */
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 			if (error == EAGAIN)
26607c478bd9Sstevel@tonic-gate 				return (ENOMEM);
26617c478bd9Sstevel@tonic-gate 			else
26627c478bd9Sstevel@tonic-gate 				return (error);
26637c478bd9Sstevel@tonic-gate 		}
26647c478bd9Sstevel@tonic-gate 		if (mctl != NULL) {
26657c478bd9Sstevel@tonic-gate 			if (mctl->b_cont == NULL)
26667c478bd9Sstevel@tonic-gate 				mctl->b_cont = mp;
26677c478bd9Sstevel@tonic-gate 			else if (mp != NULL)
26687c478bd9Sstevel@tonic-gate 				linkb(mctl, mp);
26697c478bd9Sstevel@tonic-gate 			mp = mctl;
26707c478bd9Sstevel@tonic-gate 			/*
26717c478bd9Sstevel@tonic-gate 			 * Note that for interrupt thread, the CRED() is
26727c478bd9Sstevel@tonic-gate 			 * NULL. Don't bother with the pid either.
26737c478bd9Sstevel@tonic-gate 			 */
26747c478bd9Sstevel@tonic-gate 			if ((cr = CRED()) != NULL) {
26757c478bd9Sstevel@tonic-gate 				mblk_setcred(mp, cr);
26767c478bd9Sstevel@tonic-gate 				DB_CPID(mp) = curproc->p_pid;
26777c478bd9Sstevel@tonic-gate 			}
26787c478bd9Sstevel@tonic-gate 		} else if (mp == NULL)
26797c478bd9Sstevel@tonic-gate 			return (0);
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 		mp->b_flag |= b_flag;
26827c478bd9Sstevel@tonic-gate 		mp->b_band = (uchar_t)pri;
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 		if (flags & MSG_IGNFLOW) {
26857c478bd9Sstevel@tonic-gate 			/*
26867c478bd9Sstevel@tonic-gate 			 * XXX Hack: Don't get stuck running service
26877c478bd9Sstevel@tonic-gate 			 * procedures. This is needed for sockfs when
26887c478bd9Sstevel@tonic-gate 			 * sending the unbind message out of the rput
26897c478bd9Sstevel@tonic-gate 			 * procedure - we don't want a put procedure
26907c478bd9Sstevel@tonic-gate 			 * to run service procedures.
26917c478bd9Sstevel@tonic-gate 			 */
26927c478bd9Sstevel@tonic-gate 			putnext(wqp, mp);
26937c478bd9Sstevel@tonic-gate 		} else {
26947c478bd9Sstevel@tonic-gate 			stream_willservice(stp);
26957c478bd9Sstevel@tonic-gate 			putnext(wqp, mp);
26967c478bd9Sstevel@tonic-gate 			stream_runservice(stp);
26977c478bd9Sstevel@tonic-gate 		}
26987c478bd9Sstevel@tonic-gate 		return (0);
26997c478bd9Sstevel@tonic-gate 	}
27007c478bd9Sstevel@tonic-gate 	/*
27017c478bd9Sstevel@tonic-gate 	 * Stream supports rwnext() for the write side.
27027c478bd9Sstevel@tonic-gate 	 */
27037c478bd9Sstevel@tonic-gate 	if ((error = strmakedata(iosize, uiop, stp, flags, &mp)) != 0) {
27047c478bd9Sstevel@tonic-gate 		freemsg(mctl);
27057c478bd9Sstevel@tonic-gate 		/*
27067c478bd9Sstevel@tonic-gate 		 * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
27077c478bd9Sstevel@tonic-gate 		 */
27087c478bd9Sstevel@tonic-gate 		return (error == EAGAIN ? ENOMEM : error);
27097c478bd9Sstevel@tonic-gate 	}
27107c478bd9Sstevel@tonic-gate 	if (mctl != NULL) {
27117c478bd9Sstevel@tonic-gate 		if (mctl->b_cont == NULL)
27127c478bd9Sstevel@tonic-gate 			mctl->b_cont = mp;
27137c478bd9Sstevel@tonic-gate 		else if (mp != NULL)
27147c478bd9Sstevel@tonic-gate 			linkb(mctl, mp);
27157c478bd9Sstevel@tonic-gate 		mp = mctl;
27167c478bd9Sstevel@tonic-gate 		/*
27177c478bd9Sstevel@tonic-gate 		 * Note that for interrupt thread, the CRED() is
27187c478bd9Sstevel@tonic-gate 		 * NULL.  Don't bother with the pid either.
27197c478bd9Sstevel@tonic-gate 		 */
27207c478bd9Sstevel@tonic-gate 		if ((cr = CRED()) != NULL) {
27217c478bd9Sstevel@tonic-gate 			mblk_setcred(mp, cr);
27227c478bd9Sstevel@tonic-gate 			DB_CPID(mp) = curproc->p_pid;
27237c478bd9Sstevel@tonic-gate 		}
27247c478bd9Sstevel@tonic-gate 	} else if (mp == NULL) {
27257c478bd9Sstevel@tonic-gate 		return (0);
27267c478bd9Sstevel@tonic-gate 	}
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	mp->b_flag |= b_flag;
27297c478bd9Sstevel@tonic-gate 	mp->b_band = (uchar_t)pri;
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate 	(void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
27327c478bd9Sstevel@tonic-gate 	    sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
27337c478bd9Sstevel@tonic-gate 	uiod.d_uio.uio_offset = 0;
27347c478bd9Sstevel@tonic-gate 	uiod.d_mp = mp;
27357c478bd9Sstevel@tonic-gate 	error = rwnext(wqp, &uiod);
27367c478bd9Sstevel@tonic-gate 	if (! uiod.d_mp) {
27377c478bd9Sstevel@tonic-gate 		uioskip(uiop, *iosize);
27387c478bd9Sstevel@tonic-gate 		return (error);
27397c478bd9Sstevel@tonic-gate 	}
27407c478bd9Sstevel@tonic-gate 	ASSERT(mp == uiod.d_mp);
27417c478bd9Sstevel@tonic-gate 	if (error == EINVAL) {
27427c478bd9Sstevel@tonic-gate 		/*
27437c478bd9Sstevel@tonic-gate 		 * The stream plumbing must have changed while
27447c478bd9Sstevel@tonic-gate 		 * we were away, so just turn off rwnext()s.
27457c478bd9Sstevel@tonic-gate 		 */
27467c478bd9Sstevel@tonic-gate 		error = 0;
27477c478bd9Sstevel@tonic-gate 	} else if (error == EBUSY || error == EWOULDBLOCK) {
27487c478bd9Sstevel@tonic-gate 		/*
27497c478bd9Sstevel@tonic-gate 		 * Couldn't enter a perimeter or took a page fault,
27507c478bd9Sstevel@tonic-gate 		 * so fall-back to putnext().
27517c478bd9Sstevel@tonic-gate 		 */
27527c478bd9Sstevel@tonic-gate 		error = 0;
27537c478bd9Sstevel@tonic-gate 	} else {
27547c478bd9Sstevel@tonic-gate 		freemsg(mp);
27557c478bd9Sstevel@tonic-gate 		return (error);
27567c478bd9Sstevel@tonic-gate 	}
27577c478bd9Sstevel@tonic-gate 	/* Have to check canput before consuming data from the uio */
27587c478bd9Sstevel@tonic-gate 	if (pri == 0) {
27597c478bd9Sstevel@tonic-gate 		if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
27607c478bd9Sstevel@tonic-gate 			freemsg(mp);
27617c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
27627c478bd9Sstevel@tonic-gate 		}
27637c478bd9Sstevel@tonic-gate 	} else {
27647c478bd9Sstevel@tonic-gate 		if (!bcanputnext(wqp, pri) && !(flags & MSG_IGNFLOW)) {
27657c478bd9Sstevel@tonic-gate 			freemsg(mp);
27667c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
27677c478bd9Sstevel@tonic-gate 		}
27687c478bd9Sstevel@tonic-gate 	}
27697c478bd9Sstevel@tonic-gate 	ASSERT(mp == uiod.d_mp);
27707c478bd9Sstevel@tonic-gate 	/* Copyin data from the uio */
27717c478bd9Sstevel@tonic-gate 	if ((error = struioget(wqp, mp, &uiod, 0)) != 0) {
27727c478bd9Sstevel@tonic-gate 		freemsg(mp);
27737c478bd9Sstevel@tonic-gate 		return (error);
27747c478bd9Sstevel@tonic-gate 	}
27757c478bd9Sstevel@tonic-gate 	uioskip(uiop, *iosize);
27767c478bd9Sstevel@tonic-gate 	if (flags & MSG_IGNFLOW) {
27777c478bd9Sstevel@tonic-gate 		/*
27787c478bd9Sstevel@tonic-gate 		 * XXX Hack: Don't get stuck running service procedures.
27797c478bd9Sstevel@tonic-gate 		 * This is needed for sockfs when sending the unbind message
27807c478bd9Sstevel@tonic-gate 		 * out of the rput procedure - we don't want a put procedure
27817c478bd9Sstevel@tonic-gate 		 * to run service procedures.
27827c478bd9Sstevel@tonic-gate 		 */
27837c478bd9Sstevel@tonic-gate 		putnext(wqp, mp);
27847c478bd9Sstevel@tonic-gate 	} else {
27857c478bd9Sstevel@tonic-gate 		stream_willservice(stp);
27867c478bd9Sstevel@tonic-gate 		putnext(wqp, mp);
27877c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
27887c478bd9Sstevel@tonic-gate 	}
27897c478bd9Sstevel@tonic-gate 	return (0);
27907c478bd9Sstevel@tonic-gate }
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate /*
27937c478bd9Sstevel@tonic-gate  * Write attempts to break the write request into messages conforming
27947c478bd9Sstevel@tonic-gate  * with the minimum and maximum packet sizes set downstream.
27957c478bd9Sstevel@tonic-gate  *
27967c478bd9Sstevel@tonic-gate  * Write will not block if downstream queue is full and
27977c478bd9Sstevel@tonic-gate  * O_NDELAY is set, otherwise it will block waiting for the queue to get room.
27987c478bd9Sstevel@tonic-gate  *
27997c478bd9Sstevel@tonic-gate  * A write of zero bytes gets packaged into a zero length message and sent
28007c478bd9Sstevel@tonic-gate  * downstream like any other message.
28017c478bd9Sstevel@tonic-gate  *
28027c478bd9Sstevel@tonic-gate  * If buffers of the requested sizes are not available, the write will
28037c478bd9Sstevel@tonic-gate  * sleep until the buffers become available.
28047c478bd9Sstevel@tonic-gate  *
28057c478bd9Sstevel@tonic-gate  * Write (if specified) will supply a write offset in a message if it
28067c478bd9Sstevel@tonic-gate  * makes sense. This can be specified by downstream modules as part of
28077c478bd9Sstevel@tonic-gate  * a M_SETOPTS message.  Write will not supply the write offset if it
28087c478bd9Sstevel@tonic-gate  * cannot supply any data in a buffer.  In other words, write will never
28097c478bd9Sstevel@tonic-gate  * send down an empty packet due to a write offset.
28107c478bd9Sstevel@tonic-gate  */
28117c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
28127c478bd9Sstevel@tonic-gate int
28137c478bd9Sstevel@tonic-gate strwrite(struct vnode *vp, struct uio *uiop, cred_t *crp)
28147c478bd9Sstevel@tonic-gate {
2815ff550d0eSmasputra 	return (strwrite_common(vp, uiop, crp, 0));
2816ff550d0eSmasputra }
2817ff550d0eSmasputra 
2818ff550d0eSmasputra /* ARGSUSED2 */
2819ff550d0eSmasputra int
2820ff550d0eSmasputra strwrite_common(struct vnode *vp, struct uio *uiop, cred_t *crp, int wflag)
2821ff550d0eSmasputra {
28227c478bd9Sstevel@tonic-gate 	struct stdata *stp;
28237c478bd9Sstevel@tonic-gate 	struct queue *wqp;
28247c478bd9Sstevel@tonic-gate 	ssize_t rmin, rmax;
28257c478bd9Sstevel@tonic-gate 	ssize_t iosize;
2826ff550d0eSmasputra 	int waitflag;
28277c478bd9Sstevel@tonic-gate 	int tempmode;
28287c478bd9Sstevel@tonic-gate 	int error = 0;
28297c478bd9Sstevel@tonic-gate 	int b_flag;
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
28327c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
28359acbbeafSnn35248 
28369acbbeafSnn35248 	if ((error = i_straccess(stp, JCWRITE)) != 0) {
28377c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
28387c478bd9Sstevel@tonic-gate 		return (error);
28397c478bd9Sstevel@tonic-gate 	}
28407c478bd9Sstevel@tonic-gate 
28419acbbeafSnn35248 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
28429acbbeafSnn35248 		error = strwriteable(stp, B_TRUE, B_TRUE);
28439acbbeafSnn35248 		if (error != 0) {
28449acbbeafSnn35248 			mutex_exit(&stp->sd_lock);
28459acbbeafSnn35248 			return (error);
28469acbbeafSnn35248 		}
28479acbbeafSnn35248 	}
28489acbbeafSnn35248 
28499acbbeafSnn35248 	mutex_exit(&stp->sd_lock);
28509acbbeafSnn35248 
28517c478bd9Sstevel@tonic-gate 	wqp = stp->sd_wrq;
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 	/* get these values from them cached in the stream head */
28547c478bd9Sstevel@tonic-gate 	rmin = stp->sd_qn_minpsz;
28557c478bd9Sstevel@tonic-gate 	rmax = stp->sd_qn_maxpsz;
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	/*
28587c478bd9Sstevel@tonic-gate 	 * Check the min/max packet size constraints.  If min packet size
28597c478bd9Sstevel@tonic-gate 	 * is non-zero, the write cannot be split into multiple messages
28607c478bd9Sstevel@tonic-gate 	 * and still guarantee the size constraints.
28617c478bd9Sstevel@tonic-gate 	 */
28627c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_IN, "strwrite in:q %p", wqp);
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	ASSERT((rmax >= 0) || (rmax == INFPSZ));
28657c478bd9Sstevel@tonic-gate 	if (rmax == 0) {
28667c478bd9Sstevel@tonic-gate 		return (0);
28677c478bd9Sstevel@tonic-gate 	}
28687c478bd9Sstevel@tonic-gate 	if (rmin > 0) {
28697c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid < rmin) {
28707c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
28717c478bd9Sstevel@tonic-gate 			    "strwrite out:q %p out %d error %d",
28727c478bd9Sstevel@tonic-gate 			    wqp, 0, ERANGE);
28737c478bd9Sstevel@tonic-gate 			return (ERANGE);
28747c478bd9Sstevel@tonic-gate 		}
28757c478bd9Sstevel@tonic-gate 		if ((rmax != INFPSZ) && (uiop->uio_resid > rmax)) {
28767c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
28777c478bd9Sstevel@tonic-gate 			    "strwrite out:q %p out %d error %d",
28787c478bd9Sstevel@tonic-gate 			    wqp, 1, ERANGE);
28797c478bd9Sstevel@tonic-gate 			return (ERANGE);
28807c478bd9Sstevel@tonic-gate 		}
28817c478bd9Sstevel@tonic-gate 	}
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 	/*
28847c478bd9Sstevel@tonic-gate 	 * Do until count satisfied or error.
28857c478bd9Sstevel@tonic-gate 	 */
2886ff550d0eSmasputra 	waitflag = WRITEWAIT | wflag;
28877c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & OLDNDELAY)
28887c478bd9Sstevel@tonic-gate 		tempmode = uiop->uio_fmode & ~FNDELAY;
28897c478bd9Sstevel@tonic-gate 	else
28907c478bd9Sstevel@tonic-gate 		tempmode = uiop->uio_fmode;
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 	if (rmax == INFPSZ)
28937c478bd9Sstevel@tonic-gate 		rmax = uiop->uio_resid;
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	/*
28967c478bd9Sstevel@tonic-gate 	 * Note that tempmode does not get used in strput/strmakedata
28977c478bd9Sstevel@tonic-gate 	 * but only in strwaitq. The other routines use uio_fmode
28987c478bd9Sstevel@tonic-gate 	 * unmodified.
28997c478bd9Sstevel@tonic-gate 	 */
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 	/* LINTED: constant in conditional context */
29027c478bd9Sstevel@tonic-gate 	while (1) {	/* breaks when uio_resid reaches zero */
29037c478bd9Sstevel@tonic-gate 		/*
29047c478bd9Sstevel@tonic-gate 		 * Determine the size of the next message to be
29057c478bd9Sstevel@tonic-gate 		 * packaged.  May have to break write into several
29067c478bd9Sstevel@tonic-gate 		 * messages based on max packet size.
29077c478bd9Sstevel@tonic-gate 		 */
29087c478bd9Sstevel@tonic-gate 		iosize = MIN(uiop->uio_resid, rmax);
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 		/*
29117c478bd9Sstevel@tonic-gate 		 * Put block downstream when flow control allows it.
29127c478bd9Sstevel@tonic-gate 		 */
29137c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STRDELIM) && (uiop->uio_resid == iosize))
29147c478bd9Sstevel@tonic-gate 			b_flag = MSGDELIM;
29157c478bd9Sstevel@tonic-gate 		else
29167c478bd9Sstevel@tonic-gate 			b_flag = 0;
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 		for (;;) {
29197c478bd9Sstevel@tonic-gate 			int done = 0;
29207c478bd9Sstevel@tonic-gate 
2921d3e55dcdSgww 			error = strput(stp, NULL, uiop, &iosize, b_flag, 0, 0);
29227c478bd9Sstevel@tonic-gate 			if (error == 0)
29237c478bd9Sstevel@tonic-gate 				break;
29247c478bd9Sstevel@tonic-gate 			if (error != EWOULDBLOCK)
29257c478bd9Sstevel@tonic-gate 				goto out;
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
29287c478bd9Sstevel@tonic-gate 			/*
29297c478bd9Sstevel@tonic-gate 			 * Check for a missed wakeup.
29307c478bd9Sstevel@tonic-gate 			 * Needed since strput did not hold sd_lock across
29317c478bd9Sstevel@tonic-gate 			 * the canputnext.
29327c478bd9Sstevel@tonic-gate 			 */
29337c478bd9Sstevel@tonic-gate 			if (canputnext(wqp)) {
29347c478bd9Sstevel@tonic-gate 				/* Try again */
29357c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
29367c478bd9Sstevel@tonic-gate 				continue;
29377c478bd9Sstevel@tonic-gate 			}
29387c478bd9Sstevel@tonic-gate 			TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAIT,
29397c478bd9Sstevel@tonic-gate 			    "strwrite wait:q %p wait", wqp);
29407c478bd9Sstevel@tonic-gate 			if ((error = strwaitq(stp, waitflag, (ssize_t)0,
29417c478bd9Sstevel@tonic-gate 			    tempmode, -1, &done)) != 0 || done) {
29427c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
29437c478bd9Sstevel@tonic-gate 				if ((vp->v_type == VFIFO) &&
29447c478bd9Sstevel@tonic-gate 				    (uiop->uio_fmode & FNDELAY) &&
29457c478bd9Sstevel@tonic-gate 				    (error == EAGAIN))
29467c478bd9Sstevel@tonic-gate 					error = 0;
29477c478bd9Sstevel@tonic-gate 				goto out;
29487c478bd9Sstevel@tonic-gate 			}
29497c478bd9Sstevel@tonic-gate 			TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAKE,
29507c478bd9Sstevel@tonic-gate 			    "strwrite wake:q %p awakes", wqp);
29519acbbeafSnn35248 			if ((error = i_straccess(stp, JCWRITE)) != 0) {
29527c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
29537c478bd9Sstevel@tonic-gate 				goto out;
29547c478bd9Sstevel@tonic-gate 			}
29559acbbeafSnn35248 			mutex_exit(&stp->sd_lock);
29569acbbeafSnn35248 		}
29577c478bd9Sstevel@tonic-gate 		waitflag |= NOINTR;
29587c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRWRITE_RESID,
29597c478bd9Sstevel@tonic-gate 		    "strwrite resid:q %p uiop %p", wqp, uiop);
29607c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid) {
29617c478bd9Sstevel@tonic-gate 			/* Recheck for errors - needed for sockets */
29627c478bd9Sstevel@tonic-gate 			if ((stp->sd_wput_opt & SW_RECHECK_ERR) &&
29637c478bd9Sstevel@tonic-gate 			    (stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
29647c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
29657c478bd9Sstevel@tonic-gate 				error = strwriteable(stp, B_FALSE, B_TRUE);
29667c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
29677c478bd9Sstevel@tonic-gate 				if (error != 0)
29687c478bd9Sstevel@tonic-gate 					return (error);
29697c478bd9Sstevel@tonic-gate 			}
29707c478bd9Sstevel@tonic-gate 			continue;
29717c478bd9Sstevel@tonic-gate 		}
29727c478bd9Sstevel@tonic-gate 		break;
29737c478bd9Sstevel@tonic-gate 	}
29747c478bd9Sstevel@tonic-gate out:
29757c478bd9Sstevel@tonic-gate 	/*
29767c478bd9Sstevel@tonic-gate 	 * For historical reasons, applications expect EAGAIN when a data
29777c478bd9Sstevel@tonic-gate 	 * mblk_t cannot be allocated, so change ENOMEM back to EAGAIN.
29787c478bd9Sstevel@tonic-gate 	 */
29797c478bd9Sstevel@tonic-gate 	if (error == ENOMEM)
29807c478bd9Sstevel@tonic-gate 		error = EAGAIN;
29817c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
29827c478bd9Sstevel@tonic-gate 	    "strwrite out:q %p out %d error %d", wqp, 2, error);
29837c478bd9Sstevel@tonic-gate 	return (error);
29847c478bd9Sstevel@tonic-gate }
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate /*
29877c478bd9Sstevel@tonic-gate  * Stream head write service routine.
29887c478bd9Sstevel@tonic-gate  * Its job is to wake up any sleeping writers when a queue
29897c478bd9Sstevel@tonic-gate  * downstream needs data (part of the flow control in putq and getq).
29907c478bd9Sstevel@tonic-gate  * It also must wake anyone sleeping on a poll().
29917c478bd9Sstevel@tonic-gate  * For stream head right below mux module, it must also invoke put procedure
29927c478bd9Sstevel@tonic-gate  * of next downstream module.
29937c478bd9Sstevel@tonic-gate  */
29947c478bd9Sstevel@tonic-gate int
29957c478bd9Sstevel@tonic-gate strwsrv(queue_t *q)
29967c478bd9Sstevel@tonic-gate {
29977c478bd9Sstevel@tonic-gate 	struct stdata *stp;
29987c478bd9Sstevel@tonic-gate 	queue_t *tq;
29997c478bd9Sstevel@tonic-gate 	qband_t *qbp;
30007c478bd9Sstevel@tonic-gate 	int i;
30017c478bd9Sstevel@tonic-gate 	qband_t *myqbp;
30027c478bd9Sstevel@tonic-gate 	int isevent;
30037c478bd9Sstevel@tonic-gate 	unsigned char	qbf[NBAND];	/* band flushing backenable flags */
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
30067c478bd9Sstevel@tonic-gate 	    TR_STRWSRV, "strwsrv:q %p", q);
30077c478bd9Sstevel@tonic-gate 	stp = (struct stdata *)q->q_ptr;
30087c478bd9Sstevel@tonic-gate 	ASSERT(qclaimed(q));
30097c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
30107c478bd9Sstevel@tonic-gate 	ASSERT(!(stp->sd_flag & STPLEX));
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & WSLEEP) {
30137c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~WSLEEP;
30147c478bd9Sstevel@tonic-gate 		cv_broadcast(&q->q_wait);
30157c478bd9Sstevel@tonic-gate 	}
30167c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 	/* The other end of a stream pipe went away. */
30197c478bd9Sstevel@tonic-gate 	if ((tq = q->q_next) == NULL) {
30207c478bd9Sstevel@tonic-gate 		return (0);
30217c478bd9Sstevel@tonic-gate 	}
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 	/* Find the next module forward that has a service procedure */
30247c478bd9Sstevel@tonic-gate 	claimstr(q);
30257c478bd9Sstevel@tonic-gate 	tq = q->q_nfsrv;
30267c478bd9Sstevel@tonic-gate 	ASSERT(tq != NULL);
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 	if ((q->q_flag & QBACK)) {
30297c478bd9Sstevel@tonic-gate 		if ((tq->q_flag & QFULL)) {
30307c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(tq));
30317c478bd9Sstevel@tonic-gate 			if (!(tq->q_flag & QFULL)) {
30327c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(tq));
30337c478bd9Sstevel@tonic-gate 				goto wakeup;
30347c478bd9Sstevel@tonic-gate 			}
30357c478bd9Sstevel@tonic-gate 			/*
30367c478bd9Sstevel@tonic-gate 			 * The queue must have become full again. Set QWANTW
30377c478bd9Sstevel@tonic-gate 			 * again so strwsrv will be back enabled when
30387c478bd9Sstevel@tonic-gate 			 * the queue becomes non-full next time.
30397c478bd9Sstevel@tonic-gate 			 */
30407c478bd9Sstevel@tonic-gate 			tq->q_flag |= QWANTW;
30417c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(tq));
30427c478bd9Sstevel@tonic-gate 		} else {
30437c478bd9Sstevel@tonic-gate 		wakeup:
30447c478bd9Sstevel@tonic-gate 			pollwakeup(&stp->sd_pollist, POLLWRNORM);
30457c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
30467c478bd9Sstevel@tonic-gate 			if (stp->sd_sigflags & S_WRNORM)
30477c478bd9Sstevel@tonic-gate 				strsendsig(stp->sd_siglist, S_WRNORM, 0, 0);
30487c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
30497c478bd9Sstevel@tonic-gate 		}
30507c478bd9Sstevel@tonic-gate 	}
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	isevent = 0;
30537c478bd9Sstevel@tonic-gate 	i = 1;
30547c478bd9Sstevel@tonic-gate 	bzero((caddr_t)qbf, NBAND);
30557c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(tq));
30567c478bd9Sstevel@tonic-gate 	if ((myqbp = q->q_bandp) != NULL)
30577c478bd9Sstevel@tonic-gate 		for (qbp = tq->q_bandp; qbp && myqbp; qbp = qbp->qb_next) {
30587c478bd9Sstevel@tonic-gate 			ASSERT(myqbp);
30597c478bd9Sstevel@tonic-gate 			if ((myqbp->qb_flag & QB_BACK)) {
30607c478bd9Sstevel@tonic-gate 				if (qbp->qb_flag & QB_FULL) {
30617c478bd9Sstevel@tonic-gate 					/*
30627c478bd9Sstevel@tonic-gate 					 * The band must have become full again.
30637c478bd9Sstevel@tonic-gate 					 * Set QB_WANTW again so strwsrv will
30647c478bd9Sstevel@tonic-gate 					 * be back enabled when the band becomes
30657c478bd9Sstevel@tonic-gate 					 * non-full next time.
30667c478bd9Sstevel@tonic-gate 					 */
30677c478bd9Sstevel@tonic-gate 					qbp->qb_flag |= QB_WANTW;
30687c478bd9Sstevel@tonic-gate 				} else {
30697c478bd9Sstevel@tonic-gate 					isevent = 1;
30707c478bd9Sstevel@tonic-gate 					qbf[i] = 1;
30717c478bd9Sstevel@tonic-gate 				}
30727c478bd9Sstevel@tonic-gate 			}
30737c478bd9Sstevel@tonic-gate 			myqbp = myqbp->qb_next;
30747c478bd9Sstevel@tonic-gate 			i++;
30757c478bd9Sstevel@tonic-gate 		}
30767c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(tq));
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate 	if (isevent) {
30797c478bd9Sstevel@tonic-gate 		for (i = tq->q_nband; i; i--) {
30807c478bd9Sstevel@tonic-gate 			if (qbf[i]) {
30817c478bd9Sstevel@tonic-gate 				pollwakeup(&stp->sd_pollist, POLLWRBAND);
30827c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
30837c478bd9Sstevel@tonic-gate 				if (stp->sd_sigflags & S_WRBAND)
30847c478bd9Sstevel@tonic-gate 					strsendsig(stp->sd_siglist, S_WRBAND,
30857c478bd9Sstevel@tonic-gate 					    (uchar_t)i, 0);
30867c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
30877c478bd9Sstevel@tonic-gate 			}
30887c478bd9Sstevel@tonic-gate 		}
30897c478bd9Sstevel@tonic-gate 	}
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 	releasestr(q);
30927c478bd9Sstevel@tonic-gate 	return (0);
30937c478bd9Sstevel@tonic-gate }
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate /*
30967c478bd9Sstevel@tonic-gate  * Special case of strcopyin/strcopyout for copying
30977c478bd9Sstevel@tonic-gate  * struct strioctl that can deal with both data
30987c478bd9Sstevel@tonic-gate  * models.
30997c478bd9Sstevel@tonic-gate  */
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate #ifdef	_LP64
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate static int
31047c478bd9Sstevel@tonic-gate strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
31057c478bd9Sstevel@tonic-gate {
31067c478bd9Sstevel@tonic-gate 	struct	strioctl32 strioc32;
31077c478bd9Sstevel@tonic-gate 	struct	strioctl *striocp;
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate 	if (copyflag & U_TO_K) {
31107c478bd9Sstevel@tonic-gate 		ASSERT((copyflag & K_TO_K) == 0);
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate 		if ((flag & FMODELS) == DATAMODEL_ILP32) {
31137c478bd9Sstevel@tonic-gate 			if (copyin(from, &strioc32, sizeof (strioc32)))
31147c478bd9Sstevel@tonic-gate 				return (EFAULT);
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 			striocp = (struct strioctl *)to;
31177c478bd9Sstevel@tonic-gate 			striocp->ic_cmd	= strioc32.ic_cmd;
31187c478bd9Sstevel@tonic-gate 			striocp->ic_timout = strioc32.ic_timout;
31197c478bd9Sstevel@tonic-gate 			striocp->ic_len	= strioc32.ic_len;
31207c478bd9Sstevel@tonic-gate 			striocp->ic_dp	= (char *)(uintptr_t)strioc32.ic_dp;
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 		} else { /* NATIVE data model */
31237c478bd9Sstevel@tonic-gate 			if (copyin(from, to, sizeof (struct strioctl))) {
31247c478bd9Sstevel@tonic-gate 				return (EFAULT);
31257c478bd9Sstevel@tonic-gate 			} else {
31267c478bd9Sstevel@tonic-gate 				return (0);
31277c478bd9Sstevel@tonic-gate 			}
31287c478bd9Sstevel@tonic-gate 		}
31297c478bd9Sstevel@tonic-gate 	} else {
31307c478bd9Sstevel@tonic-gate 		ASSERT(copyflag & K_TO_K);
31317c478bd9Sstevel@tonic-gate 		bcopy(from, to, sizeof (struct strioctl));
31327c478bd9Sstevel@tonic-gate 	}
31337c478bd9Sstevel@tonic-gate 	return (0);
31347c478bd9Sstevel@tonic-gate }
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate static int
31377c478bd9Sstevel@tonic-gate strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
31387c478bd9Sstevel@tonic-gate {
31397c478bd9Sstevel@tonic-gate 	struct	strioctl32 strioc32;
31407c478bd9Sstevel@tonic-gate 	struct	strioctl *striocp;
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate 	if (copyflag & U_TO_K) {
31437c478bd9Sstevel@tonic-gate 		ASSERT((copyflag & K_TO_K) == 0);
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 		if ((flag & FMODELS) == DATAMODEL_ILP32) {
31467c478bd9Sstevel@tonic-gate 			striocp = (struct strioctl *)from;
31477c478bd9Sstevel@tonic-gate 			strioc32.ic_cmd	= striocp->ic_cmd;
31487c478bd9Sstevel@tonic-gate 			strioc32.ic_timout = striocp->ic_timout;
31497c478bd9Sstevel@tonic-gate 			strioc32.ic_len	= striocp->ic_len;
31507c478bd9Sstevel@tonic-gate 			strioc32.ic_dp	= (caddr32_t)(uintptr_t)striocp->ic_dp;
31517c478bd9Sstevel@tonic-gate 			ASSERT((char *)(uintptr_t)strioc32.ic_dp ==
31527c478bd9Sstevel@tonic-gate 			    striocp->ic_dp);
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 			if (copyout(&strioc32, to, sizeof (strioc32)))
31557c478bd9Sstevel@tonic-gate 				return (EFAULT);
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 		} else { /* NATIVE data model */
31587c478bd9Sstevel@tonic-gate 			if (copyout(from, to, sizeof (struct strioctl))) {
31597c478bd9Sstevel@tonic-gate 				return (EFAULT);
31607c478bd9Sstevel@tonic-gate 			} else {
31617c478bd9Sstevel@tonic-gate 				return (0);
31627c478bd9Sstevel@tonic-gate 			}
31637c478bd9Sstevel@tonic-gate 		}
31647c478bd9Sstevel@tonic-gate 	} else {
31657c478bd9Sstevel@tonic-gate 		ASSERT(copyflag & K_TO_K);
31667c478bd9Sstevel@tonic-gate 		bcopy(from, to, sizeof (struct strioctl));
31677c478bd9Sstevel@tonic-gate 	}
31687c478bd9Sstevel@tonic-gate 	return (0);
31697c478bd9Sstevel@tonic-gate }
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate #else	/* ! _LP64 */
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
31747c478bd9Sstevel@tonic-gate static int
31757c478bd9Sstevel@tonic-gate strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
31767c478bd9Sstevel@tonic-gate {
31777c478bd9Sstevel@tonic-gate 	return (strcopyin(from, to, sizeof (struct strioctl), copyflag));
31787c478bd9Sstevel@tonic-gate }
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
31817c478bd9Sstevel@tonic-gate static int
31827c478bd9Sstevel@tonic-gate strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
31837c478bd9Sstevel@tonic-gate {
31847c478bd9Sstevel@tonic-gate 	return (strcopyout(from, to, sizeof (struct strioctl), copyflag));
31857c478bd9Sstevel@tonic-gate }
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate /*
31907c478bd9Sstevel@tonic-gate  * Determine type of job control semantics expected by user.  The
31917c478bd9Sstevel@tonic-gate  * possibilities are:
31927c478bd9Sstevel@tonic-gate  *	JCREAD	- Behaves like read() on fd; send SIGTTIN
31937c478bd9Sstevel@tonic-gate  *	JCWRITE	- Behaves like write() on fd; send SIGTTOU if TOSTOP set
31947c478bd9Sstevel@tonic-gate  *	JCSETP	- Sets a value in the stream; send SIGTTOU, ignore TOSTOP
31957c478bd9Sstevel@tonic-gate  *	JCGETP	- Gets a value in the stream; no signals.
31967c478bd9Sstevel@tonic-gate  * See straccess in strsubr.c for usage of these values.
31977c478bd9Sstevel@tonic-gate  *
31987c478bd9Sstevel@tonic-gate  * This routine also returns -1 for I_STR as a special case; the
31997c478bd9Sstevel@tonic-gate  * caller must call again with the real ioctl number for
32007c478bd9Sstevel@tonic-gate  * classification.
32017c478bd9Sstevel@tonic-gate  */
32027c478bd9Sstevel@tonic-gate static int
32037c478bd9Sstevel@tonic-gate job_control_type(int cmd)
32047c478bd9Sstevel@tonic-gate {
32057c478bd9Sstevel@tonic-gate 	switch (cmd) {
32067c478bd9Sstevel@tonic-gate 	case I_STR:
32077c478bd9Sstevel@tonic-gate 		return (-1);
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate 	case I_RECVFD:
32107c478bd9Sstevel@tonic-gate 	case I_E_RECVFD:
32117c478bd9Sstevel@tonic-gate 		return (JCREAD);
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	case I_FDINSERT:
32147c478bd9Sstevel@tonic-gate 	case I_SENDFD:
32157c478bd9Sstevel@tonic-gate 		return (JCWRITE);
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 	case TCSETA:
32187c478bd9Sstevel@tonic-gate 	case TCSETAW:
32197c478bd9Sstevel@tonic-gate 	case TCSETAF:
32207c478bd9Sstevel@tonic-gate 	case TCSBRK:
32217c478bd9Sstevel@tonic-gate 	case TCXONC:
32227c478bd9Sstevel@tonic-gate 	case TCFLSH:
32237c478bd9Sstevel@tonic-gate 	case TCDSET:	/* Obsolete */
32247c478bd9Sstevel@tonic-gate 	case TIOCSWINSZ:
32257c478bd9Sstevel@tonic-gate 	case TCSETS:
32267c478bd9Sstevel@tonic-gate 	case TCSETSW:
32277c478bd9Sstevel@tonic-gate 	case TCSETSF:
32287c478bd9Sstevel@tonic-gate 	case TIOCSETD:
32297c478bd9Sstevel@tonic-gate 	case TIOCHPCL:
32307c478bd9Sstevel@tonic-gate 	case TIOCSETP:
32317c478bd9Sstevel@tonic-gate 	case TIOCSETN:
32327c478bd9Sstevel@tonic-gate 	case TIOCEXCL:
32337c478bd9Sstevel@tonic-gate 	case TIOCNXCL:
32347c478bd9Sstevel@tonic-gate 	case TIOCFLUSH:
32357c478bd9Sstevel@tonic-gate 	case TIOCSETC:
32367c478bd9Sstevel@tonic-gate 	case TIOCLBIS:
32377c478bd9Sstevel@tonic-gate 	case TIOCLBIC:
32387c478bd9Sstevel@tonic-gate 	case TIOCLSET:
32397c478bd9Sstevel@tonic-gate 	case TIOCSBRK:
32407c478bd9Sstevel@tonic-gate 	case TIOCCBRK:
32417c478bd9Sstevel@tonic-gate 	case TIOCSDTR:
32427c478bd9Sstevel@tonic-gate 	case TIOCCDTR:
32437c478bd9Sstevel@tonic-gate 	case TIOCSLTC:
32447c478bd9Sstevel@tonic-gate 	case TIOCSTOP:
32457c478bd9Sstevel@tonic-gate 	case TIOCSTART:
32467c478bd9Sstevel@tonic-gate 	case TIOCSTI:
32477c478bd9Sstevel@tonic-gate 	case TIOCSPGRP:
32487c478bd9Sstevel@tonic-gate 	case TIOCMSET:
32497c478bd9Sstevel@tonic-gate 	case TIOCMBIS:
32507c478bd9Sstevel@tonic-gate 	case TIOCMBIC:
32517c478bd9Sstevel@tonic-gate 	case TIOCREMOTE:
32527c478bd9Sstevel@tonic-gate 	case TIOCSIGNAL:
32537c478bd9Sstevel@tonic-gate 	case LDSETT:
32547c478bd9Sstevel@tonic-gate 	case LDSMAP:	/* Obsolete */
32557c478bd9Sstevel@tonic-gate 	case DIOCSETP:
32567c478bd9Sstevel@tonic-gate 	case I_FLUSH:
32577c478bd9Sstevel@tonic-gate 	case I_SRDOPT:
32587c478bd9Sstevel@tonic-gate 	case I_SETSIG:
32597c478bd9Sstevel@tonic-gate 	case I_SWROPT:
32607c478bd9Sstevel@tonic-gate 	case I_FLUSHBAND:
32617c478bd9Sstevel@tonic-gate 	case I_SETCLTIME:
32627c478bd9Sstevel@tonic-gate 	case I_SERROPT:
32637c478bd9Sstevel@tonic-gate 	case I_ESETSIG:
32647c478bd9Sstevel@tonic-gate 	case FIONBIO:
32657c478bd9Sstevel@tonic-gate 	case FIOASYNC:
32667c478bd9Sstevel@tonic-gate 	case FIOSETOWN:
32677c478bd9Sstevel@tonic-gate 	case JBOOT:	/* Obsolete */
32687c478bd9Sstevel@tonic-gate 	case JTERM:	/* Obsolete */
32697c478bd9Sstevel@tonic-gate 	case JTIMOM:	/* Obsolete */
32707c478bd9Sstevel@tonic-gate 	case JZOMBOOT:	/* Obsolete */
32717c478bd9Sstevel@tonic-gate 	case JAGENT:	/* Obsolete */
32727c478bd9Sstevel@tonic-gate 	case JTRUN:	/* Obsolete */
32737c478bd9Sstevel@tonic-gate 	case JXTPROTO:	/* Obsolete */
32749acbbeafSnn35248 	case TIOCSETLD:
32757c478bd9Sstevel@tonic-gate 		return (JCSETP);
32767c478bd9Sstevel@tonic-gate 	}
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate 	return (JCGETP);
32797c478bd9Sstevel@tonic-gate }
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate /*
32827c478bd9Sstevel@tonic-gate  * ioctl for streams
32837c478bd9Sstevel@tonic-gate  */
32847c478bd9Sstevel@tonic-gate int
32857c478bd9Sstevel@tonic-gate strioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, int copyflag,
32867c478bd9Sstevel@tonic-gate     cred_t *crp, int *rvalp)
32877c478bd9Sstevel@tonic-gate {
32887c478bd9Sstevel@tonic-gate 	struct stdata *stp;
3289ca9327a6Smeem 	struct strcmd *scp;
32907c478bd9Sstevel@tonic-gate 	struct strioctl strioc;
32917c478bd9Sstevel@tonic-gate 	struct uio uio;
32927c478bd9Sstevel@tonic-gate 	struct iovec iov;
32937c478bd9Sstevel@tonic-gate 	int access;
32947c478bd9Sstevel@tonic-gate 	mblk_t *mp;
32957c478bd9Sstevel@tonic-gate 	int error = 0;
32967c478bd9Sstevel@tonic-gate 	int done = 0;
32977c478bd9Sstevel@tonic-gate 	ssize_t	rmin, rmax;
32987c478bd9Sstevel@tonic-gate 	queue_t *wrq;
32997c478bd9Sstevel@tonic-gate 	queue_t *rdq;
33007c478bd9Sstevel@tonic-gate 	boolean_t kioctl = B_FALSE;
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 	if (flag & FKIOCTL) {
33037c478bd9Sstevel@tonic-gate 		copyflag = K_TO_K;
33047c478bd9Sstevel@tonic-gate 		kioctl = B_TRUE;
33057c478bd9Sstevel@tonic-gate 	}
33067c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
33077c478bd9Sstevel@tonic-gate 	ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
33087c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_IOCTL_ENTER,
33117c478bd9Sstevel@tonic-gate 	    "strioctl:stp %p cmd %X arg %lX", stp, cmd, arg);
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	if (audit_active)
33147c478bd9Sstevel@tonic-gate 		audit_strioctl(vp, cmd, arg, flag, copyflag, crp, rvalp);
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 	/*
33177c478bd9Sstevel@tonic-gate 	 * If the copy is kernel to kernel, make sure that the FNATIVE
33187c478bd9Sstevel@tonic-gate 	 * flag is set.  After this it would be a serious error to have
33197c478bd9Sstevel@tonic-gate 	 * no model flag.
33207c478bd9Sstevel@tonic-gate 	 */
33217c478bd9Sstevel@tonic-gate 	if (copyflag == K_TO_K)
33227c478bd9Sstevel@tonic-gate 		flag = (flag & ~FMODELS) | FNATIVE;
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 	ASSERT((flag & FMODELS) != 0);
33257c478bd9Sstevel@tonic-gate 
33267c478bd9Sstevel@tonic-gate 	wrq = stp->sd_wrq;
33277c478bd9Sstevel@tonic-gate 	rdq = _RD(wrq);
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate 	access = job_control_type(cmd);
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	/* We should never see these here, should be handled by iwscn */
33327c478bd9Sstevel@tonic-gate 	if (cmd == SRIOCSREDIR || cmd == SRIOCISREDIR)
33337c478bd9Sstevel@tonic-gate 		return (EINVAL);
33347c478bd9Sstevel@tonic-gate 
33359acbbeafSnn35248 	mutex_enter(&stp->sd_lock);
33369acbbeafSnn35248 	if ((access != -1) && ((error = i_straccess(stp, access)) != 0)) {
33379acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
33387c478bd9Sstevel@tonic-gate 		return (error);
33399acbbeafSnn35248 	}
33409acbbeafSnn35248 	mutex_exit(&stp->sd_lock);
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	/*
33437c478bd9Sstevel@tonic-gate 	 * Check for sgttyb-related ioctls first, and complain as
33447c478bd9Sstevel@tonic-gate 	 * necessary.
33457c478bd9Sstevel@tonic-gate 	 */
33467c478bd9Sstevel@tonic-gate 	switch (cmd) {
33477c478bd9Sstevel@tonic-gate 	case TIOCGETP:
33487c478bd9Sstevel@tonic-gate 	case TIOCSETP:
33497c478bd9Sstevel@tonic-gate 	case TIOCSETN:
33507c478bd9Sstevel@tonic-gate 		if (sgttyb_handling >= 2 && !sgttyb_complaint) {
33517c478bd9Sstevel@tonic-gate 			sgttyb_complaint = B_TRUE;
33527c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE,
33537c478bd9Sstevel@tonic-gate 			    "application used obsolete TIOC[GS]ET");
33547c478bd9Sstevel@tonic-gate 		}
33557c478bd9Sstevel@tonic-gate 		if (sgttyb_handling >= 3) {
33567c478bd9Sstevel@tonic-gate 			tsignal(curthread, SIGSYS);
33577c478bd9Sstevel@tonic-gate 			return (EIO);
33587c478bd9Sstevel@tonic-gate 		}
33597c478bd9Sstevel@tonic-gate 		break;
33607c478bd9Sstevel@tonic-gate 	}
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 	switch (cmd) {
33657c478bd9Sstevel@tonic-gate 	case I_RECVFD:
33667c478bd9Sstevel@tonic-gate 	case I_E_RECVFD:
33677c478bd9Sstevel@tonic-gate 	case I_PEEK:
33687c478bd9Sstevel@tonic-gate 	case I_NREAD:
33697c478bd9Sstevel@tonic-gate 	case FIONREAD:
33707c478bd9Sstevel@tonic-gate 	case FIORDCHK:
33717c478bd9Sstevel@tonic-gate 	case I_ATMARK:
33727c478bd9Sstevel@tonic-gate 	case FIONBIO:
33737c478bd9Sstevel@tonic-gate 	case FIOASYNC:
33747c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STRDERR|STPLEX)) {
33757c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, STRDERR|STPLEX, 0);
33767c478bd9Sstevel@tonic-gate 			if (error != 0) {
33777c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
33787c478bd9Sstevel@tonic-gate 				return (error);
33797c478bd9Sstevel@tonic-gate 			}
33807c478bd9Sstevel@tonic-gate 		}
33817c478bd9Sstevel@tonic-gate 		break;
33827c478bd9Sstevel@tonic-gate 
33837c478bd9Sstevel@tonic-gate 	default:
33847c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STRDERR|STWRERR|STPLEX)) {
33857c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, STRDERR|STWRERR|STPLEX, 0);
33867c478bd9Sstevel@tonic-gate 			if (error != 0) {
33877c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
33887c478bd9Sstevel@tonic-gate 				return (error);
33897c478bd9Sstevel@tonic-gate 			}
33907c478bd9Sstevel@tonic-gate 		}
33917c478bd9Sstevel@tonic-gate 	}
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	switch (cmd) {
33967c478bd9Sstevel@tonic-gate 	default:
33977c478bd9Sstevel@tonic-gate 		/*
33987c478bd9Sstevel@tonic-gate 		 * The stream head has hardcoded knowledge of a
33997c478bd9Sstevel@tonic-gate 		 * miscellaneous collection of terminal-, keyboard- and
34007c478bd9Sstevel@tonic-gate 		 * mouse-related ioctls, enumerated below.  This hardcoded
34017c478bd9Sstevel@tonic-gate 		 * knowledge allows the stream head to automatically
34027c478bd9Sstevel@tonic-gate 		 * convert transparent ioctl requests made by userland
34037c478bd9Sstevel@tonic-gate 		 * programs into I_STR ioctls which many old STREAMS
34047c478bd9Sstevel@tonic-gate 		 * modules and drivers require.
34057c478bd9Sstevel@tonic-gate 		 *
34067c478bd9Sstevel@tonic-gate 		 * No new ioctls should ever be added to this list.
34077c478bd9Sstevel@tonic-gate 		 * Instead, the STREAMS module or driver should be written
34087c478bd9Sstevel@tonic-gate 		 * to either handle transparent ioctls or require any
34097c478bd9Sstevel@tonic-gate 		 * userland programs to use I_STR ioctls (by returning
34107c478bd9Sstevel@tonic-gate 		 * EINVAL to any transparent ioctl requests).
34117c478bd9Sstevel@tonic-gate 		 *
34127c478bd9Sstevel@tonic-gate 		 * More importantly, removing ioctls from this list should
34137c478bd9Sstevel@tonic-gate 		 * be done with the utmost care, since our STREAMS modules
34147c478bd9Sstevel@tonic-gate 		 * and drivers *count* on the stream head performing this
34157c478bd9Sstevel@tonic-gate 		 * conversion, and thus may panic while processing
34167c478bd9Sstevel@tonic-gate 		 * transparent ioctl request for one of these ioctls (keep
34177c478bd9Sstevel@tonic-gate 		 * in mind that third party modules and drivers may have
34187c478bd9Sstevel@tonic-gate 		 * similar problems).
34197c478bd9Sstevel@tonic-gate 		 */
34207c478bd9Sstevel@tonic-gate 		if (((cmd & IOCTYPE) == LDIOC) ||
34217c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == tIOC) ||
34227c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == TIOC) ||
34237c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == KIOC) ||
34247c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == MSIOC) ||
34257c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == VUIOC)) {
34267c478bd9Sstevel@tonic-gate 			/*
34277c478bd9Sstevel@tonic-gate 			 * The ioctl is a tty ioctl - set up strioc buffer
34287c478bd9Sstevel@tonic-gate 			 * and call strdoioctl() to do the work.
34297c478bd9Sstevel@tonic-gate 			 */
34307c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & STRHUP)
34317c478bd9Sstevel@tonic-gate 				return (ENXIO);
34327c478bd9Sstevel@tonic-gate 			strioc.ic_cmd = cmd;
34337c478bd9Sstevel@tonic-gate 			strioc.ic_timout = INFTIM;
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 			switch (cmd) {
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 			case TCXONC:
34387c478bd9Sstevel@tonic-gate 			case TCSBRK:
34397c478bd9Sstevel@tonic-gate 			case TCFLSH:
34407c478bd9Sstevel@tonic-gate 			case TCDSET:
34417c478bd9Sstevel@tonic-gate 				{
34427c478bd9Sstevel@tonic-gate 				int native_arg = (int)arg;
34437c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (int);
34447c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)&native_arg;
34457c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
34467c478bd9Sstevel@tonic-gate 				    K_TO_K, crp, rvalp));
34477c478bd9Sstevel@tonic-gate 				}
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate 			case TCSETA:
34507c478bd9Sstevel@tonic-gate 			case TCSETAW:
34517c478bd9Sstevel@tonic-gate 			case TCSETAF:
34527c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct termio);
34537c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
34547c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
34557c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 			case TCSETS:
34587c478bd9Sstevel@tonic-gate 			case TCSETSW:
34597c478bd9Sstevel@tonic-gate 			case TCSETSF:
34607c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct termios);
34617c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
34627c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
34637c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
34647c478bd9Sstevel@tonic-gate 
34657c478bd9Sstevel@tonic-gate 			case LDSETT:
34667c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct termcb);
34677c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
34687c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
34697c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 			case TIOCSETP:
34727c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct sgttyb);
34737c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
34747c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
34757c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 			case TIOCSTI:
34787c478bd9Sstevel@tonic-gate 				if ((flag & FREAD) == 0 &&
34797c478bd9Sstevel@tonic-gate 				    secpolicy_sti(crp) != 0) {
34807c478bd9Sstevel@tonic-gate 					return (EPERM);
34817c478bd9Sstevel@tonic-gate 				}
34829acbbeafSnn35248 				mutex_enter(&stp->sd_lock);
34839acbbeafSnn35248 				mutex_enter(&curproc->p_splock);
34849acbbeafSnn35248 				if (stp->sd_sidp != curproc->p_sessp->s_sidp &&
34857c478bd9Sstevel@tonic-gate 				    secpolicy_sti(crp) != 0) {
34869acbbeafSnn35248 					mutex_exit(&curproc->p_splock);
34879acbbeafSnn35248 					mutex_exit(&stp->sd_lock);
34887c478bd9Sstevel@tonic-gate 					return (EACCES);
34897c478bd9Sstevel@tonic-gate 				}
34909acbbeafSnn35248 				mutex_exit(&curproc->p_splock);
34919acbbeafSnn35248 				mutex_exit(&stp->sd_lock);
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (char);
34947c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
34957c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
34967c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate 			case TIOCSWINSZ:
34997c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct winsize);
35007c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
35017c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
35027c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate 			case TIOCSSIZE:
35057c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct ttysize);
35067c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
35077c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
35087c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 			case TIOCSSOFTCAR:
35117c478bd9Sstevel@tonic-gate 			case KIOCTRANS:
35127c478bd9Sstevel@tonic-gate 			case KIOCTRANSABLE:
35137c478bd9Sstevel@tonic-gate 			case KIOCCMD:
35147c478bd9Sstevel@tonic-gate 			case KIOCSDIRECT:
35157c478bd9Sstevel@tonic-gate 			case KIOCSCOMPAT:
35167c478bd9Sstevel@tonic-gate 			case KIOCSKABORTEN:
35177c478bd9Sstevel@tonic-gate 			case KIOCSRPTDELAY:
35187c478bd9Sstevel@tonic-gate 			case KIOCSRPTRATE:
35197c478bd9Sstevel@tonic-gate 			case VUIDSFORMAT:
35207c478bd9Sstevel@tonic-gate 			case TIOCSPPS:
35217c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (int);
35227c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
35237c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
35247c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate 			case KIOCSETKEY:
35277c478bd9Sstevel@tonic-gate 			case KIOCGETKEY:
35287c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct kiockey);
35297c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
35307c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
35317c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate 			case KIOCSKEY:
35347c478bd9Sstevel@tonic-gate 			case KIOCGKEY:
35357c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct kiockeymap);
35367c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
35377c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
35387c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate 			case KIOCSLED:
35417c478bd9Sstevel@tonic-gate 				/* arg is a pointer to char */
35427c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (char);
35437c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
35447c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
35457c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate 			case MSIOSETPARMS:
35487c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (Ms_parms);
35497c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
35507c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
35517c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
35527c478bd9Sstevel@tonic-gate 
35537c478bd9Sstevel@tonic-gate 			case VUIDSADDR:
35547c478bd9Sstevel@tonic-gate 			case VUIDGADDR:
35557c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct vuid_addr_probe);
35567c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
35577c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
35587c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 			/*
35617c478bd9Sstevel@tonic-gate 			 * These M_IOCTL's don't require any data to be sent
35627c478bd9Sstevel@tonic-gate 			 * downstream, and the driver will allocate and link
35637c478bd9Sstevel@tonic-gate 			 * on its own mblk_t upon M_IOCACK -- thus we set
35647c478bd9Sstevel@tonic-gate 			 * ic_len to zero and set ic_dp to arg so we know
35657c478bd9Sstevel@tonic-gate 			 * where to copyout to later.
35667c478bd9Sstevel@tonic-gate 			 */
35677c478bd9Sstevel@tonic-gate 			case TIOCGSOFTCAR:
35687c478bd9Sstevel@tonic-gate 			case TIOCGWINSZ:
35697c478bd9Sstevel@tonic-gate 			case TIOCGSIZE:
35707c478bd9Sstevel@tonic-gate 			case KIOCGTRANS:
35717c478bd9Sstevel@tonic-gate 			case KIOCGTRANSABLE:
35727c478bd9Sstevel@tonic-gate 			case KIOCTYPE:
35737c478bd9Sstevel@tonic-gate 			case KIOCGDIRECT:
35747c478bd9Sstevel@tonic-gate 			case KIOCGCOMPAT:
35757c478bd9Sstevel@tonic-gate 			case KIOCLAYOUT:
35767c478bd9Sstevel@tonic-gate 			case KIOCGLED:
35777c478bd9Sstevel@tonic-gate 			case MSIOGETPARMS:
35787c478bd9Sstevel@tonic-gate 			case MSIOBUTTONS:
35797c478bd9Sstevel@tonic-gate 			case VUIDGFORMAT:
35807c478bd9Sstevel@tonic-gate 			case TIOCGPPS:
35817c478bd9Sstevel@tonic-gate 			case TIOCGPPSEV:
35827c478bd9Sstevel@tonic-gate 			case TCGETA:
35837c478bd9Sstevel@tonic-gate 			case TCGETS:
35847c478bd9Sstevel@tonic-gate 			case LDGETT:
35857c478bd9Sstevel@tonic-gate 			case TIOCGETP:
35867c478bd9Sstevel@tonic-gate 			case KIOCGRPTDELAY:
35877c478bd9Sstevel@tonic-gate 			case KIOCGRPTRATE:
35887c478bd9Sstevel@tonic-gate 				strioc.ic_len = 0;
35897c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
35907c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
35917c478bd9Sstevel@tonic-gate 				    copyflag, crp, rvalp));
35927c478bd9Sstevel@tonic-gate 			}
35937c478bd9Sstevel@tonic-gate 		}
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 		/*
35967c478bd9Sstevel@tonic-gate 		 * Unknown cmd - send it down as a transparent ioctl.
35977c478bd9Sstevel@tonic-gate 		 */
35987c478bd9Sstevel@tonic-gate 		strioc.ic_cmd = cmd;
35997c478bd9Sstevel@tonic-gate 		strioc.ic_timout = INFTIM;
36007c478bd9Sstevel@tonic-gate 		strioc.ic_len = TRANSPARENT;
36017c478bd9Sstevel@tonic-gate 		strioc.ic_dp = (char *)&arg;
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 		return (strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp));
36047c478bd9Sstevel@tonic-gate 
36057c478bd9Sstevel@tonic-gate 	case I_STR:
36067c478bd9Sstevel@tonic-gate 		/*
36077c478bd9Sstevel@tonic-gate 		 * Stream ioctl.  Read in an strioctl buffer from the user
36087c478bd9Sstevel@tonic-gate 		 * along with any data specified and send it downstream.
36097c478bd9Sstevel@tonic-gate 		 * Strdoioctl will wait allow only one ioctl message at
36107c478bd9Sstevel@tonic-gate 		 * a time, and waits for the acknowledgement.
36117c478bd9Sstevel@tonic-gate 		 */
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
36147c478bd9Sstevel@tonic-gate 			return (ENXIO);
36157c478bd9Sstevel@tonic-gate 
36167c478bd9Sstevel@tonic-gate 		error = strcopyin_strioctl((void *)arg, &strioc, flag,
36177c478bd9Sstevel@tonic-gate 		    copyflag);
36187c478bd9Sstevel@tonic-gate 		if (error != 0)
36197c478bd9Sstevel@tonic-gate 			return (error);
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 		if ((strioc.ic_len < 0) || (strioc.ic_timout < -1))
36227c478bd9Sstevel@tonic-gate 			return (EINVAL);
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 		access = job_control_type(strioc.ic_cmd);
36259acbbeafSnn35248 		mutex_enter(&stp->sd_lock);
36269acbbeafSnn35248 		if ((access != -1) &&
36279acbbeafSnn35248 		    ((error = i_straccess(stp, access)) != 0)) {
36289acbbeafSnn35248 			mutex_exit(&stp->sd_lock);
36297c478bd9Sstevel@tonic-gate 			return (error);
36309acbbeafSnn35248 		}
36319acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
36327c478bd9Sstevel@tonic-gate 
36337c478bd9Sstevel@tonic-gate 		/*
36347c478bd9Sstevel@tonic-gate 		 * The I_STR facility provides a trap door for malicious
36357c478bd9Sstevel@tonic-gate 		 * code to send down bogus streamio(7I) ioctl commands to
36367c478bd9Sstevel@tonic-gate 		 * unsuspecting STREAMS modules and drivers which expect to
36377c478bd9Sstevel@tonic-gate 		 * only get these messages from the stream head.
36387c478bd9Sstevel@tonic-gate 		 * Explicitly prohibit any streamio ioctls which can be
36397c478bd9Sstevel@tonic-gate 		 * passed downstream by the stream head.  Note that we do
36407c478bd9Sstevel@tonic-gate 		 * not block all streamio ioctls because the ioctl
36417c478bd9Sstevel@tonic-gate 		 * numberspace is not well managed and thus it's possible
36427c478bd9Sstevel@tonic-gate 		 * that a module or driver's ioctl numbers may accidentally
36437c478bd9Sstevel@tonic-gate 		 * collide with them.
36447c478bd9Sstevel@tonic-gate 		 */
36457c478bd9Sstevel@tonic-gate 		switch (strioc.ic_cmd) {
36467c478bd9Sstevel@tonic-gate 		case I_LINK:
36477c478bd9Sstevel@tonic-gate 		case I_PLINK:
36487c478bd9Sstevel@tonic-gate 		case I_UNLINK:
36497c478bd9Sstevel@tonic-gate 		case I_PUNLINK:
36507c478bd9Sstevel@tonic-gate 		case _I_GETPEERCRED:
36517c478bd9Sstevel@tonic-gate 		case _I_PLINK_LH:
36527c478bd9Sstevel@tonic-gate 			return (EINVAL);
36537c478bd9Sstevel@tonic-gate 		}
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 		error = strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp);
36567c478bd9Sstevel@tonic-gate 		if (error == 0) {
36577c478bd9Sstevel@tonic-gate 			error = strcopyout_strioctl(&strioc, (void *)arg,
36587c478bd9Sstevel@tonic-gate 			    flag, copyflag);
36597c478bd9Sstevel@tonic-gate 		}
36607c478bd9Sstevel@tonic-gate 		return (error);
36617c478bd9Sstevel@tonic-gate 
3662ca9327a6Smeem 	case _I_CMD:
3663ca9327a6Smeem 		/*
3664ca9327a6Smeem 		 * Like I_STR, but without using M_IOC* messages and without
3665ca9327a6Smeem 		 * copyins/copyouts beyond the passed-in argument.
3666ca9327a6Smeem 		 */
3667ca9327a6Smeem 		if (stp->sd_flag & STRHUP)
3668ca9327a6Smeem 			return (ENXIO);
3669ca9327a6Smeem 
3670ca9327a6Smeem 		if ((scp = kmem_alloc(sizeof (strcmd_t), KM_NOSLEEP)) == NULL)
3671ca9327a6Smeem 			return (ENOMEM);
3672ca9327a6Smeem 
3673ca9327a6Smeem 		if (copyin((void *)arg, scp, sizeof (strcmd_t))) {
3674ca9327a6Smeem 			kmem_free(scp, sizeof (strcmd_t));
3675ca9327a6Smeem 			return (EFAULT);
3676ca9327a6Smeem 		}
3677ca9327a6Smeem 
3678ca9327a6Smeem 		access = job_control_type(scp->sc_cmd);
3679ca9327a6Smeem 		mutex_enter(&stp->sd_lock);
3680ca9327a6Smeem 		if (access != -1 && (error = i_straccess(stp, access)) != 0) {
3681ca9327a6Smeem 			mutex_exit(&stp->sd_lock);
3682ca9327a6Smeem 			kmem_free(scp, sizeof (strcmd_t));
3683ca9327a6Smeem 			return (error);
3684ca9327a6Smeem 		}
3685ca9327a6Smeem 		mutex_exit(&stp->sd_lock);
3686ca9327a6Smeem 
3687ca9327a6Smeem 		*rvalp = 0;
3688ca9327a6Smeem 		if ((error = strdocmd(stp, scp, crp)) == 0) {
3689ca9327a6Smeem 			if (copyout(scp, (void *)arg, sizeof (strcmd_t)))
3690ca9327a6Smeem 				error = EFAULT;
3691ca9327a6Smeem 		}
3692ca9327a6Smeem 		kmem_free(scp, sizeof (strcmd_t));
3693ca9327a6Smeem 		return (error);
3694ca9327a6Smeem 
36957c478bd9Sstevel@tonic-gate 	case I_NREAD:
36967c478bd9Sstevel@tonic-gate 		/*
36977c478bd9Sstevel@tonic-gate 		 * Return number of bytes of data in first message
36987c478bd9Sstevel@tonic-gate 		 * in queue in "arg" and return the number of messages
36997c478bd9Sstevel@tonic-gate 		 * in queue in return value.
37007c478bd9Sstevel@tonic-gate 		 */
37017c478bd9Sstevel@tonic-gate 	{
37027c478bd9Sstevel@tonic-gate 		size_t	size;
37037c478bd9Sstevel@tonic-gate 		int	retval;
37047c478bd9Sstevel@tonic-gate 		int	count = 0;
37057c478bd9Sstevel@tonic-gate 
37067c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rdq));
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 		size = msgdsize(rdq->q_first);
37097c478bd9Sstevel@tonic-gate 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
37107c478bd9Sstevel@tonic-gate 			count++;
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rdq));
37137c478bd9Sstevel@tonic-gate 		if (stp->sd_struiordq) {
37147c478bd9Sstevel@tonic-gate 			infod_t infod;
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 			infod.d_cmd = INFOD_COUNT;
37177c478bd9Sstevel@tonic-gate 			infod.d_count = 0;
37187c478bd9Sstevel@tonic-gate 			if (count == 0) {
37197c478bd9Sstevel@tonic-gate 				infod.d_cmd |= INFOD_FIRSTBYTES;
37207c478bd9Sstevel@tonic-gate 				infod.d_bytes = 0;
37217c478bd9Sstevel@tonic-gate 			}
37227c478bd9Sstevel@tonic-gate 			infod.d_res = 0;
37237c478bd9Sstevel@tonic-gate 			(void) infonext(rdq, &infod);
37247c478bd9Sstevel@tonic-gate 			count += infod.d_count;
37257c478bd9Sstevel@tonic-gate 			if (infod.d_res & INFOD_FIRSTBYTES)
37267c478bd9Sstevel@tonic-gate 				size = infod.d_bytes;
37277c478bd9Sstevel@tonic-gate 		}
37287c478bd9Sstevel@tonic-gate 
37297c478bd9Sstevel@tonic-gate 		/*
37307c478bd9Sstevel@tonic-gate 		 * Drop down from size_t to the "int" required by the
37317c478bd9Sstevel@tonic-gate 		 * interface.  Cap at INT_MAX.
37327c478bd9Sstevel@tonic-gate 		 */
37337c478bd9Sstevel@tonic-gate 		retval = MIN(size, INT_MAX);
37347c478bd9Sstevel@tonic-gate 		error = strcopyout(&retval, (void *)arg, sizeof (retval),
37357c478bd9Sstevel@tonic-gate 		    copyflag);
37367c478bd9Sstevel@tonic-gate 		if (!error)
37377c478bd9Sstevel@tonic-gate 			*rvalp = count;
37387c478bd9Sstevel@tonic-gate 		return (error);
37397c478bd9Sstevel@tonic-gate 	}
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate 	case FIONREAD:
37427c478bd9Sstevel@tonic-gate 		/*
37437c478bd9Sstevel@tonic-gate 		 * Return number of bytes of data in all data messages
37447c478bd9Sstevel@tonic-gate 		 * in queue in "arg".
37457c478bd9Sstevel@tonic-gate 		 */
37467c478bd9Sstevel@tonic-gate 	{
37477c478bd9Sstevel@tonic-gate 		size_t	size = 0;
37487c478bd9Sstevel@tonic-gate 		int	retval;
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rdq));
37517c478bd9Sstevel@tonic-gate 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
37527c478bd9Sstevel@tonic-gate 			size += msgdsize(mp);
37537c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rdq));
37547c478bd9Sstevel@tonic-gate 
37557c478bd9Sstevel@tonic-gate 		if (stp->sd_struiordq) {
37567c478bd9Sstevel@tonic-gate 			infod_t infod;
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate 			infod.d_cmd = INFOD_BYTES;
37597c478bd9Sstevel@tonic-gate 			infod.d_res = 0;
37607c478bd9Sstevel@tonic-gate 			infod.d_bytes = 0;
37617c478bd9Sstevel@tonic-gate 			(void) infonext(rdq, &infod);
37627c478bd9Sstevel@tonic-gate 			size += infod.d_bytes;
37637c478bd9Sstevel@tonic-gate 		}
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate 		/*
37667c478bd9Sstevel@tonic-gate 		 * Drop down from size_t to the "int" required by the
37677c478bd9Sstevel@tonic-gate 		 * interface.  Cap at INT_MAX.
37687c478bd9Sstevel@tonic-gate 		 */
37697c478bd9Sstevel@tonic-gate 		retval = MIN(size, INT_MAX);
37707c478bd9Sstevel@tonic-gate 		error = strcopyout(&retval, (void *)arg, sizeof (retval),
37717c478bd9Sstevel@tonic-gate 		    copyflag);
37727c478bd9Sstevel@tonic-gate 
37737c478bd9Sstevel@tonic-gate 		*rvalp = 0;
37747c478bd9Sstevel@tonic-gate 		return (error);
37757c478bd9Sstevel@tonic-gate 	}
37767c478bd9Sstevel@tonic-gate 	case FIORDCHK:
37777c478bd9Sstevel@tonic-gate 		/*
37787c478bd9Sstevel@tonic-gate 		 * FIORDCHK does not use arg value (like FIONREAD),
37797c478bd9Sstevel@tonic-gate 		 * instead a count is returned. I_NREAD value may
37807c478bd9Sstevel@tonic-gate 		 * not be accurate but safe. The real thing to do is
37817c478bd9Sstevel@tonic-gate 		 * to add the msgdsizes of all data  messages until
37827c478bd9Sstevel@tonic-gate 		 * a non-data message.
37837c478bd9Sstevel@tonic-gate 		 */
37847c478bd9Sstevel@tonic-gate 	{
37857c478bd9Sstevel@tonic-gate 		size_t size = 0;
37867c478bd9Sstevel@tonic-gate 
37877c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rdq));
37887c478bd9Sstevel@tonic-gate 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
37897c478bd9Sstevel@tonic-gate 			size += msgdsize(mp);
37907c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rdq));
37917c478bd9Sstevel@tonic-gate 
37927c478bd9Sstevel@tonic-gate 		if (stp->sd_struiordq) {
37937c478bd9Sstevel@tonic-gate 			infod_t infod;
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 			infod.d_cmd = INFOD_BYTES;
37967c478bd9Sstevel@tonic-gate 			infod.d_res = 0;
37977c478bd9Sstevel@tonic-gate 			infod.d_bytes = 0;
37987c478bd9Sstevel@tonic-gate 			(void) infonext(rdq, &infod);
37997c478bd9Sstevel@tonic-gate 			size += infod.d_bytes;
38007c478bd9Sstevel@tonic-gate 		}
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate 		/*
38037c478bd9Sstevel@tonic-gate 		 * Since ioctl returns an int, and memory sizes under
38047c478bd9Sstevel@tonic-gate 		 * LP64 may not fit, we return INT_MAX if the count was
38057c478bd9Sstevel@tonic-gate 		 * actually greater.
38067c478bd9Sstevel@tonic-gate 		 */
38077c478bd9Sstevel@tonic-gate 		*rvalp = MIN(size, INT_MAX);
38087c478bd9Sstevel@tonic-gate 		return (0);
38097c478bd9Sstevel@tonic-gate 	}
38107c478bd9Sstevel@tonic-gate 
38117c478bd9Sstevel@tonic-gate 	case I_FIND:
38127c478bd9Sstevel@tonic-gate 		/*
38137c478bd9Sstevel@tonic-gate 		 * Get module name.
38147c478bd9Sstevel@tonic-gate 		 */
38157c478bd9Sstevel@tonic-gate 	{
38167c478bd9Sstevel@tonic-gate 		char mname[FMNAMESZ + 1];
38177c478bd9Sstevel@tonic-gate 		queue_t *q;
38187c478bd9Sstevel@tonic-gate 
38197c478bd9Sstevel@tonic-gate 		error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
38207c478bd9Sstevel@tonic-gate 		    mname, FMNAMESZ + 1, NULL);
38217c478bd9Sstevel@tonic-gate 		if (error)
38227c478bd9Sstevel@tonic-gate 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 		/*
38257c478bd9Sstevel@tonic-gate 		 * Return EINVAL if we're handed a bogus module name.
38267c478bd9Sstevel@tonic-gate 		 */
38277c478bd9Sstevel@tonic-gate 		if (fmodsw_find(mname, FMODSW_LOAD) == NULL) {
38287c478bd9Sstevel@tonic-gate 			TRACE_0(TR_FAC_STREAMS_FR,
38297c478bd9Sstevel@tonic-gate 			    TR_I_CANT_FIND, "couldn't I_FIND");
38307c478bd9Sstevel@tonic-gate 			return (EINVAL);
38317c478bd9Sstevel@tonic-gate 		}
38327c478bd9Sstevel@tonic-gate 
38337c478bd9Sstevel@tonic-gate 		*rvalp = 0;
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 		/* Look downstream to see if module is there. */
38367c478bd9Sstevel@tonic-gate 		claimstr(stp->sd_wrq);
38377c478bd9Sstevel@tonic-gate 		for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
38387c478bd9Sstevel@tonic-gate 			if (q->q_flag&QREADR) {
38397c478bd9Sstevel@tonic-gate 				q = NULL;
38407c478bd9Sstevel@tonic-gate 				break;
38417c478bd9Sstevel@tonic-gate 			}
38427c478bd9Sstevel@tonic-gate 			if (strcmp(mname, q->q_qinfo->qi_minfo->mi_idname) == 0)
38437c478bd9Sstevel@tonic-gate 				break;
38447c478bd9Sstevel@tonic-gate 		}
38457c478bd9Sstevel@tonic-gate 		releasestr(stp->sd_wrq);
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate 		*rvalp = (q ? 1 : 0);
38487c478bd9Sstevel@tonic-gate 		return (error);
38497c478bd9Sstevel@tonic-gate 	}
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate 	case I_PUSH:
38527c478bd9Sstevel@tonic-gate 	case __I_PUSH_NOCTTY:
38537c478bd9Sstevel@tonic-gate 		/*
38547c478bd9Sstevel@tonic-gate 		 * Push a module.
38557c478bd9Sstevel@tonic-gate 		 * For the case __I_PUSH_NOCTTY push a module but
38567c478bd9Sstevel@tonic-gate 		 * do not allocate controlling tty. See bugid 4025044
38577c478bd9Sstevel@tonic-gate 		 */
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate 	{
38607c478bd9Sstevel@tonic-gate 		char mname[FMNAMESZ + 1];
38617c478bd9Sstevel@tonic-gate 		fmodsw_impl_t *fp;
38627c478bd9Sstevel@tonic-gate 		dev_t dummydev;
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
38657c478bd9Sstevel@tonic-gate 			return (ENXIO);
38667c478bd9Sstevel@tonic-gate 
38677c478bd9Sstevel@tonic-gate 		/*
38687c478bd9Sstevel@tonic-gate 		 * Get module name and look up in fmodsw.
38697c478bd9Sstevel@tonic-gate 		 */
38707c478bd9Sstevel@tonic-gate 		error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
38717c478bd9Sstevel@tonic-gate 		    mname, FMNAMESZ + 1, NULL);
38727c478bd9Sstevel@tonic-gate 		if (error)
38737c478bd9Sstevel@tonic-gate 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
38747c478bd9Sstevel@tonic-gate 
38757c478bd9Sstevel@tonic-gate 		if ((fp = fmodsw_find(mname, FMODSW_HOLD | FMODSW_LOAD)) ==
38767c478bd9Sstevel@tonic-gate 		    NULL)
38777c478bd9Sstevel@tonic-gate 			return (EINVAL);
38787c478bd9Sstevel@tonic-gate 
38797c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_I_PUSH,
38807c478bd9Sstevel@tonic-gate 		    "I_PUSH:fp %p stp %p", fp, stp);
38817c478bd9Sstevel@tonic-gate 
38827c478bd9Sstevel@tonic-gate 		if (error = strstartplumb(stp, flag, cmd)) {
38837c478bd9Sstevel@tonic-gate 			fmodsw_rele(fp);
38847c478bd9Sstevel@tonic-gate 			return (error);
38857c478bd9Sstevel@tonic-gate 		}
38867c478bd9Sstevel@tonic-gate 
38877c478bd9Sstevel@tonic-gate 		/*
38887c478bd9Sstevel@tonic-gate 		 * See if any more modules can be pushed on this stream.
38897c478bd9Sstevel@tonic-gate 		 * Note that this check must be done after strstartplumb()
38907c478bd9Sstevel@tonic-gate 		 * since otherwise multiple threads issuing I_PUSHes on
38917c478bd9Sstevel@tonic-gate 		 * the same stream will be able to exceed nstrpush.
38927c478bd9Sstevel@tonic-gate 		 */
38937c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
38947c478bd9Sstevel@tonic-gate 		if (stp->sd_pushcnt >= nstrpush) {
38957c478bd9Sstevel@tonic-gate 			fmodsw_rele(fp);
38967c478bd9Sstevel@tonic-gate 			strendplumb(stp);
38977c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
38987c478bd9Sstevel@tonic-gate 			return (EINVAL);
38997c478bd9Sstevel@tonic-gate 		}
39007c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate 		/*
39037c478bd9Sstevel@tonic-gate 		 * Push new module and call its open routine
39047c478bd9Sstevel@tonic-gate 		 * via qattach().  Modules don't change device
39057c478bd9Sstevel@tonic-gate 		 * numbers, so just ignore dummydev here.
39067c478bd9Sstevel@tonic-gate 		 */
39077c478bd9Sstevel@tonic-gate 		dummydev = vp->v_rdev;
39087c478bd9Sstevel@tonic-gate 		if ((error = qattach(rdq, &dummydev, 0, crp, fp,
39097c478bd9Sstevel@tonic-gate 		    B_FALSE)) == 0) {
39107c478bd9Sstevel@tonic-gate 			if (vp->v_type == VCHR && /* sorry, no pipes allowed */
39117c478bd9Sstevel@tonic-gate 			    (cmd == I_PUSH) && (stp->sd_flag & STRISTTY)) {
39127c478bd9Sstevel@tonic-gate 				/*
39137c478bd9Sstevel@tonic-gate 				 * try to allocate it as a controlling terminal
39147c478bd9Sstevel@tonic-gate 				 */
39159acbbeafSnn35248 				(void) strctty(stp);
39167c478bd9Sstevel@tonic-gate 			}
39177c478bd9Sstevel@tonic-gate 		}
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
39207c478bd9Sstevel@tonic-gate 
39217c478bd9Sstevel@tonic-gate 		/*
39227c478bd9Sstevel@tonic-gate 		 * As a performance concern we are caching the values of
39237c478bd9Sstevel@tonic-gate 		 * q_minpsz and q_maxpsz of the module below the stream
39247c478bd9Sstevel@tonic-gate 		 * head in the stream head.
39257c478bd9Sstevel@tonic-gate 		 */
39267c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(stp->sd_wrq->q_next));
39277c478bd9Sstevel@tonic-gate 		rmin = stp->sd_wrq->q_next->q_minpsz;
39287c478bd9Sstevel@tonic-gate 		rmax = stp->sd_wrq->q_next->q_maxpsz;
39297c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(stp->sd_wrq->q_next));
39307c478bd9Sstevel@tonic-gate 
39317c478bd9Sstevel@tonic-gate 		/* Do this processing here as a performance concern */
39327c478bd9Sstevel@tonic-gate 		if (strmsgsz != 0) {
39337c478bd9Sstevel@tonic-gate 			if (rmax == INFPSZ)
39347c478bd9Sstevel@tonic-gate 				rmax = strmsgsz;
39357c478bd9Sstevel@tonic-gate 			else  {
39367c478bd9Sstevel@tonic-gate 				if (vp->v_type == VFIFO)
39377c478bd9Sstevel@tonic-gate 					rmax = MIN(PIPE_BUF, rmax);
39387c478bd9Sstevel@tonic-gate 				else	rmax = MIN(strmsgsz, rmax);
39397c478bd9Sstevel@tonic-gate 			}
39407c478bd9Sstevel@tonic-gate 		}
39417c478bd9Sstevel@tonic-gate 
39427c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(wrq));
39437c478bd9Sstevel@tonic-gate 		stp->sd_qn_minpsz = rmin;
39447c478bd9Sstevel@tonic-gate 		stp->sd_qn_maxpsz = rmax;
39457c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(wrq));
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate 		strendplumb(stp);
39487c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
39497c478bd9Sstevel@tonic-gate 		return (error);
39507c478bd9Sstevel@tonic-gate 	}
39517c478bd9Sstevel@tonic-gate 
39527c478bd9Sstevel@tonic-gate 	case I_POP:
39537c478bd9Sstevel@tonic-gate 	{
39547c478bd9Sstevel@tonic-gate 		queue_t	*q;
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
39577c478bd9Sstevel@tonic-gate 			return (ENXIO);
39587c478bd9Sstevel@tonic-gate 		if (!wrq->q_next)	/* for broken pipes */
39597c478bd9Sstevel@tonic-gate 			return (EINVAL);
39607c478bd9Sstevel@tonic-gate 
39617c478bd9Sstevel@tonic-gate 		if (error = strstartplumb(stp, flag, cmd))
39627c478bd9Sstevel@tonic-gate 			return (error);
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 		/*
39657c478bd9Sstevel@tonic-gate 		 * If there is an anchor on this stream and popping
39667c478bd9Sstevel@tonic-gate 		 * the current module would attempt to pop through the
39677c478bd9Sstevel@tonic-gate 		 * anchor, then disallow the pop unless we have sufficient
39687c478bd9Sstevel@tonic-gate 		 * privileges; take the cheapest (non-locking) check
39697c478bd9Sstevel@tonic-gate 		 * first.
39707c478bd9Sstevel@tonic-gate 		 */
3971f4b3ec61Sdh155122 		if (secpolicy_ip_config(crp, B_TRUE) != 0 ||
3972f4b3ec61Sdh155122 		    (stp->sd_anchorzone != crgetzoneid(crp))) {
39737c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
39747c478bd9Sstevel@tonic-gate 			/*
39757c478bd9Sstevel@tonic-gate 			 * Anchors only apply if there's at least one
39767c478bd9Sstevel@tonic-gate 			 * module on the stream (sd_pushcnt > 0).
39777c478bd9Sstevel@tonic-gate 			 */
39787c478bd9Sstevel@tonic-gate 			if (stp->sd_pushcnt > 0 &&
39797c478bd9Sstevel@tonic-gate 			    stp->sd_pushcnt == stp->sd_anchor &&
39807c478bd9Sstevel@tonic-gate 			    stp->sd_vnode->v_type != VFIFO) {
39817c478bd9Sstevel@tonic-gate 				strendplumb(stp);
39827c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
3983f4b3ec61Sdh155122 				if (stp->sd_anchorzone != crgetzoneid(crp))
3984f4b3ec61Sdh155122 					return (EINVAL);
39857c478bd9Sstevel@tonic-gate 				/* Audit and report error */
3986f4b3ec61Sdh155122 				return (secpolicy_ip_config(crp, B_FALSE));
39877c478bd9Sstevel@tonic-gate 			}
39887c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
39897c478bd9Sstevel@tonic-gate 		}
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 		q = wrq->q_next;
39927c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_I_POP,
39937c478bd9Sstevel@tonic-gate 		    "I_POP:%p from %p", q, stp);
39947c478bd9Sstevel@tonic-gate 		if (q->q_next == NULL || (q->q_flag & (QREADR|QISDRV))) {
39957c478bd9Sstevel@tonic-gate 			error = EINVAL;
39967c478bd9Sstevel@tonic-gate 		} else {
39977c478bd9Sstevel@tonic-gate 			qdetach(_RD(q), 1, flag, crp, B_FALSE);
39987c478bd9Sstevel@tonic-gate 			error = 0;
39997c478bd9Sstevel@tonic-gate 		}
40007c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate 		/*
40037c478bd9Sstevel@tonic-gate 		 * As a performance concern we are caching the values of
40047c478bd9Sstevel@tonic-gate 		 * q_minpsz and q_maxpsz of the module below the stream
40057c478bd9Sstevel@tonic-gate 		 * head in the stream head.
40067c478bd9Sstevel@tonic-gate 		 */
40077c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(wrq->q_next));
40087c478bd9Sstevel@tonic-gate 		rmin = wrq->q_next->q_minpsz;
40097c478bd9Sstevel@tonic-gate 		rmax = wrq->q_next->q_maxpsz;
40107c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(wrq->q_next));
40117c478bd9Sstevel@tonic-gate 
40127c478bd9Sstevel@tonic-gate 		/* Do this processing here as a performance concern */
40137c478bd9Sstevel@tonic-gate 		if (strmsgsz != 0) {
40147c478bd9Sstevel@tonic-gate 			if (rmax == INFPSZ)
40157c478bd9Sstevel@tonic-gate 				rmax = strmsgsz;
40167c478bd9Sstevel@tonic-gate 			else  {
40177c478bd9Sstevel@tonic-gate 				if (vp->v_type == VFIFO)
40187c478bd9Sstevel@tonic-gate 					rmax = MIN(PIPE_BUF, rmax);
40197c478bd9Sstevel@tonic-gate 				else	rmax = MIN(strmsgsz, rmax);
40207c478bd9Sstevel@tonic-gate 			}
40217c478bd9Sstevel@tonic-gate 		}
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(wrq));
40247c478bd9Sstevel@tonic-gate 		stp->sd_qn_minpsz = rmin;
40257c478bd9Sstevel@tonic-gate 		stp->sd_qn_maxpsz = rmax;
40267c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(wrq));
40277c478bd9Sstevel@tonic-gate 
40287c478bd9Sstevel@tonic-gate 		/* If we popped through the anchor, then reset the anchor. */
4029f4b3ec61Sdh155122 		if (stp->sd_pushcnt < stp->sd_anchor) {
40307c478bd9Sstevel@tonic-gate 			stp->sd_anchor = 0;
4031f4b3ec61Sdh155122 			stp->sd_anchorzone = 0;
4032f4b3ec61Sdh155122 		}
40337c478bd9Sstevel@tonic-gate 		strendplumb(stp);
40347c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
40357c478bd9Sstevel@tonic-gate 		return (error);
40367c478bd9Sstevel@tonic-gate 	}
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate 	case _I_MUXID2FD:
40397c478bd9Sstevel@tonic-gate 	{
40407c478bd9Sstevel@tonic-gate 		/*
40417c478bd9Sstevel@tonic-gate 		 * Create a fd for a I_PLINK'ed lower stream with a given
40427c478bd9Sstevel@tonic-gate 		 * muxid.  With the fd, application can send down ioctls,
40437c478bd9Sstevel@tonic-gate 		 * like I_LIST, to the previously I_PLINK'ed stream.  Note
40447c478bd9Sstevel@tonic-gate 		 * that after getting the fd, the application has to do an
40457c478bd9Sstevel@tonic-gate 		 * I_PUNLINK on the muxid before it can do any operation
40467c478bd9Sstevel@tonic-gate 		 * on the lower stream.  This is required by spec1170.
40477c478bd9Sstevel@tonic-gate 		 *
40487c478bd9Sstevel@tonic-gate 		 * The fd used to do this ioctl should point to the same
40497c478bd9Sstevel@tonic-gate 		 * controlling device used to do the I_PLINK.  If it uses
40507c478bd9Sstevel@tonic-gate 		 * a different stream or an invalid muxid, I_MUXID2FD will
40517c478bd9Sstevel@tonic-gate 		 * fail.  The error code is set to EINVAL.
40527c478bd9Sstevel@tonic-gate 		 *
40537c478bd9Sstevel@tonic-gate 		 * The intended use of this interface is the following.
40547c478bd9Sstevel@tonic-gate 		 * An application I_PLINK'ed a stream and exits.  The fd
40557c478bd9Sstevel@tonic-gate 		 * to the lower stream is gone.  Another application
40567c478bd9Sstevel@tonic-gate 		 * wants to get a fd to the lower stream, it uses I_MUXID2FD.
40577c478bd9Sstevel@tonic-gate 		 */
40587c478bd9Sstevel@tonic-gate 		int muxid = (int)arg;
40597c478bd9Sstevel@tonic-gate 		int fd;
40607c478bd9Sstevel@tonic-gate 		linkinfo_t *linkp;
40617c478bd9Sstevel@tonic-gate 		struct file *fp;
4062f4b3ec61Sdh155122 		netstack_t *ns;
4063f4b3ec61Sdh155122 		str_stack_t *ss;
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 		/*
40667c478bd9Sstevel@tonic-gate 		 * Do not allow the wildcard muxid.  This ioctl is not
40677c478bd9Sstevel@tonic-gate 		 * intended to find arbitrary link.
40687c478bd9Sstevel@tonic-gate 		 */
40697c478bd9Sstevel@tonic-gate 		if (muxid == 0) {
40707c478bd9Sstevel@tonic-gate 			return (EINVAL);
40717c478bd9Sstevel@tonic-gate 		}
40727c478bd9Sstevel@tonic-gate 
4073f4b3ec61Sdh155122 		ns = netstack_find_by_cred(crp);
4074f4b3ec61Sdh155122 		ASSERT(ns != NULL);
4075f4b3ec61Sdh155122 		ss = ns->netstack_str;
4076f4b3ec61Sdh155122 		ASSERT(ss != NULL);
4077f4b3ec61Sdh155122 
40787c478bd9Sstevel@tonic-gate 		mutex_enter(&muxifier);
4079f4b3ec61Sdh155122 		linkp = findlinks(vp->v_stream, muxid, LINKPERSIST, ss);
40807c478bd9Sstevel@tonic-gate 		if (linkp == NULL) {
40817c478bd9Sstevel@tonic-gate 			mutex_exit(&muxifier);
4082f4b3ec61Sdh155122 			netstack_rele(ss->ss_netstack);
40837c478bd9Sstevel@tonic-gate 			return (EINVAL);
40847c478bd9Sstevel@tonic-gate 		}
40857c478bd9Sstevel@tonic-gate 
40867c478bd9Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1) {
40877c478bd9Sstevel@tonic-gate 			mutex_exit(&muxifier);
4088f4b3ec61Sdh155122 			netstack_rele(ss->ss_netstack);
40897c478bd9Sstevel@tonic-gate 			return (EMFILE);
40907c478bd9Sstevel@tonic-gate 		}
40917c478bd9Sstevel@tonic-gate 		fp = linkp->li_fpdown;
40927c478bd9Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
40937c478bd9Sstevel@tonic-gate 		fp->f_count++;
40947c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
40957c478bd9Sstevel@tonic-gate 		mutex_exit(&muxifier);
40967c478bd9Sstevel@tonic-gate 		setf(fd, fp);
40977c478bd9Sstevel@tonic-gate 		*rvalp = fd;
4098f4b3ec61Sdh155122 		netstack_rele(ss->ss_netstack);
40997c478bd9Sstevel@tonic-gate 		return (0);
41007c478bd9Sstevel@tonic-gate 	}
41017c478bd9Sstevel@tonic-gate 
41027c478bd9Sstevel@tonic-gate 	case _I_INSERT:
41037c478bd9Sstevel@tonic-gate 	{
41047c478bd9Sstevel@tonic-gate 		/*
41057c478bd9Sstevel@tonic-gate 		 * To insert a module to a given position in a stream.
41067c478bd9Sstevel@tonic-gate 		 * In the first release, only allow privileged user
4107f4b3ec61Sdh155122 		 * to use this ioctl. Furthermore, the insert is only allowed
4108f4b3ec61Sdh155122 		 * below an anchor if the zoneid is the same as the zoneid
4109f4b3ec61Sdh155122 		 * which created the anchor.
41107c478bd9Sstevel@tonic-gate 		 *
41117c478bd9Sstevel@tonic-gate 		 * Note that we do not plan to support this ioctl
41127c478bd9Sstevel@tonic-gate 		 * on pipes in the first release.  We want to learn more
41137c478bd9Sstevel@tonic-gate 		 * about the implications of these ioctls before extending
41147c478bd9Sstevel@tonic-gate 		 * their support.  And we do not think these features are
41157c478bd9Sstevel@tonic-gate 		 * valuable for pipes.
41167c478bd9Sstevel@tonic-gate 		 *
41177c478bd9Sstevel@tonic-gate 		 * Neither do we support O/C hot stream.  Note that only
41187c478bd9Sstevel@tonic-gate 		 * the upper streams of TCP/IP stack are O/C hot streams.
41197c478bd9Sstevel@tonic-gate 		 * The lower IP stream is not.
41207c478bd9Sstevel@tonic-gate 		 * When there is a O/C cold barrier, we only allow inserts
41217c478bd9Sstevel@tonic-gate 		 * above the barrier.
41227c478bd9Sstevel@tonic-gate 		 */
41237c478bd9Sstevel@tonic-gate 		STRUCT_DECL(strmodconf, strmodinsert);
41247c478bd9Sstevel@tonic-gate 		char mod_name[FMNAMESZ + 1];
41257c478bd9Sstevel@tonic-gate 		fmodsw_impl_t *fp;
41267c478bd9Sstevel@tonic-gate 		dev_t dummydev;
41277c478bd9Sstevel@tonic-gate 		queue_t *tmp_wrq;
41287c478bd9Sstevel@tonic-gate 		int pos;
41297c478bd9Sstevel@tonic-gate 		boolean_t is_insert;
41307c478bd9Sstevel@tonic-gate 
41317c478bd9Sstevel@tonic-gate 		STRUCT_INIT(strmodinsert, flag);
41327c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
41337c478bd9Sstevel@tonic-gate 			return (ENXIO);
41347c478bd9Sstevel@tonic-gate 		if (STRMATED(stp))
41357c478bd9Sstevel@tonic-gate 			return (EINVAL);
41367c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
41377c478bd9Sstevel@tonic-gate 			return (error);
4138f4b3ec61Sdh155122 		if (stp->sd_anchor != 0 &&
4139f4b3ec61Sdh155122 		    stp->sd_anchorzone != crgetzoneid(crp))
4140f4b3ec61Sdh155122 			return (EINVAL);
41417c478bd9Sstevel@tonic-gate 
41427c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, STRUCT_BUF(strmodinsert),
41437c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strmodinsert), copyflag);
41447c478bd9Sstevel@tonic-gate 		if (error)
41457c478bd9Sstevel@tonic-gate 			return (error);
41467c478bd9Sstevel@tonic-gate 
41477c478bd9Sstevel@tonic-gate 		/*
41487c478bd9Sstevel@tonic-gate 		 * Get module name and look up in fmodsw.
41497c478bd9Sstevel@tonic-gate 		 */
41507c478bd9Sstevel@tonic-gate 		error = (copyflag & U_TO_K ? copyinstr :
41517c478bd9Sstevel@tonic-gate 		    copystr)(STRUCT_FGETP(strmodinsert, mod_name),
41527c478bd9Sstevel@tonic-gate 		    mod_name, FMNAMESZ + 1, NULL);
41537c478bd9Sstevel@tonic-gate 		if (error)
41547c478bd9Sstevel@tonic-gate 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate 		if ((fp = fmodsw_find(mod_name, FMODSW_HOLD | FMODSW_LOAD)) ==
41577c478bd9Sstevel@tonic-gate 		    NULL)
41587c478bd9Sstevel@tonic-gate 			return (EINVAL);
41597c478bd9Sstevel@tonic-gate 
41607c478bd9Sstevel@tonic-gate 		if (error = strstartplumb(stp, flag, cmd)) {
41617c478bd9Sstevel@tonic-gate 			fmodsw_rele(fp);
41627c478bd9Sstevel@tonic-gate 			return (error);
41637c478bd9Sstevel@tonic-gate 		}
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate 		/*
41667c478bd9Sstevel@tonic-gate 		 * Is this _I_INSERT just like an I_PUSH?  We need to know
41677c478bd9Sstevel@tonic-gate 		 * this because we do some optimizations if this is a
41687c478bd9Sstevel@tonic-gate 		 * module being pushed.
41697c478bd9Sstevel@tonic-gate 		 */
41707c478bd9Sstevel@tonic-gate 		pos = STRUCT_FGET(strmodinsert, pos);
41717c478bd9Sstevel@tonic-gate 		is_insert = (pos != 0);
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate 		/*
41747c478bd9Sstevel@tonic-gate 		 * Make sure pos is valid.  Even though it is not an I_PUSH,
41757c478bd9Sstevel@tonic-gate 		 * we impose the same limit on the number of modules in a
41767c478bd9Sstevel@tonic-gate 		 * stream.
41777c478bd9Sstevel@tonic-gate 		 */
41787c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
41797c478bd9Sstevel@tonic-gate 		if (stp->sd_pushcnt >= nstrpush || pos < 0 ||
41807c478bd9Sstevel@tonic-gate 		    pos > stp->sd_pushcnt) {
41817c478bd9Sstevel@tonic-gate 			fmodsw_rele(fp);
41827c478bd9Sstevel@tonic-gate 			strendplumb(stp);
41837c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
41847c478bd9Sstevel@tonic-gate 			return (EINVAL);
41857c478bd9Sstevel@tonic-gate 		}
4186f4b3ec61Sdh155122 		if (stp->sd_anchor != 0) {
4187f4b3ec61Sdh155122 			/*
4188f4b3ec61Sdh155122 			 * Is this insert below the anchor?
4189f4b3ec61Sdh155122 			 * Pushcnt hasn't been increased yet hence
4190f4b3ec61Sdh155122 			 * we test for greater than here, and greater or
4191f4b3ec61Sdh155122 			 * equal after qattach.
4192f4b3ec61Sdh155122 			 */
4193f4b3ec61Sdh155122 			if (pos > (stp->sd_pushcnt - stp->sd_anchor) &&
4194f4b3ec61Sdh155122 			    stp->sd_anchorzone != crgetzoneid(crp)) {
4195f4b3ec61Sdh155122 				fmodsw_rele(fp);
4196f4b3ec61Sdh155122 				strendplumb(stp);
4197f4b3ec61Sdh155122 				mutex_exit(&stp->sd_lock);
4198f4b3ec61Sdh155122 				return (EPERM);
4199f4b3ec61Sdh155122 			}
4200f4b3ec61Sdh155122 		}
4201f4b3ec61Sdh155122 
42027c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
42037c478bd9Sstevel@tonic-gate 
42047c478bd9Sstevel@tonic-gate 		/*
42057c478bd9Sstevel@tonic-gate 		 * First find the correct position this module to
42067c478bd9Sstevel@tonic-gate 		 * be inserted.  We don't need to call claimstr()
42077c478bd9Sstevel@tonic-gate 		 * as the stream should not be changing at this point.
42087c478bd9Sstevel@tonic-gate 		 *
42097c478bd9Sstevel@tonic-gate 		 * Insert new module and call its open routine
42107c478bd9Sstevel@tonic-gate 		 * via qattach().  Modules don't change device
42117c478bd9Sstevel@tonic-gate 		 * numbers, so just ignore dummydev here.
42127c478bd9Sstevel@tonic-gate 		 */
42137c478bd9Sstevel@tonic-gate 		for (tmp_wrq = stp->sd_wrq; pos > 0;
42147c478bd9Sstevel@tonic-gate 		    tmp_wrq = tmp_wrq->q_next, pos--) {
42157c478bd9Sstevel@tonic-gate 			ASSERT(SAMESTR(tmp_wrq));
42167c478bd9Sstevel@tonic-gate 		}
42177c478bd9Sstevel@tonic-gate 		dummydev = vp->v_rdev;
42187c478bd9Sstevel@tonic-gate 		if ((error = qattach(_RD(tmp_wrq), &dummydev, 0, crp,
42197c478bd9Sstevel@tonic-gate 		    fp, is_insert)) != 0) {
42207c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
42217c478bd9Sstevel@tonic-gate 			strendplumb(stp);
42227c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
42237c478bd9Sstevel@tonic-gate 			return (error);
42247c478bd9Sstevel@tonic-gate 		}
42257c478bd9Sstevel@tonic-gate 
42267c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
42277c478bd9Sstevel@tonic-gate 
42287c478bd9Sstevel@tonic-gate 		/*
42297c478bd9Sstevel@tonic-gate 		 * As a performance concern we are caching the values of
42307c478bd9Sstevel@tonic-gate 		 * q_minpsz and q_maxpsz of the module below the stream
42317c478bd9Sstevel@tonic-gate 		 * head in the stream head.
42327c478bd9Sstevel@tonic-gate 		 */
42337c478bd9Sstevel@tonic-gate 		if (!is_insert) {
42347c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(stp->sd_wrq->q_next));
42357c478bd9Sstevel@tonic-gate 			rmin = stp->sd_wrq->q_next->q_minpsz;
42367c478bd9Sstevel@tonic-gate 			rmax = stp->sd_wrq->q_next->q_maxpsz;
42377c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(stp->sd_wrq->q_next));
42387c478bd9Sstevel@tonic-gate 
42397c478bd9Sstevel@tonic-gate 			/* Do this processing here as a performance concern */
42407c478bd9Sstevel@tonic-gate 			if (strmsgsz != 0) {
42417c478bd9Sstevel@tonic-gate 				if (rmax == INFPSZ) {
42427c478bd9Sstevel@tonic-gate 					rmax = strmsgsz;
42437c478bd9Sstevel@tonic-gate 				} else  {
42447c478bd9Sstevel@tonic-gate 					rmax = MIN(strmsgsz, rmax);
42457c478bd9Sstevel@tonic-gate 				}
42467c478bd9Sstevel@tonic-gate 			}
42477c478bd9Sstevel@tonic-gate 
42487c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(wrq));
42497c478bd9Sstevel@tonic-gate 			stp->sd_qn_minpsz = rmin;
42507c478bd9Sstevel@tonic-gate 			stp->sd_qn_maxpsz = rmax;
42517c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(wrq));
42527c478bd9Sstevel@tonic-gate 		}
42537c478bd9Sstevel@tonic-gate 
42547c478bd9Sstevel@tonic-gate 		/*
42557c478bd9Sstevel@tonic-gate 		 * Need to update the anchor value if this module is
42567c478bd9Sstevel@tonic-gate 		 * inserted below the anchor point.
42577c478bd9Sstevel@tonic-gate 		 */
42587c478bd9Sstevel@tonic-gate 		if (stp->sd_anchor != 0) {
42597c478bd9Sstevel@tonic-gate 			pos = STRUCT_FGET(strmodinsert, pos);
42607c478bd9Sstevel@tonic-gate 			if (pos >= (stp->sd_pushcnt - stp->sd_anchor))
42617c478bd9Sstevel@tonic-gate 				stp->sd_anchor++;
42627c478bd9Sstevel@tonic-gate 		}
42637c478bd9Sstevel@tonic-gate 
42647c478bd9Sstevel@tonic-gate 		strendplumb(stp);
42657c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
42667c478bd9Sstevel@tonic-gate 		return (0);
42677c478bd9Sstevel@tonic-gate 	}
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	case _I_REMOVE:
42707c478bd9Sstevel@tonic-gate 	{
42717c478bd9Sstevel@tonic-gate 		/*
42727c478bd9Sstevel@tonic-gate 		 * To remove a module with a given name in a stream.  The
42737c478bd9Sstevel@tonic-gate 		 * caller of this ioctl needs to provide both the name and
42747c478bd9Sstevel@tonic-gate 		 * the position of the module to be removed.  This eliminates
42757c478bd9Sstevel@tonic-gate 		 * the ambiguity of removal if a module is inserted/pushed
42767c478bd9Sstevel@tonic-gate 		 * multiple times in a stream.  In the first release, only
42777c478bd9Sstevel@tonic-gate 		 * allow privileged user to use this ioctl.
4278f4b3ec61Sdh155122 		 * Furthermore, the remove is only allowed
4279f4b3ec61Sdh155122 		 * below an anchor if the zoneid is the same as the zoneid
4280f4b3ec61Sdh155122 		 * which created the anchor.
42817c478bd9Sstevel@tonic-gate 		 *
42827c478bd9Sstevel@tonic-gate 		 * Note that we do not plan to support this ioctl
42837c478bd9Sstevel@tonic-gate 		 * on pipes in the first release.  We want to learn more
42847c478bd9Sstevel@tonic-gate 		 * about the implications of these ioctls before extending
42857c478bd9Sstevel@tonic-gate 		 * their support.  And we do not think these features are
42867c478bd9Sstevel@tonic-gate 		 * valuable for pipes.
42877c478bd9Sstevel@tonic-gate 		 *
42887c478bd9Sstevel@tonic-gate 		 * Neither do we support O/C hot stream.  Note that only
42897c478bd9Sstevel@tonic-gate 		 * the upper streams of TCP/IP stack are O/C hot streams.
42907c478bd9Sstevel@tonic-gate 		 * The lower IP stream is not.
42917c478bd9Sstevel@tonic-gate 		 * When there is a O/C cold barrier we do not allow removal
42927c478bd9Sstevel@tonic-gate 		 * below the barrier.
42937c478bd9Sstevel@tonic-gate 		 *
42947c478bd9Sstevel@tonic-gate 		 * Also note that _I_REMOVE cannot be used to remove a
42957c478bd9Sstevel@tonic-gate 		 * driver or the stream head.
42967c478bd9Sstevel@tonic-gate 		 */
42977c478bd9Sstevel@tonic-gate 		STRUCT_DECL(strmodconf, strmodremove);
42987c478bd9Sstevel@tonic-gate 		queue_t	*q;
42997c478bd9Sstevel@tonic-gate 		int pos;
43007c478bd9Sstevel@tonic-gate 		char mod_name[FMNAMESZ + 1];
43017c478bd9Sstevel@tonic-gate 		boolean_t is_remove;
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 		STRUCT_INIT(strmodremove, flag);
43047c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
43057c478bd9Sstevel@tonic-gate 			return (ENXIO);
43067c478bd9Sstevel@tonic-gate 		if (STRMATED(stp))
43077c478bd9Sstevel@tonic-gate 			return (EINVAL);
43087c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
43097c478bd9Sstevel@tonic-gate 			return (error);
4310f4b3ec61Sdh155122 		if (stp->sd_anchor != 0 &&
4311f4b3ec61Sdh155122 		    stp->sd_anchorzone != crgetzoneid(crp))
4312f4b3ec61Sdh155122 			return (EINVAL);
43137c478bd9Sstevel@tonic-gate 
43147c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, STRUCT_BUF(strmodremove),
43157c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strmodremove), copyflag);
43167c478bd9Sstevel@tonic-gate 		if (error)
43177c478bd9Sstevel@tonic-gate 			return (error);
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 		error = (copyflag & U_TO_K ? copyinstr :
43207c478bd9Sstevel@tonic-gate 		    copystr)(STRUCT_FGETP(strmodremove, mod_name),
43217c478bd9Sstevel@tonic-gate 		    mod_name, FMNAMESZ + 1, NULL);
43227c478bd9Sstevel@tonic-gate 		if (error)
43237c478bd9Sstevel@tonic-gate 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
43247c478bd9Sstevel@tonic-gate 
43257c478bd9Sstevel@tonic-gate 		if ((error = strstartplumb(stp, flag, cmd)) != 0)
43267c478bd9Sstevel@tonic-gate 			return (error);
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 		/*
43297c478bd9Sstevel@tonic-gate 		 * Match the name of given module to the name of module at
43307c478bd9Sstevel@tonic-gate 		 * the given position.
43317c478bd9Sstevel@tonic-gate 		 */
43327c478bd9Sstevel@tonic-gate 		pos = STRUCT_FGET(strmodremove, pos);
43337c478bd9Sstevel@tonic-gate 
43347c478bd9Sstevel@tonic-gate 		is_remove = (pos != 0);
43357c478bd9Sstevel@tonic-gate 		for (q = stp->sd_wrq->q_next; SAMESTR(q) && pos > 0;
43367c478bd9Sstevel@tonic-gate 		    q = q->q_next, pos--)
43377c478bd9Sstevel@tonic-gate 			;
43387c478bd9Sstevel@tonic-gate 		if (pos > 0 || ! SAMESTR(q) ||
43397c478bd9Sstevel@tonic-gate 		    strncmp(q->q_qinfo->qi_minfo->mi_idname, mod_name,
43407c478bd9Sstevel@tonic-gate 		    strlen(q->q_qinfo->qi_minfo->mi_idname)) != 0) {
43417c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
43427c478bd9Sstevel@tonic-gate 			strendplumb(stp);
43437c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
43447c478bd9Sstevel@tonic-gate 			return (EINVAL);
43457c478bd9Sstevel@tonic-gate 		}
43467c478bd9Sstevel@tonic-gate 
4347f4b3ec61Sdh155122 		/*
4348f4b3ec61Sdh155122 		 * If the position is at or below an anchor, then the zoneid
4349f4b3ec61Sdh155122 		 * must match the zoneid that created the anchor.
4350f4b3ec61Sdh155122 		 */
4351f4b3ec61Sdh155122 		if (stp->sd_anchor != 0) {
4352f4b3ec61Sdh155122 			pos = STRUCT_FGET(strmodremove, pos);
4353f4b3ec61Sdh155122 			if (pos >= (stp->sd_pushcnt - stp->sd_anchor) &&
4354f4b3ec61Sdh155122 			    stp->sd_anchorzone != crgetzoneid(crp)) {
4355f4b3ec61Sdh155122 				mutex_enter(&stp->sd_lock);
4356f4b3ec61Sdh155122 				strendplumb(stp);
4357f4b3ec61Sdh155122 				mutex_exit(&stp->sd_lock);
4358f4b3ec61Sdh155122 				return (EPERM);
4359f4b3ec61Sdh155122 			}
4360f4b3ec61Sdh155122 		}
4361f4b3ec61Sdh155122 
4362f4b3ec61Sdh155122 
43637c478bd9Sstevel@tonic-gate 		ASSERT(!(q->q_flag & QREADR));
43647c478bd9Sstevel@tonic-gate 		qdetach(_RD(q), 1, flag, crp, is_remove);
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
43677c478bd9Sstevel@tonic-gate 
43687c478bd9Sstevel@tonic-gate 		/*
43697c478bd9Sstevel@tonic-gate 		 * As a performance concern we are caching the values of
43707c478bd9Sstevel@tonic-gate 		 * q_minpsz and q_maxpsz of the module below the stream
43717c478bd9Sstevel@tonic-gate 		 * head in the stream head.
43727c478bd9Sstevel@tonic-gate 		 */
43737c478bd9Sstevel@tonic-gate 		if (!is_remove) {
43747c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(wrq->q_next));
43757c478bd9Sstevel@tonic-gate 			rmin = wrq->q_next->q_minpsz;
43767c478bd9Sstevel@tonic-gate 			rmax = wrq->q_next->q_maxpsz;
43777c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(wrq->q_next));
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate 			/* Do this processing here as a performance concern */
43807c478bd9Sstevel@tonic-gate 			if (strmsgsz != 0) {
43817c478bd9Sstevel@tonic-gate 				if (rmax == INFPSZ)
43827c478bd9Sstevel@tonic-gate 					rmax = strmsgsz;
43837c478bd9Sstevel@tonic-gate 				else  {
43847c478bd9Sstevel@tonic-gate 					if (vp->v_type == VFIFO)
43857c478bd9Sstevel@tonic-gate 						rmax = MIN(PIPE_BUF, rmax);
43867c478bd9Sstevel@tonic-gate 					else	rmax = MIN(strmsgsz, rmax);
43877c478bd9Sstevel@tonic-gate 				}
43887c478bd9Sstevel@tonic-gate 			}
43897c478bd9Sstevel@tonic-gate 
43907c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(wrq));
43917c478bd9Sstevel@tonic-gate 			stp->sd_qn_minpsz = rmin;
43927c478bd9Sstevel@tonic-gate 			stp->sd_qn_maxpsz = rmax;
43937c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(wrq));
43947c478bd9Sstevel@tonic-gate 		}
43957c478bd9Sstevel@tonic-gate 
43967c478bd9Sstevel@tonic-gate 		/*
43977c478bd9Sstevel@tonic-gate 		 * Need to update the anchor value if this module is removed
43987c478bd9Sstevel@tonic-gate 		 * at or below the anchor point.  If the removed module is at
43997c478bd9Sstevel@tonic-gate 		 * the anchor point, remove the anchor for this stream if
44007c478bd9Sstevel@tonic-gate 		 * there is no module above the anchor point.  Otherwise, if
44017c478bd9Sstevel@tonic-gate 		 * the removed module is below the anchor point, decrement the
44027c478bd9Sstevel@tonic-gate 		 * anchor point by 1.
44037c478bd9Sstevel@tonic-gate 		 */
44047c478bd9Sstevel@tonic-gate 		if (stp->sd_anchor != 0) {
44057c478bd9Sstevel@tonic-gate 			pos = STRUCT_FGET(strmodremove, pos);
4406f4b3ec61Sdh155122 			if (pos == stp->sd_pushcnt - stp->sd_anchor + 1)
44077c478bd9Sstevel@tonic-gate 				stp->sd_anchor = 0;
44087c478bd9Sstevel@tonic-gate 			else if (pos > (stp->sd_pushcnt - stp->sd_anchor + 1))
44097c478bd9Sstevel@tonic-gate 				stp->sd_anchor--;
44107c478bd9Sstevel@tonic-gate 		}
44117c478bd9Sstevel@tonic-gate 
44127c478bd9Sstevel@tonic-gate 		strendplumb(stp);
44137c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
44147c478bd9Sstevel@tonic-gate 		return (0);
44157c478bd9Sstevel@tonic-gate 	}
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate 	case I_ANCHOR:
44187c478bd9Sstevel@tonic-gate 		/*
44197c478bd9Sstevel@tonic-gate 		 * Set the anchor position on the stream to reside at
44207c478bd9Sstevel@tonic-gate 		 * the top module (in other words, the top module
44217c478bd9Sstevel@tonic-gate 		 * cannot be popped).  Anchors with a FIFO make no
44227c478bd9Sstevel@tonic-gate 		 * obvious sense, so they're not allowed.
44237c478bd9Sstevel@tonic-gate 		 */
44247c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
44257c478bd9Sstevel@tonic-gate 
44267c478bd9Sstevel@tonic-gate 		if (stp->sd_vnode->v_type == VFIFO) {
44277c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
44287c478bd9Sstevel@tonic-gate 			return (EINVAL);
44297c478bd9Sstevel@tonic-gate 		}
4430f4b3ec61Sdh155122 		/* Only allow the same zoneid to update the anchor */
4431f4b3ec61Sdh155122 		if (stp->sd_anchor != 0 &&
4432f4b3ec61Sdh155122 		    stp->sd_anchorzone != crgetzoneid(crp)) {
4433f4b3ec61Sdh155122 			mutex_exit(&stp->sd_lock);
4434f4b3ec61Sdh155122 			return (EINVAL);
4435f4b3ec61Sdh155122 		}
44367c478bd9Sstevel@tonic-gate 		stp->sd_anchor = stp->sd_pushcnt;
4437f4b3ec61Sdh155122 		stp->sd_anchorzone = crgetzoneid(crp);
44387c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
44397c478bd9Sstevel@tonic-gate 		return (0);
44407c478bd9Sstevel@tonic-gate 
44417c478bd9Sstevel@tonic-gate 	case I_LOOK:
44427c478bd9Sstevel@tonic-gate 		/*
44437c478bd9Sstevel@tonic-gate 		 * Get name of first module downstream.
44447c478bd9Sstevel@tonic-gate 		 * If no module, return an error.
44457c478bd9Sstevel@tonic-gate 		 */
44467c478bd9Sstevel@tonic-gate 	{
44477c478bd9Sstevel@tonic-gate 		claimstr(wrq);
44487c478bd9Sstevel@tonic-gate 		if (_SAMESTR(wrq) && wrq->q_next->q_next) {
44497c478bd9Sstevel@tonic-gate 			char *name = wrq->q_next->q_qinfo->qi_minfo->mi_idname;
44507c478bd9Sstevel@tonic-gate 			error = strcopyout(name, (void *)arg, strlen(name) + 1,
44517c478bd9Sstevel@tonic-gate 			    copyflag);
44527c478bd9Sstevel@tonic-gate 			releasestr(wrq);
44537c478bd9Sstevel@tonic-gate 			return (error);
44547c478bd9Sstevel@tonic-gate 		}
44557c478bd9Sstevel@tonic-gate 		releasestr(wrq);
44567c478bd9Sstevel@tonic-gate 		return (EINVAL);
44577c478bd9Sstevel@tonic-gate 	}
44587c478bd9Sstevel@tonic-gate 
44597c478bd9Sstevel@tonic-gate 	case I_LINK:
44607c478bd9Sstevel@tonic-gate 	case I_PLINK:
44617c478bd9Sstevel@tonic-gate 		/*
44627c478bd9Sstevel@tonic-gate 		 * Link a multiplexor.
44637c478bd9Sstevel@tonic-gate 		 */
4464f4b3ec61Sdh155122 		error = mlink(vp, cmd, (int)arg, crp, rvalp, 0);
4465f4b3ec61Sdh155122 		return (error);
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate 	case _I_PLINK_LH:
44687c478bd9Sstevel@tonic-gate 		/*
44697c478bd9Sstevel@tonic-gate 		 * Link a multiplexor: Call must originate from kernel.
44707c478bd9Sstevel@tonic-gate 		 */
44717c478bd9Sstevel@tonic-gate 		if (kioctl)
44727c478bd9Sstevel@tonic-gate 			return (ldi_mlink_lh(vp, cmd, arg, crp, rvalp));
44737c478bd9Sstevel@tonic-gate 
44747c478bd9Sstevel@tonic-gate 		return (EINVAL);
44757c478bd9Sstevel@tonic-gate 	case I_UNLINK:
44767c478bd9Sstevel@tonic-gate 	case I_PUNLINK:
44777c478bd9Sstevel@tonic-gate 		/*
44787c478bd9Sstevel@tonic-gate 		 * Unlink a multiplexor.
44797c478bd9Sstevel@tonic-gate 		 * If arg is -1, unlink all links for which this is the
44807c478bd9Sstevel@tonic-gate 		 * controlling stream.  Otherwise, arg is an index number
44817c478bd9Sstevel@tonic-gate 		 * for a link to be removed.
44827c478bd9Sstevel@tonic-gate 		 */
44837c478bd9Sstevel@tonic-gate 	{
44847c478bd9Sstevel@tonic-gate 		struct linkinfo *linkp;
44857c478bd9Sstevel@tonic-gate 		int native_arg = (int)arg;
44867c478bd9Sstevel@tonic-gate 		int type;
4487f4b3ec61Sdh155122 		netstack_t *ns;
4488f4b3ec61Sdh155122 		str_stack_t *ss;
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_STREAMS_FR,
44917c478bd9Sstevel@tonic-gate 		    TR_I_UNLINK, "I_UNLINK/I_PUNLINK:%p", stp);
44927c478bd9Sstevel@tonic-gate 		if (vp->v_type == VFIFO) {
44937c478bd9Sstevel@tonic-gate 			return (EINVAL);
44947c478bd9Sstevel@tonic-gate 		}
44957c478bd9Sstevel@tonic-gate 		if (cmd == I_UNLINK)
44967c478bd9Sstevel@tonic-gate 			type = LINKNORMAL;
44977c478bd9Sstevel@tonic-gate 		else	/* I_PUNLINK */
44987c478bd9Sstevel@tonic-gate 			type = LINKPERSIST;
44997c478bd9Sstevel@tonic-gate 		if (native_arg == 0) {
45007c478bd9Sstevel@tonic-gate 			return (EINVAL);
45017c478bd9Sstevel@tonic-gate 		}
4502f4b3ec61Sdh155122 		ns = netstack_find_by_cred(crp);
4503f4b3ec61Sdh155122 		ASSERT(ns != NULL);
4504f4b3ec61Sdh155122 		ss = ns->netstack_str;
4505f4b3ec61Sdh155122 		ASSERT(ss != NULL);
4506f4b3ec61Sdh155122 
45077c478bd9Sstevel@tonic-gate 		if (native_arg == MUXID_ALL)
4508f4b3ec61Sdh155122 			error = munlinkall(stp, type, crp, rvalp, ss);
45097c478bd9Sstevel@tonic-gate 		else {
45107c478bd9Sstevel@tonic-gate 			mutex_enter(&muxifier);
4511f4b3ec61Sdh155122 			if (!(linkp = findlinks(stp, (int)arg, type, ss))) {
45127c478bd9Sstevel@tonic-gate 				/* invalid user supplied index number */
45137c478bd9Sstevel@tonic-gate 				mutex_exit(&muxifier);
4514f4b3ec61Sdh155122 				netstack_rele(ss->ss_netstack);
45157c478bd9Sstevel@tonic-gate 				return (EINVAL);
45167c478bd9Sstevel@tonic-gate 			}
45177c478bd9Sstevel@tonic-gate 			/* munlink drops the muxifier lock */
4518f4b3ec61Sdh155122 			error = munlink(stp, linkp, type, crp, rvalp, ss);
45197c478bd9Sstevel@tonic-gate 		}
4520f4b3ec61Sdh155122 		netstack_rele(ss->ss_netstack);
45217c478bd9Sstevel@tonic-gate 		return (error);
45227c478bd9Sstevel@tonic-gate 	}
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 	case I_FLUSH:
45257c478bd9Sstevel@tonic-gate 		/*
45267c478bd9Sstevel@tonic-gate 		 * send a flush message downstream
45277c478bd9Sstevel@tonic-gate 		 * flush message can indicate
45287c478bd9Sstevel@tonic-gate 		 * FLUSHR - flush read queue
45297c478bd9Sstevel@tonic-gate 		 * FLUSHW - flush write queue
45307c478bd9Sstevel@tonic-gate 		 * FLUSHRW - flush read/write queue
45317c478bd9Sstevel@tonic-gate 		 */
45327c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
45337c478bd9Sstevel@tonic-gate 			return (ENXIO);
45347c478bd9Sstevel@tonic-gate 		if (arg & ~FLUSHRW)
45357c478bd9Sstevel@tonic-gate 			return (EINVAL);
45367c478bd9Sstevel@tonic-gate 
45377c478bd9Sstevel@tonic-gate 		for (;;) {
45387c478bd9Sstevel@tonic-gate 			if (putnextctl1(stp->sd_wrq, M_FLUSH, (int)arg)) {
45397c478bd9Sstevel@tonic-gate 				break;
45407c478bd9Sstevel@tonic-gate 			}
45417c478bd9Sstevel@tonic-gate 			if (error = strwaitbuf(1, BPRI_HI)) {
45427c478bd9Sstevel@tonic-gate 				return (error);
45437c478bd9Sstevel@tonic-gate 			}
45447c478bd9Sstevel@tonic-gate 		}
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 		/*
45477c478bd9Sstevel@tonic-gate 		 * Send down an unsupported ioctl and wait for the nack
45487c478bd9Sstevel@tonic-gate 		 * in order to allow the M_FLUSH to propagate back
45497c478bd9Sstevel@tonic-gate 		 * up to the stream head.
45507c478bd9Sstevel@tonic-gate 		 * Replaces if (qready()) runqueues();
45517c478bd9Sstevel@tonic-gate 		 */
45527c478bd9Sstevel@tonic-gate 		strioc.ic_cmd = -1;	/* The unsupported ioctl */
45537c478bd9Sstevel@tonic-gate 		strioc.ic_timout = 0;
45547c478bd9Sstevel@tonic-gate 		strioc.ic_len = 0;
45557c478bd9Sstevel@tonic-gate 		strioc.ic_dp = NULL;
45567c478bd9Sstevel@tonic-gate 		(void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
45577c478bd9Sstevel@tonic-gate 		*rvalp = 0;
45587c478bd9Sstevel@tonic-gate 		return (0);
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate 	case I_FLUSHBAND:
45617c478bd9Sstevel@tonic-gate 	{
45627c478bd9Sstevel@tonic-gate 		struct bandinfo binfo;
45637c478bd9Sstevel@tonic-gate 
45647c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, &binfo, sizeof (binfo),
45657c478bd9Sstevel@tonic-gate 		    copyflag);
45667c478bd9Sstevel@tonic-gate 		if (error)
45677c478bd9Sstevel@tonic-gate 			return (error);
45687c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
45697c478bd9Sstevel@tonic-gate 			return (ENXIO);
45707c478bd9Sstevel@tonic-gate 		if (binfo.bi_flag & ~FLUSHRW)
45717c478bd9Sstevel@tonic-gate 			return (EINVAL);
45727c478bd9Sstevel@tonic-gate 		while (!(mp = allocb(2, BPRI_HI))) {
45737c478bd9Sstevel@tonic-gate 			if (error = strwaitbuf(2, BPRI_HI))
45747c478bd9Sstevel@tonic-gate 				return (error);
45757c478bd9Sstevel@tonic-gate 		}
45767c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_FLUSH;
45777c478bd9Sstevel@tonic-gate 		*mp->b_wptr++ = binfo.bi_flag | FLUSHBAND;
45787c478bd9Sstevel@tonic-gate 		*mp->b_wptr++ = binfo.bi_pri;
45797c478bd9Sstevel@tonic-gate 		putnext(stp->sd_wrq, mp);
45807c478bd9Sstevel@tonic-gate 		/*
45817c478bd9Sstevel@tonic-gate 		 * Send down an unsupported ioctl and wait for the nack
45827c478bd9Sstevel@tonic-gate 		 * in order to allow the M_FLUSH to propagate back
45837c478bd9Sstevel@tonic-gate 		 * up to the stream head.
45847c478bd9Sstevel@tonic-gate 		 * Replaces if (qready()) runqueues();
45857c478bd9Sstevel@tonic-gate 		 */
45867c478bd9Sstevel@tonic-gate 		strioc.ic_cmd = -1;	/* The unsupported ioctl */
45877c478bd9Sstevel@tonic-gate 		strioc.ic_timout = 0;
45887c478bd9Sstevel@tonic-gate 		strioc.ic_len = 0;
45897c478bd9Sstevel@tonic-gate 		strioc.ic_dp = NULL;
45907c478bd9Sstevel@tonic-gate 		(void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
45917c478bd9Sstevel@tonic-gate 		*rvalp = 0;
45927c478bd9Sstevel@tonic-gate 		return (0);
45937c478bd9Sstevel@tonic-gate 	}
45947c478bd9Sstevel@tonic-gate 
45957c478bd9Sstevel@tonic-gate 	case I_SRDOPT:
45967c478bd9Sstevel@tonic-gate 		/*
45977c478bd9Sstevel@tonic-gate 		 * Set read options
45987c478bd9Sstevel@tonic-gate 		 *
45997c478bd9Sstevel@tonic-gate 		 * RNORM - default stream mode
46007c478bd9Sstevel@tonic-gate 		 * RMSGN - message no discard
46017c478bd9Sstevel@tonic-gate 		 * RMSGD - message discard
46027c478bd9Sstevel@tonic-gate 		 * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
46037c478bd9Sstevel@tonic-gate 		 * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
46047c478bd9Sstevel@tonic-gate 		 * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
46057c478bd9Sstevel@tonic-gate 		 */
46067c478bd9Sstevel@tonic-gate 		if (arg & ~(RMODEMASK | RPROTMASK))
46077c478bd9Sstevel@tonic-gate 			return (EINVAL);
46087c478bd9Sstevel@tonic-gate 
46097c478bd9Sstevel@tonic-gate 		if ((arg & (RMSGD|RMSGN)) == (RMSGD|RMSGN))
46107c478bd9Sstevel@tonic-gate 			return (EINVAL);
46117c478bd9Sstevel@tonic-gate 
46127c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
46137c478bd9Sstevel@tonic-gate 		switch (arg & RMODEMASK) {
46147c478bd9Sstevel@tonic-gate 		case RNORM:
46157c478bd9Sstevel@tonic-gate 			stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
46167c478bd9Sstevel@tonic-gate 			break;
46177c478bd9Sstevel@tonic-gate 		case RMSGD:
46187c478bd9Sstevel@tonic-gate 			stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGNODIS) |
46197c478bd9Sstevel@tonic-gate 			    RD_MSGDIS;
46207c478bd9Sstevel@tonic-gate 			break;
46217c478bd9Sstevel@tonic-gate 		case RMSGN:
46227c478bd9Sstevel@tonic-gate 			stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGDIS) |
46237c478bd9Sstevel@tonic-gate 			    RD_MSGNODIS;
46247c478bd9Sstevel@tonic-gate 			break;
46257c478bd9Sstevel@tonic-gate 		}
46267c478bd9Sstevel@tonic-gate 
46277c478bd9Sstevel@tonic-gate 		switch (arg & RPROTMASK) {
46287c478bd9Sstevel@tonic-gate 		case RPROTNORM:
46297c478bd9Sstevel@tonic-gate 			stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
46307c478bd9Sstevel@tonic-gate 			break;
46317c478bd9Sstevel@tonic-gate 
46327c478bd9Sstevel@tonic-gate 		case RPROTDAT:
46337c478bd9Sstevel@tonic-gate 			stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDIS) |
46347c478bd9Sstevel@tonic-gate 			    RD_PROTDAT);
46357c478bd9Sstevel@tonic-gate 			break;
46367c478bd9Sstevel@tonic-gate 
46377c478bd9Sstevel@tonic-gate 		case RPROTDIS:
46387c478bd9Sstevel@tonic-gate 			stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDAT) |
46397c478bd9Sstevel@tonic-gate 			    RD_PROTDIS);
46407c478bd9Sstevel@tonic-gate 			break;
46417c478bd9Sstevel@tonic-gate 		}
46427c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
46437c478bd9Sstevel@tonic-gate 		return (0);
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 	case I_GRDOPT:
46467c478bd9Sstevel@tonic-gate 		/*
46477c478bd9Sstevel@tonic-gate 		 * Get read option and return the value
46487c478bd9Sstevel@tonic-gate 		 * to spot pointed to by arg
46497c478bd9Sstevel@tonic-gate 		 */
46507c478bd9Sstevel@tonic-gate 	{
46517c478bd9Sstevel@tonic-gate 		int rdopt;
46527c478bd9Sstevel@tonic-gate 
46537c478bd9Sstevel@tonic-gate 		rdopt = ((stp->sd_read_opt & RD_MSGDIS) ? RMSGD :
46547c478bd9Sstevel@tonic-gate 		    ((stp->sd_read_opt & RD_MSGNODIS) ? RMSGN : RNORM));
46557c478bd9Sstevel@tonic-gate 		rdopt |= ((stp->sd_read_opt & RD_PROTDAT) ? RPROTDAT :
46567c478bd9Sstevel@tonic-gate 		    ((stp->sd_read_opt & RD_PROTDIS) ? RPROTDIS : RPROTNORM));
46577c478bd9Sstevel@tonic-gate 
46587c478bd9Sstevel@tonic-gate 		return (strcopyout(&rdopt, (void *)arg, sizeof (int),
46597c478bd9Sstevel@tonic-gate 		    copyflag));
46607c478bd9Sstevel@tonic-gate 	}
46617c478bd9Sstevel@tonic-gate 
46627c478bd9Sstevel@tonic-gate 	case I_SERROPT:
46637c478bd9Sstevel@tonic-gate 		/*
46647c478bd9Sstevel@tonic-gate 		 * Set error options
46657c478bd9Sstevel@tonic-gate 		 *
46667c478bd9Sstevel@tonic-gate 		 * RERRNORM - persistent read errors
46677c478bd9Sstevel@tonic-gate 		 * RERRNONPERSIST - non-persistent read errors
46687c478bd9Sstevel@tonic-gate 		 * WERRNORM - persistent write errors
46697c478bd9Sstevel@tonic-gate 		 * WERRNONPERSIST - non-persistent write errors
46707c478bd9Sstevel@tonic-gate 		 */
46717c478bd9Sstevel@tonic-gate 		if (arg & ~(RERRMASK | WERRMASK))
46727c478bd9Sstevel@tonic-gate 			return (EINVAL);
46737c478bd9Sstevel@tonic-gate 
46747c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
46757c478bd9Sstevel@tonic-gate 		switch (arg & RERRMASK) {
46767c478bd9Sstevel@tonic-gate 		case RERRNORM:
46777c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRDERRNONPERSIST;
46787c478bd9Sstevel@tonic-gate 			break;
46797c478bd9Sstevel@tonic-gate 		case RERRNONPERSIST:
46807c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRDERRNONPERSIST;
46817c478bd9Sstevel@tonic-gate 			break;
46827c478bd9Sstevel@tonic-gate 		}
46837c478bd9Sstevel@tonic-gate 		switch (arg & WERRMASK) {
46847c478bd9Sstevel@tonic-gate 		case WERRNORM:
46857c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STWRERRNONPERSIST;
46867c478bd9Sstevel@tonic-gate 			break;
46877c478bd9Sstevel@tonic-gate 		case WERRNONPERSIST:
46887c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STWRERRNONPERSIST;
46897c478bd9Sstevel@tonic-gate 			break;
46907c478bd9Sstevel@tonic-gate 		}
46917c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
46927c478bd9Sstevel@tonic-gate 		return (0);
46937c478bd9Sstevel@tonic-gate 
46947c478bd9Sstevel@tonic-gate 	case I_GERROPT:
46957c478bd9Sstevel@tonic-gate 		/*
46967c478bd9Sstevel@tonic-gate 		 * Get error option and return the value
46977c478bd9Sstevel@tonic-gate 		 * to spot pointed to by arg
46987c478bd9Sstevel@tonic-gate 		 */
46997c478bd9Sstevel@tonic-gate 	{
47007c478bd9Sstevel@tonic-gate 		int erropt = 0;
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate 		erropt |= (stp->sd_flag & STRDERRNONPERSIST) ? RERRNONPERSIST :
47037c478bd9Sstevel@tonic-gate 		    RERRNORM;
47047c478bd9Sstevel@tonic-gate 		erropt |= (stp->sd_flag & STWRERRNONPERSIST) ? WERRNONPERSIST :
47057c478bd9Sstevel@tonic-gate 		    WERRNORM;
47067c478bd9Sstevel@tonic-gate 		return (strcopyout(&erropt, (void *)arg, sizeof (int),
47077c478bd9Sstevel@tonic-gate 		    copyflag));
47087c478bd9Sstevel@tonic-gate 	}
47097c478bd9Sstevel@tonic-gate 
47107c478bd9Sstevel@tonic-gate 	case I_SETSIG:
47117c478bd9Sstevel@tonic-gate 		/*
47127c478bd9Sstevel@tonic-gate 		 * Register the calling proc to receive the SIGPOLL
47137c478bd9Sstevel@tonic-gate 		 * signal based on the events given in arg.  If
47147c478bd9Sstevel@tonic-gate 		 * arg is zero, remove the proc from register list.
47157c478bd9Sstevel@tonic-gate 		 */
47167c478bd9Sstevel@tonic-gate 	{
47177c478bd9Sstevel@tonic-gate 		strsig_t *ssp, *pssp;
47187c478bd9Sstevel@tonic-gate 		struct pid *pidp;
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate 		pssp = NULL;
47217c478bd9Sstevel@tonic-gate 		pidp = curproc->p_pidp;
47227c478bd9Sstevel@tonic-gate 		/*
47237c478bd9Sstevel@tonic-gate 		 * Hold sd_lock to prevent traversal of sd_siglist while
47247c478bd9Sstevel@tonic-gate 		 * it is modified.
47257c478bd9Sstevel@tonic-gate 		 */
47267c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
47277c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp && (ssp->ss_pidp != pidp);
47287c478bd9Sstevel@tonic-gate 		    pssp = ssp, ssp = ssp->ss_next)
47297c478bd9Sstevel@tonic-gate 			;
47307c478bd9Sstevel@tonic-gate 
47317c478bd9Sstevel@tonic-gate 		if (arg) {
47327c478bd9Sstevel@tonic-gate 			if (arg & ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
47337c478bd9Sstevel@tonic-gate 			    S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
47347c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
47357c478bd9Sstevel@tonic-gate 				return (EINVAL);
47367c478bd9Sstevel@tonic-gate 			}
47377c478bd9Sstevel@tonic-gate 			if ((arg & S_BANDURG) && !(arg & S_RDBAND)) {
47387c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
47397c478bd9Sstevel@tonic-gate 				return (EINVAL);
47407c478bd9Sstevel@tonic-gate 			}
47417c478bd9Sstevel@tonic-gate 
47427c478bd9Sstevel@tonic-gate 			/*
47437c478bd9Sstevel@tonic-gate 			 * If proc not already registered, add it
47447c478bd9Sstevel@tonic-gate 			 * to list.
47457c478bd9Sstevel@tonic-gate 			 */
47467c478bd9Sstevel@tonic-gate 			if (!ssp) {
47477c478bd9Sstevel@tonic-gate 				ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
47487c478bd9Sstevel@tonic-gate 				ssp->ss_pidp = pidp;
47497c478bd9Sstevel@tonic-gate 				ssp->ss_pid = pidp->pid_id;
47507c478bd9Sstevel@tonic-gate 				ssp->ss_next = NULL;
47517c478bd9Sstevel@tonic-gate 				if (pssp)
47527c478bd9Sstevel@tonic-gate 					pssp->ss_next = ssp;
47537c478bd9Sstevel@tonic-gate 				else
47547c478bd9Sstevel@tonic-gate 					stp->sd_siglist = ssp;
47557c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
47567c478bd9Sstevel@tonic-gate 				PID_HOLD(pidp);
47577c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
47587c478bd9Sstevel@tonic-gate 			}
47597c478bd9Sstevel@tonic-gate 
47607c478bd9Sstevel@tonic-gate 			/*
47617c478bd9Sstevel@tonic-gate 			 * Set events.
47627c478bd9Sstevel@tonic-gate 			 */
47637c478bd9Sstevel@tonic-gate 			ssp->ss_events = (int)arg;
47647c478bd9Sstevel@tonic-gate 		} else {
47657c478bd9Sstevel@tonic-gate 			/*
47667c478bd9Sstevel@tonic-gate 			 * Remove proc from register list.
47677c478bd9Sstevel@tonic-gate 			 */
47687c478bd9Sstevel@tonic-gate 			if (ssp) {
47697c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
47707c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
47717c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
47727c478bd9Sstevel@tonic-gate 				if (pssp)
47737c478bd9Sstevel@tonic-gate 					pssp->ss_next = ssp->ss_next;
47747c478bd9Sstevel@tonic-gate 				else
47757c478bd9Sstevel@tonic-gate 					stp->sd_siglist = ssp->ss_next;
47767c478bd9Sstevel@tonic-gate 				kmem_free(ssp, sizeof (strsig_t));
47777c478bd9Sstevel@tonic-gate 			} else {
47787c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
47797c478bd9Sstevel@tonic-gate 				return (EINVAL);
47807c478bd9Sstevel@tonic-gate 			}
47817c478bd9Sstevel@tonic-gate 		}
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate 		/*
47847c478bd9Sstevel@tonic-gate 		 * Recalculate OR of sig events.
47857c478bd9Sstevel@tonic-gate 		 */
47867c478bd9Sstevel@tonic-gate 		stp->sd_sigflags = 0;
47877c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
47887c478bd9Sstevel@tonic-gate 			stp->sd_sigflags |= ssp->ss_events;
47897c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
47907c478bd9Sstevel@tonic-gate 		return (0);
47917c478bd9Sstevel@tonic-gate 	}
47927c478bd9Sstevel@tonic-gate 
47937c478bd9Sstevel@tonic-gate 	case I_GETSIG:
47947c478bd9Sstevel@tonic-gate 		/*
47957c478bd9Sstevel@tonic-gate 		 * Return (in arg) the current registration of events
47967c478bd9Sstevel@tonic-gate 		 * for which the calling proc is to be signaled.
47977c478bd9Sstevel@tonic-gate 		 */
47987c478bd9Sstevel@tonic-gate 	{
47997c478bd9Sstevel@tonic-gate 		struct strsig *ssp;
48007c478bd9Sstevel@tonic-gate 		struct pid  *pidp;
48017c478bd9Sstevel@tonic-gate 
48027c478bd9Sstevel@tonic-gate 		pidp = curproc->p_pidp;
48037c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
48047c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
48057c478bd9Sstevel@tonic-gate 			if (ssp->ss_pidp == pidp) {
48067c478bd9Sstevel@tonic-gate 				error = strcopyout(&ssp->ss_events, (void *)arg,
48077c478bd9Sstevel@tonic-gate 				    sizeof (int), copyflag);
48087c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
48097c478bd9Sstevel@tonic-gate 				return (error);
48107c478bd9Sstevel@tonic-gate 			}
48117c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
48127c478bd9Sstevel@tonic-gate 		return (EINVAL);
48137c478bd9Sstevel@tonic-gate 	}
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate 	case I_ESETSIG:
48167c478bd9Sstevel@tonic-gate 		/*
48177c478bd9Sstevel@tonic-gate 		 * Register the ss_pid to receive the SIGPOLL
48187c478bd9Sstevel@tonic-gate 		 * signal based on the events is ss_events arg.  If
48197c478bd9Sstevel@tonic-gate 		 * ss_events is zero, remove the proc from register list.
48207c478bd9Sstevel@tonic-gate 		 */
48217c478bd9Sstevel@tonic-gate 	{
48227c478bd9Sstevel@tonic-gate 		struct strsig *ssp, *pssp;
48237c478bd9Sstevel@tonic-gate 		struct proc *proc;
48247c478bd9Sstevel@tonic-gate 		struct pid  *pidp;
48257c478bd9Sstevel@tonic-gate 		pid_t pid;
48267c478bd9Sstevel@tonic-gate 		struct strsigset ss;
48277c478bd9Sstevel@tonic-gate 
48287c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
48297c478bd9Sstevel@tonic-gate 		if (error)
48307c478bd9Sstevel@tonic-gate 			return (error);
48317c478bd9Sstevel@tonic-gate 
48327c478bd9Sstevel@tonic-gate 		pid = ss.ss_pid;
48337c478bd9Sstevel@tonic-gate 
48347c478bd9Sstevel@tonic-gate 		if (ss.ss_events != 0) {
48357c478bd9Sstevel@tonic-gate 			/*
48367c478bd9Sstevel@tonic-gate 			 * Permissions check by sending signal 0.
48377c478bd9Sstevel@tonic-gate 			 * Note that when kill fails it does a set_errno
48387c478bd9Sstevel@tonic-gate 			 * causing the system call to fail.
48397c478bd9Sstevel@tonic-gate 			 */
48407c478bd9Sstevel@tonic-gate 			error = kill(pid, 0);
48417c478bd9Sstevel@tonic-gate 			if (error) {
48427c478bd9Sstevel@tonic-gate 				return (error);
48437c478bd9Sstevel@tonic-gate 			}
48447c478bd9Sstevel@tonic-gate 		}
48457c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
48467c478bd9Sstevel@tonic-gate 		if (pid == 0)
48477c478bd9Sstevel@tonic-gate 			proc = curproc;
48487c478bd9Sstevel@tonic-gate 		else if (pid < 0)
48497c478bd9Sstevel@tonic-gate 			proc = pgfind(-pid);
48507c478bd9Sstevel@tonic-gate 		else
48517c478bd9Sstevel@tonic-gate 			proc = prfind(pid);
48527c478bd9Sstevel@tonic-gate 		if (proc == NULL) {
48537c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
48547c478bd9Sstevel@tonic-gate 			return (ESRCH);
48557c478bd9Sstevel@tonic-gate 		}
48567c478bd9Sstevel@tonic-gate 		if (pid < 0)
48577c478bd9Sstevel@tonic-gate 			pidp = proc->p_pgidp;
48587c478bd9Sstevel@tonic-gate 		else
48597c478bd9Sstevel@tonic-gate 			pidp = proc->p_pidp;
48607c478bd9Sstevel@tonic-gate 		ASSERT(pidp);
48617c478bd9Sstevel@tonic-gate 		/*
48627c478bd9Sstevel@tonic-gate 		 * Get a hold on the pid structure while referencing it.
48637c478bd9Sstevel@tonic-gate 		 * There is a separate PID_HOLD should it be inserted
48647c478bd9Sstevel@tonic-gate 		 * in the list below.
48657c478bd9Sstevel@tonic-gate 		 */
48667c478bd9Sstevel@tonic-gate 		PID_HOLD(pidp);
48677c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 		pssp = NULL;
48707c478bd9Sstevel@tonic-gate 		/*
48717c478bd9Sstevel@tonic-gate 		 * Hold sd_lock to prevent traversal of sd_siglist while
48727c478bd9Sstevel@tonic-gate 		 * it is modified.
48737c478bd9Sstevel@tonic-gate 		 */
48747c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
48757c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp && (ssp->ss_pid != pid);
48767c478bd9Sstevel@tonic-gate 		    pssp = ssp, ssp = ssp->ss_next)
48777c478bd9Sstevel@tonic-gate 			;
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate 		if (ss.ss_events) {
48807c478bd9Sstevel@tonic-gate 			if (ss.ss_events &
48817c478bd9Sstevel@tonic-gate 			    ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
48827c478bd9Sstevel@tonic-gate 			    S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
48837c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
48847c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
48857c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
48867c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
48877c478bd9Sstevel@tonic-gate 				return (EINVAL);
48887c478bd9Sstevel@tonic-gate 			}
48897c478bd9Sstevel@tonic-gate 			if ((ss.ss_events & S_BANDURG) &&
48907c478bd9Sstevel@tonic-gate 			    !(ss.ss_events & S_RDBAND)) {
48917c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
48927c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
48937c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
48947c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
48957c478bd9Sstevel@tonic-gate 				return (EINVAL);
48967c478bd9Sstevel@tonic-gate 			}
48977c478bd9Sstevel@tonic-gate 
48987c478bd9Sstevel@tonic-gate 			/*
48997c478bd9Sstevel@tonic-gate 			 * If proc not already registered, add it
49007c478bd9Sstevel@tonic-gate 			 * to list.
49017c478bd9Sstevel@tonic-gate 			 */
49027c478bd9Sstevel@tonic-gate 			if (!ssp) {
49037c478bd9Sstevel@tonic-gate 				ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
49047c478bd9Sstevel@tonic-gate 				ssp->ss_pidp = pidp;
49057c478bd9Sstevel@tonic-gate 				ssp->ss_pid = pid;
49067c478bd9Sstevel@tonic-gate 				ssp->ss_next = NULL;
49077c478bd9Sstevel@tonic-gate 				if (pssp)
49087c478bd9Sstevel@tonic-gate 					pssp->ss_next = ssp;
49097c478bd9Sstevel@tonic-gate 				else
49107c478bd9Sstevel@tonic-gate 					stp->sd_siglist = ssp;
49117c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
49127c478bd9Sstevel@tonic-gate 				PID_HOLD(pidp);
49137c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
49147c478bd9Sstevel@tonic-gate 			}
49157c478bd9Sstevel@tonic-gate 
49167c478bd9Sstevel@tonic-gate 			/*
49177c478bd9Sstevel@tonic-gate 			 * Set events.
49187c478bd9Sstevel@tonic-gate 			 */
49197c478bd9Sstevel@tonic-gate 			ssp->ss_events = ss.ss_events;
49207c478bd9Sstevel@tonic-gate 		} else {
49217c478bd9Sstevel@tonic-gate 			/*
49227c478bd9Sstevel@tonic-gate 			 * Remove proc from register list.
49237c478bd9Sstevel@tonic-gate 			 */
49247c478bd9Sstevel@tonic-gate 			if (ssp) {
49257c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
49267c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
49277c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
49287c478bd9Sstevel@tonic-gate 				if (pssp)
49297c478bd9Sstevel@tonic-gate 					pssp->ss_next = ssp->ss_next;
49307c478bd9Sstevel@tonic-gate 				else
49317c478bd9Sstevel@tonic-gate 					stp->sd_siglist = ssp->ss_next;
49327c478bd9Sstevel@tonic-gate 				kmem_free(ssp, sizeof (strsig_t));
49337c478bd9Sstevel@tonic-gate 			} else {
49347c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
49357c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
49367c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
49377c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
49387c478bd9Sstevel@tonic-gate 				return (EINVAL);
49397c478bd9Sstevel@tonic-gate 			}
49407c478bd9Sstevel@tonic-gate 		}
49417c478bd9Sstevel@tonic-gate 
49427c478bd9Sstevel@tonic-gate 		/*
49437c478bd9Sstevel@tonic-gate 		 * Recalculate OR of sig events.
49447c478bd9Sstevel@tonic-gate 		 */
49457c478bd9Sstevel@tonic-gate 		stp->sd_sigflags = 0;
49467c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
49477c478bd9Sstevel@tonic-gate 			stp->sd_sigflags |= ssp->ss_events;
49487c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
49497c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
49507c478bd9Sstevel@tonic-gate 		PID_RELE(pidp);
49517c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
49527c478bd9Sstevel@tonic-gate 		return (0);
49537c478bd9Sstevel@tonic-gate 	}
49547c478bd9Sstevel@tonic-gate 
49557c478bd9Sstevel@tonic-gate 	case I_EGETSIG:
49567c478bd9Sstevel@tonic-gate 		/*
49577c478bd9Sstevel@tonic-gate 		 * Return (in arg) the current registration of events
49587c478bd9Sstevel@tonic-gate 		 * for which the calling proc is to be signaled.
49597c478bd9Sstevel@tonic-gate 		 */
49607c478bd9Sstevel@tonic-gate 	{
49617c478bd9Sstevel@tonic-gate 		struct strsig *ssp;
49627c478bd9Sstevel@tonic-gate 		struct proc *proc;
49637c478bd9Sstevel@tonic-gate 		pid_t pid;
49647c478bd9Sstevel@tonic-gate 		struct pid  *pidp;
49657c478bd9Sstevel@tonic-gate 		struct strsigset ss;
49667c478bd9Sstevel@tonic-gate 
49677c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
49687c478bd9Sstevel@tonic-gate 		if (error)
49697c478bd9Sstevel@tonic-gate 			return (error);
49707c478bd9Sstevel@tonic-gate 
49717c478bd9Sstevel@tonic-gate 		pid = ss.ss_pid;
49727c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
49737c478bd9Sstevel@tonic-gate 		if (pid == 0)
49747c478bd9Sstevel@tonic-gate 			proc = curproc;
49757c478bd9Sstevel@tonic-gate 		else if (pid < 0)
49767c478bd9Sstevel@tonic-gate 			proc = pgfind(-pid);
49777c478bd9Sstevel@tonic-gate 		else
49787c478bd9Sstevel@tonic-gate 			proc = prfind(pid);
49797c478bd9Sstevel@tonic-gate 		if (proc == NULL) {
49807c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
49817c478bd9Sstevel@tonic-gate 			return (ESRCH);
49827c478bd9Sstevel@tonic-gate 		}
49837c478bd9Sstevel@tonic-gate 		if (pid < 0)
49847c478bd9Sstevel@tonic-gate 			pidp = proc->p_pgidp;
49857c478bd9Sstevel@tonic-gate 		else
49867c478bd9Sstevel@tonic-gate 			pidp = proc->p_pidp;
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate 		/* Prevent the pidp from being reassigned */
49897c478bd9Sstevel@tonic-gate 		PID_HOLD(pidp);
49907c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
49937c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
49947c478bd9Sstevel@tonic-gate 			if (ssp->ss_pid == pid) {
49957c478bd9Sstevel@tonic-gate 				ss.ss_pid = ssp->ss_pid;
49967c478bd9Sstevel@tonic-gate 				ss.ss_events = ssp->ss_events;
49977c478bd9Sstevel@tonic-gate 				error = strcopyout(&ss, (void *)arg,
49987c478bd9Sstevel@tonic-gate 				    sizeof (struct strsigset), copyflag);
49997c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
50007c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
50017c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
50027c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
50037c478bd9Sstevel@tonic-gate 				return (error);
50047c478bd9Sstevel@tonic-gate 			}
50057c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
50067c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
50077c478bd9Sstevel@tonic-gate 		PID_RELE(pidp);
50087c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
50097c478bd9Sstevel@tonic-gate 		return (EINVAL);
50107c478bd9Sstevel@tonic-gate 	}
50117c478bd9Sstevel@tonic-gate 
50127c478bd9Sstevel@tonic-gate 	case I_PEEK:
50137c478bd9Sstevel@tonic-gate 	{
50147c478bd9Sstevel@tonic-gate 		STRUCT_DECL(strpeek, strpeek);
50157c478bd9Sstevel@tonic-gate 		size_t n;
50167c478bd9Sstevel@tonic-gate 		mblk_t *fmp, *tmp_mp = NULL;
50177c478bd9Sstevel@tonic-gate 
50187c478bd9Sstevel@tonic-gate 		STRUCT_INIT(strpeek, flag);
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, STRUCT_BUF(strpeek),
50217c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strpeek), copyflag);
50227c478bd9Sstevel@tonic-gate 		if (error)
50237c478bd9Sstevel@tonic-gate 			return (error);
50247c478bd9Sstevel@tonic-gate 
50257c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rdq));
50267c478bd9Sstevel@tonic-gate 		/*
50277c478bd9Sstevel@tonic-gate 		 * Skip the invalid messages
50287c478bd9Sstevel@tonic-gate 		 */
50297c478bd9Sstevel@tonic-gate 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
50307c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type != M_SIG)
50317c478bd9Sstevel@tonic-gate 				break;
50327c478bd9Sstevel@tonic-gate 
50337c478bd9Sstevel@tonic-gate 		/*
50347c478bd9Sstevel@tonic-gate 		 * If user has requested to peek at a high priority message
50357c478bd9Sstevel@tonic-gate 		 * and first message is not, return 0
50367c478bd9Sstevel@tonic-gate 		 */
50377c478bd9Sstevel@tonic-gate 		if (mp != NULL) {
50387c478bd9Sstevel@tonic-gate 			if ((STRUCT_FGET(strpeek, flags) & RS_HIPRI) &&
50397c478bd9Sstevel@tonic-gate 			    queclass(mp) == QNORM) {
50407c478bd9Sstevel@tonic-gate 				*rvalp = 0;
50417c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(rdq));
50427c478bd9Sstevel@tonic-gate 				return (0);
50437c478bd9Sstevel@tonic-gate 			}
50447c478bd9Sstevel@tonic-gate 		} else if (stp->sd_struiordq == NULL ||
50457c478bd9Sstevel@tonic-gate 		    (STRUCT_FGET(strpeek, flags) & RS_HIPRI)) {
50467c478bd9Sstevel@tonic-gate 			/*
50477c478bd9Sstevel@tonic-gate 			 * No mblks to look at at the streamhead and
50487c478bd9Sstevel@tonic-gate 			 * 1). This isn't a synch stream or
50497c478bd9Sstevel@tonic-gate 			 * 2). This is a synch stream but caller wants high
50507c478bd9Sstevel@tonic-gate 			 *	priority messages which is not supported by
50517c478bd9Sstevel@tonic-gate 			 *	the synch stream. (it only supports QNORM)
50527c478bd9Sstevel@tonic-gate 			 */
50537c478bd9Sstevel@tonic-gate 			*rvalp = 0;
50547c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(rdq));
50557c478bd9Sstevel@tonic-gate 			return (0);
50567c478bd9Sstevel@tonic-gate 		}
50577c478bd9Sstevel@tonic-gate 
50587c478bd9Sstevel@tonic-gate 		fmp = mp;
50597c478bd9Sstevel@tonic-gate 
50607c478bd9Sstevel@tonic-gate 		if (mp && mp->b_datap->db_type == M_PASSFP) {
50617c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(rdq));
50627c478bd9Sstevel@tonic-gate 			return (EBADMSG);
50637c478bd9Sstevel@tonic-gate 		}
50647c478bd9Sstevel@tonic-gate 
50657c478bd9Sstevel@tonic-gate 		ASSERT(mp == NULL || mp->b_datap->db_type == M_PCPROTO ||
50667c478bd9Sstevel@tonic-gate 		    mp->b_datap->db_type == M_PROTO ||
50677c478bd9Sstevel@tonic-gate 		    mp->b_datap->db_type == M_DATA);
50687c478bd9Sstevel@tonic-gate 
50697c478bd9Sstevel@tonic-gate 		if (mp && mp->b_datap->db_type == M_PCPROTO) {
50707c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, flags, RS_HIPRI);
50717c478bd9Sstevel@tonic-gate 		} else {
50727c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, flags, 0);
50737c478bd9Sstevel@tonic-gate 		}
50747c478bd9Sstevel@tonic-gate 
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate 		if (mp && ((tmp_mp = dupmsg(mp)) == NULL)) {
50777c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(rdq));
50787c478bd9Sstevel@tonic-gate 			return (ENOSR);
50797c478bd9Sstevel@tonic-gate 		}
50807c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rdq));
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate 		/*
50837c478bd9Sstevel@tonic-gate 		 * set mp = tmp_mp, so that I_PEEK processing can continue.
50847c478bd9Sstevel@tonic-gate 		 * tmp_mp is used to free the dup'd message.
50857c478bd9Sstevel@tonic-gate 		 */
50867c478bd9Sstevel@tonic-gate 		mp = tmp_mp;
50877c478bd9Sstevel@tonic-gate 
50887c478bd9Sstevel@tonic-gate 		uio.uio_fmode = 0;
50897c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_CACHED;
50907c478bd9Sstevel@tonic-gate 		uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
50917c478bd9Sstevel@tonic-gate 		    UIO_SYSSPACE;
50927c478bd9Sstevel@tonic-gate 		uio.uio_limit = 0;
50937c478bd9Sstevel@tonic-gate 		/*
50947c478bd9Sstevel@tonic-gate 		 * First process PROTO blocks, if any.
50957c478bd9Sstevel@tonic-gate 		 * If user doesn't want to get ctl info by setting maxlen <= 0,
50967c478bd9Sstevel@tonic-gate 		 * then set len to -1/0 and skip control blocks part.
50977c478bd9Sstevel@tonic-gate 		 */
50987c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strpeek, ctlbuf.maxlen) < 0)
50997c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, ctlbuf.len, -1);
51007c478bd9Sstevel@tonic-gate 		else if (STRUCT_FGET(strpeek, ctlbuf.maxlen) == 0)
51017c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, ctlbuf.len, 0);
51027c478bd9Sstevel@tonic-gate 		else {
51037c478bd9Sstevel@tonic-gate 			int	ctl_part = 0;
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate 			iov.iov_base = STRUCT_FGETP(strpeek, ctlbuf.buf);
51067c478bd9Sstevel@tonic-gate 			iov.iov_len = STRUCT_FGET(strpeek, ctlbuf.maxlen);
51077c478bd9Sstevel@tonic-gate 			uio.uio_iov = &iov;
51087c478bd9Sstevel@tonic-gate 			uio.uio_resid = iov.iov_len;
51097c478bd9Sstevel@tonic-gate 			uio.uio_loffset = 0;
51107c478bd9Sstevel@tonic-gate 			uio.uio_iovcnt = 1;
51117c478bd9Sstevel@tonic-gate 			while (mp && mp->b_datap->db_type != M_DATA &&
51127c478bd9Sstevel@tonic-gate 			    uio.uio_resid >= 0) {
51137c478bd9Sstevel@tonic-gate 				ASSERT(STRUCT_FGET(strpeek, flags) == 0 ?
51147c478bd9Sstevel@tonic-gate 				    mp->b_datap->db_type == M_PROTO :
51157c478bd9Sstevel@tonic-gate 				    mp->b_datap->db_type == M_PCPROTO);
51167c478bd9Sstevel@tonic-gate 
51177c478bd9Sstevel@tonic-gate 				if ((n = MIN(uio.uio_resid,
51187c478bd9Sstevel@tonic-gate 				    mp->b_wptr - mp->b_rptr)) != 0 &&
51197c478bd9Sstevel@tonic-gate 				    (error = uiomove((char *)mp->b_rptr, n,
51207c478bd9Sstevel@tonic-gate 				    UIO_READ, &uio)) != 0) {
51217c478bd9Sstevel@tonic-gate 					freemsg(tmp_mp);
51227c478bd9Sstevel@tonic-gate 					return (error);
51237c478bd9Sstevel@tonic-gate 				}
51247c478bd9Sstevel@tonic-gate 				ctl_part = 1;
51257c478bd9Sstevel@tonic-gate 				mp = mp->b_cont;
51267c478bd9Sstevel@tonic-gate 			}
51277c478bd9Sstevel@tonic-gate 			/* No ctl message */
51287c478bd9Sstevel@tonic-gate 			if (ctl_part == 0)
51297c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, ctlbuf.len, -1);
51307c478bd9Sstevel@tonic-gate 			else
51317c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, ctlbuf.len,
51327c478bd9Sstevel@tonic-gate 				    STRUCT_FGET(strpeek, ctlbuf.maxlen) -
51337c478bd9Sstevel@tonic-gate 				    uio.uio_resid);
51347c478bd9Sstevel@tonic-gate 		}
51357c478bd9Sstevel@tonic-gate 
51367c478bd9Sstevel@tonic-gate 		/*
51377c478bd9Sstevel@tonic-gate 		 * Now process DATA blocks, if any.
51387c478bd9Sstevel@tonic-gate 		 * If user doesn't want to get data info by setting maxlen <= 0,
51397c478bd9Sstevel@tonic-gate 		 * then set len to -1/0 and skip data blocks part.
51407c478bd9Sstevel@tonic-gate 		 */
51417c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strpeek, databuf.maxlen) < 0)
51427c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, databuf.len, -1);
51437c478bd9Sstevel@tonic-gate 		else if (STRUCT_FGET(strpeek, databuf.maxlen) == 0)
51447c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, databuf.len, 0);
51457c478bd9Sstevel@tonic-gate 		else {
51467c478bd9Sstevel@tonic-gate 			int	data_part = 0;
51477c478bd9Sstevel@tonic-gate 
51487c478bd9Sstevel@tonic-gate 			iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
51497c478bd9Sstevel@tonic-gate 			iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen);
51507c478bd9Sstevel@tonic-gate 			uio.uio_iov = &iov;
51517c478bd9Sstevel@tonic-gate 			uio.uio_resid = iov.iov_len;
51527c478bd9Sstevel@tonic-gate 			uio.uio_loffset = 0;
51537c478bd9Sstevel@tonic-gate 			uio.uio_iovcnt = 1;
51547c478bd9Sstevel@tonic-gate 			while (mp && uio.uio_resid) {
51557c478bd9Sstevel@tonic-gate 				if (mp->b_datap->db_type == M_DATA) {
51567c478bd9Sstevel@tonic-gate 					if ((n = MIN(uio.uio_resid,
51577c478bd9Sstevel@tonic-gate 					    mp->b_wptr - mp->b_rptr)) != 0 &&
51587c478bd9Sstevel@tonic-gate 					    (error = uiomove((char *)mp->b_rptr,
51597c478bd9Sstevel@tonic-gate 					    n, UIO_READ, &uio)) != 0) {
51607c478bd9Sstevel@tonic-gate 						freemsg(tmp_mp);
51617c478bd9Sstevel@tonic-gate 						return (error);
51627c478bd9Sstevel@tonic-gate 					}
51637c478bd9Sstevel@tonic-gate 					data_part = 1;
51647c478bd9Sstevel@tonic-gate 				}
51657c478bd9Sstevel@tonic-gate 				ASSERT(data_part == 0 ||
51667c478bd9Sstevel@tonic-gate 				    mp->b_datap->db_type == M_DATA);
51677c478bd9Sstevel@tonic-gate 				mp = mp->b_cont;
51687c478bd9Sstevel@tonic-gate 			}
51697c478bd9Sstevel@tonic-gate 			/* No data message */
51707c478bd9Sstevel@tonic-gate 			if (data_part == 0)
51717c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, databuf.len, -1);
51727c478bd9Sstevel@tonic-gate 			else
51737c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, databuf.len,
51747c478bd9Sstevel@tonic-gate 				    STRUCT_FGET(strpeek, databuf.maxlen) -
51757c478bd9Sstevel@tonic-gate 				    uio.uio_resid);
51767c478bd9Sstevel@tonic-gate 		}
51777c478bd9Sstevel@tonic-gate 		freemsg(tmp_mp);
51787c478bd9Sstevel@tonic-gate 
51797c478bd9Sstevel@tonic-gate 		/*
51807c478bd9Sstevel@tonic-gate 		 * It is a synch stream and user wants to get
51817c478bd9Sstevel@tonic-gate 		 * data (maxlen > 0).
51827c478bd9Sstevel@tonic-gate 		 * uio setup is done by the codes that process DATA
51837c478bd9Sstevel@tonic-gate 		 * blocks above.
51847c478bd9Sstevel@tonic-gate 		 */
51857c478bd9Sstevel@tonic-gate 		if ((fmp == NULL) && STRUCT_FGET(strpeek, databuf.maxlen) > 0) {
51867c478bd9Sstevel@tonic-gate 			infod_t infod;
51877c478bd9Sstevel@tonic-gate 
51887c478bd9Sstevel@tonic-gate 			infod.d_cmd = INFOD_COPYOUT;
51897c478bd9Sstevel@tonic-gate 			infod.d_res = 0;
51907c478bd9Sstevel@tonic-gate 			infod.d_uiop = &uio;
51917c478bd9Sstevel@tonic-gate 			error = infonext(rdq, &infod);
51927c478bd9Sstevel@tonic-gate 			if (error == EINVAL || error == EBUSY)
51937c478bd9Sstevel@tonic-gate 				error = 0;
51947c478bd9Sstevel@tonic-gate 			if (error)
51957c478bd9Sstevel@tonic-gate 				return (error);
51967c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, databuf.len, STRUCT_FGET(strpeek,
51977c478bd9Sstevel@tonic-gate 			    databuf.maxlen) - uio.uio_resid);
51987c478bd9Sstevel@tonic-gate 			if (STRUCT_FGET(strpeek, databuf.len) == 0) {
51997c478bd9Sstevel@tonic-gate 				/*
52007c478bd9Sstevel@tonic-gate 				 * No data found by the infonext().
52017c478bd9Sstevel@tonic-gate 				 */
52027c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, databuf.len, -1);
52037c478bd9Sstevel@tonic-gate 			}
52047c478bd9Sstevel@tonic-gate 		}
52057c478bd9Sstevel@tonic-gate 		error = strcopyout(STRUCT_BUF(strpeek), (void *)arg,
52067c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strpeek), copyflag);
52077c478bd9Sstevel@tonic-gate 		if (error) {
52087c478bd9Sstevel@tonic-gate 			return (error);
52097c478bd9Sstevel@tonic-gate 		}
52107c478bd9Sstevel@tonic-gate 		/*
52117c478bd9Sstevel@tonic-gate 		 * If there is no message retrieved, set return code to 0
52127c478bd9Sstevel@tonic-gate 		 * otherwise, set it to 1.
52137c478bd9Sstevel@tonic-gate 		 */
52147c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strpeek, ctlbuf.len) == -1 &&
52157c478bd9Sstevel@tonic-gate 		    STRUCT_FGET(strpeek, databuf.len) == -1)
52167c478bd9Sstevel@tonic-gate 			*rvalp = 0;
52177c478bd9Sstevel@tonic-gate 		else
52187c478bd9Sstevel@tonic-gate 			*rvalp = 1;
52197c478bd9Sstevel@tonic-gate 		return (0);
52207c478bd9Sstevel@tonic-gate 	}
52217c478bd9Sstevel@tonic-gate 
52227c478bd9Sstevel@tonic-gate 	case I_FDINSERT:
52237c478bd9Sstevel@tonic-gate 	{
52247c478bd9Sstevel@tonic-gate 		STRUCT_DECL(strfdinsert, strfdinsert);
52257c478bd9Sstevel@tonic-gate 		struct file *resftp;
52267c478bd9Sstevel@tonic-gate 		struct stdata *resstp;
52277c478bd9Sstevel@tonic-gate 		t_uscalar_t	ival;
52287c478bd9Sstevel@tonic-gate 		ssize_t msgsize;
52297c478bd9Sstevel@tonic-gate 		struct strbuf mctl;
52307c478bd9Sstevel@tonic-gate 
52317c478bd9Sstevel@tonic-gate 		STRUCT_INIT(strfdinsert, flag);
52327c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
52337c478bd9Sstevel@tonic-gate 			return (ENXIO);
52347c478bd9Sstevel@tonic-gate 		/*
52357c478bd9Sstevel@tonic-gate 		 * STRDERR, STWRERR and STPLEX tested above.
52367c478bd9Sstevel@tonic-gate 		 */
52377c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, STRUCT_BUF(strfdinsert),
52387c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strfdinsert), copyflag);
52397c478bd9Sstevel@tonic-gate 		if (error)
52407c478bd9Sstevel@tonic-gate 			return (error);
52417c478bd9Sstevel@tonic-gate 
52427c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strfdinsert, offset) < 0 ||
52437c478bd9Sstevel@tonic-gate 		    (STRUCT_FGET(strfdinsert, offset) %
52447c478bd9Sstevel@tonic-gate 		    sizeof (t_uscalar_t)) != 0)
52457c478bd9Sstevel@tonic-gate 			return (EINVAL);
52467c478bd9Sstevel@tonic-gate 		if ((resftp = getf(STRUCT_FGET(strfdinsert, fildes))) != NULL) {
52477c478bd9Sstevel@tonic-gate 			if ((resstp = resftp->f_vnode->v_stream) == NULL) {
52487c478bd9Sstevel@tonic-gate 				releasef(STRUCT_FGET(strfdinsert, fildes));
52497c478bd9Sstevel@tonic-gate 				return (EINVAL);
52507c478bd9Sstevel@tonic-gate 			}
52517c478bd9Sstevel@tonic-gate 		} else
52527c478bd9Sstevel@tonic-gate 			return (EINVAL);
52537c478bd9Sstevel@tonic-gate 
52547c478bd9Sstevel@tonic-gate 		mutex_enter(&resstp->sd_lock);
52557c478bd9Sstevel@tonic-gate 		if (resstp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
52567c478bd9Sstevel@tonic-gate 			error = strgeterr(resstp,
52577c478bd9Sstevel@tonic-gate 			    STRDERR|STWRERR|STRHUP|STPLEX, 0);
52587c478bd9Sstevel@tonic-gate 			if (error != 0) {
52597c478bd9Sstevel@tonic-gate 				mutex_exit(&resstp->sd_lock);
52607c478bd9Sstevel@tonic-gate 				releasef(STRUCT_FGET(strfdinsert, fildes));
52617c478bd9Sstevel@tonic-gate 				return (error);
52627c478bd9Sstevel@tonic-gate 			}
52637c478bd9Sstevel@tonic-gate 		}
52647c478bd9Sstevel@tonic-gate 		mutex_exit(&resstp->sd_lock);
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate #ifdef	_ILP32
52677c478bd9Sstevel@tonic-gate 		{
52687c478bd9Sstevel@tonic-gate 			queue_t	*q;
52697c478bd9Sstevel@tonic-gate 			queue_t	*mate = NULL;
52707c478bd9Sstevel@tonic-gate 
52717c478bd9Sstevel@tonic-gate 			/* get read queue of stream terminus */
52727c478bd9Sstevel@tonic-gate 			claimstr(resstp->sd_wrq);
52737c478bd9Sstevel@tonic-gate 			for (q = resstp->sd_wrq->q_next; q->q_next != NULL;
52747c478bd9Sstevel@tonic-gate 			    q = q->q_next)
52757c478bd9Sstevel@tonic-gate 				if (!STRMATED(resstp) && STREAM(q) != resstp &&
52767c478bd9Sstevel@tonic-gate 				    mate == NULL) {
52777c478bd9Sstevel@tonic-gate 					ASSERT(q->q_qinfo->qi_srvp);
52787c478bd9Sstevel@tonic-gate 					ASSERT(_OTHERQ(q)->q_qinfo->qi_srvp);
52797c478bd9Sstevel@tonic-gate 					claimstr(q);
52807c478bd9Sstevel@tonic-gate 					mate = q;
52817c478bd9Sstevel@tonic-gate 				}
52827c478bd9Sstevel@tonic-gate 			q = _RD(q);
52837c478bd9Sstevel@tonic-gate 			if (mate)
52847c478bd9Sstevel@tonic-gate 				releasestr(mate);
52857c478bd9Sstevel@tonic-gate 			releasestr(resstp->sd_wrq);
52867c478bd9Sstevel@tonic-gate 			ival = (t_uscalar_t)q;
52877c478bd9Sstevel@tonic-gate 		}
52887c478bd9Sstevel@tonic-gate #else
52897c478bd9Sstevel@tonic-gate 		ival = (t_uscalar_t)getminor(resftp->f_vnode->v_rdev);
52907c478bd9Sstevel@tonic-gate #endif	/* _ILP32 */
52917c478bd9Sstevel@tonic-gate 
52927c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strfdinsert, ctlbuf.len) <
52937c478bd9Sstevel@tonic-gate 		    STRUCT_FGET(strfdinsert, offset) + sizeof (t_uscalar_t)) {
52947c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
52957c478bd9Sstevel@tonic-gate 			return (EINVAL);
52967c478bd9Sstevel@tonic-gate 		}
52977c478bd9Sstevel@tonic-gate 
52987c478bd9Sstevel@tonic-gate 		/*
52997c478bd9Sstevel@tonic-gate 		 * Check for legal flag value.
53007c478bd9Sstevel@tonic-gate 		 */
53017c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strfdinsert, flags) & ~RS_HIPRI) {
53027c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
53037c478bd9Sstevel@tonic-gate 			return (EINVAL);
53047c478bd9Sstevel@tonic-gate 		}
53057c478bd9Sstevel@tonic-gate 
53067c478bd9Sstevel@tonic-gate 		/* get these values from those cached in the stream head */
53077c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(stp->sd_wrq));
53087c478bd9Sstevel@tonic-gate 		rmin = stp->sd_qn_minpsz;
53097c478bd9Sstevel@tonic-gate 		rmax = stp->sd_qn_maxpsz;
53107c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(stp->sd_wrq));
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate 		/*
53137c478bd9Sstevel@tonic-gate 		 * Make sure ctl and data sizes together fall within
53147c478bd9Sstevel@tonic-gate 		 * the limits of the max and min receive packet sizes
53157c478bd9Sstevel@tonic-gate 		 * and do not exceed system limit.  A negative data
53167c478bd9Sstevel@tonic-gate 		 * length means that no data part is to be sent.
53177c478bd9Sstevel@tonic-gate 		 */
53187c478bd9Sstevel@tonic-gate 		ASSERT((rmax >= 0) || (rmax == INFPSZ));
53197c478bd9Sstevel@tonic-gate 		if (rmax == 0) {
53207c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
53217c478bd9Sstevel@tonic-gate 			return (ERANGE);
53227c478bd9Sstevel@tonic-gate 		}
53237c478bd9Sstevel@tonic-gate 		if ((msgsize = STRUCT_FGET(strfdinsert, databuf.len)) < 0)
53247c478bd9Sstevel@tonic-gate 			msgsize = 0;
53257c478bd9Sstevel@tonic-gate 		if ((msgsize < rmin) ||
53267c478bd9Sstevel@tonic-gate 		    ((msgsize > rmax) && (rmax != INFPSZ)) ||
53277c478bd9Sstevel@tonic-gate 		    (STRUCT_FGET(strfdinsert, ctlbuf.len) > strctlsz)) {
53287c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
53297c478bd9Sstevel@tonic-gate 			return (ERANGE);
53307c478bd9Sstevel@tonic-gate 		}
53317c478bd9Sstevel@tonic-gate 
53327c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
53337c478bd9Sstevel@tonic-gate 		while (!(STRUCT_FGET(strfdinsert, flags) & RS_HIPRI) &&
53347c478bd9Sstevel@tonic-gate 		    !canputnext(stp->sd_wrq)) {
53357c478bd9Sstevel@tonic-gate 			if ((error = strwaitq(stp, WRITEWAIT, (ssize_t)0,
53367c478bd9Sstevel@tonic-gate 			    flag, -1, &done)) != 0 || done) {
53377c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
53387c478bd9Sstevel@tonic-gate 				releasef(STRUCT_FGET(strfdinsert, fildes));
53397c478bd9Sstevel@tonic-gate 				return (error);
53407c478bd9Sstevel@tonic-gate 			}
53419acbbeafSnn35248 			if ((error = i_straccess(stp, access)) != 0) {
53427c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
53437c478bd9Sstevel@tonic-gate 				releasef(
53447c478bd9Sstevel@tonic-gate 				    STRUCT_FGET(strfdinsert, fildes));
53457c478bd9Sstevel@tonic-gate 				return (error);
53467c478bd9Sstevel@tonic-gate 			}
53477c478bd9Sstevel@tonic-gate 		}
53487c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
53497c478bd9Sstevel@tonic-gate 
53507c478bd9Sstevel@tonic-gate 		/*
53517c478bd9Sstevel@tonic-gate 		 * Copy strfdinsert.ctlbuf into native form of
53527c478bd9Sstevel@tonic-gate 		 * ctlbuf to pass down into strmakemsg().
53537c478bd9Sstevel@tonic-gate 		 */
53547c478bd9Sstevel@tonic-gate 		mctl.maxlen = STRUCT_FGET(strfdinsert, ctlbuf.maxlen);
53557c478bd9Sstevel@tonic-gate 		mctl.len = STRUCT_FGET(strfdinsert, ctlbuf.len);
53567c478bd9Sstevel@tonic-gate 		mctl.buf = STRUCT_FGETP(strfdinsert, ctlbuf.buf);
53577c478bd9Sstevel@tonic-gate 
53587c478bd9Sstevel@tonic-gate 		iov.iov_base = STRUCT_FGETP(strfdinsert, databuf.buf);
53597c478bd9Sstevel@tonic-gate 		iov.iov_len = STRUCT_FGET(strfdinsert, databuf.len);
53607c478bd9Sstevel@tonic-gate 		uio.uio_iov = &iov;
53617c478bd9Sstevel@tonic-gate 		uio.uio_iovcnt = 1;
53627c478bd9Sstevel@tonic-gate 		uio.uio_loffset = 0;
53637c478bd9Sstevel@tonic-gate 		uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
53647c478bd9Sstevel@tonic-gate 		    UIO_SYSSPACE;
53657c478bd9Sstevel@tonic-gate 		uio.uio_fmode = 0;
53667c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_CACHED;
53677c478bd9Sstevel@tonic-gate 		uio.uio_resid = iov.iov_len;
53687c478bd9Sstevel@tonic-gate 		if ((error = strmakemsg(&mctl,
53697c478bd9Sstevel@tonic-gate 		    &msgsize, &uio, stp,
53707c478bd9Sstevel@tonic-gate 		    STRUCT_FGET(strfdinsert, flags), &mp)) != 0 || !mp) {
53717c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strfdinsert, databuf.len, msgsize);
53727c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
53737c478bd9Sstevel@tonic-gate 			return (error);
53747c478bd9Sstevel@tonic-gate 		}
53757c478bd9Sstevel@tonic-gate 
53767c478bd9Sstevel@tonic-gate 		STRUCT_FSET(strfdinsert, databuf.len, msgsize);
53777c478bd9Sstevel@tonic-gate 
53787c478bd9Sstevel@tonic-gate 		/*
53797c478bd9Sstevel@tonic-gate 		 * Place the possibly reencoded queue pointer 'offset' bytes
53807c478bd9Sstevel@tonic-gate 		 * from the start of the control portion of the message.
53817c478bd9Sstevel@tonic-gate 		 */
53827c478bd9Sstevel@tonic-gate 		*((t_uscalar_t *)(mp->b_rptr +
53837c478bd9Sstevel@tonic-gate 		    STRUCT_FGET(strfdinsert, offset))) = ival;
53847c478bd9Sstevel@tonic-gate 
53857c478bd9Sstevel@tonic-gate 		/*
53867c478bd9Sstevel@tonic-gate 		 * Put message downstream.
53877c478bd9Sstevel@tonic-gate 		 */
53887c478bd9Sstevel@tonic-gate 		stream_willservice(stp);
53897c478bd9Sstevel@tonic-gate 		putnext(stp->sd_wrq, mp);
53907c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
53917c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(strfdinsert, fildes));
53927c478bd9Sstevel@tonic-gate 		return (error);
53937c478bd9Sstevel@tonic-gate 	}
53947c478bd9Sstevel@tonic-gate 
53957c478bd9Sstevel@tonic-gate 	case I_SENDFD:
53967c478bd9Sstevel@tonic-gate 	{
53977c478bd9Sstevel@tonic-gate 		struct file *fp;
53987c478bd9Sstevel@tonic-gate 
53997c478bd9Sstevel@tonic-gate 		if ((fp = getf((int)arg)) == NULL)
54007c478bd9Sstevel@tonic-gate 			return (EBADF);
54017c478bd9Sstevel@tonic-gate 		error = do_sendfp(stp, fp, crp);
54027c478bd9Sstevel@tonic-gate 		if (audit_active) {
54037c478bd9Sstevel@tonic-gate 			audit_fdsend((int)arg, fp, error);
54047c478bd9Sstevel@tonic-gate 		}
54057c478bd9Sstevel@tonic-gate 		releasef((int)arg);
54067c478bd9Sstevel@tonic-gate 		return (error);
54077c478bd9Sstevel@tonic-gate 	}
54087c478bd9Sstevel@tonic-gate 
54097c478bd9Sstevel@tonic-gate 	case I_RECVFD:
54107c478bd9Sstevel@tonic-gate 	case I_E_RECVFD:
54117c478bd9Sstevel@tonic-gate 	{
54127c478bd9Sstevel@tonic-gate 		struct k_strrecvfd *srf;
54137c478bd9Sstevel@tonic-gate 		int i, fd;
54147c478bd9Sstevel@tonic-gate 
54157c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
54167c478bd9Sstevel@tonic-gate 		while (!(mp = getq(rdq))) {
54177c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STRHUP|STREOF)) {
54187c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
54197c478bd9Sstevel@tonic-gate 				return (ENXIO);
54207c478bd9Sstevel@tonic-gate 			}
54217c478bd9Sstevel@tonic-gate 			if ((error = strwaitq(stp, GETWAIT, (ssize_t)0,
54227c478bd9Sstevel@tonic-gate 			    flag, -1, &done)) != 0 || done) {
54237c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
54247c478bd9Sstevel@tonic-gate 				return (error);
54257c478bd9Sstevel@tonic-gate 			}
54269acbbeafSnn35248 			if ((error = i_straccess(stp, access)) != 0) {
54277c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
54287c478bd9Sstevel@tonic-gate 				return (error);
54297c478bd9Sstevel@tonic-gate 			}
54307c478bd9Sstevel@tonic-gate 		}
54317c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type != M_PASSFP) {
54327c478bd9Sstevel@tonic-gate 			putback(stp, rdq, mp, mp->b_band);
54337c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
54347c478bd9Sstevel@tonic-gate 			return (EBADMSG);
54357c478bd9Sstevel@tonic-gate 		}
54367c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
54377c478bd9Sstevel@tonic-gate 
54387c478bd9Sstevel@tonic-gate 		srf = (struct k_strrecvfd *)mp->b_rptr;
54397c478bd9Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1) {
54407c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
54417c478bd9Sstevel@tonic-gate 			putback(stp, rdq, mp, mp->b_band);
54427c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
54437c478bd9Sstevel@tonic-gate 			return (EMFILE);
54447c478bd9Sstevel@tonic-gate 		}
54457c478bd9Sstevel@tonic-gate 		if (cmd == I_RECVFD) {
54467c478bd9Sstevel@tonic-gate 			struct o_strrecvfd	ostrfd;
54477c478bd9Sstevel@tonic-gate 
54487c478bd9Sstevel@tonic-gate 			/* check to see if uid/gid values are too large. */
54497c478bd9Sstevel@tonic-gate 
54507c478bd9Sstevel@tonic-gate 			if (srf->uid > (o_uid_t)USHRT_MAX ||
54517c478bd9Sstevel@tonic-gate 			    srf->gid > (o_gid_t)USHRT_MAX) {
54527c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
54537c478bd9Sstevel@tonic-gate 				putback(stp, rdq, mp, mp->b_band);
54547c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
54557c478bd9Sstevel@tonic-gate 				setf(fd, NULL);	/* release fd entry */
54567c478bd9Sstevel@tonic-gate 				return (EOVERFLOW);
54577c478bd9Sstevel@tonic-gate 			}
54587c478bd9Sstevel@tonic-gate 
54597c478bd9Sstevel@tonic-gate 			ostrfd.fd = fd;
54607c478bd9Sstevel@tonic-gate 			ostrfd.uid = (o_uid_t)srf->uid;
54617c478bd9Sstevel@tonic-gate 			ostrfd.gid = (o_gid_t)srf->gid;
54627c478bd9Sstevel@tonic-gate 
54637c478bd9Sstevel@tonic-gate 			/* Null the filler bits */
54647c478bd9Sstevel@tonic-gate 			for (i = 0; i < 8; i++)
54657c478bd9Sstevel@tonic-gate 				ostrfd.fill[i] = 0;
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate 			error = strcopyout(&ostrfd, (void *)arg,
54687c478bd9Sstevel@tonic-gate 			    sizeof (struct o_strrecvfd), copyflag);
54697c478bd9Sstevel@tonic-gate 		} else {		/* I_E_RECVFD */
54707c478bd9Sstevel@tonic-gate 			struct strrecvfd	strfd;
54717c478bd9Sstevel@tonic-gate 
54727c478bd9Sstevel@tonic-gate 			strfd.fd = fd;
54737c478bd9Sstevel@tonic-gate 			strfd.uid = srf->uid;
54747c478bd9Sstevel@tonic-gate 			strfd.gid = srf->gid;
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate 			/* null the filler bits */
54777c478bd9Sstevel@tonic-gate 			for (i = 0; i < 8; i++)
54787c478bd9Sstevel@tonic-gate 				strfd.fill[i] = 0;
54797c478bd9Sstevel@tonic-gate 
54807c478bd9Sstevel@tonic-gate 			error = strcopyout(&strfd, (void *)arg,
54817c478bd9Sstevel@tonic-gate 			    sizeof (struct strrecvfd), copyflag);
54827c478bd9Sstevel@tonic-gate 		}
54837c478bd9Sstevel@tonic-gate 
54847c478bd9Sstevel@tonic-gate 		if (error) {
54857c478bd9Sstevel@tonic-gate 			setf(fd, NULL);	/* release fd entry */
54867c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
54877c478bd9Sstevel@tonic-gate 			putback(stp, rdq, mp, mp->b_band);
54887c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
54897c478bd9Sstevel@tonic-gate 			return (error);
54907c478bd9Sstevel@tonic-gate 		}
54917c478bd9Sstevel@tonic-gate 		if (audit_active) {
54927c478bd9Sstevel@tonic-gate 			audit_fdrecv(fd, srf->fp);
54937c478bd9Sstevel@tonic-gate 		}
54947c478bd9Sstevel@tonic-gate 
54957c478bd9Sstevel@tonic-gate 		/*
54967c478bd9Sstevel@tonic-gate 		 * Always increment f_count since the freemsg() below will
54977c478bd9Sstevel@tonic-gate 		 * always call free_passfp() which performs a closef().
54987c478bd9Sstevel@tonic-gate 		 */
54997c478bd9Sstevel@tonic-gate 		mutex_enter(&srf->fp->f_tlock);
55007c478bd9Sstevel@tonic-gate 		srf->fp->f_count++;
55017c478bd9Sstevel@tonic-gate 		mutex_exit(&srf->fp->f_tlock);
55027c478bd9Sstevel@tonic-gate 		setf(fd, srf->fp);
55037c478bd9Sstevel@tonic-gate 		freemsg(mp);
55047c478bd9Sstevel@tonic-gate 		return (0);
55057c478bd9Sstevel@tonic-gate 	}
55067c478bd9Sstevel@tonic-gate 
55077c478bd9Sstevel@tonic-gate 	case I_SWROPT:
55087c478bd9Sstevel@tonic-gate 		/*
55097c478bd9Sstevel@tonic-gate 		 * Set/clear the write options. arg is a bit
55107c478bd9Sstevel@tonic-gate 		 * mask with any of the following bits set...
55117c478bd9Sstevel@tonic-gate 		 * 	SNDZERO - send zero length message
55127c478bd9Sstevel@tonic-gate 		 *	SNDPIPE - send sigpipe to process if
55137c478bd9Sstevel@tonic-gate 		 *		sd_werror is set and process is
55147c478bd9Sstevel@tonic-gate 		 *		doing a write or putmsg.
55157c478bd9Sstevel@tonic-gate 		 * The new stream head write options should reflect
55167c478bd9Sstevel@tonic-gate 		 * what is in arg.
55177c478bd9Sstevel@tonic-gate 		 */
55187c478bd9Sstevel@tonic-gate 		if (arg & ~(SNDZERO|SNDPIPE))
55197c478bd9Sstevel@tonic-gate 			return (EINVAL);
55207c478bd9Sstevel@tonic-gate 
55217c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
55227c478bd9Sstevel@tonic-gate 		stp->sd_wput_opt &= ~(SW_SIGPIPE|SW_SNDZERO);
55237c478bd9Sstevel@tonic-gate 		if (arg & SNDZERO)
55247c478bd9Sstevel@tonic-gate 			stp->sd_wput_opt |= SW_SNDZERO;
55257c478bd9Sstevel@tonic-gate 		if (arg & SNDPIPE)
55267c478bd9Sstevel@tonic-gate 			stp->sd_wput_opt |= SW_SIGPIPE;
55277c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
55287c478bd9Sstevel@tonic-gate 		return (0);
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate 	case I_GWROPT:
55317c478bd9Sstevel@tonic-gate 	{
55327c478bd9Sstevel@tonic-gate 		int wropt = 0;
55337c478bd9Sstevel@tonic-gate 
55347c478bd9Sstevel@tonic-gate 		if (stp->sd_wput_opt & SW_SNDZERO)
55357c478bd9Sstevel@tonic-gate 			wropt |= SNDZERO;
55367c478bd9Sstevel@tonic-gate 		if (stp->sd_wput_opt & SW_SIGPIPE)
55377c478bd9Sstevel@tonic-gate 			wropt |= SNDPIPE;
55387c478bd9Sstevel@tonic-gate 		return (strcopyout(&wropt, (void *)arg, sizeof (wropt),
55397c478bd9Sstevel@tonic-gate 		    copyflag));
55407c478bd9Sstevel@tonic-gate 	}
55417c478bd9Sstevel@tonic-gate 
55427c478bd9Sstevel@tonic-gate 	case I_LIST:
55437c478bd9Sstevel@tonic-gate 		/*
55447c478bd9Sstevel@tonic-gate 		 * Returns all the modules found on this stream,
55457c478bd9Sstevel@tonic-gate 		 * upto the driver. If argument is NULL, return the
55467c478bd9Sstevel@tonic-gate 		 * number of modules (including driver). If argument
55477c478bd9Sstevel@tonic-gate 		 * is not NULL, copy the names into the structure
55487c478bd9Sstevel@tonic-gate 		 * provided.
55497c478bd9Sstevel@tonic-gate 		 */
55507c478bd9Sstevel@tonic-gate 
55517c478bd9Sstevel@tonic-gate 	{
55527c478bd9Sstevel@tonic-gate 		queue_t *q;
55537c478bd9Sstevel@tonic-gate 		int num_modules, space_allocated;
55547c478bd9Sstevel@tonic-gate 		STRUCT_DECL(str_list, strlist);
55557c478bd9Sstevel@tonic-gate 		struct str_mlist *mlist_ptr;
55567c478bd9Sstevel@tonic-gate 
55577c478bd9Sstevel@tonic-gate 		if (arg == NULL) { /* Return number of modules plus driver */
55587c478bd9Sstevel@tonic-gate 			q = stp->sd_wrq;
55597c478bd9Sstevel@tonic-gate 			if (stp->sd_vnode->v_type == VFIFO) {
55607c478bd9Sstevel@tonic-gate 				*rvalp = stp->sd_pushcnt;
55617c478bd9Sstevel@tonic-gate 			} else {
55627c478bd9Sstevel@tonic-gate 				*rvalp = stp->sd_pushcnt + 1;
55637c478bd9Sstevel@tonic-gate 			}
55647c478bd9Sstevel@tonic-gate 		} else {
55657c478bd9Sstevel@tonic-gate 			STRUCT_INIT(strlist, flag);
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate 			error = strcopyin((void *)arg, STRUCT_BUF(strlist),
55687c478bd9Sstevel@tonic-gate 			    STRUCT_SIZE(strlist), copyflag);
55697c478bd9Sstevel@tonic-gate 			if (error)
55707c478bd9Sstevel@tonic-gate 				return (error);
55717c478bd9Sstevel@tonic-gate 
55727c478bd9Sstevel@tonic-gate 			space_allocated = STRUCT_FGET(strlist, sl_nmods);
55737c478bd9Sstevel@tonic-gate 			if ((space_allocated) <= 0)
55747c478bd9Sstevel@tonic-gate 				return (EINVAL);
55757c478bd9Sstevel@tonic-gate 			claimstr(stp->sd_wrq);
55767c478bd9Sstevel@tonic-gate 			q = stp->sd_wrq;
55777c478bd9Sstevel@tonic-gate 			num_modules = 0;
55787c478bd9Sstevel@tonic-gate 			while (_SAMESTR(q) && (space_allocated != 0)) {
55797c478bd9Sstevel@tonic-gate 				char *name =
55807c478bd9Sstevel@tonic-gate 				    q->q_next->q_qinfo->qi_minfo->mi_idname;
55817c478bd9Sstevel@tonic-gate 
55827c478bd9Sstevel@tonic-gate 				mlist_ptr = STRUCT_FGETP(strlist, sl_modlist);
55837c478bd9Sstevel@tonic-gate 
55847c478bd9Sstevel@tonic-gate 				error = strcopyout(name, mlist_ptr,
55857c478bd9Sstevel@tonic-gate 				    strlen(name) + 1, copyflag);
55867c478bd9Sstevel@tonic-gate 
55877c478bd9Sstevel@tonic-gate 				if (error) {
55887c478bd9Sstevel@tonic-gate 					releasestr(stp->sd_wrq);
55897c478bd9Sstevel@tonic-gate 					return (error);
55907c478bd9Sstevel@tonic-gate 				}
55917c478bd9Sstevel@tonic-gate 				q = q->q_next;
55927c478bd9Sstevel@tonic-gate 				space_allocated--;
55937c478bd9Sstevel@tonic-gate 				num_modules++;
55947c478bd9Sstevel@tonic-gate 				mlist_ptr =
55957c478bd9Sstevel@tonic-gate 				    (struct str_mlist *)((uintptr_t)mlist_ptr +
55967c478bd9Sstevel@tonic-gate 				    sizeof (struct str_mlist));
55977c478bd9Sstevel@tonic-gate 				STRUCT_FSETP(strlist, sl_modlist, mlist_ptr);
55987c478bd9Sstevel@tonic-gate 			}
55997c478bd9Sstevel@tonic-gate 			releasestr(stp->sd_wrq);
56007c478bd9Sstevel@tonic-gate 			error = strcopyout(&num_modules, (void *)arg,
56017c478bd9Sstevel@tonic-gate 			    sizeof (int), copyflag);
56027c478bd9Sstevel@tonic-gate 		}
56037c478bd9Sstevel@tonic-gate 		return (error);
56047c478bd9Sstevel@tonic-gate 	}
56057c478bd9Sstevel@tonic-gate 
56067c478bd9Sstevel@tonic-gate 	case I_CKBAND:
56077c478bd9Sstevel@tonic-gate 	{
56087c478bd9Sstevel@tonic-gate 		queue_t *q;
56097c478bd9Sstevel@tonic-gate 		qband_t *qbp;
56107c478bd9Sstevel@tonic-gate 
56117c478bd9Sstevel@tonic-gate 		if ((arg < 0) || (arg >= NBAND))
56127c478bd9Sstevel@tonic-gate 			return (EINVAL);
56137c478bd9Sstevel@tonic-gate 		q = _RD(stp->sd_wrq);
56147c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(q));
56157c478bd9Sstevel@tonic-gate 		if (arg > (int)q->q_nband) {
56167c478bd9Sstevel@tonic-gate 			*rvalp = 0;
56177c478bd9Sstevel@tonic-gate 		} else {
56187c478bd9Sstevel@tonic-gate 			if (arg == 0) {
56197c478bd9Sstevel@tonic-gate 				if (q->q_first)
56207c478bd9Sstevel@tonic-gate 					*rvalp = 1;
56217c478bd9Sstevel@tonic-gate 				else
56227c478bd9Sstevel@tonic-gate 					*rvalp = 0;
56237c478bd9Sstevel@tonic-gate 			} else {
56247c478bd9Sstevel@tonic-gate 				qbp = q->q_bandp;
56257c478bd9Sstevel@tonic-gate 				while (--arg > 0)
56267c478bd9Sstevel@tonic-gate 					qbp = qbp->qb_next;
56277c478bd9Sstevel@tonic-gate 				if (qbp->qb_first)
56287c478bd9Sstevel@tonic-gate 					*rvalp = 1;
56297c478bd9Sstevel@tonic-gate 				else
56307c478bd9Sstevel@tonic-gate 					*rvalp = 0;
56317c478bd9Sstevel@tonic-gate 			}
56327c478bd9Sstevel@tonic-gate 		}
56337c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(q));
56347c478bd9Sstevel@tonic-gate 		return (0);
56357c478bd9Sstevel@tonic-gate 	}
56367c478bd9Sstevel@tonic-gate 
56377c478bd9Sstevel@tonic-gate 	case I_GETBAND:
56387c478bd9Sstevel@tonic-gate 	{
56397c478bd9Sstevel@tonic-gate 		int intpri;
56407c478bd9Sstevel@tonic-gate 		queue_t *q;
56417c478bd9Sstevel@tonic-gate 
56427c478bd9Sstevel@tonic-gate 		q = _RD(stp->sd_wrq);
56437c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(q));
56447c478bd9Sstevel@tonic-gate 		mp = q->q_first;
56457c478bd9Sstevel@tonic-gate 		if (!mp) {
56467c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(q));
56477c478bd9Sstevel@tonic-gate 			return (ENODATA);
56487c478bd9Sstevel@tonic-gate 		}
56497c478bd9Sstevel@tonic-gate 		intpri = (int)mp->b_band;
56507c478bd9Sstevel@tonic-gate 		error = strcopyout(&intpri, (void *)arg, sizeof (int),
56517c478bd9Sstevel@tonic-gate 		    copyflag);
56527c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(q));
56537c478bd9Sstevel@tonic-gate 		return (error);
56547c478bd9Sstevel@tonic-gate 	}
56557c478bd9Sstevel@tonic-gate 
56567c478bd9Sstevel@tonic-gate 	case I_ATMARK:
56577c478bd9Sstevel@tonic-gate 	{
56587c478bd9Sstevel@tonic-gate 		queue_t *q;
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate 		if (arg & ~(ANYMARK|LASTMARK))
56617c478bd9Sstevel@tonic-gate 			return (EINVAL);
56627c478bd9Sstevel@tonic-gate 		q = _RD(stp->sd_wrq);
56637c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
56647c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STRATMARK) && (arg == ANYMARK)) {
56657c478bd9Sstevel@tonic-gate 			*rvalp = 1;
56667c478bd9Sstevel@tonic-gate 		} else {
56677c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(q));
56687c478bd9Sstevel@tonic-gate 			mp = q->q_first;
56697c478bd9Sstevel@tonic-gate 
56707c478bd9Sstevel@tonic-gate 			if (mp == NULL)
56717c478bd9Sstevel@tonic-gate 				*rvalp = 0;
56727c478bd9Sstevel@tonic-gate 			else if ((arg == ANYMARK) && (mp->b_flag & MSGMARK))
56737c478bd9Sstevel@tonic-gate 				*rvalp = 1;
56747c478bd9Sstevel@tonic-gate 			else if ((arg == LASTMARK) && (mp == stp->sd_mark))
56757c478bd9Sstevel@tonic-gate 				*rvalp = 1;
56767c478bd9Sstevel@tonic-gate 			else
56777c478bd9Sstevel@tonic-gate 				*rvalp = 0;
56787c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(q));
56797c478bd9Sstevel@tonic-gate 		}
56807c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
56817c478bd9Sstevel@tonic-gate 		return (0);
56827c478bd9Sstevel@tonic-gate 	}
56837c478bd9Sstevel@tonic-gate 
56847c478bd9Sstevel@tonic-gate 	case I_CANPUT:
56857c478bd9Sstevel@tonic-gate 	{
56867c478bd9Sstevel@tonic-gate 		char band;
56877c478bd9Sstevel@tonic-gate 
56887c478bd9Sstevel@tonic-gate 		if ((arg < 0) || (arg >= NBAND))
56897c478bd9Sstevel@tonic-gate 			return (EINVAL);
56907c478bd9Sstevel@tonic-gate 		band = (char)arg;
56917c478bd9Sstevel@tonic-gate 		*rvalp = bcanputnext(stp->sd_wrq, band);
56927c478bd9Sstevel@tonic-gate 		return (0);
56937c478bd9Sstevel@tonic-gate 	}
56947c478bd9Sstevel@tonic-gate 
56957c478bd9Sstevel@tonic-gate 	case I_SETCLTIME:
56967c478bd9Sstevel@tonic-gate 	{
56977c478bd9Sstevel@tonic-gate 		int closetime;
56987c478bd9Sstevel@tonic-gate 
56997c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, &closetime, sizeof (int),
57007c478bd9Sstevel@tonic-gate 		    copyflag);
57017c478bd9Sstevel@tonic-gate 		if (error)
57027c478bd9Sstevel@tonic-gate 			return (error);
57037c478bd9Sstevel@tonic-gate 		if (closetime < 0)
57047c478bd9Sstevel@tonic-gate 			return (EINVAL);
57057c478bd9Sstevel@tonic-gate 
57067c478bd9Sstevel@tonic-gate 		stp->sd_closetime = closetime;
57077c478bd9Sstevel@tonic-gate 		return (0);
57087c478bd9Sstevel@tonic-gate 	}
57097c478bd9Sstevel@tonic-gate 
57107c478bd9Sstevel@tonic-gate 	case I_GETCLTIME:
57117c478bd9Sstevel@tonic-gate 	{
57127c478bd9Sstevel@tonic-gate 		int closetime;
57137c478bd9Sstevel@tonic-gate 
57147c478bd9Sstevel@tonic-gate 		closetime = stp->sd_closetime;
57157c478bd9Sstevel@tonic-gate 		return (strcopyout(&closetime, (void *)arg, sizeof (int),
57167c478bd9Sstevel@tonic-gate 		    copyflag));
57177c478bd9Sstevel@tonic-gate 	}
57187c478bd9Sstevel@tonic-gate 
57197c478bd9Sstevel@tonic-gate 	case TIOCGSID:
57207c478bd9Sstevel@tonic-gate 	{
57217c478bd9Sstevel@tonic-gate 		pid_t sid;
57227c478bd9Sstevel@tonic-gate 
57239acbbeafSnn35248 		mutex_enter(&stp->sd_lock);
57247c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp == NULL) {
57259acbbeafSnn35248 			mutex_exit(&stp->sd_lock);
57267c478bd9Sstevel@tonic-gate 			return (ENOTTY);
57277c478bd9Sstevel@tonic-gate 		}
57287c478bd9Sstevel@tonic-gate 		sid = stp->sd_sidp->pid_id;
57299acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
57307c478bd9Sstevel@tonic-gate 		return (strcopyout(&sid, (void *)arg, sizeof (pid_t),
57317c478bd9Sstevel@tonic-gate 		    copyflag));
57327c478bd9Sstevel@tonic-gate 	}
57337c478bd9Sstevel@tonic-gate 
57347c478bd9Sstevel@tonic-gate 	case TIOCSPGRP:
57357c478bd9Sstevel@tonic-gate 	{
57367c478bd9Sstevel@tonic-gate 		pid_t pgrp;
57377c478bd9Sstevel@tonic-gate 		proc_t *q;
57387c478bd9Sstevel@tonic-gate 		pid_t	sid, fg_pgid, bg_pgid;
57397c478bd9Sstevel@tonic-gate 
57407c478bd9Sstevel@tonic-gate 		if (error = strcopyin((void *)arg, &pgrp, sizeof (pid_t),
57417c478bd9Sstevel@tonic-gate 		    copyflag))
57427c478bd9Sstevel@tonic-gate 			return (error);
57437c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
57447c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
57457c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp != ttoproc(curthread)->p_sessp->s_sidp) {
57467c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
57477c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
57487c478bd9Sstevel@tonic-gate 			return (ENOTTY);
57497c478bd9Sstevel@tonic-gate 		}
57507c478bd9Sstevel@tonic-gate 		if (pgrp == stp->sd_pgidp->pid_id) {
57517c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
57527c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
57537c478bd9Sstevel@tonic-gate 			return (0);
57547c478bd9Sstevel@tonic-gate 		}
57557c478bd9Sstevel@tonic-gate 		if (pgrp <= 0 || pgrp >= maxpid) {
57567c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
57577c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
57587c478bd9Sstevel@tonic-gate 			return (EINVAL);
57597c478bd9Sstevel@tonic-gate 		}
57607c478bd9Sstevel@tonic-gate 		if ((q = pgfind(pgrp)) == NULL ||
57617c478bd9Sstevel@tonic-gate 		    q->p_sessp != ttoproc(curthread)->p_sessp) {
57627c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
57637c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
57647c478bd9Sstevel@tonic-gate 			return (EPERM);
57657c478bd9Sstevel@tonic-gate 		}
57667c478bd9Sstevel@tonic-gate 		sid = stp->sd_sidp->pid_id;
57677c478bd9Sstevel@tonic-gate 		fg_pgid = q->p_pgrp;
57687c478bd9Sstevel@tonic-gate 		bg_pgid = stp->sd_pgidp->pid_id;
57697c478bd9Sstevel@tonic-gate 		CL_SET_PROCESS_GROUP(curthread, sid, bg_pgid, fg_pgid);
57707c478bd9Sstevel@tonic-gate 		PID_RELE(stp->sd_pgidp);
57719acbbeafSnn35248 		ctty_clear_sighuped();
57727c478bd9Sstevel@tonic-gate 		stp->sd_pgidp = q->p_pgidp;
57737c478bd9Sstevel@tonic-gate 		PID_HOLD(stp->sd_pgidp);
57747c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
57757c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
57767c478bd9Sstevel@tonic-gate 		return (0);
57777c478bd9Sstevel@tonic-gate 	}
57787c478bd9Sstevel@tonic-gate 
57797c478bd9Sstevel@tonic-gate 	case TIOCGPGRP:
57807c478bd9Sstevel@tonic-gate 	{
57817c478bd9Sstevel@tonic-gate 		pid_t pgrp;
57827c478bd9Sstevel@tonic-gate 
57839acbbeafSnn35248 		mutex_enter(&stp->sd_lock);
57847c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp == NULL) {
57859acbbeafSnn35248 			mutex_exit(&stp->sd_lock);
57867c478bd9Sstevel@tonic-gate 			return (ENOTTY);
57877c478bd9Sstevel@tonic-gate 		}
57887c478bd9Sstevel@tonic-gate 		pgrp = stp->sd_pgidp->pid_id;
57899acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
57907c478bd9Sstevel@tonic-gate 		return (strcopyout(&pgrp, (void *)arg, sizeof (pid_t),
57917c478bd9Sstevel@tonic-gate 		    copyflag));
57927c478bd9Sstevel@tonic-gate 	}
57937c478bd9Sstevel@tonic-gate 
57949acbbeafSnn35248 	case TIOCSCTTY:
57959acbbeafSnn35248 	{
57969acbbeafSnn35248 		return (strctty(stp));
57979acbbeafSnn35248 	}
57989acbbeafSnn35248 
57999acbbeafSnn35248 	case TIOCNOTTY:
58009acbbeafSnn35248 	{
58019acbbeafSnn35248 		/* freectty() always assumes curproc. */
58029acbbeafSnn35248 		if (freectty(B_FALSE) != 0)
58039acbbeafSnn35248 			return (0);
58049acbbeafSnn35248 		return (ENOTTY);
58059acbbeafSnn35248 	}
58069acbbeafSnn35248 
58077c478bd9Sstevel@tonic-gate 	case FIONBIO:
58087c478bd9Sstevel@tonic-gate 	case FIOASYNC:
58097c478bd9Sstevel@tonic-gate 		return (0);	/* handled by the upper layer */
58107c478bd9Sstevel@tonic-gate 	}
58117c478bd9Sstevel@tonic-gate }
58127c478bd9Sstevel@tonic-gate 
58137c478bd9Sstevel@tonic-gate /*
58147c478bd9Sstevel@tonic-gate  * Custom free routine used for M_PASSFP messages.
58157c478bd9Sstevel@tonic-gate  */
58167c478bd9Sstevel@tonic-gate static void
58177c478bd9Sstevel@tonic-gate free_passfp(struct k_strrecvfd *srf)
58187c478bd9Sstevel@tonic-gate {
58197c478bd9Sstevel@tonic-gate 	(void) closef(srf->fp);
58207c478bd9Sstevel@tonic-gate 	kmem_free(srf, sizeof (struct k_strrecvfd) + sizeof (frtn_t));
58217c478bd9Sstevel@tonic-gate }
58227c478bd9Sstevel@tonic-gate 
58237c478bd9Sstevel@tonic-gate /* ARGSUSED */
58247c478bd9Sstevel@tonic-gate int
58257c478bd9Sstevel@tonic-gate do_sendfp(struct stdata *stp, struct file *fp, struct cred *cr)
58267c478bd9Sstevel@tonic-gate {
58277c478bd9Sstevel@tonic-gate 	queue_t *qp, *nextqp;
58287c478bd9Sstevel@tonic-gate 	struct k_strrecvfd *srf;
58297c478bd9Sstevel@tonic-gate 	mblk_t *mp;
58307c478bd9Sstevel@tonic-gate 	frtn_t *frtnp;
58317c478bd9Sstevel@tonic-gate 	size_t bufsize;
58327c478bd9Sstevel@tonic-gate 	queue_t	*mate = NULL;
58337c478bd9Sstevel@tonic-gate 	syncq_t	*sq = NULL;
58347c478bd9Sstevel@tonic-gate 	int retval = 0;
58357c478bd9Sstevel@tonic-gate 
58367c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & STRHUP)
58377c478bd9Sstevel@tonic-gate 		return (ENXIO);
58387c478bd9Sstevel@tonic-gate 
58397c478bd9Sstevel@tonic-gate 	claimstr(stp->sd_wrq);
58407c478bd9Sstevel@tonic-gate 
58417c478bd9Sstevel@tonic-gate 	/* Fastpath, we have a pipe, and we are already mated, use it. */
58427c478bd9Sstevel@tonic-gate 	if (STRMATED(stp)) {
58437c478bd9Sstevel@tonic-gate 		qp = _RD(stp->sd_mate->sd_wrq);
58447c478bd9Sstevel@tonic-gate 		claimstr(qp);
58457c478bd9Sstevel@tonic-gate 		mate = qp;
58467c478bd9Sstevel@tonic-gate 	} else { /* Not already mated. */
58477c478bd9Sstevel@tonic-gate 
58487c478bd9Sstevel@tonic-gate 		/*
58497c478bd9Sstevel@tonic-gate 		 * Walk the stream to the end of this one.
58507c478bd9Sstevel@tonic-gate 		 * assumes that the claimstr() will prevent
58517c478bd9Sstevel@tonic-gate 		 * plumbing between the stream head and the
58527c478bd9Sstevel@tonic-gate 		 * driver from changing
58537c478bd9Sstevel@tonic-gate 		 */
58547c478bd9Sstevel@tonic-gate 		qp = stp->sd_wrq;
58557c478bd9Sstevel@tonic-gate 
58567c478bd9Sstevel@tonic-gate 		/*
58577c478bd9Sstevel@tonic-gate 		 * Loop until we reach the end of this stream.
58587c478bd9Sstevel@tonic-gate 		 * On completion, qp points to the write queue
58597c478bd9Sstevel@tonic-gate 		 * at the end of the stream, or the read queue
58607c478bd9Sstevel@tonic-gate 		 * at the stream head if this is a fifo.
58617c478bd9Sstevel@tonic-gate 		 */
58627c478bd9Sstevel@tonic-gate 		while (((qp = qp->q_next) != NULL) && _SAMESTR(qp))
58637c478bd9Sstevel@tonic-gate 			;
58647c478bd9Sstevel@tonic-gate 
58657c478bd9Sstevel@tonic-gate 		/*
58667c478bd9Sstevel@tonic-gate 		 * Just in case we get a q_next which is NULL, but
58677c478bd9Sstevel@tonic-gate 		 * not at the end of the stream.  This is actually
58687c478bd9Sstevel@tonic-gate 		 * broken, so we set an assert to catch it in
58697c478bd9Sstevel@tonic-gate 		 * debug, and set an error and return if not debug.
58707c478bd9Sstevel@tonic-gate 		 */
58717c478bd9Sstevel@tonic-gate 		ASSERT(qp);
58727c478bd9Sstevel@tonic-gate 		if (qp == NULL) {
58737c478bd9Sstevel@tonic-gate 			releasestr(stp->sd_wrq);
58747c478bd9Sstevel@tonic-gate 			return (EINVAL);
58757c478bd9Sstevel@tonic-gate 		}
58767c478bd9Sstevel@tonic-gate 
58777c478bd9Sstevel@tonic-gate 		/*
58787c478bd9Sstevel@tonic-gate 		 * Enter the syncq for the driver, so (hopefully)
58797c478bd9Sstevel@tonic-gate 		 * the queue values will not change on us.
58807c478bd9Sstevel@tonic-gate 		 * XXXX - This will only prevent the race IFF only
58817c478bd9Sstevel@tonic-gate 		 *   the write side modifies the q_next member, and
58827c478bd9Sstevel@tonic-gate 		 *   the put procedure is protected by at least
58837c478bd9Sstevel@tonic-gate 		 *   MT_PERQ.
58847c478bd9Sstevel@tonic-gate 		 */
58857c478bd9Sstevel@tonic-gate 		if ((sq = qp->q_syncq) != NULL)
58867c478bd9Sstevel@tonic-gate 			entersq(sq, SQ_PUT);
58877c478bd9Sstevel@tonic-gate 
58887c478bd9Sstevel@tonic-gate 		/* Now get the q_next value from this qp. */
58897c478bd9Sstevel@tonic-gate 		nextqp = qp->q_next;
58907c478bd9Sstevel@tonic-gate 
58917c478bd9Sstevel@tonic-gate 		/*
58927c478bd9Sstevel@tonic-gate 		 * If nextqp exists and the other stream is different
58937c478bd9Sstevel@tonic-gate 		 * from this one claim the stream, set the mate, and
58947c478bd9Sstevel@tonic-gate 		 * get the read queue at the stream head of the other
58957c478bd9Sstevel@tonic-gate 		 * stream.  Assumes that nextqp was at least valid when
58967c478bd9Sstevel@tonic-gate 		 * we got it.  Hopefully the entersq of the driver
58977c478bd9Sstevel@tonic-gate 		 * will prevent it from changing on us.
58987c478bd9Sstevel@tonic-gate 		 */
58997c478bd9Sstevel@tonic-gate 		if ((nextqp != NULL) && (STREAM(nextqp) != stp)) {
59007c478bd9Sstevel@tonic-gate 			ASSERT(qp->q_qinfo->qi_srvp);
59017c478bd9Sstevel@tonic-gate 			ASSERT(_OTHERQ(qp)->q_qinfo->qi_srvp);
59027c478bd9Sstevel@tonic-gate 			ASSERT(_OTHERQ(qp->q_next)->q_qinfo->qi_srvp);
59037c478bd9Sstevel@tonic-gate 			claimstr(nextqp);
59047c478bd9Sstevel@tonic-gate 
59057c478bd9Sstevel@tonic-gate 			/* Make sure we still have a q_next */
59067c478bd9Sstevel@tonic-gate 			if (nextqp != qp->q_next) {
59077c478bd9Sstevel@tonic-gate 				releasestr(stp->sd_wrq);
59087c478bd9Sstevel@tonic-gate 				releasestr(nextqp);
59097c478bd9Sstevel@tonic-gate 				return (EINVAL);
59107c478bd9Sstevel@tonic-gate 			}
59117c478bd9Sstevel@tonic-gate 
59127c478bd9Sstevel@tonic-gate 			qp = _RD(STREAM(nextqp)->sd_wrq);
59137c478bd9Sstevel@tonic-gate 			mate = qp;
59147c478bd9Sstevel@tonic-gate 		}
59157c478bd9Sstevel@tonic-gate 		/* If we entered the synq above, leave it. */
59167c478bd9Sstevel@tonic-gate 		if (sq != NULL)
59177c478bd9Sstevel@tonic-gate 			leavesq(sq, SQ_PUT);
59187c478bd9Sstevel@tonic-gate 	} /*  STRMATED(STP)  */
59197c478bd9Sstevel@tonic-gate 
59207c478bd9Sstevel@tonic-gate 	/* XXX prevents substitution of the ops vector */
59217c478bd9Sstevel@tonic-gate 	if (qp->q_qinfo != &strdata && qp->q_qinfo != &fifo_strdata) {
59227c478bd9Sstevel@tonic-gate 		retval = EINVAL;
59237c478bd9Sstevel@tonic-gate 		goto out;
59247c478bd9Sstevel@tonic-gate 	}
59257c478bd9Sstevel@tonic-gate 
59267c478bd9Sstevel@tonic-gate 	if (qp->q_flag & QFULL) {
59277c478bd9Sstevel@tonic-gate 		retval = EAGAIN;
59287c478bd9Sstevel@tonic-gate 		goto out;
59297c478bd9Sstevel@tonic-gate 	}
59307c478bd9Sstevel@tonic-gate 
59317c478bd9Sstevel@tonic-gate 	/*
59327c478bd9Sstevel@tonic-gate 	 * Since M_PASSFP messages include a file descriptor, we use
59337c478bd9Sstevel@tonic-gate 	 * esballoc() and specify a custom free routine (free_passfp()) that
59347c478bd9Sstevel@tonic-gate 	 * will close the descriptor as part of freeing the message.  For
59357c478bd9Sstevel@tonic-gate 	 * convenience, we stash the frtn_t right after the data block.
59367c478bd9Sstevel@tonic-gate 	 */
59377c478bd9Sstevel@tonic-gate 	bufsize = sizeof (struct k_strrecvfd) + sizeof (frtn_t);
59387c478bd9Sstevel@tonic-gate 	srf = kmem_alloc(bufsize, KM_NOSLEEP);
59397c478bd9Sstevel@tonic-gate 	if (srf == NULL) {
59407c478bd9Sstevel@tonic-gate 		retval = EAGAIN;
59417c478bd9Sstevel@tonic-gate 		goto out;
59427c478bd9Sstevel@tonic-gate 	}
59437c478bd9Sstevel@tonic-gate 
59447c478bd9Sstevel@tonic-gate 	frtnp = (frtn_t *)(srf + 1);
59457c478bd9Sstevel@tonic-gate 	frtnp->free_arg = (caddr_t)srf;
59467c478bd9Sstevel@tonic-gate 	frtnp->free_func = free_passfp;
59477c478bd9Sstevel@tonic-gate 
59487c478bd9Sstevel@tonic-gate 	mp = esballoc((uchar_t *)srf, bufsize, BPRI_MED, frtnp);
59497c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
59507c478bd9Sstevel@tonic-gate 		kmem_free(srf, bufsize);
59517c478bd9Sstevel@tonic-gate 		retval = EAGAIN;
59527c478bd9Sstevel@tonic-gate 		goto out;
59537c478bd9Sstevel@tonic-gate 	}
59547c478bd9Sstevel@tonic-gate 	mp->b_wptr += sizeof (struct k_strrecvfd);
59557c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PASSFP;
59567c478bd9Sstevel@tonic-gate 
59577c478bd9Sstevel@tonic-gate 	srf->fp = fp;
59587c478bd9Sstevel@tonic-gate 	srf->uid = crgetuid(curthread->t_cred);
59597c478bd9Sstevel@tonic-gate 	srf->gid = crgetgid(curthread->t_cred);
59607c478bd9Sstevel@tonic-gate 	mutex_enter(&fp->f_tlock);
59617c478bd9Sstevel@tonic-gate 	fp->f_count++;
59627c478bd9Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
59637c478bd9Sstevel@tonic-gate 
59647c478bd9Sstevel@tonic-gate 	put(qp, mp);
59657c478bd9Sstevel@tonic-gate out:
59667c478bd9Sstevel@tonic-gate 	releasestr(stp->sd_wrq);
59677c478bd9Sstevel@tonic-gate 	if (mate)
59687c478bd9Sstevel@tonic-gate 		releasestr(mate);
59697c478bd9Sstevel@tonic-gate 	return (retval);
59707c478bd9Sstevel@tonic-gate }
59717c478bd9Sstevel@tonic-gate 
59727c478bd9Sstevel@tonic-gate /*
59737c478bd9Sstevel@tonic-gate  * Send an ioctl message downstream and wait for acknowledgement.
59747c478bd9Sstevel@tonic-gate  * flags may be set to either U_TO_K or K_TO_K and a combination
59757c478bd9Sstevel@tonic-gate  * of STR_NOERROR or STR_NOSIG
59767c478bd9Sstevel@tonic-gate  * STR_NOSIG: Signals are essentially ignored or held and have
59777c478bd9Sstevel@tonic-gate  *	no effect for the duration of the call.
59787c478bd9Sstevel@tonic-gate  * STR_NOERROR: Ignores stream head read, write and hup errors.
59797c478bd9Sstevel@tonic-gate  *	Additionally, if an existing ioctl times out, it is assumed
59807c478bd9Sstevel@tonic-gate  *	lost and and this ioctl will continue as if the previous ioctl had
59817c478bd9Sstevel@tonic-gate  *	finished.  ETIME may be returned if this ioctl times out (i.e.
59827c478bd9Sstevel@tonic-gate  *	ic_timout is not INFTIM).  Non-stream head errors may be returned if
59837c478bd9Sstevel@tonic-gate  *	the ioc_error indicates that the driver/module had problems,
59847c478bd9Sstevel@tonic-gate  *	an EFAULT was found when accessing user data, a lack of
59857c478bd9Sstevel@tonic-gate  * 	resources, etc.
59867c478bd9Sstevel@tonic-gate  */
59877c478bd9Sstevel@tonic-gate int
59887c478bd9Sstevel@tonic-gate strdoioctl(
59897c478bd9Sstevel@tonic-gate 	struct stdata *stp,
59907c478bd9Sstevel@tonic-gate 	struct strioctl *strioc,
59917c478bd9Sstevel@tonic-gate 	int fflags,		/* file flags with model info */
59927c478bd9Sstevel@tonic-gate 	int flag,
59937c478bd9Sstevel@tonic-gate 	cred_t *crp,
59947c478bd9Sstevel@tonic-gate 	int *rvalp)
59957c478bd9Sstevel@tonic-gate {
59967c478bd9Sstevel@tonic-gate 	mblk_t *bp;
59977c478bd9Sstevel@tonic-gate 	struct iocblk *iocbp;
59987c478bd9Sstevel@tonic-gate 	struct copyreq *reqp;
59997c478bd9Sstevel@tonic-gate 	struct copyresp *resp;
60007c478bd9Sstevel@tonic-gate 	int id;
60017c478bd9Sstevel@tonic-gate 	int transparent = 0;
60027c478bd9Sstevel@tonic-gate 	int error = 0;
60037c478bd9Sstevel@tonic-gate 	int len = 0;
60047c478bd9Sstevel@tonic-gate 	caddr_t taddr;
60057c478bd9Sstevel@tonic-gate 	int copyflag = (flag & (U_TO_K | K_TO_K));
60067c478bd9Sstevel@tonic-gate 	int sigflag = (flag & STR_NOSIG);
60077c478bd9Sstevel@tonic-gate 	int errs;
60087c478bd9Sstevel@tonic-gate 	uint_t waitflags;
60097c478bd9Sstevel@tonic-gate 
60107c478bd9Sstevel@tonic-gate 	ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
60117c478bd9Sstevel@tonic-gate 	ASSERT((fflags & FMODELS) != 0);
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_STREAMS_FR,
60147c478bd9Sstevel@tonic-gate 	    TR_STRDOIOCTL,
60157c478bd9Sstevel@tonic-gate 	    "strdoioctl:stp %p strioc %p", stp, strioc);
60167c478bd9Sstevel@tonic-gate 	if (strioc->ic_len == TRANSPARENT) {	/* send arg in M_DATA block */
60177c478bd9Sstevel@tonic-gate 		transparent = 1;
60187c478bd9Sstevel@tonic-gate 		strioc->ic_len = sizeof (intptr_t);
60197c478bd9Sstevel@tonic-gate 	}
60207c478bd9Sstevel@tonic-gate 
60217c478bd9Sstevel@tonic-gate 	if (strioc->ic_len < 0 || (strmsgsz > 0 && strioc->ic_len > strmsgsz))
60227c478bd9Sstevel@tonic-gate 		return (EINVAL);
60237c478bd9Sstevel@tonic-gate 
60247c478bd9Sstevel@tonic-gate 	if ((bp = allocb_cred_wait(sizeof (union ioctypes), sigflag, &error,
60257c478bd9Sstevel@tonic-gate 	    crp)) == NULL)
60267c478bd9Sstevel@tonic-gate 			return (error);
60277c478bd9Sstevel@tonic-gate 
60287c478bd9Sstevel@tonic-gate 	bzero(bp->b_wptr, sizeof (union ioctypes));
60297c478bd9Sstevel@tonic-gate 
60307c478bd9Sstevel@tonic-gate 	iocbp = (struct iocblk *)bp->b_wptr;
60317c478bd9Sstevel@tonic-gate 	iocbp->ioc_count = strioc->ic_len;
60327c478bd9Sstevel@tonic-gate 	iocbp->ioc_cmd = strioc->ic_cmd;
60337c478bd9Sstevel@tonic-gate 	iocbp->ioc_flag = (fflags & FMODELS);
60347c478bd9Sstevel@tonic-gate 
60357c478bd9Sstevel@tonic-gate 	crhold(crp);
60367c478bd9Sstevel@tonic-gate 	iocbp->ioc_cr = crp;
60377c478bd9Sstevel@tonic-gate 	DB_TYPE(bp) = M_IOCTL;
60387c478bd9Sstevel@tonic-gate 	DB_CPID(bp) = curproc->p_pid;
60397c478bd9Sstevel@tonic-gate 	bp->b_wptr += sizeof (struct iocblk);
60407c478bd9Sstevel@tonic-gate 
60417c478bd9Sstevel@tonic-gate 	if (flag & STR_NOERROR)
60427c478bd9Sstevel@tonic-gate 		errs = STPLEX;
60437c478bd9Sstevel@tonic-gate 	else
60447c478bd9Sstevel@tonic-gate 		errs = STRHUP|STRDERR|STWRERR|STPLEX;
60457c478bd9Sstevel@tonic-gate 
60467c478bd9Sstevel@tonic-gate 	/*
60477c478bd9Sstevel@tonic-gate 	 * If there is data to copy into ioctl block, do so.
60487c478bd9Sstevel@tonic-gate 	 */
60497c478bd9Sstevel@tonic-gate 	if (iocbp->ioc_count > 0) {
60507c478bd9Sstevel@tonic-gate 		if (transparent)
60517c478bd9Sstevel@tonic-gate 			/*
60527c478bd9Sstevel@tonic-gate 			 * Note: STR_NOERROR does not have an effect
60537c478bd9Sstevel@tonic-gate 			 * in putiocd()
60547c478bd9Sstevel@tonic-gate 			 */
60557c478bd9Sstevel@tonic-gate 			id = K_TO_K | sigflag;
60567c478bd9Sstevel@tonic-gate 		else
60577c478bd9Sstevel@tonic-gate 			id = flag;
60587c478bd9Sstevel@tonic-gate 		if ((error = putiocd(bp, strioc->ic_dp, id, crp)) != 0) {
60597c478bd9Sstevel@tonic-gate 			freemsg(bp);
60607c478bd9Sstevel@tonic-gate 			crfree(crp);
60617c478bd9Sstevel@tonic-gate 			return (error);
60627c478bd9Sstevel@tonic-gate 		}
60637c478bd9Sstevel@tonic-gate 
60647c478bd9Sstevel@tonic-gate 		/*
60657c478bd9Sstevel@tonic-gate 		 * We could have slept copying in user pages.
60667c478bd9Sstevel@tonic-gate 		 * Recheck the stream head state (the other end
60677c478bd9Sstevel@tonic-gate 		 * of a pipe could have gone away).
60687c478bd9Sstevel@tonic-gate 		 */
60697c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & errs) {
60707c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
60717c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, errs, 0);
60727c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
60737c478bd9Sstevel@tonic-gate 			if (error != 0) {
60747c478bd9Sstevel@tonic-gate 				freemsg(bp);
60757c478bd9Sstevel@tonic-gate 				crfree(crp);
60767c478bd9Sstevel@tonic-gate 				return (error);
60777c478bd9Sstevel@tonic-gate 			}
60787c478bd9Sstevel@tonic-gate 		}
60797c478bd9Sstevel@tonic-gate 	}
60807c478bd9Sstevel@tonic-gate 	if (transparent)
60817c478bd9Sstevel@tonic-gate 		iocbp->ioc_count = TRANSPARENT;
60827c478bd9Sstevel@tonic-gate 
60837c478bd9Sstevel@tonic-gate 	/*
60847c478bd9Sstevel@tonic-gate 	 * Block for up to STRTIMOUT milliseconds if there is an outstanding
60857c478bd9Sstevel@tonic-gate 	 * ioctl for this stream already running.  All processes
60867c478bd9Sstevel@tonic-gate 	 * sleeping here will be awakened as a result of an ACK
60877c478bd9Sstevel@tonic-gate 	 * or NAK being received for the outstanding ioctl, or
60887c478bd9Sstevel@tonic-gate 	 * as a result of the timer expiring on the outstanding
60897c478bd9Sstevel@tonic-gate 	 * ioctl (a failure), or as a result of any waiting
60907c478bd9Sstevel@tonic-gate 	 * process's timer expiring (also a failure).
60917c478bd9Sstevel@tonic-gate 	 */
60927c478bd9Sstevel@tonic-gate 
60937c478bd9Sstevel@tonic-gate 	error = 0;
60947c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
60957c478bd9Sstevel@tonic-gate 	while (stp->sd_flag & (IOCWAIT | IOCWAITNE)) {
60967c478bd9Sstevel@tonic-gate 		clock_t cv_rval;
60977c478bd9Sstevel@tonic-gate 
60987c478bd9Sstevel@tonic-gate 		TRACE_0(TR_FAC_STREAMS_FR,
60997c478bd9Sstevel@tonic-gate 		    TR_STRDOIOCTL_WAIT,
61007c478bd9Sstevel@tonic-gate 		    "strdoioctl sleeps - IOCWAIT");
61017c478bd9Sstevel@tonic-gate 		cv_rval = str_cv_wait(&stp->sd_iocmonitor, &stp->sd_lock,
61027c478bd9Sstevel@tonic-gate 		    STRTIMOUT, sigflag);
61037c478bd9Sstevel@tonic-gate 		if (cv_rval <= 0) {
61047c478bd9Sstevel@tonic-gate 			if (cv_rval == 0) {
61057c478bd9Sstevel@tonic-gate 				error = EINTR;
61067c478bd9Sstevel@tonic-gate 			} else {
61077c478bd9Sstevel@tonic-gate 				if (flag & STR_NOERROR) {
61087c478bd9Sstevel@tonic-gate 					/*
61097c478bd9Sstevel@tonic-gate 					 * Terminating current ioctl in
61107c478bd9Sstevel@tonic-gate 					 * progress -- assume it got lost and
61117c478bd9Sstevel@tonic-gate 					 * wake up the other thread so that the
61127c478bd9Sstevel@tonic-gate 					 * operation completes.
61137c478bd9Sstevel@tonic-gate 					 */
61147c478bd9Sstevel@tonic-gate 					if (!(stp->sd_flag & IOCWAITNE)) {
61157c478bd9Sstevel@tonic-gate 						stp->sd_flag |= IOCWAITNE;
61167c478bd9Sstevel@tonic-gate 						cv_broadcast(&stp->sd_monitor);
61177c478bd9Sstevel@tonic-gate 					}
61187c478bd9Sstevel@tonic-gate 					/*
61197c478bd9Sstevel@tonic-gate 					 * Otherwise, there's a running
61207c478bd9Sstevel@tonic-gate 					 * STR_NOERROR -- we have no choice
61217c478bd9Sstevel@tonic-gate 					 * here but to wait forever (or until
61227c478bd9Sstevel@tonic-gate 					 * interrupted).
61237c478bd9Sstevel@tonic-gate 					 */
61247c478bd9Sstevel@tonic-gate 				} else {
61257c478bd9Sstevel@tonic-gate 					/*
61267c478bd9Sstevel@tonic-gate 					 * pending ioctl has caused
61277c478bd9Sstevel@tonic-gate 					 * us to time out
61287c478bd9Sstevel@tonic-gate 					 */
61297c478bd9Sstevel@tonic-gate 					error = ETIME;
61307c478bd9Sstevel@tonic-gate 				}
61317c478bd9Sstevel@tonic-gate 			}
61327c478bd9Sstevel@tonic-gate 		} else if ((stp->sd_flag & errs)) {
61337c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, errs, 0);
61347c478bd9Sstevel@tonic-gate 		}
61357c478bd9Sstevel@tonic-gate 		if (error) {
61367c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
61377c478bd9Sstevel@tonic-gate 			freemsg(bp);
61387c478bd9Sstevel@tonic-gate 			crfree(crp);
61397c478bd9Sstevel@tonic-gate 			return (error);
61407c478bd9Sstevel@tonic-gate 		}
61417c478bd9Sstevel@tonic-gate 	}
61427c478bd9Sstevel@tonic-gate 
61437c478bd9Sstevel@tonic-gate 	/*
61447c478bd9Sstevel@tonic-gate 	 * Have control of ioctl mechanism.
61457c478bd9Sstevel@tonic-gate 	 * Send down ioctl packet and wait for response.
61467c478bd9Sstevel@tonic-gate 	 */
61477c478bd9Sstevel@tonic-gate 	if (stp->sd_iocblk != (mblk_t *)-1) {
61487c478bd9Sstevel@tonic-gate 		freemsg(stp->sd_iocblk);
61497c478bd9Sstevel@tonic-gate 	}
61507c478bd9Sstevel@tonic-gate 	stp->sd_iocblk = NULL;
61517c478bd9Sstevel@tonic-gate 
61527c478bd9Sstevel@tonic-gate 	/*
61537c478bd9Sstevel@tonic-gate 	 * If this is marked with 'noerror' (internal; mostly
61547c478bd9Sstevel@tonic-gate 	 * I_{P,}{UN,}LINK), then make sure nobody else is able to get
61557c478bd9Sstevel@tonic-gate 	 * in here by setting IOCWAITNE.
61567c478bd9Sstevel@tonic-gate 	 */
61577c478bd9Sstevel@tonic-gate 	waitflags = IOCWAIT;
61587c478bd9Sstevel@tonic-gate 	if (flag & STR_NOERROR)
61597c478bd9Sstevel@tonic-gate 		waitflags |= IOCWAITNE;
61607c478bd9Sstevel@tonic-gate 
61617c478bd9Sstevel@tonic-gate 	stp->sd_flag |= waitflags;
61627c478bd9Sstevel@tonic-gate 
61637c478bd9Sstevel@tonic-gate 	/*
61647c478bd9Sstevel@tonic-gate 	 * Assign sequence number.
61657c478bd9Sstevel@tonic-gate 	 */
61667c478bd9Sstevel@tonic-gate 	iocbp->ioc_id = stp->sd_iocid = getiocseqno();
61677c478bd9Sstevel@tonic-gate 
61687c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
61697c478bd9Sstevel@tonic-gate 
61707c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
61717c478bd9Sstevel@tonic-gate 	    TR_STRDOIOCTL_PUT, "strdoioctl put: stp %p", stp);
61727c478bd9Sstevel@tonic-gate 	stream_willservice(stp);
61737c478bd9Sstevel@tonic-gate 	putnext(stp->sd_wrq, bp);
61747c478bd9Sstevel@tonic-gate 	stream_runservice(stp);
61757c478bd9Sstevel@tonic-gate 
61767c478bd9Sstevel@tonic-gate 	/*
61777c478bd9Sstevel@tonic-gate 	 * Timed wait for acknowledgment.  The wait time is limited by the
61787c478bd9Sstevel@tonic-gate 	 * timeout value, which must be a positive integer (number of
6179c28749e9Skais 	 * milliseconds) to wait, or 0 (use default value of STRTIMOUT
61807c478bd9Sstevel@tonic-gate 	 * milliseconds), or -1 (wait forever).  This will be awakened
61817c478bd9Sstevel@tonic-gate 	 * either by an ACK/NAK message arriving, the timer expiring, or
61827c478bd9Sstevel@tonic-gate 	 * the timer expiring on another ioctl waiting for control of the
61837c478bd9Sstevel@tonic-gate 	 * mechanism.
61847c478bd9Sstevel@tonic-gate 	 */
61857c478bd9Sstevel@tonic-gate waitioc:
61867c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
61877c478bd9Sstevel@tonic-gate 
61887c478bd9Sstevel@tonic-gate 
61897c478bd9Sstevel@tonic-gate 	/*
61907c478bd9Sstevel@tonic-gate 	 * If the reply has already arrived, don't sleep.  If awakened from
61917c478bd9Sstevel@tonic-gate 	 * the sleep, fail only if the reply has not arrived by then.
61927c478bd9Sstevel@tonic-gate 	 * Otherwise, process the reply.
61937c478bd9Sstevel@tonic-gate 	 */
61947c478bd9Sstevel@tonic-gate 	while (!stp->sd_iocblk) {
61957c478bd9Sstevel@tonic-gate 		clock_t cv_rval;
61967c478bd9Sstevel@tonic-gate 
61977c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & errs) {
61987c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, errs, 0);
61997c478bd9Sstevel@tonic-gate 			if (error != 0) {
62007c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~waitflags;
62017c478bd9Sstevel@tonic-gate 				cv_broadcast(&stp->sd_iocmonitor);
62027c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
62037c478bd9Sstevel@tonic-gate 				crfree(crp);
62047c478bd9Sstevel@tonic-gate 				return (error);
62057c478bd9Sstevel@tonic-gate 			}
62067c478bd9Sstevel@tonic-gate 		}
62077c478bd9Sstevel@tonic-gate 
62087c478bd9Sstevel@tonic-gate 		TRACE_0(TR_FAC_STREAMS_FR,
62097c478bd9Sstevel@tonic-gate 		    TR_STRDOIOCTL_WAIT2,
62107c478bd9Sstevel@tonic-gate 		    "strdoioctl sleeps awaiting reply");
62117c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
62127c478bd9Sstevel@tonic-gate 
62137c478bd9Sstevel@tonic-gate 		cv_rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock,
62147c478bd9Sstevel@tonic-gate 		    (strioc->ic_timout ?
62157c478bd9Sstevel@tonic-gate 		    strioc->ic_timout * 1000 : STRTIMOUT), sigflag);
62167c478bd9Sstevel@tonic-gate 
62177c478bd9Sstevel@tonic-gate 		/*
62187c478bd9Sstevel@tonic-gate 		 * There are four possible cases here: interrupt, timeout,
62197c478bd9Sstevel@tonic-gate 		 * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
62207c478bd9Sstevel@tonic-gate 		 * valid M_IOCTL reply).
62217c478bd9Sstevel@tonic-gate 		 *
62227c478bd9Sstevel@tonic-gate 		 * If we've been awakened by a STR_NOERROR ioctl on some other
62237c478bd9Sstevel@tonic-gate 		 * thread, then sd_iocblk will still be NULL, and IOCWAITNE
62247c478bd9Sstevel@tonic-gate 		 * will be set.  Pretend as if we just timed out.  Note that
62257c478bd9Sstevel@tonic-gate 		 * this other thread waited at least STRTIMOUT before trying to
62267c478bd9Sstevel@tonic-gate 		 * awaken our thread, so this is indistinguishable (even for
62277c478bd9Sstevel@tonic-gate 		 * INFTIM) from the case where we failed with ETIME waiting on
62287c478bd9Sstevel@tonic-gate 		 * IOCWAIT in the prior loop.
62297c478bd9Sstevel@tonic-gate 		 */
62307c478bd9Sstevel@tonic-gate 		if (cv_rval > 0 && !(flag & STR_NOERROR) &&
62317c478bd9Sstevel@tonic-gate 		    stp->sd_iocblk == NULL && (stp->sd_flag & IOCWAITNE)) {
62327c478bd9Sstevel@tonic-gate 			cv_rval = -1;
62337c478bd9Sstevel@tonic-gate 		}
62347c478bd9Sstevel@tonic-gate 
62357c478bd9Sstevel@tonic-gate 		/*
62367c478bd9Sstevel@tonic-gate 		 * note: STR_NOERROR does not protect
62377c478bd9Sstevel@tonic-gate 		 * us here.. use ic_timout < 0
62387c478bd9Sstevel@tonic-gate 		 */
62397c478bd9Sstevel@tonic-gate 		if (cv_rval <= 0) {
62407c478bd9Sstevel@tonic-gate 			if (cv_rval == 0) {
62417c478bd9Sstevel@tonic-gate 				error = EINTR;
62427c478bd9Sstevel@tonic-gate 			} else {
62437c478bd9Sstevel@tonic-gate 				error =  ETIME;
62447c478bd9Sstevel@tonic-gate 			}
62457c478bd9Sstevel@tonic-gate 			/*
62467c478bd9Sstevel@tonic-gate 			 * A message could have come in after we were scheduled
62477c478bd9Sstevel@tonic-gate 			 * but before we were actually run.
62487c478bd9Sstevel@tonic-gate 			 */
62497c478bd9Sstevel@tonic-gate 			bp = stp->sd_iocblk;
62507c478bd9Sstevel@tonic-gate 			stp->sd_iocblk = NULL;
62517c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
62527c478bd9Sstevel@tonic-gate 				if ((bp->b_datap->db_type == M_COPYIN) ||
62537c478bd9Sstevel@tonic-gate 				    (bp->b_datap->db_type == M_COPYOUT)) {
62547c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
62557c478bd9Sstevel@tonic-gate 					if (bp->b_cont) {
62567c478bd9Sstevel@tonic-gate 						freemsg(bp->b_cont);
62577c478bd9Sstevel@tonic-gate 						bp->b_cont = NULL;
62587c478bd9Sstevel@tonic-gate 					}
62597c478bd9Sstevel@tonic-gate 					bp->b_datap->db_type = M_IOCDATA;
62607c478bd9Sstevel@tonic-gate 					bp->b_wptr = bp->b_rptr +
62617c478bd9Sstevel@tonic-gate 					    sizeof (struct copyresp);
62627c478bd9Sstevel@tonic-gate 					resp = (struct copyresp *)bp->b_rptr;
62637c478bd9Sstevel@tonic-gate 					resp->cp_rval =
62647c478bd9Sstevel@tonic-gate 					    (caddr_t)1; /* failure */
62657c478bd9Sstevel@tonic-gate 					stream_willservice(stp);
62667c478bd9Sstevel@tonic-gate 					putnext(stp->sd_wrq, bp);
62677c478bd9Sstevel@tonic-gate 					stream_runservice(stp);
62687c478bd9Sstevel@tonic-gate 					mutex_enter(&stp->sd_lock);
62697c478bd9Sstevel@tonic-gate 				} else {
62707c478bd9Sstevel@tonic-gate 					freemsg(bp);
62717c478bd9Sstevel@tonic-gate 				}
62727c478bd9Sstevel@tonic-gate 			}
62737c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~waitflags;
62747c478bd9Sstevel@tonic-gate 			cv_broadcast(&stp->sd_iocmonitor);
62757c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
62767c478bd9Sstevel@tonic-gate 			crfree(crp);
62777c478bd9Sstevel@tonic-gate 			return (error);
62787c478bd9Sstevel@tonic-gate 		}
62797c478bd9Sstevel@tonic-gate 	}
62807c478bd9Sstevel@tonic-gate 	bp = stp->sd_iocblk;
62817c478bd9Sstevel@tonic-gate 	/*
62827c478bd9Sstevel@tonic-gate 	 * Note: it is strictly impossible to get here with sd_iocblk set to
62837c478bd9Sstevel@tonic-gate 	 * -1.  This is because the initial loop above doesn't allow any new
62847c478bd9Sstevel@tonic-gate 	 * ioctls into the fray until all others have passed this point.
62857c478bd9Sstevel@tonic-gate 	 */
62867c478bd9Sstevel@tonic-gate 	ASSERT(bp != NULL && bp != (mblk_t *)-1);
62877c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
62887c478bd9Sstevel@tonic-gate 	    TR_STRDOIOCTL_ACK, "strdoioctl got reply: bp %p", bp);
62897c478bd9Sstevel@tonic-gate 	if ((bp->b_datap->db_type == M_IOCACK) ||
62907c478bd9Sstevel@tonic-gate 	    (bp->b_datap->db_type == M_IOCNAK)) {
62917c478bd9Sstevel@tonic-gate 		/* for detection of duplicate ioctl replies */
62927c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = (mblk_t *)-1;
62937c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~waitflags;
62947c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_iocmonitor);
62957c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
62967c478bd9Sstevel@tonic-gate 	} else {
62977c478bd9Sstevel@tonic-gate 		/*
62987c478bd9Sstevel@tonic-gate 		 * flags not cleared here because we're still doing
62997c478bd9Sstevel@tonic-gate 		 * copy in/out for ioctl.
63007c478bd9Sstevel@tonic-gate 		 */
63017c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = NULL;
63027c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
63037c478bd9Sstevel@tonic-gate 	}
63047c478bd9Sstevel@tonic-gate 
63057c478bd9Sstevel@tonic-gate 
63067c478bd9Sstevel@tonic-gate 	/*
63077c478bd9Sstevel@tonic-gate 	 * Have received acknowledgment.
63087c478bd9Sstevel@tonic-gate 	 */
63097c478bd9Sstevel@tonic-gate 
63107c478bd9Sstevel@tonic-gate 	switch (bp->b_datap->db_type) {
63117c478bd9Sstevel@tonic-gate 	case M_IOCACK:
63127c478bd9Sstevel@tonic-gate 		/*
63137c478bd9Sstevel@tonic-gate 		 * Positive ack.
63147c478bd9Sstevel@tonic-gate 		 */
63157c478bd9Sstevel@tonic-gate 		iocbp = (struct iocblk *)bp->b_rptr;
63167c478bd9Sstevel@tonic-gate 
63177c478bd9Sstevel@tonic-gate 		/*
63187c478bd9Sstevel@tonic-gate 		 * Set error if indicated.
63197c478bd9Sstevel@tonic-gate 		 */
63207c478bd9Sstevel@tonic-gate 		if (iocbp->ioc_error) {
63217c478bd9Sstevel@tonic-gate 			error = iocbp->ioc_error;
63227c478bd9Sstevel@tonic-gate 			break;
63237c478bd9Sstevel@tonic-gate 		}
63247c478bd9Sstevel@tonic-gate 
63257c478bd9Sstevel@tonic-gate 		/*
63267c478bd9Sstevel@tonic-gate 		 * Set return value.
63277c478bd9Sstevel@tonic-gate 		 */
63287c478bd9Sstevel@tonic-gate 		*rvalp = iocbp->ioc_rval;
63297c478bd9Sstevel@tonic-gate 
63307c478bd9Sstevel@tonic-gate 		/*
63317c478bd9Sstevel@tonic-gate 		 * Data may have been returned in ACK message (ioc_count > 0).
63327c478bd9Sstevel@tonic-gate 		 * If so, copy it out to the user's buffer.
63337c478bd9Sstevel@tonic-gate 		 */
63347c478bd9Sstevel@tonic-gate 		if (iocbp->ioc_count && !transparent) {
63357c478bd9Sstevel@tonic-gate 			if (error = getiocd(bp, strioc->ic_dp, copyflag))
63367c478bd9Sstevel@tonic-gate 				break;
63377c478bd9Sstevel@tonic-gate 		}
63387c478bd9Sstevel@tonic-gate 		if (!transparent) {
63397c478bd9Sstevel@tonic-gate 			if (len)	/* an M_COPYOUT was used with I_STR */
63407c478bd9Sstevel@tonic-gate 				strioc->ic_len = len;
63417c478bd9Sstevel@tonic-gate 			else
63427c478bd9Sstevel@tonic-gate 				strioc->ic_len = (int)iocbp->ioc_count;
63437c478bd9Sstevel@tonic-gate 		}
63447c478bd9Sstevel@tonic-gate 		break;
63457c478bd9Sstevel@tonic-gate 
63467c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
63477c478bd9Sstevel@tonic-gate 		/*
63487c478bd9Sstevel@tonic-gate 		 * Negative ack.
63497c478bd9Sstevel@tonic-gate 		 *
63507c478bd9Sstevel@tonic-gate 		 * The only thing to do is set error as specified
63517c478bd9Sstevel@tonic-gate 		 * in neg ack packet.
63527c478bd9Sstevel@tonic-gate 		 */
63537c478bd9Sstevel@tonic-gate 		iocbp = (struct iocblk *)bp->b_rptr;
63547c478bd9Sstevel@tonic-gate 
63557c478bd9Sstevel@tonic-gate 		error = (iocbp->ioc_error ? iocbp->ioc_error : EINVAL);
63567c478bd9Sstevel@tonic-gate 		break;
63577c478bd9Sstevel@tonic-gate 
63587c478bd9Sstevel@tonic-gate 	case M_COPYIN:
63597c478bd9Sstevel@tonic-gate 		/*
63607c478bd9Sstevel@tonic-gate 		 * Driver or module has requested user ioctl data.
63617c478bd9Sstevel@tonic-gate 		 */
63627c478bd9Sstevel@tonic-gate 		reqp = (struct copyreq *)bp->b_rptr;
63637c478bd9Sstevel@tonic-gate 
63647c478bd9Sstevel@tonic-gate 		/*
63657c478bd9Sstevel@tonic-gate 		 * M_COPYIN should *never* have a message attached, though
63667c478bd9Sstevel@tonic-gate 		 * it's harmless if it does -- thus, panic on a DEBUG
63677c478bd9Sstevel@tonic-gate 		 * kernel and just free it on a non-DEBUG build.
63687c478bd9Sstevel@tonic-gate 		 */
63697c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_cont == NULL);
63707c478bd9Sstevel@tonic-gate 		if (bp->b_cont != NULL) {
63717c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
63727c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
63737c478bd9Sstevel@tonic-gate 		}
63747c478bd9Sstevel@tonic-gate 
63757c478bd9Sstevel@tonic-gate 		error = putiocd(bp, reqp->cq_addr, flag, crp);
63767c478bd9Sstevel@tonic-gate 		if (error && bp->b_cont) {
63777c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
63787c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
63797c478bd9Sstevel@tonic-gate 		}
63807c478bd9Sstevel@tonic-gate 
63817c478bd9Sstevel@tonic-gate 		bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
63827c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCDATA;
63837c478bd9Sstevel@tonic-gate 
63847c478bd9Sstevel@tonic-gate 		mblk_setcred(bp, crp);
63857c478bd9Sstevel@tonic-gate 		DB_CPID(bp) = curproc->p_pid;
63867c478bd9Sstevel@tonic-gate 		resp = (struct copyresp *)bp->b_rptr;
63877c478bd9Sstevel@tonic-gate 		resp->cp_rval = (caddr_t)(uintptr_t)error;
63887c478bd9Sstevel@tonic-gate 		resp->cp_flag = (fflags & FMODELS);
63897c478bd9Sstevel@tonic-gate 
63907c478bd9Sstevel@tonic-gate 		stream_willservice(stp);
63917c478bd9Sstevel@tonic-gate 		putnext(stp->sd_wrq, bp);
63927c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
63937c478bd9Sstevel@tonic-gate 
63947c478bd9Sstevel@tonic-gate 		if (error) {
63957c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
63967c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~waitflags;
63977c478bd9Sstevel@tonic-gate 			cv_broadcast(&stp->sd_iocmonitor);
63987c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
63997c478bd9Sstevel@tonic-gate 			crfree(crp);
64007c478bd9Sstevel@tonic-gate 			return (error);
64017c478bd9Sstevel@tonic-gate 		}
64027c478bd9Sstevel@tonic-gate 
64037c478bd9Sstevel@tonic-gate 		goto waitioc;
64047c478bd9Sstevel@tonic-gate 
64057c478bd9Sstevel@tonic-gate 	case M_COPYOUT:
64067c478bd9Sstevel@tonic-gate 		/*
64077c478bd9Sstevel@tonic-gate 		 * Driver or module has ioctl data for a user.
64087c478bd9Sstevel@tonic-gate 		 */
64097c478bd9Sstevel@tonic-gate 		reqp = (struct copyreq *)bp->b_rptr;
64107c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_cont != NULL);
64117c478bd9Sstevel@tonic-gate 
64127c478bd9Sstevel@tonic-gate 		/*
64137c478bd9Sstevel@tonic-gate 		 * Always (transparent or non-transparent )
64147c478bd9Sstevel@tonic-gate 		 * use the address specified in the request
64157c478bd9Sstevel@tonic-gate 		 */
64167c478bd9Sstevel@tonic-gate 		taddr = reqp->cq_addr;
64177c478bd9Sstevel@tonic-gate 		if (!transparent)
64187c478bd9Sstevel@tonic-gate 			len = (int)reqp->cq_size;
64197c478bd9Sstevel@tonic-gate 
64207c478bd9Sstevel@tonic-gate 		/* copyout data to the provided address */
64217c478bd9Sstevel@tonic-gate 		error = getiocd(bp, taddr, copyflag);
64227c478bd9Sstevel@tonic-gate 
64237c478bd9Sstevel@tonic-gate 		freemsg(bp->b_cont);
64247c478bd9Sstevel@tonic-gate 		bp->b_cont = NULL;
64257c478bd9Sstevel@tonic-gate 
64267c478bd9Sstevel@tonic-gate 		bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
64277c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCDATA;
64287c478bd9Sstevel@tonic-gate 
64297c478bd9Sstevel@tonic-gate 		mblk_setcred(bp, crp);
64307c478bd9Sstevel@tonic-gate 		DB_CPID(bp) = curproc->p_pid;
64317c478bd9Sstevel@tonic-gate 		resp = (struct copyresp *)bp->b_rptr;
64327c478bd9Sstevel@tonic-gate 		resp->cp_rval = (caddr_t)(uintptr_t)error;
64337c478bd9Sstevel@tonic-gate 		resp->cp_flag = (fflags & FMODELS);
64347c478bd9Sstevel@tonic-gate 
64357c478bd9Sstevel@tonic-gate 		stream_willservice(stp);
64367c478bd9Sstevel@tonic-gate 		putnext(stp->sd_wrq, bp);
64377c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
64387c478bd9Sstevel@tonic-gate 
64397c478bd9Sstevel@tonic-gate 		if (error) {
64407c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
64417c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~waitflags;
64427c478bd9Sstevel@tonic-gate 			cv_broadcast(&stp->sd_iocmonitor);
64437c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
64447c478bd9Sstevel@tonic-gate 			crfree(crp);
64457c478bd9Sstevel@tonic-gate 			return (error);
64467c478bd9Sstevel@tonic-gate 		}
64477c478bd9Sstevel@tonic-gate 		goto waitioc;
64487c478bd9Sstevel@tonic-gate 
64497c478bd9Sstevel@tonic-gate 	default:
64507c478bd9Sstevel@tonic-gate 		ASSERT(0);
64517c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
64527c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~waitflags;
64537c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_iocmonitor);
64547c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
64557c478bd9Sstevel@tonic-gate 		break;
64567c478bd9Sstevel@tonic-gate 	}
64577c478bd9Sstevel@tonic-gate 
64587c478bd9Sstevel@tonic-gate 	freemsg(bp);
64597c478bd9Sstevel@tonic-gate 	crfree(crp);
64607c478bd9Sstevel@tonic-gate 	return (error);
64617c478bd9Sstevel@tonic-gate }
64627c478bd9Sstevel@tonic-gate 
64637c478bd9Sstevel@tonic-gate /*
6464ca9327a6Smeem  * Send an M_CMD message downstream and wait for a reply.  This is a ptools
6465ca9327a6Smeem  * special used to retrieve information from modules/drivers a stream without
6466ca9327a6Smeem  * being subjected to flow control or interfering with pending messages on the
6467ca9327a6Smeem  * stream (e.g. an ioctl in flight).
6468ca9327a6Smeem  */
6469ca9327a6Smeem int
6470ca9327a6Smeem strdocmd(struct stdata *stp, struct strcmd *scp, cred_t *crp)
6471ca9327a6Smeem {
6472ca9327a6Smeem 	mblk_t *mp;
6473ca9327a6Smeem 	struct cmdblk *cmdp;
6474ca9327a6Smeem 	int error = 0;
6475ca9327a6Smeem 	int errs = STRHUP|STRDERR|STWRERR|STPLEX;
6476ca9327a6Smeem 	clock_t rval, timeout = STRTIMOUT;
6477ca9327a6Smeem 
6478ca9327a6Smeem 	if (scp->sc_len < 0 || scp->sc_len > sizeof (scp->sc_buf) ||
6479ca9327a6Smeem 	    scp->sc_timeout < -1)
6480ca9327a6Smeem 		return (EINVAL);
6481ca9327a6Smeem 
6482ca9327a6Smeem 	if (scp->sc_timeout > 0)
6483ca9327a6Smeem 		timeout = scp->sc_timeout * MILLISEC;
6484ca9327a6Smeem 
6485ca9327a6Smeem 	if ((mp = allocb_cred(sizeof (struct cmdblk), crp)) == NULL)
6486ca9327a6Smeem 		return (ENOMEM);
6487ca9327a6Smeem 
6488ca9327a6Smeem 	crhold(crp);
6489ca9327a6Smeem 
6490ca9327a6Smeem 	cmdp = (struct cmdblk *)mp->b_wptr;
6491ca9327a6Smeem 	cmdp->cb_cr = crp;
6492ca9327a6Smeem 	cmdp->cb_cmd = scp->sc_cmd;
6493ca9327a6Smeem 	cmdp->cb_len = scp->sc_len;
6494ca9327a6Smeem 	cmdp->cb_error = 0;
6495ca9327a6Smeem 	mp->b_wptr += sizeof (struct cmdblk);
6496ca9327a6Smeem 
6497ca9327a6Smeem 	DB_TYPE(mp) = M_CMD;
6498ca9327a6Smeem 	DB_CPID(mp) = curproc->p_pid;
6499ca9327a6Smeem 
6500ca9327a6Smeem 	/*
6501ca9327a6Smeem 	 * Copy in the payload.
6502ca9327a6Smeem 	 */
6503ca9327a6Smeem 	if (cmdp->cb_len > 0) {
6504ca9327a6Smeem 		mp->b_cont = allocb_cred(sizeof (scp->sc_buf), crp);
6505ca9327a6Smeem 		if (mp->b_cont == NULL) {
6506ca9327a6Smeem 			error = ENOMEM;
6507ca9327a6Smeem 			goto out;
6508ca9327a6Smeem 		}
6509ca9327a6Smeem 
6510ca9327a6Smeem 		/* cb_len comes from sc_len, which has already been checked */
6511ca9327a6Smeem 		ASSERT(cmdp->cb_len <= sizeof (scp->sc_buf));
6512ca9327a6Smeem 		(void) bcopy(scp->sc_buf, mp->b_cont->b_wptr, cmdp->cb_len);
6513ca9327a6Smeem 		mp->b_cont->b_wptr += cmdp->cb_len;
6514ca9327a6Smeem 		DB_CPID(mp->b_cont) = curproc->p_pid;
6515ca9327a6Smeem 	}
6516ca9327a6Smeem 
6517ca9327a6Smeem 	/*
6518ca9327a6Smeem 	 * Since this mechanism is strictly for ptools, and since only one
6519ca9327a6Smeem 	 * process can be grabbed at a time, we simply fail if there's
6520ca9327a6Smeem 	 * currently an operation pending.
6521ca9327a6Smeem 	 */
6522ca9327a6Smeem 	mutex_enter(&stp->sd_lock);
6523ca9327a6Smeem 	if (stp->sd_flag & STRCMDWAIT) {
6524ca9327a6Smeem 		mutex_exit(&stp->sd_lock);
6525ca9327a6Smeem 		error = EBUSY;
6526ca9327a6Smeem 		goto out;
6527ca9327a6Smeem 	}
6528ca9327a6Smeem 	stp->sd_flag |= STRCMDWAIT;
6529ca9327a6Smeem 	ASSERT(stp->sd_cmdblk == NULL);
6530ca9327a6Smeem 	mutex_exit(&stp->sd_lock);
6531ca9327a6Smeem 
6532ca9327a6Smeem 	putnext(stp->sd_wrq, mp);
6533ca9327a6Smeem 	mp = NULL;
6534ca9327a6Smeem 
6535ca9327a6Smeem 	/*
6536ca9327a6Smeem 	 * Timed wait for acknowledgment.  If the reply has already arrived,
6537ca9327a6Smeem 	 * don't sleep.  If awakened from the sleep, fail only if the reply
6538ca9327a6Smeem 	 * has not arrived by then.  Otherwise, process the reply.
6539ca9327a6Smeem 	 */
6540ca9327a6Smeem 	mutex_enter(&stp->sd_lock);
6541ca9327a6Smeem 	while (stp->sd_cmdblk == NULL) {
6542ca9327a6Smeem 		if (stp->sd_flag & errs) {
6543ca9327a6Smeem 			if ((error = strgeterr(stp, errs, 0)) != 0)
6544ca9327a6Smeem 				goto waitout;
6545ca9327a6Smeem 		}
6546ca9327a6Smeem 
6547ca9327a6Smeem 		rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock, timeout, 0);
6548ca9327a6Smeem 		if (stp->sd_cmdblk != NULL)
6549ca9327a6Smeem 			break;
6550ca9327a6Smeem 
6551ca9327a6Smeem 		if (rval <= 0) {
6552ca9327a6Smeem 			error = (rval == 0) ? EINTR : ETIME;
6553ca9327a6Smeem 			goto waitout;
6554ca9327a6Smeem 		}
6555ca9327a6Smeem 	}
6556ca9327a6Smeem 
6557ca9327a6Smeem 	/*
6558ca9327a6Smeem 	 * We received a reply.
6559ca9327a6Smeem 	 */
6560ca9327a6Smeem 	mp = stp->sd_cmdblk;
6561ca9327a6Smeem 	stp->sd_cmdblk = NULL;
6562ca9327a6Smeem 	ASSERT(mp != NULL && DB_TYPE(mp) == M_CMD);
6563ca9327a6Smeem 	ASSERT(stp->sd_flag & STRCMDWAIT);
6564ca9327a6Smeem 	stp->sd_flag &= ~STRCMDWAIT;
6565ca9327a6Smeem 	mutex_exit(&stp->sd_lock);
6566ca9327a6Smeem 
6567ca9327a6Smeem 	cmdp = (struct cmdblk *)mp->b_rptr;
6568ca9327a6Smeem 	if ((error = cmdp->cb_error) != 0)
6569ca9327a6Smeem 		goto out;
6570ca9327a6Smeem 
6571ca9327a6Smeem 	/*
6572ca9327a6Smeem 	 * Data may have been returned in the reply (cb_len > 0).
6573ca9327a6Smeem 	 * If so, copy it out to the user's buffer.
6574ca9327a6Smeem 	 */
6575ca9327a6Smeem 	if (cmdp->cb_len > 0) {
6576ca9327a6Smeem 		if (mp->b_cont == NULL || MBLKL(mp->b_cont) < cmdp->cb_len) {
6577ca9327a6Smeem 			error = EPROTO;
6578ca9327a6Smeem 			goto out;
6579ca9327a6Smeem 		}
6580ca9327a6Smeem 
6581ca9327a6Smeem 		cmdp->cb_len = MIN(cmdp->cb_len, sizeof (scp->sc_buf));
6582ca9327a6Smeem 		(void) bcopy(mp->b_cont->b_rptr, scp->sc_buf, cmdp->cb_len);
6583ca9327a6Smeem 	}
6584ca9327a6Smeem 	scp->sc_len = cmdp->cb_len;
6585ca9327a6Smeem out:
6586ca9327a6Smeem 	freemsg(mp);
6587ca9327a6Smeem 	crfree(crp);
6588ca9327a6Smeem 	return (error);
6589ca9327a6Smeem waitout:
6590ca9327a6Smeem 	ASSERT(stp->sd_cmdblk == NULL);
6591ca9327a6Smeem 	stp->sd_flag &= ~STRCMDWAIT;
6592ca9327a6Smeem 	mutex_exit(&stp->sd_lock);
6593ca9327a6Smeem 	crfree(crp);
6594ca9327a6Smeem 	return (error);
6595ca9327a6Smeem }
6596ca9327a6Smeem 
6597ca9327a6Smeem /*
65987c478bd9Sstevel@tonic-gate  * For the SunOS keyboard driver.
65997c478bd9Sstevel@tonic-gate  * Return the next available "ioctl" sequence number.
66007c478bd9Sstevel@tonic-gate  * Exported, so that streams modules can send "ioctl" messages
66017c478bd9Sstevel@tonic-gate  * downstream from their open routine.
66027c478bd9Sstevel@tonic-gate  */
66037c478bd9Sstevel@tonic-gate int
66047c478bd9Sstevel@tonic-gate getiocseqno(void)
66057c478bd9Sstevel@tonic-gate {
66067c478bd9Sstevel@tonic-gate 	int	i;
66077c478bd9Sstevel@tonic-gate 
66087c478bd9Sstevel@tonic-gate 	mutex_enter(&strresources);
66097c478bd9Sstevel@tonic-gate 	i = ++ioc_id;
66107c478bd9Sstevel@tonic-gate 	mutex_exit(&strresources);
66117c478bd9Sstevel@tonic-gate 	return (i);
66127c478bd9Sstevel@tonic-gate }
66137c478bd9Sstevel@tonic-gate 
66147c478bd9Sstevel@tonic-gate /*
66157c478bd9Sstevel@tonic-gate  * Get the next message from the read queue.  If the message is
66167c478bd9Sstevel@tonic-gate  * priority, STRPRI will have been set by strrput().  This flag
66177c478bd9Sstevel@tonic-gate  * should be reset only when the entire message at the front of the
66187c478bd9Sstevel@tonic-gate  * queue as been consumed.
66197c478bd9Sstevel@tonic-gate  *
66207c478bd9Sstevel@tonic-gate  * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
66217c478bd9Sstevel@tonic-gate  */
66227c478bd9Sstevel@tonic-gate int
66237c478bd9Sstevel@tonic-gate strgetmsg(
66247c478bd9Sstevel@tonic-gate 	struct vnode *vp,
66257c478bd9Sstevel@tonic-gate 	struct strbuf *mctl,
66267c478bd9Sstevel@tonic-gate 	struct strbuf *mdata,
66277c478bd9Sstevel@tonic-gate 	unsigned char *prip,
66287c478bd9Sstevel@tonic-gate 	int *flagsp,
66297c478bd9Sstevel@tonic-gate 	int fmode,
66307c478bd9Sstevel@tonic-gate 	rval_t *rvp)
66317c478bd9Sstevel@tonic-gate {
66327c478bd9Sstevel@tonic-gate 	struct stdata *stp;
66337c478bd9Sstevel@tonic-gate 	mblk_t *bp, *nbp;
66347c478bd9Sstevel@tonic-gate 	mblk_t *savemp = NULL;
66357c478bd9Sstevel@tonic-gate 	mblk_t *savemptail = NULL;
66367c478bd9Sstevel@tonic-gate 	uint_t old_sd_flag;
66377c478bd9Sstevel@tonic-gate 	int flg;
66387c478bd9Sstevel@tonic-gate 	int more = 0;
66397c478bd9Sstevel@tonic-gate 	int error = 0;
66407c478bd9Sstevel@tonic-gate 	char first = 1;
66417c478bd9Sstevel@tonic-gate 	uint_t mark;		/* Contains MSG*MARK and _LASTMARK */
66427c478bd9Sstevel@tonic-gate #define	_LASTMARK	0x8000	/* Distinct from MSG*MARK */
66437c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
66447c478bd9Sstevel@tonic-gate 	queue_t *q;
66457c478bd9Sstevel@tonic-gate 	int	pr = 0;			/* Partial read successful */
66467c478bd9Sstevel@tonic-gate 	struct uio uios;
66477c478bd9Sstevel@tonic-gate 	struct uio *uiop = &uios;
66487c478bd9Sstevel@tonic-gate 	struct iovec iovs;
66497c478bd9Sstevel@tonic-gate 	unsigned char type;
66507c478bd9Sstevel@tonic-gate 
66517c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_STRGETMSG_ENTER,
66527c478bd9Sstevel@tonic-gate 	    "strgetmsg:%p", vp);
66537c478bd9Sstevel@tonic-gate 
66547c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
66557c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
66567c478bd9Sstevel@tonic-gate 	rvp->r_val1 = 0;
66577c478bd9Sstevel@tonic-gate 
66587c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
66599acbbeafSnn35248 
66609acbbeafSnn35248 	if ((error = i_straccess(stp, JCREAD)) != 0) {
66617c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
66627c478bd9Sstevel@tonic-gate 		return (error);
66637c478bd9Sstevel@tonic-gate 	}
66647c478bd9Sstevel@tonic-gate 
66659acbbeafSnn35248 	if (stp->sd_flag & (STRDERR|STPLEX)) {
66669acbbeafSnn35248 		error = strgeterr(stp, STRDERR|STPLEX, 0);
66679acbbeafSnn35248 		if (error != 0) {
66689acbbeafSnn35248 			mutex_exit(&stp->sd_lock);
66699acbbeafSnn35248 			return (error);
66709acbbeafSnn35248 		}
66719acbbeafSnn35248 	}
66729acbbeafSnn35248 	mutex_exit(&stp->sd_lock);
66739acbbeafSnn35248 
66747c478bd9Sstevel@tonic-gate 	switch (*flagsp) {
66757c478bd9Sstevel@tonic-gate 	case MSG_HIPRI:
66767c478bd9Sstevel@tonic-gate 		if (*prip != 0)
66777c478bd9Sstevel@tonic-gate 			return (EINVAL);
66787c478bd9Sstevel@tonic-gate 		break;
66797c478bd9Sstevel@tonic-gate 
66807c478bd9Sstevel@tonic-gate 	case MSG_ANY:
66817c478bd9Sstevel@tonic-gate 	case MSG_BAND:
66827c478bd9Sstevel@tonic-gate 		break;
66837c478bd9Sstevel@tonic-gate 
66847c478bd9Sstevel@tonic-gate 	default:
66857c478bd9Sstevel@tonic-gate 		return (EINVAL);
66867c478bd9Sstevel@tonic-gate 	}
66877c478bd9Sstevel@tonic-gate 	/*
66887c478bd9Sstevel@tonic-gate 	 * Setup uio and iov for data part
66897c478bd9Sstevel@tonic-gate 	 */
66907c478bd9Sstevel@tonic-gate 	iovs.iov_base = mdata->buf;
66917c478bd9Sstevel@tonic-gate 	iovs.iov_len = mdata->maxlen;
66927c478bd9Sstevel@tonic-gate 	uios.uio_iov = &iovs;
66937c478bd9Sstevel@tonic-gate 	uios.uio_iovcnt = 1;
66947c478bd9Sstevel@tonic-gate 	uios.uio_loffset = 0;
66957c478bd9Sstevel@tonic-gate 	uios.uio_segflg = UIO_USERSPACE;
66967c478bd9Sstevel@tonic-gate 	uios.uio_fmode = 0;
66977c478bd9Sstevel@tonic-gate 	uios.uio_extflg = UIO_COPY_CACHED;
66987c478bd9Sstevel@tonic-gate 	uios.uio_resid = mdata->maxlen;
66997c478bd9Sstevel@tonic-gate 	uios.uio_offset = 0;
67007c478bd9Sstevel@tonic-gate 
67017c478bd9Sstevel@tonic-gate 	q = _RD(stp->sd_wrq);
67027c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
67037c478bd9Sstevel@tonic-gate 	old_sd_flag = stp->sd_flag;
67047c478bd9Sstevel@tonic-gate 	mark = 0;
67057c478bd9Sstevel@tonic-gate 	for (;;) {
67067c478bd9Sstevel@tonic-gate 		int done = 0;
67077c478bd9Sstevel@tonic-gate 		mblk_t *q_first = q->q_first;
67087c478bd9Sstevel@tonic-gate 
67097c478bd9Sstevel@tonic-gate 		/*
67107c478bd9Sstevel@tonic-gate 		 * Get the next message of appropriate priority
67117c478bd9Sstevel@tonic-gate 		 * from the stream head.  If the caller is interested
67127c478bd9Sstevel@tonic-gate 		 * in band or hipri messages, then they should already
67137c478bd9Sstevel@tonic-gate 		 * be enqueued at the stream head.  On the other hand
67147c478bd9Sstevel@tonic-gate 		 * if the caller wants normal (band 0) messages, they
67157c478bd9Sstevel@tonic-gate 		 * might be deferred in a synchronous stream and they
67167c478bd9Sstevel@tonic-gate 		 * will need to be pulled up.
67177c478bd9Sstevel@tonic-gate 		 *
67187c478bd9Sstevel@tonic-gate 		 * After we have dequeued a message, we might find that
67197c478bd9Sstevel@tonic-gate 		 * it was a deferred M_SIG that was enqueued at the
67207c478bd9Sstevel@tonic-gate 		 * stream head.  It must now be posted as part of the
67217c478bd9Sstevel@tonic-gate 		 * read by calling strsignal_nolock().
67227c478bd9Sstevel@tonic-gate 		 *
67237c478bd9Sstevel@tonic-gate 		 * Also note that strrput does not enqueue an M_PCSIG,
67247c478bd9Sstevel@tonic-gate 		 * and there cannot be more than one hipri message,
67257c478bd9Sstevel@tonic-gate 		 * so there was no need to have the M_PCSIG case.
67267c478bd9Sstevel@tonic-gate 		 *
67277c478bd9Sstevel@tonic-gate 		 * At some time it might be nice to try and wrap the
67287c478bd9Sstevel@tonic-gate 		 * functionality of kstrgetmsg() and strgetmsg() into
67297c478bd9Sstevel@tonic-gate 		 * a common routine so to reduce the amount of replicated
67307c478bd9Sstevel@tonic-gate 		 * code (since they are extremely similar).
67317c478bd9Sstevel@tonic-gate 		 */
67327c478bd9Sstevel@tonic-gate 		if (!(*flagsp & (MSG_HIPRI|MSG_BAND))) {
67337c478bd9Sstevel@tonic-gate 			/* Asking for normal, band0 data */
67347c478bd9Sstevel@tonic-gate 			bp = strget(stp, q, uiop, first, &error);
67357c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(&stp->sd_lock));
67367c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
673717169044Sbrutus 				ASSERT(!(bp->b_datap->db_flags & DBLK_UIOA));
67387c478bd9Sstevel@tonic-gate 				if (bp->b_datap->db_type == M_SIG) {
67397c478bd9Sstevel@tonic-gate 					strsignal_nolock(stp, *bp->b_rptr,
67407c478bd9Sstevel@tonic-gate 					    (int32_t)bp->b_band);
67417c478bd9Sstevel@tonic-gate 					continue;
67427c478bd9Sstevel@tonic-gate 				} else {
67437c478bd9Sstevel@tonic-gate 					break;
67447c478bd9Sstevel@tonic-gate 				}
67457c478bd9Sstevel@tonic-gate 			}
67467c478bd9Sstevel@tonic-gate 			if (error != 0) {
67477c478bd9Sstevel@tonic-gate 				goto getmout;
67487c478bd9Sstevel@tonic-gate 			}
67497c478bd9Sstevel@tonic-gate 
67507c478bd9Sstevel@tonic-gate 		/*
67517c478bd9Sstevel@tonic-gate 		 * We can't depend on the value of STRPRI here because
67527c478bd9Sstevel@tonic-gate 		 * the stream head may be in transit. Therefore, we
67537c478bd9Sstevel@tonic-gate 		 * must look at the type of the first message to
67547c478bd9Sstevel@tonic-gate 		 * determine if a high priority messages is waiting
67557c478bd9Sstevel@tonic-gate 		 */
67567c478bd9Sstevel@tonic-gate 		} else if ((*flagsp & MSG_HIPRI) && q_first != NULL &&
67577c478bd9Sstevel@tonic-gate 		    q_first->b_datap->db_type >= QPCTL &&
6758*301ce41fSja97890 		    (bp = getq_noenab(q, 0)) != NULL) {
67597c478bd9Sstevel@tonic-gate 			/* Asked for HIPRI and got one */
67607c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_datap->db_type >= QPCTL);
67617c478bd9Sstevel@tonic-gate 			break;
67627c478bd9Sstevel@tonic-gate 		} else if ((*flagsp & MSG_BAND) && q_first != NULL &&
67637c478bd9Sstevel@tonic-gate 		    ((q_first->b_band >= *prip) ||
67647c478bd9Sstevel@tonic-gate 		    q_first->b_datap->db_type >= QPCTL) &&
6765*301ce41fSja97890 		    (bp = getq_noenab(q, 0)) != NULL) {
67667c478bd9Sstevel@tonic-gate 			/*
67677c478bd9Sstevel@tonic-gate 			 * Asked for at least band "prip" and got either at
67687c478bd9Sstevel@tonic-gate 			 * least that band or a hipri message.
67697c478bd9Sstevel@tonic-gate 			 */
67707c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_band >= *prip ||
67717c478bd9Sstevel@tonic-gate 			    bp->b_datap->db_type >= QPCTL);
67727c478bd9Sstevel@tonic-gate 			if (bp->b_datap->db_type == M_SIG) {
67737c478bd9Sstevel@tonic-gate 				strsignal_nolock(stp, *bp->b_rptr,
67747c478bd9Sstevel@tonic-gate 				    (int32_t)bp->b_band);
67757c478bd9Sstevel@tonic-gate 				continue;
67767c478bd9Sstevel@tonic-gate 			} else {
67777c478bd9Sstevel@tonic-gate 				break;
67787c478bd9Sstevel@tonic-gate 			}
67797c478bd9Sstevel@tonic-gate 		}
67807c478bd9Sstevel@tonic-gate 
67817c478bd9Sstevel@tonic-gate 		/* No data. Time to sleep? */
67827c478bd9Sstevel@tonic-gate 		qbackenable(q, 0);
67837c478bd9Sstevel@tonic-gate 
67847c478bd9Sstevel@tonic-gate 		/*
67857c478bd9Sstevel@tonic-gate 		 * If STRHUP or STREOF, return 0 length control and data.
67867c478bd9Sstevel@tonic-gate 		 * If resid is 0, then a read(fd,buf,0) was done. Do not
67877c478bd9Sstevel@tonic-gate 		 * sleep to satisfy this request because by default we have
67887c478bd9Sstevel@tonic-gate 		 * zero bytes to return.
67897c478bd9Sstevel@tonic-gate 		 */
67907c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & (STRHUP|STREOF)) || (mctl->maxlen == 0 &&
67917c478bd9Sstevel@tonic-gate 		    mdata->maxlen == 0)) {
67927c478bd9Sstevel@tonic-gate 			mctl->len = mdata->len = 0;
67937c478bd9Sstevel@tonic-gate 			*flagsp = 0;
67947c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
67957c478bd9Sstevel@tonic-gate 			return (0);
67967c478bd9Sstevel@tonic-gate 		}
67977c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_WAIT,
67987c478bd9Sstevel@tonic-gate 		    "strgetmsg calls strwaitq:%p, %p",
67997c478bd9Sstevel@tonic-gate 		    vp, uiop);
68007c478bd9Sstevel@tonic-gate 		if (((error = strwaitq(stp, GETWAIT, (ssize_t)0, fmode, -1,
68017c478bd9Sstevel@tonic-gate 		    &done)) != 0) || done) {
68027c478bd9Sstevel@tonic-gate 			TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_DONE,
68037c478bd9Sstevel@tonic-gate 			    "strgetmsg error or done:%p, %p",
68047c478bd9Sstevel@tonic-gate 			    vp, uiop);
68057c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
68067c478bd9Sstevel@tonic-gate 			return (error);
68077c478bd9Sstevel@tonic-gate 		}
68087c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_AWAKE,
68097c478bd9Sstevel@tonic-gate 		    "strgetmsg awakes:%p, %p", vp, uiop);
68109acbbeafSnn35248 		if ((error = i_straccess(stp, JCREAD)) != 0) {
68117c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
68127c478bd9Sstevel@tonic-gate 			return (error);
68137c478bd9Sstevel@tonic-gate 		}
68147c478bd9Sstevel@tonic-gate 		first = 0;
68157c478bd9Sstevel@tonic-gate 	}
68167c478bd9Sstevel@tonic-gate 	ASSERT(bp != NULL);
68177c478bd9Sstevel@tonic-gate 	/*
68187c478bd9Sstevel@tonic-gate 	 * Extract any mark information. If the message is not completely
68197c478bd9Sstevel@tonic-gate 	 * consumed this information will be put in the mblk
68207c478bd9Sstevel@tonic-gate 	 * that is putback.
68217c478bd9Sstevel@tonic-gate 	 * If MSGMARKNEXT is set and the message is completely consumed
68227c478bd9Sstevel@tonic-gate 	 * the STRATMARK flag will be set below. Likewise, if
68237c478bd9Sstevel@tonic-gate 	 * MSGNOTMARKNEXT is set and the message is
68247c478bd9Sstevel@tonic-gate 	 * completely consumed STRNOTATMARK will be set.
68257c478bd9Sstevel@tonic-gate 	 */
68267c478bd9Sstevel@tonic-gate 	mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
68277c478bd9Sstevel@tonic-gate 	ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
68287c478bd9Sstevel@tonic-gate 	    (MSGMARKNEXT|MSGNOTMARKNEXT));
68297c478bd9Sstevel@tonic-gate 	if (mark != 0 && bp == stp->sd_mark) {
68307c478bd9Sstevel@tonic-gate 		mark |= _LASTMARK;
68317c478bd9Sstevel@tonic-gate 		stp->sd_mark = NULL;
68327c478bd9Sstevel@tonic-gate 	}
68337c478bd9Sstevel@tonic-gate 	/*
68347c478bd9Sstevel@tonic-gate 	 * keep track of the original message type and priority
68357c478bd9Sstevel@tonic-gate 	 */
68367c478bd9Sstevel@tonic-gate 	pri = bp->b_band;
68377c478bd9Sstevel@tonic-gate 	type = bp->b_datap->db_type;
68387c478bd9Sstevel@tonic-gate 	if (type == M_PASSFP) {
68397c478bd9Sstevel@tonic-gate 		if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
68407c478bd9Sstevel@tonic-gate 			stp->sd_mark = bp;
68417c478bd9Sstevel@tonic-gate 		bp->b_flag |= mark & ~_LASTMARK;
68427c478bd9Sstevel@tonic-gate 		putback(stp, q, bp, pri);
68437c478bd9Sstevel@tonic-gate 		qbackenable(q, pri);
68447c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
68457c478bd9Sstevel@tonic-gate 		return (EBADMSG);
68467c478bd9Sstevel@tonic-gate 	}
68477c478bd9Sstevel@tonic-gate 	ASSERT(type != M_SIG);
68487c478bd9Sstevel@tonic-gate 
68497c478bd9Sstevel@tonic-gate 	/*
68507c478bd9Sstevel@tonic-gate 	 * Set this flag so strrput will not generate signals. Need to
68517c478bd9Sstevel@tonic-gate 	 * make sure this flag is cleared before leaving this routine
68527c478bd9Sstevel@tonic-gate 	 * else signals will stop being sent.
68537c478bd9Sstevel@tonic-gate 	 */
68547c478bd9Sstevel@tonic-gate 	stp->sd_flag |= STRGETINPROG;
68557c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
68567c478bd9Sstevel@tonic-gate 
68577c478bd9Sstevel@tonic-gate 	if (STREAM_NEEDSERVICE(stp))
68587c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
68597c478bd9Sstevel@tonic-gate 
68607c478bd9Sstevel@tonic-gate 	/*
68617c478bd9Sstevel@tonic-gate 	 * Set HIPRI flag if message is priority.
68627c478bd9Sstevel@tonic-gate 	 */
68637c478bd9Sstevel@tonic-gate 	if (type >= QPCTL)
68647c478bd9Sstevel@tonic-gate 		flg = MSG_HIPRI;
68657c478bd9Sstevel@tonic-gate 	else
68667c478bd9Sstevel@tonic-gate 		flg = MSG_BAND;
68677c478bd9Sstevel@tonic-gate 
68687c478bd9Sstevel@tonic-gate 	/*
68697c478bd9Sstevel@tonic-gate 	 * First process PROTO or PCPROTO blocks, if any.
68707c478bd9Sstevel@tonic-gate 	 */
68717c478bd9Sstevel@tonic-gate 	if (mctl->maxlen >= 0 && type != M_DATA) {
68727c478bd9Sstevel@tonic-gate 		size_t	n, bcnt;
68737c478bd9Sstevel@tonic-gate 		char	*ubuf;
68747c478bd9Sstevel@tonic-gate 
68757c478bd9Sstevel@tonic-gate 		bcnt = mctl->maxlen;
68767c478bd9Sstevel@tonic-gate 		ubuf = mctl->buf;
68777c478bd9Sstevel@tonic-gate 		while (bp != NULL && bp->b_datap->db_type != M_DATA) {
68787c478bd9Sstevel@tonic-gate 			if ((n = MIN(bcnt, bp->b_wptr - bp->b_rptr)) != 0 &&
68797c478bd9Sstevel@tonic-gate 			    copyout(bp->b_rptr, ubuf, n)) {
68807c478bd9Sstevel@tonic-gate 				error = EFAULT;
68817c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
68827c478bd9Sstevel@tonic-gate 				/*
68837c478bd9Sstevel@tonic-gate 				 * clear stream head pri flag based on
68847c478bd9Sstevel@tonic-gate 				 * first message type
68857c478bd9Sstevel@tonic-gate 				 */
68867c478bd9Sstevel@tonic-gate 				if (type >= QPCTL) {
68877c478bd9Sstevel@tonic-gate 					ASSERT(type == M_PCPROTO);
68887c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
68897c478bd9Sstevel@tonic-gate 				}
68907c478bd9Sstevel@tonic-gate 				more = 0;
68917c478bd9Sstevel@tonic-gate 				freemsg(bp);
68927c478bd9Sstevel@tonic-gate 				goto getmout;
68937c478bd9Sstevel@tonic-gate 			}
68947c478bd9Sstevel@tonic-gate 			ubuf += n;
68957c478bd9Sstevel@tonic-gate 			bp->b_rptr += n;
68967c478bd9Sstevel@tonic-gate 			if (bp->b_rptr >= bp->b_wptr) {
68977c478bd9Sstevel@tonic-gate 				nbp = bp;
68987c478bd9Sstevel@tonic-gate 				bp = bp->b_cont;
68997c478bd9Sstevel@tonic-gate 				freeb(nbp);
69007c478bd9Sstevel@tonic-gate 			}
69017c478bd9Sstevel@tonic-gate 			ASSERT(n <= bcnt);
69027c478bd9Sstevel@tonic-gate 			bcnt -= n;
69037c478bd9Sstevel@tonic-gate 			if (bcnt == 0)
69047c478bd9Sstevel@tonic-gate 				break;
69057c478bd9Sstevel@tonic-gate 		}
69067c478bd9Sstevel@tonic-gate 		mctl->len = mctl->maxlen - bcnt;
69077c478bd9Sstevel@tonic-gate 	} else
69087c478bd9Sstevel@tonic-gate 		mctl->len = -1;
69097c478bd9Sstevel@tonic-gate 
69107c478bd9Sstevel@tonic-gate 	if (bp && bp->b_datap->db_type != M_DATA) {
69117c478bd9Sstevel@tonic-gate 		/*
69127c478bd9Sstevel@tonic-gate 		 * More PROTO blocks in msg.
69137c478bd9Sstevel@tonic-gate 		 */
69147c478bd9Sstevel@tonic-gate 		more |= MORECTL;
69157c478bd9Sstevel@tonic-gate 		savemp = bp;
69167c478bd9Sstevel@tonic-gate 		while (bp && bp->b_datap->db_type != M_DATA) {
69177c478bd9Sstevel@tonic-gate 			savemptail = bp;
69187c478bd9Sstevel@tonic-gate 			bp = bp->b_cont;
69197c478bd9Sstevel@tonic-gate 		}
69207c478bd9Sstevel@tonic-gate 		savemptail->b_cont = NULL;
69217c478bd9Sstevel@tonic-gate 	}
69227c478bd9Sstevel@tonic-gate 
69237c478bd9Sstevel@tonic-gate 	/*
69247c478bd9Sstevel@tonic-gate 	 * Now process DATA blocks, if any.
69257c478bd9Sstevel@tonic-gate 	 */
69267c478bd9Sstevel@tonic-gate 	if (mdata->maxlen >= 0 && bp) {
69277c478bd9Sstevel@tonic-gate 		/*
69287c478bd9Sstevel@tonic-gate 		 * struiocopyout will consume a potential zero-length
69297c478bd9Sstevel@tonic-gate 		 * M_DATA even if uio_resid is zero.
69307c478bd9Sstevel@tonic-gate 		 */
69317c478bd9Sstevel@tonic-gate 		size_t oldresid = uiop->uio_resid;
69327c478bd9Sstevel@tonic-gate 
69337c478bd9Sstevel@tonic-gate 		bp = struiocopyout(bp, uiop, &error);
69347c478bd9Sstevel@tonic-gate 		if (error != 0) {
69357c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
69367c478bd9Sstevel@tonic-gate 			/*
69377c478bd9Sstevel@tonic-gate 			 * clear stream head hi pri flag based on
69387c478bd9Sstevel@tonic-gate 			 * first message
69397c478bd9Sstevel@tonic-gate 			 */
69407c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
69417c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
69427c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
69437c478bd9Sstevel@tonic-gate 			}
69447c478bd9Sstevel@tonic-gate 			more = 0;
69457c478bd9Sstevel@tonic-gate 			freemsg(savemp);
69467c478bd9Sstevel@tonic-gate 			goto getmout;
69477c478bd9Sstevel@tonic-gate 		}
69487c478bd9Sstevel@tonic-gate 		/*
69497c478bd9Sstevel@tonic-gate 		 * (pr == 1) indicates a partial read.
69507c478bd9Sstevel@tonic-gate 		 */
69517c478bd9Sstevel@tonic-gate 		if (oldresid > uiop->uio_resid)
69527c478bd9Sstevel@tonic-gate 			pr = 1;
69537c478bd9Sstevel@tonic-gate 		mdata->len = mdata->maxlen - uiop->uio_resid;
69547c478bd9Sstevel@tonic-gate 	} else
69557c478bd9Sstevel@tonic-gate 		mdata->len = -1;
69567c478bd9Sstevel@tonic-gate 
69577c478bd9Sstevel@tonic-gate 	if (bp) {			/* more data blocks in msg */
69587c478bd9Sstevel@tonic-gate 		more |= MOREDATA;
69597c478bd9Sstevel@tonic-gate 		if (savemp)
69607c478bd9Sstevel@tonic-gate 			savemptail->b_cont = bp;
69617c478bd9Sstevel@tonic-gate 		else
69627c478bd9Sstevel@tonic-gate 			savemp = bp;
69637c478bd9Sstevel@tonic-gate 	}
69647c478bd9Sstevel@tonic-gate 
69657c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
69667c478bd9Sstevel@tonic-gate 	if (savemp) {
69677c478bd9Sstevel@tonic-gate 		if (pr && (savemp->b_datap->db_type == M_DATA) &&
69687c478bd9Sstevel@tonic-gate 		    msgnodata(savemp)) {
69697c478bd9Sstevel@tonic-gate 			/*
69707c478bd9Sstevel@tonic-gate 			 * Avoid queuing a zero-length tail part of
69717c478bd9Sstevel@tonic-gate 			 * a message. pr=1 indicates that we read some of
69727c478bd9Sstevel@tonic-gate 			 * the message.
69737c478bd9Sstevel@tonic-gate 			 */
69747c478bd9Sstevel@tonic-gate 			freemsg(savemp);
69757c478bd9Sstevel@tonic-gate 			more &= ~MOREDATA;
69767c478bd9Sstevel@tonic-gate 			/*
69777c478bd9Sstevel@tonic-gate 			 * clear stream head hi pri flag based on
69787c478bd9Sstevel@tonic-gate 			 * first message
69797c478bd9Sstevel@tonic-gate 			 */
69807c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
69817c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
69827c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
69837c478bd9Sstevel@tonic-gate 			}
69847c478bd9Sstevel@tonic-gate 		} else {
69857c478bd9Sstevel@tonic-gate 			savemp->b_band = pri;
69867c478bd9Sstevel@tonic-gate 			/*
69877c478bd9Sstevel@tonic-gate 			 * If the first message was HIPRI and the one we're
69887c478bd9Sstevel@tonic-gate 			 * putting back isn't, then clear STRPRI, otherwise
69897c478bd9Sstevel@tonic-gate 			 * set STRPRI again.  Note that we must set STRPRI
69907c478bd9Sstevel@tonic-gate 			 * again since the flush logic in strrput_nondata()
69917c478bd9Sstevel@tonic-gate 			 * may have cleared it while we had sd_lock dropped.
69927c478bd9Sstevel@tonic-gate 			 */
69937c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
69947c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
69957c478bd9Sstevel@tonic-gate 				if (queclass(savemp) < QPCTL)
69967c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
69977c478bd9Sstevel@tonic-gate 				else
69987c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRPRI;
69997c478bd9Sstevel@tonic-gate 			} else if (queclass(savemp) >= QPCTL) {
70007c478bd9Sstevel@tonic-gate 				/*
70017c478bd9Sstevel@tonic-gate 				 * The first message was not a HIPRI message,
70027c478bd9Sstevel@tonic-gate 				 * but the one we are about to putback is.
70037c478bd9Sstevel@tonic-gate 				 * For simplicitly, we do not allow for HIPRI
70047c478bd9Sstevel@tonic-gate 				 * messages to be embedded in the message
70057c478bd9Sstevel@tonic-gate 				 * body, so just force it to same type as
70067c478bd9Sstevel@tonic-gate 				 * first message.
70077c478bd9Sstevel@tonic-gate 				 */
70087c478bd9Sstevel@tonic-gate 				ASSERT(type == M_DATA || type == M_PROTO);
70097c478bd9Sstevel@tonic-gate 				ASSERT(savemp->b_datap->db_type == M_PCPROTO);
70107c478bd9Sstevel@tonic-gate 				savemp->b_datap->db_type = type;
70117c478bd9Sstevel@tonic-gate 			}
70127c478bd9Sstevel@tonic-gate 			if (mark != 0) {
70137c478bd9Sstevel@tonic-gate 				savemp->b_flag |= mark & ~_LASTMARK;
70147c478bd9Sstevel@tonic-gate 				if ((mark & _LASTMARK) &&
70157c478bd9Sstevel@tonic-gate 				    (stp->sd_mark == NULL)) {
70167c478bd9Sstevel@tonic-gate 					/*
70177c478bd9Sstevel@tonic-gate 					 * If another marked message arrived
70187c478bd9Sstevel@tonic-gate 					 * while sd_lock was not held sd_mark
70197c478bd9Sstevel@tonic-gate 					 * would be non-NULL.
70207c478bd9Sstevel@tonic-gate 					 */
70217c478bd9Sstevel@tonic-gate 					stp->sd_mark = savemp;
70227c478bd9Sstevel@tonic-gate 				}
70237c478bd9Sstevel@tonic-gate 			}
70247c478bd9Sstevel@tonic-gate 			putback(stp, q, savemp, pri);
70257c478bd9Sstevel@tonic-gate 		}
70267c478bd9Sstevel@tonic-gate 	} else {
70277c478bd9Sstevel@tonic-gate 		/*
70287c478bd9Sstevel@tonic-gate 		 * The complete message was consumed.
70297c478bd9Sstevel@tonic-gate 		 *
70307c478bd9Sstevel@tonic-gate 		 * If another M_PCPROTO arrived while sd_lock was not held
70317c478bd9Sstevel@tonic-gate 		 * it would have been discarded since STRPRI was still set.
70327c478bd9Sstevel@tonic-gate 		 *
70337c478bd9Sstevel@tonic-gate 		 * Move the MSG*MARKNEXT information
70347c478bd9Sstevel@tonic-gate 		 * to the stream head just in case
70357c478bd9Sstevel@tonic-gate 		 * the read queue becomes empty.
70367c478bd9Sstevel@tonic-gate 		 * clear stream head hi pri flag based on
70377c478bd9Sstevel@tonic-gate 		 * first message
70387c478bd9Sstevel@tonic-gate 		 *
70397c478bd9Sstevel@tonic-gate 		 * If the stream head was at the mark
70407c478bd9Sstevel@tonic-gate 		 * (STRATMARK) before we dropped sd_lock above
70417c478bd9Sstevel@tonic-gate 		 * and some data was consumed then we have
70427c478bd9Sstevel@tonic-gate 		 * moved past the mark thus STRATMARK is
70437c478bd9Sstevel@tonic-gate 		 * cleared. However, if a message arrived in
70447c478bd9Sstevel@tonic-gate 		 * strrput during the copyout above causing
70457c478bd9Sstevel@tonic-gate 		 * STRATMARK to be set we can not clear that
70467c478bd9Sstevel@tonic-gate 		 * flag.
70477c478bd9Sstevel@tonic-gate 		 */
70487c478bd9Sstevel@tonic-gate 		if (type >= QPCTL) {
70497c478bd9Sstevel@tonic-gate 			ASSERT(type == M_PCPROTO);
70507c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRPRI;
70517c478bd9Sstevel@tonic-gate 		}
70527c478bd9Sstevel@tonic-gate 		if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
70537c478bd9Sstevel@tonic-gate 			if (mark & MSGMARKNEXT) {
70547c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRNOTATMARK;
70557c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRATMARK;
70567c478bd9Sstevel@tonic-gate 			} else if (mark & MSGNOTMARKNEXT) {
70577c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRATMARK;
70587c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRNOTATMARK;
70597c478bd9Sstevel@tonic-gate 			} else {
70607c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
70617c478bd9Sstevel@tonic-gate 			}
70627c478bd9Sstevel@tonic-gate 		} else if (pr && (old_sd_flag & STRATMARK)) {
70637c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRATMARK;
70647c478bd9Sstevel@tonic-gate 		}
70657c478bd9Sstevel@tonic-gate 	}
70667c478bd9Sstevel@tonic-gate 
70677c478bd9Sstevel@tonic-gate 	*flagsp = flg;
70687c478bd9Sstevel@tonic-gate 	*prip = pri;
70697c478bd9Sstevel@tonic-gate 
70707c478bd9Sstevel@tonic-gate 	/*
70717c478bd9Sstevel@tonic-gate 	 * Getmsg cleanup processing - if the state of the queue has changed
70727c478bd9Sstevel@tonic-gate 	 * some signals may need to be sent and/or poll awakened.
70737c478bd9Sstevel@tonic-gate 	 */
70747c478bd9Sstevel@tonic-gate getmout:
70757c478bd9Sstevel@tonic-gate 	qbackenable(q, pri);
70767c478bd9Sstevel@tonic-gate 
70777c478bd9Sstevel@tonic-gate 	/*
70787c478bd9Sstevel@tonic-gate 	 * We dropped the stream head lock above. Send all M_SIG messages
70797c478bd9Sstevel@tonic-gate 	 * before processing stream head for SIGPOLL messages.
70807c478bd9Sstevel@tonic-gate 	 */
70817c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
70827c478bd9Sstevel@tonic-gate 	while ((bp = q->q_first) != NULL &&
70837c478bd9Sstevel@tonic-gate 	    (bp->b_datap->db_type == M_SIG)) {
70847c478bd9Sstevel@tonic-gate 		/*
70857c478bd9Sstevel@tonic-gate 		 * sd_lock is held so the content of the read queue can not
70867c478bd9Sstevel@tonic-gate 		 * change.
70877c478bd9Sstevel@tonic-gate 		 */
70887c478bd9Sstevel@tonic-gate 		bp = getq(q);
70897c478bd9Sstevel@tonic-gate 		ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
70907c478bd9Sstevel@tonic-gate 
70917c478bd9Sstevel@tonic-gate 		strsignal_nolock(stp, *bp->b_rptr, (int32_t)bp->b_band);
70927c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
70937c478bd9Sstevel@tonic-gate 		freemsg(bp);
70947c478bd9Sstevel@tonic-gate 		if (STREAM_NEEDSERVICE(stp))
70957c478bd9Sstevel@tonic-gate 			stream_runservice(stp);
70967c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
70977c478bd9Sstevel@tonic-gate 	}
70987c478bd9Sstevel@tonic-gate 
70997c478bd9Sstevel@tonic-gate 	/*
71007c478bd9Sstevel@tonic-gate 	 * stream head cannot change while we make the determination
71017c478bd9Sstevel@tonic-gate 	 * whether or not to send a signal. Drop the flag to allow strrput
71027c478bd9Sstevel@tonic-gate 	 * to send firstmsgsigs again.
71037c478bd9Sstevel@tonic-gate 	 */
71047c478bd9Sstevel@tonic-gate 	stp->sd_flag &= ~STRGETINPROG;
71057c478bd9Sstevel@tonic-gate 
71067c478bd9Sstevel@tonic-gate 	/*
71077c478bd9Sstevel@tonic-gate 	 * If the type of message at the front of the queue changed
71087c478bd9Sstevel@tonic-gate 	 * due to the receive the appropriate signals and pollwakeup events
71097c478bd9Sstevel@tonic-gate 	 * are generated. The type of changes are:
71107c478bd9Sstevel@tonic-gate 	 *	Processed a hipri message, q_first is not hipri.
71117c478bd9Sstevel@tonic-gate 	 *	Processed a band X message, and q_first is band Y.
71127c478bd9Sstevel@tonic-gate 	 * The generated signals and pollwakeups are identical to what
71137c478bd9Sstevel@tonic-gate 	 * strrput() generates should the message that is now on q_first
71147c478bd9Sstevel@tonic-gate 	 * arrive to an empty read queue.
71157c478bd9Sstevel@tonic-gate 	 *
71167c478bd9Sstevel@tonic-gate 	 * Note: only strrput will send a signal for a hipri message.
71177c478bd9Sstevel@tonic-gate 	 */
71187c478bd9Sstevel@tonic-gate 	if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
71197c478bd9Sstevel@tonic-gate 		strsigset_t signals = 0;
71207c478bd9Sstevel@tonic-gate 		strpollset_t pollwakeups = 0;
71217c478bd9Sstevel@tonic-gate 
71227c478bd9Sstevel@tonic-gate 		if (flg & MSG_HIPRI) {
71237c478bd9Sstevel@tonic-gate 			/*
71247c478bd9Sstevel@tonic-gate 			 * Removed a hipri message. Regular data at
71257c478bd9Sstevel@tonic-gate 			 * the front of  the queue.
71267c478bd9Sstevel@tonic-gate 			 */
71277c478bd9Sstevel@tonic-gate 			if (bp->b_band == 0) {
71287c478bd9Sstevel@tonic-gate 				signals = S_INPUT | S_RDNORM;
71297c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDNORM;
71307c478bd9Sstevel@tonic-gate 			} else {
71317c478bd9Sstevel@tonic-gate 				signals = S_INPUT | S_RDBAND;
71327c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDBAND;
71337c478bd9Sstevel@tonic-gate 			}
71347c478bd9Sstevel@tonic-gate 		} else if (pri != bp->b_band) {
71357c478bd9Sstevel@tonic-gate 			/*
71367c478bd9Sstevel@tonic-gate 			 * The band is different for the new q_first.
71377c478bd9Sstevel@tonic-gate 			 */
71387c478bd9Sstevel@tonic-gate 			if (bp->b_band == 0) {
71397c478bd9Sstevel@tonic-gate 				signals = S_RDNORM;
71407c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDNORM;
71417c478bd9Sstevel@tonic-gate 			} else {
71427c478bd9Sstevel@tonic-gate 				signals = S_RDBAND;
71437c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDBAND;
71447c478bd9Sstevel@tonic-gate 			}
71457c478bd9Sstevel@tonic-gate 		}
71467c478bd9Sstevel@tonic-gate 
71477c478bd9Sstevel@tonic-gate 		if (pollwakeups != 0) {
71487c478bd9Sstevel@tonic-gate 			if (pollwakeups == (POLLIN | POLLRDNORM)) {
71497c478bd9Sstevel@tonic-gate 				if (!(stp->sd_rput_opt & SR_POLLIN))
71507c478bd9Sstevel@tonic-gate 					goto no_pollwake;
71517c478bd9Sstevel@tonic-gate 				stp->sd_rput_opt &= ~SR_POLLIN;
71527c478bd9Sstevel@tonic-gate 			}
71537c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
71547c478bd9Sstevel@tonic-gate 			pollwakeup(&stp->sd_pollist, pollwakeups);
71557c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
71567c478bd9Sstevel@tonic-gate 		}
71577c478bd9Sstevel@tonic-gate no_pollwake:
71587c478bd9Sstevel@tonic-gate 
71597c478bd9Sstevel@tonic-gate 		if (stp->sd_sigflags & signals)
71607c478bd9Sstevel@tonic-gate 			strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
71617c478bd9Sstevel@tonic-gate 	}
71627c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
71637c478bd9Sstevel@tonic-gate 
71647c478bd9Sstevel@tonic-gate 	rvp->r_val1 = more;
71657c478bd9Sstevel@tonic-gate 	return (error);
71667c478bd9Sstevel@tonic-gate #undef	_LASTMARK
71677c478bd9Sstevel@tonic-gate }
71687c478bd9Sstevel@tonic-gate 
71697c478bd9Sstevel@tonic-gate /*
71707c478bd9Sstevel@tonic-gate  * Get the next message from the read queue.  If the message is
71717c478bd9Sstevel@tonic-gate  * priority, STRPRI will have been set by strrput().  This flag
71727c478bd9Sstevel@tonic-gate  * should be reset only when the entire message at the front of the
71737c478bd9Sstevel@tonic-gate  * queue as been consumed.
71747c478bd9Sstevel@tonic-gate  *
71757c478bd9Sstevel@tonic-gate  * If uiop is NULL all data is returned in mctlp.
71767c478bd9Sstevel@tonic-gate  * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
71777c478bd9Sstevel@tonic-gate  * not enabled.
71787c478bd9Sstevel@tonic-gate  * The timeout parameter is in milliseconds; -1 for infinity.
71797c478bd9Sstevel@tonic-gate  * This routine handles the consolidation private flags:
71807c478bd9Sstevel@tonic-gate  *	MSG_IGNERROR	Ignore any stream head error except STPLEX.
71817c478bd9Sstevel@tonic-gate  *	MSG_DELAYERROR	Defer the error check until the queue is empty.
71827c478bd9Sstevel@tonic-gate  *	MSG_HOLDSIG	Hold signals while waiting for data.
71837c478bd9Sstevel@tonic-gate  *	MSG_IPEEK	Only peek at messages.
71847c478bd9Sstevel@tonic-gate  *	MSG_DISCARDTAIL	Discard the tail M_DATA part of the message
71857c478bd9Sstevel@tonic-gate  *			that doesn't fit.
71867c478bd9Sstevel@tonic-gate  *	MSG_NOMARK	If the message is marked leave it on the queue.
71877c478bd9Sstevel@tonic-gate  *
71887c478bd9Sstevel@tonic-gate  * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
71897c478bd9Sstevel@tonic-gate  */
71907c478bd9Sstevel@tonic-gate int
71917c478bd9Sstevel@tonic-gate kstrgetmsg(
71927c478bd9Sstevel@tonic-gate 	struct vnode *vp,
71937c478bd9Sstevel@tonic-gate 	mblk_t **mctlp,
71947c478bd9Sstevel@tonic-gate 	struct uio *uiop,
71957c478bd9Sstevel@tonic-gate 	unsigned char *prip,
71967c478bd9Sstevel@tonic-gate 	int *flagsp,
71977c478bd9Sstevel@tonic-gate 	clock_t timout,
71987c478bd9Sstevel@tonic-gate 	rval_t *rvp)
71997c478bd9Sstevel@tonic-gate {
72007c478bd9Sstevel@tonic-gate 	struct stdata *stp;
72017c478bd9Sstevel@tonic-gate 	mblk_t *bp, *nbp;
72027c478bd9Sstevel@tonic-gate 	mblk_t *savemp = NULL;
72037c478bd9Sstevel@tonic-gate 	mblk_t *savemptail = NULL;
72047c478bd9Sstevel@tonic-gate 	int flags;
72057c478bd9Sstevel@tonic-gate 	uint_t old_sd_flag;
72067c478bd9Sstevel@tonic-gate 	int flg;
72077c478bd9Sstevel@tonic-gate 	int more = 0;
72087c478bd9Sstevel@tonic-gate 	int error = 0;
72097c478bd9Sstevel@tonic-gate 	char first = 1;
72107c478bd9Sstevel@tonic-gate 	uint_t mark;		/* Contains MSG*MARK and _LASTMARK */
72117c478bd9Sstevel@tonic-gate #define	_LASTMARK	0x8000	/* Distinct from MSG*MARK */
72127c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
72137c478bd9Sstevel@tonic-gate 	queue_t *q;
72147c478bd9Sstevel@tonic-gate 	int	pr = 0;			/* Partial read successful */
72157c478bd9Sstevel@tonic-gate 	unsigned char type;
72167c478bd9Sstevel@tonic-gate 
72177c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_ENTER,
72187c478bd9Sstevel@tonic-gate 	    "kstrgetmsg:%p", vp);
72197c478bd9Sstevel@tonic-gate 
72207c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
72217c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
72227c478bd9Sstevel@tonic-gate 	rvp->r_val1 = 0;
72237c478bd9Sstevel@tonic-gate 
72249acbbeafSnn35248 	mutex_enter(&stp->sd_lock);
72259acbbeafSnn35248 
72269acbbeafSnn35248 	if ((error = i_straccess(stp, JCREAD)) != 0) {
72279acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
72287c478bd9Sstevel@tonic-gate 		return (error);
72299acbbeafSnn35248 	}
72307c478bd9Sstevel@tonic-gate 
72317c478bd9Sstevel@tonic-gate 	flags = *flagsp;
72327c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STRDERR|STPLEX)) {
72337c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STPLEX) ||
72347c478bd9Sstevel@tonic-gate 		    (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == 0) {
72357c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, STRDERR|STPLEX,
72367c478bd9Sstevel@tonic-gate 			    (flags & MSG_IPEEK));
72379acbbeafSnn35248 			if (error != 0) {
72387c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
72397c478bd9Sstevel@tonic-gate 				return (error);
72407c478bd9Sstevel@tonic-gate 			}
72417c478bd9Sstevel@tonic-gate 		}
72429acbbeafSnn35248 	}
72439acbbeafSnn35248 	mutex_exit(&stp->sd_lock);
72447c478bd9Sstevel@tonic-gate 
72457c478bd9Sstevel@tonic-gate 	switch (flags & (MSG_HIPRI|MSG_ANY|MSG_BAND)) {
72467c478bd9Sstevel@tonic-gate 	case MSG_HIPRI:
72477c478bd9Sstevel@tonic-gate 		if (*prip != 0)
72487c478bd9Sstevel@tonic-gate 			return (EINVAL);
72497c478bd9Sstevel@tonic-gate 		break;
72507c478bd9Sstevel@tonic-gate 
72517c478bd9Sstevel@tonic-gate 	case MSG_ANY:
72527c478bd9Sstevel@tonic-gate 	case MSG_BAND:
72537c478bd9Sstevel@tonic-gate 		break;
72547c478bd9Sstevel@tonic-gate 
72557c478bd9Sstevel@tonic-gate 	default:
72567c478bd9Sstevel@tonic-gate 		return (EINVAL);
72577c478bd9Sstevel@tonic-gate 	}
72587c478bd9Sstevel@tonic-gate 
72597c478bd9Sstevel@tonic-gate retry:
72607c478bd9Sstevel@tonic-gate 	q = _RD(stp->sd_wrq);
72617c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
72627c478bd9Sstevel@tonic-gate 	old_sd_flag = stp->sd_flag;
72637c478bd9Sstevel@tonic-gate 	mark = 0;
72647c478bd9Sstevel@tonic-gate 	for (;;) {
72657c478bd9Sstevel@tonic-gate 		int done = 0;
72667c478bd9Sstevel@tonic-gate 		int waitflag;
72677c478bd9Sstevel@tonic-gate 		int fmode;
72687c478bd9Sstevel@tonic-gate 		mblk_t *q_first = q->q_first;
72697c478bd9Sstevel@tonic-gate 
72707c478bd9Sstevel@tonic-gate 		/*
72717c478bd9Sstevel@tonic-gate 		 * This section of the code operates just like the code
72727c478bd9Sstevel@tonic-gate 		 * in strgetmsg().  There is a comment there about what
72737c478bd9Sstevel@tonic-gate 		 * is going on here.
72747c478bd9Sstevel@tonic-gate 		 */
72757c478bd9Sstevel@tonic-gate 		if (!(flags & (MSG_HIPRI|MSG_BAND))) {
72767c478bd9Sstevel@tonic-gate 			/* Asking for normal, band0 data */
72777c478bd9Sstevel@tonic-gate 			bp = strget(stp, q, uiop, first, &error);
72787c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(&stp->sd_lock));
72797c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
72807c478bd9Sstevel@tonic-gate 				if (bp->b_datap->db_type == M_SIG) {
72817c478bd9Sstevel@tonic-gate 					strsignal_nolock(stp, *bp->b_rptr,
72827c478bd9Sstevel@tonic-gate 					    (int32_t)bp->b_band);
72837c478bd9Sstevel@tonic-gate 					continue;
72847c478bd9Sstevel@tonic-gate 				} else {
72857c478bd9Sstevel@tonic-gate 					break;
72867c478bd9Sstevel@tonic-gate 				}
72877c478bd9Sstevel@tonic-gate 			}
72887c478bd9Sstevel@tonic-gate 			if (error != 0) {
72897c478bd9Sstevel@tonic-gate 				goto getmout;
72907c478bd9Sstevel@tonic-gate 			}
72917c478bd9Sstevel@tonic-gate 		/*
72927c478bd9Sstevel@tonic-gate 		 * We can't depend on the value of STRPRI here because
72937c478bd9Sstevel@tonic-gate 		 * the stream head may be in transit. Therefore, we
72947c478bd9Sstevel@tonic-gate 		 * must look at the type of the first message to
72957c478bd9Sstevel@tonic-gate 		 * determine if a high priority messages is waiting
72967c478bd9Sstevel@tonic-gate 		 */
72977c478bd9Sstevel@tonic-gate 		} else if ((flags & MSG_HIPRI) && q_first != NULL &&
72987c478bd9Sstevel@tonic-gate 		    q_first->b_datap->db_type >= QPCTL &&
7299*301ce41fSja97890 		    (bp = getq_noenab(q, 0)) != NULL) {
73007c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_datap->db_type >= QPCTL);
73017c478bd9Sstevel@tonic-gate 			break;
73027c478bd9Sstevel@tonic-gate 		} else if ((flags & MSG_BAND) && q_first != NULL &&
73037c478bd9Sstevel@tonic-gate 		    ((q_first->b_band >= *prip) ||
73047c478bd9Sstevel@tonic-gate 		    q_first->b_datap->db_type >= QPCTL) &&
7305*301ce41fSja97890 		    (bp = getq_noenab(q, 0)) != NULL) {
73067c478bd9Sstevel@tonic-gate 			/*
73077c478bd9Sstevel@tonic-gate 			 * Asked for at least band "prip" and got either at
73087c478bd9Sstevel@tonic-gate 			 * least that band or a hipri message.
73097c478bd9Sstevel@tonic-gate 			 */
73107c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_band >= *prip ||
73117c478bd9Sstevel@tonic-gate 			    bp->b_datap->db_type >= QPCTL);
73127c478bd9Sstevel@tonic-gate 			if (bp->b_datap->db_type == M_SIG) {
73137c478bd9Sstevel@tonic-gate 				strsignal_nolock(stp, *bp->b_rptr,
73147c478bd9Sstevel@tonic-gate 				    (int32_t)bp->b_band);
73157c478bd9Sstevel@tonic-gate 				continue;
73167c478bd9Sstevel@tonic-gate 			} else {
73177c478bd9Sstevel@tonic-gate 				break;
73187c478bd9Sstevel@tonic-gate 			}
73197c478bd9Sstevel@tonic-gate 		}
73207c478bd9Sstevel@tonic-gate 
73217c478bd9Sstevel@tonic-gate 		/* No data. Time to sleep? */
73227c478bd9Sstevel@tonic-gate 		qbackenable(q, 0);
73237c478bd9Sstevel@tonic-gate 
73247c478bd9Sstevel@tonic-gate 		/*
73257c478bd9Sstevel@tonic-gate 		 * Delayed error notification?
73267c478bd9Sstevel@tonic-gate 		 */
73277c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & (STRDERR|STPLEX)) &&
73287c478bd9Sstevel@tonic-gate 		    (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == MSG_DELAYERROR) {
73297c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, STRDERR|STPLEX,
73307c478bd9Sstevel@tonic-gate 			    (flags & MSG_IPEEK));
73317c478bd9Sstevel@tonic-gate 			if (error != 0) {
73327c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
73337c478bd9Sstevel@tonic-gate 				return (error);
73347c478bd9Sstevel@tonic-gate 			}
73357c478bd9Sstevel@tonic-gate 		}
73367c478bd9Sstevel@tonic-gate 
73377c478bd9Sstevel@tonic-gate 		/*
73387c478bd9Sstevel@tonic-gate 		 * If STRHUP or STREOF, return 0 length control and data.
73397c478bd9Sstevel@tonic-gate 		 * If a read(fd,buf,0) has been done, do not sleep, just
73407c478bd9Sstevel@tonic-gate 		 * return.
73417c478bd9Sstevel@tonic-gate 		 *
73427c478bd9Sstevel@tonic-gate 		 * If mctlp == NULL and uiop == NULL, then the code will
73437c478bd9Sstevel@tonic-gate 		 * do the strwaitq. This is an understood way of saying
73447c478bd9Sstevel@tonic-gate 		 * sleep "polling" until a message is received.
73457c478bd9Sstevel@tonic-gate 		 */
73467c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & (STRHUP|STREOF)) ||
73477c478bd9Sstevel@tonic-gate 		    (uiop != NULL && uiop->uio_resid == 0)) {
73487c478bd9Sstevel@tonic-gate 			if (mctlp != NULL)
73497c478bd9Sstevel@tonic-gate 				*mctlp = NULL;
73507c478bd9Sstevel@tonic-gate 			*flagsp = 0;
73517c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
73527c478bd9Sstevel@tonic-gate 			return (0);
73537c478bd9Sstevel@tonic-gate 		}
73547c478bd9Sstevel@tonic-gate 
73557c478bd9Sstevel@tonic-gate 		waitflag = GETWAIT;
73567c478bd9Sstevel@tonic-gate 		if (flags &
73577c478bd9Sstevel@tonic-gate 		    (MSG_HOLDSIG|MSG_IGNERROR|MSG_IPEEK|MSG_DELAYERROR)) {
73587c478bd9Sstevel@tonic-gate 			if (flags & MSG_HOLDSIG)
73597c478bd9Sstevel@tonic-gate 				waitflag |= STR_NOSIG;
73607c478bd9Sstevel@tonic-gate 			if (flags & MSG_IGNERROR)
73617c478bd9Sstevel@tonic-gate 				waitflag |= STR_NOERROR;
73627c478bd9Sstevel@tonic-gate 			if (flags & MSG_IPEEK)
73637c478bd9Sstevel@tonic-gate 				waitflag |= STR_PEEK;
73647c478bd9Sstevel@tonic-gate 			if (flags & MSG_DELAYERROR)
73657c478bd9Sstevel@tonic-gate 				waitflag |= STR_DELAYERR;
73667c478bd9Sstevel@tonic-gate 		}
73677c478bd9Sstevel@tonic-gate 		if (uiop != NULL)
73687c478bd9Sstevel@tonic-gate 			fmode = uiop->uio_fmode;
73697c478bd9Sstevel@tonic-gate 		else
73707c478bd9Sstevel@tonic-gate 			fmode = 0;
73717c478bd9Sstevel@tonic-gate 
73727c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_WAIT,
73737c478bd9Sstevel@tonic-gate 		    "kstrgetmsg calls strwaitq:%p, %p",
73747c478bd9Sstevel@tonic-gate 		    vp, uiop);
73757c478bd9Sstevel@tonic-gate 		if (((error = strwaitq(stp, waitflag, (ssize_t)0,
737617169044Sbrutus 		    fmode, timout, &done))) != 0 || done) {
73777c478bd9Sstevel@tonic-gate 			TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_DONE,
73787c478bd9Sstevel@tonic-gate 			    "kstrgetmsg error or done:%p, %p",
73797c478bd9Sstevel@tonic-gate 			    vp, uiop);
73807c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
73817c478bd9Sstevel@tonic-gate 			return (error);
73827c478bd9Sstevel@tonic-gate 		}
73837c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_AWAKE,
73847c478bd9Sstevel@tonic-gate 		    "kstrgetmsg awakes:%p, %p", vp, uiop);
73859acbbeafSnn35248 		if ((error = i_straccess(stp, JCREAD)) != 0) {
73867c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
73877c478bd9Sstevel@tonic-gate 			return (error);
73887c478bd9Sstevel@tonic-gate 		}
73897c478bd9Sstevel@tonic-gate 		first = 0;
73907c478bd9Sstevel@tonic-gate 	}
73917c478bd9Sstevel@tonic-gate 	ASSERT(bp != NULL);
73927c478bd9Sstevel@tonic-gate 	/*
73937c478bd9Sstevel@tonic-gate 	 * Extract any mark information. If the message is not completely
73947c478bd9Sstevel@tonic-gate 	 * consumed this information will be put in the mblk
73957c478bd9Sstevel@tonic-gate 	 * that is putback.
73967c478bd9Sstevel@tonic-gate 	 * If MSGMARKNEXT is set and the message is completely consumed
73977c478bd9Sstevel@tonic-gate 	 * the STRATMARK flag will be set below. Likewise, if
73987c478bd9Sstevel@tonic-gate 	 * MSGNOTMARKNEXT is set and the message is
73997c478bd9Sstevel@tonic-gate 	 * completely consumed STRNOTATMARK will be set.
74007c478bd9Sstevel@tonic-gate 	 */
74017c478bd9Sstevel@tonic-gate 	mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
74027c478bd9Sstevel@tonic-gate 	ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
74037c478bd9Sstevel@tonic-gate 	    (MSGMARKNEXT|MSGNOTMARKNEXT));
74047c478bd9Sstevel@tonic-gate 	pri = bp->b_band;
74057c478bd9Sstevel@tonic-gate 	if (mark != 0) {
74067c478bd9Sstevel@tonic-gate 		/*
74077c478bd9Sstevel@tonic-gate 		 * If the caller doesn't want the mark return.
74087c478bd9Sstevel@tonic-gate 		 * Used to implement MSG_WAITALL in sockets.
74097c478bd9Sstevel@tonic-gate 		 */
74107c478bd9Sstevel@tonic-gate 		if (flags & MSG_NOMARK) {
74117c478bd9Sstevel@tonic-gate 			putback(stp, q, bp, pri);
74127c478bd9Sstevel@tonic-gate 			qbackenable(q, pri);
74137c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
74147c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
74157c478bd9Sstevel@tonic-gate 		}
74167c478bd9Sstevel@tonic-gate 		if (bp == stp->sd_mark) {
74177c478bd9Sstevel@tonic-gate 			mark |= _LASTMARK;
74187c478bd9Sstevel@tonic-gate 			stp->sd_mark = NULL;
74197c478bd9Sstevel@tonic-gate 		}
74207c478bd9Sstevel@tonic-gate 	}
74217c478bd9Sstevel@tonic-gate 
74227c478bd9Sstevel@tonic-gate 	/*
74237c478bd9Sstevel@tonic-gate 	 * keep track of the first message type
74247c478bd9Sstevel@tonic-gate 	 */
74257c478bd9Sstevel@tonic-gate 	type = bp->b_datap->db_type;
74267c478bd9Sstevel@tonic-gate 
74277c478bd9Sstevel@tonic-gate 	if (bp->b_datap->db_type == M_PASSFP) {
74287c478bd9Sstevel@tonic-gate 		if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
74297c478bd9Sstevel@tonic-gate 			stp->sd_mark = bp;
74307c478bd9Sstevel@tonic-gate 		bp->b_flag |= mark & ~_LASTMARK;
74317c478bd9Sstevel@tonic-gate 		putback(stp, q, bp, pri);
74327c478bd9Sstevel@tonic-gate 		qbackenable(q, pri);
74337c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
74347c478bd9Sstevel@tonic-gate 		return (EBADMSG);
74357c478bd9Sstevel@tonic-gate 	}
74367c478bd9Sstevel@tonic-gate 	ASSERT(type != M_SIG);
74377c478bd9Sstevel@tonic-gate 
74387c478bd9Sstevel@tonic-gate 	if (flags & MSG_IPEEK) {
74397c478bd9Sstevel@tonic-gate 		/*
74407c478bd9Sstevel@tonic-gate 		 * Clear any struioflag - we do the uiomove over again
74417c478bd9Sstevel@tonic-gate 		 * when peeking since it simplifies the code.
74427c478bd9Sstevel@tonic-gate 		 *
74437c478bd9Sstevel@tonic-gate 		 * Dup the message and put the original back on the queue.
74447c478bd9Sstevel@tonic-gate 		 * If dupmsg() fails, try again with copymsg() to see if
74457c478bd9Sstevel@tonic-gate 		 * there is indeed a shortage of memory.  dupmsg() may fail
74467c478bd9Sstevel@tonic-gate 		 * if db_ref in any of the messages reaches its limit.
74477c478bd9Sstevel@tonic-gate 		 */
744817169044Sbrutus 
744917169044Sbrutus 		ASSERT(!(bp->b_datap->db_flags & DBLK_UIOA));
74507c478bd9Sstevel@tonic-gate 		if ((nbp = dupmsg(bp)) == NULL && (nbp = copymsg(bp)) == NULL) {
74517c478bd9Sstevel@tonic-gate 			/*
74527c478bd9Sstevel@tonic-gate 			 * Restore the state of the stream head since we
74537c478bd9Sstevel@tonic-gate 			 * need to drop sd_lock (strwaitbuf is sleeping).
74547c478bd9Sstevel@tonic-gate 			 */
74557c478bd9Sstevel@tonic-gate 			size_t size = msgdsize(bp);
74567c478bd9Sstevel@tonic-gate 
74577c478bd9Sstevel@tonic-gate 			if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
74587c478bd9Sstevel@tonic-gate 				stp->sd_mark = bp;
74597c478bd9Sstevel@tonic-gate 			bp->b_flag |= mark & ~_LASTMARK;
74607c478bd9Sstevel@tonic-gate 			putback(stp, q, bp, pri);
74617c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
74627c478bd9Sstevel@tonic-gate 			error = strwaitbuf(size, BPRI_HI);
74637c478bd9Sstevel@tonic-gate 			if (error) {
74647c478bd9Sstevel@tonic-gate 				/*
74657c478bd9Sstevel@tonic-gate 				 * There is no net change to the queue thus
74667c478bd9Sstevel@tonic-gate 				 * no need to qbackenable.
74677c478bd9Sstevel@tonic-gate 				 */
74687c478bd9Sstevel@tonic-gate 				return (error);
74697c478bd9Sstevel@tonic-gate 			}
74707c478bd9Sstevel@tonic-gate 			goto retry;
74717c478bd9Sstevel@tonic-gate 		}
74727c478bd9Sstevel@tonic-gate 
74737c478bd9Sstevel@tonic-gate 		if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
74747c478bd9Sstevel@tonic-gate 			stp->sd_mark = bp;
74757c478bd9Sstevel@tonic-gate 		bp->b_flag |= mark & ~_LASTMARK;
74767c478bd9Sstevel@tonic-gate 		putback(stp, q, bp, pri);
74777c478bd9Sstevel@tonic-gate 		bp = nbp;
74787c478bd9Sstevel@tonic-gate 	}
74797c478bd9Sstevel@tonic-gate 
74807c478bd9Sstevel@tonic-gate 	/*
74817c478bd9Sstevel@tonic-gate 	 * Set this flag so strrput will not generate signals. Need to
74827c478bd9Sstevel@tonic-gate 	 * make sure this flag is cleared before leaving this routine
74837c478bd9Sstevel@tonic-gate 	 * else signals will stop being sent.
74847c478bd9Sstevel@tonic-gate 	 */
74857c478bd9Sstevel@tonic-gate 	stp->sd_flag |= STRGETINPROG;
74867c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
74877c478bd9Sstevel@tonic-gate 
748851dd2c77Svk199839 	if ((stp->sd_rputdatafunc != NULL) && (DB_TYPE(bp) == M_DATA)) {
748951dd2c77Svk199839 		mblk_t *tmp, *prevmp;
7490c28749e9Skais 
749151dd2c77Svk199839 		/*
749251dd2c77Svk199839 		 * Put first non-data mblk back to stream head and
749351dd2c77Svk199839 		 * cut the mblk chain so sd_rputdatafunc only sees
749451dd2c77Svk199839 		 * M_DATA mblks. We can skip the first mblk since it
749551dd2c77Svk199839 		 * is M_DATA according to the condition above.
749651dd2c77Svk199839 		 */
749751dd2c77Svk199839 		for (prevmp = bp, tmp = bp->b_cont; tmp != NULL;
749851dd2c77Svk199839 		    prevmp = tmp, tmp = tmp->b_cont) {
749951dd2c77Svk199839 			if (DB_TYPE(tmp) != M_DATA) {
750051dd2c77Svk199839 				prevmp->b_cont = NULL;
750151dd2c77Svk199839 				mutex_enter(&stp->sd_lock);
750251dd2c77Svk199839 				putback(stp, q, tmp, tmp->b_band);
750351dd2c77Svk199839 				mutex_exit(&stp->sd_lock);
750451dd2c77Svk199839 				break;
750551dd2c77Svk199839 			}
750651dd2c77Svk199839 		}
750751dd2c77Svk199839 
750817169044Sbrutus 		ASSERT(!(bp->b_datap->db_flags & DBLK_UIOA));
750951dd2c77Svk199839 		bp = (stp->sd_rputdatafunc)(stp->sd_vnode, bp,
751051dd2c77Svk199839 		    NULL, NULL, NULL, NULL);
7511c28749e9Skais 
7512c28749e9Skais 		if (bp == NULL)
7513c28749e9Skais 			goto retry;
7514c28749e9Skais 	}
7515c28749e9Skais 
75167c478bd9Sstevel@tonic-gate 	if (STREAM_NEEDSERVICE(stp))
75177c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
75187c478bd9Sstevel@tonic-gate 
75197c478bd9Sstevel@tonic-gate 	/*
75207c478bd9Sstevel@tonic-gate 	 * Set HIPRI flag if message is priority.
75217c478bd9Sstevel@tonic-gate 	 */
75227c478bd9Sstevel@tonic-gate 	if (type >= QPCTL)
75237c478bd9Sstevel@tonic-gate 		flg = MSG_HIPRI;
75247c478bd9Sstevel@tonic-gate 	else
75257c478bd9Sstevel@tonic-gate 		flg = MSG_BAND;
75267c478bd9Sstevel@tonic-gate 
75277c478bd9Sstevel@tonic-gate 	/*
75287c478bd9Sstevel@tonic-gate 	 * First process PROTO or PCPROTO blocks, if any.
75297c478bd9Sstevel@tonic-gate 	 */
75307c478bd9Sstevel@tonic-gate 	if (mctlp != NULL && type != M_DATA) {
75317c478bd9Sstevel@tonic-gate 		mblk_t *nbp;
75327c478bd9Sstevel@tonic-gate 
75337c478bd9Sstevel@tonic-gate 		*mctlp = bp;
75347c478bd9Sstevel@tonic-gate 		while (bp->b_cont && bp->b_cont->b_datap->db_type != M_DATA)
75357c478bd9Sstevel@tonic-gate 			bp = bp->b_cont;
75367c478bd9Sstevel@tonic-gate 		nbp = bp->b_cont;
75377c478bd9Sstevel@tonic-gate 		bp->b_cont = NULL;
75387c478bd9Sstevel@tonic-gate 		bp = nbp;
75397c478bd9Sstevel@tonic-gate 	}
75407c478bd9Sstevel@tonic-gate 
75417c478bd9Sstevel@tonic-gate 	if (bp && bp->b_datap->db_type != M_DATA) {
75427c478bd9Sstevel@tonic-gate 		/*
75437c478bd9Sstevel@tonic-gate 		 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
75447c478bd9Sstevel@tonic-gate 		 */
75457c478bd9Sstevel@tonic-gate 		more |= MORECTL;
75467c478bd9Sstevel@tonic-gate 		savemp = bp;
75477c478bd9Sstevel@tonic-gate 		while (bp && bp->b_datap->db_type != M_DATA) {
75487c478bd9Sstevel@tonic-gate 			savemptail = bp;
75497c478bd9Sstevel@tonic-gate 			bp = bp->b_cont;
75507c478bd9Sstevel@tonic-gate 		}
75517c478bd9Sstevel@tonic-gate 		savemptail->b_cont = NULL;
75527c478bd9Sstevel@tonic-gate 	}
75537c478bd9Sstevel@tonic-gate 
75547c478bd9Sstevel@tonic-gate 	/*
75557c478bd9Sstevel@tonic-gate 	 * Now process DATA blocks, if any.
75567c478bd9Sstevel@tonic-gate 	 */
75577c478bd9Sstevel@tonic-gate 	if (uiop == NULL) {
75587c478bd9Sstevel@tonic-gate 		/* Append data to tail of mctlp */
755917169044Sbrutus 
756017169044Sbrutus 		ASSERT(bp == NULL || !(bp->b_datap->db_flags & DBLK_UIOA));
75617c478bd9Sstevel@tonic-gate 		if (mctlp != NULL) {
75627c478bd9Sstevel@tonic-gate 			mblk_t **mpp = mctlp;
75637c478bd9Sstevel@tonic-gate 
75647c478bd9Sstevel@tonic-gate 			while (*mpp != NULL)
75657c478bd9Sstevel@tonic-gate 				mpp = &((*mpp)->b_cont);
75667c478bd9Sstevel@tonic-gate 			*mpp = bp;
75677c478bd9Sstevel@tonic-gate 			bp = NULL;
75687c478bd9Sstevel@tonic-gate 		}
756917169044Sbrutus 	} else if (bp && (bp->b_datap->db_flags & DBLK_UIOA)) {
757017169044Sbrutus 		/*
757117169044Sbrutus 		 * A uioa mblk_t chain, as uio processing has already
757217169044Sbrutus 		 * been done we simple skip over processing.
757317169044Sbrutus 		 */
757417169044Sbrutus 		bp = NULL;
757517169044Sbrutus 		pr = 0;
757617169044Sbrutus 
75777c478bd9Sstevel@tonic-gate 	} else if (uiop->uio_resid >= 0 && bp) {
75787c478bd9Sstevel@tonic-gate 		size_t oldresid = uiop->uio_resid;
75797c478bd9Sstevel@tonic-gate 
75807c478bd9Sstevel@tonic-gate 		/*
75817c478bd9Sstevel@tonic-gate 		 * If a streams message is likely to consist
75827c478bd9Sstevel@tonic-gate 		 * of many small mblks, it is pulled up into
75837c478bd9Sstevel@tonic-gate 		 * one continuous chunk of memory.
75847c478bd9Sstevel@tonic-gate 		 * see longer comment at top of page
75857c478bd9Sstevel@tonic-gate 		 * by mblk_pull_len declaration.
75867c478bd9Sstevel@tonic-gate 		 */
75877c478bd9Sstevel@tonic-gate 
75887c478bd9Sstevel@tonic-gate 		if (MBLKL(bp) < mblk_pull_len) {
75897c478bd9Sstevel@tonic-gate 			(void) pullupmsg(bp, -1);
75907c478bd9Sstevel@tonic-gate 		}
75917c478bd9Sstevel@tonic-gate 
75927c478bd9Sstevel@tonic-gate 		bp = struiocopyout(bp, uiop, &error);
75937c478bd9Sstevel@tonic-gate 		if (error != 0) {
75947c478bd9Sstevel@tonic-gate 			if (mctlp != NULL) {
75957c478bd9Sstevel@tonic-gate 				freemsg(*mctlp);
75967c478bd9Sstevel@tonic-gate 				*mctlp = NULL;
75977c478bd9Sstevel@tonic-gate 			} else
75987c478bd9Sstevel@tonic-gate 				freemsg(savemp);
75997c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
76007c478bd9Sstevel@tonic-gate 			/*
76017c478bd9Sstevel@tonic-gate 			 * clear stream head hi pri flag based on
76027c478bd9Sstevel@tonic-gate 			 * first message
76037c478bd9Sstevel@tonic-gate 			 */
76047c478bd9Sstevel@tonic-gate 			if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
76057c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
76067c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
76077c478bd9Sstevel@tonic-gate 			}
76087c478bd9Sstevel@tonic-gate 			more = 0;
76097c478bd9Sstevel@tonic-gate 			goto getmout;
76107c478bd9Sstevel@tonic-gate 		}
76117c478bd9Sstevel@tonic-gate 		/*
76127c478bd9Sstevel@tonic-gate 		 * (pr == 1) indicates a partial read.
76137c478bd9Sstevel@tonic-gate 		 */
76147c478bd9Sstevel@tonic-gate 		if (oldresid > uiop->uio_resid)
76157c478bd9Sstevel@tonic-gate 			pr = 1;
76167c478bd9Sstevel@tonic-gate 	}
76177c478bd9Sstevel@tonic-gate 
76187c478bd9Sstevel@tonic-gate 	if (bp) {			/* more data blocks in msg */
76197c478bd9Sstevel@tonic-gate 		more |= MOREDATA;
76207c478bd9Sstevel@tonic-gate 		if (savemp)
76217c478bd9Sstevel@tonic-gate 			savemptail->b_cont = bp;
76227c478bd9Sstevel@tonic-gate 		else
76237c478bd9Sstevel@tonic-gate 			savemp = bp;
76247c478bd9Sstevel@tonic-gate 	}
76257c478bd9Sstevel@tonic-gate 
76267c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
76277c478bd9Sstevel@tonic-gate 	if (savemp) {
76287c478bd9Sstevel@tonic-gate 		if (flags & (MSG_IPEEK|MSG_DISCARDTAIL)) {
76297c478bd9Sstevel@tonic-gate 			/*
76307c478bd9Sstevel@tonic-gate 			 * When MSG_DISCARDTAIL is set or
76317c478bd9Sstevel@tonic-gate 			 * when peeking discard any tail. When peeking this
76327c478bd9Sstevel@tonic-gate 			 * is the tail of the dup that was copied out - the
76337c478bd9Sstevel@tonic-gate 			 * message has already been putback on the queue.
76347c478bd9Sstevel@tonic-gate 			 * Return MOREDATA to the caller even though the data
76357c478bd9Sstevel@tonic-gate 			 * is discarded. This is used by sockets (to
76367c478bd9Sstevel@tonic-gate 			 * set MSG_TRUNC).
76377c478bd9Sstevel@tonic-gate 			 */
76387c478bd9Sstevel@tonic-gate 			freemsg(savemp);
76397c478bd9Sstevel@tonic-gate 			if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
76407c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
76417c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
76427c478bd9Sstevel@tonic-gate 			}
76437c478bd9Sstevel@tonic-gate 		} else if (pr && (savemp->b_datap->db_type == M_DATA) &&
76447c478bd9Sstevel@tonic-gate 		    msgnodata(savemp)) {
76457c478bd9Sstevel@tonic-gate 			/*
76467c478bd9Sstevel@tonic-gate 			 * Avoid queuing a zero-length tail part of
76477c478bd9Sstevel@tonic-gate 			 * a message. pr=1 indicates that we read some of
76487c478bd9Sstevel@tonic-gate 			 * the message.
76497c478bd9Sstevel@tonic-gate 			 */
76507c478bd9Sstevel@tonic-gate 			freemsg(savemp);
76517c478bd9Sstevel@tonic-gate 			more &= ~MOREDATA;
76527c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
76537c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
76547c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
76557c478bd9Sstevel@tonic-gate 			}
76567c478bd9Sstevel@tonic-gate 		} else {
76577c478bd9Sstevel@tonic-gate 			savemp->b_band = pri;
76587c478bd9Sstevel@tonic-gate 			/*
76597c478bd9Sstevel@tonic-gate 			 * If the first message was HIPRI and the one we're
76607c478bd9Sstevel@tonic-gate 			 * putting back isn't, then clear STRPRI, otherwise
76617c478bd9Sstevel@tonic-gate 			 * set STRPRI again.  Note that we must set STRPRI
76627c478bd9Sstevel@tonic-gate 			 * again since the flush logic in strrput_nondata()
76637c478bd9Sstevel@tonic-gate 			 * may have cleared it while we had sd_lock dropped.
76647c478bd9Sstevel@tonic-gate 			 */
766517169044Sbrutus 
766617169044Sbrutus 			ASSERT(!(savemp->b_datap->db_flags & DBLK_UIOA));
76677c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
76687c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
76697c478bd9Sstevel@tonic-gate 				if (queclass(savemp) < QPCTL)
76707c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
76717c478bd9Sstevel@tonic-gate 				else
76727c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRPRI;
76737c478bd9Sstevel@tonic-gate 			} else if (queclass(savemp) >= QPCTL) {
76747c478bd9Sstevel@tonic-gate 				/*
76757c478bd9Sstevel@tonic-gate 				 * The first message was not a HIPRI message,
76767c478bd9Sstevel@tonic-gate 				 * but the one we are about to putback is.
76777c478bd9Sstevel@tonic-gate 				 * For simplicitly, we do not allow for HIPRI
76787c478bd9Sstevel@tonic-gate 				 * messages to be embedded in the message
76797c478bd9Sstevel@tonic-gate 				 * body, so just force it to same type as
76807c478bd9Sstevel@tonic-gate 				 * first message.
76817c478bd9Sstevel@tonic-gate 				 */
76827c478bd9Sstevel@tonic-gate 				ASSERT(type == M_DATA || type == M_PROTO);
76837c478bd9Sstevel@tonic-gate 				ASSERT(savemp->b_datap->db_type == M_PCPROTO);
76847c478bd9Sstevel@tonic-gate 				savemp->b_datap->db_type = type;
76857c478bd9Sstevel@tonic-gate 			}
76867c478bd9Sstevel@tonic-gate 			if (mark != 0) {
76877c478bd9Sstevel@tonic-gate 				if ((mark & _LASTMARK) &&
76887c478bd9Sstevel@tonic-gate 				    (stp->sd_mark == NULL)) {
76897c478bd9Sstevel@tonic-gate 					/*
76907c478bd9Sstevel@tonic-gate 					 * If another marked message arrived
76917c478bd9Sstevel@tonic-gate 					 * while sd_lock was not held sd_mark
76927c478bd9Sstevel@tonic-gate 					 * would be non-NULL.
76937c478bd9Sstevel@tonic-gate 					 */
76947c478bd9Sstevel@tonic-gate 					stp->sd_mark = savemp;
76957c478bd9Sstevel@tonic-gate 				}
76967c478bd9Sstevel@tonic-gate 				savemp->b_flag |= mark & ~_LASTMARK;
76977c478bd9Sstevel@tonic-gate 			}
76987c478bd9Sstevel@tonic-gate 			putback(stp, q, savemp, pri);
76997c478bd9Sstevel@tonic-gate 		}
77007c478bd9Sstevel@tonic-gate 	} else if (!(flags & MSG_IPEEK)) {
77017c478bd9Sstevel@tonic-gate 		/*
77027c478bd9Sstevel@tonic-gate 		 * The complete message was consumed.
77037c478bd9Sstevel@tonic-gate 		 *
77047c478bd9Sstevel@tonic-gate 		 * If another M_PCPROTO arrived while sd_lock was not held
77057c478bd9Sstevel@tonic-gate 		 * it would have been discarded since STRPRI was still set.
77067c478bd9Sstevel@tonic-gate 		 *
77077c478bd9Sstevel@tonic-gate 		 * Move the MSG*MARKNEXT information
77087c478bd9Sstevel@tonic-gate 		 * to the stream head just in case
77097c478bd9Sstevel@tonic-gate 		 * the read queue becomes empty.
77107c478bd9Sstevel@tonic-gate 		 * clear stream head hi pri flag based on
77117c478bd9Sstevel@tonic-gate 		 * first message
77127c478bd9Sstevel@tonic-gate 		 *
77137c478bd9Sstevel@tonic-gate 		 * If the stream head was at the mark
77147c478bd9Sstevel@tonic-gate 		 * (STRATMARK) before we dropped sd_lock above
77157c478bd9Sstevel@tonic-gate 		 * and some data was consumed then we have
77167c478bd9Sstevel@tonic-gate 		 * moved past the mark thus STRATMARK is
77177c478bd9Sstevel@tonic-gate 		 * cleared. However, if a message arrived in
77187c478bd9Sstevel@tonic-gate 		 * strrput during the copyout above causing
77197c478bd9Sstevel@tonic-gate 		 * STRATMARK to be set we can not clear that
77207c478bd9Sstevel@tonic-gate 		 * flag.
77217c478bd9Sstevel@tonic-gate 		 * XXX A "perimeter" would help by single-threading strrput,
77227c478bd9Sstevel@tonic-gate 		 * strread, strgetmsg and kstrgetmsg.
77237c478bd9Sstevel@tonic-gate 		 */
77247c478bd9Sstevel@tonic-gate 		if (type >= QPCTL) {
77257c478bd9Sstevel@tonic-gate 			ASSERT(type == M_PCPROTO);
77267c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRPRI;
77277c478bd9Sstevel@tonic-gate 		}
77287c478bd9Sstevel@tonic-gate 		if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
77297c478bd9Sstevel@tonic-gate 			if (mark & MSGMARKNEXT) {
77307c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRNOTATMARK;
77317c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRATMARK;
77327c478bd9Sstevel@tonic-gate 			} else if (mark & MSGNOTMARKNEXT) {
77337c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRATMARK;
77347c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRNOTATMARK;
77357c478bd9Sstevel@tonic-gate 			} else {
77367c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
77377c478bd9Sstevel@tonic-gate 			}
77387c478bd9Sstevel@tonic-gate 		} else if (pr && (old_sd_flag & STRATMARK)) {
77397c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRATMARK;
77407c478bd9Sstevel@tonic-gate 		}
77417c478bd9Sstevel@tonic-gate 	}
77427c478bd9Sstevel@tonic-gate 
77437c478bd9Sstevel@tonic-gate 	*flagsp = flg;
77447c478bd9Sstevel@tonic-gate 	*prip = pri;
77457c478bd9Sstevel@tonic-gate 
77467c478bd9Sstevel@tonic-gate 	/*
77477c478bd9Sstevel@tonic-gate 	 * Getmsg cleanup processing - if the state of the queue has changed
77487c478bd9Sstevel@tonic-gate 	 * some signals may need to be sent and/or poll awakened.
77497c478bd9Sstevel@tonic-gate 	 */
77507c478bd9Sstevel@tonic-gate getmout:
77517c478bd9Sstevel@tonic-gate 	qbackenable(q, pri);
77527c478bd9Sstevel@tonic-gate 
77537c478bd9Sstevel@tonic-gate 	/*
77547c478bd9Sstevel@tonic-gate 	 * We dropped the stream head lock above. Send all M_SIG messages
77557c478bd9Sstevel@tonic-gate 	 * before processing stream head for SIGPOLL messages.
77567c478bd9Sstevel@tonic-gate 	 */
77577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
77587c478bd9Sstevel@tonic-gate 	while ((bp = q->q_first) != NULL &&
77597c478bd9Sstevel@tonic-gate 	    (bp->b_datap->db_type == M_SIG)) {
77607c478bd9Sstevel@tonic-gate 		/*
77617c478bd9Sstevel@tonic-gate 		 * sd_lock is held so the content of the read queue can not
77627c478bd9Sstevel@tonic-gate 		 * change.
77637c478bd9Sstevel@tonic-gate 		 */
77647c478bd9Sstevel@tonic-gate 		bp = getq(q);
77657c478bd9Sstevel@tonic-gate 		ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
77667c478bd9Sstevel@tonic-gate 
77677c478bd9Sstevel@tonic-gate 		strsignal_nolock(stp, *bp->b_rptr, (int32_t)bp->b_band);
77687c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
77697c478bd9Sstevel@tonic-gate 		freemsg(bp);
77707c478bd9Sstevel@tonic-gate 		if (STREAM_NEEDSERVICE(stp))
77717c478bd9Sstevel@tonic-gate 			stream_runservice(stp);
77727c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
77737c478bd9Sstevel@tonic-gate 	}
77747c478bd9Sstevel@tonic-gate 
77757c478bd9Sstevel@tonic-gate 	/*
77767c478bd9Sstevel@tonic-gate 	 * stream head cannot change while we make the determination
77777c478bd9Sstevel@tonic-gate 	 * whether or not to send a signal. Drop the flag to allow strrput
77787c478bd9Sstevel@tonic-gate 	 * to send firstmsgsigs again.
77797c478bd9Sstevel@tonic-gate 	 */
77807c478bd9Sstevel@tonic-gate 	stp->sd_flag &= ~STRGETINPROG;
77817c478bd9Sstevel@tonic-gate 
77827c478bd9Sstevel@tonic-gate 	/*
77837c478bd9Sstevel@tonic-gate 	 * If the type of message at the front of the queue changed
77847c478bd9Sstevel@tonic-gate 	 * due to the receive the appropriate signals and pollwakeup events
77857c478bd9Sstevel@tonic-gate 	 * are generated. The type of changes are:
77867c478bd9Sstevel@tonic-gate 	 *	Processed a hipri message, q_first is not hipri.
77877c478bd9Sstevel@tonic-gate 	 *	Processed a band X message, and q_first is band Y.
77887c478bd9Sstevel@tonic-gate 	 * The generated signals and pollwakeups are identical to what
77897c478bd9Sstevel@tonic-gate 	 * strrput() generates should the message that is now on q_first
77907c478bd9Sstevel@tonic-gate 	 * arrive to an empty read queue.
77917c478bd9Sstevel@tonic-gate 	 *
77927c478bd9Sstevel@tonic-gate 	 * Note: only strrput will send a signal for a hipri message.
77937c478bd9Sstevel@tonic-gate 	 */
77947c478bd9Sstevel@tonic-gate 	if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
77957c478bd9Sstevel@tonic-gate 		strsigset_t signals = 0;
77967c478bd9Sstevel@tonic-gate 		strpollset_t pollwakeups = 0;
77977c478bd9Sstevel@tonic-gate 
77987c478bd9Sstevel@tonic-gate 		if (flg & MSG_HIPRI) {
77997c478bd9Sstevel@tonic-gate 			/*
78007c478bd9Sstevel@tonic-gate 			 * Removed a hipri message. Regular data at
78017c478bd9Sstevel@tonic-gate 			 * the front of  the queue.
78027c478bd9Sstevel@tonic-gate 			 */
78037c478bd9Sstevel@tonic-gate 			if (bp->b_band == 0) {
78047c478bd9Sstevel@tonic-gate 				signals = S_INPUT | S_RDNORM;
78057c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDNORM;
78067c478bd9Sstevel@tonic-gate 			} else {
78077c478bd9Sstevel@tonic-gate 				signals = S_INPUT | S_RDBAND;
78087c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDBAND;
78097c478bd9Sstevel@tonic-gate 			}
78107c478bd9Sstevel@tonic-gate 		} else if (pri != bp->b_band) {
78117c478bd9Sstevel@tonic-gate 			/*
78127c478bd9Sstevel@tonic-gate 			 * The band is different for the new q_first.
78137c478bd9Sstevel@tonic-gate 			 */
78147c478bd9Sstevel@tonic-gate 			if (bp->b_band == 0) {
78157c478bd9Sstevel@tonic-gate 				signals = S_RDNORM;
78167c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDNORM;
78177c478bd9Sstevel@tonic-gate 			} else {
78187c478bd9Sstevel@tonic-gate 				signals = S_RDBAND;
78197c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDBAND;
78207c478bd9Sstevel@tonic-gate 			}
78217c478bd9Sstevel@tonic-gate 		}
78227c478bd9Sstevel@tonic-gate 
78237c478bd9Sstevel@tonic-gate 		if (pollwakeups != 0) {
78247c478bd9Sstevel@tonic-gate 			if (pollwakeups == (POLLIN | POLLRDNORM)) {
78257c478bd9Sstevel@tonic-gate 				if (!(stp->sd_rput_opt & SR_POLLIN))
78267c478bd9Sstevel@tonic-gate 					goto no_pollwake;
78277c478bd9Sstevel@tonic-gate 				stp->sd_rput_opt &= ~SR_POLLIN;
78287c478bd9Sstevel@tonic-gate 			}
78297c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
78307c478bd9Sstevel@tonic-gate 			pollwakeup(&stp->sd_pollist, pollwakeups);
78317c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
78327c478bd9Sstevel@tonic-gate 		}
78337c478bd9Sstevel@tonic-gate no_pollwake:
78347c478bd9Sstevel@tonic-gate 
78357c478bd9Sstevel@tonic-gate 		if (stp->sd_sigflags & signals)
78367c478bd9Sstevel@tonic-gate 			strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
78377c478bd9Sstevel@tonic-gate 	}
78387c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
78397c478bd9Sstevel@tonic-gate 
78407c478bd9Sstevel@tonic-gate 	rvp->r_val1 = more;
78417c478bd9Sstevel@tonic-gate 	return (error);
78427c478bd9Sstevel@tonic-gate #undef	_LASTMARK
78437c478bd9Sstevel@tonic-gate }
78447c478bd9Sstevel@tonic-gate 
78457c478bd9Sstevel@tonic-gate /*
78467c478bd9Sstevel@tonic-gate  * Put a message downstream.
78477c478bd9Sstevel@tonic-gate  *
78487c478bd9Sstevel@tonic-gate  * NOTE: strputmsg and kstrputmsg have much of the logic in common.
78497c478bd9Sstevel@tonic-gate  */
78507c478bd9Sstevel@tonic-gate int
78517c478bd9Sstevel@tonic-gate strputmsg(
78527c478bd9Sstevel@tonic-gate 	struct vnode *vp,
78537c478bd9Sstevel@tonic-gate 	struct strbuf *mctl,
78547c478bd9Sstevel@tonic-gate 	struct strbuf *mdata,
78557c478bd9Sstevel@tonic-gate 	unsigned char pri,
78567c478bd9Sstevel@tonic-gate 	int flag,
78577c478bd9Sstevel@tonic-gate 	int fmode)
78587c478bd9Sstevel@tonic-gate {
78597c478bd9Sstevel@tonic-gate 	struct stdata *stp;
78607c478bd9Sstevel@tonic-gate 	queue_t *wqp;
78617c478bd9Sstevel@tonic-gate 	mblk_t *mp;
78627c478bd9Sstevel@tonic-gate 	ssize_t msgsize;
78637c478bd9Sstevel@tonic-gate 	ssize_t rmin, rmax;
78647c478bd9Sstevel@tonic-gate 	int error;
78657c478bd9Sstevel@tonic-gate 	struct uio uios;
78667c478bd9Sstevel@tonic-gate 	struct uio *uiop = &uios;
78677c478bd9Sstevel@tonic-gate 	struct iovec iovs;
78687c478bd9Sstevel@tonic-gate 	int xpg4 = 0;
78697c478bd9Sstevel@tonic-gate 
78707c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
78717c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
78727c478bd9Sstevel@tonic-gate 	wqp = stp->sd_wrq;
78737c478bd9Sstevel@tonic-gate 
78747c478bd9Sstevel@tonic-gate 	/*
78757c478bd9Sstevel@tonic-gate 	 * If it is an XPG4 application, we need to send
78767c478bd9Sstevel@tonic-gate 	 * SIGPIPE below
78777c478bd9Sstevel@tonic-gate 	 */
78787c478bd9Sstevel@tonic-gate 
78797c478bd9Sstevel@tonic-gate 	xpg4 = (flag & MSG_XPG4) ? 1 : 0;
78807c478bd9Sstevel@tonic-gate 	flag &= ~MSG_XPG4;
78817c478bd9Sstevel@tonic-gate 
78827c478bd9Sstevel@tonic-gate 	if (audit_active)
78837c478bd9Sstevel@tonic-gate 		audit_strputmsg(vp, mctl, mdata, pri, flag, fmode);
78847c478bd9Sstevel@tonic-gate 
78857c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
78869acbbeafSnn35248 
78879acbbeafSnn35248 	if ((error = i_straccess(stp, JCWRITE)) != 0) {
78887c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
78897c478bd9Sstevel@tonic-gate 		return (error);
78907c478bd9Sstevel@tonic-gate 	}
78917c478bd9Sstevel@tonic-gate 
78929acbbeafSnn35248 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
78939acbbeafSnn35248 		error = strwriteable(stp, B_FALSE, xpg4);
78949acbbeafSnn35248 		if (error != 0) {
78959acbbeafSnn35248 			mutex_exit(&stp->sd_lock);
78969acbbeafSnn35248 			return (error);
78979acbbeafSnn35248 		}
78989acbbeafSnn35248 	}
78999acbbeafSnn35248 
79009acbbeafSnn35248 	mutex_exit(&stp->sd_lock);
79019acbbeafSnn35248 
79027c478bd9Sstevel@tonic-gate 	/*
79037c478bd9Sstevel@tonic-gate 	 * Check for legal flag value.
79047c478bd9Sstevel@tonic-gate 	 */
79057c478bd9Sstevel@tonic-gate 	switch (flag) {
79067c478bd9Sstevel@tonic-gate 	case MSG_HIPRI:
79077c478bd9Sstevel@tonic-gate 		if ((mctl->len < 0) || (pri != 0))
79087c478bd9Sstevel@tonic-gate 			return (EINVAL);
79097c478bd9Sstevel@tonic-gate 		break;
79107c478bd9Sstevel@tonic-gate 	case MSG_BAND:
79117c478bd9Sstevel@tonic-gate 		break;
79127c478bd9Sstevel@tonic-gate 
79137c478bd9Sstevel@tonic-gate 	default:
79147c478bd9Sstevel@tonic-gate 		return (EINVAL);
79157c478bd9Sstevel@tonic-gate 	}
79167c478bd9Sstevel@tonic-gate 
79177c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_IN,
79187c478bd9Sstevel@tonic-gate 	    "strputmsg in:stp %p", stp);
79197c478bd9Sstevel@tonic-gate 
79207c478bd9Sstevel@tonic-gate 	/* get these values from those cached in the stream head */
79217c478bd9Sstevel@tonic-gate 	rmin = stp->sd_qn_minpsz;
79227c478bd9Sstevel@tonic-gate 	rmax = stp->sd_qn_maxpsz;
79237c478bd9Sstevel@tonic-gate 
79247c478bd9Sstevel@tonic-gate 	/*
79257c478bd9Sstevel@tonic-gate 	 * Make sure ctl and data sizes together fall within the
79267c478bd9Sstevel@tonic-gate 	 * limits of the max and min receive packet sizes and do
79277c478bd9Sstevel@tonic-gate 	 * not exceed system limit.
79287c478bd9Sstevel@tonic-gate 	 */
79297c478bd9Sstevel@tonic-gate 	ASSERT((rmax >= 0) || (rmax == INFPSZ));
79307c478bd9Sstevel@tonic-gate 	if (rmax == 0) {
79317c478bd9Sstevel@tonic-gate 		return (ERANGE);
79327c478bd9Sstevel@tonic-gate 	}
79337c478bd9Sstevel@tonic-gate 	/*
79347c478bd9Sstevel@tonic-gate 	 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
79357c478bd9Sstevel@tonic-gate 	 * Needed to prevent partial failures in the strmakedata loop.
79367c478bd9Sstevel@tonic-gate 	 */
79377c478bd9Sstevel@tonic-gate 	if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
79387c478bd9Sstevel@tonic-gate 		rmax = stp->sd_maxblk;
79397c478bd9Sstevel@tonic-gate 
79407c478bd9Sstevel@tonic-gate 	if ((msgsize = mdata->len) < 0) {
79417c478bd9Sstevel@tonic-gate 		msgsize = 0;
79427c478bd9Sstevel@tonic-gate 		rmin = 0;	/* no range check for NULL data part */
79437c478bd9Sstevel@tonic-gate 	}
79447c478bd9Sstevel@tonic-gate 	if ((msgsize < rmin) ||
79457c478bd9Sstevel@tonic-gate 	    ((msgsize > rmax) && (rmax != INFPSZ)) ||
79467c478bd9Sstevel@tonic-gate 	    (mctl->len > strctlsz)) {
79477c478bd9Sstevel@tonic-gate 		return (ERANGE);
79487c478bd9Sstevel@tonic-gate 	}
79497c478bd9Sstevel@tonic-gate 
79507c478bd9Sstevel@tonic-gate 	/*
79517c478bd9Sstevel@tonic-gate 	 * Setup uio and iov for data part
79527c478bd9Sstevel@tonic-gate 	 */
79537c478bd9Sstevel@tonic-gate 	iovs.iov_base = mdata->buf;
79547c478bd9Sstevel@tonic-gate 	iovs.iov_len = msgsize;
79557c478bd9Sstevel@tonic-gate 	uios.uio_iov = &iovs;
79567c478bd9Sstevel@tonic-gate 	uios.uio_iovcnt = 1;
79577c478bd9Sstevel@tonic-gate 	uios.uio_loffset = 0;
79587c478bd9Sstevel@tonic-gate 	uios.uio_segflg = UIO_USERSPACE;
79597c478bd9Sstevel@tonic-gate 	uios.uio_fmode = fmode;
79607c478bd9Sstevel@tonic-gate 	uios.uio_extflg = UIO_COPY_DEFAULT;
79617c478bd9Sstevel@tonic-gate 	uios.uio_resid = msgsize;
79627c478bd9Sstevel@tonic-gate 	uios.uio_offset = 0;
79637c478bd9Sstevel@tonic-gate 
79647c478bd9Sstevel@tonic-gate 	/* Ignore flow control in strput for HIPRI */
79657c478bd9Sstevel@tonic-gate 	if (flag & MSG_HIPRI)
79667c478bd9Sstevel@tonic-gate 		flag |= MSG_IGNFLOW;
79677c478bd9Sstevel@tonic-gate 
79687c478bd9Sstevel@tonic-gate 	for (;;) {
79697c478bd9Sstevel@tonic-gate 		int done = 0;
79707c478bd9Sstevel@tonic-gate 
79717c478bd9Sstevel@tonic-gate 		/*
79727c478bd9Sstevel@tonic-gate 		 * strput will always free the ctl mblk - even when strput
79737c478bd9Sstevel@tonic-gate 		 * fails.
79747c478bd9Sstevel@tonic-gate 		 */
79757c478bd9Sstevel@tonic-gate 		if ((error = strmakectl(mctl, flag, fmode, &mp)) != 0) {
79767c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
79777c478bd9Sstevel@tonic-gate 			    "strputmsg out:stp %p out %d error %d",
79787c478bd9Sstevel@tonic-gate 			    stp, 1, error);
79797c478bd9Sstevel@tonic-gate 			return (error);
79807c478bd9Sstevel@tonic-gate 		}
79817c478bd9Sstevel@tonic-gate 		/*
79827c478bd9Sstevel@tonic-gate 		 * Verify that the whole message can be transferred by
79837c478bd9Sstevel@tonic-gate 		 * strput.
79847c478bd9Sstevel@tonic-gate 		 */
79857c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_maxblk == INFPSZ ||
79867c478bd9Sstevel@tonic-gate 		    stp->sd_maxblk >= mdata->len);
79877c478bd9Sstevel@tonic-gate 
79887c478bd9Sstevel@tonic-gate 		msgsize = mdata->len;
79897c478bd9Sstevel@tonic-gate 		error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
79907c478bd9Sstevel@tonic-gate 		mdata->len = msgsize;
79917c478bd9Sstevel@tonic-gate 
79927c478bd9Sstevel@tonic-gate 		if (error == 0)
79937c478bd9Sstevel@tonic-gate 			break;
79947c478bd9Sstevel@tonic-gate 
79957c478bd9Sstevel@tonic-gate 		if (error != EWOULDBLOCK)
79967c478bd9Sstevel@tonic-gate 			goto out;
79977c478bd9Sstevel@tonic-gate 
79987c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
79997c478bd9Sstevel@tonic-gate 		/*
80007c478bd9Sstevel@tonic-gate 		 * Check for a missed wakeup.
80017c478bd9Sstevel@tonic-gate 		 * Needed since strput did not hold sd_lock across
80027c478bd9Sstevel@tonic-gate 		 * the canputnext.
80037c478bd9Sstevel@tonic-gate 		 */
80047c478bd9Sstevel@tonic-gate 		if (bcanputnext(wqp, pri)) {
80057c478bd9Sstevel@tonic-gate 			/* Try again */
80067c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
80077c478bd9Sstevel@tonic-gate 			continue;
80087c478bd9Sstevel@tonic-gate 		}
80097c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAIT,
80107c478bd9Sstevel@tonic-gate 		    "strputmsg wait:stp %p waits pri %d", stp, pri);
80117c478bd9Sstevel@tonic-gate 		if (((error = strwaitq(stp, WRITEWAIT, (ssize_t)0, fmode, -1,
80127c478bd9Sstevel@tonic-gate 		    &done)) != 0) || done) {
80137c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
80147c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
80157c478bd9Sstevel@tonic-gate 			    "strputmsg out:q %p out %d error %d",
80167c478bd9Sstevel@tonic-gate 			    stp, 0, error);
80177c478bd9Sstevel@tonic-gate 			return (error);
80187c478bd9Sstevel@tonic-gate 		}
80197c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAKE,
80207c478bd9Sstevel@tonic-gate 		    "strputmsg wake:stp %p wakes", stp);
80219acbbeafSnn35248 		if ((error = i_straccess(stp, JCWRITE)) != 0) {
80227c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
80237c478bd9Sstevel@tonic-gate 			return (error);
80247c478bd9Sstevel@tonic-gate 		}
80259acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
80269acbbeafSnn35248 	}
80277c478bd9Sstevel@tonic-gate out:
80287c478bd9Sstevel@tonic-gate 	/*
80297c478bd9Sstevel@tonic-gate 	 * For historic reasons, applications expect EAGAIN
80307c478bd9Sstevel@tonic-gate 	 * when data mblk could not be allocated. so change
80317c478bd9Sstevel@tonic-gate 	 * ENOMEM back to EAGAIN
80327c478bd9Sstevel@tonic-gate 	 */
80337c478bd9Sstevel@tonic-gate 	if (error == ENOMEM)
80347c478bd9Sstevel@tonic-gate 		error = EAGAIN;
80357c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
80367c478bd9Sstevel@tonic-gate 	    "strputmsg out:stp %p out %d error %d", stp, 2, error);
80377c478bd9Sstevel@tonic-gate 	return (error);
80387c478bd9Sstevel@tonic-gate }
80397c478bd9Sstevel@tonic-gate 
80407c478bd9Sstevel@tonic-gate /*
80417c478bd9Sstevel@tonic-gate  * Put a message downstream.
80427c478bd9Sstevel@tonic-gate  * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
80437c478bd9Sstevel@tonic-gate  * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
80447c478bd9Sstevel@tonic-gate  * and the fmode parameter.
80457c478bd9Sstevel@tonic-gate  *
80467c478bd9Sstevel@tonic-gate  * This routine handles the consolidation private flags:
80477c478bd9Sstevel@tonic-gate  *	MSG_IGNERROR	Ignore any stream head error except STPLEX.
80487c478bd9Sstevel@tonic-gate  *	MSG_HOLDSIG	Hold signals while waiting for data.
80497c478bd9Sstevel@tonic-gate  *	MSG_IGNFLOW	Don't check streams flow control.
80507c478bd9Sstevel@tonic-gate  *
80517c478bd9Sstevel@tonic-gate  * NOTE: strputmsg and kstrputmsg have much of the logic in common.
80527c478bd9Sstevel@tonic-gate  */
80537c478bd9Sstevel@tonic-gate int
80547c478bd9Sstevel@tonic-gate kstrputmsg(
80557c478bd9Sstevel@tonic-gate 	struct vnode *vp,
80567c478bd9Sstevel@tonic-gate 	mblk_t *mctl,
80577c478bd9Sstevel@tonic-gate 	struct uio *uiop,
80587c478bd9Sstevel@tonic-gate 	ssize_t msgsize,
80597c478bd9Sstevel@tonic-gate 	unsigned char pri,
80607c478bd9Sstevel@tonic-gate 	int flag,
80617c478bd9Sstevel@tonic-gate 	int fmode)
80627c478bd9Sstevel@tonic-gate {
80637c478bd9Sstevel@tonic-gate 	struct stdata *stp;
80647c478bd9Sstevel@tonic-gate 	queue_t *wqp;
80657c478bd9Sstevel@tonic-gate 	ssize_t rmin, rmax;
80667c478bd9Sstevel@tonic-gate 	int error;
80677c478bd9Sstevel@tonic-gate 
80687c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
80697c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
80707c478bd9Sstevel@tonic-gate 	wqp = stp->sd_wrq;
80717c478bd9Sstevel@tonic-gate 	if (audit_active)
80727c478bd9Sstevel@tonic-gate 		audit_strputmsg(vp, NULL, NULL, pri, flag, fmode);
8073e4506d67Smeem 	if (mctl == NULL)
8074e4506d67Smeem 		return (EINVAL);
80757c478bd9Sstevel@tonic-gate 
80769acbbeafSnn35248 	mutex_enter(&stp->sd_lock);
80779acbbeafSnn35248 
80789acbbeafSnn35248 	if ((error = i_straccess(stp, JCWRITE)) != 0) {
80799acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
80807c478bd9Sstevel@tonic-gate 		freemsg(mctl);
80817c478bd9Sstevel@tonic-gate 		return (error);
80827c478bd9Sstevel@tonic-gate 	}
80837c478bd9Sstevel@tonic-gate 
80847c478bd9Sstevel@tonic-gate 	if ((stp->sd_flag & STPLEX) || !(flag & MSG_IGNERROR)) {
80857c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
80867c478bd9Sstevel@tonic-gate 			error = strwriteable(stp, B_FALSE, B_TRUE);
80877c478bd9Sstevel@tonic-gate 			if (error != 0) {
80889acbbeafSnn35248 				mutex_exit(&stp->sd_lock);
80897c478bd9Sstevel@tonic-gate 				freemsg(mctl);
80907c478bd9Sstevel@tonic-gate 				return (error);
80917c478bd9Sstevel@tonic-gate 			}
80927c478bd9Sstevel@tonic-gate 		}
80937c478bd9Sstevel@tonic-gate 	}
80947c478bd9Sstevel@tonic-gate 
80959acbbeafSnn35248 	mutex_exit(&stp->sd_lock);
80969acbbeafSnn35248 
80977c478bd9Sstevel@tonic-gate 	/*
80987c478bd9Sstevel@tonic-gate 	 * Check for legal flag value.
80997c478bd9Sstevel@tonic-gate 	 */
81007c478bd9Sstevel@tonic-gate 	switch (flag & (MSG_HIPRI|MSG_BAND|MSG_ANY)) {
81017c478bd9Sstevel@tonic-gate 	case MSG_HIPRI:
8102e4506d67Smeem 		if (pri != 0) {
81037c478bd9Sstevel@tonic-gate 			freemsg(mctl);
81047c478bd9Sstevel@tonic-gate 			return (EINVAL);
81057c478bd9Sstevel@tonic-gate 		}
81067c478bd9Sstevel@tonic-gate 		break;
81077c478bd9Sstevel@tonic-gate 	case MSG_BAND:
81087c478bd9Sstevel@tonic-gate 		break;
81097c478bd9Sstevel@tonic-gate 	default:
81107c478bd9Sstevel@tonic-gate 		freemsg(mctl);
81117c478bd9Sstevel@tonic-gate 		return (EINVAL);
81127c478bd9Sstevel@tonic-gate 	}
81137c478bd9Sstevel@tonic-gate 
81147c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_IN,
81157c478bd9Sstevel@tonic-gate 	    "kstrputmsg in:stp %p", stp);
81167c478bd9Sstevel@tonic-gate 
81177c478bd9Sstevel@tonic-gate 	/* get these values from those cached in the stream head */
81187c478bd9Sstevel@tonic-gate 	rmin = stp->sd_qn_minpsz;
81197c478bd9Sstevel@tonic-gate 	rmax = stp->sd_qn_maxpsz;
81207c478bd9Sstevel@tonic-gate 
81217c478bd9Sstevel@tonic-gate 	/*
81227c478bd9Sstevel@tonic-gate 	 * Make sure ctl and data sizes together fall within the
81237c478bd9Sstevel@tonic-gate 	 * limits of the max and min receive packet sizes and do
81247c478bd9Sstevel@tonic-gate 	 * not exceed system limit.
81257c478bd9Sstevel@tonic-gate 	 */
81267c478bd9Sstevel@tonic-gate 	ASSERT((rmax >= 0) || (rmax == INFPSZ));
81277c478bd9Sstevel@tonic-gate 	if (rmax == 0) {
81287c478bd9Sstevel@tonic-gate 		freemsg(mctl);
81297c478bd9Sstevel@tonic-gate 		return (ERANGE);
81307c478bd9Sstevel@tonic-gate 	}
81317c478bd9Sstevel@tonic-gate 	/*
81327c478bd9Sstevel@tonic-gate 	 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
81337c478bd9Sstevel@tonic-gate 	 * Needed to prevent partial failures in the strmakedata loop.
81347c478bd9Sstevel@tonic-gate 	 */
81357c478bd9Sstevel@tonic-gate 	if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
81367c478bd9Sstevel@tonic-gate 		rmax = stp->sd_maxblk;
81377c478bd9Sstevel@tonic-gate 
81387c478bd9Sstevel@tonic-gate 	if (uiop == NULL) {
81397c478bd9Sstevel@tonic-gate 		msgsize = -1;
81407c478bd9Sstevel@tonic-gate 		rmin = -1;	/* no range check for NULL data part */
81417c478bd9Sstevel@tonic-gate 	} else {
81427c478bd9Sstevel@tonic-gate 		/* Use uio flags as well as the fmode parameter flags */
81437c478bd9Sstevel@tonic-gate 		fmode |= uiop->uio_fmode;
81447c478bd9Sstevel@tonic-gate 
81457c478bd9Sstevel@tonic-gate 		if ((msgsize < rmin) ||
81467c478bd9Sstevel@tonic-gate 		    ((msgsize > rmax) && (rmax != INFPSZ))) {
81477c478bd9Sstevel@tonic-gate 			freemsg(mctl);
81487c478bd9Sstevel@tonic-gate 			return (ERANGE);
81497c478bd9Sstevel@tonic-gate 		}
81507c478bd9Sstevel@tonic-gate 	}
81517c478bd9Sstevel@tonic-gate 
81527c478bd9Sstevel@tonic-gate 	/* Ignore flow control in strput for HIPRI */
81537c478bd9Sstevel@tonic-gate 	if (flag & MSG_HIPRI)
81547c478bd9Sstevel@tonic-gate 		flag |= MSG_IGNFLOW;
81557c478bd9Sstevel@tonic-gate 
81567c478bd9Sstevel@tonic-gate 	for (;;) {
81577c478bd9Sstevel@tonic-gate 		int done = 0;
81587c478bd9Sstevel@tonic-gate 		int waitflag;
81597c478bd9Sstevel@tonic-gate 		mblk_t *mp;
81607c478bd9Sstevel@tonic-gate 
81617c478bd9Sstevel@tonic-gate 		/*
81627c478bd9Sstevel@tonic-gate 		 * strput will always free the ctl mblk - even when strput
81637c478bd9Sstevel@tonic-gate 		 * fails. If MSG_IGNFLOW is set then any error returned
81647c478bd9Sstevel@tonic-gate 		 * will cause us to break the loop, so we don't need a copy
81657c478bd9Sstevel@tonic-gate 		 * of the message. If MSG_IGNFLOW is not set, then we can
81667c478bd9Sstevel@tonic-gate 		 * get hit by flow control and be forced to try again. In
81677c478bd9Sstevel@tonic-gate 		 * this case we need to have a copy of the message. We
81687c478bd9Sstevel@tonic-gate 		 * do this using copymsg since the message may get modified
81697c478bd9Sstevel@tonic-gate 		 * by something below us.
81707c478bd9Sstevel@tonic-gate 		 *
81717c478bd9Sstevel@tonic-gate 		 * We've observed that many TPI providers do not check db_ref
81727c478bd9Sstevel@tonic-gate 		 * on the control messages but blindly reuse them for the
81737c478bd9Sstevel@tonic-gate 		 * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
81747c478bd9Sstevel@tonic-gate 		 * friendly to such providers than using dupmsg. Also, note
81757c478bd9Sstevel@tonic-gate 		 * that sockfs uses MSG_IGNFLOW for all TPI control messages.
81767c478bd9Sstevel@tonic-gate 		 * Only data messages are subject to flow control, hence
81777c478bd9Sstevel@tonic-gate 		 * subject to this copymsg.
81787c478bd9Sstevel@tonic-gate 		 */
81797c478bd9Sstevel@tonic-gate 		if (flag & MSG_IGNFLOW) {
81807c478bd9Sstevel@tonic-gate 			mp = mctl;
81817c478bd9Sstevel@tonic-gate 			mctl = NULL;
81827c478bd9Sstevel@tonic-gate 		} else {
81837c478bd9Sstevel@tonic-gate 			do {
81847c478bd9Sstevel@tonic-gate 				/*
81857c478bd9Sstevel@tonic-gate 				 * If a message has a free pointer, the message
81867c478bd9Sstevel@tonic-gate 				 * must be dupmsg to maintain this pointer.
81877c478bd9Sstevel@tonic-gate 				 * Code using this facility must be sure
81887c478bd9Sstevel@tonic-gate 				 * that modules below will not change the
81897c478bd9Sstevel@tonic-gate 				 * contents of the dblk without checking db_ref
81907c478bd9Sstevel@tonic-gate 				 * first. If db_ref is > 1, then the module
81917c478bd9Sstevel@tonic-gate 				 * needs to do a copymsg first. Otherwise,
81927c478bd9Sstevel@tonic-gate 				 * the contents of the dblk may become
81937c478bd9Sstevel@tonic-gate 				 * inconsistent because the freesmg/freeb below
81947c478bd9Sstevel@tonic-gate 				 * may end up calling atomic_add_32_nv.
81957c478bd9Sstevel@tonic-gate 				 * The atomic_add_32_nv in freeb (accessing
81967c478bd9Sstevel@tonic-gate 				 * all of db_ref, db_type, db_flags, and
81977c478bd9Sstevel@tonic-gate 				 * db_struioflag) does not prevent other threads
81987c478bd9Sstevel@tonic-gate 				 * from concurrently trying to modify e.g.
81997c478bd9Sstevel@tonic-gate 				 * db_type.
82007c478bd9Sstevel@tonic-gate 				 */
82017c478bd9Sstevel@tonic-gate 				if (mctl->b_datap->db_frtnp != NULL)
82027c478bd9Sstevel@tonic-gate 					mp = dupmsg(mctl);
82037c478bd9Sstevel@tonic-gate 				else
82047c478bd9Sstevel@tonic-gate 					mp = copymsg(mctl);
82057c478bd9Sstevel@tonic-gate 
8206e4506d67Smeem 				if (mp != NULL)
82077c478bd9Sstevel@tonic-gate 					break;
82087c478bd9Sstevel@tonic-gate 
82097c478bd9Sstevel@tonic-gate 				error = strwaitbuf(msgdsize(mctl), BPRI_MED);
82107c478bd9Sstevel@tonic-gate 				if (error) {
82117c478bd9Sstevel@tonic-gate 					freemsg(mctl);
82127c478bd9Sstevel@tonic-gate 					return (error);
82137c478bd9Sstevel@tonic-gate 				}
82147c478bd9Sstevel@tonic-gate 			} while (mp == NULL);
82157c478bd9Sstevel@tonic-gate 		}
82167c478bd9Sstevel@tonic-gate 		/*
82177c478bd9Sstevel@tonic-gate 		 * Verify that all of msgsize can be transferred by
82187c478bd9Sstevel@tonic-gate 		 * strput.
82197c478bd9Sstevel@tonic-gate 		 */
8220e4506d67Smeem 		ASSERT(stp->sd_maxblk == INFPSZ || stp->sd_maxblk >= msgsize);
82217c478bd9Sstevel@tonic-gate 		error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
82227c478bd9Sstevel@tonic-gate 		if (error == 0)
82237c478bd9Sstevel@tonic-gate 			break;
82247c478bd9Sstevel@tonic-gate 
82257c478bd9Sstevel@tonic-gate 		if (error != EWOULDBLOCK)
82267c478bd9Sstevel@tonic-gate 			goto out;
82277c478bd9Sstevel@tonic-gate 
82287c478bd9Sstevel@tonic-gate 		/*
82297c478bd9Sstevel@tonic-gate 		 * IF MSG_IGNFLOW is set we should have broken out of loop
82307c478bd9Sstevel@tonic-gate 		 * above.
82317c478bd9Sstevel@tonic-gate 		 */
82327c478bd9Sstevel@tonic-gate 		ASSERT(!(flag & MSG_IGNFLOW));
82337c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
82347c478bd9Sstevel@tonic-gate 		/*
82357c478bd9Sstevel@tonic-gate 		 * Check for a missed wakeup.
82367c478bd9Sstevel@tonic-gate 		 * Needed since strput did not hold sd_lock across
82377c478bd9Sstevel@tonic-gate 		 * the canputnext.
82387c478bd9Sstevel@tonic-gate 		 */
82397c478bd9Sstevel@tonic-gate 		if (bcanputnext(wqp, pri)) {
82407c478bd9Sstevel@tonic-gate 			/* Try again */
82417c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
82427c478bd9Sstevel@tonic-gate 			continue;
82437c478bd9Sstevel@tonic-gate 		}
82447c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAIT,
82457c478bd9Sstevel@tonic-gate 		    "kstrputmsg wait:stp %p waits pri %d", stp, pri);
82467c478bd9Sstevel@tonic-gate 
82477c478bd9Sstevel@tonic-gate 		waitflag = WRITEWAIT;
82487c478bd9Sstevel@tonic-gate 		if (flag & (MSG_HOLDSIG|MSG_IGNERROR)) {
82497c478bd9Sstevel@tonic-gate 			if (flag & MSG_HOLDSIG)
82507c478bd9Sstevel@tonic-gate 				waitflag |= STR_NOSIG;
82517c478bd9Sstevel@tonic-gate 			if (flag & MSG_IGNERROR)
82527c478bd9Sstevel@tonic-gate 				waitflag |= STR_NOERROR;
82537c478bd9Sstevel@tonic-gate 		}
82547c478bd9Sstevel@tonic-gate 		if (((error = strwaitq(stp, waitflag,
82557c478bd9Sstevel@tonic-gate 		    (ssize_t)0, fmode, -1, &done)) != 0) || done) {
82567c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
82577c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
82587c478bd9Sstevel@tonic-gate 			    "kstrputmsg out:stp %p out %d error %d",
82597c478bd9Sstevel@tonic-gate 			    stp, 0, error);
82607c478bd9Sstevel@tonic-gate 			freemsg(mctl);
82617c478bd9Sstevel@tonic-gate 			return (error);
82627c478bd9Sstevel@tonic-gate 		}
82637c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAKE,
82647c478bd9Sstevel@tonic-gate 		    "kstrputmsg wake:stp %p wakes", stp);
82659acbbeafSnn35248 		if ((error = i_straccess(stp, JCWRITE)) != 0) {
82667c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
82677c478bd9Sstevel@tonic-gate 			freemsg(mctl);
82687c478bd9Sstevel@tonic-gate 			return (error);
82697c478bd9Sstevel@tonic-gate 		}
82709acbbeafSnn35248 		mutex_exit(&stp->sd_lock);
82717c478bd9Sstevel@tonic-gate 	}
82727c478bd9Sstevel@tonic-gate out:
82737c478bd9Sstevel@tonic-gate 	freemsg(mctl);
82747c478bd9Sstevel@tonic-gate 	/*
82757c478bd9Sstevel@tonic-gate 	 * For historic reasons, applications expect EAGAIN
82767c478bd9Sstevel@tonic-gate 	 * when data mblk could not be allocated. so change
82777c478bd9Sstevel@tonic-gate 	 * ENOMEM back to EAGAIN
82787c478bd9Sstevel@tonic-gate 	 */
82797c478bd9Sstevel@tonic-gate 	if (error == ENOMEM)
82807c478bd9Sstevel@tonic-gate 		error = EAGAIN;
82817c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
82827c478bd9Sstevel@tonic-gate 	    "kstrputmsg out:stp %p out %d error %d", stp, 2, error);
82837c478bd9Sstevel@tonic-gate 	return (error);
82847c478bd9Sstevel@tonic-gate }
82857c478bd9Sstevel@tonic-gate 
82867c478bd9Sstevel@tonic-gate /*
82877c478bd9Sstevel@tonic-gate  * Determines whether the necessary conditions are set on a stream
82887c478bd9Sstevel@tonic-gate  * for it to be readable, writeable, or have exceptions.
82897c478bd9Sstevel@tonic-gate  *
82907c478bd9Sstevel@tonic-gate  * strpoll handles the consolidation private events:
82917c478bd9Sstevel@tonic-gate  *	POLLNOERR	Do not return POLLERR even if there are stream
82927c478bd9Sstevel@tonic-gate  *			head errors.
82937c478bd9Sstevel@tonic-gate  *			Used by sockfs.
82947c478bd9Sstevel@tonic-gate  *	POLLRDDATA	Do not return POLLIN unless at least one message on
82957c478bd9Sstevel@tonic-gate  *			the queue contains one or more M_DATA mblks. Thus
82967c478bd9Sstevel@tonic-gate  *			when this flag is set a queue with only
82977c478bd9Sstevel@tonic-gate  *			M_PROTO/M_PCPROTO mblks does not return POLLIN.
82987c478bd9Sstevel@tonic-gate  *			Used by sockfs to ignore T_EXDATA_IND messages.
82997c478bd9Sstevel@tonic-gate  *
83007c478bd9Sstevel@tonic-gate  * Note: POLLRDDATA assumes that synch streams only return messages with
83017c478bd9Sstevel@tonic-gate  * an M_DATA attached (i.e. not messages consisting of only
83027c478bd9Sstevel@tonic-gate  * an M_PROTO/M_PCPROTO part).
83037c478bd9Sstevel@tonic-gate  */
83047c478bd9Sstevel@tonic-gate int
83057c478bd9Sstevel@tonic-gate strpoll(
83067c478bd9Sstevel@tonic-gate 	struct stdata *stp,
83077c478bd9Sstevel@tonic-gate 	short events_arg,
83087c478bd9Sstevel@tonic-gate 	int anyyet,
83097c478bd9Sstevel@tonic-gate 	short *reventsp,
83107c478bd9Sstevel@tonic-gate 	struct pollhead **phpp)
83117c478bd9Sstevel@tonic-gate {
83127c478bd9Sstevel@tonic-gate 	int events = (ushort_t)events_arg;
83137c478bd9Sstevel@tonic-gate 	int retevents = 0;
83147c478bd9Sstevel@tonic-gate 	mblk_t *mp;
83157c478bd9Sstevel@tonic-gate 	qband_t *qbp;
83167c478bd9Sstevel@tonic-gate 	long sd_flags = stp->sd_flag;
83177c478bd9Sstevel@tonic-gate 	int headlocked = 0;
83187c478bd9Sstevel@tonic-gate 
83197c478bd9Sstevel@tonic-gate 	/*
83207c478bd9Sstevel@tonic-gate 	 * For performance, a single 'if' tests for most possible edge
83217c478bd9Sstevel@tonic-gate 	 * conditions in one shot
83227c478bd9Sstevel@tonic-gate 	 */
83237c478bd9Sstevel@tonic-gate 	if (sd_flags & (STPLEX | STRDERR | STWRERR)) {
83247c478bd9Sstevel@tonic-gate 		if (sd_flags & STPLEX) {
83257c478bd9Sstevel@tonic-gate 			*reventsp = POLLNVAL;
83267c478bd9Sstevel@tonic-gate 			return (EINVAL);
83277c478bd9Sstevel@tonic-gate 		}
83287c478bd9Sstevel@tonic-gate 		if (((events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) &&
83297c478bd9Sstevel@tonic-gate 		    (sd_flags & STRDERR)) ||
83307c478bd9Sstevel@tonic-gate 		    ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) &&
83317c478bd9Sstevel@tonic-gate 		    (sd_flags & STWRERR))) {
83327c478bd9Sstevel@tonic-gate 			if (!(events & POLLNOERR)) {
83337c478bd9Sstevel@tonic-gate 				*reventsp = POLLERR;
83347c478bd9Sstevel@tonic-gate 				return (0);
83357c478bd9Sstevel@tonic-gate 			}
83367c478bd9Sstevel@tonic-gate 		}
83377c478bd9Sstevel@tonic-gate 	}
83387c478bd9Sstevel@tonic-gate 	if (sd_flags & STRHUP) {
83397c478bd9Sstevel@tonic-gate 		retevents |= POLLHUP;
83407c478bd9Sstevel@tonic-gate 	} else if (events & (POLLWRNORM | POLLWRBAND)) {
83417c478bd9Sstevel@tonic-gate 		queue_t *tq;
83427c478bd9Sstevel@tonic-gate 		queue_t	*qp = stp->sd_wrq;
83437c478bd9Sstevel@tonic-gate 
83447c478bd9Sstevel@tonic-gate 		claimstr(qp);
83457c478bd9Sstevel@tonic-gate 		/* Find next module forward that has a service procedure */
83467c478bd9Sstevel@tonic-gate 		tq = qp->q_next->q_nfsrv;
83477c478bd9Sstevel@tonic-gate 		ASSERT(tq != NULL);
83487c478bd9Sstevel@tonic-gate 
83497c478bd9Sstevel@tonic-gate 		polllock(&stp->sd_pollist, QLOCK(tq));
83507c478bd9Sstevel@tonic-gate 		if (events & POLLWRNORM) {
83517c478bd9Sstevel@tonic-gate 			queue_t *sqp;
83527c478bd9Sstevel@tonic-gate 
83537c478bd9Sstevel@tonic-gate 			if (tq->q_flag & QFULL)
83547c478bd9Sstevel@tonic-gate 				/* ensure backq svc procedure runs */
83557c478bd9Sstevel@tonic-gate 				tq->q_flag |= QWANTW;
83567c478bd9Sstevel@tonic-gate 			else if ((sqp = stp->sd_struiowrq) != NULL) {
83577c478bd9Sstevel@tonic-gate 				/* Check sync stream barrier write q */
83587c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(tq));
83597c478bd9Sstevel@tonic-gate 				polllock(&stp->sd_pollist, QLOCK(sqp));
83607c478bd9Sstevel@tonic-gate 				if (sqp->q_flag & QFULL)
83617c478bd9Sstevel@tonic-gate 					/* ensure pollwakeup() is done */
83627c478bd9Sstevel@tonic-gate 					sqp->q_flag |= QWANTWSYNC;
83637c478bd9Sstevel@tonic-gate 				else
83647c478bd9Sstevel@tonic-gate 					retevents |= POLLOUT;
83657c478bd9Sstevel@tonic-gate 				/* More write events to process ??? */
83667c478bd9Sstevel@tonic-gate 				if (! (events & POLLWRBAND)) {
83677c478bd9Sstevel@tonic-gate 					mutex_exit(QLOCK(sqp));
83687c478bd9Sstevel@tonic-gate 					releasestr(qp);
83697c478bd9Sstevel@tonic-gate 					goto chkrd;
83707c478bd9Sstevel@tonic-gate 				}
83717c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(sqp));
83727c478bd9Sstevel@tonic-gate 				polllock(&stp->sd_pollist, QLOCK(tq));
83737c478bd9Sstevel@tonic-gate 			} else
83747c478bd9Sstevel@tonic-gate 				retevents |= POLLOUT;
83757c478bd9Sstevel@tonic-gate 		}
83767c478bd9Sstevel@tonic-gate 		if (events & POLLWRBAND) {
83777c478bd9Sstevel@tonic-gate 			qbp = tq->q_bandp;
83787c478bd9Sstevel@tonic-gate 			if (qbp) {
83797c478bd9Sstevel@tonic-gate 				while (qbp) {
83807c478bd9Sstevel@tonic-gate 					if (qbp->qb_flag & QB_FULL)
83817c478bd9Sstevel@tonic-gate 						qbp->qb_flag |= QB_WANTW;
83827c478bd9Sstevel@tonic-gate 					else
83837c478bd9Sstevel@tonic-gate 						retevents |= POLLWRBAND;
83847c478bd9Sstevel@tonic-gate 					qbp = qbp->qb_next;
83857c478bd9Sstevel@tonic-gate 				}
83867c478bd9Sstevel@tonic-gate 			} else {
83877c478bd9Sstevel@tonic-gate 				retevents |= POLLWRBAND;
83887c478bd9Sstevel@tonic-gate 			}
83897c478bd9Sstevel@tonic-gate 		}
83907c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(tq));
83917c478bd9Sstevel@tonic-gate 		releasestr(qp);
83927c478bd9Sstevel@tonic-gate 	}
83937c478bd9Sstevel@tonic-gate chkrd:
83947c478bd9Sstevel@tonic-gate 	if (sd_flags & STRPRI) {
83957c478bd9Sstevel@tonic-gate 		retevents |= (events & POLLPRI);
83967c478bd9Sstevel@tonic-gate 	} else if (events & (POLLRDNORM | POLLRDBAND | POLLIN)) {
83977c478bd9Sstevel@tonic-gate 		queue_t	*qp = _RD(stp->sd_wrq);
83987c478bd9Sstevel@tonic-gate 		int normevents = (events & (POLLIN | POLLRDNORM));
83997c478bd9Sstevel@tonic-gate 
84007c478bd9Sstevel@tonic-gate 		/*
84017c478bd9Sstevel@tonic-gate 		 * Note: Need to do polllock() here since ps_lock may be
84027c478bd9Sstevel@tonic-gate 		 * held. See bug 4191544.
84037c478bd9Sstevel@tonic-gate 		 */
84047c478bd9Sstevel@tonic-gate 		polllock(&stp->sd_pollist, &stp->sd_lock);
84057c478bd9Sstevel@tonic-gate 		headlocked = 1;
84067c478bd9Sstevel@tonic-gate 		mp = qp->q_first;
84077c478bd9Sstevel@tonic-gate 		while (mp) {
84087c478bd9Sstevel@tonic-gate 			/*
84097c478bd9Sstevel@tonic-gate 			 * For POLLRDDATA we scan b_cont and b_next until we
84107c478bd9Sstevel@tonic-gate 			 * find an M_DATA.
84117c478bd9Sstevel@tonic-gate 			 */
84127c478bd9Sstevel@tonic-gate 			if ((events & POLLRDDATA) &&
84137c478bd9Sstevel@tonic-gate 			    mp->b_datap->db_type != M_DATA) {
84147c478bd9Sstevel@tonic-gate 				mblk_t *nmp = mp->b_cont;
84157c478bd9Sstevel@tonic-gate 
84167c478bd9Sstevel@tonic-gate 				while (nmp != NULL &&
84177c478bd9Sstevel@tonic-gate 				    nmp->b_datap->db_type != M_DATA)
84187c478bd9Sstevel@tonic-gate 					nmp = nmp->b_cont;
84197c478bd9Sstevel@tonic-gate 				if (nmp == NULL) {
84207c478bd9Sstevel@tonic-gate 					mp = mp->b_next;
84217c478bd9Sstevel@tonic-gate 					continue;
84227c478bd9Sstevel@tonic-gate 				}
84237c478bd9Sstevel@tonic-gate 			}
84247c478bd9Sstevel@tonic-gate 			if (mp->b_band == 0)
84257c478bd9Sstevel@tonic-gate 				retevents |= normevents;
84267c478bd9Sstevel@tonic-gate 			else
84277c478bd9Sstevel@tonic-gate 				retevents |= (events & (POLLIN | POLLRDBAND));
84287c478bd9Sstevel@tonic-gate 			break;
84297c478bd9Sstevel@tonic-gate 		}
84307c478bd9Sstevel@tonic-gate 		if (! (retevents & normevents) &&
84317c478bd9Sstevel@tonic-gate 		    (stp->sd_wakeq & RSLEEP)) {
84327c478bd9Sstevel@tonic-gate 			/*
84337c478bd9Sstevel@tonic-gate 			 * Sync stream barrier read queue has data.
84347c478bd9Sstevel@tonic-gate 			 */
84357c478bd9Sstevel@tonic-gate 			retevents |= normevents;
84367c478bd9Sstevel@tonic-gate 		}
84377c478bd9Sstevel@tonic-gate 		/* Treat eof as normal data */
84387c478bd9Sstevel@tonic-gate 		if (sd_flags & STREOF)
84397c478bd9Sstevel@tonic-gate 			retevents |= normevents;
84407c478bd9Sstevel@tonic-gate 	}
84417c478bd9Sstevel@tonic-gate 
84427c478bd9Sstevel@tonic-gate 	*reventsp = (short)retevents;
84437c478bd9Sstevel@tonic-gate 	if (retevents) {
84447c478bd9Sstevel@tonic-gate 		if (headlocked)
84457c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
84467c478bd9Sstevel@tonic-gate 		return (0);
84477c478bd9Sstevel@tonic-gate 	}
84487c478bd9Sstevel@tonic-gate 
84497c478bd9Sstevel@tonic-gate 	/*
84507c478bd9Sstevel@tonic-gate 	 * If poll() has not found any events yet, set up event cell
84517c478bd9Sstevel@tonic-gate 	 * to wake up the poll if a requested event occurs on this
84527c478bd9Sstevel@tonic-gate 	 * stream.  Check for collisions with outstanding poll requests.
84537c478bd9Sstevel@tonic-gate 	 */
84547c478bd9Sstevel@tonic-gate 	if (!anyyet) {
84557c478bd9Sstevel@tonic-gate 		*phpp = &stp->sd_pollist;
84567c478bd9Sstevel@tonic-gate 		if (headlocked == 0) {
84577c478bd9Sstevel@tonic-gate 			polllock(&stp->sd_pollist, &stp->sd_lock);
84587c478bd9Sstevel@tonic-gate 			headlocked = 1;
84597c478bd9Sstevel@tonic-gate 		}
84607c478bd9Sstevel@tonic-gate 		stp->sd_rput_opt |= SR_POLLIN;
84617c478bd9Sstevel@tonic-gate 	}
84627c478bd9Sstevel@tonic-gate 	if (headlocked)
84637c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
84647c478bd9Sstevel@tonic-gate 	return (0);
84657c478bd9Sstevel@tonic-gate }
84667c478bd9Sstevel@tonic-gate 
84677c478bd9Sstevel@tonic-gate /*
84687c478bd9Sstevel@tonic-gate  * The purpose of putback() is to assure sleeping polls/reads
84697c478bd9Sstevel@tonic-gate  * are awakened when there are no new messages arriving at the,
84707c478bd9Sstevel@tonic-gate  * stream head, and a message is placed back on the read queue.
84717c478bd9Sstevel@tonic-gate  *
84727c478bd9Sstevel@tonic-gate  * sd_lock must be held when messages are placed back on stream
84737c478bd9Sstevel@tonic-gate  * head.  (getq() holds sd_lock when it removes messages from
84747c478bd9Sstevel@tonic-gate  * the queue)
84757c478bd9Sstevel@tonic-gate  */
84767c478bd9Sstevel@tonic-gate 
84777c478bd9Sstevel@tonic-gate static void
84787c478bd9Sstevel@tonic-gate putback(struct stdata *stp, queue_t *q, mblk_t *bp, int band)
84797c478bd9Sstevel@tonic-gate {
8480*301ce41fSja97890 	mblk_t	*qfirst;
84817c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
84823584396bSja97890 
8483*301ce41fSja97890 	/*
8484*301ce41fSja97890 	 * As a result of lock-step ordering around q_lock and sd_lock,
8485*301ce41fSja97890 	 * it's possible for function calls like putnext() and
8486*301ce41fSja97890 	 * canputnext() to get an inaccurate picture of how much
8487*301ce41fSja97890 	 * data is really being processed at the stream head.
8488*301ce41fSja97890 	 * We only consolidate with existing messages on the queue
8489*301ce41fSja97890 	 * if the length of the message we want to put back is smaller
8490*301ce41fSja97890 	 * than the queue hiwater mark.
8491*301ce41fSja97890 	 */
84923584396bSja97890 	if ((stp->sd_rput_opt & SR_CONSOL_DATA) &&
8493*301ce41fSja97890 	    (DB_TYPE(bp) == M_DATA) && ((qfirst = q->q_first) != NULL) &&
8494*301ce41fSja97890 	    (DB_TYPE(qfirst) == M_DATA) &&
8495*301ce41fSja97890 	    ((qfirst->b_flag & (MSGMARK|MSGDELIM)) == 0) &&
8496*301ce41fSja97890 	    ((bp->b_flag & (MSGMARK|MSGDELIM|MSGMARKNEXT)) == 0) &&
8497*301ce41fSja97890 	    (mp_cont_len(bp, NULL) < q->q_hiwat)) {
84983584396bSja97890 		/*
84993584396bSja97890 		 * We use the same logic as defined in strrput()
85003584396bSja97890 		 * but in reverse as we are putting back onto the
85013584396bSja97890 		 * queue and want to retain byte ordering.
8502*301ce41fSja97890 		 * Consolidate M_DATA messages with M_DATA ONLY.
8503*301ce41fSja97890 		 * strrput() allows the consolidation of M_DATA onto
8504*301ce41fSja97890 		 * M_PROTO | M_PCPROTO but not the other way round.
8505*301ce41fSja97890 		 *
85063584396bSja97890 		 * The consolidation does not take place if the message
85073584396bSja97890 		 * we are returning to the queue is marked with either
85083584396bSja97890 		 * of the marks or the delim flag or if q_first
85093584396bSja97890 		 * is marked with MSGMARK. The MSGMARK check is needed to
85103584396bSja97890 		 * handle the odd semantics of MSGMARK where essentially
85113584396bSja97890 		 * the whole message is to be treated as marked.
85123584396bSja97890 		 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from q_first
85133584396bSja97890 		 * to the front of the b_cont chain.
85143584396bSja97890 		 */
85153584396bSja97890 		rmvq_noenab(q, qfirst);
8516*301ce41fSja97890 
85173584396bSja97890 		/*
85183584396bSja97890 		 * The first message in the b_cont list
85193584396bSja97890 		 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
85203584396bSja97890 		 * We need to handle the case where we
85213584396bSja97890 		 * are appending:
85223584396bSja97890 		 *
85233584396bSja97890 		 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
85243584396bSja97890 		 * 2) a MSGMARKNEXT to a plain message.
85253584396bSja97890 		 * 3) a MSGNOTMARKNEXT to a plain message
85263584396bSja97890 		 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
85273584396bSja97890 		 *    message.
85283584396bSja97890 		 *
85293584396bSja97890 		 * Thus we never append a MSGMARKNEXT or
85303584396bSja97890 		 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
85313584396bSja97890 		 */
85323584396bSja97890 		if (qfirst->b_flag & MSGMARKNEXT) {
85333584396bSja97890 			bp->b_flag |= MSGMARKNEXT;
85343584396bSja97890 			bp->b_flag &= ~MSGNOTMARKNEXT;
85353584396bSja97890 			qfirst->b_flag &= ~MSGMARKNEXT;
85363584396bSja97890 		} else if (qfirst->b_flag & MSGNOTMARKNEXT) {
85373584396bSja97890 			bp->b_flag |= MSGNOTMARKNEXT;
85383584396bSja97890 			qfirst->b_flag &= ~MSGNOTMARKNEXT;
85393584396bSja97890 		}
85403584396bSja97890 
85413584396bSja97890 		linkb(bp, qfirst);
85423584396bSja97890 	}
85437c478bd9Sstevel@tonic-gate 	(void) putbq(q, bp);
85443584396bSja97890 
85457c478bd9Sstevel@tonic-gate 	/*
85467c478bd9Sstevel@tonic-gate 	 * A message may have come in when the sd_lock was dropped in the
85477c478bd9Sstevel@tonic-gate 	 * calling routine. If this is the case and STR*ATMARK info was
85487c478bd9Sstevel@tonic-gate 	 * received, need to move that from the stream head to the q_last
85497c478bd9Sstevel@tonic-gate 	 * so that SIOCATMARK can return the proper value.
85507c478bd9Sstevel@tonic-gate 	 */
85517c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STRATMARK | STRNOTATMARK)) {
85527c478bd9Sstevel@tonic-gate 		unsigned short *flagp = &q->q_last->b_flag;
85537c478bd9Sstevel@tonic-gate 		uint_t b_flag = (uint_t)*flagp;
85547c478bd9Sstevel@tonic-gate 
85557c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRATMARK) {
85567c478bd9Sstevel@tonic-gate 			b_flag &= ~MSGNOTMARKNEXT;
85577c478bd9Sstevel@tonic-gate 			b_flag |= MSGMARKNEXT;
85587c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRATMARK;
85597c478bd9Sstevel@tonic-gate 		} else {
85607c478bd9Sstevel@tonic-gate 			b_flag &= ~MSGMARKNEXT;
85617c478bd9Sstevel@tonic-gate 			b_flag |= MSGNOTMARKNEXT;
85627c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRNOTATMARK;
85637c478bd9Sstevel@tonic-gate 		}
85647c478bd9Sstevel@tonic-gate 		*flagp = (unsigned short) b_flag;
85657c478bd9Sstevel@tonic-gate 	}
85667c478bd9Sstevel@tonic-gate 
85677c478bd9Sstevel@tonic-gate #ifdef	DEBUG
85687c478bd9Sstevel@tonic-gate 	/*
85697c478bd9Sstevel@tonic-gate 	 * Make sure that the flags are not messed up.
85707c478bd9Sstevel@tonic-gate 	 */
85717c478bd9Sstevel@tonic-gate 	{
85727c478bd9Sstevel@tonic-gate 		mblk_t *mp;
85737c478bd9Sstevel@tonic-gate 		mp = q->q_last;
85747c478bd9Sstevel@tonic-gate 		while (mp != NULL) {
85757c478bd9Sstevel@tonic-gate 			ASSERT((mp->b_flag & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
85767c478bd9Sstevel@tonic-gate 			    (MSGMARKNEXT|MSGNOTMARKNEXT));
85777c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
85787c478bd9Sstevel@tonic-gate 		}
85797c478bd9Sstevel@tonic-gate 	}
85807c478bd9Sstevel@tonic-gate #endif
85817c478bd9Sstevel@tonic-gate 	if (q->q_first == bp) {
85827c478bd9Sstevel@tonic-gate 		short pollevents;
85837c478bd9Sstevel@tonic-gate 
85847c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & RSLEEP) {
85857c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~RSLEEP;
85867c478bd9Sstevel@tonic-gate 			cv_broadcast(&q->q_wait);
85877c478bd9Sstevel@tonic-gate 		}
85887c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRPRI) {
85897c478bd9Sstevel@tonic-gate 			pollevents = POLLPRI;
85907c478bd9Sstevel@tonic-gate 		} else {
85917c478bd9Sstevel@tonic-gate 			if (band == 0) {
85927c478bd9Sstevel@tonic-gate 				if (!(stp->sd_rput_opt & SR_POLLIN))
85937c478bd9Sstevel@tonic-gate 					return;
85947c478bd9Sstevel@tonic-gate 				stp->sd_rput_opt &= ~SR_POLLIN;
85957c478bd9Sstevel@tonic-gate 				pollevents = POLLIN | POLLRDNORM;
85967c478bd9Sstevel@tonic-gate 			} else {
85977c478bd9Sstevel@tonic-gate 				pollevents = POLLIN | POLLRDBAND;
85987c478bd9Sstevel@tonic-gate 			}
85997c478bd9Sstevel@tonic-gate 		}
86007c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
86017c478bd9Sstevel@tonic-gate 		pollwakeup(&stp->sd_pollist, pollevents);
86027c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
86037c478bd9Sstevel@tonic-gate 	}
86047c478bd9Sstevel@tonic-gate }
86057c478bd9Sstevel@tonic-gate 
86067c478bd9Sstevel@tonic-gate /*
86077c478bd9Sstevel@tonic-gate  * Return the held vnode attached to the stream head of a
86087c478bd9Sstevel@tonic-gate  * given queue
86097c478bd9Sstevel@tonic-gate  * It is the responsibility of the calling routine to ensure
86107c478bd9Sstevel@tonic-gate  * that the queue does not go away (e.g. pop).
86117c478bd9Sstevel@tonic-gate  */
86127c478bd9Sstevel@tonic-gate vnode_t *
86137c478bd9Sstevel@tonic-gate strq2vp(queue_t *qp)
86147c478bd9Sstevel@tonic-gate {
86157c478bd9Sstevel@tonic-gate 	vnode_t *vp;
86167c478bd9Sstevel@tonic-gate 	vp = STREAM(qp)->sd_vnode;
86177c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
86187c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
86197c478bd9Sstevel@tonic-gate 	return (vp);
86207c478bd9Sstevel@tonic-gate }
86217c478bd9Sstevel@tonic-gate 
86227c478bd9Sstevel@tonic-gate /*
86237c478bd9Sstevel@tonic-gate  * return the stream head write queue for the given vp
86247c478bd9Sstevel@tonic-gate  * It is the responsibility of the calling routine to ensure
86257c478bd9Sstevel@tonic-gate  * that the stream or vnode do not close.
86267c478bd9Sstevel@tonic-gate  */
86277c478bd9Sstevel@tonic-gate queue_t *
86287c478bd9Sstevel@tonic-gate strvp2wq(vnode_t *vp)
86297c478bd9Sstevel@tonic-gate {
86307c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream != NULL);
86317c478bd9Sstevel@tonic-gate 	return (vp->v_stream->sd_wrq);
86327c478bd9Sstevel@tonic-gate }
86337c478bd9Sstevel@tonic-gate 
86347c478bd9Sstevel@tonic-gate /*
86357c478bd9Sstevel@tonic-gate  * pollwakeup stream head
86367c478bd9Sstevel@tonic-gate  * It is the responsibility of the calling routine to ensure
86377c478bd9Sstevel@tonic-gate  * that the stream or vnode do not close.
86387c478bd9Sstevel@tonic-gate  */
86397c478bd9Sstevel@tonic-gate void
86407c478bd9Sstevel@tonic-gate strpollwakeup(vnode_t *vp, short event)
86417c478bd9Sstevel@tonic-gate {
86427c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
86437c478bd9Sstevel@tonic-gate 	pollwakeup(&vp->v_stream->sd_pollist, event);
86447c478bd9Sstevel@tonic-gate }
86457c478bd9Sstevel@tonic-gate 
86467c478bd9Sstevel@tonic-gate /*
86477c478bd9Sstevel@tonic-gate  * Mate the stream heads of two vnodes together. If the two vnodes are the
86487c478bd9Sstevel@tonic-gate  * same, we just make the write-side point at the read-side -- otherwise,
86497c478bd9Sstevel@tonic-gate  * we do a full mate.  Only works on vnodes associated with streams that are
86507c478bd9Sstevel@tonic-gate  * still being built and thus have only a stream head.
86517c478bd9Sstevel@tonic-gate  */
86527c478bd9Sstevel@tonic-gate void
86537c478bd9Sstevel@tonic-gate strmate(vnode_t *vp1, vnode_t *vp2)
86547c478bd9Sstevel@tonic-gate {
86557c478bd9Sstevel@tonic-gate 	queue_t *wrq1 = strvp2wq(vp1);
86567c478bd9Sstevel@tonic-gate 	queue_t *wrq2 = strvp2wq(vp2);
86577c478bd9Sstevel@tonic-gate 
86587c478bd9Sstevel@tonic-gate 	/*
86597c478bd9Sstevel@tonic-gate 	 * Verify that there are no modules on the stream yet.  We also
86607c478bd9Sstevel@tonic-gate 	 * rely on the stream head always having a service procedure to
86617c478bd9Sstevel@tonic-gate 	 * avoid tweaking q_nfsrv.
86627c478bd9Sstevel@tonic-gate 	 */
86637c478bd9Sstevel@tonic-gate 	ASSERT(wrq1->q_next == NULL && wrq2->q_next == NULL);
86647c478bd9Sstevel@tonic-gate 	ASSERT(wrq1->q_qinfo->qi_srvp != NULL);
86657c478bd9Sstevel@tonic-gate 	ASSERT(wrq2->q_qinfo->qi_srvp != NULL);
86667c478bd9Sstevel@tonic-gate 
86677c478bd9Sstevel@tonic-gate 	/*
86687c478bd9Sstevel@tonic-gate 	 * If the queues are the same, just twist; otherwise do a full mate.
86697c478bd9Sstevel@tonic-gate 	 */
86707c478bd9Sstevel@tonic-gate 	if (wrq1 == wrq2) {
86717c478bd9Sstevel@tonic-gate 		wrq1->q_next = _RD(wrq1);
86727c478bd9Sstevel@tonic-gate 	} else {
86737c478bd9Sstevel@tonic-gate 		wrq1->q_next = _RD(wrq2);
86747c478bd9Sstevel@tonic-gate 		wrq2->q_next = _RD(wrq1);
86757c478bd9Sstevel@tonic-gate 		STREAM(wrq1)->sd_mate = STREAM(wrq2);
86767c478bd9Sstevel@tonic-gate 		STREAM(wrq1)->sd_flag |= STRMATE;
86777c478bd9Sstevel@tonic-gate 		STREAM(wrq2)->sd_mate = STREAM(wrq1);
86787c478bd9Sstevel@tonic-gate 		STREAM(wrq2)->sd_flag |= STRMATE;
86797c478bd9Sstevel@tonic-gate 	}
86807c478bd9Sstevel@tonic-gate }
86817c478bd9Sstevel@tonic-gate 
86827c478bd9Sstevel@tonic-gate /*
86837c478bd9Sstevel@tonic-gate  * XXX will go away when console is correctly fixed.
86847c478bd9Sstevel@tonic-gate  * Clean up the console PIDS, from previous I_SETSIG,
86857c478bd9Sstevel@tonic-gate  * called only for cnopen which never calls strclean().
86867c478bd9Sstevel@tonic-gate  */
86877c478bd9Sstevel@tonic-gate void
86887c478bd9Sstevel@tonic-gate str_cn_clean(struct vnode *vp)
86897c478bd9Sstevel@tonic-gate {
86907c478bd9Sstevel@tonic-gate 	strsig_t *ssp, *pssp, *tssp;
86917c478bd9Sstevel@tonic-gate 	struct stdata *stp;
86927c478bd9Sstevel@tonic-gate 	struct pid  *pidp;
86937c478bd9Sstevel@tonic-gate 	int update = 0;
86947c478bd9Sstevel@tonic-gate 
86957c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
86967c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
86977c478bd9Sstevel@tonic-gate 	pssp = NULL;
86987c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
86997c478bd9Sstevel@tonic-gate 	ssp = stp->sd_siglist;
87007c478bd9Sstevel@tonic-gate 	while (ssp) {
87017c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
87027c478bd9Sstevel@tonic-gate 		pidp = ssp->ss_pidp;
87037c478bd9Sstevel@tonic-gate 		/*
87047c478bd9Sstevel@tonic-gate 		 * Get rid of PID if the proc is gone.
87057c478bd9Sstevel@tonic-gate 		 */
87067c478bd9Sstevel@tonic-gate 		if (pidp->pid_prinactive) {
87077c478bd9Sstevel@tonic-gate 			tssp = ssp->ss_next;
87087c478bd9Sstevel@tonic-gate 			if (pssp)
87097c478bd9Sstevel@tonic-gate 				pssp->ss_next = tssp;
87107c478bd9Sstevel@tonic-gate 			else
87117c478bd9Sstevel@tonic-gate 				stp->sd_siglist = tssp;
87127c478bd9Sstevel@tonic-gate 			ASSERT(pidp->pid_ref <= 1);
87137c478bd9Sstevel@tonic-gate 			PID_RELE(ssp->ss_pidp);
87147c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
87157c478bd9Sstevel@tonic-gate 			kmem_free(ssp, sizeof (strsig_t));
87167c478bd9Sstevel@tonic-gate 			update = 1;
87177c478bd9Sstevel@tonic-gate 			ssp = tssp;
87187c478bd9Sstevel@tonic-gate 			continue;
87197c478bd9Sstevel@tonic-gate 		} else
87207c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
87217c478bd9Sstevel@tonic-gate 		pssp = ssp;
87227c478bd9Sstevel@tonic-gate 		ssp = ssp->ss_next;
87237c478bd9Sstevel@tonic-gate 	}
87247c478bd9Sstevel@tonic-gate 	if (update) {
87257c478bd9Sstevel@tonic-gate 		stp->sd_sigflags = 0;
87267c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
87277c478bd9Sstevel@tonic-gate 			stp->sd_sigflags |= ssp->ss_events;
87287c478bd9Sstevel@tonic-gate 	}
87297c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
87307c478bd9Sstevel@tonic-gate }
87317c478bd9Sstevel@tonic-gate 
87327c478bd9Sstevel@tonic-gate /*
87337c478bd9Sstevel@tonic-gate  * Return B_TRUE if there is data in the message, B_FALSE otherwise.
87347c478bd9Sstevel@tonic-gate  */
87357c478bd9Sstevel@tonic-gate static boolean_t
87367c478bd9Sstevel@tonic-gate msghasdata(mblk_t *bp)
87377c478bd9Sstevel@tonic-gate {
87387c478bd9Sstevel@tonic-gate 	for (; bp; bp = bp->b_cont)
87397c478bd9Sstevel@tonic-gate 		if (bp->b_datap->db_type == M_DATA) {
87407c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_wptr >= bp->b_rptr);
87417c478bd9Sstevel@tonic-gate 			if (bp->b_wptr > bp->b_rptr)
87427c478bd9Sstevel@tonic-gate 				return (B_TRUE);
87437c478bd9Sstevel@tonic-gate 		}
87447c478bd9Sstevel@tonic-gate 	return (B_FALSE);
87457c478bd9Sstevel@tonic-gate }
874617169044Sbrutus 
874717169044Sbrutus /*
874817169044Sbrutus  * Called on the first strget() of a sodirect/uioa enabled streamhead,
874917169044Sbrutus  * if any mblk_t(s) enqueued they must first be uioamove()d before uioa
875017169044Sbrutus  * can be enabled for the underlying transport's use.
875117169044Sbrutus  */
875217169044Sbrutus void
875317169044Sbrutus struioainit(queue_t *q, sodirect_t *sodp, uio_t *uiop)
875417169044Sbrutus {
875517169044Sbrutus 	uioa_t	*uioap = (uioa_t *)uiop;
875617169044Sbrutus 	mblk_t	*bp = q->q_first;
875717169044Sbrutus 	mblk_t	*lbp = NULL;
875817169044Sbrutus 	mblk_t	*nbp, *wbp;
875917169044Sbrutus 	int	len;
876017169044Sbrutus 	int	error;
876117169044Sbrutus 
876217169044Sbrutus 	ASSERT(MUTEX_HELD(sodp->sod_lock));
876317169044Sbrutus 	ASSERT(&sodp->sod_uioa == uioap);
876417169044Sbrutus 
876517169044Sbrutus 	/*
876617169044Sbrutus 	 * Walk the b_next/b_prev doubly linked list of b_cont chain(s)
876717169044Sbrutus 	 * and schedule any M_DATA mblk_t's for uio asynchronous move.
876817169044Sbrutus 	 */
876917169044Sbrutus 	do {
877017169044Sbrutus 		/* Next mblk_t chain */
877117169044Sbrutus 		nbp = bp->b_next;
877217169044Sbrutus 		/* Walk the chain */
877317169044Sbrutus 		wbp = bp;
877417169044Sbrutus 		do {
877517169044Sbrutus 			if (wbp->b_datap->db_type != M_DATA) {
877617169044Sbrutus 				/* Not M_DATA, no more uioa */
877717169044Sbrutus 				goto nouioa;
877817169044Sbrutus 			}
877917169044Sbrutus 			if ((len = wbp->b_wptr - wbp->b_rptr) > 0) {
878017169044Sbrutus 				/* Have a M_DATA mblk_t with data */
878117169044Sbrutus 				if (len > uioap->uio_resid) {
878217169044Sbrutus 					/* Not enough uio sapce */
878317169044Sbrutus 					goto nouioa;
878417169044Sbrutus 				}
878517169044Sbrutus 				error = uioamove(wbp->b_rptr, len,
878617169044Sbrutus 				    UIO_READ, uioap);
878717169044Sbrutus 				if (!error) {
878817169044Sbrutus 					/* Scheduled, mark dblk_t as such */
878917169044Sbrutus 					wbp->b_datap->db_flags |= DBLK_UIOA;
879017169044Sbrutus 				} else {
879117169044Sbrutus 					/* Error of some sort, no more uioa */
879217169044Sbrutus 					uioap->uioa_state &= UIOA_CLR;
879317169044Sbrutus 					uioap->uioa_state |= UIOA_FINI;
879417169044Sbrutus 					return;
879517169044Sbrutus 				}
879617169044Sbrutus 			}
879717169044Sbrutus 			/* Save last wbp processed */
879817169044Sbrutus 			lbp = wbp;
879917169044Sbrutus 		} while ((wbp = wbp->b_cont) != NULL);
880017169044Sbrutus 	} while ((bp = nbp) != NULL);
880117169044Sbrutus 
880217169044Sbrutus 	return;
880317169044Sbrutus 
880417169044Sbrutus nouioa:
880517169044Sbrutus 	/* No more uioa */
880617169044Sbrutus 	uioap->uioa_state &= UIOA_CLR;
880717169044Sbrutus 	uioap->uioa_state |= UIOA_FINI;
880817169044Sbrutus 
880917169044Sbrutus 	/*
881017169044Sbrutus 	 * If we processed 1 or more mblk_t(s) then we need to split the
881117169044Sbrutus 	 * current mblk_t chain in 2 so that all the uioamove()ed mblk_t(s)
881217169044Sbrutus 	 * are in the current chain and the rest are in the following new
881317169044Sbrutus 	 * chain.
881417169044Sbrutus 	 */
881517169044Sbrutus 	if (lbp != NULL) {
881617169044Sbrutus 		/* New end of current chain */
881717169044Sbrutus 		lbp->b_cont = NULL;
881817169044Sbrutus 
881917169044Sbrutus 		/* Insert new chain wbp after bp */
882017169044Sbrutus 		if ((wbp->b_next = nbp) != NULL)
882117169044Sbrutus 			nbp->b_prev = wbp;
882217169044Sbrutus 		else
882317169044Sbrutus 			q->q_last = wbp;
882417169044Sbrutus 		wbp->b_prev = bp;
882517169044Sbrutus 		bp->b_next = wbp;
882617169044Sbrutus 	}
882717169044Sbrutus }
8828