xref: /titanic_53/usr/src/uts/common/os/streamio.c (revision c28749e97052f09388969427adf7df641cdcdc22)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/signal.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/proc.h>
407c478bd9Sstevel@tonic-gate #include <sys/cred.h>
417c478bd9Sstevel@tonic-gate #include <sys/user.h>
427c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <sys/stream.h>
457c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
467c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
477c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
487c478bd9Sstevel@tonic-gate #include <sys/var.h>
497c478bd9Sstevel@tonic-gate #include <sys/poll.h>
507c478bd9Sstevel@tonic-gate #include <sys/termio.h>
517c478bd9Sstevel@tonic-gate #include <sys/ttold.h>
527c478bd9Sstevel@tonic-gate #include <sys/systm.h>
537c478bd9Sstevel@tonic-gate #include <sys/uio.h>
547c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
557c478bd9Sstevel@tonic-gate #include <sys/sad.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>
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * what is mblk_pull_len?
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  * If a streams message consists of many short messages,
857c478bd9Sstevel@tonic-gate  * a performance degradation occurs from copyout overhead.
867c478bd9Sstevel@tonic-gate  * To decrease the per mblk overhead, messages that are
877c478bd9Sstevel@tonic-gate  * likely to consist of many small mblks are pulled up into
887c478bd9Sstevel@tonic-gate  * one continuous chunk of memory.
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  * To avoid the processing overhead of examining every
917c478bd9Sstevel@tonic-gate  * mblk, a quick heuristic is used. If the first mblk in
927c478bd9Sstevel@tonic-gate  * the message is shorter than mblk_pull_len, it is likely
937c478bd9Sstevel@tonic-gate  * that the rest of the mblk will be short.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * This heuristic was decided upon after performance tests
967c478bd9Sstevel@tonic-gate  * indicated that anything more complex slowed down the main
977c478bd9Sstevel@tonic-gate  * code path.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate #define	MBLK_PULL_LEN 64
1007c478bd9Sstevel@tonic-gate uint32_t mblk_pull_len = MBLK_PULL_LEN;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * The sgttyb_handling flag controls the handling of the old BSD
1047c478bd9Sstevel@tonic-gate  * TIOCGETP, TIOCSETP, and TIOCSETN ioctls as follows:
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * 0 - Emit no warnings at all and retain old, broken behavior.
1077c478bd9Sstevel@tonic-gate  * 1 - Emit no warnings and silently handle new semantics.
1087c478bd9Sstevel@tonic-gate  * 2 - Send cmn_err(CE_NOTE) when either TIOCSETP or TIOCSETN is used
1097c478bd9Sstevel@tonic-gate  *     (once per system invocation).  Handle with new semantics.
1107c478bd9Sstevel@tonic-gate  * 3 - Send SIGSYS when any TIOCGETP, TIOCSETP, or TIOCSETN call is
1117c478bd9Sstevel@tonic-gate  *     made (so that offenders drop core and are easy to debug).
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * The "new semantics" are that TIOCGETP returns B38400 for
1147c478bd9Sstevel@tonic-gate  * sg_[io]speed if the corresponding value is over B38400, and that
1157c478bd9Sstevel@tonic-gate  * TIOCSET[PN] accept B38400 in these cases to mean "retain current
1167c478bd9Sstevel@tonic-gate  * bit rate."
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate int sgttyb_handling = 1;
1197c478bd9Sstevel@tonic-gate static boolean_t sgttyb_complaint;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /* don't push drcompat module by default on Style-2 streams */
1227c478bd9Sstevel@tonic-gate static int push_drcompat = 0;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * id value used to distinguish between different ioctl messages
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate static uint32_t ioc_id;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static void putback(struct stdata *, queue_t *, mblk_t *, int);
1307c478bd9Sstevel@tonic-gate static void strcleanall(struct vnode *);
1317c478bd9Sstevel@tonic-gate static int strwsrv(queue_t *);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * qinit and module_info structures for stream head read and write queues
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate struct module_info strm_info = { 0, "strrhead", 0, INFPSZ, STRHIGH, STRLOW };
1377c478bd9Sstevel@tonic-gate struct module_info stwm_info = { 0, "strwhead", 0, 0, 0, 0 };
1387c478bd9Sstevel@tonic-gate struct qinit strdata = { strrput, NULL, NULL, NULL, NULL, &strm_info };
1397c478bd9Sstevel@tonic-gate struct qinit stwdata = { NULL, strwsrv, NULL, NULL, NULL, &stwm_info };
1407c478bd9Sstevel@tonic-gate struct module_info fiform_info = { 0, "fifostrrhead", 0, PIPE_BUF, FIFOHIWAT,
1417c478bd9Sstevel@tonic-gate     FIFOLOWAT };
1427c478bd9Sstevel@tonic-gate struct module_info fifowm_info = { 0, "fifostrwhead", 0, 0, 0, 0 };
1437c478bd9Sstevel@tonic-gate struct qinit fifo_strdata = { strrput, NULL, NULL, NULL, NULL, &fiform_info };
1447c478bd9Sstevel@tonic-gate struct qinit fifo_stwdata = { NULL, strwsrv, NULL, NULL, NULL, &fifowm_info };
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate extern kmutex_t	strresources;	/* protects global resources */
1477c478bd9Sstevel@tonic-gate extern kmutex_t muxifier;	/* single-threads multiplexor creation */
1487c478bd9Sstevel@tonic-gate kmutex_t sad_lock;		/* protects sad drivers autopush */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static boolean_t msghasdata(mblk_t *bp);
1517c478bd9Sstevel@tonic-gate #define	msgnodata(bp) (!msghasdata(bp))
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Stream head locking notes:
1557c478bd9Sstevel@tonic-gate  *	There are four monitors associated with the stream head:
1567c478bd9Sstevel@tonic-gate  *	1. v_stream monitor: in stropen() and strclose() v_lock
1577c478bd9Sstevel@tonic-gate  *		is held while the association of vnode and stream
1587c478bd9Sstevel@tonic-gate  *		head is established or tested for.
1597c478bd9Sstevel@tonic-gate  *	2. open/close/push/pop monitor: sd_lock is held while each
1607c478bd9Sstevel@tonic-gate  *		thread bids for exclusive access to this monitor
1617c478bd9Sstevel@tonic-gate  *		for opening or closing a stream.  In addition, this
1627c478bd9Sstevel@tonic-gate  *		monitor is entered during pushes and pops.  This
1637c478bd9Sstevel@tonic-gate  *		guarantees that during plumbing operations there
1647c478bd9Sstevel@tonic-gate  *		is only one thread trying to change the plumbing.
1657c478bd9Sstevel@tonic-gate  *		Any other threads present in the stream are only
1667c478bd9Sstevel@tonic-gate  *		using the plumbing.
1677c478bd9Sstevel@tonic-gate  *	3. read/write monitor: in the case of read, a thread holds
1687c478bd9Sstevel@tonic-gate  *		sd_lock while trying to get data from the stream
1697c478bd9Sstevel@tonic-gate  *		head queue.  if there is none to fulfill a read
1707c478bd9Sstevel@tonic-gate  *		request, it sets RSLEEP and calls cv_wait_sig() down
1717c478bd9Sstevel@tonic-gate  *		in strwaitq() to await the arrival of new data.
1727c478bd9Sstevel@tonic-gate  *		when new data arrives in strrput(), sd_lock is acquired
1737c478bd9Sstevel@tonic-gate  *		before testing for RSLEEP and calling cv_broadcast().
1747c478bd9Sstevel@tonic-gate  *		the behavior of strwrite(), strwsrv(), and WSLEEP
1757c478bd9Sstevel@tonic-gate  *		mirror this.
1767c478bd9Sstevel@tonic-gate  *	4. ioctl monitor: sd_lock is gotten to ensure that only one
1777c478bd9Sstevel@tonic-gate  *		thread is doing an ioctl at a time.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static int
1817c478bd9Sstevel@tonic-gate push_mod(queue_t *qp, dev_t *devp, struct stdata *stp, const char *name,
1827c478bd9Sstevel@tonic-gate     int anchor, cred_t *crp)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	int error;
1857c478bd9Sstevel@tonic-gate 	fmodsw_impl_t *fp;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STRHUP|STRDERR|STWRERR)) {
1887c478bd9Sstevel@tonic-gate 		error = (stp->sd_flag & STRHUP) ? ENXIO : EIO;
1897c478bd9Sstevel@tonic-gate 		return (error);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	if (stp->sd_pushcnt >= nstrpush) {
1927c478bd9Sstevel@tonic-gate 		return (EINVAL);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if ((fp = fmodsw_find(name, FMODSW_HOLD | FMODSW_LOAD)) == NULL) {
1967c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STREOPENFAIL;
1977c478bd9Sstevel@tonic-gate 		return (EINVAL);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/*
2017c478bd9Sstevel@tonic-gate 	 * push new module and call its open routine via qattach
2027c478bd9Sstevel@tonic-gate 	 */
2037c478bd9Sstevel@tonic-gate 	if ((error = qattach(qp, devp, 0, crp, fp, B_FALSE)) != 0)
2047c478bd9Sstevel@tonic-gate 		return (error);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * Check to see if caller wants a STREAMS anchor
2087c478bd9Sstevel@tonic-gate 	 * put at this place in the stream, and add if so.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
2117c478bd9Sstevel@tonic-gate 	if (anchor == stp->sd_pushcnt)
2127c478bd9Sstevel@tonic-gate 		stp->sd_anchor = stp->sd_pushcnt;
2137c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	return (0);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Open a stream device.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate int
2227c478bd9Sstevel@tonic-gate stropen(vnode_t *vp, dev_t *devp, int flag, cred_t *crp)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	struct stdata *stp;
2257c478bd9Sstevel@tonic-gate 	queue_t *qp;
2267c478bd9Sstevel@tonic-gate 	int s;
2277c478bd9Sstevel@tonic-gate 	dev_t dummydev;
2287c478bd9Sstevel@tonic-gate 	struct autopush *ap;
2297c478bd9Sstevel@tonic-gate 	int error = 0;
2307c478bd9Sstevel@tonic-gate 	ssize_t	rmin, rmax;
2317c478bd9Sstevel@tonic-gate 	int cloneopen;
2327c478bd9Sstevel@tonic-gate 	queue_t *brq;
2337c478bd9Sstevel@tonic-gate 	major_t major;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
2367c478bd9Sstevel@tonic-gate 	if (audit_active)
2377c478bd9Sstevel@tonic-gate 		audit_stropen(vp, devp, flag, crp);
2387c478bd9Sstevel@tonic-gate #endif
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * If the stream already exists, wait for any open in progress
2427c478bd9Sstevel@tonic-gate 	 * to complete, then call the open function of each module and
2437c478bd9Sstevel@tonic-gate 	 * driver in the stream.  Otherwise create the stream.
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_STROPEN, "stropen:%p", vp);
2467c478bd9Sstevel@tonic-gate retry:
2477c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
2487c478bd9Sstevel@tonic-gate 	if ((stp = vp->v_stream) != NULL) {
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		/*
2517c478bd9Sstevel@tonic-gate 		 * Waiting for stream to be created to device
2527c478bd9Sstevel@tonic-gate 		 * due to another open.
2537c478bd9Sstevel@tonic-gate 		 */
2547c478bd9Sstevel@tonic-gate 	    mutex_exit(&vp->v_lock);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	    if (STRMATED(stp)) {
2577c478bd9Sstevel@tonic-gate 		struct stdata *strmatep = stp->sd_mate;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		STRLOCKMATES(stp);
2607c478bd9Sstevel@tonic-gate 		if (strmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
2617c478bd9Sstevel@tonic-gate 			if (flag & (FNDELAY|FNONBLOCK)) {
2627c478bd9Sstevel@tonic-gate 				error = EAGAIN;
2637c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
2647c478bd9Sstevel@tonic-gate 				goto ckreturn;
2657c478bd9Sstevel@tonic-gate 			}
2667c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
2677c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&strmatep->sd_monitor,
2687c478bd9Sstevel@tonic-gate 			    &strmatep->sd_lock)) {
2697c478bd9Sstevel@tonic-gate 				error = EINTR;
2707c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
2717c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
2727c478bd9Sstevel@tonic-gate 				goto ckreturn;
2737c478bd9Sstevel@tonic-gate 			}
2747c478bd9Sstevel@tonic-gate 			mutex_exit(&strmatep->sd_lock);
2757c478bd9Sstevel@tonic-gate 			goto retry;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
2787c478bd9Sstevel@tonic-gate 			if (flag & (FNDELAY|FNONBLOCK)) {
2797c478bd9Sstevel@tonic-gate 				error = EAGAIN;
2807c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
2817c478bd9Sstevel@tonic-gate 				goto ckreturn;
2827c478bd9Sstevel@tonic-gate 			}
2837c478bd9Sstevel@tonic-gate 			mutex_exit(&strmatep->sd_lock);
2847c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&stp->sd_monitor, &stp->sd_lock)) {
2857c478bd9Sstevel@tonic-gate 				error = EINTR;
2867c478bd9Sstevel@tonic-gate 				goto ckreturn;
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
2897c478bd9Sstevel@tonic-gate 			goto retry;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STRDERR|STWRERR)) {
2937c478bd9Sstevel@tonic-gate 			error = EIO;
2947c478bd9Sstevel@tonic-gate 			mutex_exit(&strmatep->sd_lock);
2957c478bd9Sstevel@tonic-gate 			goto ckreturn;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STWOPEN;
2997c478bd9Sstevel@tonic-gate 		STRUNLOCKMATES(stp);
3007c478bd9Sstevel@tonic-gate 	    } else {
3017c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
3027c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
3037c478bd9Sstevel@tonic-gate 			if (flag & (FNDELAY|FNONBLOCK)) {
3047c478bd9Sstevel@tonic-gate 				error = EAGAIN;
3057c478bd9Sstevel@tonic-gate 				goto ckreturn;
3067c478bd9Sstevel@tonic-gate 			}
3077c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&stp->sd_monitor, &stp->sd_lock)) {
3087c478bd9Sstevel@tonic-gate 				error = EINTR;
3097c478bd9Sstevel@tonic-gate 				goto ckreturn;
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
3127c478bd9Sstevel@tonic-gate 			goto retry;  /* could be clone! */
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STRDERR|STWRERR)) {
3167c478bd9Sstevel@tonic-gate 			error = EIO;
3177c478bd9Sstevel@tonic-gate 			goto ckreturn;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STWOPEN;
3217c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
3227c478bd9Sstevel@tonic-gate 	    }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		/*
3257c478bd9Sstevel@tonic-gate 		 * Open all modules and devices down stream to notify
3267c478bd9Sstevel@tonic-gate 		 * that another user is streaming.  For modules, set the
3277c478bd9Sstevel@tonic-gate 		 * last argument to MODOPEN and do not pass any open flags.
3287c478bd9Sstevel@tonic-gate 		 * Ignore dummydev since this is not the first open.
3297c478bd9Sstevel@tonic-gate 		 */
3307c478bd9Sstevel@tonic-gate 	    claimstr(stp->sd_wrq);
3317c478bd9Sstevel@tonic-gate 	    qp = stp->sd_wrq;
3327c478bd9Sstevel@tonic-gate 	    while (_SAMESTR(qp)) {
3337c478bd9Sstevel@tonic-gate 		qp = qp->q_next;
3347c478bd9Sstevel@tonic-gate 		if ((error = qreopen(_RD(qp), devp, flag, crp)) != 0)
3357c478bd9Sstevel@tonic-gate 			break;
3367c478bd9Sstevel@tonic-gate 	    }
3377c478bd9Sstevel@tonic-gate 	    releasestr(stp->sd_wrq);
3387c478bd9Sstevel@tonic-gate 	    mutex_enter(&stp->sd_lock);
3397c478bd9Sstevel@tonic-gate 	    stp->sd_flag &= ~(STRHUP|STWOPEN|STRDERR|STWRERR);
3407c478bd9Sstevel@tonic-gate 	    stp->sd_rerror = 0;
3417c478bd9Sstevel@tonic-gate 	    stp->sd_werror = 0;
3427c478bd9Sstevel@tonic-gate ckreturn:
3437c478bd9Sstevel@tonic-gate 	    cv_broadcast(&stp->sd_monitor);
3447c478bd9Sstevel@tonic-gate 	    mutex_exit(&stp->sd_lock);
3457c478bd9Sstevel@tonic-gate 	    return (error);
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * This vnode isn't streaming.  SPECFS already
3507c478bd9Sstevel@tonic-gate 	 * checked for multiple vnodes pointing to the
3517c478bd9Sstevel@tonic-gate 	 * same stream, so create a stream to the driver.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	qp = allocq();
3547c478bd9Sstevel@tonic-gate 	stp = shalloc(qp);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/*
3577c478bd9Sstevel@tonic-gate 	 * Initialize stream head.  shalloc() has given us
3587c478bd9Sstevel@tonic-gate 	 * exclusive access, and we have the vnode locked;
3597c478bd9Sstevel@tonic-gate 	 * we can do whatever we want with stp.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	stp->sd_flag = STWOPEN;
3627c478bd9Sstevel@tonic-gate 	stp->sd_siglist = NULL;
3637c478bd9Sstevel@tonic-gate 	stp->sd_pollist.ph_list = NULL;
3647c478bd9Sstevel@tonic-gate 	stp->sd_sigflags = 0;
3657c478bd9Sstevel@tonic-gate 	stp->sd_mark = NULL;
3667c478bd9Sstevel@tonic-gate 	stp->sd_closetime = STRTIMOUT;
3677c478bd9Sstevel@tonic-gate 	stp->sd_sidp = NULL;
3687c478bd9Sstevel@tonic-gate 	stp->sd_pgidp = NULL;
3697c478bd9Sstevel@tonic-gate 	stp->sd_vnode = vp;
3707c478bd9Sstevel@tonic-gate 	stp->sd_rerror = 0;
3717c478bd9Sstevel@tonic-gate 	stp->sd_werror = 0;
3727c478bd9Sstevel@tonic-gate 	stp->sd_wroff = 0;
373*c28749e9Skais 	stp->sd_tail = 0;
3747c478bd9Sstevel@tonic-gate 	stp->sd_iocblk = NULL;
3757c478bd9Sstevel@tonic-gate 	stp->sd_pushcnt = 0;
3767c478bd9Sstevel@tonic-gate 	stp->sd_qn_minpsz = 0;
3777c478bd9Sstevel@tonic-gate 	stp->sd_qn_maxpsz = INFPSZ - 1;	/* used to check for initialization */
3787c478bd9Sstevel@tonic-gate 	stp->sd_maxblk = INFPSZ;
3797c478bd9Sstevel@tonic-gate 	qp->q_ptr = _WR(qp)->q_ptr = stp;
3807c478bd9Sstevel@tonic-gate 	STREAM(qp) = STREAM(_WR(qp)) = stp;
3817c478bd9Sstevel@tonic-gate 	vp->v_stream = stp;
3827c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
3837c478bd9Sstevel@tonic-gate 	if (vp->v_type == VFIFO) {
3847c478bd9Sstevel@tonic-gate 		stp->sd_flag |= OLDNDELAY;
3857c478bd9Sstevel@tonic-gate 		/*
3867c478bd9Sstevel@tonic-gate 		 * This means, both for pipes and fifos
3877c478bd9Sstevel@tonic-gate 		 * strwrite will send SIGPIPE if the other
3887c478bd9Sstevel@tonic-gate 		 * end is closed. For putmsg it depends
3897c478bd9Sstevel@tonic-gate 		 * on whether it is a XPG4_2 application
3907c478bd9Sstevel@tonic-gate 		 * or not
3917c478bd9Sstevel@tonic-gate 		 */
3927c478bd9Sstevel@tonic-gate 		stp->sd_wput_opt = SW_SIGPIPE;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		/* setq might sleep in kmem_alloc - avoid holding locks. */
3957c478bd9Sstevel@tonic-gate 		setq(qp, &fifo_strdata, &fifo_stwdata, NULL, QMTSAFE,
3967c478bd9Sstevel@tonic-gate 		    SQ_CI|SQ_CO, B_FALSE);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		set_qend(qp);
399ad1660d0Smeem 		stp->sd_strtab = fifo_getinfo();
4007c478bd9Sstevel@tonic-gate 		_WR(qp)->q_nfsrv = _WR(qp);
4017c478bd9Sstevel@tonic-gate 		qp->q_nfsrv = qp;
4027c478bd9Sstevel@tonic-gate 		/*
4037c478bd9Sstevel@tonic-gate 		 * Wake up others that are waiting for stream to be created.
4047c478bd9Sstevel@tonic-gate 		 */
4057c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
4067c478bd9Sstevel@tonic-gate 		/*
4077c478bd9Sstevel@tonic-gate 		 * nothing is be pushed on stream yet, so
4087c478bd9Sstevel@tonic-gate 		 * optimized stream head packetsizes are just that
4097c478bd9Sstevel@tonic-gate 		 * of the read queue
4107c478bd9Sstevel@tonic-gate 		 */
4117c478bd9Sstevel@tonic-gate 		stp->sd_qn_minpsz = qp->q_minpsz;
4127c478bd9Sstevel@tonic-gate 		stp->sd_qn_maxpsz = qp->q_maxpsz;
4137c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STWOPEN;
4147c478bd9Sstevel@tonic-gate 		goto fifo_opendone;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	/* setq might sleep in kmem_alloc - avoid holding locks. */
4177c478bd9Sstevel@tonic-gate 	setq(qp, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_FALSE);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	set_qend(qp);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * Open driver and create stream to it (via qattach).
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	cloneopen = (getmajor(*devp) == clone_major);
4257c478bd9Sstevel@tonic-gate 	if ((error = qattach(qp, devp, flag, crp, NULL, B_FALSE)) != 0) {
4267c478bd9Sstevel@tonic-gate 		mutex_enter(&vp->v_lock);
4277c478bd9Sstevel@tonic-gate 		vp->v_stream = NULL;
4287c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
4297c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
4307c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);
4317c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
4327c478bd9Sstevel@tonic-gate 		freeq(_RD(qp));
4337c478bd9Sstevel@tonic-gate 		shfree(stp);
4347c478bd9Sstevel@tonic-gate 		return (error);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * Set sd_strtab after open in order to handle clonable drivers
4387c478bd9Sstevel@tonic-gate 	 */
4397c478bd9Sstevel@tonic-gate 	stp->sd_strtab = STREAMSTAB(getmajor(*devp));
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/*
4427c478bd9Sstevel@tonic-gate 	 * Historical note: dummydev used to be be prior to the initial
4437c478bd9Sstevel@tonic-gate 	 * open (via qattach above), which made the value seen
4447c478bd9Sstevel@tonic-gate 	 * inconsistent between an I_PUSH and an autopush of a module.
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 	dummydev = *devp;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * For clone open of old style (Q not associated) network driver,
4507c478bd9Sstevel@tonic-gate 	 * push DRMODNAME module to handle DL_ATTACH/DL_DETACH
4517c478bd9Sstevel@tonic-gate 	 */
4527c478bd9Sstevel@tonic-gate 	brq = _RD(_WR(qp)->q_next);
4537c478bd9Sstevel@tonic-gate 	major = getmajor(*devp);
4547c478bd9Sstevel@tonic-gate 	if (push_drcompat && cloneopen && NETWORK_DRV(major) &&
4557c478bd9Sstevel@tonic-gate 	    ((brq->q_flag & _QASSOCIATED) == 0)) {
4567c478bd9Sstevel@tonic-gate 		if (push_mod(qp, &dummydev, stp, DRMODNAME, 0, crp) != 0)
4577c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot push " DRMODNAME
4587c478bd9Sstevel@tonic-gate 			    " streams module");
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * check for autopush
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	mutex_enter(&sad_lock);
4657c478bd9Sstevel@tonic-gate 	ap = strphash(getemajor(*devp));
4667c478bd9Sstevel@tonic-gate #define	DEVT(ap)	makedevice(ap->ap_major, ap->ap_minor)
4677c478bd9Sstevel@tonic-gate #define	DEVLT(ap)	makedevice(ap->ap_major, ap->ap_lastminor)
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	while (ap) {
4707c478bd9Sstevel@tonic-gate 		if (ap->ap_major == (getemajor(*devp))) {
4717c478bd9Sstevel@tonic-gate 			if (ap->ap_type == SAP_ALL)
4727c478bd9Sstevel@tonic-gate 				break;
4737c478bd9Sstevel@tonic-gate 			else if ((ap->ap_type == SAP_ONE) &&
4747c478bd9Sstevel@tonic-gate 			    (getminor(DEVT(ap)) == getminor(*devp)))
4757c478bd9Sstevel@tonic-gate 				break;
4767c478bd9Sstevel@tonic-gate 			else if (ap->ap_type == SAP_RANGE &&
4777c478bd9Sstevel@tonic-gate 			    getminor(*devp) >= getminor(DEVT(ap)) &&
4787c478bd9Sstevel@tonic-gate 			    getminor(*devp) <= getminor(DEVLT(ap)))
4797c478bd9Sstevel@tonic-gate 				break;
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 		ap = ap->ap_nextp;
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 	if (ap == NULL) {
4847c478bd9Sstevel@tonic-gate 		mutex_exit(&sad_lock);
4857c478bd9Sstevel@tonic-gate 		goto opendone;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	ap->ap_cnt++;
4887c478bd9Sstevel@tonic-gate 	mutex_exit(&sad_lock);
4897c478bd9Sstevel@tonic-gate 	for (s = 0; s < ap->ap_npush; s++) {
4907c478bd9Sstevel@tonic-gate 		error = push_mod(qp, &dummydev, stp, ap->ap_list[s],
4917c478bd9Sstevel@tonic-gate 		    ap->ap_anchor, crp);
4927c478bd9Sstevel@tonic-gate 		if (error != 0)
4937c478bd9Sstevel@tonic-gate 			break;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	mutex_enter(&sad_lock);
4967c478bd9Sstevel@tonic-gate 	if (--(ap->ap_cnt) <= 0)
4977c478bd9Sstevel@tonic-gate 		ap_free(ap);
4987c478bd9Sstevel@tonic-gate 	mutex_exit(&sad_lock);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 * let specfs know that open failed part way through
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if (error) {
5057c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
5067c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STREOPENFAIL;
5077c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate opendone:
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * Wake up others that are waiting for stream to be created.
5147c478bd9Sstevel@tonic-gate 	 */
5157c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
5167c478bd9Sstevel@tonic-gate 	stp->sd_flag &= ~STWOPEN;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/*
5197c478bd9Sstevel@tonic-gate 	 * As a performance concern we are caching the values of
5207c478bd9Sstevel@tonic-gate 	 * q_minpsz and q_maxpsz of the module below the stream
5217c478bd9Sstevel@tonic-gate 	 * head in the stream head.
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(stp->sd_wrq->q_next));
5247c478bd9Sstevel@tonic-gate 	rmin = stp->sd_wrq->q_next->q_minpsz;
5257c478bd9Sstevel@tonic-gate 	rmax = stp->sd_wrq->q_next->q_maxpsz;
5267c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(stp->sd_wrq->q_next));
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/* do this processing here as a performance concern */
5297c478bd9Sstevel@tonic-gate 	if (strmsgsz != 0) {
5307c478bd9Sstevel@tonic-gate 		if (rmax == INFPSZ)
5317c478bd9Sstevel@tonic-gate 			rmax = strmsgsz;
5327c478bd9Sstevel@tonic-gate 		else
5337c478bd9Sstevel@tonic-gate 			rmax = MIN(strmsgsz, rmax);
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(stp->sd_wrq));
5377c478bd9Sstevel@tonic-gate 	stp->sd_qn_minpsz = rmin;
5387c478bd9Sstevel@tonic-gate 	stp->sd_qn_maxpsz = rmax;
5397c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(stp->sd_wrq));
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate fifo_opendone:
5427c478bd9Sstevel@tonic-gate 	cv_broadcast(&stp->sd_monitor);
5437c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
5447c478bd9Sstevel@tonic-gate 	return (error);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate static int strsink(queue_t *, mblk_t *);
5487c478bd9Sstevel@tonic-gate static struct qinit deadrend = {
5497c478bd9Sstevel@tonic-gate 	strsink, NULL, NULL, NULL, NULL, &strm_info, NULL
5507c478bd9Sstevel@tonic-gate };
5517c478bd9Sstevel@tonic-gate static struct qinit deadwend = {
5527c478bd9Sstevel@tonic-gate 	NULL, NULL, NULL, NULL, NULL, &stwm_info, NULL
5537c478bd9Sstevel@tonic-gate };
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate  * Close a stream.
5577c478bd9Sstevel@tonic-gate  * This is called from closef() on the last close of an open stream.
5587c478bd9Sstevel@tonic-gate  * Strclean() will already have removed the siglist and pollist
5597c478bd9Sstevel@tonic-gate  * information, so all that remains is to remove all multiplexor links
5607c478bd9Sstevel@tonic-gate  * for the stream, pop all the modules (and the driver), and free the
5617c478bd9Sstevel@tonic-gate  * stream structure.
5627c478bd9Sstevel@tonic-gate  */
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate int
5657c478bd9Sstevel@tonic-gate strclose(struct vnode *vp, int flag, cred_t *crp)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	struct stdata *stp;
5687c478bd9Sstevel@tonic-gate 	queue_t *qp;
5697c478bd9Sstevel@tonic-gate 	int rval;
5707c478bd9Sstevel@tonic-gate 	int freestp = 1;
5717c478bd9Sstevel@tonic-gate 	queue_t *rmq;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
5747c478bd9Sstevel@tonic-gate 	if (audit_active)
5757c478bd9Sstevel@tonic-gate 		audit_strclose(vp, flag, crp);
5767c478bd9Sstevel@tonic-gate #endif
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
5797c478bd9Sstevel@tonic-gate 		TR_STRCLOSE, "strclose:%p", vp);
5807c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
5837c478bd9Sstevel@tonic-gate 	ASSERT(!(stp->sd_flag & STPLEX));
5847c478bd9Sstevel@tonic-gate 	qp = stp->sd_wrq;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/*
5877c478bd9Sstevel@tonic-gate 	 * Needed so that strpoll will return non-zero for this fd.
5887c478bd9Sstevel@tonic-gate 	 * Note that with POLLNOERR STRHUP does still cause POLLHUP.
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
5917c478bd9Sstevel@tonic-gate 	stp->sd_flag |= STRHUP;
5927c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * Since we call pollwakeup in close() now, the poll list should
5967c478bd9Sstevel@tonic-gate 	 * be empty in most cases. The only exception is the layered devices
5977c478bd9Sstevel@tonic-gate 	 * (e.g. the console drivers with redirection modules pushed on top
5987c478bd9Sstevel@tonic-gate 	 * of it).
5997c478bd9Sstevel@tonic-gate 	 */
6007c478bd9Sstevel@tonic-gate 	if (stp->sd_pollist.ph_list != NULL) {
6017c478bd9Sstevel@tonic-gate 		pollwakeup(&stp->sd_pollist, POLLERR);
6027c478bd9Sstevel@tonic-gate 		pollhead_clean(&stp->sd_pollist);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_pollist.ph_list == NULL);
6057c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_sidp == NULL);
6067c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_pgidp == NULL);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/*
6097c478bd9Sstevel@tonic-gate 	 * If the registered process or process group did not have an
6107c478bd9Sstevel@tonic-gate 	 * open instance of this stream then strclean would not be
6117c478bd9Sstevel@tonic-gate 	 * called. Thus at the time of closing all remaining siglist entries
6127c478bd9Sstevel@tonic-gate 	 * are removed.
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	if (stp->sd_siglist != NULL)
6157c478bd9Sstevel@tonic-gate 		strcleanall(vp);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_siglist == NULL);
6187c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_sigflags == 0);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	if (STRMATED(stp)) {
6217c478bd9Sstevel@tonic-gate 		struct stdata *strmatep = stp->sd_mate;
6227c478bd9Sstevel@tonic-gate 		int waited = 1;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		STRLOCKMATES(stp);
6257c478bd9Sstevel@tonic-gate 		while (waited) {
6267c478bd9Sstevel@tonic-gate 			waited = 0;
6277c478bd9Sstevel@tonic-gate 			while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
6287c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
6297c478bd9Sstevel@tonic-gate 				cv_wait(&stp->sd_monitor, &stp->sd_lock);
6307c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
6317c478bd9Sstevel@tonic-gate 				STRLOCKMATES(stp);
6327c478bd9Sstevel@tonic-gate 				waited = 1;
6337c478bd9Sstevel@tonic-gate 			}
6347c478bd9Sstevel@tonic-gate 			while (strmatep->sd_flag &
6357c478bd9Sstevel@tonic-gate 			    (STWOPEN|STRCLOSE|STRPLUMB)) {
6367c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
6377c478bd9Sstevel@tonic-gate 				cv_wait(&strmatep->sd_monitor,
6387c478bd9Sstevel@tonic-gate 				    &strmatep->sd_lock);
6397c478bd9Sstevel@tonic-gate 				mutex_exit(&strmatep->sd_lock);
6407c478bd9Sstevel@tonic-gate 				STRLOCKMATES(stp);
6417c478bd9Sstevel@tonic-gate 				waited = 1;
6427c478bd9Sstevel@tonic-gate 			}
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRCLOSE;
6457c478bd9Sstevel@tonic-gate 		STRUNLOCKMATES(stp);
6467c478bd9Sstevel@tonic-gate 	} else {
6477c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
6487c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRCLOSE;
6497c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_first == NULL);	/* No more delayed write */
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/* Check if an I_LINK was ever done on this stream */
6557c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & STRHASLINKS) {
6567c478bd9Sstevel@tonic-gate 		(void) munlinkall(stp, LINKCLOSE|LINKNORMAL, crp, &rval);
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	while (_SAMESTR(qp)) {
6607c478bd9Sstevel@tonic-gate 		/*
6617c478bd9Sstevel@tonic-gate 		 * Holding sd_lock prevents q_next from changing in
6627c478bd9Sstevel@tonic-gate 		 * this stream.
6637c478bd9Sstevel@tonic-gate 		 */
6647c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
6657c478bd9Sstevel@tonic-gate 		if (!(flag & (FNDELAY|FNONBLOCK)) && (stp->sd_closetime > 0)) {
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 			/*
6687c478bd9Sstevel@tonic-gate 			 * sleep until awakened by strwsrv() or timeout
6697c478bd9Sstevel@tonic-gate 			 */
6707c478bd9Sstevel@tonic-gate 			for (;;) {
6717c478bd9Sstevel@tonic-gate 				mutex_enter(QLOCK(qp->q_next));
6727c478bd9Sstevel@tonic-gate 				if (!(qp->q_next->q_mblkcnt)) {
6737c478bd9Sstevel@tonic-gate 					mutex_exit(QLOCK(qp->q_next));
6747c478bd9Sstevel@tonic-gate 					break;
6757c478bd9Sstevel@tonic-gate 				}
6767c478bd9Sstevel@tonic-gate 				stp->sd_flag |= WSLEEP;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 				/* ensure strwsrv gets enabled */
6797c478bd9Sstevel@tonic-gate 				qp->q_next->q_flag |= QWANTW;
6807c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(qp->q_next));
6817c478bd9Sstevel@tonic-gate 				/* get out if we timed out or recv'd a signal */
6827c478bd9Sstevel@tonic-gate 				if (str_cv_wait(&qp->q_wait, &stp->sd_lock,
6837c478bd9Sstevel@tonic-gate 				    stp->sd_closetime, 0) <= 0) {
6847c478bd9Sstevel@tonic-gate 					break;
6857c478bd9Sstevel@tonic-gate 				}
6867c478bd9Sstevel@tonic-gate 			}
6877c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~WSLEEP;
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		rmq = qp->q_next;
6927c478bd9Sstevel@tonic-gate 		if (rmq->q_flag & QISDRV) {
6937c478bd9Sstevel@tonic-gate 			ASSERT(!_SAMESTR(rmq));
6947c478bd9Sstevel@tonic-gate 			wait_sq_svc(_RD(qp)->q_syncq);
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 		qdetach(_RD(rmq), 1, flag, crp, B_FALSE);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/* Prevent qenable from re-enabling the stream head queue */
7017c478bd9Sstevel@tonic-gate 	disable_svc(_RD(qp));
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * Wait until service procedure of each queue is
7057c478bd9Sstevel@tonic-gate 	 * run, if QINSERVICE is set.
7067c478bd9Sstevel@tonic-gate 	 */
7077c478bd9Sstevel@tonic-gate 	wait_svc(_RD(qp));
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/*
7107c478bd9Sstevel@tonic-gate 	 * Now, flush both queues.
7117c478bd9Sstevel@tonic-gate 	 */
7127c478bd9Sstevel@tonic-gate 	flushq(_RD(qp), FLUSHALL);
7137c478bd9Sstevel@tonic-gate 	flushq(qp, FLUSHALL);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/*
7167c478bd9Sstevel@tonic-gate 	 * If the write queue of the stream head is pointing to a
7177c478bd9Sstevel@tonic-gate 	 * read queue, we have a twisted stream.  If the read queue
7187c478bd9Sstevel@tonic-gate 	 * is alive, convert the stream head queues into a dead end.
7197c478bd9Sstevel@tonic-gate 	 * If the read queue is dead, free the dead pair.
7207c478bd9Sstevel@tonic-gate 	 */
7217c478bd9Sstevel@tonic-gate 	if (qp->q_next && !_SAMESTR(qp)) {
7227c478bd9Sstevel@tonic-gate 		if (qp->q_next->q_qinfo == &deadrend) {	/* half-closed pipe */
7237c478bd9Sstevel@tonic-gate 			flushq(qp->q_next, FLUSHALL); /* ensure no message */
7247c478bd9Sstevel@tonic-gate 			shfree(qp->q_next->q_stream);
7257c478bd9Sstevel@tonic-gate 			freeq(qp->q_next);
7267c478bd9Sstevel@tonic-gate 			freeq(_RD(qp));
7277c478bd9Sstevel@tonic-gate 		} else if (qp->q_next == _RD(qp)) {	/* fifo */
7287c478bd9Sstevel@tonic-gate 			freeq(_RD(qp));
7297c478bd9Sstevel@tonic-gate 		} else {				/* pipe */
7307c478bd9Sstevel@tonic-gate 			freestp = 0;
7317c478bd9Sstevel@tonic-gate 			/*
7327c478bd9Sstevel@tonic-gate 			 * The q_info pointers are never accessed when
7337c478bd9Sstevel@tonic-gate 			 * SQLOCK is held.
7347c478bd9Sstevel@tonic-gate 			 */
7357c478bd9Sstevel@tonic-gate 			ASSERT(qp->q_syncq == _RD(qp)->q_syncq);
7367c478bd9Sstevel@tonic-gate 			mutex_enter(SQLOCK(qp->q_syncq));
7377c478bd9Sstevel@tonic-gate 			qp->q_qinfo = &deadwend;
7387c478bd9Sstevel@tonic-gate 			_RD(qp)->q_qinfo = &deadrend;
7397c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(qp->q_syncq));
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 	} else {
7427c478bd9Sstevel@tonic-gate 		freeq(_RD(qp)); /* free stream head queue pair */
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
7467c478bd9Sstevel@tonic-gate 	if (stp->sd_iocblk) {
7477c478bd9Sstevel@tonic-gate 		if (stp->sd_iocblk != (mblk_t *)-1) {
7487c478bd9Sstevel@tonic-gate 			freemsg(stp->sd_iocblk);
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = NULL;
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 	stp->sd_vnode = NULL;
7537c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
7547c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
7557c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
7567c478bd9Sstevel@tonic-gate 	stp->sd_flag &= ~STRCLOSE;
7577c478bd9Sstevel@tonic-gate 	cv_broadcast(&stp->sd_monitor);
7587c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	if (freestp)
7617c478bd9Sstevel@tonic-gate 		shfree(stp);
7627c478bd9Sstevel@tonic-gate 	return (0);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate static int
7667c478bd9Sstevel@tonic-gate strsink(queue_t *q, mblk_t *bp)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	struct copyresp *resp;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	switch (bp->b_datap->db_type) {
7717c478bd9Sstevel@tonic-gate 	case M_FLUSH:
7727c478bd9Sstevel@tonic-gate 		if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
7737c478bd9Sstevel@tonic-gate 			*bp->b_rptr &= ~FLUSHR;
7747c478bd9Sstevel@tonic-gate 			bp->b_flag |= MSGNOLOOP;
7757c478bd9Sstevel@tonic-gate 			/*
7767c478bd9Sstevel@tonic-gate 			 * Protect against the driver passing up
7777c478bd9Sstevel@tonic-gate 			 * messages after it has done a qprocsoff.
7787c478bd9Sstevel@tonic-gate 			 */
7797c478bd9Sstevel@tonic-gate 			if (_OTHERQ(q)->q_next == NULL)
7807c478bd9Sstevel@tonic-gate 				freemsg(bp);
7817c478bd9Sstevel@tonic-gate 			else
7827c478bd9Sstevel@tonic-gate 				qreply(q, bp);
7837c478bd9Sstevel@tonic-gate 		} else {
7847c478bd9Sstevel@tonic-gate 			freemsg(bp);
7857c478bd9Sstevel@tonic-gate 		}
7867c478bd9Sstevel@tonic-gate 		break;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	case M_COPYIN:
7897c478bd9Sstevel@tonic-gate 	case M_COPYOUT:
7907c478bd9Sstevel@tonic-gate 		if (bp->b_cont) {
7917c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
7927c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCDATA;
7957c478bd9Sstevel@tonic-gate 		bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
7967c478bd9Sstevel@tonic-gate 		resp = (struct copyresp *)bp->b_rptr;
7977c478bd9Sstevel@tonic-gate 		resp->cp_rval = (caddr_t)1;	/* failure */
7987c478bd9Sstevel@tonic-gate 		/*
7997c478bd9Sstevel@tonic-gate 		 * Protect against the driver passing up
8007c478bd9Sstevel@tonic-gate 		 * messages after it has done a qprocsoff.
8017c478bd9Sstevel@tonic-gate 		 */
8027c478bd9Sstevel@tonic-gate 		if (_OTHERQ(q)->q_next == NULL)
8037c478bd9Sstevel@tonic-gate 			freemsg(bp);
8047c478bd9Sstevel@tonic-gate 		else
8057c478bd9Sstevel@tonic-gate 			qreply(q, bp);
8067c478bd9Sstevel@tonic-gate 		break;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	case M_IOCTL:
8097c478bd9Sstevel@tonic-gate 		if (bp->b_cont) {
8107c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
8117c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCNAK;
8147c478bd9Sstevel@tonic-gate 		/*
8157c478bd9Sstevel@tonic-gate 		 * Protect against the driver passing up
8167c478bd9Sstevel@tonic-gate 		 * messages after it has done a qprocsoff.
8177c478bd9Sstevel@tonic-gate 		 */
8187c478bd9Sstevel@tonic-gate 		if (_OTHERQ(q)->q_next == NULL)
8197c478bd9Sstevel@tonic-gate 			freemsg(bp);
8207c478bd9Sstevel@tonic-gate 		else
8217c478bd9Sstevel@tonic-gate 			qreply(q, bp);
8227c478bd9Sstevel@tonic-gate 		break;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	default:
8257c478bd9Sstevel@tonic-gate 		freemsg(bp);
8267c478bd9Sstevel@tonic-gate 		break;
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	return (0);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate /*
8337c478bd9Sstevel@tonic-gate  * Clean up after a process when it closes a stream.  This is called
8347c478bd9Sstevel@tonic-gate  * from closef for all closes, whereas strclose is called only for the
8357c478bd9Sstevel@tonic-gate  * last close on a stream.  The siglist is scanned for entries for the
8367c478bd9Sstevel@tonic-gate  * current process, and these are removed.
8377c478bd9Sstevel@tonic-gate  */
8387c478bd9Sstevel@tonic-gate void
8397c478bd9Sstevel@tonic-gate strclean(struct vnode *vp)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	strsig_t *ssp, *pssp, *tssp;
8427c478bd9Sstevel@tonic-gate 	stdata_t *stp;
8437c478bd9Sstevel@tonic-gate 	int update = 0;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
8467c478bd9Sstevel@tonic-gate 		TR_STRCLEAN, "strclean:%p", vp);
8477c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
8487c478bd9Sstevel@tonic-gate 	pssp = NULL;
8497c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
8507c478bd9Sstevel@tonic-gate 	ssp = stp->sd_siglist;
8517c478bd9Sstevel@tonic-gate 	while (ssp) {
8527c478bd9Sstevel@tonic-gate 		if (ssp->ss_pidp == curproc->p_pidp) {
8537c478bd9Sstevel@tonic-gate 			tssp = ssp->ss_next;
8547c478bd9Sstevel@tonic-gate 			if (pssp)
8557c478bd9Sstevel@tonic-gate 				pssp->ss_next = tssp;
8567c478bd9Sstevel@tonic-gate 			else
8577c478bd9Sstevel@tonic-gate 				stp->sd_siglist = tssp;
8587c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
8597c478bd9Sstevel@tonic-gate 			PID_RELE(ssp->ss_pidp);
8607c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
8617c478bd9Sstevel@tonic-gate 			kmem_free(ssp, sizeof (strsig_t));
8627c478bd9Sstevel@tonic-gate 			update = 1;
8637c478bd9Sstevel@tonic-gate 			ssp = tssp;
8647c478bd9Sstevel@tonic-gate 		} else {
8657c478bd9Sstevel@tonic-gate 			pssp = ssp;
8667c478bd9Sstevel@tonic-gate 			ssp = ssp->ss_next;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 	if (update) {
8707c478bd9Sstevel@tonic-gate 		stp->sd_sigflags = 0;
8717c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
8727c478bd9Sstevel@tonic-gate 			stp->sd_sigflags |= ssp->ss_events;
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate /*
8787c478bd9Sstevel@tonic-gate  * Used on the last close to remove any remaining items on the siglist.
8797c478bd9Sstevel@tonic-gate  * These could be present on the siglist due to I_ESETSIG calls that
8807c478bd9Sstevel@tonic-gate  * use process groups or processed that do not have an open file descriptor
8817c478bd9Sstevel@tonic-gate  * for this stream (Such entries would not be removed by strclean).
8827c478bd9Sstevel@tonic-gate  */
8837c478bd9Sstevel@tonic-gate static void
8847c478bd9Sstevel@tonic-gate strcleanall(struct vnode *vp)
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate 	strsig_t *ssp, *nssp;
8877c478bd9Sstevel@tonic-gate 	stdata_t *stp;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
8907c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
8917c478bd9Sstevel@tonic-gate 	ssp = stp->sd_siglist;
8927c478bd9Sstevel@tonic-gate 	stp->sd_siglist = NULL;
8937c478bd9Sstevel@tonic-gate 	while (ssp) {
8947c478bd9Sstevel@tonic-gate 		nssp = ssp->ss_next;
8957c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
8967c478bd9Sstevel@tonic-gate 		PID_RELE(ssp->ss_pidp);
8977c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
8987c478bd9Sstevel@tonic-gate 		kmem_free(ssp, sizeof (strsig_t));
8997c478bd9Sstevel@tonic-gate 		ssp = nssp;
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 	stp->sd_sigflags = 0;
9027c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate /*
9067c478bd9Sstevel@tonic-gate  * Retrieve the next message from the logical stream head read queue
9077c478bd9Sstevel@tonic-gate  * using either rwnext (if sync stream) or getq_noenab.
9087c478bd9Sstevel@tonic-gate  * It is the callers responsibility to call qbackenable after
9097c478bd9Sstevel@tonic-gate  * it is finished with the message. The caller should not call
9107c478bd9Sstevel@tonic-gate  * qbackenable until after any putback calls to avoid spurious backenabling.
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate mblk_t *
9137c478bd9Sstevel@tonic-gate strget(struct stdata *stp, queue_t *q, struct uio *uiop, int first,
9147c478bd9Sstevel@tonic-gate     int *errorp)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	mblk_t *bp;
9177c478bd9Sstevel@tonic-gate 	int error;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
9207c478bd9Sstevel@tonic-gate 	/* Holding sd_lock prevents the read queue from changing  */
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (uiop != NULL && stp->sd_struiordq != NULL &&
9237c478bd9Sstevel@tonic-gate 	    q->q_first == NULL &&
9247c478bd9Sstevel@tonic-gate 	    (!first || (stp->sd_wakeq & RSLEEP))) {
9257c478bd9Sstevel@tonic-gate 		/*
9267c478bd9Sstevel@tonic-gate 		 * Stream supports rwnext() for the read side.
9277c478bd9Sstevel@tonic-gate 		 * If this is the first time we're called by e.g. strread
9287c478bd9Sstevel@tonic-gate 		 * only do the downcall if there is a deferred wakeup
9297c478bd9Sstevel@tonic-gate 		 * (registered in sd_wakeq).
9307c478bd9Sstevel@tonic-gate 		 */
9317c478bd9Sstevel@tonic-gate 		struiod_t uiod;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		if (first)
9347c478bd9Sstevel@tonic-gate 			stp->sd_wakeq &= ~RSLEEP;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 		(void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
9377c478bd9Sstevel@tonic-gate 			sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
9387c478bd9Sstevel@tonic-gate 		uiod.d_mp = 0;
9397c478bd9Sstevel@tonic-gate 		/*
9407c478bd9Sstevel@tonic-gate 		 * Mark that a thread is in rwnext on the read side
9417c478bd9Sstevel@tonic-gate 		 * to prevent strrput from nacking ioctls immediately.
9427c478bd9Sstevel@tonic-gate 		 * When the last concurrent rwnext returns
9437c478bd9Sstevel@tonic-gate 		 * the ioctls are nack'ed.
9447c478bd9Sstevel@tonic-gate 		 */
9457c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&stp->sd_lock));
9467c478bd9Sstevel@tonic-gate 		stp->sd_struiodnak++;
9477c478bd9Sstevel@tonic-gate 		/*
9487c478bd9Sstevel@tonic-gate 		 * Note: rwnext will drop sd_lock.
9497c478bd9Sstevel@tonic-gate 		 */
9507c478bd9Sstevel@tonic-gate 		error = rwnext(q, &uiod);
9517c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
9527c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
9537c478bd9Sstevel@tonic-gate 		stp->sd_struiodnak--;
9547c478bd9Sstevel@tonic-gate 		while (stp->sd_struiodnak == 0 &&
9557c478bd9Sstevel@tonic-gate 		    ((bp = stp->sd_struionak) != NULL)) {
9567c478bd9Sstevel@tonic-gate 			stp->sd_struionak = bp->b_next;
9577c478bd9Sstevel@tonic-gate 			bp->b_next = NULL;
9587c478bd9Sstevel@tonic-gate 			bp->b_datap->db_type = M_IOCNAK;
9597c478bd9Sstevel@tonic-gate 			/*
9607c478bd9Sstevel@tonic-gate 			 * Protect against the driver passing up
9617c478bd9Sstevel@tonic-gate 			 * messages after it has done a qprocsoff.
9627c478bd9Sstevel@tonic-gate 			 */
9637c478bd9Sstevel@tonic-gate 			if (_OTHERQ(q)->q_next == NULL)
9647c478bd9Sstevel@tonic-gate 				freemsg(bp);
9657c478bd9Sstevel@tonic-gate 			else {
9667c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
9677c478bd9Sstevel@tonic-gate 				qreply(q, bp);
9687c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
9697c478bd9Sstevel@tonic-gate 			}
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&stp->sd_lock));
9727c478bd9Sstevel@tonic-gate 		if (error == 0 || error == EWOULDBLOCK) {
9737c478bd9Sstevel@tonic-gate 			if ((bp = uiod.d_mp) != NULL) {
9747c478bd9Sstevel@tonic-gate 				*errorp = 0;
9757c478bd9Sstevel@tonic-gate 				ASSERT(MUTEX_HELD(&stp->sd_lock));
9767c478bd9Sstevel@tonic-gate 				return (bp);
9777c478bd9Sstevel@tonic-gate 			}
9787c478bd9Sstevel@tonic-gate 			error = 0;
9797c478bd9Sstevel@tonic-gate 		} else if (error == EINVAL) {
9807c478bd9Sstevel@tonic-gate 			/*
9817c478bd9Sstevel@tonic-gate 			 * The stream plumbing must have
9827c478bd9Sstevel@tonic-gate 			 * changed while we were away, so
9837c478bd9Sstevel@tonic-gate 			 * just turn off rwnext()s.
9847c478bd9Sstevel@tonic-gate 			 */
9857c478bd9Sstevel@tonic-gate 			error = 0;
9867c478bd9Sstevel@tonic-gate 		} else if (error == EBUSY) {
9877c478bd9Sstevel@tonic-gate 			/*
9887c478bd9Sstevel@tonic-gate 			 * The module might have data in transit using putnext
9897c478bd9Sstevel@tonic-gate 			 * Fall back on waiting + getq.
9907c478bd9Sstevel@tonic-gate 			 */
9917c478bd9Sstevel@tonic-gate 			error = 0;
9927c478bd9Sstevel@tonic-gate 		} else {
9937c478bd9Sstevel@tonic-gate 			*errorp = error;
9947c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(&stp->sd_lock));
9957c478bd9Sstevel@tonic-gate 			return (NULL);
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 		/*
9987c478bd9Sstevel@tonic-gate 		 * Try a getq in case a rwnext() generated mblk
9997c478bd9Sstevel@tonic-gate 		 * has bubbled up via strrput().
10007c478bd9Sstevel@tonic-gate 		 */
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 	*errorp = 0;
10037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
10047c478bd9Sstevel@tonic-gate 	return (getq_noenab(q));
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate  * Copy out the message pointed to by `bp' into the uio pointed to by `uiop'.
10097c478bd9Sstevel@tonic-gate  * If the message does not fit in the uio the remainder of it is returned;
10107c478bd9Sstevel@tonic-gate  * otherwise NULL is returned.  Any embedded zero-length mblk_t's are
10117c478bd9Sstevel@tonic-gate  * consumed, even if uio_resid reaches zero.  On error, `*errorp' is set to
10127c478bd9Sstevel@tonic-gate  * the error code, the message is consumed, and NULL is returned.
10137c478bd9Sstevel@tonic-gate  */
10147c478bd9Sstevel@tonic-gate static mblk_t *
10157c478bd9Sstevel@tonic-gate struiocopyout(mblk_t *bp, struct uio *uiop, int *errorp)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	int error;
10187c478bd9Sstevel@tonic-gate 	ptrdiff_t n;
10197c478bd9Sstevel@tonic-gate 	mblk_t *nbp;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_wptr >= bp->b_rptr);
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	do {
10247c478bd9Sstevel@tonic-gate 		if ((n = MIN(uiop->uio_resid, MBLKL(bp))) != 0) {
10257c478bd9Sstevel@tonic-gate 			ASSERT(n > 0);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 			error = uiomove(bp->b_rptr, n, UIO_READ, uiop);
10287c478bd9Sstevel@tonic-gate 			if (error != 0) {
10297c478bd9Sstevel@tonic-gate 				freemsg(bp);
10307c478bd9Sstevel@tonic-gate 				*errorp = error;
10317c478bd9Sstevel@tonic-gate 				return (NULL);
10327c478bd9Sstevel@tonic-gate 			}
10337c478bd9Sstevel@tonic-gate 		}
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 		bp->b_rptr += n;
10367c478bd9Sstevel@tonic-gate 		while (bp != NULL && (bp->b_rptr >= bp->b_wptr)) {
10377c478bd9Sstevel@tonic-gate 			nbp = bp;
10387c478bd9Sstevel@tonic-gate 			bp = bp->b_cont;
10397c478bd9Sstevel@tonic-gate 			freeb(nbp);
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 	} while (bp != NULL && uiop->uio_resid > 0);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	*errorp = 0;
10447c478bd9Sstevel@tonic-gate 	return (bp);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate  * Read a stream according to the mode flags in sd_flag:
10497c478bd9Sstevel@tonic-gate  *
10507c478bd9Sstevel@tonic-gate  * (default mode)		- Byte stream, msg boundaries are ignored
10517c478bd9Sstevel@tonic-gate  * RD_MSGDIS (msg discard)	- Read on msg boundaries and throw away
10527c478bd9Sstevel@tonic-gate  *				any data remaining in msg
10537c478bd9Sstevel@tonic-gate  * RD_MSGNODIS (msg non-discard) - Read on msg boundaries and put back
10547c478bd9Sstevel@tonic-gate  *				any remaining data on head of read queue
10557c478bd9Sstevel@tonic-gate  *
10567c478bd9Sstevel@tonic-gate  * Consume readable messages on the front of the queue until
10577c478bd9Sstevel@tonic-gate  * ttolwp(curthread)->lwp_count
10587c478bd9Sstevel@tonic-gate  * is satisfied, the readable messages are exhausted, or a message
10597c478bd9Sstevel@tonic-gate  * boundary is reached in a message mode.  If no data was read and
10607c478bd9Sstevel@tonic-gate  * the stream was not opened with the NDELAY flag, block until data arrives.
10617c478bd9Sstevel@tonic-gate  * Otherwise return the data read and update the count.
10627c478bd9Sstevel@tonic-gate  *
10637c478bd9Sstevel@tonic-gate  * In default mode a 0 length message signifies end-of-file and terminates
10647c478bd9Sstevel@tonic-gate  * a read in progress.  The 0 length message is removed from the queue
10657c478bd9Sstevel@tonic-gate  * only if it is the only message read (no data is read).
10667c478bd9Sstevel@tonic-gate  *
10677c478bd9Sstevel@tonic-gate  * An attempt to read an M_PROTO or M_PCPROTO message results in an
10687c478bd9Sstevel@tonic-gate  * EBADMSG error return, unless either RD_PROTDAT or RD_PROTDIS are set.
10697c478bd9Sstevel@tonic-gate  * If RD_PROTDAT is set, M_PROTO and M_PCPROTO messages are read as data.
10707c478bd9Sstevel@tonic-gate  * If RD_PROTDIS is set, the M_PROTO and M_PCPROTO parts of the message
10717c478bd9Sstevel@tonic-gate  * are unlinked from and M_DATA blocks in the message, the protos are
10727c478bd9Sstevel@tonic-gate  * thrown away, and the data is read.
10737c478bd9Sstevel@tonic-gate  */
10747c478bd9Sstevel@tonic-gate /* ARGSUSED */
10757c478bd9Sstevel@tonic-gate int
10767c478bd9Sstevel@tonic-gate strread(struct vnode *vp, struct uio *uiop, cred_t *crp)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	struct stdata *stp;
10797c478bd9Sstevel@tonic-gate 	mblk_t *bp, *nbp;
10807c478bd9Sstevel@tonic-gate 	queue_t *q;
10817c478bd9Sstevel@tonic-gate 	int error = 0;
10827c478bd9Sstevel@tonic-gate 	uint_t old_sd_flag;
10837c478bd9Sstevel@tonic-gate 	int first;
10847c478bd9Sstevel@tonic-gate 	char rflg;
10857c478bd9Sstevel@tonic-gate 	uint_t mark;		/* Contains MSG*MARK and _LASTMARK */
10867c478bd9Sstevel@tonic-gate #define	_LASTMARK	0x8000	/* Distinct from MSG*MARK */
10877c478bd9Sstevel@tonic-gate 	short delim;
10887c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
10897c478bd9Sstevel@tonic-gate 	char waitflag;
10907c478bd9Sstevel@tonic-gate 	unsigned char type;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
10937c478bd9Sstevel@tonic-gate 		TR_STRREAD_ENTER, "strread:%p", vp);
10947c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
10957c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
10987c478bd9Sstevel@tonic-gate 		if (error = straccess(stp, JCREAD))
10997c478bd9Sstevel@tonic-gate 			return (error);
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
11027c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STRDERR|STPLEX)) {
11037c478bd9Sstevel@tonic-gate 		error = strgeterr(stp, STRDERR|STPLEX, 0);
11047c478bd9Sstevel@tonic-gate 		if (error != 0) {
11057c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
11067c478bd9Sstevel@tonic-gate 			return (error);
11077c478bd9Sstevel@tonic-gate 		}
11087c478bd9Sstevel@tonic-gate 	}
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	/*
11117c478bd9Sstevel@tonic-gate 	 * Loop terminates when uiop->uio_resid == 0.
11127c478bd9Sstevel@tonic-gate 	 */
11137c478bd9Sstevel@tonic-gate 	rflg = 0;
11147c478bd9Sstevel@tonic-gate 	waitflag = READWAIT;
11157c478bd9Sstevel@tonic-gate 	q = _RD(stp->sd_wrq);
11167c478bd9Sstevel@tonic-gate 	for (;;) {
11177c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&stp->sd_lock));
11187c478bd9Sstevel@tonic-gate 		old_sd_flag = stp->sd_flag;
11197c478bd9Sstevel@tonic-gate 		mark = 0;
11207c478bd9Sstevel@tonic-gate 		delim = 0;
11217c478bd9Sstevel@tonic-gate 		first = 1;
11227c478bd9Sstevel@tonic-gate 		while ((bp = strget(stp, q, uiop, first, &error)) == NULL) {
11237c478bd9Sstevel@tonic-gate 			int done = 0;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(&stp->sd_lock));
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 			if (error != 0)
11287c478bd9Sstevel@tonic-gate 				goto oops;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STRHUP|STREOF)) {
11317c478bd9Sstevel@tonic-gate 				goto oops;
11327c478bd9Sstevel@tonic-gate 			}
11337c478bd9Sstevel@tonic-gate 			if (rflg && !(stp->sd_flag & STRDELIM)) {
11347c478bd9Sstevel@tonic-gate 				goto oops;
11357c478bd9Sstevel@tonic-gate 			}
11367c478bd9Sstevel@tonic-gate 			/*
11377c478bd9Sstevel@tonic-gate 			 * If a read(fd,buf,0) has been done, there is no
11387c478bd9Sstevel@tonic-gate 			 * need to sleep. We always have zero bytes to
11397c478bd9Sstevel@tonic-gate 			 * return.
11407c478bd9Sstevel@tonic-gate 			 */
11417c478bd9Sstevel@tonic-gate 			if (uiop->uio_resid == 0) {
11427c478bd9Sstevel@tonic-gate 				goto oops;
11437c478bd9Sstevel@tonic-gate 			}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 			qbackenable(q, 0);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_WAIT,
11487c478bd9Sstevel@tonic-gate 				"strread calls strwaitq:%p, %p, %p",
11497c478bd9Sstevel@tonic-gate 				vp, uiop, crp);
11507c478bd9Sstevel@tonic-gate 			if ((error = strwaitq(stp, waitflag, uiop->uio_resid,
11517c478bd9Sstevel@tonic-gate 			    uiop->uio_fmode, -1, &done)) != 0 || done) {
11527c478bd9Sstevel@tonic-gate 				TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_DONE,
11537c478bd9Sstevel@tonic-gate 					"strread error or done:%p, %p, %p",
11547c478bd9Sstevel@tonic-gate 					vp, uiop, crp);
11557c478bd9Sstevel@tonic-gate 				if ((uiop->uio_fmode & FNDELAY) &&
11567c478bd9Sstevel@tonic-gate 				    (stp->sd_flag & OLDNDELAY) &&
11577c478bd9Sstevel@tonic-gate 				    (error == EAGAIN))
11587c478bd9Sstevel@tonic-gate 					error = 0;
11597c478bd9Sstevel@tonic-gate 				goto oops;
11607c478bd9Sstevel@tonic-gate 			}
11617c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_AWAKE,
11627c478bd9Sstevel@tonic-gate 				"strread awakes:%p, %p, %p", vp, uiop, crp);
11637c478bd9Sstevel@tonic-gate 			if (stp->sd_sidp != NULL &&
11647c478bd9Sstevel@tonic-gate 			    stp->sd_vnode->v_type != VFIFO) {
11657c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
11667c478bd9Sstevel@tonic-gate 				if (error = straccess(stp, JCREAD))
11677c478bd9Sstevel@tonic-gate 					goto oops1;
11687c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
11697c478bd9Sstevel@tonic-gate 			}
11707c478bd9Sstevel@tonic-gate 			first = 0;
11717c478bd9Sstevel@tonic-gate 		}
11727c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&stp->sd_lock));
11737c478bd9Sstevel@tonic-gate 		ASSERT(bp);
11747c478bd9Sstevel@tonic-gate 		pri = bp->b_band;
11757c478bd9Sstevel@tonic-gate 		/*
11767c478bd9Sstevel@tonic-gate 		 * Extract any mark information. If the message is not
11777c478bd9Sstevel@tonic-gate 		 * completely consumed this information will be put in the mblk
11787c478bd9Sstevel@tonic-gate 		 * that is putback.
11797c478bd9Sstevel@tonic-gate 		 * If MSGMARKNEXT is set and the message is completely consumed
11807c478bd9Sstevel@tonic-gate 		 * the STRATMARK flag will be set below. Likewise, if
11817c478bd9Sstevel@tonic-gate 		 * MSGNOTMARKNEXT is set and the message is
11827c478bd9Sstevel@tonic-gate 		 * completely consumed STRNOTATMARK will be set.
11837c478bd9Sstevel@tonic-gate 		 *
11847c478bd9Sstevel@tonic-gate 		 * For some unknown reason strread only breaks the read at the
11857c478bd9Sstevel@tonic-gate 		 * last mark.
11867c478bd9Sstevel@tonic-gate 		 */
11877c478bd9Sstevel@tonic-gate 		mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
11887c478bd9Sstevel@tonic-gate 		ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
11897c478bd9Sstevel@tonic-gate 			(MSGMARKNEXT|MSGNOTMARKNEXT));
11907c478bd9Sstevel@tonic-gate 		if (mark != 0 && bp == stp->sd_mark) {
11917c478bd9Sstevel@tonic-gate 			if (rflg) {
11927c478bd9Sstevel@tonic-gate 				putback(stp, q, bp, pri);
11937c478bd9Sstevel@tonic-gate 				goto oops;
11947c478bd9Sstevel@tonic-gate 			}
11957c478bd9Sstevel@tonic-gate 			mark |= _LASTMARK;
11967c478bd9Sstevel@tonic-gate 			stp->sd_mark = NULL;
11977c478bd9Sstevel@tonic-gate 		}
11987c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STRDELIM) && (bp->b_flag & MSGDELIM))
11997c478bd9Sstevel@tonic-gate 			delim = 1;
12007c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 		if (STREAM_NEEDSERVICE(stp))
12037c478bd9Sstevel@tonic-gate 			stream_runservice(stp);
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 		type = bp->b_datap->db_type;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 		switch (type) {
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 		case M_DATA:
12107c478bd9Sstevel@tonic-gate ismdata:
12117c478bd9Sstevel@tonic-gate 			if (msgnodata(bp)) {
12127c478bd9Sstevel@tonic-gate 				if (mark || delim) {
12137c478bd9Sstevel@tonic-gate 					freemsg(bp);
12147c478bd9Sstevel@tonic-gate 				} else if (rflg) {
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 					/*
12177c478bd9Sstevel@tonic-gate 					 * If already read data put zero
12187c478bd9Sstevel@tonic-gate 					 * length message back on queue else
12197c478bd9Sstevel@tonic-gate 					 * free msg and return 0.
12207c478bd9Sstevel@tonic-gate 					 */
12217c478bd9Sstevel@tonic-gate 					bp->b_band = pri;
12227c478bd9Sstevel@tonic-gate 					mutex_enter(&stp->sd_lock);
12237c478bd9Sstevel@tonic-gate 					putback(stp, q, bp, pri);
12247c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
12257c478bd9Sstevel@tonic-gate 				} else {
12267c478bd9Sstevel@tonic-gate 					freemsg(bp);
12277c478bd9Sstevel@tonic-gate 				}
12287c478bd9Sstevel@tonic-gate 				error =  0;
12297c478bd9Sstevel@tonic-gate 				goto oops1;
12307c478bd9Sstevel@tonic-gate 			}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 			rflg = 1;
12337c478bd9Sstevel@tonic-gate 			waitflag |= NOINTR;
12347c478bd9Sstevel@tonic-gate 			bp = struiocopyout(bp, uiop, &error);
12357c478bd9Sstevel@tonic-gate 			if (error != 0)
12367c478bd9Sstevel@tonic-gate 				goto oops1;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
12397c478bd9Sstevel@tonic-gate 			if (bp) {
12407c478bd9Sstevel@tonic-gate 				/*
12417c478bd9Sstevel@tonic-gate 				 * Have remaining data in message.
12427c478bd9Sstevel@tonic-gate 				 * Free msg if in discard mode.
12437c478bd9Sstevel@tonic-gate 				 */
12447c478bd9Sstevel@tonic-gate 				if (stp->sd_read_opt & RD_MSGDIS) {
12457c478bd9Sstevel@tonic-gate 					freemsg(bp);
12467c478bd9Sstevel@tonic-gate 				} else {
12477c478bd9Sstevel@tonic-gate 					bp->b_band = pri;
12487c478bd9Sstevel@tonic-gate 					if ((mark & _LASTMARK) &&
12497c478bd9Sstevel@tonic-gate 					    (stp->sd_mark == NULL))
12507c478bd9Sstevel@tonic-gate 						stp->sd_mark = bp;
12517c478bd9Sstevel@tonic-gate 					bp->b_flag |= mark & ~_LASTMARK;
12527c478bd9Sstevel@tonic-gate 					if (delim)
12537c478bd9Sstevel@tonic-gate 						bp->b_flag |= MSGDELIM;
12547c478bd9Sstevel@tonic-gate 					if (msgnodata(bp))
12557c478bd9Sstevel@tonic-gate 						freemsg(bp);
12567c478bd9Sstevel@tonic-gate 					else
12577c478bd9Sstevel@tonic-gate 						putback(stp, q, bp, pri);
12587c478bd9Sstevel@tonic-gate 				}
12597c478bd9Sstevel@tonic-gate 			} else {
12607c478bd9Sstevel@tonic-gate 				/*
12617c478bd9Sstevel@tonic-gate 				 * Consumed the complete message.
12627c478bd9Sstevel@tonic-gate 				 * Move the MSG*MARKNEXT information
12637c478bd9Sstevel@tonic-gate 				 * to the stream head just in case
12647c478bd9Sstevel@tonic-gate 				 * the read queue becomes empty.
12657c478bd9Sstevel@tonic-gate 				 *
12667c478bd9Sstevel@tonic-gate 				 * If the stream head was at the mark
12677c478bd9Sstevel@tonic-gate 				 * (STRATMARK) before we dropped sd_lock above
12687c478bd9Sstevel@tonic-gate 				 * and some data was consumed then we have
12697c478bd9Sstevel@tonic-gate 				 * moved past the mark thus STRATMARK is
12707c478bd9Sstevel@tonic-gate 				 * cleared. However, if a message arrived in
12717c478bd9Sstevel@tonic-gate 				 * strrput during the copyout above causing
12727c478bd9Sstevel@tonic-gate 				 * STRATMARK to be set we can not clear that
12737c478bd9Sstevel@tonic-gate 				 * flag.
12747c478bd9Sstevel@tonic-gate 				 */
12757c478bd9Sstevel@tonic-gate 				if (mark &
12767c478bd9Sstevel@tonic-gate 				    (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
12777c478bd9Sstevel@tonic-gate 					if (mark & MSGMARKNEXT) {
12787c478bd9Sstevel@tonic-gate 						stp->sd_flag &= ~STRNOTATMARK;
12797c478bd9Sstevel@tonic-gate 						stp->sd_flag |= STRATMARK;
12807c478bd9Sstevel@tonic-gate 					} else if (mark & MSGNOTMARKNEXT) {
12817c478bd9Sstevel@tonic-gate 						stp->sd_flag &= ~STRATMARK;
12827c478bd9Sstevel@tonic-gate 						stp->sd_flag |= STRNOTATMARK;
12837c478bd9Sstevel@tonic-gate 					} else {
12847c478bd9Sstevel@tonic-gate 						stp->sd_flag &=
12857c478bd9Sstevel@tonic-gate 						    ~(STRATMARK|STRNOTATMARK);
12867c478bd9Sstevel@tonic-gate 					}
12877c478bd9Sstevel@tonic-gate 				} else if (rflg && (old_sd_flag & STRATMARK)) {
12887c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRATMARK;
12897c478bd9Sstevel@tonic-gate 				}
12907c478bd9Sstevel@tonic-gate 			}
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 			/*
12937c478bd9Sstevel@tonic-gate 			 * Check for signal messages at the front of the read
12947c478bd9Sstevel@tonic-gate 			 * queue and generate the signal(s) if appropriate.
12957c478bd9Sstevel@tonic-gate 			 * The only signal that can be on queue is M_SIG at
12967c478bd9Sstevel@tonic-gate 			 * this point.
12977c478bd9Sstevel@tonic-gate 			 */
12987c478bd9Sstevel@tonic-gate 			while ((((bp = q->q_first)) != NULL) &&
12997c478bd9Sstevel@tonic-gate 				(bp->b_datap->db_type == M_SIG)) {
13007c478bd9Sstevel@tonic-gate 				bp = getq_noenab(q);
13017c478bd9Sstevel@tonic-gate 				/*
13027c478bd9Sstevel@tonic-gate 				 * sd_lock is held so the content of the
13037c478bd9Sstevel@tonic-gate 				 * read queue can not change.
13047c478bd9Sstevel@tonic-gate 				 */
13057c478bd9Sstevel@tonic-gate 				ASSERT(bp != NULL &&
13067c478bd9Sstevel@tonic-gate 					bp->b_datap->db_type == M_SIG);
13077c478bd9Sstevel@tonic-gate 				strsignal_nolock(stp, *bp->b_rptr,
13087c478bd9Sstevel@tonic-gate 					(int32_t)bp->b_band);
13097c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
13107c478bd9Sstevel@tonic-gate 				freemsg(bp);
13117c478bd9Sstevel@tonic-gate 				if (STREAM_NEEDSERVICE(stp))
13127c478bd9Sstevel@tonic-gate 					stream_runservice(stp);
13137c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
13147c478bd9Sstevel@tonic-gate 			}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 			if ((uiop->uio_resid == 0) || (mark & _LASTMARK) ||
13177c478bd9Sstevel@tonic-gate 			    delim ||
13187c478bd9Sstevel@tonic-gate 			    (stp->sd_read_opt & (RD_MSGDIS|RD_MSGNODIS))) {
13197c478bd9Sstevel@tonic-gate 				goto oops;
13207c478bd9Sstevel@tonic-gate 			}
13217c478bd9Sstevel@tonic-gate 			continue;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 		case M_SIG:
13247c478bd9Sstevel@tonic-gate 			strsignal(stp, *bp->b_rptr, (int32_t)bp->b_band);
13257c478bd9Sstevel@tonic-gate 			freemsg(bp);
13267c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
13277c478bd9Sstevel@tonic-gate 			continue;
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 		case M_PROTO:
13307c478bd9Sstevel@tonic-gate 		case M_PCPROTO:
13317c478bd9Sstevel@tonic-gate 			/*
13327c478bd9Sstevel@tonic-gate 			 * Only data messages are readable.
13337c478bd9Sstevel@tonic-gate 			 * Any others generate an error, unless
13347c478bd9Sstevel@tonic-gate 			 * RD_PROTDIS or RD_PROTDAT is set.
13357c478bd9Sstevel@tonic-gate 			 */
13367c478bd9Sstevel@tonic-gate 			if (stp->sd_read_opt & RD_PROTDAT) {
13377c478bd9Sstevel@tonic-gate 				for (nbp = bp; nbp; nbp = nbp->b_next) {
13387c478bd9Sstevel@tonic-gate 				    if ((nbp->b_datap->db_type == M_PROTO) ||
13397c478bd9Sstevel@tonic-gate 					(nbp->b_datap->db_type == M_PCPROTO))
13407c478bd9Sstevel@tonic-gate 					nbp->b_datap->db_type = M_DATA;
13417c478bd9Sstevel@tonic-gate 				    else
13427c478bd9Sstevel@tonic-gate 					break;
13437c478bd9Sstevel@tonic-gate 				}
13447c478bd9Sstevel@tonic-gate 				/*
13457c478bd9Sstevel@tonic-gate 				 * clear stream head hi pri flag based on
13467c478bd9Sstevel@tonic-gate 				 * first message
13477c478bd9Sstevel@tonic-gate 				 */
13487c478bd9Sstevel@tonic-gate 				if (type == M_PCPROTO) {
13497c478bd9Sstevel@tonic-gate 					mutex_enter(&stp->sd_lock);
13507c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
13517c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
13527c478bd9Sstevel@tonic-gate 				}
13537c478bd9Sstevel@tonic-gate 				goto ismdata;
13547c478bd9Sstevel@tonic-gate 			} else if (stp->sd_read_opt & RD_PROTDIS) {
13557c478bd9Sstevel@tonic-gate 				/*
13567c478bd9Sstevel@tonic-gate 				 * discard non-data messages
13577c478bd9Sstevel@tonic-gate 				 */
13587c478bd9Sstevel@tonic-gate 				while (bp &&
13597c478bd9Sstevel@tonic-gate 				    ((bp->b_datap->db_type == M_PROTO) ||
13607c478bd9Sstevel@tonic-gate 				    (bp->b_datap->db_type == M_PCPROTO))) {
13617c478bd9Sstevel@tonic-gate 					nbp = unlinkb(bp);
13627c478bd9Sstevel@tonic-gate 					freeb(bp);
13637c478bd9Sstevel@tonic-gate 					bp = nbp;
13647c478bd9Sstevel@tonic-gate 				}
13657c478bd9Sstevel@tonic-gate 				/*
13667c478bd9Sstevel@tonic-gate 				 * clear stream head hi pri flag based on
13677c478bd9Sstevel@tonic-gate 				 * first message
13687c478bd9Sstevel@tonic-gate 				 */
13697c478bd9Sstevel@tonic-gate 				if (type == M_PCPROTO) {
13707c478bd9Sstevel@tonic-gate 					mutex_enter(&stp->sd_lock);
13717c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
13727c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
13737c478bd9Sstevel@tonic-gate 				}
13747c478bd9Sstevel@tonic-gate 				if (bp) {
13757c478bd9Sstevel@tonic-gate 					bp->b_band = pri;
13767c478bd9Sstevel@tonic-gate 					goto ismdata;
13777c478bd9Sstevel@tonic-gate 				} else {
13787c478bd9Sstevel@tonic-gate 					break;
13797c478bd9Sstevel@tonic-gate 				}
13807c478bd9Sstevel@tonic-gate 			}
13817c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
13827c478bd9Sstevel@tonic-gate 		case M_PASSFP:
13837c478bd9Sstevel@tonic-gate 			if ((bp->b_datap->db_type == M_PASSFP) &&
13847c478bd9Sstevel@tonic-gate 			    (stp->sd_read_opt & RD_PROTDIS)) {
13857c478bd9Sstevel@tonic-gate 				freemsg(bp);
13867c478bd9Sstevel@tonic-gate 				break;
13877c478bd9Sstevel@tonic-gate 			}
13887c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
13897c478bd9Sstevel@tonic-gate 			putback(stp, q, bp, pri);
13907c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
13917c478bd9Sstevel@tonic-gate 			if (rflg == 0)
13927c478bd9Sstevel@tonic-gate 				error = EBADMSG;
13937c478bd9Sstevel@tonic-gate 			goto oops1;
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 		default:
13967c478bd9Sstevel@tonic-gate 			/*
13977c478bd9Sstevel@tonic-gate 			 * Garbage on stream head read queue.
13987c478bd9Sstevel@tonic-gate 			 */
13997c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "bad %x found at stream head\n",
14007c478bd9Sstevel@tonic-gate 				bp->b_datap->db_type);
14017c478bd9Sstevel@tonic-gate 			freemsg(bp);
14027c478bd9Sstevel@tonic-gate 			goto oops1;
14037c478bd9Sstevel@tonic-gate 		}
14047c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate oops:
14077c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
14087c478bd9Sstevel@tonic-gate oops1:
14097c478bd9Sstevel@tonic-gate 	qbackenable(q, pri);
14107c478bd9Sstevel@tonic-gate 	return (error);
14117c478bd9Sstevel@tonic-gate #undef	_LASTMARK
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate /*
14157c478bd9Sstevel@tonic-gate  * Default processing of M_PROTO/M_PCPROTO messages.
14167c478bd9Sstevel@tonic-gate  * Determine which wakeups and signals are needed.
14177c478bd9Sstevel@tonic-gate  * This can be replaced by a user-specified procedure for kernel users
14187c478bd9Sstevel@tonic-gate  * of STREAMS.
14197c478bd9Sstevel@tonic-gate  */
14207c478bd9Sstevel@tonic-gate /* ARGSUSED */
14217c478bd9Sstevel@tonic-gate mblk_t *
14227c478bd9Sstevel@tonic-gate strrput_proto(vnode_t *vp, mblk_t *mp,
14237c478bd9Sstevel@tonic-gate     strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
14247c478bd9Sstevel@tonic-gate     strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
14257c478bd9Sstevel@tonic-gate {
14267c478bd9Sstevel@tonic-gate 	*wakeups = RSLEEP;
14277c478bd9Sstevel@tonic-gate 	*allmsgsigs = 0;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
14307c478bd9Sstevel@tonic-gate 	case M_PROTO:
14317c478bd9Sstevel@tonic-gate 		if (mp->b_band == 0) {
14327c478bd9Sstevel@tonic-gate 			*firstmsgsigs = S_INPUT | S_RDNORM;
14337c478bd9Sstevel@tonic-gate 			*pollwakeups = POLLIN | POLLRDNORM;
14347c478bd9Sstevel@tonic-gate 		} else {
14357c478bd9Sstevel@tonic-gate 			*firstmsgsigs = S_INPUT | S_RDBAND;
14367c478bd9Sstevel@tonic-gate 			*pollwakeups = POLLIN | POLLRDBAND;
14377c478bd9Sstevel@tonic-gate 		}
14387c478bd9Sstevel@tonic-gate 		break;
14397c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
14407c478bd9Sstevel@tonic-gate 		*firstmsgsigs = S_HIPRI;
14417c478bd9Sstevel@tonic-gate 		*pollwakeups = POLLPRI;
14427c478bd9Sstevel@tonic-gate 		break;
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 	return (mp);
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate /*
14487c478bd9Sstevel@tonic-gate  * Default processing of everything but M_DATA, M_PROTO, M_PCPROTO and
14497c478bd9Sstevel@tonic-gate  * M_PASSFP messages.
14507c478bd9Sstevel@tonic-gate  * Determine which wakeups and signals are needed.
14517c478bd9Sstevel@tonic-gate  * This can be replaced by a user-specified procedure for kernel users
14527c478bd9Sstevel@tonic-gate  * of STREAMS.
14537c478bd9Sstevel@tonic-gate  */
14547c478bd9Sstevel@tonic-gate /* ARGSUSED */
14557c478bd9Sstevel@tonic-gate mblk_t *
14567c478bd9Sstevel@tonic-gate strrput_misc(vnode_t *vp, mblk_t *mp,
14577c478bd9Sstevel@tonic-gate     strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
14587c478bd9Sstevel@tonic-gate     strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
14597c478bd9Sstevel@tonic-gate {
14607c478bd9Sstevel@tonic-gate 	*wakeups = 0;
14617c478bd9Sstevel@tonic-gate 	*firstmsgsigs = 0;
14627c478bd9Sstevel@tonic-gate 	*allmsgsigs = 0;
14637c478bd9Sstevel@tonic-gate 	*pollwakeups = 0;
14647c478bd9Sstevel@tonic-gate 	return (mp);
14657c478bd9Sstevel@tonic-gate }
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate /*
14687c478bd9Sstevel@tonic-gate  * Stream read put procedure.  Called from downstream driver/module
14697c478bd9Sstevel@tonic-gate  * with messages for the stream head.  Data, protocol, and in-stream
14707c478bd9Sstevel@tonic-gate  * signal messages are placed on the queue, others are handled directly.
14717c478bd9Sstevel@tonic-gate  */
14727c478bd9Sstevel@tonic-gate int
14737c478bd9Sstevel@tonic-gate strrput(queue_t *q, mblk_t *bp)
14747c478bd9Sstevel@tonic-gate {
14757c478bd9Sstevel@tonic-gate 	struct stdata	*stp;
14767c478bd9Sstevel@tonic-gate 	ulong_t		rput_opt;
14777c478bd9Sstevel@tonic-gate 	strwakeup_t	wakeups;
14787c478bd9Sstevel@tonic-gate 	strsigset_t	firstmsgsigs;	/* Signals if first message on queue */
14797c478bd9Sstevel@tonic-gate 	strsigset_t	allmsgsigs;	/* Signals for all messages */
14807c478bd9Sstevel@tonic-gate 	strsigset_t	signals;	/* Signals events to generate */
14817c478bd9Sstevel@tonic-gate 	strpollset_t	pollwakeups;
14827c478bd9Sstevel@tonic-gate 	mblk_t		*nextbp;
14837c478bd9Sstevel@tonic-gate 	uchar_t		band = 0;
14847c478bd9Sstevel@tonic-gate 	int		hipri_sig;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	stp = (struct stdata *)q->q_ptr;
14877c478bd9Sstevel@tonic-gate 	/*
14887c478bd9Sstevel@tonic-gate 	 * Use rput_opt for optimized access to the SR_ flags except
14897c478bd9Sstevel@tonic-gate 	 * SR_POLLIN. That flag has to be checked under sd_lock since it
14907c478bd9Sstevel@tonic-gate 	 * is modified by strpoll().
14917c478bd9Sstevel@tonic-gate 	 */
14927c478bd9Sstevel@tonic-gate 	rput_opt = stp->sd_rput_opt;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	ASSERT(qclaimed(q));
14957c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_ENTER,
14967c478bd9Sstevel@tonic-gate 		"strrput called with message type:q %p bp %p", q, bp);
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	/*
14997c478bd9Sstevel@tonic-gate 	 * Perform initial processing and pass to the parameterized functions.
15007c478bd9Sstevel@tonic-gate 	 */
15017c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_next == NULL);
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	switch (bp->b_datap->db_type) {
15047c478bd9Sstevel@tonic-gate 	case M_DATA:
15057c478bd9Sstevel@tonic-gate 		/*
15067c478bd9Sstevel@tonic-gate 		 * sockfs is the only consumer of STREOF and when it is set,
15077c478bd9Sstevel@tonic-gate 		 * it implies that the receiver is not interested in receiving
15087c478bd9Sstevel@tonic-gate 		 * any more data, hence the mblk is freed to prevent unnecessary
15097c478bd9Sstevel@tonic-gate 		 * message queueing at the stream head.
15107c478bd9Sstevel@tonic-gate 		 */
15117c478bd9Sstevel@tonic-gate 		if (stp->sd_flag == STREOF) {
15127c478bd9Sstevel@tonic-gate 			freemsg(bp);
15137c478bd9Sstevel@tonic-gate 			return (0);
15147c478bd9Sstevel@tonic-gate 		}
15157c478bd9Sstevel@tonic-gate 		if ((rput_opt & SR_IGN_ZEROLEN) &&
15167c478bd9Sstevel@tonic-gate 		    bp->b_rptr == bp->b_wptr && msgnodata(bp)) {
15177c478bd9Sstevel@tonic-gate 			/*
15187c478bd9Sstevel@tonic-gate 			 * Ignore zero-length M_DATA messages. These might be
15197c478bd9Sstevel@tonic-gate 			 * generated by some transports.
15207c478bd9Sstevel@tonic-gate 			 * The zero-length M_DATA messages, even if they
15217c478bd9Sstevel@tonic-gate 			 * are ignored, should effect the atmark tracking and
15227c478bd9Sstevel@tonic-gate 			 * should wake up a thread sleeping in strwaitmark.
15237c478bd9Sstevel@tonic-gate 			 */
15247c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
15257c478bd9Sstevel@tonic-gate 			if (bp->b_flag & MSGMARKNEXT) {
15267c478bd9Sstevel@tonic-gate 				/*
15277c478bd9Sstevel@tonic-gate 				 * Record the position of the mark either
15287c478bd9Sstevel@tonic-gate 				 * in q_last or in STRATMARK.
15297c478bd9Sstevel@tonic-gate 				 */
15307c478bd9Sstevel@tonic-gate 				if (q->q_last != NULL) {
15317c478bd9Sstevel@tonic-gate 					q->q_last->b_flag &= ~MSGNOTMARKNEXT;
15327c478bd9Sstevel@tonic-gate 					q->q_last->b_flag |= MSGMARKNEXT;
15337c478bd9Sstevel@tonic-gate 				} else {
15347c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRNOTATMARK;
15357c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRATMARK;
15367c478bd9Sstevel@tonic-gate 				}
15377c478bd9Sstevel@tonic-gate 			} else if (bp->b_flag & MSGNOTMARKNEXT) {
15387c478bd9Sstevel@tonic-gate 				/*
15397c478bd9Sstevel@tonic-gate 				 * Record that this is not the position of
15407c478bd9Sstevel@tonic-gate 				 * the mark either in q_last or in
15417c478bd9Sstevel@tonic-gate 				 * STRNOTATMARK.
15427c478bd9Sstevel@tonic-gate 				 */
15437c478bd9Sstevel@tonic-gate 				if (q->q_last != NULL) {
15447c478bd9Sstevel@tonic-gate 					q->q_last->b_flag &= ~MSGMARKNEXT;
15457c478bd9Sstevel@tonic-gate 					q->q_last->b_flag |= MSGNOTMARKNEXT;
15467c478bd9Sstevel@tonic-gate 				} else {
15477c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRATMARK;
15487c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRNOTATMARK;
15497c478bd9Sstevel@tonic-gate 				}
15507c478bd9Sstevel@tonic-gate 			}
15517c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & RSLEEP) {
15527c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~RSLEEP;
15537c478bd9Sstevel@tonic-gate 				cv_broadcast(&q->q_wait);
15547c478bd9Sstevel@tonic-gate 			}
15557c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
15567c478bd9Sstevel@tonic-gate 			freemsg(bp);
15577c478bd9Sstevel@tonic-gate 			return (0);
15587c478bd9Sstevel@tonic-gate 		}
15597c478bd9Sstevel@tonic-gate 		wakeups = RSLEEP;
15607c478bd9Sstevel@tonic-gate 		if (bp->b_band == 0) {
15617c478bd9Sstevel@tonic-gate 			firstmsgsigs = S_INPUT | S_RDNORM;
15627c478bd9Sstevel@tonic-gate 			pollwakeups = POLLIN | POLLRDNORM;
15637c478bd9Sstevel@tonic-gate 		} else {
15647c478bd9Sstevel@tonic-gate 			firstmsgsigs = S_INPUT | S_RDBAND;
15657c478bd9Sstevel@tonic-gate 			pollwakeups = POLLIN | POLLRDBAND;
15667c478bd9Sstevel@tonic-gate 		}
15677c478bd9Sstevel@tonic-gate 		if (rput_opt & SR_SIGALLDATA)
15687c478bd9Sstevel@tonic-gate 			allmsgsigs = firstmsgsigs;
15697c478bd9Sstevel@tonic-gate 		else
15707c478bd9Sstevel@tonic-gate 			allmsgsigs = 0;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
15737c478bd9Sstevel@tonic-gate 		if ((rput_opt & SR_CONSOL_DATA) &&
15747c478bd9Sstevel@tonic-gate 		    (bp->b_flag & (MSGMARK|MSGDELIM)) == 0) {
15757c478bd9Sstevel@tonic-gate 			/*
15767c478bd9Sstevel@tonic-gate 			 * Consolidate on M_DATA message onto an M_DATA,
15777c478bd9Sstevel@tonic-gate 			 * M_PROTO, or M_PCPROTO by merging it with q_last.
15787c478bd9Sstevel@tonic-gate 			 * The consolidation does not take place if
15797c478bd9Sstevel@tonic-gate 			 * the old message is marked with either of the
15807c478bd9Sstevel@tonic-gate 			 * marks or the delim flag or if the new
15817c478bd9Sstevel@tonic-gate 			 * message is marked with MSGMARK. The MSGMARK
15827c478bd9Sstevel@tonic-gate 			 * check is needed to handle the odd semantics of
15837c478bd9Sstevel@tonic-gate 			 * MSGMARK where essentially the whole message
15847c478bd9Sstevel@tonic-gate 			 * is to be treated as marked.
15857c478bd9Sstevel@tonic-gate 			 * Carry any MSGMARKNEXT  and MSGNOTMARKNEXT from the
15867c478bd9Sstevel@tonic-gate 			 * new message to the front of the b_cont chain.
15877c478bd9Sstevel@tonic-gate 			 */
15887c478bd9Sstevel@tonic-gate 			mblk_t *lbp;
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 			lbp = q->q_last;
15917c478bd9Sstevel@tonic-gate 			if (lbp != NULL &&
15927c478bd9Sstevel@tonic-gate 			    (lbp->b_datap->db_type == M_DATA ||
15937c478bd9Sstevel@tonic-gate 			    lbp->b_datap->db_type == M_PROTO ||
15947c478bd9Sstevel@tonic-gate 			    lbp->b_datap->db_type == M_PCPROTO) &&
15957c478bd9Sstevel@tonic-gate 			    !(lbp->b_flag & (MSGDELIM|MSGMARK|
15967c478bd9Sstevel@tonic-gate 			    MSGMARKNEXT))) {
15977c478bd9Sstevel@tonic-gate 				rmvq_noenab(q, lbp);
15987c478bd9Sstevel@tonic-gate 				/*
15997c478bd9Sstevel@tonic-gate 				 * The first message in the b_cont list
16007c478bd9Sstevel@tonic-gate 				 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
16017c478bd9Sstevel@tonic-gate 				 * We need to handle the case where we
16027c478bd9Sstevel@tonic-gate 				 * are appending
16037c478bd9Sstevel@tonic-gate 				 *
16047c478bd9Sstevel@tonic-gate 				 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
16057c478bd9Sstevel@tonic-gate 				 * 2) a MSGMARKNEXT to a plain message.
16067c478bd9Sstevel@tonic-gate 				 * 3) a MSGNOTMARKNEXT to a plain message
16077c478bd9Sstevel@tonic-gate 				 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
16087c478bd9Sstevel@tonic-gate 				 *    message.
16097c478bd9Sstevel@tonic-gate 				 *
16107c478bd9Sstevel@tonic-gate 				 * Thus we never append a MSGMARKNEXT or
16117c478bd9Sstevel@tonic-gate 				 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
16127c478bd9Sstevel@tonic-gate 				 */
16137c478bd9Sstevel@tonic-gate 				if (bp->b_flag & MSGMARKNEXT) {
16147c478bd9Sstevel@tonic-gate 					lbp->b_flag |= MSGMARKNEXT;
16157c478bd9Sstevel@tonic-gate 					lbp->b_flag &= ~MSGNOTMARKNEXT;
16167c478bd9Sstevel@tonic-gate 					bp->b_flag &= ~MSGMARKNEXT;
16177c478bd9Sstevel@tonic-gate 				} else if (bp->b_flag & MSGNOTMARKNEXT) {
16187c478bd9Sstevel@tonic-gate 					lbp->b_flag |= MSGNOTMARKNEXT;
16197c478bd9Sstevel@tonic-gate 					bp->b_flag &= ~MSGNOTMARKNEXT;
16207c478bd9Sstevel@tonic-gate 				}
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 				linkb(lbp, bp);
16237c478bd9Sstevel@tonic-gate 				bp = lbp;
16247c478bd9Sstevel@tonic-gate 				/*
16257c478bd9Sstevel@tonic-gate 				 * The new message logically isn't the first
16267c478bd9Sstevel@tonic-gate 				 * even though the q_first check below thinks
16277c478bd9Sstevel@tonic-gate 				 * it is. Clear the firstmsgsigs to make it
16287c478bd9Sstevel@tonic-gate 				 * not appear to be first.
16297c478bd9Sstevel@tonic-gate 				 */
16307c478bd9Sstevel@tonic-gate 				firstmsgsigs = 0;
16317c478bd9Sstevel@tonic-gate 			}
16327c478bd9Sstevel@tonic-gate 		}
16337c478bd9Sstevel@tonic-gate 		break;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	case M_PASSFP:
16367c478bd9Sstevel@tonic-gate 		wakeups = RSLEEP;
16377c478bd9Sstevel@tonic-gate 		allmsgsigs = 0;
16387c478bd9Sstevel@tonic-gate 		if (bp->b_band == 0) {
16397c478bd9Sstevel@tonic-gate 			firstmsgsigs = S_INPUT | S_RDNORM;
16407c478bd9Sstevel@tonic-gate 			pollwakeups = POLLIN | POLLRDNORM;
16417c478bd9Sstevel@tonic-gate 		} else {
16427c478bd9Sstevel@tonic-gate 			firstmsgsigs = S_INPUT | S_RDBAND;
16437c478bd9Sstevel@tonic-gate 			pollwakeups = POLLIN | POLLRDBAND;
16447c478bd9Sstevel@tonic-gate 		}
16457c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
16467c478bd9Sstevel@tonic-gate 		break;
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	case M_PROTO:
16497c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
16507c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_rprotofunc != NULL);
16517c478bd9Sstevel@tonic-gate 		bp = (stp->sd_rprotofunc)(stp->sd_vnode, bp,
16527c478bd9Sstevel@tonic-gate 			&wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
16537c478bd9Sstevel@tonic-gate #define	ALLSIG	(S_INPUT|S_HIPRI|S_OUTPUT|S_MSG|S_ERROR|S_HANGUP|S_RDNORM|\
16547c478bd9Sstevel@tonic-gate 		S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)
16557c478bd9Sstevel@tonic-gate #define	ALLPOLL	(POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLWRNORM|POLLRDBAND|\
16567c478bd9Sstevel@tonic-gate 		POLLWRBAND)
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 		ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
16597c478bd9Sstevel@tonic-gate 		ASSERT((firstmsgsigs & ~ALLSIG) == 0);
16607c478bd9Sstevel@tonic-gate 		ASSERT((allmsgsigs & ~ALLSIG) == 0);
16617c478bd9Sstevel@tonic-gate 		ASSERT((pollwakeups & ~ALLPOLL) == 0);
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
16647c478bd9Sstevel@tonic-gate 		break;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	default:
16677c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_rmiscfunc != NULL);
16687c478bd9Sstevel@tonic-gate 		bp = (stp->sd_rmiscfunc)(stp->sd_vnode, bp,
16697c478bd9Sstevel@tonic-gate 			&wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
16707c478bd9Sstevel@tonic-gate 		ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
16717c478bd9Sstevel@tonic-gate 		ASSERT((firstmsgsigs & ~ALLSIG) == 0);
16727c478bd9Sstevel@tonic-gate 		ASSERT((allmsgsigs & ~ALLSIG) == 0);
16737c478bd9Sstevel@tonic-gate 		ASSERT((pollwakeups & ~ALLPOLL) == 0);
16747c478bd9Sstevel@tonic-gate #undef	ALLSIG
16757c478bd9Sstevel@tonic-gate #undef	ALLPOLL
16767c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
16777c478bd9Sstevel@tonic-gate 		break;
16787c478bd9Sstevel@tonic-gate 	}
16797c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	/* By default generate superset of signals */
16827c478bd9Sstevel@tonic-gate 	signals = (firstmsgsigs | allmsgsigs);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	/*
16857c478bd9Sstevel@tonic-gate 	 * The  proto and misc functions can return multiple messages
16867c478bd9Sstevel@tonic-gate 	 * as a b_next chain. Such messages are processed separately.
16877c478bd9Sstevel@tonic-gate 	 */
16887c478bd9Sstevel@tonic-gate one_more:
16897c478bd9Sstevel@tonic-gate 	hipri_sig = 0;
16907c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
16917c478bd9Sstevel@tonic-gate 		nextbp = NULL;
16927c478bd9Sstevel@tonic-gate 	} else {
16937c478bd9Sstevel@tonic-gate 		nextbp = bp->b_next;
16947c478bd9Sstevel@tonic-gate 		bp->b_next = NULL;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 		switch (bp->b_datap->db_type) {
16977c478bd9Sstevel@tonic-gate 		case M_PCPROTO:
16987c478bd9Sstevel@tonic-gate 			/*
16997c478bd9Sstevel@tonic-gate 			 * Only one priority protocol message is allowed at the
17007c478bd9Sstevel@tonic-gate 			 * stream head at a time.
17017c478bd9Sstevel@tonic-gate 			 */
17027c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & STRPRI) {
17037c478bd9Sstevel@tonic-gate 				TRACE_0(TR_FAC_STREAMS_FR, TR_STRRPUT_PROTERR,
17047c478bd9Sstevel@tonic-gate 				    "M_PCPROTO already at head");
17057c478bd9Sstevel@tonic-gate 				freemsg(bp);
17067c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
17077c478bd9Sstevel@tonic-gate 				goto done;
17087c478bd9Sstevel@tonic-gate 			}
17097c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRPRI;
17107c478bd9Sstevel@tonic-gate 			hipri_sig = 1;
17117c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
17127c478bd9Sstevel@tonic-gate 		case M_DATA:
17137c478bd9Sstevel@tonic-gate 		case M_PROTO:
17147c478bd9Sstevel@tonic-gate 		case M_PASSFP:
17157c478bd9Sstevel@tonic-gate 			band = bp->b_band;
17167c478bd9Sstevel@tonic-gate 			/*
17177c478bd9Sstevel@tonic-gate 			 * Marking doesn't work well when messages
17187c478bd9Sstevel@tonic-gate 			 * are marked in more than one band.  We only
17197c478bd9Sstevel@tonic-gate 			 * remember the last message received, even if
17207c478bd9Sstevel@tonic-gate 			 * it is placed on the queue ahead of other
17217c478bd9Sstevel@tonic-gate 			 * marked messages.
17227c478bd9Sstevel@tonic-gate 			 */
17237c478bd9Sstevel@tonic-gate 			if (bp->b_flag & MSGMARK)
17247c478bd9Sstevel@tonic-gate 				stp->sd_mark = bp;
17257c478bd9Sstevel@tonic-gate 			(void) putq(q, bp);
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 			/*
17287c478bd9Sstevel@tonic-gate 			 * If message is a PCPROTO message, always use
17297c478bd9Sstevel@tonic-gate 			 * firstmsgsigs to determine if a signal should be
17307c478bd9Sstevel@tonic-gate 			 * sent as strrput is the only place to send
17317c478bd9Sstevel@tonic-gate 			 * signals for PCPROTO. Other messages are based on
17327c478bd9Sstevel@tonic-gate 			 * the STRGETINPROG flag. The flag determines if
17337c478bd9Sstevel@tonic-gate 			 * strrput or (k)strgetmsg will be responsible for
17347c478bd9Sstevel@tonic-gate 			 * sending the signals, in the firstmsgsigs case.
17357c478bd9Sstevel@tonic-gate 			 */
17367c478bd9Sstevel@tonic-gate 			if ((hipri_sig == 1) ||
17377c478bd9Sstevel@tonic-gate 			    (((stp->sd_flag & STRGETINPROG) == 0) &&
17387c478bd9Sstevel@tonic-gate 			    (q->q_first == bp)))
17397c478bd9Sstevel@tonic-gate 				signals = (firstmsgsigs | allmsgsigs);
17407c478bd9Sstevel@tonic-gate 			else
17417c478bd9Sstevel@tonic-gate 				signals = allmsgsigs;
17427c478bd9Sstevel@tonic-gate 			break;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		default:
17457c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
17467c478bd9Sstevel@tonic-gate 			(void) strrput_nondata(q, bp);
17477c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
17487c478bd9Sstevel@tonic-gate 			break;
17497c478bd9Sstevel@tonic-gate 		}
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
17527c478bd9Sstevel@tonic-gate 	/*
17537c478bd9Sstevel@tonic-gate 	 * Wake sleeping read/getmsg and cancel deferred wakeup
17547c478bd9Sstevel@tonic-gate 	 */
17557c478bd9Sstevel@tonic-gate 	if (wakeups & RSLEEP)
17567c478bd9Sstevel@tonic-gate 		stp->sd_wakeq &= ~RSLEEP;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	wakeups &= stp->sd_flag;
17597c478bd9Sstevel@tonic-gate 	if (wakeups & RSLEEP) {
17607c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~RSLEEP;
17617c478bd9Sstevel@tonic-gate 		cv_broadcast(&q->q_wait);
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate 	if (wakeups & WSLEEP) {
17647c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~WSLEEP;
17657c478bd9Sstevel@tonic-gate 		cv_broadcast(&_WR(q)->q_wait);
17667c478bd9Sstevel@tonic-gate 	}
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	if (pollwakeups != 0) {
17697c478bd9Sstevel@tonic-gate 		if (pollwakeups == (POLLIN | POLLRDNORM)) {
17707c478bd9Sstevel@tonic-gate 			/*
17717c478bd9Sstevel@tonic-gate 			 * Can't use rput_opt since it was not
17727c478bd9Sstevel@tonic-gate 			 * read when sd_lock was held and SR_POLLIN is changed
17737c478bd9Sstevel@tonic-gate 			 * by strpoll() under sd_lock.
17747c478bd9Sstevel@tonic-gate 			 */
17757c478bd9Sstevel@tonic-gate 			if (!(stp->sd_rput_opt & SR_POLLIN))
17767c478bd9Sstevel@tonic-gate 				goto no_pollwake;
17777c478bd9Sstevel@tonic-gate 			stp->sd_rput_opt &= ~SR_POLLIN;
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
17807c478bd9Sstevel@tonic-gate 		pollwakeup(&stp->sd_pollist, pollwakeups);
17817c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
17827c478bd9Sstevel@tonic-gate 	}
17837c478bd9Sstevel@tonic-gate no_pollwake:
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	/*
17867c478bd9Sstevel@tonic-gate 	 * strsendsig can handle multiple signals with a
17877c478bd9Sstevel@tonic-gate 	 * single call.
17887c478bd9Sstevel@tonic-gate 	 */
17897c478bd9Sstevel@tonic-gate 	if (stp->sd_sigflags & signals)
17907c478bd9Sstevel@tonic-gate 		strsendsig(stp->sd_siglist, signals, band, 0);
17917c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate done:
17957c478bd9Sstevel@tonic-gate 	if (nextbp == NULL)
17967c478bd9Sstevel@tonic-gate 		return (0);
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	/*
17997c478bd9Sstevel@tonic-gate 	 * Any signals were handled the first time.
18007c478bd9Sstevel@tonic-gate 	 * Wakeups and pollwakeups are redone to avoid any race
18017c478bd9Sstevel@tonic-gate 	 * conditions - all the messages are not queued until the
18027c478bd9Sstevel@tonic-gate 	 * last message has been processed by strrput.
18037c478bd9Sstevel@tonic-gate 	 */
18047c478bd9Sstevel@tonic-gate 	bp = nextbp;
18057c478bd9Sstevel@tonic-gate 	signals = firstmsgsigs = allmsgsigs = 0;
18067c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
18077c478bd9Sstevel@tonic-gate 	goto one_more;
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate static void
18117c478bd9Sstevel@tonic-gate log_dupioc(queue_t *rq, mblk_t *bp)
18127c478bd9Sstevel@tonic-gate {
18137c478bd9Sstevel@tonic-gate 	queue_t *wq, *qp;
18147c478bd9Sstevel@tonic-gate 	char *modnames, *mnp, *dname;
18157c478bd9Sstevel@tonic-gate 	size_t maxmodstr;
18167c478bd9Sstevel@tonic-gate 	boolean_t islast;
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	/*
18197c478bd9Sstevel@tonic-gate 	 * Allocate a buffer large enough to hold the names of nstrpush modules
18207c478bd9Sstevel@tonic-gate 	 * and one driver, with spaces between and NUL terminator.  If we can't
18217c478bd9Sstevel@tonic-gate 	 * get memory, then we'll just log the driver name.
18227c478bd9Sstevel@tonic-gate 	 */
18237c478bd9Sstevel@tonic-gate 	maxmodstr = nstrpush * (FMNAMESZ + 1);
18247c478bd9Sstevel@tonic-gate 	mnp = modnames = kmem_alloc(maxmodstr, KM_NOSLEEP);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/* march down write side to print log message down to the driver */
18277c478bd9Sstevel@tonic-gate 	wq = WR(rq);
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	/* make sure q_next doesn't shift around while we're grabbing data */
18307c478bd9Sstevel@tonic-gate 	claimstr(wq);
18317c478bd9Sstevel@tonic-gate 	qp = wq->q_next;
18327c478bd9Sstevel@tonic-gate 	do {
18337c478bd9Sstevel@tonic-gate 		if ((dname = qp->q_qinfo->qi_minfo->mi_idname) == NULL)
18347c478bd9Sstevel@tonic-gate 			dname = "?";
18357c478bd9Sstevel@tonic-gate 		islast = !SAMESTR(qp) || qp->q_next == NULL;
18367c478bd9Sstevel@tonic-gate 		if (modnames == NULL) {
18377c478bd9Sstevel@tonic-gate 			/*
18387c478bd9Sstevel@tonic-gate 			 * If we don't have memory, then get the driver name in
18397c478bd9Sstevel@tonic-gate 			 * the log where we can see it.  Note that memory
18407c478bd9Sstevel@tonic-gate 			 * pressure is a possible cause of these sorts of bugs.
18417c478bd9Sstevel@tonic-gate 			 */
18427c478bd9Sstevel@tonic-gate 			if (islast) {
18437c478bd9Sstevel@tonic-gate 				modnames = dname;
18447c478bd9Sstevel@tonic-gate 				maxmodstr = 0;
18457c478bd9Sstevel@tonic-gate 			}
18467c478bd9Sstevel@tonic-gate 		} else {
18477c478bd9Sstevel@tonic-gate 			mnp += snprintf(mnp, FMNAMESZ + 1, "%s", dname);
18487c478bd9Sstevel@tonic-gate 			if (!islast)
18497c478bd9Sstevel@tonic-gate 				*mnp++ = ' ';
18507c478bd9Sstevel@tonic-gate 		}
18517c478bd9Sstevel@tonic-gate 		qp = qp->q_next;
18527c478bd9Sstevel@tonic-gate 	} while (!islast);
18537c478bd9Sstevel@tonic-gate 	releasestr(wq);
18547c478bd9Sstevel@tonic-gate 	/* Cannot happen unless stream head is corrupt. */
18557c478bd9Sstevel@tonic-gate 	ASSERT(modnames != NULL);
18567c478bd9Sstevel@tonic-gate 	(void) strlog(rq->q_qinfo->qi_minfo->mi_idnum, 0, 1,
18577c478bd9Sstevel@tonic-gate 	    SL_CONSOLE|SL_TRACE|SL_ERROR,
18587c478bd9Sstevel@tonic-gate 	    "Warning: stream %p received duplicate %X M_IOC%s; module list: %s",
18597c478bd9Sstevel@tonic-gate 	    rq->q_ptr, ((struct iocblk *)bp->b_rptr)->ioc_cmd,
18607c478bd9Sstevel@tonic-gate 	    (DB_TYPE(bp) == M_IOCACK ? "ACK" : "NAK"), modnames);
18617c478bd9Sstevel@tonic-gate 	if (maxmodstr != 0)
18627c478bd9Sstevel@tonic-gate 		kmem_free(modnames, maxmodstr);
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate int
18667c478bd9Sstevel@tonic-gate strrput_nondata(queue_t *q, mblk_t *bp)
18677c478bd9Sstevel@tonic-gate {
18687c478bd9Sstevel@tonic-gate 	struct stdata *stp;
18697c478bd9Sstevel@tonic-gate 	struct iocblk *iocbp;
18707c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
18717c478bd9Sstevel@tonic-gate 	struct copyreq *reqp;
18727c478bd9Sstevel@tonic-gate 	struct copyresp *resp;
18737c478bd9Sstevel@tonic-gate 	unsigned char bpri;
18747c478bd9Sstevel@tonic-gate 	unsigned char  flushed_already = 0;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	stp = (struct stdata *)q->q_ptr;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	ASSERT(!(stp->sd_flag & STPLEX));
18797c478bd9Sstevel@tonic-gate 	ASSERT(qclaimed(q));
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	switch (bp->b_datap->db_type) {
18827c478bd9Sstevel@tonic-gate 	case M_ERROR:
18837c478bd9Sstevel@tonic-gate 		/*
18847c478bd9Sstevel@tonic-gate 		 * An error has occurred downstream, the errno is in the first
18857c478bd9Sstevel@tonic-gate 		 * bytes of the message.
18867c478bd9Sstevel@tonic-gate 		 */
18877c478bd9Sstevel@tonic-gate 		if ((bp->b_wptr - bp->b_rptr) == 2) {	/* New flavor */
18887c478bd9Sstevel@tonic-gate 			unsigned char rw = 0;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
18917c478bd9Sstevel@tonic-gate 			if (*bp->b_rptr != NOERROR) {	/* read error */
18927c478bd9Sstevel@tonic-gate 				if (*bp->b_rptr != 0) {
18937c478bd9Sstevel@tonic-gate 					if (stp->sd_flag & STRDERR)
18947c478bd9Sstevel@tonic-gate 						flushed_already |= FLUSHR;
18957c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRDERR;
18967c478bd9Sstevel@tonic-gate 					rw |= FLUSHR;
18977c478bd9Sstevel@tonic-gate 				} else {
18987c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRDERR;
18997c478bd9Sstevel@tonic-gate 				}
19007c478bd9Sstevel@tonic-gate 				stp->sd_rerror = *bp->b_rptr;
19017c478bd9Sstevel@tonic-gate 			}
19027c478bd9Sstevel@tonic-gate 			bp->b_rptr++;
19037c478bd9Sstevel@tonic-gate 			if (*bp->b_rptr != NOERROR) {	/* write error */
19047c478bd9Sstevel@tonic-gate 				if (*bp->b_rptr != 0) {
19057c478bd9Sstevel@tonic-gate 					if (stp->sd_flag & STWRERR)
19067c478bd9Sstevel@tonic-gate 						flushed_already |= FLUSHW;
19077c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STWRERR;
19087c478bd9Sstevel@tonic-gate 					rw |= FLUSHW;
19097c478bd9Sstevel@tonic-gate 				} else {
19107c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STWRERR;
19117c478bd9Sstevel@tonic-gate 				}
19127c478bd9Sstevel@tonic-gate 				stp->sd_werror = *bp->b_rptr;
19137c478bd9Sstevel@tonic-gate 			}
19147c478bd9Sstevel@tonic-gate 			if (rw) {
19157c478bd9Sstevel@tonic-gate 				TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_WAKE,
19167c478bd9Sstevel@tonic-gate 					"strrput cv_broadcast:q %p, bp %p",
19177c478bd9Sstevel@tonic-gate 					q, bp);
19187c478bd9Sstevel@tonic-gate 				cv_broadcast(&q->q_wait); /* readers */
19197c478bd9Sstevel@tonic-gate 				cv_broadcast(&_WR(q)->q_wait); /* writers */
19207c478bd9Sstevel@tonic-gate 				cv_broadcast(&stp->sd_monitor); /* ioctllers */
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
19237c478bd9Sstevel@tonic-gate 				pollwakeup(&stp->sd_pollist, POLLERR);
19247c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 				if (stp->sd_sigflags & S_ERROR)
19277c478bd9Sstevel@tonic-gate 					strsendsig(stp->sd_siglist, S_ERROR, 0,
19287c478bd9Sstevel@tonic-gate 					    ((rw & FLUSHR) ? stp->sd_rerror :
19297c478bd9Sstevel@tonic-gate 					    stp->sd_werror));
19307c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
19317c478bd9Sstevel@tonic-gate 				/*
19327c478bd9Sstevel@tonic-gate 				 * Send the M_FLUSH only
19337c478bd9Sstevel@tonic-gate 				 * for the first M_ERROR
19347c478bd9Sstevel@tonic-gate 				 * message on the stream
19357c478bd9Sstevel@tonic-gate 				 */
19367c478bd9Sstevel@tonic-gate 				if (flushed_already == rw) {
19377c478bd9Sstevel@tonic-gate 					freemsg(bp);
19387c478bd9Sstevel@tonic-gate 					return (0);
19397c478bd9Sstevel@tonic-gate 				}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 				bp->b_datap->db_type = M_FLUSH;
19427c478bd9Sstevel@tonic-gate 				*bp->b_rptr = rw;
19437c478bd9Sstevel@tonic-gate 				bp->b_wptr = bp->b_rptr + 1;
19447c478bd9Sstevel@tonic-gate 				/*
19457c478bd9Sstevel@tonic-gate 				 * Protect against the driver
19467c478bd9Sstevel@tonic-gate 				 * passing up messages after
19477c478bd9Sstevel@tonic-gate 				 * it has done a qprocsoff
19487c478bd9Sstevel@tonic-gate 				 */
19497c478bd9Sstevel@tonic-gate 				if (_OTHERQ(q)->q_next == NULL)
19507c478bd9Sstevel@tonic-gate 					freemsg(bp);
19517c478bd9Sstevel@tonic-gate 				else
19527c478bd9Sstevel@tonic-gate 					qreply(q, bp);
19537c478bd9Sstevel@tonic-gate 				return (0);
19547c478bd9Sstevel@tonic-gate 			} else
19557c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
19567c478bd9Sstevel@tonic-gate 		} else if (*bp->b_rptr != 0) {		/* Old flavor */
19577c478bd9Sstevel@tonic-gate 				if (stp->sd_flag & (STRDERR|STWRERR))
19587c478bd9Sstevel@tonic-gate 					flushed_already = FLUSHRW;
19597c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
19607c478bd9Sstevel@tonic-gate 				stp->sd_flag |= (STRDERR|STWRERR);
19617c478bd9Sstevel@tonic-gate 				stp->sd_rerror = *bp->b_rptr;
19627c478bd9Sstevel@tonic-gate 				stp->sd_werror = *bp->b_rptr;
19637c478bd9Sstevel@tonic-gate 				TRACE_2(TR_FAC_STREAMS_FR,
19647c478bd9Sstevel@tonic-gate 					TR_STRRPUT_WAKE2,
19657c478bd9Sstevel@tonic-gate 					"strrput wakeup #2:q %p, bp %p", q, bp);
19667c478bd9Sstevel@tonic-gate 				cv_broadcast(&q->q_wait); /* the readers */
19677c478bd9Sstevel@tonic-gate 				cv_broadcast(&_WR(q)->q_wait); /* the writers */
19687c478bd9Sstevel@tonic-gate 				cv_broadcast(&stp->sd_monitor); /* ioctllers */
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
19717c478bd9Sstevel@tonic-gate 				pollwakeup(&stp->sd_pollist, POLLERR);
19727c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 				if (stp->sd_sigflags & S_ERROR)
19757c478bd9Sstevel@tonic-gate 					strsendsig(stp->sd_siglist, S_ERROR, 0,
19767c478bd9Sstevel@tonic-gate 					    (stp->sd_werror ? stp->sd_werror :
19777c478bd9Sstevel@tonic-gate 					    stp->sd_rerror));
19787c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 				/*
19817c478bd9Sstevel@tonic-gate 				 * Send the M_FLUSH only
19827c478bd9Sstevel@tonic-gate 				 * for the first M_ERROR
19837c478bd9Sstevel@tonic-gate 				 * message on the stream
19847c478bd9Sstevel@tonic-gate 				 */
19857c478bd9Sstevel@tonic-gate 				if (flushed_already != FLUSHRW) {
19867c478bd9Sstevel@tonic-gate 					bp->b_datap->db_type = M_FLUSH;
19877c478bd9Sstevel@tonic-gate 					*bp->b_rptr = FLUSHRW;
19887c478bd9Sstevel@tonic-gate 					/*
19897c478bd9Sstevel@tonic-gate 					 * Protect against the driver passing up
19907c478bd9Sstevel@tonic-gate 					 * messages after it has done a
19917c478bd9Sstevel@tonic-gate 					 * qprocsoff.
19927c478bd9Sstevel@tonic-gate 					 */
19937c478bd9Sstevel@tonic-gate 				if (_OTHERQ(q)->q_next == NULL)
19947c478bd9Sstevel@tonic-gate 					freemsg(bp);
19957c478bd9Sstevel@tonic-gate 				else
19967c478bd9Sstevel@tonic-gate 					qreply(q, bp);
19977c478bd9Sstevel@tonic-gate 				return (0);
19987c478bd9Sstevel@tonic-gate 				}
19997c478bd9Sstevel@tonic-gate 		}
20007c478bd9Sstevel@tonic-gate 		freemsg(bp);
20017c478bd9Sstevel@tonic-gate 		return (0);
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	case M_HANGUP:
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 		freemsg(bp);
20067c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
20077c478bd9Sstevel@tonic-gate 		stp->sd_werror = ENXIO;
20087c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRHUP;
20097c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~(WSLEEP|RSLEEP);
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 		/*
20127c478bd9Sstevel@tonic-gate 		 * send signal if controlling tty
20137c478bd9Sstevel@tonic-gate 		 */
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp) {
20167c478bd9Sstevel@tonic-gate 			prsignal(stp->sd_sidp, SIGHUP);
20177c478bd9Sstevel@tonic-gate 			if (stp->sd_sidp != stp->sd_pgidp)
20187c478bd9Sstevel@tonic-gate 				pgsignal(stp->sd_pgidp, SIGTSTP);
20197c478bd9Sstevel@tonic-gate 		}
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 		/*
20227c478bd9Sstevel@tonic-gate 		 * wake up read, write, and exception pollers and
20237c478bd9Sstevel@tonic-gate 		 * reset wakeup mechanism.
20247c478bd9Sstevel@tonic-gate 		 */
20257c478bd9Sstevel@tonic-gate 		cv_broadcast(&q->q_wait);	/* the readers */
20267c478bd9Sstevel@tonic-gate 		cv_broadcast(&_WR(q)->q_wait);	/* the writers */
20277c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);	/* the ioctllers */
20287c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
20297c478bd9Sstevel@tonic-gate 		strhup(stp);
20307c478bd9Sstevel@tonic-gate 		return (0);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	case M_UNHANGUP:
20337c478bd9Sstevel@tonic-gate 		freemsg(bp);
20347c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
20357c478bd9Sstevel@tonic-gate 		stp->sd_werror = 0;
20367c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STRHUP;
20377c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
20387c478bd9Sstevel@tonic-gate 		return (0);
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	case M_SIG:
20417c478bd9Sstevel@tonic-gate 		/*
20427c478bd9Sstevel@tonic-gate 		 * Someone downstream wants to post a signal.  The
20437c478bd9Sstevel@tonic-gate 		 * signal to post is contained in the first byte of the
20447c478bd9Sstevel@tonic-gate 		 * message.  If the message would go on the front of
20457c478bd9Sstevel@tonic-gate 		 * the queue, send a signal to the process group
20467c478bd9Sstevel@tonic-gate 		 * (if not SIGPOLL) or to the siglist processes
20477c478bd9Sstevel@tonic-gate 		 * (SIGPOLL).  If something is already on the queue,
20487c478bd9Sstevel@tonic-gate 		 * OR if we are delivering a delayed suspend (*sigh*
20497c478bd9Sstevel@tonic-gate 		 * another "tty" hack) and there's no one sleeping already,
20507c478bd9Sstevel@tonic-gate 		 * just enqueue the message.
20517c478bd9Sstevel@tonic-gate 		 */
20527c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
20537c478bd9Sstevel@tonic-gate 		if (q->q_first || (*bp->b_rptr == SIGTSTP &&
20547c478bd9Sstevel@tonic-gate 		    !(stp->sd_flag & RSLEEP))) {
20557c478bd9Sstevel@tonic-gate 			(void) putq(q, bp);
20567c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
20577c478bd9Sstevel@tonic-gate 			return (0);
20587c478bd9Sstevel@tonic-gate 		}
20597c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
20607c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	case M_PCSIG:
20637c478bd9Sstevel@tonic-gate 		/*
20647c478bd9Sstevel@tonic-gate 		 * Don't enqueue, just post the signal.
20657c478bd9Sstevel@tonic-gate 		 */
20667c478bd9Sstevel@tonic-gate 		strsignal(stp, *bp->b_rptr, 0L);
20677c478bd9Sstevel@tonic-gate 		freemsg(bp);
20687c478bd9Sstevel@tonic-gate 		return (0);
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	case M_FLUSH:
20717c478bd9Sstevel@tonic-gate 		/*
20727c478bd9Sstevel@tonic-gate 		 * Flush queues.  The indication of which queues to flush
20737c478bd9Sstevel@tonic-gate 		 * is in the first byte of the message.  If the read queue
20747c478bd9Sstevel@tonic-gate 		 * is specified, then flush it.  If FLUSHBAND is set, just
20757c478bd9Sstevel@tonic-gate 		 * flush the band specified by the second byte of the message.
20767c478bd9Sstevel@tonic-gate 		 *
20777c478bd9Sstevel@tonic-gate 		 * If a module has issued a M_SETOPT to not flush hi
20787c478bd9Sstevel@tonic-gate 		 * priority messages off of the stream head, then pass this
20797c478bd9Sstevel@tonic-gate 		 * flag into the flushq code to preserve such messages.
20807c478bd9Sstevel@tonic-gate 		 */
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 		if (*bp->b_rptr & FLUSHR) {
20837c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
20847c478bd9Sstevel@tonic-gate 			if (*bp->b_rptr & FLUSHBAND) {
20857c478bd9Sstevel@tonic-gate 				ASSERT((bp->b_wptr - bp->b_rptr) >= 2);
20867c478bd9Sstevel@tonic-gate 				flushband(q, *(bp->b_rptr + 1), FLUSHALL);
20877c478bd9Sstevel@tonic-gate 			} else
20887c478bd9Sstevel@tonic-gate 				flushq_common(q, FLUSHALL,
20897c478bd9Sstevel@tonic-gate 				    stp->sd_read_opt & RFLUSHPCPROT);
20907c478bd9Sstevel@tonic-gate 			if ((q->q_first == NULL) ||
20917c478bd9Sstevel@tonic-gate 			    (q->q_first->b_datap->db_type < QPCTL))
20927c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
20937c478bd9Sstevel@tonic-gate 			else {
20947c478bd9Sstevel@tonic-gate 				ASSERT(stp->sd_flag & STRPRI);
20957c478bd9Sstevel@tonic-gate 			}
20967c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
20977c478bd9Sstevel@tonic-gate 		}
20987c478bd9Sstevel@tonic-gate 		if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
20997c478bd9Sstevel@tonic-gate 			*bp->b_rptr &= ~FLUSHR;
21007c478bd9Sstevel@tonic-gate 			bp->b_flag |= MSGNOLOOP;
21017c478bd9Sstevel@tonic-gate 			/*
21027c478bd9Sstevel@tonic-gate 			 * Protect against the driver passing up
21037c478bd9Sstevel@tonic-gate 			 * messages after it has done a qprocsoff.
21047c478bd9Sstevel@tonic-gate 			 */
21057c478bd9Sstevel@tonic-gate 			if (_OTHERQ(q)->q_next == NULL)
21067c478bd9Sstevel@tonic-gate 				freemsg(bp);
21077c478bd9Sstevel@tonic-gate 			else
21087c478bd9Sstevel@tonic-gate 				qreply(q, bp);
21097c478bd9Sstevel@tonic-gate 			return (0);
21107c478bd9Sstevel@tonic-gate 		}
21117c478bd9Sstevel@tonic-gate 		freemsg(bp);
21127c478bd9Sstevel@tonic-gate 		return (0);
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 	case M_IOCACK:
21157c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
21167c478bd9Sstevel@tonic-gate 		iocbp = (struct iocblk *)bp->b_rptr;
21177c478bd9Sstevel@tonic-gate 		/*
21187c478bd9Sstevel@tonic-gate 		 * If not waiting for ACK or NAK then just free msg.
21197c478bd9Sstevel@tonic-gate 		 * If incorrect id sequence number then just free msg.
21207c478bd9Sstevel@tonic-gate 		 * If already have ACK or NAK for user then this is a
21217c478bd9Sstevel@tonic-gate 		 *    duplicate, display a warning and free the msg.
21227c478bd9Sstevel@tonic-gate 		 */
21237c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
21247c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
21257c478bd9Sstevel@tonic-gate 		    (stp->sd_iocid != iocbp->ioc_id)) {
21267c478bd9Sstevel@tonic-gate 			/*
21277c478bd9Sstevel@tonic-gate 			 * If the ACK/NAK is a dup, display a message
21287c478bd9Sstevel@tonic-gate 			 * Dup is when sd_iocid == ioc_id, and
21297c478bd9Sstevel@tonic-gate 			 * sd_iocblk == <valid ptr> or -1 (the former
21307c478bd9Sstevel@tonic-gate 			 * is when an ioctl has been put on the stream
21317c478bd9Sstevel@tonic-gate 			 * head, but has not yet been consumed, the
21327c478bd9Sstevel@tonic-gate 			 * later is when it has been consumed).
21337c478bd9Sstevel@tonic-gate 			 */
21347c478bd9Sstevel@tonic-gate 			if ((stp->sd_iocid == iocbp->ioc_id) &&
21357c478bd9Sstevel@tonic-gate 			    (stp->sd_iocblk != NULL)) {
21367c478bd9Sstevel@tonic-gate 				log_dupioc(q, bp);
21377c478bd9Sstevel@tonic-gate 			}
21387c478bd9Sstevel@tonic-gate 			freemsg(bp);
21397c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
21407c478bd9Sstevel@tonic-gate 			return (0);
21417c478bd9Sstevel@tonic-gate 		}
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 		/*
21447c478bd9Sstevel@tonic-gate 		 * Assign ACK or NAK to user and wake up.
21457c478bd9Sstevel@tonic-gate 		 */
21467c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = bp;
21477c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);
21487c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
21497c478bd9Sstevel@tonic-gate 		return (0);
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 	case M_COPYIN:
21527c478bd9Sstevel@tonic-gate 	case M_COPYOUT:
21537c478bd9Sstevel@tonic-gate 		reqp = (struct copyreq *)bp->b_rptr;
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 		/*
21567c478bd9Sstevel@tonic-gate 		 * If not waiting for ACK or NAK then just fail request.
21577c478bd9Sstevel@tonic-gate 		 * If already have ACK, NAK, or copy request, then just
21587c478bd9Sstevel@tonic-gate 		 * fail request.
21597c478bd9Sstevel@tonic-gate 		 * If incorrect id sequence number then just fail request.
21607c478bd9Sstevel@tonic-gate 		 */
21617c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
21627c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
21637c478bd9Sstevel@tonic-gate 		    (stp->sd_iocid != reqp->cq_id)) {
21647c478bd9Sstevel@tonic-gate 			if (bp->b_cont) {
21657c478bd9Sstevel@tonic-gate 				freemsg(bp->b_cont);
21667c478bd9Sstevel@tonic-gate 				bp->b_cont = NULL;
21677c478bd9Sstevel@tonic-gate 			}
21687c478bd9Sstevel@tonic-gate 			bp->b_datap->db_type = M_IOCDATA;
21697c478bd9Sstevel@tonic-gate 			bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
21707c478bd9Sstevel@tonic-gate 			resp = (struct copyresp *)bp->b_rptr;
21717c478bd9Sstevel@tonic-gate 			resp->cp_rval = (caddr_t)1;	/* failure */
21727c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
21737c478bd9Sstevel@tonic-gate 			putnext(stp->sd_wrq, bp);
21747c478bd9Sstevel@tonic-gate 			return (0);
21757c478bd9Sstevel@tonic-gate 		}
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 		/*
21787c478bd9Sstevel@tonic-gate 		 * Assign copy request to user and wake up.
21797c478bd9Sstevel@tonic-gate 		 */
21807c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = bp;
21817c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);
21827c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
21837c478bd9Sstevel@tonic-gate 		return (0);
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	case M_SETOPTS:
21867c478bd9Sstevel@tonic-gate 		/*
21877c478bd9Sstevel@tonic-gate 		 * Set stream head options (read option, write offset,
21887c478bd9Sstevel@tonic-gate 		 * min/max packet size, and/or high/low water marks for
21897c478bd9Sstevel@tonic-gate 		 * the read side only).
21907c478bd9Sstevel@tonic-gate 		 */
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 		bpri = 0;
21937c478bd9Sstevel@tonic-gate 		sop = (struct stroptions *)bp->b_rptr;
21947c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
21957c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_READOPT) {
21967c478bd9Sstevel@tonic-gate 			switch (sop->so_readopt & RMODEMASK) {
21977c478bd9Sstevel@tonic-gate 			case RNORM:
21987c478bd9Sstevel@tonic-gate 				stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
21997c478bd9Sstevel@tonic-gate 				break;
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 			case RMSGD:
22027c478bd9Sstevel@tonic-gate 				stp->sd_read_opt =
22037c478bd9Sstevel@tonic-gate 				    ((stp->sd_read_opt & ~RD_MSGNODIS) |
22047c478bd9Sstevel@tonic-gate 				    RD_MSGDIS);
22057c478bd9Sstevel@tonic-gate 				break;
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 			case RMSGN:
22087c478bd9Sstevel@tonic-gate 				stp->sd_read_opt =
22097c478bd9Sstevel@tonic-gate 				    ((stp->sd_read_opt & ~RD_MSGDIS) |
22107c478bd9Sstevel@tonic-gate 				    RD_MSGNODIS);
22117c478bd9Sstevel@tonic-gate 				break;
22127c478bd9Sstevel@tonic-gate 			}
22137c478bd9Sstevel@tonic-gate 			switch (sop->so_readopt & RPROTMASK) {
22147c478bd9Sstevel@tonic-gate 			case RPROTNORM:
22157c478bd9Sstevel@tonic-gate 				stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
22167c478bd9Sstevel@tonic-gate 				break;
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 			case RPROTDAT:
22197c478bd9Sstevel@tonic-gate 				stp->sd_read_opt =
22207c478bd9Sstevel@tonic-gate 				    ((stp->sd_read_opt & ~RD_PROTDIS) |
22217c478bd9Sstevel@tonic-gate 				    RD_PROTDAT);
22227c478bd9Sstevel@tonic-gate 				break;
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 			case RPROTDIS:
22257c478bd9Sstevel@tonic-gate 				stp->sd_read_opt =
22267c478bd9Sstevel@tonic-gate 				    ((stp->sd_read_opt & ~RD_PROTDAT) |
22277c478bd9Sstevel@tonic-gate 				    RD_PROTDIS);
22287c478bd9Sstevel@tonic-gate 				break;
22297c478bd9Sstevel@tonic-gate 			}
22307c478bd9Sstevel@tonic-gate 			switch (sop->so_readopt & RFLUSHMASK) {
22317c478bd9Sstevel@tonic-gate 			case RFLUSHPCPROT:
22327c478bd9Sstevel@tonic-gate 				/*
22337c478bd9Sstevel@tonic-gate 				 * This sets the stream head to NOT flush
22347c478bd9Sstevel@tonic-gate 				 * M_PCPROTO messages.
22357c478bd9Sstevel@tonic-gate 				 */
22367c478bd9Sstevel@tonic-gate 				stp->sd_read_opt |= RFLUSHPCPROT;
22377c478bd9Sstevel@tonic-gate 				break;
22387c478bd9Sstevel@tonic-gate 			}
22397c478bd9Sstevel@tonic-gate 		}
22407c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_ERROPT) {
22417c478bd9Sstevel@tonic-gate 			switch (sop->so_erropt & RERRMASK) {
22427c478bd9Sstevel@tonic-gate 			case RERRNORM:
22437c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRDERRNONPERSIST;
22447c478bd9Sstevel@tonic-gate 				break;
22457c478bd9Sstevel@tonic-gate 			case RERRNONPERSIST:
22467c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRDERRNONPERSIST;
22477c478bd9Sstevel@tonic-gate 				break;
22487c478bd9Sstevel@tonic-gate 			}
22497c478bd9Sstevel@tonic-gate 			switch (sop->so_erropt & WERRMASK) {
22507c478bd9Sstevel@tonic-gate 			case WERRNORM:
22517c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STWRERRNONPERSIST;
22527c478bd9Sstevel@tonic-gate 				break;
22537c478bd9Sstevel@tonic-gate 			case WERRNONPERSIST:
22547c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STWRERRNONPERSIST;
22557c478bd9Sstevel@tonic-gate 				break;
22567c478bd9Sstevel@tonic-gate 			}
22577c478bd9Sstevel@tonic-gate 		}
22587c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_COPYOPT) {
22597c478bd9Sstevel@tonic-gate 			if (sop->so_copyopt & ZCVMSAFE) {
22607c478bd9Sstevel@tonic-gate 				stp->sd_copyflag |= STZCVMSAFE;
22617c478bd9Sstevel@tonic-gate 				stp->sd_copyflag &= ~STZCVMUNSAFE;
22627c478bd9Sstevel@tonic-gate 			} else if (sop->so_copyopt & ZCVMUNSAFE) {
22637c478bd9Sstevel@tonic-gate 				stp->sd_copyflag |= STZCVMUNSAFE;
22647c478bd9Sstevel@tonic-gate 				stp->sd_copyflag &= ~STZCVMSAFE;
22657c478bd9Sstevel@tonic-gate 			}
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 			if (sop->so_copyopt & COPYCACHED) {
22687c478bd9Sstevel@tonic-gate 				stp->sd_copyflag |= STRCOPYCACHED;
22697c478bd9Sstevel@tonic-gate 			}
22707c478bd9Sstevel@tonic-gate 		}
22717c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_WROFF)
22727c478bd9Sstevel@tonic-gate 			stp->sd_wroff = sop->so_wroff;
2273*c28749e9Skais 		if (sop->so_flags & SO_TAIL)
2274*c28749e9Skais 			stp->sd_tail = sop->so_tail;
22757c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MINPSZ)
22767c478bd9Sstevel@tonic-gate 			q->q_minpsz = sop->so_minpsz;
22777c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MAXPSZ)
22787c478bd9Sstevel@tonic-gate 			q->q_maxpsz = sop->so_maxpsz;
22797c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MAXBLK)
22807c478bd9Sstevel@tonic-gate 			stp->sd_maxblk = sop->so_maxblk;
22817c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_HIWAT) {
22827c478bd9Sstevel@tonic-gate 		    if (sop->so_flags & SO_BAND) {
22837c478bd9Sstevel@tonic-gate 			if (strqset(q, QHIWAT, sop->so_band, sop->so_hiwat))
22847c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
22857c478bd9Sstevel@tonic-gate 				    "strrput: could not allocate qband\n");
22867c478bd9Sstevel@tonic-gate 			else
22877c478bd9Sstevel@tonic-gate 				bpri = sop->so_band;
22887c478bd9Sstevel@tonic-gate 		    } else {
22897c478bd9Sstevel@tonic-gate 			q->q_hiwat = sop->so_hiwat;
22907c478bd9Sstevel@tonic-gate 		    }
22917c478bd9Sstevel@tonic-gate 		}
22927c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_LOWAT) {
22937c478bd9Sstevel@tonic-gate 		    if (sop->so_flags & SO_BAND) {
22947c478bd9Sstevel@tonic-gate 			if (strqset(q, QLOWAT, sop->so_band, sop->so_lowat))
22957c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
22967c478bd9Sstevel@tonic-gate 				    "strrput: could not allocate qband\n");
22977c478bd9Sstevel@tonic-gate 			else
22987c478bd9Sstevel@tonic-gate 				bpri = sop->so_band;
22997c478bd9Sstevel@tonic-gate 		    } else {
23007c478bd9Sstevel@tonic-gate 			q->q_lowat = sop->so_lowat;
23017c478bd9Sstevel@tonic-gate 		    }
23027c478bd9Sstevel@tonic-gate 		}
23037c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MREADON)
23047c478bd9Sstevel@tonic-gate 			stp->sd_flag |= SNDMREAD;
23057c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_MREADOFF)
23067c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~SNDMREAD;
23077c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_NDELON)
23087c478bd9Sstevel@tonic-gate 			stp->sd_flag |= OLDNDELAY;
23097c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_NDELOFF)
23107c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~OLDNDELAY;
23117c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_ISTTY)
23127c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRISTTY;
23137c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_ISNTTY)
23147c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRISTTY;
23157c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_TOSTOP)
23167c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRTOSTOP;
23177c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_TONSTOP)
23187c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRTOSTOP;
23197c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_DELIM)
23207c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRDELIM;
23217c478bd9Sstevel@tonic-gate 		if (sop->so_flags & SO_NODELIM)
23227c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRDELIM;
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
23257c478bd9Sstevel@tonic-gate 		freemsg(bp);
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 		/* Check backenable in case the water marks changed */
23287c478bd9Sstevel@tonic-gate 		qbackenable(q, bpri);
23297c478bd9Sstevel@tonic-gate 		return (0);
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	/*
23327c478bd9Sstevel@tonic-gate 	 * The following set of cases deal with situations where two stream
23337c478bd9Sstevel@tonic-gate 	 * heads are connected to each other (twisted streams).  These messages
23347c478bd9Sstevel@tonic-gate 	 * have no meaning at the stream head.
23357c478bd9Sstevel@tonic-gate 	 */
23367c478bd9Sstevel@tonic-gate 	case M_BREAK:
23377c478bd9Sstevel@tonic-gate 	case M_CTL:
23387c478bd9Sstevel@tonic-gate 	case M_DELAY:
23397c478bd9Sstevel@tonic-gate 	case M_START:
23407c478bd9Sstevel@tonic-gate 	case M_STOP:
23417c478bd9Sstevel@tonic-gate 	case M_IOCDATA:
23427c478bd9Sstevel@tonic-gate 	case M_STARTI:
23437c478bd9Sstevel@tonic-gate 	case M_STOPI:
23447c478bd9Sstevel@tonic-gate 		freemsg(bp);
23457c478bd9Sstevel@tonic-gate 		return (0);
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	case M_IOCTL:
23487c478bd9Sstevel@tonic-gate 		/*
23497c478bd9Sstevel@tonic-gate 		 * Always NAK this condition
23507c478bd9Sstevel@tonic-gate 		 * (makes no sense)
23517c478bd9Sstevel@tonic-gate 		 * If there is one or more threads in the read side
23527c478bd9Sstevel@tonic-gate 		 * rwnext we have to defer the nacking until that thread
23537c478bd9Sstevel@tonic-gate 		 * returns (in strget).
23547c478bd9Sstevel@tonic-gate 		 */
23557c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
23567c478bd9Sstevel@tonic-gate 		if (stp->sd_struiodnak != 0) {
23577c478bd9Sstevel@tonic-gate 			/*
23587c478bd9Sstevel@tonic-gate 			 * Defer NAK to the streamhead. Queue at the end
23597c478bd9Sstevel@tonic-gate 			 * the list.
23607c478bd9Sstevel@tonic-gate 			 */
23617c478bd9Sstevel@tonic-gate 			mblk_t *mp = stp->sd_struionak;
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 			while (mp && mp->b_next)
23647c478bd9Sstevel@tonic-gate 				mp = mp->b_next;
23657c478bd9Sstevel@tonic-gate 			if (mp)
23667c478bd9Sstevel@tonic-gate 				mp->b_next = bp;
23677c478bd9Sstevel@tonic-gate 			else
23687c478bd9Sstevel@tonic-gate 				stp->sd_struionak = bp;
23697c478bd9Sstevel@tonic-gate 			bp->b_next = NULL;
23707c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
23717c478bd9Sstevel@tonic-gate 			return (0);
23727c478bd9Sstevel@tonic-gate 		}
23737c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCNAK;
23767c478bd9Sstevel@tonic-gate 		/*
23777c478bd9Sstevel@tonic-gate 		 * Protect against the driver passing up
23787c478bd9Sstevel@tonic-gate 		 * messages after it has done a qprocsoff.
23797c478bd9Sstevel@tonic-gate 		 */
23807c478bd9Sstevel@tonic-gate 		if (_OTHERQ(q)->q_next == NULL)
23817c478bd9Sstevel@tonic-gate 			freemsg(bp);
23827c478bd9Sstevel@tonic-gate 		else
23837c478bd9Sstevel@tonic-gate 			qreply(q, bp);
23847c478bd9Sstevel@tonic-gate 		return (0);
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	default:
23877c478bd9Sstevel@tonic-gate #ifdef DEBUG
23887c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
23897c478bd9Sstevel@tonic-gate 			"bad message type %x received at stream head\n",
23907c478bd9Sstevel@tonic-gate 			bp->b_datap->db_type);
23917c478bd9Sstevel@tonic-gate #endif
23927c478bd9Sstevel@tonic-gate 		freemsg(bp);
23937c478bd9Sstevel@tonic-gate 		return (0);
23947c478bd9Sstevel@tonic-gate 	}
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
23977c478bd9Sstevel@tonic-gate }
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate /*
24007c478bd9Sstevel@tonic-gate  * Check if the stream pointed to by `stp' can be written to, and return an
24017c478bd9Sstevel@tonic-gate  * error code if not.  If `eiohup' is set, then return EIO if STRHUP is set.
24027c478bd9Sstevel@tonic-gate  * If `sigpipeok' is set and the SW_SIGPIPE option is enabled on the stream,
24037c478bd9Sstevel@tonic-gate  * then always return EPIPE and send a SIGPIPE to the invoking thread.
24047c478bd9Sstevel@tonic-gate  */
24057c478bd9Sstevel@tonic-gate static int
24067c478bd9Sstevel@tonic-gate strwriteable(struct stdata *stp, boolean_t eiohup, boolean_t sigpipeok)
24077c478bd9Sstevel@tonic-gate {
24087c478bd9Sstevel@tonic-gate 	int error;
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	/*
24137c478bd9Sstevel@tonic-gate 	 * For modem support, POSIX states that on writes, EIO should
24147c478bd9Sstevel@tonic-gate 	 * be returned if the stream has been hung up.
24157c478bd9Sstevel@tonic-gate 	 */
24167c478bd9Sstevel@tonic-gate 	if (eiohup && (stp->sd_flag & (STPLEX|STRHUP)) == STRHUP)
24177c478bd9Sstevel@tonic-gate 		error = EIO;
24187c478bd9Sstevel@tonic-gate 	else
24197c478bd9Sstevel@tonic-gate 		error = strgeterr(stp, STRHUP|STPLEX|STWRERR, 0);
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	if (error != 0) {
24227c478bd9Sstevel@tonic-gate 		if (!(stp->sd_flag & STPLEX) &&
24237c478bd9Sstevel@tonic-gate 		    (stp->sd_wput_opt & SW_SIGPIPE) && sigpipeok) {
24247c478bd9Sstevel@tonic-gate 			tsignal(curthread, SIGPIPE);
24257c478bd9Sstevel@tonic-gate 			error = EPIPE;
24267c478bd9Sstevel@tonic-gate 		}
24277c478bd9Sstevel@tonic-gate 	}
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 	return (error);
24307c478bd9Sstevel@tonic-gate }
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate /*
24337c478bd9Sstevel@tonic-gate  * Copyin and send data down a stream.
24347c478bd9Sstevel@tonic-gate  * The caller will allocate and copyin any control part that precedes the
24357c478bd9Sstevel@tonic-gate  * message and pass than in as mctl.
24367c478bd9Sstevel@tonic-gate  *
24377c478bd9Sstevel@tonic-gate  * Caller should *not* hold sd_lock.
24387c478bd9Sstevel@tonic-gate  * When EWOULDBLOCK is returned the caller has to redo the canputnext
24397c478bd9Sstevel@tonic-gate  * under sd_lock in order to avoid missing a backenabling wakeup.
24407c478bd9Sstevel@tonic-gate  *
24417c478bd9Sstevel@tonic-gate  * Use iosize = -1 to not send any M_DATA. iosize = 0 sends zero-length M_DATA.
24427c478bd9Sstevel@tonic-gate  *
24437c478bd9Sstevel@tonic-gate  * Set MSG_IGNFLOW in flags to ignore flow control for hipri messages.
24447c478bd9Sstevel@tonic-gate  * For sync streams we can only ignore flow control by reverting to using
24457c478bd9Sstevel@tonic-gate  * putnext.
24467c478bd9Sstevel@tonic-gate  *
24477c478bd9Sstevel@tonic-gate  * If sd_maxblk is less than *iosize this routine might return without
24487c478bd9Sstevel@tonic-gate  * transferring all of *iosize. In all cases, on return *iosize will contain
24497c478bd9Sstevel@tonic-gate  * the amount of data that was transferred.
24507c478bd9Sstevel@tonic-gate  */
24517c478bd9Sstevel@tonic-gate static int
24527c478bd9Sstevel@tonic-gate strput(struct stdata *stp, mblk_t *mctl, struct uio *uiop, ssize_t *iosize,
24537c478bd9Sstevel@tonic-gate     int b_flag, int pri, int flags)
24547c478bd9Sstevel@tonic-gate {
24557c478bd9Sstevel@tonic-gate 	struiod_t uiod;
24567c478bd9Sstevel@tonic-gate 	mblk_t *mp;
24577c478bd9Sstevel@tonic-gate 	queue_t *wqp = stp->sd_wrq;
24587c478bd9Sstevel@tonic-gate 	int error = 0;
24597c478bd9Sstevel@tonic-gate 	ssize_t count = *iosize;
24607c478bd9Sstevel@tonic-gate 	cred_t *cr;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	if (uiop != NULL && count >= 0)
24657c478bd9Sstevel@tonic-gate 		flags |= stp->sd_struiowrq ? STRUIO_POSTPONE : 0;
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 	if (!(flags & STRUIO_POSTPONE)) {
24687c478bd9Sstevel@tonic-gate 		/*
24697c478bd9Sstevel@tonic-gate 		 * Use regular canputnext, strmakedata, putnext sequence.
24707c478bd9Sstevel@tonic-gate 		 */
24717c478bd9Sstevel@tonic-gate 		if (pri == 0) {
24727c478bd9Sstevel@tonic-gate 			if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
24737c478bd9Sstevel@tonic-gate 				freemsg(mctl);
24747c478bd9Sstevel@tonic-gate 				return (EWOULDBLOCK);
24757c478bd9Sstevel@tonic-gate 			}
24767c478bd9Sstevel@tonic-gate 		} else {
24777c478bd9Sstevel@tonic-gate 			if (!(flags & MSG_IGNFLOW) && !bcanputnext(wqp, pri)) {
24787c478bd9Sstevel@tonic-gate 				freemsg(mctl);
24797c478bd9Sstevel@tonic-gate 				return (EWOULDBLOCK);
24807c478bd9Sstevel@tonic-gate 			}
24817c478bd9Sstevel@tonic-gate 		}
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate 		if ((error = strmakedata(iosize, uiop, stp, flags,
24847c478bd9Sstevel@tonic-gate 					&mp)) != 0) {
24857c478bd9Sstevel@tonic-gate 			freemsg(mctl);
24867c478bd9Sstevel@tonic-gate 			/*
24877c478bd9Sstevel@tonic-gate 			 * need to change return code to ENOMEM
24887c478bd9Sstevel@tonic-gate 			 * so that this is not confused with
24897c478bd9Sstevel@tonic-gate 			 * flow control, EAGAIN.
24907c478bd9Sstevel@tonic-gate 			 */
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 			if (error == EAGAIN)
24937c478bd9Sstevel@tonic-gate 				return (ENOMEM);
24947c478bd9Sstevel@tonic-gate 			else
24957c478bd9Sstevel@tonic-gate 				return (error);
24967c478bd9Sstevel@tonic-gate 		}
24977c478bd9Sstevel@tonic-gate 		if (mctl != NULL) {
24987c478bd9Sstevel@tonic-gate 			if (mctl->b_cont == NULL)
24997c478bd9Sstevel@tonic-gate 				mctl->b_cont = mp;
25007c478bd9Sstevel@tonic-gate 			else if (mp != NULL)
25017c478bd9Sstevel@tonic-gate 				linkb(mctl, mp);
25027c478bd9Sstevel@tonic-gate 			mp = mctl;
25037c478bd9Sstevel@tonic-gate 			/*
25047c478bd9Sstevel@tonic-gate 			 * Note that for interrupt thread, the CRED() is
25057c478bd9Sstevel@tonic-gate 			 * NULL. Don't bother with the pid either.
25067c478bd9Sstevel@tonic-gate 			 */
25077c478bd9Sstevel@tonic-gate 			if ((cr = CRED()) != NULL) {
25087c478bd9Sstevel@tonic-gate 				mblk_setcred(mp, cr);
25097c478bd9Sstevel@tonic-gate 				DB_CPID(mp) = curproc->p_pid;
25107c478bd9Sstevel@tonic-gate 			}
25117c478bd9Sstevel@tonic-gate 		} else if (mp == NULL)
25127c478bd9Sstevel@tonic-gate 			return (0);
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 		mp->b_flag |= b_flag;
25157c478bd9Sstevel@tonic-gate 		mp->b_band = (uchar_t)pri;
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 		if (flags & MSG_IGNFLOW) {
25187c478bd9Sstevel@tonic-gate 			/*
25197c478bd9Sstevel@tonic-gate 			 * XXX Hack: Don't get stuck running service
25207c478bd9Sstevel@tonic-gate 			 * procedures. This is needed for sockfs when
25217c478bd9Sstevel@tonic-gate 			 * sending the unbind message out of the rput
25227c478bd9Sstevel@tonic-gate 			 * procedure - we don't want a put procedure
25237c478bd9Sstevel@tonic-gate 			 * to run service procedures.
25247c478bd9Sstevel@tonic-gate 			 */
25257c478bd9Sstevel@tonic-gate 			putnext(wqp, mp);
25267c478bd9Sstevel@tonic-gate 		} else {
25277c478bd9Sstevel@tonic-gate 			stream_willservice(stp);
25287c478bd9Sstevel@tonic-gate 			putnext(wqp, mp);
25297c478bd9Sstevel@tonic-gate 			stream_runservice(stp);
25307c478bd9Sstevel@tonic-gate 		}
25317c478bd9Sstevel@tonic-gate 		return (0);
25327c478bd9Sstevel@tonic-gate 	}
25337c478bd9Sstevel@tonic-gate 	/*
25347c478bd9Sstevel@tonic-gate 	 * Stream supports rwnext() for the write side.
25357c478bd9Sstevel@tonic-gate 	 */
25367c478bd9Sstevel@tonic-gate 	if ((error = strmakedata(iosize, uiop, stp, flags, &mp)) != 0) {
25377c478bd9Sstevel@tonic-gate 		freemsg(mctl);
25387c478bd9Sstevel@tonic-gate 		/*
25397c478bd9Sstevel@tonic-gate 		 * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
25407c478bd9Sstevel@tonic-gate 		 */
25417c478bd9Sstevel@tonic-gate 		return (error == EAGAIN ? ENOMEM : error);
25427c478bd9Sstevel@tonic-gate 	}
25437c478bd9Sstevel@tonic-gate 	if (mctl != NULL) {
25447c478bd9Sstevel@tonic-gate 		if (mctl->b_cont == NULL)
25457c478bd9Sstevel@tonic-gate 			mctl->b_cont = mp;
25467c478bd9Sstevel@tonic-gate 		else if (mp != NULL)
25477c478bd9Sstevel@tonic-gate 			linkb(mctl, mp);
25487c478bd9Sstevel@tonic-gate 		mp = mctl;
25497c478bd9Sstevel@tonic-gate 		/*
25507c478bd9Sstevel@tonic-gate 		 * Note that for interrupt thread, the CRED() is
25517c478bd9Sstevel@tonic-gate 		 * NULL.  Don't bother with the pid either.
25527c478bd9Sstevel@tonic-gate 		 */
25537c478bd9Sstevel@tonic-gate 		if ((cr = CRED()) != NULL) {
25547c478bd9Sstevel@tonic-gate 			mblk_setcred(mp, cr);
25557c478bd9Sstevel@tonic-gate 			DB_CPID(mp) = curproc->p_pid;
25567c478bd9Sstevel@tonic-gate 		}
25577c478bd9Sstevel@tonic-gate 	} else if (mp == NULL) {
25587c478bd9Sstevel@tonic-gate 		return (0);
25597c478bd9Sstevel@tonic-gate 	}
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 	mp->b_flag |= b_flag;
25627c478bd9Sstevel@tonic-gate 	mp->b_band = (uchar_t)pri;
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 	(void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
25657c478bd9Sstevel@tonic-gate 		sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
25667c478bd9Sstevel@tonic-gate 	uiod.d_uio.uio_offset = 0;
25677c478bd9Sstevel@tonic-gate 	uiod.d_mp = mp;
25687c478bd9Sstevel@tonic-gate 	error = rwnext(wqp, &uiod);
25697c478bd9Sstevel@tonic-gate 	if (! uiod.d_mp) {
25707c478bd9Sstevel@tonic-gate 		uioskip(uiop, *iosize);
25717c478bd9Sstevel@tonic-gate 		return (error);
25727c478bd9Sstevel@tonic-gate 	}
25737c478bd9Sstevel@tonic-gate 	ASSERT(mp == uiod.d_mp);
25747c478bd9Sstevel@tonic-gate 	if (error == EINVAL) {
25757c478bd9Sstevel@tonic-gate 		/*
25767c478bd9Sstevel@tonic-gate 		 * The stream plumbing must have changed while
25777c478bd9Sstevel@tonic-gate 		 * we were away, so just turn off rwnext()s.
25787c478bd9Sstevel@tonic-gate 		 */
25797c478bd9Sstevel@tonic-gate 		error = 0;
25807c478bd9Sstevel@tonic-gate 	} else if (error == EBUSY || error == EWOULDBLOCK) {
25817c478bd9Sstevel@tonic-gate 		/*
25827c478bd9Sstevel@tonic-gate 		 * Couldn't enter a perimeter or took a page fault,
25837c478bd9Sstevel@tonic-gate 		 * so fall-back to putnext().
25847c478bd9Sstevel@tonic-gate 		 */
25857c478bd9Sstevel@tonic-gate 		error = 0;
25867c478bd9Sstevel@tonic-gate 	} else {
25877c478bd9Sstevel@tonic-gate 		freemsg(mp);
25887c478bd9Sstevel@tonic-gate 		return (error);
25897c478bd9Sstevel@tonic-gate 	}
25907c478bd9Sstevel@tonic-gate 	/* Have to check canput before consuming data from the uio */
25917c478bd9Sstevel@tonic-gate 	if (pri == 0) {
25927c478bd9Sstevel@tonic-gate 		if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
25937c478bd9Sstevel@tonic-gate 			freemsg(mp);
25947c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
25957c478bd9Sstevel@tonic-gate 		}
25967c478bd9Sstevel@tonic-gate 	} else {
25977c478bd9Sstevel@tonic-gate 		if (!bcanputnext(wqp, pri) && !(flags & MSG_IGNFLOW)) {
25987c478bd9Sstevel@tonic-gate 			freemsg(mp);
25997c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
26007c478bd9Sstevel@tonic-gate 		}
26017c478bd9Sstevel@tonic-gate 	}
26027c478bd9Sstevel@tonic-gate 	ASSERT(mp == uiod.d_mp);
26037c478bd9Sstevel@tonic-gate 	/* Copyin data from the uio */
26047c478bd9Sstevel@tonic-gate 	if ((error = struioget(wqp, mp, &uiod, 0)) != 0) {
26057c478bd9Sstevel@tonic-gate 		freemsg(mp);
26067c478bd9Sstevel@tonic-gate 		return (error);
26077c478bd9Sstevel@tonic-gate 	}
26087c478bd9Sstevel@tonic-gate 	uioskip(uiop, *iosize);
26097c478bd9Sstevel@tonic-gate 	if (flags & MSG_IGNFLOW) {
26107c478bd9Sstevel@tonic-gate 		/*
26117c478bd9Sstevel@tonic-gate 		 * XXX Hack: Don't get stuck running service procedures.
26127c478bd9Sstevel@tonic-gate 		 * This is needed for sockfs when sending the unbind message
26137c478bd9Sstevel@tonic-gate 		 * out of the rput procedure - we don't want a put procedure
26147c478bd9Sstevel@tonic-gate 		 * to run service procedures.
26157c478bd9Sstevel@tonic-gate 		 */
26167c478bd9Sstevel@tonic-gate 		putnext(wqp, mp);
26177c478bd9Sstevel@tonic-gate 	} else {
26187c478bd9Sstevel@tonic-gate 		stream_willservice(stp);
26197c478bd9Sstevel@tonic-gate 		putnext(wqp, mp);
26207c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
26217c478bd9Sstevel@tonic-gate 	}
26227c478bd9Sstevel@tonic-gate 	return (0);
26237c478bd9Sstevel@tonic-gate }
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate /*
26267c478bd9Sstevel@tonic-gate  * Write attempts to break the write request into messages conforming
26277c478bd9Sstevel@tonic-gate  * with the minimum and maximum packet sizes set downstream.
26287c478bd9Sstevel@tonic-gate  *
26297c478bd9Sstevel@tonic-gate  * Write will not block if downstream queue is full and
26307c478bd9Sstevel@tonic-gate  * O_NDELAY is set, otherwise it will block waiting for the queue to get room.
26317c478bd9Sstevel@tonic-gate  *
26327c478bd9Sstevel@tonic-gate  * A write of zero bytes gets packaged into a zero length message and sent
26337c478bd9Sstevel@tonic-gate  * downstream like any other message.
26347c478bd9Sstevel@tonic-gate  *
26357c478bd9Sstevel@tonic-gate  * If buffers of the requested sizes are not available, the write will
26367c478bd9Sstevel@tonic-gate  * sleep until the buffers become available.
26377c478bd9Sstevel@tonic-gate  *
26387c478bd9Sstevel@tonic-gate  * Write (if specified) will supply a write offset in a message if it
26397c478bd9Sstevel@tonic-gate  * makes sense. This can be specified by downstream modules as part of
26407c478bd9Sstevel@tonic-gate  * a M_SETOPTS message.  Write will not supply the write offset if it
26417c478bd9Sstevel@tonic-gate  * cannot supply any data in a buffer.  In other words, write will never
26427c478bd9Sstevel@tonic-gate  * send down an empty packet due to a write offset.
26437c478bd9Sstevel@tonic-gate  */
26447c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
26457c478bd9Sstevel@tonic-gate int
26467c478bd9Sstevel@tonic-gate strwrite(struct vnode *vp, struct uio *uiop, cred_t *crp)
26477c478bd9Sstevel@tonic-gate {
2648ff550d0eSmasputra 	return (strwrite_common(vp, uiop, crp, 0));
2649ff550d0eSmasputra }
2650ff550d0eSmasputra 
2651ff550d0eSmasputra /* ARGSUSED2 */
2652ff550d0eSmasputra int
2653ff550d0eSmasputra strwrite_common(struct vnode *vp, struct uio *uiop, cred_t *crp, int wflag)
2654ff550d0eSmasputra {
26557c478bd9Sstevel@tonic-gate 	struct stdata *stp;
26567c478bd9Sstevel@tonic-gate 	struct queue *wqp;
26577c478bd9Sstevel@tonic-gate 	ssize_t rmin, rmax;
26587c478bd9Sstevel@tonic-gate 	ssize_t iosize;
2659ff550d0eSmasputra 	int waitflag;
26607c478bd9Sstevel@tonic-gate 	int tempmode;
26617c478bd9Sstevel@tonic-gate 	int error = 0;
26627c478bd9Sstevel@tonic-gate 	int b_flag;
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
26657c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
26687c478bd9Sstevel@tonic-gate 		if ((error = straccess(stp, JCWRITE)) != 0)
26697c478bd9Sstevel@tonic-gate 			return (error);
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
26727c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
26737c478bd9Sstevel@tonic-gate 		error = strwriteable(stp, B_TRUE, B_TRUE);
26747c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
26757c478bd9Sstevel@tonic-gate 		if (error != 0)
26767c478bd9Sstevel@tonic-gate 			return (error);
26777c478bd9Sstevel@tonic-gate 	}
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 	wqp = stp->sd_wrq;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	/* get these values from them cached in the stream head */
26827c478bd9Sstevel@tonic-gate 	rmin = stp->sd_qn_minpsz;
26837c478bd9Sstevel@tonic-gate 	rmax = stp->sd_qn_maxpsz;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	/*
26867c478bd9Sstevel@tonic-gate 	 * Check the min/max packet size constraints.  If min packet size
26877c478bd9Sstevel@tonic-gate 	 * is non-zero, the write cannot be split into multiple messages
26887c478bd9Sstevel@tonic-gate 	 * and still guarantee the size constraints.
26897c478bd9Sstevel@tonic-gate 	 */
26907c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_IN, "strwrite in:q %p", wqp);
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 	ASSERT((rmax >= 0) || (rmax == INFPSZ));
26937c478bd9Sstevel@tonic-gate 	if (rmax == 0) {
26947c478bd9Sstevel@tonic-gate 		return (0);
26957c478bd9Sstevel@tonic-gate 	}
26967c478bd9Sstevel@tonic-gate 	if (rmin > 0) {
26977c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid < rmin) {
26987c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
26997c478bd9Sstevel@tonic-gate 				"strwrite out:q %p out %d error %d",
27007c478bd9Sstevel@tonic-gate 				wqp, 0, ERANGE);
27017c478bd9Sstevel@tonic-gate 			return (ERANGE);
27027c478bd9Sstevel@tonic-gate 		}
27037c478bd9Sstevel@tonic-gate 		if ((rmax != INFPSZ) && (uiop->uio_resid > rmax)) {
27047c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
27057c478bd9Sstevel@tonic-gate 				"strwrite out:q %p out %d error %d",
27067c478bd9Sstevel@tonic-gate 				wqp, 1, ERANGE);
27077c478bd9Sstevel@tonic-gate 			return (ERANGE);
27087c478bd9Sstevel@tonic-gate 		}
27097c478bd9Sstevel@tonic-gate 	}
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate 	/*
27127c478bd9Sstevel@tonic-gate 	 * Do until count satisfied or error.
27137c478bd9Sstevel@tonic-gate 	 */
2714ff550d0eSmasputra 	waitflag = WRITEWAIT | wflag;
27157c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & OLDNDELAY)
27167c478bd9Sstevel@tonic-gate 		tempmode = uiop->uio_fmode & ~FNDELAY;
27177c478bd9Sstevel@tonic-gate 	else
27187c478bd9Sstevel@tonic-gate 		tempmode = uiop->uio_fmode;
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	if (rmax == INFPSZ)
27217c478bd9Sstevel@tonic-gate 		rmax = uiop->uio_resid;
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	/*
27247c478bd9Sstevel@tonic-gate 	 * Note that tempmode does not get used in strput/strmakedata
27257c478bd9Sstevel@tonic-gate 	 * but only in strwaitq. The other routines use uio_fmode
27267c478bd9Sstevel@tonic-gate 	 * unmodified.
27277c478bd9Sstevel@tonic-gate 	 */
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	/* LINTED: constant in conditional context */
27307c478bd9Sstevel@tonic-gate 	while (1) {	/* breaks when uio_resid reaches zero */
27317c478bd9Sstevel@tonic-gate 		/*
27327c478bd9Sstevel@tonic-gate 		 * Determine the size of the next message to be
27337c478bd9Sstevel@tonic-gate 		 * packaged.  May have to break write into several
27347c478bd9Sstevel@tonic-gate 		 * messages based on max packet size.
27357c478bd9Sstevel@tonic-gate 		 */
27367c478bd9Sstevel@tonic-gate 		iosize = MIN(uiop->uio_resid, rmax);
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 		/*
27397c478bd9Sstevel@tonic-gate 		 * Put block downstream when flow control allows it.
27407c478bd9Sstevel@tonic-gate 		 */
27417c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STRDELIM) && (uiop->uio_resid == iosize))
27427c478bd9Sstevel@tonic-gate 			b_flag = MSGDELIM;
27437c478bd9Sstevel@tonic-gate 		else
27447c478bd9Sstevel@tonic-gate 			b_flag = 0;
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 		for (;;) {
27477c478bd9Sstevel@tonic-gate 			int done = 0;
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 			error = strput(stp, NULL, uiop, &iosize, b_flag,
27507c478bd9Sstevel@tonic-gate 				0, 0);
27517c478bd9Sstevel@tonic-gate 			if (error == 0)
27527c478bd9Sstevel@tonic-gate 				break;
27537c478bd9Sstevel@tonic-gate 			if (error != EWOULDBLOCK)
27547c478bd9Sstevel@tonic-gate 				goto out;
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
27577c478bd9Sstevel@tonic-gate 			/*
27587c478bd9Sstevel@tonic-gate 			 * Check for a missed wakeup.
27597c478bd9Sstevel@tonic-gate 			 * Needed since strput did not hold sd_lock across
27607c478bd9Sstevel@tonic-gate 			 * the canputnext.
27617c478bd9Sstevel@tonic-gate 			 */
27627c478bd9Sstevel@tonic-gate 			if (canputnext(wqp)) {
27637c478bd9Sstevel@tonic-gate 				/* Try again */
27647c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
27657c478bd9Sstevel@tonic-gate 				continue;
27667c478bd9Sstevel@tonic-gate 			}
27677c478bd9Sstevel@tonic-gate 			TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAIT,
27687c478bd9Sstevel@tonic-gate 				"strwrite wait:q %p wait", wqp);
27697c478bd9Sstevel@tonic-gate 			if ((error = strwaitq(stp, waitflag, (ssize_t)0,
27707c478bd9Sstevel@tonic-gate 			    tempmode, -1, &done)) != 0 || done) {
27717c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
27727c478bd9Sstevel@tonic-gate 				if ((vp->v_type == VFIFO) &&
27737c478bd9Sstevel@tonic-gate 				    (uiop->uio_fmode & FNDELAY) &&
27747c478bd9Sstevel@tonic-gate 				    (error == EAGAIN))
27757c478bd9Sstevel@tonic-gate 					error = 0;
27767c478bd9Sstevel@tonic-gate 				goto out;
27777c478bd9Sstevel@tonic-gate 			}
27787c478bd9Sstevel@tonic-gate 			TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAKE,
27797c478bd9Sstevel@tonic-gate 				"strwrite wake:q %p awakes", wqp);
27807c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
27817c478bd9Sstevel@tonic-gate 			if (stp->sd_sidp != NULL &&
27827c478bd9Sstevel@tonic-gate 			    stp->sd_vnode->v_type != VFIFO)
27837c478bd9Sstevel@tonic-gate 				if (error = straccess(stp, JCWRITE))
27847c478bd9Sstevel@tonic-gate 					goto out;
27857c478bd9Sstevel@tonic-gate 		}
27867c478bd9Sstevel@tonic-gate 		waitflag |= NOINTR;
27877c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRWRITE_RESID,
27887c478bd9Sstevel@tonic-gate 			"strwrite resid:q %p uiop %p", wqp, uiop);
27897c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid) {
27907c478bd9Sstevel@tonic-gate 			/* Recheck for errors - needed for sockets */
27917c478bd9Sstevel@tonic-gate 			if ((stp->sd_wput_opt & SW_RECHECK_ERR) &&
27927c478bd9Sstevel@tonic-gate 			    (stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
27937c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
27947c478bd9Sstevel@tonic-gate 				error = strwriteable(stp, B_FALSE, B_TRUE);
27957c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
27967c478bd9Sstevel@tonic-gate 				if (error != 0)
27977c478bd9Sstevel@tonic-gate 					return (error);
27987c478bd9Sstevel@tonic-gate 			}
27997c478bd9Sstevel@tonic-gate 			continue;
28007c478bd9Sstevel@tonic-gate 		}
28017c478bd9Sstevel@tonic-gate 		break;
28027c478bd9Sstevel@tonic-gate 	}
28037c478bd9Sstevel@tonic-gate out:
28047c478bd9Sstevel@tonic-gate 	/*
28057c478bd9Sstevel@tonic-gate 	 * For historical reasons, applications expect EAGAIN when a data
28067c478bd9Sstevel@tonic-gate 	 * mblk_t cannot be allocated, so change ENOMEM back to EAGAIN.
28077c478bd9Sstevel@tonic-gate 	 */
28087c478bd9Sstevel@tonic-gate 	if (error == ENOMEM)
28097c478bd9Sstevel@tonic-gate 		error = EAGAIN;
28107c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
28117c478bd9Sstevel@tonic-gate 		"strwrite out:q %p out %d error %d", wqp, 2, error);
28127c478bd9Sstevel@tonic-gate 	return (error);
28137c478bd9Sstevel@tonic-gate }
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate /*
28167c478bd9Sstevel@tonic-gate  * Stream head write service routine.
28177c478bd9Sstevel@tonic-gate  * Its job is to wake up any sleeping writers when a queue
28187c478bd9Sstevel@tonic-gate  * downstream needs data (part of the flow control in putq and getq).
28197c478bd9Sstevel@tonic-gate  * It also must wake anyone sleeping on a poll().
28207c478bd9Sstevel@tonic-gate  * For stream head right below mux module, it must also invoke put procedure
28217c478bd9Sstevel@tonic-gate  * of next downstream module.
28227c478bd9Sstevel@tonic-gate  */
28237c478bd9Sstevel@tonic-gate int
28247c478bd9Sstevel@tonic-gate strwsrv(queue_t *q)
28257c478bd9Sstevel@tonic-gate {
28267c478bd9Sstevel@tonic-gate 	struct stdata *stp;
28277c478bd9Sstevel@tonic-gate 	queue_t *tq;
28287c478bd9Sstevel@tonic-gate 	qband_t *qbp;
28297c478bd9Sstevel@tonic-gate 	int i;
28307c478bd9Sstevel@tonic-gate 	qband_t *myqbp;
28317c478bd9Sstevel@tonic-gate 	int isevent;
28327c478bd9Sstevel@tonic-gate 	unsigned char	qbf[NBAND];	/* band flushing backenable flags */
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
28357c478bd9Sstevel@tonic-gate 		TR_STRWSRV, "strwsrv:q %p", q);
28367c478bd9Sstevel@tonic-gate 	stp = (struct stdata *)q->q_ptr;
28377c478bd9Sstevel@tonic-gate 	ASSERT(qclaimed(q));
28387c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
28397c478bd9Sstevel@tonic-gate 	ASSERT(!(stp->sd_flag & STPLEX));
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & WSLEEP) {
28427c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~WSLEEP;
28437c478bd9Sstevel@tonic-gate 		cv_broadcast(&q->q_wait);
28447c478bd9Sstevel@tonic-gate 	}
28457c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	/* The other end of a stream pipe went away. */
28487c478bd9Sstevel@tonic-gate 	if ((tq = q->q_next) == NULL) {
28497c478bd9Sstevel@tonic-gate 		return (0);
28507c478bd9Sstevel@tonic-gate 	}
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	/* Find the next module forward that has a service procedure */
28537c478bd9Sstevel@tonic-gate 	claimstr(q);
28547c478bd9Sstevel@tonic-gate 	tq = q->q_nfsrv;
28557c478bd9Sstevel@tonic-gate 	ASSERT(tq != NULL);
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	if ((q->q_flag & QBACK)) {
28587c478bd9Sstevel@tonic-gate 		if ((tq->q_flag & QFULL)) {
28597c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(tq));
28607c478bd9Sstevel@tonic-gate 			if (!(tq->q_flag & QFULL)) {
28617c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(tq));
28627c478bd9Sstevel@tonic-gate 				goto wakeup;
28637c478bd9Sstevel@tonic-gate 			}
28647c478bd9Sstevel@tonic-gate 			/*
28657c478bd9Sstevel@tonic-gate 			 * The queue must have become full again. Set QWANTW
28667c478bd9Sstevel@tonic-gate 			 * again so strwsrv will be back enabled when
28677c478bd9Sstevel@tonic-gate 			 * the queue becomes non-full next time.
28687c478bd9Sstevel@tonic-gate 			 */
28697c478bd9Sstevel@tonic-gate 			tq->q_flag |= QWANTW;
28707c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(tq));
28717c478bd9Sstevel@tonic-gate 		} else {
28727c478bd9Sstevel@tonic-gate 		wakeup:
28737c478bd9Sstevel@tonic-gate 			pollwakeup(&stp->sd_pollist, POLLWRNORM);
28747c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
28757c478bd9Sstevel@tonic-gate 			if (stp->sd_sigflags & S_WRNORM)
28767c478bd9Sstevel@tonic-gate 				strsendsig(stp->sd_siglist, S_WRNORM, 0, 0);
28777c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
28787c478bd9Sstevel@tonic-gate 		}
28797c478bd9Sstevel@tonic-gate 	}
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 	isevent = 0;
28827c478bd9Sstevel@tonic-gate 	i = 1;
28837c478bd9Sstevel@tonic-gate 	bzero((caddr_t)qbf, NBAND);
28847c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(tq));
28857c478bd9Sstevel@tonic-gate 	if ((myqbp = q->q_bandp) != NULL)
28867c478bd9Sstevel@tonic-gate 		for (qbp = tq->q_bandp; qbp && myqbp; qbp = qbp->qb_next) {
28877c478bd9Sstevel@tonic-gate 			ASSERT(myqbp);
28887c478bd9Sstevel@tonic-gate 			if ((myqbp->qb_flag & QB_BACK)) {
28897c478bd9Sstevel@tonic-gate 				if (qbp->qb_flag & QB_FULL) {
28907c478bd9Sstevel@tonic-gate 					/*
28917c478bd9Sstevel@tonic-gate 					 * The band must have become full again.
28927c478bd9Sstevel@tonic-gate 					 * Set QB_WANTW again so strwsrv will
28937c478bd9Sstevel@tonic-gate 					 * be back enabled when the band becomes
28947c478bd9Sstevel@tonic-gate 					 * non-full next time.
28957c478bd9Sstevel@tonic-gate 					 */
28967c478bd9Sstevel@tonic-gate 					qbp->qb_flag |= QB_WANTW;
28977c478bd9Sstevel@tonic-gate 				} else {
28987c478bd9Sstevel@tonic-gate 					isevent = 1;
28997c478bd9Sstevel@tonic-gate 					qbf[i] = 1;
29007c478bd9Sstevel@tonic-gate 				}
29017c478bd9Sstevel@tonic-gate 			}
29027c478bd9Sstevel@tonic-gate 			myqbp = myqbp->qb_next;
29037c478bd9Sstevel@tonic-gate 			i++;
29047c478bd9Sstevel@tonic-gate 		}
29057c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(tq));
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 	if (isevent) {
29087c478bd9Sstevel@tonic-gate 	    for (i = tq->q_nband; i; i--) {
29097c478bd9Sstevel@tonic-gate 		if (qbf[i]) {
29107c478bd9Sstevel@tonic-gate 			pollwakeup(&stp->sd_pollist, POLLWRBAND);
29117c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
29127c478bd9Sstevel@tonic-gate 			if (stp->sd_sigflags & S_WRBAND)
29137c478bd9Sstevel@tonic-gate 				strsendsig(stp->sd_siglist, S_WRBAND,
29147c478bd9Sstevel@tonic-gate 					(uchar_t)i, 0);
29157c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
29167c478bd9Sstevel@tonic-gate 		}
29177c478bd9Sstevel@tonic-gate 	    }
29187c478bd9Sstevel@tonic-gate 	}
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 	releasestr(q);
29217c478bd9Sstevel@tonic-gate 	return (0);
29227c478bd9Sstevel@tonic-gate }
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate /*
29257c478bd9Sstevel@tonic-gate  * Special case of strcopyin/strcopyout for copying
29267c478bd9Sstevel@tonic-gate  * struct strioctl that can deal with both data
29277c478bd9Sstevel@tonic-gate  * models.
29287c478bd9Sstevel@tonic-gate  */
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate #ifdef	_LP64
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate static int
29337c478bd9Sstevel@tonic-gate strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
29347c478bd9Sstevel@tonic-gate {
29357c478bd9Sstevel@tonic-gate 	struct	strioctl32 strioc32;
29367c478bd9Sstevel@tonic-gate 	struct	strioctl *striocp;
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	if (copyflag & U_TO_K) {
29397c478bd9Sstevel@tonic-gate 		ASSERT((copyflag & K_TO_K) == 0);
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 		if ((flag & FMODELS) == DATAMODEL_ILP32) {
29427c478bd9Sstevel@tonic-gate 			if (copyin(from, &strioc32, sizeof (strioc32)))
29437c478bd9Sstevel@tonic-gate 				return (EFAULT);
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate 			striocp = (struct strioctl *)to;
29467c478bd9Sstevel@tonic-gate 			striocp->ic_cmd	= strioc32.ic_cmd;
29477c478bd9Sstevel@tonic-gate 			striocp->ic_timout = strioc32.ic_timout;
29487c478bd9Sstevel@tonic-gate 			striocp->ic_len	= strioc32.ic_len;
29497c478bd9Sstevel@tonic-gate 			striocp->ic_dp	= (char *)(uintptr_t)strioc32.ic_dp;
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 		} else { /* NATIVE data model */
29527c478bd9Sstevel@tonic-gate 			if (copyin(from, to, sizeof (struct strioctl))) {
29537c478bd9Sstevel@tonic-gate 				return (EFAULT);
29547c478bd9Sstevel@tonic-gate 			} else {
29557c478bd9Sstevel@tonic-gate 				return (0);
29567c478bd9Sstevel@tonic-gate 			}
29577c478bd9Sstevel@tonic-gate 		}
29587c478bd9Sstevel@tonic-gate 	} else {
29597c478bd9Sstevel@tonic-gate 		ASSERT(copyflag & K_TO_K);
29607c478bd9Sstevel@tonic-gate 		bcopy(from, to, sizeof (struct strioctl));
29617c478bd9Sstevel@tonic-gate 	}
29627c478bd9Sstevel@tonic-gate 	return (0);
29637c478bd9Sstevel@tonic-gate }
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate static int
29667c478bd9Sstevel@tonic-gate strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
29677c478bd9Sstevel@tonic-gate {
29687c478bd9Sstevel@tonic-gate 	struct	strioctl32 strioc32;
29697c478bd9Sstevel@tonic-gate 	struct	strioctl *striocp;
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	if (copyflag & U_TO_K) {
29727c478bd9Sstevel@tonic-gate 		ASSERT((copyflag & K_TO_K) == 0);
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 		if ((flag & FMODELS) == DATAMODEL_ILP32) {
29757c478bd9Sstevel@tonic-gate 			striocp = (struct strioctl *)from;
29767c478bd9Sstevel@tonic-gate 			strioc32.ic_cmd	= striocp->ic_cmd;
29777c478bd9Sstevel@tonic-gate 			strioc32.ic_timout = striocp->ic_timout;
29787c478bd9Sstevel@tonic-gate 			strioc32.ic_len	= striocp->ic_len;
29797c478bd9Sstevel@tonic-gate 			strioc32.ic_dp	= (caddr32_t)(uintptr_t)striocp->ic_dp;
29807c478bd9Sstevel@tonic-gate 			ASSERT((char *)(uintptr_t)strioc32.ic_dp ==
29817c478bd9Sstevel@tonic-gate 			    striocp->ic_dp);
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 			if (copyout(&strioc32, to, sizeof (strioc32)))
29847c478bd9Sstevel@tonic-gate 				return (EFAULT);
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate 		} else { /* NATIVE data model */
29877c478bd9Sstevel@tonic-gate 			if (copyout(from, to, sizeof (struct strioctl))) {
29887c478bd9Sstevel@tonic-gate 				return (EFAULT);
29897c478bd9Sstevel@tonic-gate 			} else {
29907c478bd9Sstevel@tonic-gate 				return (0);
29917c478bd9Sstevel@tonic-gate 			}
29927c478bd9Sstevel@tonic-gate 		}
29937c478bd9Sstevel@tonic-gate 	} else {
29947c478bd9Sstevel@tonic-gate 		ASSERT(copyflag & K_TO_K);
29957c478bd9Sstevel@tonic-gate 		bcopy(from, to, sizeof (struct strioctl));
29967c478bd9Sstevel@tonic-gate 	}
29977c478bd9Sstevel@tonic-gate 	return (0);
29987c478bd9Sstevel@tonic-gate }
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate #else	/* ! _LP64 */
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
30037c478bd9Sstevel@tonic-gate static int
30047c478bd9Sstevel@tonic-gate strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
30057c478bd9Sstevel@tonic-gate {
30067c478bd9Sstevel@tonic-gate 	return (strcopyin(from, to, sizeof (struct strioctl), copyflag));
30077c478bd9Sstevel@tonic-gate }
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
30107c478bd9Sstevel@tonic-gate static int
30117c478bd9Sstevel@tonic-gate strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
30127c478bd9Sstevel@tonic-gate {
30137c478bd9Sstevel@tonic-gate 	return (strcopyout(from, to, sizeof (struct strioctl), copyflag));
30147c478bd9Sstevel@tonic-gate }
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate /*
30197c478bd9Sstevel@tonic-gate  * Determine type of job control semantics expected by user.  The
30207c478bd9Sstevel@tonic-gate  * possibilities are:
30217c478bd9Sstevel@tonic-gate  *	JCREAD	- Behaves like read() on fd; send SIGTTIN
30227c478bd9Sstevel@tonic-gate  *	JCWRITE	- Behaves like write() on fd; send SIGTTOU if TOSTOP set
30237c478bd9Sstevel@tonic-gate  *	JCSETP	- Sets a value in the stream; send SIGTTOU, ignore TOSTOP
30247c478bd9Sstevel@tonic-gate  *	JCGETP	- Gets a value in the stream; no signals.
30257c478bd9Sstevel@tonic-gate  * See straccess in strsubr.c for usage of these values.
30267c478bd9Sstevel@tonic-gate  *
30277c478bd9Sstevel@tonic-gate  * This routine also returns -1 for I_STR as a special case; the
30287c478bd9Sstevel@tonic-gate  * caller must call again with the real ioctl number for
30297c478bd9Sstevel@tonic-gate  * classification.
30307c478bd9Sstevel@tonic-gate  */
30317c478bd9Sstevel@tonic-gate static int
30327c478bd9Sstevel@tonic-gate job_control_type(int cmd)
30337c478bd9Sstevel@tonic-gate {
30347c478bd9Sstevel@tonic-gate 	switch (cmd) {
30357c478bd9Sstevel@tonic-gate 	case I_STR:
30367c478bd9Sstevel@tonic-gate 		return (-1);
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 	case I_RECVFD:
30397c478bd9Sstevel@tonic-gate 	case I_E_RECVFD:
30407c478bd9Sstevel@tonic-gate 		return (JCREAD);
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 	case I_FDINSERT:
30437c478bd9Sstevel@tonic-gate 	case I_SENDFD:
30447c478bd9Sstevel@tonic-gate 		return (JCWRITE);
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 	case TCSETA:
30477c478bd9Sstevel@tonic-gate 	case TCSETAW:
30487c478bd9Sstevel@tonic-gate 	case TCSETAF:
30497c478bd9Sstevel@tonic-gate 	case TCSBRK:
30507c478bd9Sstevel@tonic-gate 	case TCXONC:
30517c478bd9Sstevel@tonic-gate 	case TCFLSH:
30527c478bd9Sstevel@tonic-gate 	case TCDSET:	/* Obsolete */
30537c478bd9Sstevel@tonic-gate 	case TIOCSWINSZ:
30547c478bd9Sstevel@tonic-gate 	case TCSETS:
30557c478bd9Sstevel@tonic-gate 	case TCSETSW:
30567c478bd9Sstevel@tonic-gate 	case TCSETSF:
30577c478bd9Sstevel@tonic-gate 	case TIOCSETD:
30587c478bd9Sstevel@tonic-gate 	case TIOCHPCL:
30597c478bd9Sstevel@tonic-gate 	case TIOCSETP:
30607c478bd9Sstevel@tonic-gate 	case TIOCSETN:
30617c478bd9Sstevel@tonic-gate 	case TIOCEXCL:
30627c478bd9Sstevel@tonic-gate 	case TIOCNXCL:
30637c478bd9Sstevel@tonic-gate 	case TIOCFLUSH:
30647c478bd9Sstevel@tonic-gate 	case TIOCSETC:
30657c478bd9Sstevel@tonic-gate 	case TIOCLBIS:
30667c478bd9Sstevel@tonic-gate 	case TIOCLBIC:
30677c478bd9Sstevel@tonic-gate 	case TIOCLSET:
30687c478bd9Sstevel@tonic-gate 	case TIOCSBRK:
30697c478bd9Sstevel@tonic-gate 	case TIOCCBRK:
30707c478bd9Sstevel@tonic-gate 	case TIOCSDTR:
30717c478bd9Sstevel@tonic-gate 	case TIOCCDTR:
30727c478bd9Sstevel@tonic-gate 	case TIOCSLTC:
30737c478bd9Sstevel@tonic-gate 	case TIOCSTOP:
30747c478bd9Sstevel@tonic-gate 	case TIOCSTART:
30757c478bd9Sstevel@tonic-gate 	case TIOCSTI:
30767c478bd9Sstevel@tonic-gate 	case TIOCSPGRP:
30777c478bd9Sstevel@tonic-gate 	case TIOCMSET:
30787c478bd9Sstevel@tonic-gate 	case TIOCMBIS:
30797c478bd9Sstevel@tonic-gate 	case TIOCMBIC:
30807c478bd9Sstevel@tonic-gate 	case TIOCREMOTE:
30817c478bd9Sstevel@tonic-gate 	case TIOCSIGNAL:
30827c478bd9Sstevel@tonic-gate 	case LDSETT:
30837c478bd9Sstevel@tonic-gate 	case LDSMAP:	/* Obsolete */
30847c478bd9Sstevel@tonic-gate 	case DIOCSETP:
30857c478bd9Sstevel@tonic-gate 	case I_FLUSH:
30867c478bd9Sstevel@tonic-gate 	case I_SRDOPT:
30877c478bd9Sstevel@tonic-gate 	case I_SETSIG:
30887c478bd9Sstevel@tonic-gate 	case I_SWROPT:
30897c478bd9Sstevel@tonic-gate 	case I_FLUSHBAND:
30907c478bd9Sstevel@tonic-gate 	case I_SETCLTIME:
30917c478bd9Sstevel@tonic-gate 	case I_SERROPT:
30927c478bd9Sstevel@tonic-gate 	case I_ESETSIG:
30937c478bd9Sstevel@tonic-gate 	case FIONBIO:
30947c478bd9Sstevel@tonic-gate 	case FIOASYNC:
30957c478bd9Sstevel@tonic-gate 	case FIOSETOWN:
30967c478bd9Sstevel@tonic-gate 	case JBOOT:	/* Obsolete */
30977c478bd9Sstevel@tonic-gate 	case JTERM:	/* Obsolete */
30987c478bd9Sstevel@tonic-gate 	case JTIMOM:	/* Obsolete */
30997c478bd9Sstevel@tonic-gate 	case JZOMBOOT:	/* Obsolete */
31007c478bd9Sstevel@tonic-gate 	case JAGENT:	/* Obsolete */
31017c478bd9Sstevel@tonic-gate 	case JTRUN:	/* Obsolete */
31027c478bd9Sstevel@tonic-gate 	case JXTPROTO:	/* Obsolete */
31037c478bd9Sstevel@tonic-gate 		return (JCSETP);
31047c478bd9Sstevel@tonic-gate 	}
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 	return (JCGETP);
31077c478bd9Sstevel@tonic-gate }
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate /*
31107c478bd9Sstevel@tonic-gate  * ioctl for streams
31117c478bd9Sstevel@tonic-gate  */
31127c478bd9Sstevel@tonic-gate int
31137c478bd9Sstevel@tonic-gate strioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, int copyflag,
31147c478bd9Sstevel@tonic-gate     cred_t *crp, int *rvalp)
31157c478bd9Sstevel@tonic-gate {
31167c478bd9Sstevel@tonic-gate 	struct stdata *stp;
31177c478bd9Sstevel@tonic-gate 	struct strioctl strioc;
31187c478bd9Sstevel@tonic-gate 	struct uio uio;
31197c478bd9Sstevel@tonic-gate 	struct iovec iov;
31207c478bd9Sstevel@tonic-gate 	int access;
31217c478bd9Sstevel@tonic-gate 	mblk_t *mp;
31227c478bd9Sstevel@tonic-gate 	int error = 0;
31237c478bd9Sstevel@tonic-gate 	int done = 0;
31247c478bd9Sstevel@tonic-gate 	ssize_t	rmin, rmax;
31257c478bd9Sstevel@tonic-gate 	queue_t *wrq;
31267c478bd9Sstevel@tonic-gate 	queue_t *rdq;
31277c478bd9Sstevel@tonic-gate 	boolean_t kioctl = B_FALSE;
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 	if (flag & FKIOCTL) {
31307c478bd9Sstevel@tonic-gate 		copyflag = K_TO_K;
31317c478bd9Sstevel@tonic-gate 		kioctl = B_TRUE;
31327c478bd9Sstevel@tonic-gate 	}
31337c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
31347c478bd9Sstevel@tonic-gate 	ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
31357c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_IOCTL_ENTER,
31387c478bd9Sstevel@tonic-gate 		"strioctl:stp %p cmd %X arg %lX", stp, cmd, arg);
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
31417c478bd9Sstevel@tonic-gate 	if (audit_active)
31427c478bd9Sstevel@tonic-gate 		audit_strioctl(vp, cmd, arg, flag, copyflag, crp, rvalp);
31437c478bd9Sstevel@tonic-gate #endif
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 	/*
31467c478bd9Sstevel@tonic-gate 	 * If the copy is kernel to kernel, make sure that the FNATIVE
31477c478bd9Sstevel@tonic-gate 	 * flag is set.  After this it would be a serious error to have
31487c478bd9Sstevel@tonic-gate 	 * no model flag.
31497c478bd9Sstevel@tonic-gate 	 */
31507c478bd9Sstevel@tonic-gate 	if (copyflag == K_TO_K)
31517c478bd9Sstevel@tonic-gate 		flag = (flag & ~FMODELS) | FNATIVE;
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate 	ASSERT((flag & FMODELS) != 0);
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	wrq = stp->sd_wrq;
31567c478bd9Sstevel@tonic-gate 	rdq = _RD(wrq);
31577c478bd9Sstevel@tonic-gate 
31587c478bd9Sstevel@tonic-gate 	access = job_control_type(cmd);
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate 	/* We should never see these here, should be handled by iwscn */
31617c478bd9Sstevel@tonic-gate 	if (cmd == SRIOCSREDIR || cmd == SRIOCISREDIR)
31627c478bd9Sstevel@tonic-gate 		return (EINVAL);
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	if (access != -1 && stp->sd_sidp != NULL &&
31657c478bd9Sstevel@tonic-gate 	    stp->sd_vnode->v_type != VFIFO)
31667c478bd9Sstevel@tonic-gate 		if (error = straccess(stp, access))
31677c478bd9Sstevel@tonic-gate 			return (error);
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate 	/*
31707c478bd9Sstevel@tonic-gate 	 * Check for sgttyb-related ioctls first, and complain as
31717c478bd9Sstevel@tonic-gate 	 * necessary.
31727c478bd9Sstevel@tonic-gate 	 */
31737c478bd9Sstevel@tonic-gate 	switch (cmd) {
31747c478bd9Sstevel@tonic-gate 	case TIOCGETP:
31757c478bd9Sstevel@tonic-gate 	case TIOCSETP:
31767c478bd9Sstevel@tonic-gate 	case TIOCSETN:
31777c478bd9Sstevel@tonic-gate 		if (sgttyb_handling >= 2 && !sgttyb_complaint) {
31787c478bd9Sstevel@tonic-gate 			sgttyb_complaint = B_TRUE;
31797c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE,
31807c478bd9Sstevel@tonic-gate 			    "application used obsolete TIOC[GS]ET");
31817c478bd9Sstevel@tonic-gate 		}
31827c478bd9Sstevel@tonic-gate 		if (sgttyb_handling >= 3) {
31837c478bd9Sstevel@tonic-gate 			tsignal(curthread, SIGSYS);
31847c478bd9Sstevel@tonic-gate 			return (EIO);
31857c478bd9Sstevel@tonic-gate 		}
31867c478bd9Sstevel@tonic-gate 		break;
31877c478bd9Sstevel@tonic-gate 	}
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	switch (cmd) {
31927c478bd9Sstevel@tonic-gate 	case I_RECVFD:
31937c478bd9Sstevel@tonic-gate 	case I_E_RECVFD:
31947c478bd9Sstevel@tonic-gate 	case I_PEEK:
31957c478bd9Sstevel@tonic-gate 	case I_NREAD:
31967c478bd9Sstevel@tonic-gate 	case FIONREAD:
31977c478bd9Sstevel@tonic-gate 	case FIORDCHK:
31987c478bd9Sstevel@tonic-gate 	case I_ATMARK:
31997c478bd9Sstevel@tonic-gate 	case FIONBIO:
32007c478bd9Sstevel@tonic-gate 	case FIOASYNC:
32017c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STRDERR|STPLEX)) {
32027c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, STRDERR|STPLEX, 0);
32037c478bd9Sstevel@tonic-gate 			if (error != 0) {
32047c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
32057c478bd9Sstevel@tonic-gate 				return (error);
32067c478bd9Sstevel@tonic-gate 			}
32077c478bd9Sstevel@tonic-gate 		}
32087c478bd9Sstevel@tonic-gate 		break;
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 	default:
32117c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STRDERR|STWRERR|STPLEX)) {
32127c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, STRDERR|STWRERR|STPLEX, 0);
32137c478bd9Sstevel@tonic-gate 			if (error != 0) {
32147c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
32157c478bd9Sstevel@tonic-gate 				return (error);
32167c478bd9Sstevel@tonic-gate 			}
32177c478bd9Sstevel@tonic-gate 		}
32187c478bd9Sstevel@tonic-gate 	}
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 	switch (cmd) {
32237c478bd9Sstevel@tonic-gate 	default:
32247c478bd9Sstevel@tonic-gate 		/*
32257c478bd9Sstevel@tonic-gate 		 * The stream head has hardcoded knowledge of a
32267c478bd9Sstevel@tonic-gate 		 * miscellaneous collection of terminal-, keyboard- and
32277c478bd9Sstevel@tonic-gate 		 * mouse-related ioctls, enumerated below.  This hardcoded
32287c478bd9Sstevel@tonic-gate 		 * knowledge allows the stream head to automatically
32297c478bd9Sstevel@tonic-gate 		 * convert transparent ioctl requests made by userland
32307c478bd9Sstevel@tonic-gate 		 * programs into I_STR ioctls which many old STREAMS
32317c478bd9Sstevel@tonic-gate 		 * modules and drivers require.
32327c478bd9Sstevel@tonic-gate 		 *
32337c478bd9Sstevel@tonic-gate 		 * No new ioctls should ever be added to this list.
32347c478bd9Sstevel@tonic-gate 		 * Instead, the STREAMS module or driver should be written
32357c478bd9Sstevel@tonic-gate 		 * to either handle transparent ioctls or require any
32367c478bd9Sstevel@tonic-gate 		 * userland programs to use I_STR ioctls (by returning
32377c478bd9Sstevel@tonic-gate 		 * EINVAL to any transparent ioctl requests).
32387c478bd9Sstevel@tonic-gate 		 *
32397c478bd9Sstevel@tonic-gate 		 * More importantly, removing ioctls from this list should
32407c478bd9Sstevel@tonic-gate 		 * be done with the utmost care, since our STREAMS modules
32417c478bd9Sstevel@tonic-gate 		 * and drivers *count* on the stream head performing this
32427c478bd9Sstevel@tonic-gate 		 * conversion, and thus may panic while processing
32437c478bd9Sstevel@tonic-gate 		 * transparent ioctl request for one of these ioctls (keep
32447c478bd9Sstevel@tonic-gate 		 * in mind that third party modules and drivers may have
32457c478bd9Sstevel@tonic-gate 		 * similar problems).
32467c478bd9Sstevel@tonic-gate 		 */
32477c478bd9Sstevel@tonic-gate 		if (((cmd & IOCTYPE) == LDIOC) ||
32487c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == tIOC) ||
32497c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == TIOC) ||
32507c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == KIOC) ||
32517c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == MSIOC) ||
32527c478bd9Sstevel@tonic-gate 		    ((cmd & IOCTYPE) == VUIOC)) {
32537c478bd9Sstevel@tonic-gate 			/*
32547c478bd9Sstevel@tonic-gate 			 * The ioctl is a tty ioctl - set up strioc buffer
32557c478bd9Sstevel@tonic-gate 			 * and call strdoioctl() to do the work.
32567c478bd9Sstevel@tonic-gate 			 */
32577c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & STRHUP)
32587c478bd9Sstevel@tonic-gate 				return (ENXIO);
32597c478bd9Sstevel@tonic-gate 			strioc.ic_cmd = cmd;
32607c478bd9Sstevel@tonic-gate 			strioc.ic_timout = INFTIM;
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 			switch (cmd) {
32637c478bd9Sstevel@tonic-gate 
32647c478bd9Sstevel@tonic-gate 			case TCXONC:
32657c478bd9Sstevel@tonic-gate 			case TCSBRK:
32667c478bd9Sstevel@tonic-gate 			case TCFLSH:
32677c478bd9Sstevel@tonic-gate 			case TCDSET:
32687c478bd9Sstevel@tonic-gate 				{
32697c478bd9Sstevel@tonic-gate 				int native_arg = (int)arg;
32707c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (int);
32717c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)&native_arg;
32727c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
32737c478bd9Sstevel@tonic-gate 				    K_TO_K, crp, rvalp));
32747c478bd9Sstevel@tonic-gate 				}
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate 			case TCSETA:
32777c478bd9Sstevel@tonic-gate 			case TCSETAW:
32787c478bd9Sstevel@tonic-gate 			case TCSETAF:
32797c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct termio);
32807c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
32817c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
32827c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 			case TCSETS:
32857c478bd9Sstevel@tonic-gate 			case TCSETSW:
32867c478bd9Sstevel@tonic-gate 			case TCSETSF:
32877c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct termios);
32887c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
32897c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
32907c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 			case LDSETT:
32937c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct termcb);
32947c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
32957c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
32967c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 			case TIOCSETP:
32997c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct sgttyb);
33007c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33017c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33027c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 			case TIOCSTI:
33057c478bd9Sstevel@tonic-gate 				if ((flag & FREAD) == 0 &&
33067c478bd9Sstevel@tonic-gate 				    secpolicy_sti(crp) != 0) {
33077c478bd9Sstevel@tonic-gate 					return (EPERM);
33087c478bd9Sstevel@tonic-gate 				}
33097c478bd9Sstevel@tonic-gate 				if (stp->sd_sidp !=
33107c478bd9Sstevel@tonic-gate 				    ttoproc(curthread)->p_sessp->s_sidp &&
33117c478bd9Sstevel@tonic-gate 				    secpolicy_sti(crp) != 0) {
33127c478bd9Sstevel@tonic-gate 					return (EACCES);
33137c478bd9Sstevel@tonic-gate 				}
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (char);
33167c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33177c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33187c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 			case TIOCSWINSZ:
33217c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct winsize);
33227c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33237c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33247c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33257c478bd9Sstevel@tonic-gate 
33267c478bd9Sstevel@tonic-gate 			case TIOCSSIZE:
33277c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct ttysize);
33287c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33297c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33307c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 			case TIOCSSOFTCAR:
33337c478bd9Sstevel@tonic-gate 			case KIOCTRANS:
33347c478bd9Sstevel@tonic-gate 			case KIOCTRANSABLE:
33357c478bd9Sstevel@tonic-gate 			case KIOCCMD:
33367c478bd9Sstevel@tonic-gate 			case KIOCSDIRECT:
33377c478bd9Sstevel@tonic-gate 			case KIOCSCOMPAT:
33387c478bd9Sstevel@tonic-gate 			case KIOCSKABORTEN:
33397c478bd9Sstevel@tonic-gate 			case KIOCSRPTDELAY:
33407c478bd9Sstevel@tonic-gate 			case KIOCSRPTRATE:
33417c478bd9Sstevel@tonic-gate 			case VUIDSFORMAT:
33427c478bd9Sstevel@tonic-gate 			case TIOCSPPS:
33437c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (int);
33447c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33457c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33467c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 			case KIOCSETKEY:
33497c478bd9Sstevel@tonic-gate 			case KIOCGETKEY:
33507c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct kiockey);
33517c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33527c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33537c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33547c478bd9Sstevel@tonic-gate 
33557c478bd9Sstevel@tonic-gate 			case KIOCSKEY:
33567c478bd9Sstevel@tonic-gate 			case KIOCGKEY:
33577c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct kiockeymap);
33587c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33597c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33607c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 			case KIOCSLED:
33637c478bd9Sstevel@tonic-gate 				/* arg is a pointer to char */
33647c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (char);
33657c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33667c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33677c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate 			case MSIOSETPARMS:
33707c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (Ms_parms);
33717c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33727c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33737c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 			case VUIDSADDR:
33767c478bd9Sstevel@tonic-gate 			case VUIDGADDR:
33777c478bd9Sstevel@tonic-gate 				strioc.ic_len = sizeof (struct vuid_addr_probe);
33787c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
33797c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
33807c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate 			/*
33837c478bd9Sstevel@tonic-gate 			 * These M_IOCTL's don't require any data to be sent
33847c478bd9Sstevel@tonic-gate 			 * downstream, and the driver will allocate and link
33857c478bd9Sstevel@tonic-gate 			 * on its own mblk_t upon M_IOCACK -- thus we set
33867c478bd9Sstevel@tonic-gate 			 * ic_len to zero and set ic_dp to arg so we know
33877c478bd9Sstevel@tonic-gate 			 * where to copyout to later.
33887c478bd9Sstevel@tonic-gate 			 */
33897c478bd9Sstevel@tonic-gate 			case TIOCGSOFTCAR:
33907c478bd9Sstevel@tonic-gate 			case TIOCGWINSZ:
33917c478bd9Sstevel@tonic-gate 			case TIOCGSIZE:
33927c478bd9Sstevel@tonic-gate 			case KIOCGTRANS:
33937c478bd9Sstevel@tonic-gate 			case KIOCGTRANSABLE:
33947c478bd9Sstevel@tonic-gate 			case KIOCTYPE:
33957c478bd9Sstevel@tonic-gate 			case KIOCGDIRECT:
33967c478bd9Sstevel@tonic-gate 			case KIOCGCOMPAT:
33977c478bd9Sstevel@tonic-gate 			case KIOCLAYOUT:
33987c478bd9Sstevel@tonic-gate 			case KIOCGLED:
33997c478bd9Sstevel@tonic-gate 			case MSIOGETPARMS:
34007c478bd9Sstevel@tonic-gate 			case MSIOBUTTONS:
34017c478bd9Sstevel@tonic-gate 			case VUIDGFORMAT:
34027c478bd9Sstevel@tonic-gate 			case TIOCGPPS:
34037c478bd9Sstevel@tonic-gate 			case TIOCGPPSEV:
34047c478bd9Sstevel@tonic-gate 			case TCGETA:
34057c478bd9Sstevel@tonic-gate 			case TCGETS:
34067c478bd9Sstevel@tonic-gate 			case LDGETT:
34077c478bd9Sstevel@tonic-gate 			case TIOCGETP:
34087c478bd9Sstevel@tonic-gate 			case KIOCGRPTDELAY:
34097c478bd9Sstevel@tonic-gate 			case KIOCGRPTRATE:
34107c478bd9Sstevel@tonic-gate 				strioc.ic_len = 0;
34117c478bd9Sstevel@tonic-gate 				strioc.ic_dp = (char *)arg;
34127c478bd9Sstevel@tonic-gate 				return (strdoioctl(stp, &strioc, flag,
34137c478bd9Sstevel@tonic-gate 					copyflag, crp, rvalp));
34147c478bd9Sstevel@tonic-gate 			}
34157c478bd9Sstevel@tonic-gate 		}
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 		/*
34187c478bd9Sstevel@tonic-gate 		 * Unknown cmd - send it down as a transparent ioctl.
34197c478bd9Sstevel@tonic-gate 		 */
34207c478bd9Sstevel@tonic-gate 		strioc.ic_cmd = cmd;
34217c478bd9Sstevel@tonic-gate 		strioc.ic_timout = INFTIM;
34227c478bd9Sstevel@tonic-gate 		strioc.ic_len = TRANSPARENT;
34237c478bd9Sstevel@tonic-gate 		strioc.ic_dp = (char *)&arg;
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 		return (strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp));
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate 	case I_STR:
34287c478bd9Sstevel@tonic-gate 		/*
34297c478bd9Sstevel@tonic-gate 		 * Stream ioctl.  Read in an strioctl buffer from the user
34307c478bd9Sstevel@tonic-gate 		 * along with any data specified and send it downstream.
34317c478bd9Sstevel@tonic-gate 		 * Strdoioctl will wait allow only one ioctl message at
34327c478bd9Sstevel@tonic-gate 		 * a time, and waits for the acknowledgement.
34337c478bd9Sstevel@tonic-gate 		 */
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
34367c478bd9Sstevel@tonic-gate 			return (ENXIO);
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 		error = strcopyin_strioctl((void *)arg, &strioc, flag,
34397c478bd9Sstevel@tonic-gate 		    copyflag);
34407c478bd9Sstevel@tonic-gate 		if (error != 0)
34417c478bd9Sstevel@tonic-gate 			return (error);
34427c478bd9Sstevel@tonic-gate 
34437c478bd9Sstevel@tonic-gate 		if ((strioc.ic_len < 0) || (strioc.ic_timout < -1))
34447c478bd9Sstevel@tonic-gate 			return (EINVAL);
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 		access = job_control_type(strioc.ic_cmd);
34477c478bd9Sstevel@tonic-gate 		if (access != -1 && stp->sd_sidp != NULL &&
34487c478bd9Sstevel@tonic-gate 		    stp->sd_vnode->v_type != VFIFO &&
34497c478bd9Sstevel@tonic-gate 		    (error = straccess(stp, access)) != 0)
34507c478bd9Sstevel@tonic-gate 			return (error);
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 		/*
34537c478bd9Sstevel@tonic-gate 		 * The I_STR facility provides a trap door for malicious
34547c478bd9Sstevel@tonic-gate 		 * code to send down bogus streamio(7I) ioctl commands to
34557c478bd9Sstevel@tonic-gate 		 * unsuspecting STREAMS modules and drivers which expect to
34567c478bd9Sstevel@tonic-gate 		 * only get these messages from the stream head.
34577c478bd9Sstevel@tonic-gate 		 * Explicitly prohibit any streamio ioctls which can be
34587c478bd9Sstevel@tonic-gate 		 * passed downstream by the stream head.  Note that we do
34597c478bd9Sstevel@tonic-gate 		 * not block all streamio ioctls because the ioctl
34607c478bd9Sstevel@tonic-gate 		 * numberspace is not well managed and thus it's possible
34617c478bd9Sstevel@tonic-gate 		 * that a module or driver's ioctl numbers may accidentally
34627c478bd9Sstevel@tonic-gate 		 * collide with them.
34637c478bd9Sstevel@tonic-gate 		 */
34647c478bd9Sstevel@tonic-gate 		switch (strioc.ic_cmd) {
34657c478bd9Sstevel@tonic-gate 		case I_LINK:
34667c478bd9Sstevel@tonic-gate 		case I_PLINK:
34677c478bd9Sstevel@tonic-gate 		case I_UNLINK:
34687c478bd9Sstevel@tonic-gate 		case I_PUNLINK:
34697c478bd9Sstevel@tonic-gate 		case _I_GETPEERCRED:
34707c478bd9Sstevel@tonic-gate 		case _I_PLINK_LH:
34717c478bd9Sstevel@tonic-gate 			return (EINVAL);
34727c478bd9Sstevel@tonic-gate 		}
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 		error = strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp);
34757c478bd9Sstevel@tonic-gate 		if (error == 0) {
34767c478bd9Sstevel@tonic-gate 			error = strcopyout_strioctl(&strioc, (void *)arg,
34777c478bd9Sstevel@tonic-gate 			    flag, copyflag);
34787c478bd9Sstevel@tonic-gate 		}
34797c478bd9Sstevel@tonic-gate 		return (error);
34807c478bd9Sstevel@tonic-gate 
34817c478bd9Sstevel@tonic-gate 	case I_NREAD:
34827c478bd9Sstevel@tonic-gate 		/*
34837c478bd9Sstevel@tonic-gate 		 * Return number of bytes of data in first message
34847c478bd9Sstevel@tonic-gate 		 * in queue in "arg" and return the number of messages
34857c478bd9Sstevel@tonic-gate 		 * in queue in return value.
34867c478bd9Sstevel@tonic-gate 		 */
34877c478bd9Sstevel@tonic-gate 	    {
34887c478bd9Sstevel@tonic-gate 		size_t	size;
34897c478bd9Sstevel@tonic-gate 		int	retval;
34907c478bd9Sstevel@tonic-gate 		int	count = 0;
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rdq));
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 		size = msgdsize(rdq->q_first);
34957c478bd9Sstevel@tonic-gate 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
34967c478bd9Sstevel@tonic-gate 			count++;
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rdq));
34997c478bd9Sstevel@tonic-gate 		if (stp->sd_struiordq) {
35007c478bd9Sstevel@tonic-gate 			infod_t infod;
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 			infod.d_cmd = INFOD_COUNT;
35037c478bd9Sstevel@tonic-gate 			infod.d_count = 0;
35047c478bd9Sstevel@tonic-gate 			if (count == 0) {
35057c478bd9Sstevel@tonic-gate 				infod.d_cmd |= INFOD_FIRSTBYTES;
35067c478bd9Sstevel@tonic-gate 				infod.d_bytes = 0;
35077c478bd9Sstevel@tonic-gate 			}
35087c478bd9Sstevel@tonic-gate 			infod.d_res = 0;
35097c478bd9Sstevel@tonic-gate 			(void) infonext(rdq, &infod);
35107c478bd9Sstevel@tonic-gate 			count += infod.d_count;
35117c478bd9Sstevel@tonic-gate 			if (infod.d_res & INFOD_FIRSTBYTES)
35127c478bd9Sstevel@tonic-gate 				size = infod.d_bytes;
35137c478bd9Sstevel@tonic-gate 		}
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 		/*
35167c478bd9Sstevel@tonic-gate 		 * Drop down from size_t to the "int" required by the
35177c478bd9Sstevel@tonic-gate 		 * interface.  Cap at INT_MAX.
35187c478bd9Sstevel@tonic-gate 		 */
35197c478bd9Sstevel@tonic-gate 		retval = MIN(size, INT_MAX);
35207c478bd9Sstevel@tonic-gate 		error = strcopyout(&retval, (void *)arg, sizeof (retval),
35217c478bd9Sstevel@tonic-gate 		    copyflag);
35227c478bd9Sstevel@tonic-gate 		if (!error)
35237c478bd9Sstevel@tonic-gate 			*rvalp = count;
35247c478bd9Sstevel@tonic-gate 		return (error);
35257c478bd9Sstevel@tonic-gate 	    }
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	case FIONREAD:
35287c478bd9Sstevel@tonic-gate 		/*
35297c478bd9Sstevel@tonic-gate 		 * Return number of bytes of data in all data messages
35307c478bd9Sstevel@tonic-gate 		 * in queue in "arg".
35317c478bd9Sstevel@tonic-gate 		 */
35327c478bd9Sstevel@tonic-gate 	    {
35337c478bd9Sstevel@tonic-gate 		size_t	size = 0;
35347c478bd9Sstevel@tonic-gate 		int	retval;
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rdq));
35377c478bd9Sstevel@tonic-gate 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
35387c478bd9Sstevel@tonic-gate 			size += msgdsize(mp);
35397c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rdq));
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 		if (stp->sd_struiordq) {
35427c478bd9Sstevel@tonic-gate 			infod_t infod;
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate 			infod.d_cmd = INFOD_BYTES;
35457c478bd9Sstevel@tonic-gate 			infod.d_res = 0;
35467c478bd9Sstevel@tonic-gate 			infod.d_bytes = 0;
35477c478bd9Sstevel@tonic-gate 			(void) infonext(rdq, &infod);
35487c478bd9Sstevel@tonic-gate 			size += infod.d_bytes;
35497c478bd9Sstevel@tonic-gate 		}
35507c478bd9Sstevel@tonic-gate 
35517c478bd9Sstevel@tonic-gate 		/*
35527c478bd9Sstevel@tonic-gate 		 * Drop down from size_t to the "int" required by the
35537c478bd9Sstevel@tonic-gate 		 * interface.  Cap at INT_MAX.
35547c478bd9Sstevel@tonic-gate 		 */
35557c478bd9Sstevel@tonic-gate 		retval = MIN(size, INT_MAX);
35567c478bd9Sstevel@tonic-gate 		error = strcopyout(&retval, (void *)arg, sizeof (retval),
35577c478bd9Sstevel@tonic-gate 		    copyflag);
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate 		*rvalp = 0;
35607c478bd9Sstevel@tonic-gate 		return (error);
35617c478bd9Sstevel@tonic-gate 	    }
35627c478bd9Sstevel@tonic-gate 	case FIORDCHK:
35637c478bd9Sstevel@tonic-gate 		/*
35647c478bd9Sstevel@tonic-gate 		 * FIORDCHK does not use arg value (like FIONREAD),
35657c478bd9Sstevel@tonic-gate 		 * instead a count is returned. I_NREAD value may
35667c478bd9Sstevel@tonic-gate 		 * not be accurate but safe. The real thing to do is
35677c478bd9Sstevel@tonic-gate 		 * to add the msgdsizes of all data  messages until
35687c478bd9Sstevel@tonic-gate 		 * a non-data message.
35697c478bd9Sstevel@tonic-gate 		 */
35707c478bd9Sstevel@tonic-gate 	    {
35717c478bd9Sstevel@tonic-gate 		size_t size = 0;
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rdq));
35747c478bd9Sstevel@tonic-gate 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
35757c478bd9Sstevel@tonic-gate 			size += msgdsize(mp);
35767c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rdq));
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate 		if (stp->sd_struiordq) {
35797c478bd9Sstevel@tonic-gate 			infod_t infod;
35807c478bd9Sstevel@tonic-gate 
35817c478bd9Sstevel@tonic-gate 			infod.d_cmd = INFOD_BYTES;
35827c478bd9Sstevel@tonic-gate 			infod.d_res = 0;
35837c478bd9Sstevel@tonic-gate 			infod.d_bytes = 0;
35847c478bd9Sstevel@tonic-gate 			(void) infonext(rdq, &infod);
35857c478bd9Sstevel@tonic-gate 			size += infod.d_bytes;
35867c478bd9Sstevel@tonic-gate 		}
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 		/*
35897c478bd9Sstevel@tonic-gate 		 * Since ioctl returns an int, and memory sizes under
35907c478bd9Sstevel@tonic-gate 		 * LP64 may not fit, we return INT_MAX if the count was
35917c478bd9Sstevel@tonic-gate 		 * actually greater.
35927c478bd9Sstevel@tonic-gate 		 */
35937c478bd9Sstevel@tonic-gate 		*rvalp = MIN(size, INT_MAX);
35947c478bd9Sstevel@tonic-gate 		return (0);
35957c478bd9Sstevel@tonic-gate 	    }
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 	case I_FIND:
35987c478bd9Sstevel@tonic-gate 		/*
35997c478bd9Sstevel@tonic-gate 		 * Get module name.
36007c478bd9Sstevel@tonic-gate 		 */
36017c478bd9Sstevel@tonic-gate 	    {
36027c478bd9Sstevel@tonic-gate 		char mname[FMNAMESZ + 1];
36037c478bd9Sstevel@tonic-gate 		queue_t *q;
36047c478bd9Sstevel@tonic-gate 
36057c478bd9Sstevel@tonic-gate 		error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
36067c478bd9Sstevel@tonic-gate 		    mname, FMNAMESZ + 1, NULL);
36077c478bd9Sstevel@tonic-gate 		if (error)
36087c478bd9Sstevel@tonic-gate 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate 		/*
36117c478bd9Sstevel@tonic-gate 		 * Return EINVAL if we're handed a bogus module name.
36127c478bd9Sstevel@tonic-gate 		 */
36137c478bd9Sstevel@tonic-gate 		if (fmodsw_find(mname, FMODSW_LOAD) == NULL) {
36147c478bd9Sstevel@tonic-gate 			TRACE_0(TR_FAC_STREAMS_FR,
36157c478bd9Sstevel@tonic-gate 				TR_I_CANT_FIND, "couldn't I_FIND");
36167c478bd9Sstevel@tonic-gate 			return (EINVAL);
36177c478bd9Sstevel@tonic-gate 		}
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate 		*rvalp = 0;
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 		/* Look downstream to see if module is there. */
36227c478bd9Sstevel@tonic-gate 		claimstr(stp->sd_wrq);
36237c478bd9Sstevel@tonic-gate 		for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
36247c478bd9Sstevel@tonic-gate 			if (q->q_flag&QREADR) {
36257c478bd9Sstevel@tonic-gate 				q = NULL;
36267c478bd9Sstevel@tonic-gate 				break;
36277c478bd9Sstevel@tonic-gate 			}
36287c478bd9Sstevel@tonic-gate 			if (strcmp(mname, q->q_qinfo->qi_minfo->mi_idname) == 0)
36297c478bd9Sstevel@tonic-gate 				break;
36307c478bd9Sstevel@tonic-gate 		}
36317c478bd9Sstevel@tonic-gate 		releasestr(stp->sd_wrq);
36327c478bd9Sstevel@tonic-gate 
36337c478bd9Sstevel@tonic-gate 		*rvalp = (q ? 1 : 0);
36347c478bd9Sstevel@tonic-gate 		return (error);
36357c478bd9Sstevel@tonic-gate 	    }
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	case I_PUSH:
36387c478bd9Sstevel@tonic-gate 	case __I_PUSH_NOCTTY:
36397c478bd9Sstevel@tonic-gate 		/*
36407c478bd9Sstevel@tonic-gate 		 * Push a module.
36417c478bd9Sstevel@tonic-gate 		 * For the case __I_PUSH_NOCTTY push a module but
36427c478bd9Sstevel@tonic-gate 		 * do not allocate controlling tty. See bugid 4025044
36437c478bd9Sstevel@tonic-gate 		 */
36447c478bd9Sstevel@tonic-gate 
36457c478bd9Sstevel@tonic-gate 	    {
36467c478bd9Sstevel@tonic-gate 		char mname[FMNAMESZ + 1];
36477c478bd9Sstevel@tonic-gate 		fmodsw_impl_t *fp;
36487c478bd9Sstevel@tonic-gate 		dev_t dummydev;
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
36517c478bd9Sstevel@tonic-gate 			return (ENXIO);
36527c478bd9Sstevel@tonic-gate 
36537c478bd9Sstevel@tonic-gate 		/*
36547c478bd9Sstevel@tonic-gate 		 * Get module name and look up in fmodsw.
36557c478bd9Sstevel@tonic-gate 		 */
36567c478bd9Sstevel@tonic-gate 		error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
36577c478bd9Sstevel@tonic-gate 		    mname, FMNAMESZ + 1, NULL);
36587c478bd9Sstevel@tonic-gate 		if (error)
36597c478bd9Sstevel@tonic-gate 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 		if ((fp = fmodsw_find(mname, FMODSW_HOLD | FMODSW_LOAD)) ==
36627c478bd9Sstevel@tonic-gate 		    NULL)
36637c478bd9Sstevel@tonic-gate 			return (EINVAL);
36647c478bd9Sstevel@tonic-gate 
36657c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_I_PUSH,
36667c478bd9Sstevel@tonic-gate 		    "I_PUSH:fp %p stp %p", fp, stp);
36677c478bd9Sstevel@tonic-gate 
36687c478bd9Sstevel@tonic-gate 		if (error = strstartplumb(stp, flag, cmd)) {
36697c478bd9Sstevel@tonic-gate 			fmodsw_rele(fp);
36707c478bd9Sstevel@tonic-gate 			return (error);
36717c478bd9Sstevel@tonic-gate 		}
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 		/*
36747c478bd9Sstevel@tonic-gate 		 * See if any more modules can be pushed on this stream.
36757c478bd9Sstevel@tonic-gate 		 * Note that this check must be done after strstartplumb()
36767c478bd9Sstevel@tonic-gate 		 * since otherwise multiple threads issuing I_PUSHes on
36777c478bd9Sstevel@tonic-gate 		 * the same stream will be able to exceed nstrpush.
36787c478bd9Sstevel@tonic-gate 		 */
36797c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
36807c478bd9Sstevel@tonic-gate 		if (stp->sd_pushcnt >= nstrpush) {
36817c478bd9Sstevel@tonic-gate 			fmodsw_rele(fp);
36827c478bd9Sstevel@tonic-gate 			strendplumb(stp);
36837c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
36847c478bd9Sstevel@tonic-gate 			return (EINVAL);
36857c478bd9Sstevel@tonic-gate 		}
36867c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate 		/*
36897c478bd9Sstevel@tonic-gate 		 * Push new module and call its open routine
36907c478bd9Sstevel@tonic-gate 		 * via qattach().  Modules don't change device
36917c478bd9Sstevel@tonic-gate 		 * numbers, so just ignore dummydev here.
36927c478bd9Sstevel@tonic-gate 		 */
36937c478bd9Sstevel@tonic-gate 		dummydev = vp->v_rdev;
36947c478bd9Sstevel@tonic-gate 		if ((error = qattach(rdq, &dummydev, 0, crp, fp,
36957c478bd9Sstevel@tonic-gate 		    B_FALSE)) == 0) {
36967c478bd9Sstevel@tonic-gate 			if (vp->v_type == VCHR && /* sorry, no pipes allowed */
36977c478bd9Sstevel@tonic-gate 			    (cmd == I_PUSH) && (stp->sd_flag & STRISTTY)) {
36987c478bd9Sstevel@tonic-gate 				/*
36997c478bd9Sstevel@tonic-gate 				 * try to allocate it as a controlling terminal
37007c478bd9Sstevel@tonic-gate 				 */
3701ad1660d0Smeem 				stralloctty(stp);
37027c478bd9Sstevel@tonic-gate 			}
37037c478bd9Sstevel@tonic-gate 		}
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate 		/*
37087c478bd9Sstevel@tonic-gate 		 * As a performance concern we are caching the values of
37097c478bd9Sstevel@tonic-gate 		 * q_minpsz and q_maxpsz of the module below the stream
37107c478bd9Sstevel@tonic-gate 		 * head in the stream head.
37117c478bd9Sstevel@tonic-gate 		 */
37127c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(stp->sd_wrq->q_next));
37137c478bd9Sstevel@tonic-gate 		rmin = stp->sd_wrq->q_next->q_minpsz;
37147c478bd9Sstevel@tonic-gate 		rmax = stp->sd_wrq->q_next->q_maxpsz;
37157c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(stp->sd_wrq->q_next));
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate 		/* Do this processing here as a performance concern */
37187c478bd9Sstevel@tonic-gate 		if (strmsgsz != 0) {
37197c478bd9Sstevel@tonic-gate 			if (rmax == INFPSZ)
37207c478bd9Sstevel@tonic-gate 				rmax = strmsgsz;
37217c478bd9Sstevel@tonic-gate 			else  {
37227c478bd9Sstevel@tonic-gate 				if (vp->v_type == VFIFO)
37237c478bd9Sstevel@tonic-gate 					rmax = MIN(PIPE_BUF, rmax);
37247c478bd9Sstevel@tonic-gate 				else	rmax = MIN(strmsgsz, rmax);
37257c478bd9Sstevel@tonic-gate 			}
37267c478bd9Sstevel@tonic-gate 		}
37277c478bd9Sstevel@tonic-gate 
37287c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(wrq));
37297c478bd9Sstevel@tonic-gate 		stp->sd_qn_minpsz = rmin;
37307c478bd9Sstevel@tonic-gate 		stp->sd_qn_maxpsz = rmax;
37317c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(wrq));
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 		strendplumb(stp);
37347c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
37357c478bd9Sstevel@tonic-gate 		return (error);
37367c478bd9Sstevel@tonic-gate 	    }
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 	case I_POP:
37397c478bd9Sstevel@tonic-gate 	    {
37407c478bd9Sstevel@tonic-gate 		queue_t	*q;
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
37437c478bd9Sstevel@tonic-gate 			return (ENXIO);
37447c478bd9Sstevel@tonic-gate 		if (!wrq->q_next)	/* for broken pipes */
37457c478bd9Sstevel@tonic-gate 			return (EINVAL);
37467c478bd9Sstevel@tonic-gate 
37477c478bd9Sstevel@tonic-gate 		if (error = strstartplumb(stp, flag, cmd))
37487c478bd9Sstevel@tonic-gate 			return (error);
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 		/*
37517c478bd9Sstevel@tonic-gate 		 * If there is an anchor on this stream and popping
37527c478bd9Sstevel@tonic-gate 		 * the current module would attempt to pop through the
37537c478bd9Sstevel@tonic-gate 		 * anchor, then disallow the pop unless we have sufficient
37547c478bd9Sstevel@tonic-gate 		 * privileges; take the cheapest (non-locking) check
37557c478bd9Sstevel@tonic-gate 		 * first.
37567c478bd9Sstevel@tonic-gate 		 */
37577c478bd9Sstevel@tonic-gate 		if (secpolicy_net_config(crp, B_TRUE) != 0) {
37587c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
37597c478bd9Sstevel@tonic-gate 			/*
37607c478bd9Sstevel@tonic-gate 			 * Anchors only apply if there's at least one
37617c478bd9Sstevel@tonic-gate 			 * module on the stream (sd_pushcnt > 0).
37627c478bd9Sstevel@tonic-gate 			 */
37637c478bd9Sstevel@tonic-gate 			if (stp->sd_pushcnt > 0 &&
37647c478bd9Sstevel@tonic-gate 			    stp->sd_pushcnt == stp->sd_anchor &&
37657c478bd9Sstevel@tonic-gate 			    stp->sd_vnode->v_type != VFIFO) {
37667c478bd9Sstevel@tonic-gate 				strendplumb(stp);
37677c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
37687c478bd9Sstevel@tonic-gate 				/* Audit and report error */
37697c478bd9Sstevel@tonic-gate 				return (secpolicy_net_config(crp, B_FALSE));
37707c478bd9Sstevel@tonic-gate 			}
37717c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
37727c478bd9Sstevel@tonic-gate 		}
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 		q = wrq->q_next;
37757c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_I_POP,
37767c478bd9Sstevel@tonic-gate 			"I_POP:%p from %p", q, stp);
37777c478bd9Sstevel@tonic-gate 		if (q->q_next == NULL || (q->q_flag & (QREADR|QISDRV))) {
37787c478bd9Sstevel@tonic-gate 			error = EINVAL;
37797c478bd9Sstevel@tonic-gate 		} else {
37807c478bd9Sstevel@tonic-gate 			qdetach(_RD(q), 1, flag, crp, B_FALSE);
37817c478bd9Sstevel@tonic-gate 			error = 0;
37827c478bd9Sstevel@tonic-gate 		}
37837c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate 		/*
37867c478bd9Sstevel@tonic-gate 		 * As a performance concern we are caching the values of
37877c478bd9Sstevel@tonic-gate 		 * q_minpsz and q_maxpsz of the module below the stream
37887c478bd9Sstevel@tonic-gate 		 * head in the stream head.
37897c478bd9Sstevel@tonic-gate 		 */
37907c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(wrq->q_next));
37917c478bd9Sstevel@tonic-gate 		rmin = wrq->q_next->q_minpsz;
37927c478bd9Sstevel@tonic-gate 		rmax = wrq->q_next->q_maxpsz;
37937c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(wrq->q_next));
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 		/* Do this processing here as a performance concern */
37967c478bd9Sstevel@tonic-gate 		if (strmsgsz != 0) {
37977c478bd9Sstevel@tonic-gate 			if (rmax == INFPSZ)
37987c478bd9Sstevel@tonic-gate 				rmax = strmsgsz;
37997c478bd9Sstevel@tonic-gate 			else  {
38007c478bd9Sstevel@tonic-gate 				if (vp->v_type == VFIFO)
38017c478bd9Sstevel@tonic-gate 					rmax = MIN(PIPE_BUF, rmax);
38027c478bd9Sstevel@tonic-gate 				else	rmax = MIN(strmsgsz, rmax);
38037c478bd9Sstevel@tonic-gate 			}
38047c478bd9Sstevel@tonic-gate 		}
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(wrq));
38077c478bd9Sstevel@tonic-gate 		stp->sd_qn_minpsz = rmin;
38087c478bd9Sstevel@tonic-gate 		stp->sd_qn_maxpsz = rmax;
38097c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(wrq));
38107c478bd9Sstevel@tonic-gate 
38117c478bd9Sstevel@tonic-gate 		/* If we popped through the anchor, then reset the anchor. */
38127c478bd9Sstevel@tonic-gate 		if (stp->sd_pushcnt < stp->sd_anchor)
38137c478bd9Sstevel@tonic-gate 			stp->sd_anchor = 0;
38147c478bd9Sstevel@tonic-gate 
38157c478bd9Sstevel@tonic-gate 		strendplumb(stp);
38167c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
38177c478bd9Sstevel@tonic-gate 		return (error);
38187c478bd9Sstevel@tonic-gate 	    }
38197c478bd9Sstevel@tonic-gate 
38207c478bd9Sstevel@tonic-gate 	case _I_MUXID2FD:
38217c478bd9Sstevel@tonic-gate 	{
38227c478bd9Sstevel@tonic-gate 		/*
38237c478bd9Sstevel@tonic-gate 		 * Create a fd for a I_PLINK'ed lower stream with a given
38247c478bd9Sstevel@tonic-gate 		 * muxid.  With the fd, application can send down ioctls,
38257c478bd9Sstevel@tonic-gate 		 * like I_LIST, to the previously I_PLINK'ed stream.  Note
38267c478bd9Sstevel@tonic-gate 		 * that after getting the fd, the application has to do an
38277c478bd9Sstevel@tonic-gate 		 * I_PUNLINK on the muxid before it can do any operation
38287c478bd9Sstevel@tonic-gate 		 * on the lower stream.  This is required by spec1170.
38297c478bd9Sstevel@tonic-gate 		 *
38307c478bd9Sstevel@tonic-gate 		 * The fd used to do this ioctl should point to the same
38317c478bd9Sstevel@tonic-gate 		 * controlling device used to do the I_PLINK.  If it uses
38327c478bd9Sstevel@tonic-gate 		 * a different stream or an invalid muxid, I_MUXID2FD will
38337c478bd9Sstevel@tonic-gate 		 * fail.  The error code is set to EINVAL.
38347c478bd9Sstevel@tonic-gate 		 *
38357c478bd9Sstevel@tonic-gate 		 * The intended use of this interface is the following.
38367c478bd9Sstevel@tonic-gate 		 * An application I_PLINK'ed a stream and exits.  The fd
38377c478bd9Sstevel@tonic-gate 		 * to the lower stream is gone.  Another application
38387c478bd9Sstevel@tonic-gate 		 * wants to get a fd to the lower stream, it uses I_MUXID2FD.
38397c478bd9Sstevel@tonic-gate 		 */
38407c478bd9Sstevel@tonic-gate 		int muxid = (int)arg;
38417c478bd9Sstevel@tonic-gate 		int fd;
38427c478bd9Sstevel@tonic-gate 		linkinfo_t *linkp;
38437c478bd9Sstevel@tonic-gate 		struct file *fp;
38447c478bd9Sstevel@tonic-gate 
38457c478bd9Sstevel@tonic-gate 		/*
38467c478bd9Sstevel@tonic-gate 		 * Do not allow the wildcard muxid.  This ioctl is not
38477c478bd9Sstevel@tonic-gate 		 * intended to find arbitrary link.
38487c478bd9Sstevel@tonic-gate 		 */
38497c478bd9Sstevel@tonic-gate 		if (muxid == 0) {
38507c478bd9Sstevel@tonic-gate 			return (EINVAL);
38517c478bd9Sstevel@tonic-gate 		}
38527c478bd9Sstevel@tonic-gate 
38537c478bd9Sstevel@tonic-gate 		mutex_enter(&muxifier);
38547c478bd9Sstevel@tonic-gate 		linkp = findlinks(vp->v_stream, muxid, LINKPERSIST);
38557c478bd9Sstevel@tonic-gate 		if (linkp == NULL) {
38567c478bd9Sstevel@tonic-gate 			mutex_exit(&muxifier);
38577c478bd9Sstevel@tonic-gate 			return (EINVAL);
38587c478bd9Sstevel@tonic-gate 		}
38597c478bd9Sstevel@tonic-gate 
38607c478bd9Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1) {
38617c478bd9Sstevel@tonic-gate 			mutex_exit(&muxifier);
38627c478bd9Sstevel@tonic-gate 			return (EMFILE);
38637c478bd9Sstevel@tonic-gate 		}
38647c478bd9Sstevel@tonic-gate 		fp = linkp->li_fpdown;
38657c478bd9Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
38667c478bd9Sstevel@tonic-gate 		fp->f_count++;
38677c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
38687c478bd9Sstevel@tonic-gate 		mutex_exit(&muxifier);
38697c478bd9Sstevel@tonic-gate 		setf(fd, fp);
38707c478bd9Sstevel@tonic-gate 		*rvalp = fd;
38717c478bd9Sstevel@tonic-gate 		return (0);
38727c478bd9Sstevel@tonic-gate 	}
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate 	case _I_INSERT:
38757c478bd9Sstevel@tonic-gate 	{
38767c478bd9Sstevel@tonic-gate 		/*
38777c478bd9Sstevel@tonic-gate 		 * To insert a module to a given position in a stream.
38787c478bd9Sstevel@tonic-gate 		 * In the first release, only allow privileged user
38797c478bd9Sstevel@tonic-gate 		 * to use this ioctl.
38807c478bd9Sstevel@tonic-gate 		 *
38817c478bd9Sstevel@tonic-gate 		 * Note that we do not plan to support this ioctl
38827c478bd9Sstevel@tonic-gate 		 * on pipes in the first release.  We want to learn more
38837c478bd9Sstevel@tonic-gate 		 * about the implications of these ioctls before extending
38847c478bd9Sstevel@tonic-gate 		 * their support.  And we do not think these features are
38857c478bd9Sstevel@tonic-gate 		 * valuable for pipes.
38867c478bd9Sstevel@tonic-gate 		 *
38877c478bd9Sstevel@tonic-gate 		 * Neither do we support O/C hot stream.  Note that only
38887c478bd9Sstevel@tonic-gate 		 * the upper streams of TCP/IP stack are O/C hot streams.
38897c478bd9Sstevel@tonic-gate 		 * The lower IP stream is not.
38907c478bd9Sstevel@tonic-gate 		 * When there is a O/C cold barrier, we only allow inserts
38917c478bd9Sstevel@tonic-gate 		 * above the barrier.
38927c478bd9Sstevel@tonic-gate 		 */
38937c478bd9Sstevel@tonic-gate 		STRUCT_DECL(strmodconf, strmodinsert);
38947c478bd9Sstevel@tonic-gate 		char mod_name[FMNAMESZ + 1];
38957c478bd9Sstevel@tonic-gate 		fmodsw_impl_t *fp;
38967c478bd9Sstevel@tonic-gate 		dev_t dummydev;
38977c478bd9Sstevel@tonic-gate 		queue_t *tmp_wrq;
38987c478bd9Sstevel@tonic-gate 		int pos;
38997c478bd9Sstevel@tonic-gate 		boolean_t is_insert;
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 		STRUCT_INIT(strmodinsert, flag);
39027c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
39037c478bd9Sstevel@tonic-gate 			return (ENXIO);
39047c478bd9Sstevel@tonic-gate 		if (STRMATED(stp))
39057c478bd9Sstevel@tonic-gate 			return (EINVAL);
39067c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
39077c478bd9Sstevel@tonic-gate 			return (error);
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, STRUCT_BUF(strmodinsert),
39107c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strmodinsert), copyflag);
39117c478bd9Sstevel@tonic-gate 		if (error)
39127c478bd9Sstevel@tonic-gate 			return (error);
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate 		/*
39157c478bd9Sstevel@tonic-gate 		 * Get module name and look up in fmodsw.
39167c478bd9Sstevel@tonic-gate 		 */
39177c478bd9Sstevel@tonic-gate 		error = (copyflag & U_TO_K ? copyinstr :
39187c478bd9Sstevel@tonic-gate 		    copystr)(STRUCT_FGETP(strmodinsert, mod_name),
39197c478bd9Sstevel@tonic-gate 		    mod_name, FMNAMESZ + 1, NULL);
39207c478bd9Sstevel@tonic-gate 		if (error)
39217c478bd9Sstevel@tonic-gate 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate 		if ((fp = fmodsw_find(mod_name, FMODSW_HOLD | FMODSW_LOAD)) ==
39247c478bd9Sstevel@tonic-gate 		    NULL)
39257c478bd9Sstevel@tonic-gate 			return (EINVAL);
39267c478bd9Sstevel@tonic-gate 
39277c478bd9Sstevel@tonic-gate 		if (error = strstartplumb(stp, flag, cmd)) {
39287c478bd9Sstevel@tonic-gate 			fmodsw_rele(fp);
39297c478bd9Sstevel@tonic-gate 			return (error);
39307c478bd9Sstevel@tonic-gate 		}
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate 		/*
39337c478bd9Sstevel@tonic-gate 		 * Is this _I_INSERT just like an I_PUSH?  We need to know
39347c478bd9Sstevel@tonic-gate 		 * this because we do some optimizations if this is a
39357c478bd9Sstevel@tonic-gate 		 * module being pushed.
39367c478bd9Sstevel@tonic-gate 		 */
39377c478bd9Sstevel@tonic-gate 		pos = STRUCT_FGET(strmodinsert, pos);
39387c478bd9Sstevel@tonic-gate 		is_insert = (pos != 0);
39397c478bd9Sstevel@tonic-gate 
39407c478bd9Sstevel@tonic-gate 		/*
39417c478bd9Sstevel@tonic-gate 		 * Make sure pos is valid.  Even though it is not an I_PUSH,
39427c478bd9Sstevel@tonic-gate 		 * we impose the same limit on the number of modules in a
39437c478bd9Sstevel@tonic-gate 		 * stream.
39447c478bd9Sstevel@tonic-gate 		 */
39457c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
39467c478bd9Sstevel@tonic-gate 		if (stp->sd_pushcnt >= nstrpush || pos < 0 ||
39477c478bd9Sstevel@tonic-gate 		    pos > stp->sd_pushcnt) {
39487c478bd9Sstevel@tonic-gate 			fmodsw_rele(fp);
39497c478bd9Sstevel@tonic-gate 			strendplumb(stp);
39507c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
39517c478bd9Sstevel@tonic-gate 			return (EINVAL);
39527c478bd9Sstevel@tonic-gate 		}
39537c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate 		/*
39567c478bd9Sstevel@tonic-gate 		 * First find the correct position this module to
39577c478bd9Sstevel@tonic-gate 		 * be inserted.  We don't need to call claimstr()
39587c478bd9Sstevel@tonic-gate 		 * as the stream should not be changing at this point.
39597c478bd9Sstevel@tonic-gate 		 *
39607c478bd9Sstevel@tonic-gate 		 * Insert new module and call its open routine
39617c478bd9Sstevel@tonic-gate 		 * via qattach().  Modules don't change device
39627c478bd9Sstevel@tonic-gate 		 * numbers, so just ignore dummydev here.
39637c478bd9Sstevel@tonic-gate 		 */
39647c478bd9Sstevel@tonic-gate 		for (tmp_wrq = stp->sd_wrq; pos > 0;
39657c478bd9Sstevel@tonic-gate 		    tmp_wrq = tmp_wrq->q_next, pos--) {
39667c478bd9Sstevel@tonic-gate 			ASSERT(SAMESTR(tmp_wrq));
39677c478bd9Sstevel@tonic-gate 		}
39687c478bd9Sstevel@tonic-gate 		dummydev = vp->v_rdev;
39697c478bd9Sstevel@tonic-gate 		if ((error = qattach(_RD(tmp_wrq), &dummydev, 0, crp,
39707c478bd9Sstevel@tonic-gate 		    fp, is_insert)) != 0) {
39717c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
39727c478bd9Sstevel@tonic-gate 			strendplumb(stp);
39737c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
39747c478bd9Sstevel@tonic-gate 			return (error);
39757c478bd9Sstevel@tonic-gate 		}
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 		/*
39807c478bd9Sstevel@tonic-gate 		 * As a performance concern we are caching the values of
39817c478bd9Sstevel@tonic-gate 		 * q_minpsz and q_maxpsz of the module below the stream
39827c478bd9Sstevel@tonic-gate 		 * head in the stream head.
39837c478bd9Sstevel@tonic-gate 		 */
39847c478bd9Sstevel@tonic-gate 		if (!is_insert) {
39857c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(stp->sd_wrq->q_next));
39867c478bd9Sstevel@tonic-gate 			rmin = stp->sd_wrq->q_next->q_minpsz;
39877c478bd9Sstevel@tonic-gate 			rmax = stp->sd_wrq->q_next->q_maxpsz;
39887c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(stp->sd_wrq->q_next));
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 			/* Do this processing here as a performance concern */
39917c478bd9Sstevel@tonic-gate 			if (strmsgsz != 0) {
39927c478bd9Sstevel@tonic-gate 				if (rmax == INFPSZ) {
39937c478bd9Sstevel@tonic-gate 					rmax = strmsgsz;
39947c478bd9Sstevel@tonic-gate 				} else  {
39957c478bd9Sstevel@tonic-gate 					rmax = MIN(strmsgsz, rmax);
39967c478bd9Sstevel@tonic-gate 				}
39977c478bd9Sstevel@tonic-gate 			}
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(wrq));
40007c478bd9Sstevel@tonic-gate 			stp->sd_qn_minpsz = rmin;
40017c478bd9Sstevel@tonic-gate 			stp->sd_qn_maxpsz = rmax;
40027c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(wrq));
40037c478bd9Sstevel@tonic-gate 		}
40047c478bd9Sstevel@tonic-gate 
40057c478bd9Sstevel@tonic-gate 		/*
40067c478bd9Sstevel@tonic-gate 		 * Need to update the anchor value if this module is
40077c478bd9Sstevel@tonic-gate 		 * inserted below the anchor point.
40087c478bd9Sstevel@tonic-gate 		 */
40097c478bd9Sstevel@tonic-gate 		if (stp->sd_anchor != 0) {
40107c478bd9Sstevel@tonic-gate 			pos = STRUCT_FGET(strmodinsert, pos);
40117c478bd9Sstevel@tonic-gate 			if (pos >= (stp->sd_pushcnt - stp->sd_anchor))
40127c478bd9Sstevel@tonic-gate 				stp->sd_anchor++;
40137c478bd9Sstevel@tonic-gate 		}
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate 		strendplumb(stp);
40167c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
40177c478bd9Sstevel@tonic-gate 		return (0);
40187c478bd9Sstevel@tonic-gate 	}
40197c478bd9Sstevel@tonic-gate 
40207c478bd9Sstevel@tonic-gate 	case _I_REMOVE:
40217c478bd9Sstevel@tonic-gate 	{
40227c478bd9Sstevel@tonic-gate 		/*
40237c478bd9Sstevel@tonic-gate 		 * To remove a module with a given name in a stream.  The
40247c478bd9Sstevel@tonic-gate 		 * caller of this ioctl needs to provide both the name and
40257c478bd9Sstevel@tonic-gate 		 * the position of the module to be removed.  This eliminates
40267c478bd9Sstevel@tonic-gate 		 * the ambiguity of removal if a module is inserted/pushed
40277c478bd9Sstevel@tonic-gate 		 * multiple times in a stream.  In the first release, only
40287c478bd9Sstevel@tonic-gate 		 * allow privileged user to use this ioctl.
40297c478bd9Sstevel@tonic-gate 		 *
40307c478bd9Sstevel@tonic-gate 		 * Note that we do not plan to support this ioctl
40317c478bd9Sstevel@tonic-gate 		 * on pipes in the first release.  We want to learn more
40327c478bd9Sstevel@tonic-gate 		 * about the implications of these ioctls before extending
40337c478bd9Sstevel@tonic-gate 		 * their support.  And we do not think these features are
40347c478bd9Sstevel@tonic-gate 		 * valuable for pipes.
40357c478bd9Sstevel@tonic-gate 		 *
40367c478bd9Sstevel@tonic-gate 		 * Neither do we support O/C hot stream.  Note that only
40377c478bd9Sstevel@tonic-gate 		 * the upper streams of TCP/IP stack are O/C hot streams.
40387c478bd9Sstevel@tonic-gate 		 * The lower IP stream is not.
40397c478bd9Sstevel@tonic-gate 		 * When there is a O/C cold barrier we do not allow removal
40407c478bd9Sstevel@tonic-gate 		 * below the barrier.
40417c478bd9Sstevel@tonic-gate 		 *
40427c478bd9Sstevel@tonic-gate 		 * Also note that _I_REMOVE cannot be used to remove a
40437c478bd9Sstevel@tonic-gate 		 * driver or the stream head.
40447c478bd9Sstevel@tonic-gate 		 */
40457c478bd9Sstevel@tonic-gate 		STRUCT_DECL(strmodconf, strmodremove);
40467c478bd9Sstevel@tonic-gate 		queue_t	*q;
40477c478bd9Sstevel@tonic-gate 		int pos;
40487c478bd9Sstevel@tonic-gate 		char mod_name[FMNAMESZ + 1];
40497c478bd9Sstevel@tonic-gate 		boolean_t is_remove;
40507c478bd9Sstevel@tonic-gate 
40517c478bd9Sstevel@tonic-gate 		STRUCT_INIT(strmodremove, flag);
40527c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
40537c478bd9Sstevel@tonic-gate 			return (ENXIO);
40547c478bd9Sstevel@tonic-gate 		if (STRMATED(stp))
40557c478bd9Sstevel@tonic-gate 			return (EINVAL);
40567c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
40577c478bd9Sstevel@tonic-gate 			return (error);
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, STRUCT_BUF(strmodremove),
40607c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strmodremove), copyflag);
40617c478bd9Sstevel@tonic-gate 		if (error)
40627c478bd9Sstevel@tonic-gate 			return (error);
40637c478bd9Sstevel@tonic-gate 
40647c478bd9Sstevel@tonic-gate 		error = (copyflag & U_TO_K ? copyinstr :
40657c478bd9Sstevel@tonic-gate 		    copystr)(STRUCT_FGETP(strmodremove, mod_name),
40667c478bd9Sstevel@tonic-gate 		    mod_name, FMNAMESZ + 1, NULL);
40677c478bd9Sstevel@tonic-gate 		if (error)
40687c478bd9Sstevel@tonic-gate 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 		if ((error = strstartplumb(stp, flag, cmd)) != 0)
40717c478bd9Sstevel@tonic-gate 			return (error);
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate 		/*
40747c478bd9Sstevel@tonic-gate 		 * Match the name of given module to the name of module at
40757c478bd9Sstevel@tonic-gate 		 * the given position.
40767c478bd9Sstevel@tonic-gate 		 */
40777c478bd9Sstevel@tonic-gate 		pos = STRUCT_FGET(strmodremove, pos);
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 		is_remove = (pos != 0);
40807c478bd9Sstevel@tonic-gate 		for (q = stp->sd_wrq->q_next; SAMESTR(q) && pos > 0;
40817c478bd9Sstevel@tonic-gate 		    q = q->q_next, pos--)
40827c478bd9Sstevel@tonic-gate 			;
40837c478bd9Sstevel@tonic-gate 		if (pos > 0 || ! SAMESTR(q) ||
40847c478bd9Sstevel@tonic-gate 		    strncmp(q->q_qinfo->qi_minfo->mi_idname, mod_name,
40857c478bd9Sstevel@tonic-gate 		    strlen(q->q_qinfo->qi_minfo->mi_idname)) != 0) {
40867c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
40877c478bd9Sstevel@tonic-gate 			strendplumb(stp);
40887c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
40897c478bd9Sstevel@tonic-gate 			return (EINVAL);
40907c478bd9Sstevel@tonic-gate 		}
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate 		ASSERT(!(q->q_flag & QREADR));
40937c478bd9Sstevel@tonic-gate 		qdetach(_RD(q), 1, flag, crp, is_remove);
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
40967c478bd9Sstevel@tonic-gate 
40977c478bd9Sstevel@tonic-gate 		/*
40987c478bd9Sstevel@tonic-gate 		 * As a performance concern we are caching the values of
40997c478bd9Sstevel@tonic-gate 		 * q_minpsz and q_maxpsz of the module below the stream
41007c478bd9Sstevel@tonic-gate 		 * head in the stream head.
41017c478bd9Sstevel@tonic-gate 		 */
41027c478bd9Sstevel@tonic-gate 		if (!is_remove) {
41037c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(wrq->q_next));
41047c478bd9Sstevel@tonic-gate 			rmin = wrq->q_next->q_minpsz;
41057c478bd9Sstevel@tonic-gate 			rmax = wrq->q_next->q_maxpsz;
41067c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(wrq->q_next));
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 			/* Do this processing here as a performance concern */
41097c478bd9Sstevel@tonic-gate 			if (strmsgsz != 0) {
41107c478bd9Sstevel@tonic-gate 				if (rmax == INFPSZ)
41117c478bd9Sstevel@tonic-gate 					rmax = strmsgsz;
41127c478bd9Sstevel@tonic-gate 				else  {
41137c478bd9Sstevel@tonic-gate 					if (vp->v_type == VFIFO)
41147c478bd9Sstevel@tonic-gate 						rmax = MIN(PIPE_BUF, rmax);
41157c478bd9Sstevel@tonic-gate 					else	rmax = MIN(strmsgsz, rmax);
41167c478bd9Sstevel@tonic-gate 				}
41177c478bd9Sstevel@tonic-gate 			}
41187c478bd9Sstevel@tonic-gate 
41197c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(wrq));
41207c478bd9Sstevel@tonic-gate 			stp->sd_qn_minpsz = rmin;
41217c478bd9Sstevel@tonic-gate 			stp->sd_qn_maxpsz = rmax;
41227c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(wrq));
41237c478bd9Sstevel@tonic-gate 		}
41247c478bd9Sstevel@tonic-gate 
41257c478bd9Sstevel@tonic-gate 		/*
41267c478bd9Sstevel@tonic-gate 		 * Need to update the anchor value if this module is removed
41277c478bd9Sstevel@tonic-gate 		 * at or below the anchor point.  If the removed module is at
41287c478bd9Sstevel@tonic-gate 		 * the anchor point, remove the anchor for this stream if
41297c478bd9Sstevel@tonic-gate 		 * there is no module above the anchor point.  Otherwise, if
41307c478bd9Sstevel@tonic-gate 		 * the removed module is below the anchor point, decrement the
41317c478bd9Sstevel@tonic-gate 		 * anchor point by 1.
41327c478bd9Sstevel@tonic-gate 		 */
41337c478bd9Sstevel@tonic-gate 		if (stp->sd_anchor != 0) {
41347c478bd9Sstevel@tonic-gate 			pos = STRUCT_FGET(strmodremove, pos);
41357c478bd9Sstevel@tonic-gate 			if (pos == 0)
41367c478bd9Sstevel@tonic-gate 				stp->sd_anchor = 0;
41377c478bd9Sstevel@tonic-gate 			else if (pos > (stp->sd_pushcnt - stp->sd_anchor + 1))
41387c478bd9Sstevel@tonic-gate 				stp->sd_anchor--;
41397c478bd9Sstevel@tonic-gate 		}
41407c478bd9Sstevel@tonic-gate 
41417c478bd9Sstevel@tonic-gate 		strendplumb(stp);
41427c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
41437c478bd9Sstevel@tonic-gate 		return (0);
41447c478bd9Sstevel@tonic-gate 	}
41457c478bd9Sstevel@tonic-gate 
41467c478bd9Sstevel@tonic-gate 	case I_ANCHOR:
41477c478bd9Sstevel@tonic-gate 		/*
41487c478bd9Sstevel@tonic-gate 		 * Set the anchor position on the stream to reside at
41497c478bd9Sstevel@tonic-gate 		 * the top module (in other words, the top module
41507c478bd9Sstevel@tonic-gate 		 * cannot be popped).  Anchors with a FIFO make no
41517c478bd9Sstevel@tonic-gate 		 * obvious sense, so they're not allowed.
41527c478bd9Sstevel@tonic-gate 		 */
41537c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
41547c478bd9Sstevel@tonic-gate 
41557c478bd9Sstevel@tonic-gate 		if (stp->sd_vnode->v_type == VFIFO) {
41567c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
41577c478bd9Sstevel@tonic-gate 			return (EINVAL);
41587c478bd9Sstevel@tonic-gate 		}
41597c478bd9Sstevel@tonic-gate 
41607c478bd9Sstevel@tonic-gate 		stp->sd_anchor = stp->sd_pushcnt;
41617c478bd9Sstevel@tonic-gate 
41627c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
41637c478bd9Sstevel@tonic-gate 		return (0);
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate 	case I_LOOK:
41667c478bd9Sstevel@tonic-gate 		/*
41677c478bd9Sstevel@tonic-gate 		 * Get name of first module downstream.
41687c478bd9Sstevel@tonic-gate 		 * If no module, return an error.
41697c478bd9Sstevel@tonic-gate 		 */
41707c478bd9Sstevel@tonic-gate 	    {
41717c478bd9Sstevel@tonic-gate 		claimstr(wrq);
41727c478bd9Sstevel@tonic-gate 		if (_SAMESTR(wrq) && wrq->q_next->q_next) {
41737c478bd9Sstevel@tonic-gate 			char *name = wrq->q_next->q_qinfo->qi_minfo->mi_idname;
41747c478bd9Sstevel@tonic-gate 			error = strcopyout(name, (void *)arg, strlen(name) + 1,
41757c478bd9Sstevel@tonic-gate 			    copyflag);
41767c478bd9Sstevel@tonic-gate 			releasestr(wrq);
41777c478bd9Sstevel@tonic-gate 			return (error);
41787c478bd9Sstevel@tonic-gate 		}
41797c478bd9Sstevel@tonic-gate 		releasestr(wrq);
41807c478bd9Sstevel@tonic-gate 		return (EINVAL);
41817c478bd9Sstevel@tonic-gate 	    }
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate 	case I_LINK:
41847c478bd9Sstevel@tonic-gate 	case I_PLINK:
41857c478bd9Sstevel@tonic-gate 		/*
41867c478bd9Sstevel@tonic-gate 		 * Link a multiplexor.
41877c478bd9Sstevel@tonic-gate 		 */
41887c478bd9Sstevel@tonic-gate 		return (mlink(vp, cmd, (int)arg, crp, rvalp, 0));
41897c478bd9Sstevel@tonic-gate 
41907c478bd9Sstevel@tonic-gate 	case _I_PLINK_LH:
41917c478bd9Sstevel@tonic-gate 		/*
41927c478bd9Sstevel@tonic-gate 		 * Link a multiplexor: Call must originate from kernel.
41937c478bd9Sstevel@tonic-gate 		 */
41947c478bd9Sstevel@tonic-gate 		if (kioctl)
41957c478bd9Sstevel@tonic-gate 			return (ldi_mlink_lh(vp, cmd, arg, crp, rvalp));
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate 		return (EINVAL);
41987c478bd9Sstevel@tonic-gate 	case I_UNLINK:
41997c478bd9Sstevel@tonic-gate 	case I_PUNLINK:
42007c478bd9Sstevel@tonic-gate 		/*
42017c478bd9Sstevel@tonic-gate 		 * Unlink a multiplexor.
42027c478bd9Sstevel@tonic-gate 		 * If arg is -1, unlink all links for which this is the
42037c478bd9Sstevel@tonic-gate 		 * controlling stream.  Otherwise, arg is an index number
42047c478bd9Sstevel@tonic-gate 		 * for a link to be removed.
42057c478bd9Sstevel@tonic-gate 		 */
42067c478bd9Sstevel@tonic-gate 	    {
42077c478bd9Sstevel@tonic-gate 		struct linkinfo *linkp;
42087c478bd9Sstevel@tonic-gate 		int native_arg = (int)arg;
42097c478bd9Sstevel@tonic-gate 		int type;
42107c478bd9Sstevel@tonic-gate 
42117c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_STREAMS_FR,
42127c478bd9Sstevel@tonic-gate 			TR_I_UNLINK, "I_UNLINK/I_PUNLINK:%p", stp);
42137c478bd9Sstevel@tonic-gate 		if (vp->v_type == VFIFO) {
42147c478bd9Sstevel@tonic-gate 			return (EINVAL);
42157c478bd9Sstevel@tonic-gate 		}
42167c478bd9Sstevel@tonic-gate 		if (cmd == I_UNLINK)
42177c478bd9Sstevel@tonic-gate 			type = LINKNORMAL;
42187c478bd9Sstevel@tonic-gate 		else	/* I_PUNLINK */
42197c478bd9Sstevel@tonic-gate 			type = LINKPERSIST;
42207c478bd9Sstevel@tonic-gate 		if (native_arg == 0) {
42217c478bd9Sstevel@tonic-gate 			return (EINVAL);
42227c478bd9Sstevel@tonic-gate 		}
42237c478bd9Sstevel@tonic-gate 		if (native_arg == MUXID_ALL)
42247c478bd9Sstevel@tonic-gate 			error = munlinkall(stp, type, crp, rvalp);
42257c478bd9Sstevel@tonic-gate 		else {
42267c478bd9Sstevel@tonic-gate 			mutex_enter(&muxifier);
42277c478bd9Sstevel@tonic-gate 			if (!(linkp = findlinks(stp, (int)arg, type))) {
42287c478bd9Sstevel@tonic-gate 				/* invalid user supplied index number */
42297c478bd9Sstevel@tonic-gate 				mutex_exit(&muxifier);
42307c478bd9Sstevel@tonic-gate 				return (EINVAL);
42317c478bd9Sstevel@tonic-gate 			}
42327c478bd9Sstevel@tonic-gate 			/* munlink drops the muxifier lock */
42337c478bd9Sstevel@tonic-gate 			error = munlink(stp, linkp, type, crp, rvalp);
42347c478bd9Sstevel@tonic-gate 		}
42357c478bd9Sstevel@tonic-gate 		return (error);
42367c478bd9Sstevel@tonic-gate 	    }
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 	case I_FLUSH:
42397c478bd9Sstevel@tonic-gate 		/*
42407c478bd9Sstevel@tonic-gate 		 * send a flush message downstream
42417c478bd9Sstevel@tonic-gate 		 * flush message can indicate
42427c478bd9Sstevel@tonic-gate 		 * FLUSHR - flush read queue
42437c478bd9Sstevel@tonic-gate 		 * FLUSHW - flush write queue
42447c478bd9Sstevel@tonic-gate 		 * FLUSHRW - flush read/write queue
42457c478bd9Sstevel@tonic-gate 		 */
42467c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
42477c478bd9Sstevel@tonic-gate 			return (ENXIO);
42487c478bd9Sstevel@tonic-gate 		if (arg & ~FLUSHRW)
42497c478bd9Sstevel@tonic-gate 			return (EINVAL);
42507c478bd9Sstevel@tonic-gate 
42517c478bd9Sstevel@tonic-gate 		for (;;) {
42527c478bd9Sstevel@tonic-gate 			if (putnextctl1(stp->sd_wrq, M_FLUSH, (int)arg)) {
42537c478bd9Sstevel@tonic-gate 				break;
42547c478bd9Sstevel@tonic-gate 			}
42557c478bd9Sstevel@tonic-gate 			if (error = strwaitbuf(1, BPRI_HI)) {
42567c478bd9Sstevel@tonic-gate 				return (error);
42577c478bd9Sstevel@tonic-gate 			}
42587c478bd9Sstevel@tonic-gate 		}
42597c478bd9Sstevel@tonic-gate 
42607c478bd9Sstevel@tonic-gate 		/*
42617c478bd9Sstevel@tonic-gate 		 * Send down an unsupported ioctl and wait for the nack
42627c478bd9Sstevel@tonic-gate 		 * in order to allow the M_FLUSH to propagate back
42637c478bd9Sstevel@tonic-gate 		 * up to the stream head.
42647c478bd9Sstevel@tonic-gate 		 * Replaces if (qready()) runqueues();
42657c478bd9Sstevel@tonic-gate 		 */
42667c478bd9Sstevel@tonic-gate 		strioc.ic_cmd = -1;	/* The unsupported ioctl */
42677c478bd9Sstevel@tonic-gate 		strioc.ic_timout = 0;
42687c478bd9Sstevel@tonic-gate 		strioc.ic_len = 0;
42697c478bd9Sstevel@tonic-gate 		strioc.ic_dp = NULL;
42707c478bd9Sstevel@tonic-gate 		(void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
42717c478bd9Sstevel@tonic-gate 		*rvalp = 0;
42727c478bd9Sstevel@tonic-gate 		return (0);
42737c478bd9Sstevel@tonic-gate 
42747c478bd9Sstevel@tonic-gate 	case I_FLUSHBAND:
42757c478bd9Sstevel@tonic-gate 	    {
42767c478bd9Sstevel@tonic-gate 		struct bandinfo binfo;
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, &binfo, sizeof (binfo),
42797c478bd9Sstevel@tonic-gate 		    copyflag);
42807c478bd9Sstevel@tonic-gate 		if (error)
42817c478bd9Sstevel@tonic-gate 			return (error);
42827c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
42837c478bd9Sstevel@tonic-gate 			return (ENXIO);
42847c478bd9Sstevel@tonic-gate 		if (binfo.bi_flag & ~FLUSHRW)
42857c478bd9Sstevel@tonic-gate 			return (EINVAL);
42867c478bd9Sstevel@tonic-gate 		while (!(mp = allocb(2, BPRI_HI))) {
42877c478bd9Sstevel@tonic-gate 			if (error = strwaitbuf(2, BPRI_HI))
42887c478bd9Sstevel@tonic-gate 				return (error);
42897c478bd9Sstevel@tonic-gate 		}
42907c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_FLUSH;
42917c478bd9Sstevel@tonic-gate 		*mp->b_wptr++ = binfo.bi_flag | FLUSHBAND;
42927c478bd9Sstevel@tonic-gate 		*mp->b_wptr++ = binfo.bi_pri;
42937c478bd9Sstevel@tonic-gate 		putnext(stp->sd_wrq, mp);
42947c478bd9Sstevel@tonic-gate 		/*
42957c478bd9Sstevel@tonic-gate 		 * Send down an unsupported ioctl and wait for the nack
42967c478bd9Sstevel@tonic-gate 		 * in order to allow the M_FLUSH to propagate back
42977c478bd9Sstevel@tonic-gate 		 * up to the stream head.
42987c478bd9Sstevel@tonic-gate 		 * Replaces if (qready()) runqueues();
42997c478bd9Sstevel@tonic-gate 		 */
43007c478bd9Sstevel@tonic-gate 		strioc.ic_cmd = -1;	/* The unsupported ioctl */
43017c478bd9Sstevel@tonic-gate 		strioc.ic_timout = 0;
43027c478bd9Sstevel@tonic-gate 		strioc.ic_len = 0;
43037c478bd9Sstevel@tonic-gate 		strioc.ic_dp = NULL;
43047c478bd9Sstevel@tonic-gate 		(void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
43057c478bd9Sstevel@tonic-gate 		*rvalp = 0;
43067c478bd9Sstevel@tonic-gate 		return (0);
43077c478bd9Sstevel@tonic-gate 	    }
43087c478bd9Sstevel@tonic-gate 
43097c478bd9Sstevel@tonic-gate 	case I_SRDOPT:
43107c478bd9Sstevel@tonic-gate 		/*
43117c478bd9Sstevel@tonic-gate 		 * Set read options
43127c478bd9Sstevel@tonic-gate 		 *
43137c478bd9Sstevel@tonic-gate 		 * RNORM - default stream mode
43147c478bd9Sstevel@tonic-gate 		 * RMSGN - message no discard
43157c478bd9Sstevel@tonic-gate 		 * RMSGD - message discard
43167c478bd9Sstevel@tonic-gate 		 * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
43177c478bd9Sstevel@tonic-gate 		 * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
43187c478bd9Sstevel@tonic-gate 		 * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
43197c478bd9Sstevel@tonic-gate 		 */
43207c478bd9Sstevel@tonic-gate 		if (arg & ~(RMODEMASK | RPROTMASK))
43217c478bd9Sstevel@tonic-gate 			return (EINVAL);
43227c478bd9Sstevel@tonic-gate 
43237c478bd9Sstevel@tonic-gate 		if ((arg & (RMSGD|RMSGN)) == (RMSGD|RMSGN))
43247c478bd9Sstevel@tonic-gate 			return (EINVAL);
43257c478bd9Sstevel@tonic-gate 
43267c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
43277c478bd9Sstevel@tonic-gate 		switch (arg & RMODEMASK) {
43287c478bd9Sstevel@tonic-gate 		case RNORM:
43297c478bd9Sstevel@tonic-gate 			stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
43307c478bd9Sstevel@tonic-gate 			break;
43317c478bd9Sstevel@tonic-gate 		case RMSGD:
43327c478bd9Sstevel@tonic-gate 			stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGNODIS) |
43337c478bd9Sstevel@tonic-gate 			    RD_MSGDIS;
43347c478bd9Sstevel@tonic-gate 			break;
43357c478bd9Sstevel@tonic-gate 		case RMSGN:
43367c478bd9Sstevel@tonic-gate 			stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGDIS) |
43377c478bd9Sstevel@tonic-gate 			    RD_MSGNODIS;
43387c478bd9Sstevel@tonic-gate 			break;
43397c478bd9Sstevel@tonic-gate 		}
43407c478bd9Sstevel@tonic-gate 
43417c478bd9Sstevel@tonic-gate 		switch (arg & RPROTMASK) {
43427c478bd9Sstevel@tonic-gate 		case RPROTNORM:
43437c478bd9Sstevel@tonic-gate 			stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
43447c478bd9Sstevel@tonic-gate 			break;
43457c478bd9Sstevel@tonic-gate 
43467c478bd9Sstevel@tonic-gate 		case RPROTDAT:
43477c478bd9Sstevel@tonic-gate 			stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDIS) |
43487c478bd9Sstevel@tonic-gate 			    RD_PROTDAT);
43497c478bd9Sstevel@tonic-gate 			break;
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate 		case RPROTDIS:
43527c478bd9Sstevel@tonic-gate 			stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDAT) |
43537c478bd9Sstevel@tonic-gate 			    RD_PROTDIS);
43547c478bd9Sstevel@tonic-gate 			break;
43557c478bd9Sstevel@tonic-gate 		}
43567c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
43577c478bd9Sstevel@tonic-gate 		return (0);
43587c478bd9Sstevel@tonic-gate 
43597c478bd9Sstevel@tonic-gate 	case I_GRDOPT:
43607c478bd9Sstevel@tonic-gate 		/*
43617c478bd9Sstevel@tonic-gate 		 * Get read option and return the value
43627c478bd9Sstevel@tonic-gate 		 * to spot pointed to by arg
43637c478bd9Sstevel@tonic-gate 		 */
43647c478bd9Sstevel@tonic-gate 	    {
43657c478bd9Sstevel@tonic-gate 		int rdopt;
43667c478bd9Sstevel@tonic-gate 
43677c478bd9Sstevel@tonic-gate 		rdopt = ((stp->sd_read_opt & RD_MSGDIS) ? RMSGD :
43687c478bd9Sstevel@tonic-gate 		    ((stp->sd_read_opt & RD_MSGNODIS) ? RMSGN : RNORM));
43697c478bd9Sstevel@tonic-gate 		rdopt |= ((stp->sd_read_opt & RD_PROTDAT) ? RPROTDAT :
43707c478bd9Sstevel@tonic-gate 		    ((stp->sd_read_opt & RD_PROTDIS) ? RPROTDIS : RPROTNORM));
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate 		return (strcopyout(&rdopt, (void *)arg, sizeof (int),
43737c478bd9Sstevel@tonic-gate 		    copyflag));
43747c478bd9Sstevel@tonic-gate 	    }
43757c478bd9Sstevel@tonic-gate 
43767c478bd9Sstevel@tonic-gate 	case I_SERROPT:
43777c478bd9Sstevel@tonic-gate 		/*
43787c478bd9Sstevel@tonic-gate 		 * Set error options
43797c478bd9Sstevel@tonic-gate 		 *
43807c478bd9Sstevel@tonic-gate 		 * RERRNORM - persistent read errors
43817c478bd9Sstevel@tonic-gate 		 * RERRNONPERSIST - non-persistent read errors
43827c478bd9Sstevel@tonic-gate 		 * WERRNORM - persistent write errors
43837c478bd9Sstevel@tonic-gate 		 * WERRNONPERSIST - non-persistent write errors
43847c478bd9Sstevel@tonic-gate 		 */
43857c478bd9Sstevel@tonic-gate 		if (arg & ~(RERRMASK | WERRMASK))
43867c478bd9Sstevel@tonic-gate 			return (EINVAL);
43877c478bd9Sstevel@tonic-gate 
43887c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
43897c478bd9Sstevel@tonic-gate 		switch (arg & RERRMASK) {
43907c478bd9Sstevel@tonic-gate 		case RERRNORM:
43917c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRDERRNONPERSIST;
43927c478bd9Sstevel@tonic-gate 			break;
43937c478bd9Sstevel@tonic-gate 		case RERRNONPERSIST:
43947c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STRDERRNONPERSIST;
43957c478bd9Sstevel@tonic-gate 			break;
43967c478bd9Sstevel@tonic-gate 		}
43977c478bd9Sstevel@tonic-gate 		switch (arg & WERRMASK) {
43987c478bd9Sstevel@tonic-gate 		case WERRNORM:
43997c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STWRERRNONPERSIST;
44007c478bd9Sstevel@tonic-gate 			break;
44017c478bd9Sstevel@tonic-gate 		case WERRNONPERSIST:
44027c478bd9Sstevel@tonic-gate 			stp->sd_flag |= STWRERRNONPERSIST;
44037c478bd9Sstevel@tonic-gate 			break;
44047c478bd9Sstevel@tonic-gate 		}
44057c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
44067c478bd9Sstevel@tonic-gate 		return (0);
44077c478bd9Sstevel@tonic-gate 
44087c478bd9Sstevel@tonic-gate 	case I_GERROPT:
44097c478bd9Sstevel@tonic-gate 		/*
44107c478bd9Sstevel@tonic-gate 		 * Get error option and return the value
44117c478bd9Sstevel@tonic-gate 		 * to spot pointed to by arg
44127c478bd9Sstevel@tonic-gate 		 */
44137c478bd9Sstevel@tonic-gate 	    {
44147c478bd9Sstevel@tonic-gate 		int erropt = 0;
44157c478bd9Sstevel@tonic-gate 
44167c478bd9Sstevel@tonic-gate 		erropt |= (stp->sd_flag & STRDERRNONPERSIST) ? RERRNONPERSIST :
44177c478bd9Sstevel@tonic-gate 			RERRNORM;
44187c478bd9Sstevel@tonic-gate 		erropt |= (stp->sd_flag & STWRERRNONPERSIST) ? WERRNONPERSIST :
44197c478bd9Sstevel@tonic-gate 			WERRNORM;
44207c478bd9Sstevel@tonic-gate 		return (strcopyout(&erropt, (void *)arg, sizeof (int),
44217c478bd9Sstevel@tonic-gate 		    copyflag));
44227c478bd9Sstevel@tonic-gate 	    }
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 	case I_SETSIG:
44257c478bd9Sstevel@tonic-gate 		/*
44267c478bd9Sstevel@tonic-gate 		 * Register the calling proc to receive the SIGPOLL
44277c478bd9Sstevel@tonic-gate 		 * signal based on the events given in arg.  If
44287c478bd9Sstevel@tonic-gate 		 * arg is zero, remove the proc from register list.
44297c478bd9Sstevel@tonic-gate 		 */
44307c478bd9Sstevel@tonic-gate 	    {
44317c478bd9Sstevel@tonic-gate 		strsig_t *ssp, *pssp;
44327c478bd9Sstevel@tonic-gate 		struct pid *pidp;
44337c478bd9Sstevel@tonic-gate 
44347c478bd9Sstevel@tonic-gate 		pssp = NULL;
44357c478bd9Sstevel@tonic-gate 		pidp = curproc->p_pidp;
44367c478bd9Sstevel@tonic-gate 		/*
44377c478bd9Sstevel@tonic-gate 		 * Hold sd_lock to prevent traversal of sd_siglist while
44387c478bd9Sstevel@tonic-gate 		 * it is modified.
44397c478bd9Sstevel@tonic-gate 		 */
44407c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
44417c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp && (ssp->ss_pidp != pidp);
44427c478bd9Sstevel@tonic-gate 			pssp = ssp, ssp = ssp->ss_next)
44437c478bd9Sstevel@tonic-gate 			;
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate 		if (arg) {
44467c478bd9Sstevel@tonic-gate 			if (arg & ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
44477c478bd9Sstevel@tonic-gate 			    S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
44487c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
44497c478bd9Sstevel@tonic-gate 				return (EINVAL);
44507c478bd9Sstevel@tonic-gate 			}
44517c478bd9Sstevel@tonic-gate 			if ((arg & S_BANDURG) && !(arg & S_RDBAND)) {
44527c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
44537c478bd9Sstevel@tonic-gate 				return (EINVAL);
44547c478bd9Sstevel@tonic-gate 			}
44557c478bd9Sstevel@tonic-gate 
44567c478bd9Sstevel@tonic-gate 			/*
44577c478bd9Sstevel@tonic-gate 			 * If proc not already registered, add it
44587c478bd9Sstevel@tonic-gate 			 * to list.
44597c478bd9Sstevel@tonic-gate 			 */
44607c478bd9Sstevel@tonic-gate 			if (!ssp) {
44617c478bd9Sstevel@tonic-gate 				ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
44627c478bd9Sstevel@tonic-gate 				ssp->ss_pidp = pidp;
44637c478bd9Sstevel@tonic-gate 				ssp->ss_pid = pidp->pid_id;
44647c478bd9Sstevel@tonic-gate 				ssp->ss_next = NULL;
44657c478bd9Sstevel@tonic-gate 				if (pssp)
44667c478bd9Sstevel@tonic-gate 					pssp->ss_next = ssp;
44677c478bd9Sstevel@tonic-gate 				else
44687c478bd9Sstevel@tonic-gate 					stp->sd_siglist = ssp;
44697c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
44707c478bd9Sstevel@tonic-gate 				PID_HOLD(pidp);
44717c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
44727c478bd9Sstevel@tonic-gate 			}
44737c478bd9Sstevel@tonic-gate 
44747c478bd9Sstevel@tonic-gate 			/*
44757c478bd9Sstevel@tonic-gate 			 * Set events.
44767c478bd9Sstevel@tonic-gate 			 */
44777c478bd9Sstevel@tonic-gate 			ssp->ss_events = (int)arg;
44787c478bd9Sstevel@tonic-gate 		} else {
44797c478bd9Sstevel@tonic-gate 			/*
44807c478bd9Sstevel@tonic-gate 			 * Remove proc from register list.
44817c478bd9Sstevel@tonic-gate 			 */
44827c478bd9Sstevel@tonic-gate 			if (ssp) {
44837c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
44847c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
44857c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
44867c478bd9Sstevel@tonic-gate 				if (pssp)
44877c478bd9Sstevel@tonic-gate 					pssp->ss_next = ssp->ss_next;
44887c478bd9Sstevel@tonic-gate 				else
44897c478bd9Sstevel@tonic-gate 					stp->sd_siglist = ssp->ss_next;
44907c478bd9Sstevel@tonic-gate 				kmem_free(ssp, sizeof (strsig_t));
44917c478bd9Sstevel@tonic-gate 			} else {
44927c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
44937c478bd9Sstevel@tonic-gate 				return (EINVAL);
44947c478bd9Sstevel@tonic-gate 			}
44957c478bd9Sstevel@tonic-gate 		}
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate 		/*
44987c478bd9Sstevel@tonic-gate 		 * Recalculate OR of sig events.
44997c478bd9Sstevel@tonic-gate 		 */
45007c478bd9Sstevel@tonic-gate 		stp->sd_sigflags = 0;
45017c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
45027c478bd9Sstevel@tonic-gate 			stp->sd_sigflags |= ssp->ss_events;
45037c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
45047c478bd9Sstevel@tonic-gate 		return (0);
45057c478bd9Sstevel@tonic-gate 	    }
45067c478bd9Sstevel@tonic-gate 
45077c478bd9Sstevel@tonic-gate 	case I_GETSIG:
45087c478bd9Sstevel@tonic-gate 		/*
45097c478bd9Sstevel@tonic-gate 		 * Return (in arg) the current registration of events
45107c478bd9Sstevel@tonic-gate 		 * for which the calling proc is to be signaled.
45117c478bd9Sstevel@tonic-gate 		 */
45127c478bd9Sstevel@tonic-gate 	    {
45137c478bd9Sstevel@tonic-gate 		struct strsig *ssp;
45147c478bd9Sstevel@tonic-gate 		struct pid  *pidp;
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate 		pidp = curproc->p_pidp;
45177c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
45187c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
45197c478bd9Sstevel@tonic-gate 			if (ssp->ss_pidp == pidp) {
45207c478bd9Sstevel@tonic-gate 				error = strcopyout(&ssp->ss_events, (void *)arg,
45217c478bd9Sstevel@tonic-gate 				    sizeof (int), copyflag);
45227c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
45237c478bd9Sstevel@tonic-gate 				return (error);
45247c478bd9Sstevel@tonic-gate 			}
45257c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
45267c478bd9Sstevel@tonic-gate 		return (EINVAL);
45277c478bd9Sstevel@tonic-gate 	    }
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	case I_ESETSIG:
45307c478bd9Sstevel@tonic-gate 		/*
45317c478bd9Sstevel@tonic-gate 		 * Register the ss_pid to receive the SIGPOLL
45327c478bd9Sstevel@tonic-gate 		 * signal based on the events is ss_events arg.  If
45337c478bd9Sstevel@tonic-gate 		 * ss_events is zero, remove the proc from register list.
45347c478bd9Sstevel@tonic-gate 		 */
45357c478bd9Sstevel@tonic-gate 	{
45367c478bd9Sstevel@tonic-gate 		struct strsig *ssp, *pssp;
45377c478bd9Sstevel@tonic-gate 		struct proc *proc;
45387c478bd9Sstevel@tonic-gate 		struct pid  *pidp;
45397c478bd9Sstevel@tonic-gate 		pid_t pid;
45407c478bd9Sstevel@tonic-gate 		struct strsigset ss;
45417c478bd9Sstevel@tonic-gate 
45427c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
45437c478bd9Sstevel@tonic-gate 		if (error)
45447c478bd9Sstevel@tonic-gate 			return (error);
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 		pid = ss.ss_pid;
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate 		if (ss.ss_events != 0) {
45497c478bd9Sstevel@tonic-gate 			/*
45507c478bd9Sstevel@tonic-gate 			 * Permissions check by sending signal 0.
45517c478bd9Sstevel@tonic-gate 			 * Note that when kill fails it does a set_errno
45527c478bd9Sstevel@tonic-gate 			 * causing the system call to fail.
45537c478bd9Sstevel@tonic-gate 			 */
45547c478bd9Sstevel@tonic-gate 			error = kill(pid, 0);
45557c478bd9Sstevel@tonic-gate 			if (error) {
45567c478bd9Sstevel@tonic-gate 				return (error);
45577c478bd9Sstevel@tonic-gate 			}
45587c478bd9Sstevel@tonic-gate 		}
45597c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
45607c478bd9Sstevel@tonic-gate 		if (pid == 0)
45617c478bd9Sstevel@tonic-gate 			proc = curproc;
45627c478bd9Sstevel@tonic-gate 		else if (pid < 0)
45637c478bd9Sstevel@tonic-gate 			proc = pgfind(-pid);
45647c478bd9Sstevel@tonic-gate 		else
45657c478bd9Sstevel@tonic-gate 			proc = prfind(pid);
45667c478bd9Sstevel@tonic-gate 		if (proc == NULL) {
45677c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
45687c478bd9Sstevel@tonic-gate 			return (ESRCH);
45697c478bd9Sstevel@tonic-gate 		}
45707c478bd9Sstevel@tonic-gate 		if (pid < 0)
45717c478bd9Sstevel@tonic-gate 			pidp = proc->p_pgidp;
45727c478bd9Sstevel@tonic-gate 		else
45737c478bd9Sstevel@tonic-gate 			pidp = proc->p_pidp;
45747c478bd9Sstevel@tonic-gate 		ASSERT(pidp);
45757c478bd9Sstevel@tonic-gate 		/*
45767c478bd9Sstevel@tonic-gate 		 * Get a hold on the pid structure while referencing it.
45777c478bd9Sstevel@tonic-gate 		 * There is a separate PID_HOLD should it be inserted
45787c478bd9Sstevel@tonic-gate 		 * in the list below.
45797c478bd9Sstevel@tonic-gate 		 */
45807c478bd9Sstevel@tonic-gate 		PID_HOLD(pidp);
45817c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate 		pssp = NULL;
45847c478bd9Sstevel@tonic-gate 		/*
45857c478bd9Sstevel@tonic-gate 		 * Hold sd_lock to prevent traversal of sd_siglist while
45867c478bd9Sstevel@tonic-gate 		 * it is modified.
45877c478bd9Sstevel@tonic-gate 		 */
45887c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
45897c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp && (ssp->ss_pid != pid);
45907c478bd9Sstevel@tonic-gate 				pssp = ssp, ssp = ssp->ss_next)
45917c478bd9Sstevel@tonic-gate 			;
45927c478bd9Sstevel@tonic-gate 
45937c478bd9Sstevel@tonic-gate 		if (ss.ss_events) {
45947c478bd9Sstevel@tonic-gate 			if (ss.ss_events &
45957c478bd9Sstevel@tonic-gate 			    ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
45967c478bd9Sstevel@tonic-gate 			    S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
45977c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
45987c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
45997c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
46007c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
46017c478bd9Sstevel@tonic-gate 				return (EINVAL);
46027c478bd9Sstevel@tonic-gate 			}
46037c478bd9Sstevel@tonic-gate 			if ((ss.ss_events & S_BANDURG) &&
46047c478bd9Sstevel@tonic-gate 			    !(ss.ss_events & S_RDBAND)) {
46057c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
46067c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
46077c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
46087c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
46097c478bd9Sstevel@tonic-gate 				return (EINVAL);
46107c478bd9Sstevel@tonic-gate 			}
46117c478bd9Sstevel@tonic-gate 
46127c478bd9Sstevel@tonic-gate 			/*
46137c478bd9Sstevel@tonic-gate 			 * If proc not already registered, add it
46147c478bd9Sstevel@tonic-gate 			 * to list.
46157c478bd9Sstevel@tonic-gate 			 */
46167c478bd9Sstevel@tonic-gate 			if (!ssp) {
46177c478bd9Sstevel@tonic-gate 				ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
46187c478bd9Sstevel@tonic-gate 				ssp->ss_pidp = pidp;
46197c478bd9Sstevel@tonic-gate 				ssp->ss_pid = pid;
46207c478bd9Sstevel@tonic-gate 				ssp->ss_next = NULL;
46217c478bd9Sstevel@tonic-gate 				if (pssp)
46227c478bd9Sstevel@tonic-gate 					pssp->ss_next = ssp;
46237c478bd9Sstevel@tonic-gate 				else
46247c478bd9Sstevel@tonic-gate 					stp->sd_siglist = ssp;
46257c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
46267c478bd9Sstevel@tonic-gate 				PID_HOLD(pidp);
46277c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
46287c478bd9Sstevel@tonic-gate 			}
46297c478bd9Sstevel@tonic-gate 
46307c478bd9Sstevel@tonic-gate 			/*
46317c478bd9Sstevel@tonic-gate 			 * Set events.
46327c478bd9Sstevel@tonic-gate 			 */
46337c478bd9Sstevel@tonic-gate 			ssp->ss_events = ss.ss_events;
46347c478bd9Sstevel@tonic-gate 		} else {
46357c478bd9Sstevel@tonic-gate 			/*
46367c478bd9Sstevel@tonic-gate 			 * Remove proc from register list.
46377c478bd9Sstevel@tonic-gate 			 */
46387c478bd9Sstevel@tonic-gate 			if (ssp) {
46397c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
46407c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
46417c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
46427c478bd9Sstevel@tonic-gate 				if (pssp)
46437c478bd9Sstevel@tonic-gate 					pssp->ss_next = ssp->ss_next;
46447c478bd9Sstevel@tonic-gate 				else
46457c478bd9Sstevel@tonic-gate 					stp->sd_siglist = ssp->ss_next;
46467c478bd9Sstevel@tonic-gate 				kmem_free(ssp, sizeof (strsig_t));
46477c478bd9Sstevel@tonic-gate 			} else {
46487c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
46497c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
46507c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
46517c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
46527c478bd9Sstevel@tonic-gate 				return (EINVAL);
46537c478bd9Sstevel@tonic-gate 			}
46547c478bd9Sstevel@tonic-gate 		}
46557c478bd9Sstevel@tonic-gate 
46567c478bd9Sstevel@tonic-gate 		/*
46577c478bd9Sstevel@tonic-gate 		 * Recalculate OR of sig events.
46587c478bd9Sstevel@tonic-gate 		 */
46597c478bd9Sstevel@tonic-gate 		stp->sd_sigflags = 0;
46607c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
46617c478bd9Sstevel@tonic-gate 			stp->sd_sigflags |= ssp->ss_events;
46627c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
46637c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
46647c478bd9Sstevel@tonic-gate 		PID_RELE(pidp);
46657c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
46667c478bd9Sstevel@tonic-gate 		return (0);
46677c478bd9Sstevel@tonic-gate 	    }
46687c478bd9Sstevel@tonic-gate 
46697c478bd9Sstevel@tonic-gate 	case I_EGETSIG:
46707c478bd9Sstevel@tonic-gate 		/*
46717c478bd9Sstevel@tonic-gate 		 * Return (in arg) the current registration of events
46727c478bd9Sstevel@tonic-gate 		 * for which the calling proc is to be signaled.
46737c478bd9Sstevel@tonic-gate 		 */
46747c478bd9Sstevel@tonic-gate 	    {
46757c478bd9Sstevel@tonic-gate 		struct strsig *ssp;
46767c478bd9Sstevel@tonic-gate 		struct proc *proc;
46777c478bd9Sstevel@tonic-gate 		pid_t pid;
46787c478bd9Sstevel@tonic-gate 		struct pid  *pidp;
46797c478bd9Sstevel@tonic-gate 		struct strsigset ss;
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
46827c478bd9Sstevel@tonic-gate 		if (error)
46837c478bd9Sstevel@tonic-gate 			return (error);
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate 		pid = ss.ss_pid;
46867c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
46877c478bd9Sstevel@tonic-gate 		if (pid == 0)
46887c478bd9Sstevel@tonic-gate 			proc = curproc;
46897c478bd9Sstevel@tonic-gate 		else if (pid < 0)
46907c478bd9Sstevel@tonic-gate 			proc = pgfind(-pid);
46917c478bd9Sstevel@tonic-gate 		else
46927c478bd9Sstevel@tonic-gate 			proc = prfind(pid);
46937c478bd9Sstevel@tonic-gate 		if (proc == NULL) {
46947c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
46957c478bd9Sstevel@tonic-gate 			return (ESRCH);
46967c478bd9Sstevel@tonic-gate 		}
46977c478bd9Sstevel@tonic-gate 		if (pid < 0)
46987c478bd9Sstevel@tonic-gate 			pidp = proc->p_pgidp;
46997c478bd9Sstevel@tonic-gate 		else
47007c478bd9Sstevel@tonic-gate 			pidp = proc->p_pidp;
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate 		/* Prevent the pidp from being reassigned */
47037c478bd9Sstevel@tonic-gate 		PID_HOLD(pidp);
47047c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
47057c478bd9Sstevel@tonic-gate 
47067c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
47077c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
47087c478bd9Sstevel@tonic-gate 			if (ssp->ss_pid == pid) {
47097c478bd9Sstevel@tonic-gate 				ss.ss_pid = ssp->ss_pid;
47107c478bd9Sstevel@tonic-gate 				ss.ss_events = ssp->ss_events;
47117c478bd9Sstevel@tonic-gate 				error = strcopyout(&ss, (void *)arg,
47127c478bd9Sstevel@tonic-gate 				    sizeof (struct strsigset), copyflag);
47137c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
47147c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
47157c478bd9Sstevel@tonic-gate 				PID_RELE(pidp);
47167c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
47177c478bd9Sstevel@tonic-gate 				return (error);
47187c478bd9Sstevel@tonic-gate 			}
47197c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
47207c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
47217c478bd9Sstevel@tonic-gate 		PID_RELE(pidp);
47227c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
47237c478bd9Sstevel@tonic-gate 		return (EINVAL);
47247c478bd9Sstevel@tonic-gate 	    }
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate 	case I_PEEK:
47277c478bd9Sstevel@tonic-gate 	    {
47287c478bd9Sstevel@tonic-gate 		STRUCT_DECL(strpeek, strpeek);
47297c478bd9Sstevel@tonic-gate 		size_t n;
47307c478bd9Sstevel@tonic-gate 		mblk_t *fmp, *tmp_mp = NULL;
47317c478bd9Sstevel@tonic-gate 
47327c478bd9Sstevel@tonic-gate 		STRUCT_INIT(strpeek, flag);
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, STRUCT_BUF(strpeek),
47357c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strpeek), copyflag);
47367c478bd9Sstevel@tonic-gate 		if (error)
47377c478bd9Sstevel@tonic-gate 			return (error);
47387c478bd9Sstevel@tonic-gate 
47397c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rdq));
47407c478bd9Sstevel@tonic-gate 		/*
47417c478bd9Sstevel@tonic-gate 		 * Skip the invalid messages
47427c478bd9Sstevel@tonic-gate 		 */
47437c478bd9Sstevel@tonic-gate 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
47447c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type != M_SIG)
47457c478bd9Sstevel@tonic-gate 				break;
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate 		/*
47487c478bd9Sstevel@tonic-gate 		 * If user has requested to peek at a high priority message
47497c478bd9Sstevel@tonic-gate 		 * and first message is not, return 0
47507c478bd9Sstevel@tonic-gate 		 */
47517c478bd9Sstevel@tonic-gate 		if (mp != NULL) {
47527c478bd9Sstevel@tonic-gate 			if ((STRUCT_FGET(strpeek, flags) & RS_HIPRI) &&
47537c478bd9Sstevel@tonic-gate 			    queclass(mp) == QNORM) {
47547c478bd9Sstevel@tonic-gate 				*rvalp = 0;
47557c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(rdq));
47567c478bd9Sstevel@tonic-gate 				return (0);
47577c478bd9Sstevel@tonic-gate 			}
47587c478bd9Sstevel@tonic-gate 		} else if (stp->sd_struiordq == NULL ||
47597c478bd9Sstevel@tonic-gate 		    (STRUCT_FGET(strpeek, flags) & RS_HIPRI)) {
47607c478bd9Sstevel@tonic-gate 			/*
47617c478bd9Sstevel@tonic-gate 			 * No mblks to look at at the streamhead and
47627c478bd9Sstevel@tonic-gate 			 * 1). This isn't a synch stream or
47637c478bd9Sstevel@tonic-gate 			 * 2). This is a synch stream but caller wants high
47647c478bd9Sstevel@tonic-gate 			 *	priority messages which is not supported by
47657c478bd9Sstevel@tonic-gate 			 *	the synch stream. (it only supports QNORM)
47667c478bd9Sstevel@tonic-gate 			 */
47677c478bd9Sstevel@tonic-gate 			*rvalp = 0;
47687c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(rdq));
47697c478bd9Sstevel@tonic-gate 			return (0);
47707c478bd9Sstevel@tonic-gate 		}
47717c478bd9Sstevel@tonic-gate 
47727c478bd9Sstevel@tonic-gate 		fmp = mp;
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate 		if (mp && mp->b_datap->db_type == M_PASSFP) {
47757c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(rdq));
47767c478bd9Sstevel@tonic-gate 			return (EBADMSG);
47777c478bd9Sstevel@tonic-gate 		}
47787c478bd9Sstevel@tonic-gate 
47797c478bd9Sstevel@tonic-gate 		ASSERT(mp == NULL || mp->b_datap->db_type == M_PCPROTO ||
47807c478bd9Sstevel@tonic-gate 		    mp->b_datap->db_type == M_PROTO ||
47817c478bd9Sstevel@tonic-gate 		    mp->b_datap->db_type == M_DATA);
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate 		if (mp && mp->b_datap->db_type == M_PCPROTO) {
47847c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, flags, RS_HIPRI);
47857c478bd9Sstevel@tonic-gate 		} else {
47867c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, flags, 0);
47877c478bd9Sstevel@tonic-gate 		}
47887c478bd9Sstevel@tonic-gate 
47897c478bd9Sstevel@tonic-gate 
47907c478bd9Sstevel@tonic-gate 		if (mp && ((tmp_mp = dupmsg(mp)) == NULL)) {
47917c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(rdq));
47927c478bd9Sstevel@tonic-gate 			return (ENOSR);
47937c478bd9Sstevel@tonic-gate 		}
47947c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rdq));
47957c478bd9Sstevel@tonic-gate 
47967c478bd9Sstevel@tonic-gate 		/*
47977c478bd9Sstevel@tonic-gate 		 * set mp = tmp_mp, so that I_PEEK processing can continue.
47987c478bd9Sstevel@tonic-gate 		 * tmp_mp is used to free the dup'd message.
47997c478bd9Sstevel@tonic-gate 		 */
48007c478bd9Sstevel@tonic-gate 		mp = tmp_mp;
48017c478bd9Sstevel@tonic-gate 
48027c478bd9Sstevel@tonic-gate 		uio.uio_fmode = 0;
48037c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_CACHED;
48047c478bd9Sstevel@tonic-gate 		uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
48057c478bd9Sstevel@tonic-gate 		    UIO_SYSSPACE;
48067c478bd9Sstevel@tonic-gate 		uio.uio_limit = 0;
48077c478bd9Sstevel@tonic-gate 		/*
48087c478bd9Sstevel@tonic-gate 		 * First process PROTO blocks, if any.
48097c478bd9Sstevel@tonic-gate 		 * If user doesn't want to get ctl info by setting maxlen <= 0,
48107c478bd9Sstevel@tonic-gate 		 * then set len to -1/0 and skip control blocks part.
48117c478bd9Sstevel@tonic-gate 		 */
48127c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strpeek, ctlbuf.maxlen) < 0)
48137c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, ctlbuf.len, -1);
48147c478bd9Sstevel@tonic-gate 		else if (STRUCT_FGET(strpeek, ctlbuf.maxlen) == 0)
48157c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, ctlbuf.len, 0);
48167c478bd9Sstevel@tonic-gate 		else {
48177c478bd9Sstevel@tonic-gate 			int	ctl_part = 0;
48187c478bd9Sstevel@tonic-gate 
48197c478bd9Sstevel@tonic-gate 			iov.iov_base = STRUCT_FGETP(strpeek, ctlbuf.buf);
48207c478bd9Sstevel@tonic-gate 			iov.iov_len = STRUCT_FGET(strpeek, ctlbuf.maxlen);
48217c478bd9Sstevel@tonic-gate 			uio.uio_iov = &iov;
48227c478bd9Sstevel@tonic-gate 			uio.uio_resid = iov.iov_len;
48237c478bd9Sstevel@tonic-gate 			uio.uio_loffset = 0;
48247c478bd9Sstevel@tonic-gate 			uio.uio_iovcnt = 1;
48257c478bd9Sstevel@tonic-gate 			while (mp && mp->b_datap->db_type != M_DATA &&
48267c478bd9Sstevel@tonic-gate 			    uio.uio_resid >= 0) {
48277c478bd9Sstevel@tonic-gate 				ASSERT(STRUCT_FGET(strpeek, flags) == 0 ?
48287c478bd9Sstevel@tonic-gate 				    mp->b_datap->db_type == M_PROTO :
48297c478bd9Sstevel@tonic-gate 				    mp->b_datap->db_type == M_PCPROTO);
48307c478bd9Sstevel@tonic-gate 
48317c478bd9Sstevel@tonic-gate 				if ((n = MIN(uio.uio_resid,
48327c478bd9Sstevel@tonic-gate 				    mp->b_wptr - mp->b_rptr)) != 0 &&
48337c478bd9Sstevel@tonic-gate 				    (error = uiomove((char *)mp->b_rptr, n,
48347c478bd9Sstevel@tonic-gate 				    UIO_READ, &uio)) != 0) {
48357c478bd9Sstevel@tonic-gate 					freemsg(tmp_mp);
48367c478bd9Sstevel@tonic-gate 					return (error);
48377c478bd9Sstevel@tonic-gate 				}
48387c478bd9Sstevel@tonic-gate 				ctl_part = 1;
48397c478bd9Sstevel@tonic-gate 				mp = mp->b_cont;
48407c478bd9Sstevel@tonic-gate 			}
48417c478bd9Sstevel@tonic-gate 			/* No ctl message */
48427c478bd9Sstevel@tonic-gate 			if (ctl_part == 0)
48437c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, ctlbuf.len, -1);
48447c478bd9Sstevel@tonic-gate 			else
48457c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, ctlbuf.len,
48467c478bd9Sstevel@tonic-gate 				    STRUCT_FGET(strpeek, ctlbuf.maxlen) -
48477c478bd9Sstevel@tonic-gate 				    uio.uio_resid);
48487c478bd9Sstevel@tonic-gate 		}
48497c478bd9Sstevel@tonic-gate 
48507c478bd9Sstevel@tonic-gate 		/*
48517c478bd9Sstevel@tonic-gate 		 * Now process DATA blocks, if any.
48527c478bd9Sstevel@tonic-gate 		 * If user doesn't want to get data info by setting maxlen <= 0,
48537c478bd9Sstevel@tonic-gate 		 * then set len to -1/0 and skip data blocks part.
48547c478bd9Sstevel@tonic-gate 		 */
48557c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strpeek, databuf.maxlen) < 0)
48567c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, databuf.len, -1);
48577c478bd9Sstevel@tonic-gate 		else if (STRUCT_FGET(strpeek, databuf.maxlen) == 0)
48587c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, databuf.len, 0);
48597c478bd9Sstevel@tonic-gate 		else {
48607c478bd9Sstevel@tonic-gate 			int	data_part = 0;
48617c478bd9Sstevel@tonic-gate 
48627c478bd9Sstevel@tonic-gate 			iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
48637c478bd9Sstevel@tonic-gate 			iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen);
48647c478bd9Sstevel@tonic-gate 			uio.uio_iov = &iov;
48657c478bd9Sstevel@tonic-gate 			uio.uio_resid = iov.iov_len;
48667c478bd9Sstevel@tonic-gate 			uio.uio_loffset = 0;
48677c478bd9Sstevel@tonic-gate 			uio.uio_iovcnt = 1;
48687c478bd9Sstevel@tonic-gate 			while (mp && uio.uio_resid) {
48697c478bd9Sstevel@tonic-gate 				if (mp->b_datap->db_type == M_DATA) {
48707c478bd9Sstevel@tonic-gate 					if ((n = MIN(uio.uio_resid,
48717c478bd9Sstevel@tonic-gate 					    mp->b_wptr - mp->b_rptr)) != 0 &&
48727c478bd9Sstevel@tonic-gate 					    (error = uiomove((char *)mp->b_rptr,
48737c478bd9Sstevel@tonic-gate 						n, UIO_READ, &uio)) != 0) {
48747c478bd9Sstevel@tonic-gate 						freemsg(tmp_mp);
48757c478bd9Sstevel@tonic-gate 						return (error);
48767c478bd9Sstevel@tonic-gate 					}
48777c478bd9Sstevel@tonic-gate 					data_part = 1;
48787c478bd9Sstevel@tonic-gate 				}
48797c478bd9Sstevel@tonic-gate 				ASSERT(data_part == 0 ||
48807c478bd9Sstevel@tonic-gate 				    mp->b_datap->db_type == M_DATA);
48817c478bd9Sstevel@tonic-gate 				mp = mp->b_cont;
48827c478bd9Sstevel@tonic-gate 			}
48837c478bd9Sstevel@tonic-gate 			/* No data message */
48847c478bd9Sstevel@tonic-gate 			if (data_part == 0)
48857c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, databuf.len, -1);
48867c478bd9Sstevel@tonic-gate 			else
48877c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, databuf.len,
48887c478bd9Sstevel@tonic-gate 				    STRUCT_FGET(strpeek, databuf.maxlen) -
48897c478bd9Sstevel@tonic-gate 				    uio.uio_resid);
48907c478bd9Sstevel@tonic-gate 		}
48917c478bd9Sstevel@tonic-gate 		freemsg(tmp_mp);
48927c478bd9Sstevel@tonic-gate 
48937c478bd9Sstevel@tonic-gate 		/*
48947c478bd9Sstevel@tonic-gate 		 * It is a synch stream and user wants to get
48957c478bd9Sstevel@tonic-gate 		 * data (maxlen > 0).
48967c478bd9Sstevel@tonic-gate 		 * uio setup is done by the codes that process DATA
48977c478bd9Sstevel@tonic-gate 		 * blocks above.
48987c478bd9Sstevel@tonic-gate 		 */
48997c478bd9Sstevel@tonic-gate 		if ((fmp == NULL) && STRUCT_FGET(strpeek, databuf.maxlen) > 0) {
49007c478bd9Sstevel@tonic-gate 			infod_t infod;
49017c478bd9Sstevel@tonic-gate 
49027c478bd9Sstevel@tonic-gate 			infod.d_cmd = INFOD_COPYOUT;
49037c478bd9Sstevel@tonic-gate 			infod.d_res = 0;
49047c478bd9Sstevel@tonic-gate 			infod.d_uiop = &uio;
49057c478bd9Sstevel@tonic-gate 			error = infonext(rdq, &infod);
49067c478bd9Sstevel@tonic-gate 			if (error == EINVAL || error == EBUSY)
49077c478bd9Sstevel@tonic-gate 				error = 0;
49087c478bd9Sstevel@tonic-gate 			if (error)
49097c478bd9Sstevel@tonic-gate 				return (error);
49107c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strpeek, databuf.len, STRUCT_FGET(strpeek,
49117c478bd9Sstevel@tonic-gate 			    databuf.maxlen) - uio.uio_resid);
49127c478bd9Sstevel@tonic-gate 			if (STRUCT_FGET(strpeek, databuf.len) == 0) {
49137c478bd9Sstevel@tonic-gate 				/*
49147c478bd9Sstevel@tonic-gate 				 * No data found by the infonext().
49157c478bd9Sstevel@tonic-gate 				 */
49167c478bd9Sstevel@tonic-gate 				STRUCT_FSET(strpeek, databuf.len, -1);
49177c478bd9Sstevel@tonic-gate 			}
49187c478bd9Sstevel@tonic-gate 		}
49197c478bd9Sstevel@tonic-gate 		error = strcopyout(STRUCT_BUF(strpeek), (void *)arg,
49207c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strpeek), copyflag);
49217c478bd9Sstevel@tonic-gate 		if (error) {
49227c478bd9Sstevel@tonic-gate 			return (error);
49237c478bd9Sstevel@tonic-gate 		}
49247c478bd9Sstevel@tonic-gate 		/*
49257c478bd9Sstevel@tonic-gate 		 * If there is no message retrieved, set return code to 0
49267c478bd9Sstevel@tonic-gate 		 * otherwise, set it to 1.
49277c478bd9Sstevel@tonic-gate 		 */
49287c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strpeek, ctlbuf.len) == -1 &&
49297c478bd9Sstevel@tonic-gate 		    STRUCT_FGET(strpeek, databuf.len) == -1)
49307c478bd9Sstevel@tonic-gate 			*rvalp = 0;
49317c478bd9Sstevel@tonic-gate 		else
49327c478bd9Sstevel@tonic-gate 			*rvalp = 1;
49337c478bd9Sstevel@tonic-gate 		return (0);
49347c478bd9Sstevel@tonic-gate 	    }
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate 	case I_FDINSERT:
49377c478bd9Sstevel@tonic-gate 	    {
49387c478bd9Sstevel@tonic-gate 		STRUCT_DECL(strfdinsert, strfdinsert);
49397c478bd9Sstevel@tonic-gate 		struct file *resftp;
49407c478bd9Sstevel@tonic-gate 		struct stdata *resstp;
49417c478bd9Sstevel@tonic-gate 		t_uscalar_t	ival;
49427c478bd9Sstevel@tonic-gate 		ssize_t msgsize;
49437c478bd9Sstevel@tonic-gate 		struct strbuf mctl;
49447c478bd9Sstevel@tonic-gate 
49457c478bd9Sstevel@tonic-gate 		STRUCT_INIT(strfdinsert, flag);
49467c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRHUP)
49477c478bd9Sstevel@tonic-gate 			return (ENXIO);
49487c478bd9Sstevel@tonic-gate 		/*
49497c478bd9Sstevel@tonic-gate 		 * STRDERR, STWRERR and STPLEX tested above.
49507c478bd9Sstevel@tonic-gate 		 */
49517c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, STRUCT_BUF(strfdinsert),
49527c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(strfdinsert), copyflag);
49537c478bd9Sstevel@tonic-gate 		if (error)
49547c478bd9Sstevel@tonic-gate 			return (error);
49557c478bd9Sstevel@tonic-gate 
49567c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strfdinsert, offset) < 0 ||
49577c478bd9Sstevel@tonic-gate 		    (STRUCT_FGET(strfdinsert, offset) %
49587c478bd9Sstevel@tonic-gate 		    sizeof (t_uscalar_t)) != 0)
49597c478bd9Sstevel@tonic-gate 			return (EINVAL);
49607c478bd9Sstevel@tonic-gate 		if ((resftp = getf(STRUCT_FGET(strfdinsert, fildes))) != NULL) {
49617c478bd9Sstevel@tonic-gate 			if ((resstp = resftp->f_vnode->v_stream) == NULL) {
49627c478bd9Sstevel@tonic-gate 				releasef(STRUCT_FGET(strfdinsert, fildes));
49637c478bd9Sstevel@tonic-gate 				return (EINVAL);
49647c478bd9Sstevel@tonic-gate 			}
49657c478bd9Sstevel@tonic-gate 		} else
49667c478bd9Sstevel@tonic-gate 			return (EINVAL);
49677c478bd9Sstevel@tonic-gate 
49687c478bd9Sstevel@tonic-gate 		mutex_enter(&resstp->sd_lock);
49697c478bd9Sstevel@tonic-gate 		if (resstp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
49707c478bd9Sstevel@tonic-gate 			error = strgeterr(resstp,
49717c478bd9Sstevel@tonic-gate 					STRDERR|STWRERR|STRHUP|STPLEX, 0);
49727c478bd9Sstevel@tonic-gate 			if (error != 0) {
49737c478bd9Sstevel@tonic-gate 				mutex_exit(&resstp->sd_lock);
49747c478bd9Sstevel@tonic-gate 				releasef(STRUCT_FGET(strfdinsert, fildes));
49757c478bd9Sstevel@tonic-gate 				return (error);
49767c478bd9Sstevel@tonic-gate 			}
49777c478bd9Sstevel@tonic-gate 		}
49787c478bd9Sstevel@tonic-gate 		mutex_exit(&resstp->sd_lock);
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate #ifdef	_ILP32
49817c478bd9Sstevel@tonic-gate 		{
49827c478bd9Sstevel@tonic-gate 			queue_t	*q;
49837c478bd9Sstevel@tonic-gate 			queue_t	*mate = NULL;
49847c478bd9Sstevel@tonic-gate 
49857c478bd9Sstevel@tonic-gate 			/* get read queue of stream terminus */
49867c478bd9Sstevel@tonic-gate 			claimstr(resstp->sd_wrq);
49877c478bd9Sstevel@tonic-gate 			for (q = resstp->sd_wrq->q_next; q->q_next != NULL;
49887c478bd9Sstevel@tonic-gate 			    q = q->q_next)
49897c478bd9Sstevel@tonic-gate 				if (!STRMATED(resstp) && STREAM(q) != resstp &&
49907c478bd9Sstevel@tonic-gate 				    mate == NULL) {
49917c478bd9Sstevel@tonic-gate 					ASSERT(q->q_qinfo->qi_srvp);
49927c478bd9Sstevel@tonic-gate 					ASSERT(_OTHERQ(q)->q_qinfo->qi_srvp);
49937c478bd9Sstevel@tonic-gate 					claimstr(q);
49947c478bd9Sstevel@tonic-gate 					mate = q;
49957c478bd9Sstevel@tonic-gate 				}
49967c478bd9Sstevel@tonic-gate 			q = _RD(q);
49977c478bd9Sstevel@tonic-gate 			if (mate)
49987c478bd9Sstevel@tonic-gate 				releasestr(mate);
49997c478bd9Sstevel@tonic-gate 			releasestr(resstp->sd_wrq);
50007c478bd9Sstevel@tonic-gate 			ival = (t_uscalar_t)q;
50017c478bd9Sstevel@tonic-gate 		}
50027c478bd9Sstevel@tonic-gate #else
50037c478bd9Sstevel@tonic-gate 		ival = (t_uscalar_t)getminor(resftp->f_vnode->v_rdev);
50047c478bd9Sstevel@tonic-gate #endif	/* _ILP32 */
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strfdinsert, ctlbuf.len) <
50077c478bd9Sstevel@tonic-gate 		    STRUCT_FGET(strfdinsert, offset) + sizeof (t_uscalar_t)) {
50087c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
50097c478bd9Sstevel@tonic-gate 			return (EINVAL);
50107c478bd9Sstevel@tonic-gate 		}
50117c478bd9Sstevel@tonic-gate 
50127c478bd9Sstevel@tonic-gate 		/*
50137c478bd9Sstevel@tonic-gate 		 * Check for legal flag value.
50147c478bd9Sstevel@tonic-gate 		 */
50157c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(strfdinsert, flags) & ~RS_HIPRI) {
50167c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
50177c478bd9Sstevel@tonic-gate 			return (EINVAL);
50187c478bd9Sstevel@tonic-gate 		}
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 		/* get these values from those cached in the stream head */
50217c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(stp->sd_wrq));
50227c478bd9Sstevel@tonic-gate 		rmin = stp->sd_qn_minpsz;
50237c478bd9Sstevel@tonic-gate 		rmax = stp->sd_qn_maxpsz;
50247c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(stp->sd_wrq));
50257c478bd9Sstevel@tonic-gate 
50267c478bd9Sstevel@tonic-gate 		/*
50277c478bd9Sstevel@tonic-gate 		 * Make sure ctl and data sizes together fall within
50287c478bd9Sstevel@tonic-gate 		 * the limits of the max and min receive packet sizes
50297c478bd9Sstevel@tonic-gate 		 * and do not exceed system limit.  A negative data
50307c478bd9Sstevel@tonic-gate 		 * length means that no data part is to be sent.
50317c478bd9Sstevel@tonic-gate 		 */
50327c478bd9Sstevel@tonic-gate 		ASSERT((rmax >= 0) || (rmax == INFPSZ));
50337c478bd9Sstevel@tonic-gate 		if (rmax == 0) {
50347c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
50357c478bd9Sstevel@tonic-gate 			return (ERANGE);
50367c478bd9Sstevel@tonic-gate 		}
50377c478bd9Sstevel@tonic-gate 		if ((msgsize = STRUCT_FGET(strfdinsert, databuf.len)) < 0)
50387c478bd9Sstevel@tonic-gate 			msgsize = 0;
50397c478bd9Sstevel@tonic-gate 		if ((msgsize < rmin) ||
50407c478bd9Sstevel@tonic-gate 		    ((msgsize > rmax) && (rmax != INFPSZ)) ||
50417c478bd9Sstevel@tonic-gate 		    (STRUCT_FGET(strfdinsert, ctlbuf.len) > strctlsz)) {
50427c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
50437c478bd9Sstevel@tonic-gate 			return (ERANGE);
50447c478bd9Sstevel@tonic-gate 		}
50457c478bd9Sstevel@tonic-gate 
50467c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
50477c478bd9Sstevel@tonic-gate 		while (!(STRUCT_FGET(strfdinsert, flags) & RS_HIPRI) &&
50487c478bd9Sstevel@tonic-gate 		    !canputnext(stp->sd_wrq)) {
50497c478bd9Sstevel@tonic-gate 			if ((error = strwaitq(stp, WRITEWAIT, (ssize_t)0,
50507c478bd9Sstevel@tonic-gate 			    flag, -1, &done)) != 0 || done) {
50517c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
50527c478bd9Sstevel@tonic-gate 				releasef(STRUCT_FGET(strfdinsert, fildes));
50537c478bd9Sstevel@tonic-gate 				return (error);
50547c478bd9Sstevel@tonic-gate 			}
50557c478bd9Sstevel@tonic-gate 			if (stp->sd_sidp != NULL &&
50567c478bd9Sstevel@tonic-gate 			    stp->sd_vnode->v_type != VFIFO) {
50577c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
50587c478bd9Sstevel@tonic-gate 				if (error = straccess(stp, access)) {
50597c478bd9Sstevel@tonic-gate 					releasef(
50607c478bd9Sstevel@tonic-gate 					    STRUCT_FGET(strfdinsert, fildes));
50617c478bd9Sstevel@tonic-gate 					return (error);
50627c478bd9Sstevel@tonic-gate 				}
50637c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
50647c478bd9Sstevel@tonic-gate 			}
50657c478bd9Sstevel@tonic-gate 		}
50667c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
50677c478bd9Sstevel@tonic-gate 
50687c478bd9Sstevel@tonic-gate 		/*
50697c478bd9Sstevel@tonic-gate 		 * Copy strfdinsert.ctlbuf into native form of
50707c478bd9Sstevel@tonic-gate 		 * ctlbuf to pass down into strmakemsg().
50717c478bd9Sstevel@tonic-gate 		 */
50727c478bd9Sstevel@tonic-gate 		mctl.maxlen = STRUCT_FGET(strfdinsert, ctlbuf.maxlen);
50737c478bd9Sstevel@tonic-gate 		mctl.len = STRUCT_FGET(strfdinsert, ctlbuf.len);
50747c478bd9Sstevel@tonic-gate 		mctl.buf = STRUCT_FGETP(strfdinsert, ctlbuf.buf);
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate 		iov.iov_base = STRUCT_FGETP(strfdinsert, databuf.buf);
50777c478bd9Sstevel@tonic-gate 		iov.iov_len = STRUCT_FGET(strfdinsert, databuf.len);
50787c478bd9Sstevel@tonic-gate 		uio.uio_iov = &iov;
50797c478bd9Sstevel@tonic-gate 		uio.uio_iovcnt = 1;
50807c478bd9Sstevel@tonic-gate 		uio.uio_loffset = 0;
50817c478bd9Sstevel@tonic-gate 		uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
50827c478bd9Sstevel@tonic-gate 		    UIO_SYSSPACE;
50837c478bd9Sstevel@tonic-gate 		uio.uio_fmode = 0;
50847c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_CACHED;
50857c478bd9Sstevel@tonic-gate 		uio.uio_resid = iov.iov_len;
50867c478bd9Sstevel@tonic-gate 		if ((error = strmakemsg(&mctl,
50877c478bd9Sstevel@tonic-gate 		    &msgsize, &uio, stp,
50887c478bd9Sstevel@tonic-gate 		    STRUCT_FGET(strfdinsert, flags), &mp)) != 0 || !mp) {
50897c478bd9Sstevel@tonic-gate 			STRUCT_FSET(strfdinsert, databuf.len, msgsize);
50907c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(strfdinsert, fildes));
50917c478bd9Sstevel@tonic-gate 			return (error);
50927c478bd9Sstevel@tonic-gate 		}
50937c478bd9Sstevel@tonic-gate 
50947c478bd9Sstevel@tonic-gate 		STRUCT_FSET(strfdinsert, databuf.len, msgsize);
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate 		/*
50977c478bd9Sstevel@tonic-gate 		 * Place the possibly reencoded queue pointer 'offset' bytes
50987c478bd9Sstevel@tonic-gate 		 * from the start of the control portion of the message.
50997c478bd9Sstevel@tonic-gate 		 */
51007c478bd9Sstevel@tonic-gate 		*((t_uscalar_t *)(mp->b_rptr +
51017c478bd9Sstevel@tonic-gate 		    STRUCT_FGET(strfdinsert, offset))) = ival;
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate 		/*
51047c478bd9Sstevel@tonic-gate 		 * Put message downstream.
51057c478bd9Sstevel@tonic-gate 		 */
51067c478bd9Sstevel@tonic-gate 		stream_willservice(stp);
51077c478bd9Sstevel@tonic-gate 		putnext(stp->sd_wrq, mp);
51087c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
51097c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(strfdinsert, fildes));
51107c478bd9Sstevel@tonic-gate 		return (error);
51117c478bd9Sstevel@tonic-gate 	    }
51127c478bd9Sstevel@tonic-gate 
51137c478bd9Sstevel@tonic-gate 	case I_SENDFD:
51147c478bd9Sstevel@tonic-gate 	    {
51157c478bd9Sstevel@tonic-gate 		struct file *fp;
51167c478bd9Sstevel@tonic-gate 
51177c478bd9Sstevel@tonic-gate 		if ((fp = getf((int)arg)) == NULL)
51187c478bd9Sstevel@tonic-gate 			return (EBADF);
51197c478bd9Sstevel@tonic-gate 		error = do_sendfp(stp, fp, crp);
51207c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
51217c478bd9Sstevel@tonic-gate 		if (audit_active) {
51227c478bd9Sstevel@tonic-gate 			audit_fdsend((int)arg, fp, error);
51237c478bd9Sstevel@tonic-gate 		}
51247c478bd9Sstevel@tonic-gate #endif
51257c478bd9Sstevel@tonic-gate 		releasef((int)arg);
51267c478bd9Sstevel@tonic-gate 		return (error);
51277c478bd9Sstevel@tonic-gate 	    }
51287c478bd9Sstevel@tonic-gate 
51297c478bd9Sstevel@tonic-gate 	case I_RECVFD:
51307c478bd9Sstevel@tonic-gate 	case I_E_RECVFD:
51317c478bd9Sstevel@tonic-gate 	    {
51327c478bd9Sstevel@tonic-gate 		struct k_strrecvfd *srf;
51337c478bd9Sstevel@tonic-gate 		int i, fd;
51347c478bd9Sstevel@tonic-gate 
51357c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
51367c478bd9Sstevel@tonic-gate 		while (!(mp = getq(rdq))) {
51377c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STRHUP|STREOF)) {
51387c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
51397c478bd9Sstevel@tonic-gate 				return (ENXIO);
51407c478bd9Sstevel@tonic-gate 			}
51417c478bd9Sstevel@tonic-gate 			if ((error = strwaitq(stp, GETWAIT, (ssize_t)0,
51427c478bd9Sstevel@tonic-gate 			    flag, -1, &done)) != 0 || done) {
51437c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
51447c478bd9Sstevel@tonic-gate 				return (error);
51457c478bd9Sstevel@tonic-gate 			}
51467c478bd9Sstevel@tonic-gate 			if (stp->sd_sidp != NULL &&
51477c478bd9Sstevel@tonic-gate 			    stp->sd_vnode->v_type != VFIFO) {
51487c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
51497c478bd9Sstevel@tonic-gate 				if (error = straccess(stp, access))
51507c478bd9Sstevel@tonic-gate 					return (error);
51517c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
51527c478bd9Sstevel@tonic-gate 			}
51537c478bd9Sstevel@tonic-gate 		}
51547c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type != M_PASSFP) {
51557c478bd9Sstevel@tonic-gate 			putback(stp, rdq, mp, mp->b_band);
51567c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
51577c478bd9Sstevel@tonic-gate 			return (EBADMSG);
51587c478bd9Sstevel@tonic-gate 		}
51597c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
51607c478bd9Sstevel@tonic-gate 
51617c478bd9Sstevel@tonic-gate 		srf = (struct k_strrecvfd *)mp->b_rptr;
51627c478bd9Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1) {
51637c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
51647c478bd9Sstevel@tonic-gate 			putback(stp, rdq, mp, mp->b_band);
51657c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
51667c478bd9Sstevel@tonic-gate 			return (EMFILE);
51677c478bd9Sstevel@tonic-gate 		}
51687c478bd9Sstevel@tonic-gate 		if (cmd == I_RECVFD) {
51697c478bd9Sstevel@tonic-gate 			struct o_strrecvfd	ostrfd;
51707c478bd9Sstevel@tonic-gate 
51717c478bd9Sstevel@tonic-gate 			/* check to see if uid/gid values are too large. */
51727c478bd9Sstevel@tonic-gate 
51737c478bd9Sstevel@tonic-gate 			if (srf->uid > (o_uid_t)USHRT_MAX ||
51747c478bd9Sstevel@tonic-gate 			    srf->gid > (o_gid_t)USHRT_MAX) {
51757c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
51767c478bd9Sstevel@tonic-gate 				putback(stp, rdq, mp, mp->b_band);
51777c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
51787c478bd9Sstevel@tonic-gate 				setf(fd, NULL);	/* release fd entry */
51797c478bd9Sstevel@tonic-gate 				return (EOVERFLOW);
51807c478bd9Sstevel@tonic-gate 			}
51817c478bd9Sstevel@tonic-gate 
51827c478bd9Sstevel@tonic-gate 			ostrfd.fd = fd;
51837c478bd9Sstevel@tonic-gate 			ostrfd.uid = (o_uid_t)srf->uid;
51847c478bd9Sstevel@tonic-gate 			ostrfd.gid = (o_gid_t)srf->gid;
51857c478bd9Sstevel@tonic-gate 
51867c478bd9Sstevel@tonic-gate 			/* Null the filler bits */
51877c478bd9Sstevel@tonic-gate 			for (i = 0; i < 8; i++)
51887c478bd9Sstevel@tonic-gate 				ostrfd.fill[i] = 0;
51897c478bd9Sstevel@tonic-gate 
51907c478bd9Sstevel@tonic-gate 			error = strcopyout(&ostrfd, (void *)arg,
51917c478bd9Sstevel@tonic-gate 			    sizeof (struct o_strrecvfd), copyflag);
51927c478bd9Sstevel@tonic-gate 		} else {		/* I_E_RECVFD */
51937c478bd9Sstevel@tonic-gate 			struct strrecvfd	strfd;
51947c478bd9Sstevel@tonic-gate 
51957c478bd9Sstevel@tonic-gate 			strfd.fd = fd;
51967c478bd9Sstevel@tonic-gate 			strfd.uid = srf->uid;
51977c478bd9Sstevel@tonic-gate 			strfd.gid = srf->gid;
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate 			/* null the filler bits */
52007c478bd9Sstevel@tonic-gate 			for (i = 0; i < 8; i++)
52017c478bd9Sstevel@tonic-gate 				strfd.fill[i] = 0;
52027c478bd9Sstevel@tonic-gate 
52037c478bd9Sstevel@tonic-gate 			error = strcopyout(&strfd, (void *)arg,
52047c478bd9Sstevel@tonic-gate 			    sizeof (struct strrecvfd), copyflag);
52057c478bd9Sstevel@tonic-gate 		}
52067c478bd9Sstevel@tonic-gate 
52077c478bd9Sstevel@tonic-gate 		if (error) {
52087c478bd9Sstevel@tonic-gate 			setf(fd, NULL);	/* release fd entry */
52097c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
52107c478bd9Sstevel@tonic-gate 			putback(stp, rdq, mp, mp->b_band);
52117c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
52127c478bd9Sstevel@tonic-gate 			return (error);
52137c478bd9Sstevel@tonic-gate 		}
52147c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
52157c478bd9Sstevel@tonic-gate 		if (audit_active) {
52167c478bd9Sstevel@tonic-gate 			audit_fdrecv(fd, srf->fp);
52177c478bd9Sstevel@tonic-gate 		}
52187c478bd9Sstevel@tonic-gate #endif
52197c478bd9Sstevel@tonic-gate 
52207c478bd9Sstevel@tonic-gate 		/*
52217c478bd9Sstevel@tonic-gate 		 * Always increment f_count since the freemsg() below will
52227c478bd9Sstevel@tonic-gate 		 * always call free_passfp() which performs a closef().
52237c478bd9Sstevel@tonic-gate 		 */
52247c478bd9Sstevel@tonic-gate 		mutex_enter(&srf->fp->f_tlock);
52257c478bd9Sstevel@tonic-gate 		srf->fp->f_count++;
52267c478bd9Sstevel@tonic-gate 		mutex_exit(&srf->fp->f_tlock);
52277c478bd9Sstevel@tonic-gate 		setf(fd, srf->fp);
52287c478bd9Sstevel@tonic-gate 		freemsg(mp);
52297c478bd9Sstevel@tonic-gate 		return (0);
52307c478bd9Sstevel@tonic-gate 	    }
52317c478bd9Sstevel@tonic-gate 
52327c478bd9Sstevel@tonic-gate 	case I_SWROPT:
52337c478bd9Sstevel@tonic-gate 		/*
52347c478bd9Sstevel@tonic-gate 		 * Set/clear the write options. arg is a bit
52357c478bd9Sstevel@tonic-gate 		 * mask with any of the following bits set...
52367c478bd9Sstevel@tonic-gate 		 * 	SNDZERO - send zero length message
52377c478bd9Sstevel@tonic-gate 		 *	SNDPIPE - send sigpipe to process if
52387c478bd9Sstevel@tonic-gate 		 *		sd_werror is set and process is
52397c478bd9Sstevel@tonic-gate 		 *		doing a write or putmsg.
52407c478bd9Sstevel@tonic-gate 		 * The new stream head write options should reflect
52417c478bd9Sstevel@tonic-gate 		 * what is in arg.
52427c478bd9Sstevel@tonic-gate 		 */
52437c478bd9Sstevel@tonic-gate 		if (arg & ~(SNDZERO|SNDPIPE))
52447c478bd9Sstevel@tonic-gate 			return (EINVAL);
52457c478bd9Sstevel@tonic-gate 
52467c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
52477c478bd9Sstevel@tonic-gate 		stp->sd_wput_opt &= ~(SW_SIGPIPE|SW_SNDZERO);
52487c478bd9Sstevel@tonic-gate 		if (arg & SNDZERO)
52497c478bd9Sstevel@tonic-gate 			stp->sd_wput_opt |= SW_SNDZERO;
52507c478bd9Sstevel@tonic-gate 		if (arg & SNDPIPE)
52517c478bd9Sstevel@tonic-gate 			stp->sd_wput_opt |= SW_SIGPIPE;
52527c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
52537c478bd9Sstevel@tonic-gate 		return (0);
52547c478bd9Sstevel@tonic-gate 
52557c478bd9Sstevel@tonic-gate 	case I_GWROPT:
52567c478bd9Sstevel@tonic-gate 	    {
52577c478bd9Sstevel@tonic-gate 		int wropt = 0;
52587c478bd9Sstevel@tonic-gate 
52597c478bd9Sstevel@tonic-gate 		if (stp->sd_wput_opt & SW_SNDZERO)
52607c478bd9Sstevel@tonic-gate 			wropt |= SNDZERO;
52617c478bd9Sstevel@tonic-gate 		if (stp->sd_wput_opt & SW_SIGPIPE)
52627c478bd9Sstevel@tonic-gate 			wropt |= SNDPIPE;
52637c478bd9Sstevel@tonic-gate 		return (strcopyout(&wropt, (void *)arg, sizeof (wropt),
52647c478bd9Sstevel@tonic-gate 		    copyflag));
52657c478bd9Sstevel@tonic-gate 	    }
52667c478bd9Sstevel@tonic-gate 
52677c478bd9Sstevel@tonic-gate 	case I_LIST:
52687c478bd9Sstevel@tonic-gate 		/*
52697c478bd9Sstevel@tonic-gate 		 * Returns all the modules found on this stream,
52707c478bd9Sstevel@tonic-gate 		 * upto the driver. If argument is NULL, return the
52717c478bd9Sstevel@tonic-gate 		 * number of modules (including driver). If argument
52727c478bd9Sstevel@tonic-gate 		 * is not NULL, copy the names into the structure
52737c478bd9Sstevel@tonic-gate 		 * provided.
52747c478bd9Sstevel@tonic-gate 		 */
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 	    {
52777c478bd9Sstevel@tonic-gate 		queue_t *q;
52787c478bd9Sstevel@tonic-gate 		int num_modules, space_allocated;
52797c478bd9Sstevel@tonic-gate 		STRUCT_DECL(str_list, strlist);
52807c478bd9Sstevel@tonic-gate 		struct str_mlist *mlist_ptr;
52817c478bd9Sstevel@tonic-gate 
52827c478bd9Sstevel@tonic-gate 		if (arg == NULL) { /* Return number of modules plus driver */
52837c478bd9Sstevel@tonic-gate 			q = stp->sd_wrq;
52847c478bd9Sstevel@tonic-gate 			if (stp->sd_vnode->v_type == VFIFO) {
52857c478bd9Sstevel@tonic-gate 				*rvalp = stp->sd_pushcnt;
52867c478bd9Sstevel@tonic-gate 			} else {
52877c478bd9Sstevel@tonic-gate 				*rvalp = stp->sd_pushcnt + 1;
52887c478bd9Sstevel@tonic-gate 			}
52897c478bd9Sstevel@tonic-gate 		} else {
52907c478bd9Sstevel@tonic-gate 			STRUCT_INIT(strlist, flag);
52917c478bd9Sstevel@tonic-gate 
52927c478bd9Sstevel@tonic-gate 			error = strcopyin((void *)arg, STRUCT_BUF(strlist),
52937c478bd9Sstevel@tonic-gate 			    STRUCT_SIZE(strlist), copyflag);
52947c478bd9Sstevel@tonic-gate 			if (error)
52957c478bd9Sstevel@tonic-gate 				return (error);
52967c478bd9Sstevel@tonic-gate 
52977c478bd9Sstevel@tonic-gate 			space_allocated = STRUCT_FGET(strlist, sl_nmods);
52987c478bd9Sstevel@tonic-gate 			if ((space_allocated) <= 0)
52997c478bd9Sstevel@tonic-gate 				return (EINVAL);
53007c478bd9Sstevel@tonic-gate 			claimstr(stp->sd_wrq);
53017c478bd9Sstevel@tonic-gate 			q = stp->sd_wrq;
53027c478bd9Sstevel@tonic-gate 			num_modules = 0;
53037c478bd9Sstevel@tonic-gate 			while (_SAMESTR(q) && (space_allocated != 0)) {
53047c478bd9Sstevel@tonic-gate 				char *name =
53057c478bd9Sstevel@tonic-gate 				    q->q_next->q_qinfo->qi_minfo->mi_idname;
53067c478bd9Sstevel@tonic-gate 
53077c478bd9Sstevel@tonic-gate 				mlist_ptr = STRUCT_FGETP(strlist, sl_modlist);
53087c478bd9Sstevel@tonic-gate 
53097c478bd9Sstevel@tonic-gate 				error = strcopyout(name, mlist_ptr,
53107c478bd9Sstevel@tonic-gate 				    strlen(name) + 1, copyflag);
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate 				if (error) {
53137c478bd9Sstevel@tonic-gate 					releasestr(stp->sd_wrq);
53147c478bd9Sstevel@tonic-gate 					return (error);
53157c478bd9Sstevel@tonic-gate 				}
53167c478bd9Sstevel@tonic-gate 				q = q->q_next;
53177c478bd9Sstevel@tonic-gate 				space_allocated--;
53187c478bd9Sstevel@tonic-gate 				num_modules++;
53197c478bd9Sstevel@tonic-gate 				mlist_ptr =
53207c478bd9Sstevel@tonic-gate 				    (struct str_mlist *)((uintptr_t)mlist_ptr +
53217c478bd9Sstevel@tonic-gate 				    sizeof (struct str_mlist));
53227c478bd9Sstevel@tonic-gate 				STRUCT_FSETP(strlist, sl_modlist, mlist_ptr);
53237c478bd9Sstevel@tonic-gate 			}
53247c478bd9Sstevel@tonic-gate 			releasestr(stp->sd_wrq);
53257c478bd9Sstevel@tonic-gate 			error = strcopyout(&num_modules, (void *)arg,
53267c478bd9Sstevel@tonic-gate 			    sizeof (int), copyflag);
53277c478bd9Sstevel@tonic-gate 		}
53287c478bd9Sstevel@tonic-gate 		return (error);
53297c478bd9Sstevel@tonic-gate 	    }
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 	case I_CKBAND:
53327c478bd9Sstevel@tonic-gate 	    {
53337c478bd9Sstevel@tonic-gate 		queue_t *q;
53347c478bd9Sstevel@tonic-gate 		qband_t *qbp;
53357c478bd9Sstevel@tonic-gate 
53367c478bd9Sstevel@tonic-gate 		if ((arg < 0) || (arg >= NBAND))
53377c478bd9Sstevel@tonic-gate 			return (EINVAL);
53387c478bd9Sstevel@tonic-gate 		q = _RD(stp->sd_wrq);
53397c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(q));
53407c478bd9Sstevel@tonic-gate 		if (arg > (int)q->q_nband) {
53417c478bd9Sstevel@tonic-gate 			*rvalp = 0;
53427c478bd9Sstevel@tonic-gate 		} else {
53437c478bd9Sstevel@tonic-gate 			if (arg == 0) {
53447c478bd9Sstevel@tonic-gate 				if (q->q_first)
53457c478bd9Sstevel@tonic-gate 					*rvalp = 1;
53467c478bd9Sstevel@tonic-gate 				else
53477c478bd9Sstevel@tonic-gate 					*rvalp = 0;
53487c478bd9Sstevel@tonic-gate 			} else {
53497c478bd9Sstevel@tonic-gate 				qbp = q->q_bandp;
53507c478bd9Sstevel@tonic-gate 				while (--arg > 0)
53517c478bd9Sstevel@tonic-gate 					qbp = qbp->qb_next;
53527c478bd9Sstevel@tonic-gate 				if (qbp->qb_first)
53537c478bd9Sstevel@tonic-gate 					*rvalp = 1;
53547c478bd9Sstevel@tonic-gate 				else
53557c478bd9Sstevel@tonic-gate 					*rvalp = 0;
53567c478bd9Sstevel@tonic-gate 			}
53577c478bd9Sstevel@tonic-gate 		}
53587c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(q));
53597c478bd9Sstevel@tonic-gate 		return (0);
53607c478bd9Sstevel@tonic-gate 	    }
53617c478bd9Sstevel@tonic-gate 
53627c478bd9Sstevel@tonic-gate 	case I_GETBAND:
53637c478bd9Sstevel@tonic-gate 	    {
53647c478bd9Sstevel@tonic-gate 		int intpri;
53657c478bd9Sstevel@tonic-gate 		queue_t *q;
53667c478bd9Sstevel@tonic-gate 
53677c478bd9Sstevel@tonic-gate 		q = _RD(stp->sd_wrq);
53687c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(q));
53697c478bd9Sstevel@tonic-gate 		mp = q->q_first;
53707c478bd9Sstevel@tonic-gate 		if (!mp) {
53717c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(q));
53727c478bd9Sstevel@tonic-gate 			return (ENODATA);
53737c478bd9Sstevel@tonic-gate 		}
53747c478bd9Sstevel@tonic-gate 		intpri = (int)mp->b_band;
53757c478bd9Sstevel@tonic-gate 		error = strcopyout(&intpri, (void *)arg, sizeof (int),
53767c478bd9Sstevel@tonic-gate 		    copyflag);
53777c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(q));
53787c478bd9Sstevel@tonic-gate 		return (error);
53797c478bd9Sstevel@tonic-gate 	    }
53807c478bd9Sstevel@tonic-gate 
53817c478bd9Sstevel@tonic-gate 	case I_ATMARK:
53827c478bd9Sstevel@tonic-gate 	    {
53837c478bd9Sstevel@tonic-gate 		queue_t *q;
53847c478bd9Sstevel@tonic-gate 
53857c478bd9Sstevel@tonic-gate 		if (arg & ~(ANYMARK|LASTMARK))
53867c478bd9Sstevel@tonic-gate 			return (EINVAL);
53877c478bd9Sstevel@tonic-gate 		q = _RD(stp->sd_wrq);
53887c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
53897c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STRATMARK) && (arg == ANYMARK)) {
53907c478bd9Sstevel@tonic-gate 			*rvalp = 1;
53917c478bd9Sstevel@tonic-gate 		} else {
53927c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(q));
53937c478bd9Sstevel@tonic-gate 			mp = q->q_first;
53947c478bd9Sstevel@tonic-gate 
53957c478bd9Sstevel@tonic-gate 			if (mp == NULL)
53967c478bd9Sstevel@tonic-gate 				*rvalp = 0;
53977c478bd9Sstevel@tonic-gate 			else if ((arg == ANYMARK) && (mp->b_flag & MSGMARK))
53987c478bd9Sstevel@tonic-gate 				*rvalp = 1;
53997c478bd9Sstevel@tonic-gate 			else if ((arg == LASTMARK) && (mp == stp->sd_mark))
54007c478bd9Sstevel@tonic-gate 				*rvalp = 1;
54017c478bd9Sstevel@tonic-gate 			else
54027c478bd9Sstevel@tonic-gate 				*rvalp = 0;
54037c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(q));
54047c478bd9Sstevel@tonic-gate 		}
54057c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
54067c478bd9Sstevel@tonic-gate 		return (0);
54077c478bd9Sstevel@tonic-gate 	    }
54087c478bd9Sstevel@tonic-gate 
54097c478bd9Sstevel@tonic-gate 	case I_CANPUT:
54107c478bd9Sstevel@tonic-gate 	    {
54117c478bd9Sstevel@tonic-gate 		char band;
54127c478bd9Sstevel@tonic-gate 
54137c478bd9Sstevel@tonic-gate 		if ((arg < 0) || (arg >= NBAND))
54147c478bd9Sstevel@tonic-gate 			return (EINVAL);
54157c478bd9Sstevel@tonic-gate 		band = (char)arg;
54167c478bd9Sstevel@tonic-gate 		*rvalp = bcanputnext(stp->sd_wrq, band);
54177c478bd9Sstevel@tonic-gate 		return (0);
54187c478bd9Sstevel@tonic-gate 	    }
54197c478bd9Sstevel@tonic-gate 
54207c478bd9Sstevel@tonic-gate 	case I_SETCLTIME:
54217c478bd9Sstevel@tonic-gate 	    {
54227c478bd9Sstevel@tonic-gate 		int closetime;
54237c478bd9Sstevel@tonic-gate 
54247c478bd9Sstevel@tonic-gate 		error = strcopyin((void *)arg, &closetime, sizeof (int),
54257c478bd9Sstevel@tonic-gate 		    copyflag);
54267c478bd9Sstevel@tonic-gate 		if (error)
54277c478bd9Sstevel@tonic-gate 			return (error);
54287c478bd9Sstevel@tonic-gate 		if (closetime < 0)
54297c478bd9Sstevel@tonic-gate 			return (EINVAL);
54307c478bd9Sstevel@tonic-gate 
54317c478bd9Sstevel@tonic-gate 		stp->sd_closetime = closetime;
54327c478bd9Sstevel@tonic-gate 		return (0);
54337c478bd9Sstevel@tonic-gate 	    }
54347c478bd9Sstevel@tonic-gate 
54357c478bd9Sstevel@tonic-gate 	case I_GETCLTIME:
54367c478bd9Sstevel@tonic-gate 	    {
54377c478bd9Sstevel@tonic-gate 		int closetime;
54387c478bd9Sstevel@tonic-gate 
54397c478bd9Sstevel@tonic-gate 		closetime = stp->sd_closetime;
54407c478bd9Sstevel@tonic-gate 		return (strcopyout(&closetime, (void *)arg, sizeof (int),
54417c478bd9Sstevel@tonic-gate 		    copyflag));
54427c478bd9Sstevel@tonic-gate 	    }
54437c478bd9Sstevel@tonic-gate 
54447c478bd9Sstevel@tonic-gate 	case TIOCGSID:
54457c478bd9Sstevel@tonic-gate 	{
54467c478bd9Sstevel@tonic-gate 		pid_t sid;
54477c478bd9Sstevel@tonic-gate 
54487c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
54497c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp == NULL) {
54507c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
54517c478bd9Sstevel@tonic-gate 			return (ENOTTY);
54527c478bd9Sstevel@tonic-gate 		}
54537c478bd9Sstevel@tonic-gate 		sid = stp->sd_sidp->pid_id;
54547c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
54557c478bd9Sstevel@tonic-gate 		return (strcopyout(&sid, (void *)arg, sizeof (pid_t),
54567c478bd9Sstevel@tonic-gate 		    copyflag));
54577c478bd9Sstevel@tonic-gate 	}
54587c478bd9Sstevel@tonic-gate 
54597c478bd9Sstevel@tonic-gate 	case TIOCSPGRP:
54607c478bd9Sstevel@tonic-gate 	{
54617c478bd9Sstevel@tonic-gate 		pid_t pgrp;
54627c478bd9Sstevel@tonic-gate 		proc_t *q;
54637c478bd9Sstevel@tonic-gate 		pid_t	sid, fg_pgid, bg_pgid;
54647c478bd9Sstevel@tonic-gate 
54657c478bd9Sstevel@tonic-gate 		if (error = strcopyin((void *)arg, &pgrp, sizeof (pid_t),
54667c478bd9Sstevel@tonic-gate 		    copyflag))
54677c478bd9Sstevel@tonic-gate 			return (error);
54687c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
54697c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
54707c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp != ttoproc(curthread)->p_sessp->s_sidp) {
54717c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
54727c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
54737c478bd9Sstevel@tonic-gate 			return (ENOTTY);
54747c478bd9Sstevel@tonic-gate 		}
54757c478bd9Sstevel@tonic-gate 		if (pgrp == stp->sd_pgidp->pid_id) {
54767c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
54777c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
54787c478bd9Sstevel@tonic-gate 			return (0);
54797c478bd9Sstevel@tonic-gate 		}
54807c478bd9Sstevel@tonic-gate 		if (pgrp <= 0 || pgrp >= maxpid) {
54817c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
54827c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
54837c478bd9Sstevel@tonic-gate 			return (EINVAL);
54847c478bd9Sstevel@tonic-gate 		}
54857c478bd9Sstevel@tonic-gate 		if ((q = pgfind(pgrp)) == NULL ||
54867c478bd9Sstevel@tonic-gate 		    q->p_sessp != ttoproc(curthread)->p_sessp) {
54877c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
54887c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
54897c478bd9Sstevel@tonic-gate 			return (EPERM);
54907c478bd9Sstevel@tonic-gate 		}
54917c478bd9Sstevel@tonic-gate 		sid = stp->sd_sidp->pid_id;
54927c478bd9Sstevel@tonic-gate 		fg_pgid = q->p_pgrp;
54937c478bd9Sstevel@tonic-gate 		bg_pgid = stp->sd_pgidp->pid_id;
54947c478bd9Sstevel@tonic-gate 		CL_SET_PROCESS_GROUP(curthread, sid, bg_pgid, fg_pgid);
54957c478bd9Sstevel@tonic-gate 		PID_RELE(stp->sd_pgidp);
54967c478bd9Sstevel@tonic-gate 		stp->sd_pgidp = q->p_pgidp;
54977c478bd9Sstevel@tonic-gate 		PID_HOLD(stp->sd_pgidp);
54987c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
54997c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
55007c478bd9Sstevel@tonic-gate 		return (0);
55017c478bd9Sstevel@tonic-gate 	}
55027c478bd9Sstevel@tonic-gate 
55037c478bd9Sstevel@tonic-gate 	case TIOCGPGRP:
55047c478bd9Sstevel@tonic-gate 	{
55057c478bd9Sstevel@tonic-gate 		pid_t pgrp;
55067c478bd9Sstevel@tonic-gate 
55077c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
55087c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp == NULL) {
55097c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
55107c478bd9Sstevel@tonic-gate 			return (ENOTTY);
55117c478bd9Sstevel@tonic-gate 		}
55127c478bd9Sstevel@tonic-gate 		pgrp = stp->sd_pgidp->pid_id;
55137c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
55147c478bd9Sstevel@tonic-gate 		return (strcopyout(&pgrp, (void *)arg, sizeof (pid_t),
55157c478bd9Sstevel@tonic-gate 		    copyflag));
55167c478bd9Sstevel@tonic-gate 	}
55177c478bd9Sstevel@tonic-gate 
55187c478bd9Sstevel@tonic-gate 	case FIONBIO:
55197c478bd9Sstevel@tonic-gate 	case FIOASYNC:
55207c478bd9Sstevel@tonic-gate 		return (0);	/* handled by the upper layer */
55217c478bd9Sstevel@tonic-gate 	}
55227c478bd9Sstevel@tonic-gate }
55237c478bd9Sstevel@tonic-gate 
55247c478bd9Sstevel@tonic-gate /*
55257c478bd9Sstevel@tonic-gate  * Custom free routine used for M_PASSFP messages.
55267c478bd9Sstevel@tonic-gate  */
55277c478bd9Sstevel@tonic-gate static void
55287c478bd9Sstevel@tonic-gate free_passfp(struct k_strrecvfd *srf)
55297c478bd9Sstevel@tonic-gate {
55307c478bd9Sstevel@tonic-gate 	(void) closef(srf->fp);
55317c478bd9Sstevel@tonic-gate 	kmem_free(srf, sizeof (struct k_strrecvfd) + sizeof (frtn_t));
55327c478bd9Sstevel@tonic-gate }
55337c478bd9Sstevel@tonic-gate 
55347c478bd9Sstevel@tonic-gate /* ARGSUSED */
55357c478bd9Sstevel@tonic-gate int
55367c478bd9Sstevel@tonic-gate do_sendfp(struct stdata *stp, struct file *fp, struct cred *cr)
55377c478bd9Sstevel@tonic-gate {
55387c478bd9Sstevel@tonic-gate 	queue_t *qp, *nextqp;
55397c478bd9Sstevel@tonic-gate 	struct k_strrecvfd *srf;
55407c478bd9Sstevel@tonic-gate 	mblk_t *mp;
55417c478bd9Sstevel@tonic-gate 	frtn_t *frtnp;
55427c478bd9Sstevel@tonic-gate 	size_t bufsize;
55437c478bd9Sstevel@tonic-gate 	queue_t	*mate = NULL;
55447c478bd9Sstevel@tonic-gate 	syncq_t	*sq = NULL;
55457c478bd9Sstevel@tonic-gate 	int retval = 0;
55467c478bd9Sstevel@tonic-gate 
55477c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & STRHUP)
55487c478bd9Sstevel@tonic-gate 		return (ENXIO);
55497c478bd9Sstevel@tonic-gate 
55507c478bd9Sstevel@tonic-gate 	claimstr(stp->sd_wrq);
55517c478bd9Sstevel@tonic-gate 
55527c478bd9Sstevel@tonic-gate 	/* Fastpath, we have a pipe, and we are already mated, use it. */
55537c478bd9Sstevel@tonic-gate 	if (STRMATED(stp)) {
55547c478bd9Sstevel@tonic-gate 		qp = _RD(stp->sd_mate->sd_wrq);
55557c478bd9Sstevel@tonic-gate 		claimstr(qp);
55567c478bd9Sstevel@tonic-gate 		mate = qp;
55577c478bd9Sstevel@tonic-gate 	} else { /* Not already mated. */
55587c478bd9Sstevel@tonic-gate 
55597c478bd9Sstevel@tonic-gate 		/*
55607c478bd9Sstevel@tonic-gate 		 * Walk the stream to the end of this one.
55617c478bd9Sstevel@tonic-gate 		 * assumes that the claimstr() will prevent
55627c478bd9Sstevel@tonic-gate 		 * plumbing between the stream head and the
55637c478bd9Sstevel@tonic-gate 		 * driver from changing
55647c478bd9Sstevel@tonic-gate 		 */
55657c478bd9Sstevel@tonic-gate 		qp = stp->sd_wrq;
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate 		/*
55687c478bd9Sstevel@tonic-gate 		 * Loop until we reach the end of this stream.
55697c478bd9Sstevel@tonic-gate 		 * On completion, qp points to the write queue
55707c478bd9Sstevel@tonic-gate 		 * at the end of the stream, or the read queue
55717c478bd9Sstevel@tonic-gate 		 * at the stream head if this is a fifo.
55727c478bd9Sstevel@tonic-gate 		 */
55737c478bd9Sstevel@tonic-gate 		while (((qp = qp->q_next) != NULL) && _SAMESTR(qp))
55747c478bd9Sstevel@tonic-gate 			;
55757c478bd9Sstevel@tonic-gate 
55767c478bd9Sstevel@tonic-gate 		/*
55777c478bd9Sstevel@tonic-gate 		 * Just in case we get a q_next which is NULL, but
55787c478bd9Sstevel@tonic-gate 		 * not at the end of the stream.  This is actually
55797c478bd9Sstevel@tonic-gate 		 * broken, so we set an assert to catch it in
55807c478bd9Sstevel@tonic-gate 		 * debug, and set an error and return if not debug.
55817c478bd9Sstevel@tonic-gate 		 */
55827c478bd9Sstevel@tonic-gate 		ASSERT(qp);
55837c478bd9Sstevel@tonic-gate 		if (qp == NULL) {
55847c478bd9Sstevel@tonic-gate 			releasestr(stp->sd_wrq);
55857c478bd9Sstevel@tonic-gate 			return (EINVAL);
55867c478bd9Sstevel@tonic-gate 		}
55877c478bd9Sstevel@tonic-gate 
55887c478bd9Sstevel@tonic-gate 		/*
55897c478bd9Sstevel@tonic-gate 		 * Enter the syncq for the driver, so (hopefully)
55907c478bd9Sstevel@tonic-gate 		 * the queue values will not change on us.
55917c478bd9Sstevel@tonic-gate 		 * XXXX - This will only prevent the race IFF only
55927c478bd9Sstevel@tonic-gate 		 *   the write side modifies the q_next member, and
55937c478bd9Sstevel@tonic-gate 		 *   the put procedure is protected by at least
55947c478bd9Sstevel@tonic-gate 		 *   MT_PERQ.
55957c478bd9Sstevel@tonic-gate 		 */
55967c478bd9Sstevel@tonic-gate 		if ((sq = qp->q_syncq) != NULL)
55977c478bd9Sstevel@tonic-gate 			entersq(sq, SQ_PUT);
55987c478bd9Sstevel@tonic-gate 
55997c478bd9Sstevel@tonic-gate 		/* Now get the q_next value from this qp. */
56007c478bd9Sstevel@tonic-gate 		nextqp = qp->q_next;
56017c478bd9Sstevel@tonic-gate 
56027c478bd9Sstevel@tonic-gate 		/*
56037c478bd9Sstevel@tonic-gate 		 * If nextqp exists and the other stream is different
56047c478bd9Sstevel@tonic-gate 		 * from this one claim the stream, set the mate, and
56057c478bd9Sstevel@tonic-gate 		 * get the read queue at the stream head of the other
56067c478bd9Sstevel@tonic-gate 		 * stream.  Assumes that nextqp was at least valid when
56077c478bd9Sstevel@tonic-gate 		 * we got it.  Hopefully the entersq of the driver
56087c478bd9Sstevel@tonic-gate 		 * will prevent it from changing on us.
56097c478bd9Sstevel@tonic-gate 		 */
56107c478bd9Sstevel@tonic-gate 		if ((nextqp != NULL) && (STREAM(nextqp) != stp)) {
56117c478bd9Sstevel@tonic-gate 			ASSERT(qp->q_qinfo->qi_srvp);
56127c478bd9Sstevel@tonic-gate 			ASSERT(_OTHERQ(qp)->q_qinfo->qi_srvp);
56137c478bd9Sstevel@tonic-gate 			ASSERT(_OTHERQ(qp->q_next)->q_qinfo->qi_srvp);
56147c478bd9Sstevel@tonic-gate 			claimstr(nextqp);
56157c478bd9Sstevel@tonic-gate 
56167c478bd9Sstevel@tonic-gate 			/* Make sure we still have a q_next */
56177c478bd9Sstevel@tonic-gate 			if (nextqp != qp->q_next) {
56187c478bd9Sstevel@tonic-gate 				releasestr(stp->sd_wrq);
56197c478bd9Sstevel@tonic-gate 				releasestr(nextqp);
56207c478bd9Sstevel@tonic-gate 				return (EINVAL);
56217c478bd9Sstevel@tonic-gate 			}
56227c478bd9Sstevel@tonic-gate 
56237c478bd9Sstevel@tonic-gate 			qp = _RD(STREAM(nextqp)->sd_wrq);
56247c478bd9Sstevel@tonic-gate 			mate = qp;
56257c478bd9Sstevel@tonic-gate 		}
56267c478bd9Sstevel@tonic-gate 		/* If we entered the synq above, leave it. */
56277c478bd9Sstevel@tonic-gate 		if (sq != NULL)
56287c478bd9Sstevel@tonic-gate 			leavesq(sq, SQ_PUT);
56297c478bd9Sstevel@tonic-gate 	} /*  STRMATED(STP)  */
56307c478bd9Sstevel@tonic-gate 
56317c478bd9Sstevel@tonic-gate 	/* XXX prevents substitution of the ops vector */
56327c478bd9Sstevel@tonic-gate 	if (qp->q_qinfo != &strdata && qp->q_qinfo != &fifo_strdata) {
56337c478bd9Sstevel@tonic-gate 		retval = EINVAL;
56347c478bd9Sstevel@tonic-gate 		goto out;
56357c478bd9Sstevel@tonic-gate 	}
56367c478bd9Sstevel@tonic-gate 
56377c478bd9Sstevel@tonic-gate 	if (qp->q_flag & QFULL) {
56387c478bd9Sstevel@tonic-gate 		retval = EAGAIN;
56397c478bd9Sstevel@tonic-gate 		goto out;
56407c478bd9Sstevel@tonic-gate 	}
56417c478bd9Sstevel@tonic-gate 
56427c478bd9Sstevel@tonic-gate 	/*
56437c478bd9Sstevel@tonic-gate 	 * Since M_PASSFP messages include a file descriptor, we use
56447c478bd9Sstevel@tonic-gate 	 * esballoc() and specify a custom free routine (free_passfp()) that
56457c478bd9Sstevel@tonic-gate 	 * will close the descriptor as part of freeing the message.  For
56467c478bd9Sstevel@tonic-gate 	 * convenience, we stash the frtn_t right after the data block.
56477c478bd9Sstevel@tonic-gate 	 */
56487c478bd9Sstevel@tonic-gate 	bufsize = sizeof (struct k_strrecvfd) + sizeof (frtn_t);
56497c478bd9Sstevel@tonic-gate 	srf = kmem_alloc(bufsize, KM_NOSLEEP);
56507c478bd9Sstevel@tonic-gate 	if (srf == NULL) {
56517c478bd9Sstevel@tonic-gate 		retval = EAGAIN;
56527c478bd9Sstevel@tonic-gate 		goto out;
56537c478bd9Sstevel@tonic-gate 	}
56547c478bd9Sstevel@tonic-gate 
56557c478bd9Sstevel@tonic-gate 	frtnp = (frtn_t *)(srf + 1);
56567c478bd9Sstevel@tonic-gate 	frtnp->free_arg = (caddr_t)srf;
56577c478bd9Sstevel@tonic-gate 	frtnp->free_func = free_passfp;
56587c478bd9Sstevel@tonic-gate 
56597c478bd9Sstevel@tonic-gate 	mp = esballoc((uchar_t *)srf, bufsize, BPRI_MED, frtnp);
56607c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
56617c478bd9Sstevel@tonic-gate 		kmem_free(srf, bufsize);
56627c478bd9Sstevel@tonic-gate 		retval = EAGAIN;
56637c478bd9Sstevel@tonic-gate 		goto out;
56647c478bd9Sstevel@tonic-gate 	}
56657c478bd9Sstevel@tonic-gate 	mp->b_wptr += sizeof (struct k_strrecvfd);
56667c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PASSFP;
56677c478bd9Sstevel@tonic-gate 
56687c478bd9Sstevel@tonic-gate 	srf->fp = fp;
56697c478bd9Sstevel@tonic-gate 	srf->uid = crgetuid(curthread->t_cred);
56707c478bd9Sstevel@tonic-gate 	srf->gid = crgetgid(curthread->t_cred);
56717c478bd9Sstevel@tonic-gate 	mutex_enter(&fp->f_tlock);
56727c478bd9Sstevel@tonic-gate 	fp->f_count++;
56737c478bd9Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
56747c478bd9Sstevel@tonic-gate 
56757c478bd9Sstevel@tonic-gate 	put(qp, mp);
56767c478bd9Sstevel@tonic-gate out:
56777c478bd9Sstevel@tonic-gate 	releasestr(stp->sd_wrq);
56787c478bd9Sstevel@tonic-gate 	if (mate)
56797c478bd9Sstevel@tonic-gate 		releasestr(mate);
56807c478bd9Sstevel@tonic-gate 	return (retval);
56817c478bd9Sstevel@tonic-gate }
56827c478bd9Sstevel@tonic-gate 
56837c478bd9Sstevel@tonic-gate /*
56847c478bd9Sstevel@tonic-gate  * Send an ioctl message downstream and wait for acknowledgement.
56857c478bd9Sstevel@tonic-gate  * flags may be set to either U_TO_K or K_TO_K and a combination
56867c478bd9Sstevel@tonic-gate  * of STR_NOERROR or STR_NOSIG
56877c478bd9Sstevel@tonic-gate  * STR_NOSIG: Signals are essentially ignored or held and have
56887c478bd9Sstevel@tonic-gate  *	no effect for the duration of the call.
56897c478bd9Sstevel@tonic-gate  * STR_NOERROR: Ignores stream head read, write and hup errors.
56907c478bd9Sstevel@tonic-gate  *	Additionally, if an existing ioctl times out, it is assumed
56917c478bd9Sstevel@tonic-gate  *	lost and and this ioctl will continue as if the previous ioctl had
56927c478bd9Sstevel@tonic-gate  *	finished.  ETIME may be returned if this ioctl times out (i.e.
56937c478bd9Sstevel@tonic-gate  *	ic_timout is not INFTIM).  Non-stream head errors may be returned if
56947c478bd9Sstevel@tonic-gate  *	the ioc_error indicates that the driver/module had problems,
56957c478bd9Sstevel@tonic-gate  *	an EFAULT was found when accessing user data, a lack of
56967c478bd9Sstevel@tonic-gate  * 	resources, etc.
56977c478bd9Sstevel@tonic-gate  */
56987c478bd9Sstevel@tonic-gate int
56997c478bd9Sstevel@tonic-gate strdoioctl(
57007c478bd9Sstevel@tonic-gate 	struct stdata *stp,
57017c478bd9Sstevel@tonic-gate 	struct strioctl *strioc,
57027c478bd9Sstevel@tonic-gate 	int fflags,		/* file flags with model info */
57037c478bd9Sstevel@tonic-gate 	int flag,
57047c478bd9Sstevel@tonic-gate 	cred_t *crp,
57057c478bd9Sstevel@tonic-gate 	int *rvalp)
57067c478bd9Sstevel@tonic-gate {
57077c478bd9Sstevel@tonic-gate 	mblk_t *bp;
57087c478bd9Sstevel@tonic-gate 	struct iocblk *iocbp;
57097c478bd9Sstevel@tonic-gate 	struct copyreq *reqp;
57107c478bd9Sstevel@tonic-gate 	struct copyresp *resp;
57117c478bd9Sstevel@tonic-gate 	int id;
57127c478bd9Sstevel@tonic-gate 	int transparent = 0;
57137c478bd9Sstevel@tonic-gate 	int error = 0;
57147c478bd9Sstevel@tonic-gate 	int len = 0;
57157c478bd9Sstevel@tonic-gate 	caddr_t taddr;
57167c478bd9Sstevel@tonic-gate 	int copyflag = (flag & (U_TO_K | K_TO_K));
57177c478bd9Sstevel@tonic-gate 	int sigflag = (flag & STR_NOSIG);
57187c478bd9Sstevel@tonic-gate 	int errs;
57197c478bd9Sstevel@tonic-gate 	uint_t waitflags;
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate 	ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
57227c478bd9Sstevel@tonic-gate 	ASSERT((fflags & FMODELS) != 0);
57237c478bd9Sstevel@tonic-gate 
57247c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_STREAMS_FR,
57257c478bd9Sstevel@tonic-gate 		TR_STRDOIOCTL,
57267c478bd9Sstevel@tonic-gate 		"strdoioctl:stp %p strioc %p", stp, strioc);
57277c478bd9Sstevel@tonic-gate 	if (strioc->ic_len == TRANSPARENT) {	/* send arg in M_DATA block */
57287c478bd9Sstevel@tonic-gate 		transparent = 1;
57297c478bd9Sstevel@tonic-gate 		strioc->ic_len = sizeof (intptr_t);
57307c478bd9Sstevel@tonic-gate 	}
57317c478bd9Sstevel@tonic-gate 
57327c478bd9Sstevel@tonic-gate 	if (strioc->ic_len < 0 || (strmsgsz > 0 && strioc->ic_len > strmsgsz))
57337c478bd9Sstevel@tonic-gate 		return (EINVAL);
57347c478bd9Sstevel@tonic-gate 
57357c478bd9Sstevel@tonic-gate 	if ((bp = allocb_cred_wait(sizeof (union ioctypes), sigflag, &error,
57367c478bd9Sstevel@tonic-gate 	    crp)) == NULL)
57377c478bd9Sstevel@tonic-gate 			return (error);
57387c478bd9Sstevel@tonic-gate 
57397c478bd9Sstevel@tonic-gate 	bzero(bp->b_wptr, sizeof (union ioctypes));
57407c478bd9Sstevel@tonic-gate 
57417c478bd9Sstevel@tonic-gate 	iocbp = (struct iocblk *)bp->b_wptr;
57427c478bd9Sstevel@tonic-gate 	iocbp->ioc_count = strioc->ic_len;
57437c478bd9Sstevel@tonic-gate 	iocbp->ioc_cmd = strioc->ic_cmd;
57447c478bd9Sstevel@tonic-gate 	iocbp->ioc_flag = (fflags & FMODELS);
57457c478bd9Sstevel@tonic-gate 
57467c478bd9Sstevel@tonic-gate 	crhold(crp);
57477c478bd9Sstevel@tonic-gate 	iocbp->ioc_cr = crp;
57487c478bd9Sstevel@tonic-gate 	DB_TYPE(bp) = M_IOCTL;
57497c478bd9Sstevel@tonic-gate 	DB_CPID(bp) = curproc->p_pid;
57507c478bd9Sstevel@tonic-gate 	bp->b_wptr += sizeof (struct iocblk);
57517c478bd9Sstevel@tonic-gate 
57527c478bd9Sstevel@tonic-gate 	if (flag & STR_NOERROR)
57537c478bd9Sstevel@tonic-gate 		errs = STPLEX;
57547c478bd9Sstevel@tonic-gate 	else
57557c478bd9Sstevel@tonic-gate 		errs = STRHUP|STRDERR|STWRERR|STPLEX;
57567c478bd9Sstevel@tonic-gate 
57577c478bd9Sstevel@tonic-gate 	/*
57587c478bd9Sstevel@tonic-gate 	 * If there is data to copy into ioctl block, do so.
57597c478bd9Sstevel@tonic-gate 	 */
57607c478bd9Sstevel@tonic-gate 	if (iocbp->ioc_count > 0) {
57617c478bd9Sstevel@tonic-gate 		if (transparent)
57627c478bd9Sstevel@tonic-gate 			/*
57637c478bd9Sstevel@tonic-gate 			 * Note: STR_NOERROR does not have an effect
57647c478bd9Sstevel@tonic-gate 			 * in putiocd()
57657c478bd9Sstevel@tonic-gate 			 */
57667c478bd9Sstevel@tonic-gate 			id = K_TO_K | sigflag;
57677c478bd9Sstevel@tonic-gate 		else
57687c478bd9Sstevel@tonic-gate 			id = flag;
57697c478bd9Sstevel@tonic-gate 		if ((error = putiocd(bp, strioc->ic_dp, id, crp)) != 0) {
57707c478bd9Sstevel@tonic-gate 			freemsg(bp);
57717c478bd9Sstevel@tonic-gate 			crfree(crp);
57727c478bd9Sstevel@tonic-gate 			return (error);
57737c478bd9Sstevel@tonic-gate 		}
57747c478bd9Sstevel@tonic-gate 
57757c478bd9Sstevel@tonic-gate 		/*
57767c478bd9Sstevel@tonic-gate 		 * We could have slept copying in user pages.
57777c478bd9Sstevel@tonic-gate 		 * Recheck the stream head state (the other end
57787c478bd9Sstevel@tonic-gate 		 * of a pipe could have gone away).
57797c478bd9Sstevel@tonic-gate 		 */
57807c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & errs) {
57817c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
57827c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, errs, 0);
57837c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
57847c478bd9Sstevel@tonic-gate 			if (error != 0) {
57857c478bd9Sstevel@tonic-gate 				freemsg(bp);
57867c478bd9Sstevel@tonic-gate 				crfree(crp);
57877c478bd9Sstevel@tonic-gate 				return (error);
57887c478bd9Sstevel@tonic-gate 			}
57897c478bd9Sstevel@tonic-gate 		}
57907c478bd9Sstevel@tonic-gate 	}
57917c478bd9Sstevel@tonic-gate 	if (transparent)
57927c478bd9Sstevel@tonic-gate 		iocbp->ioc_count = TRANSPARENT;
57937c478bd9Sstevel@tonic-gate 
57947c478bd9Sstevel@tonic-gate 	/*
57957c478bd9Sstevel@tonic-gate 	 * Block for up to STRTIMOUT milliseconds if there is an outstanding
57967c478bd9Sstevel@tonic-gate 	 * ioctl for this stream already running.  All processes
57977c478bd9Sstevel@tonic-gate 	 * sleeping here will be awakened as a result of an ACK
57987c478bd9Sstevel@tonic-gate 	 * or NAK being received for the outstanding ioctl, or
57997c478bd9Sstevel@tonic-gate 	 * as a result of the timer expiring on the outstanding
58007c478bd9Sstevel@tonic-gate 	 * ioctl (a failure), or as a result of any waiting
58017c478bd9Sstevel@tonic-gate 	 * process's timer expiring (also a failure).
58027c478bd9Sstevel@tonic-gate 	 */
58037c478bd9Sstevel@tonic-gate 
58047c478bd9Sstevel@tonic-gate 	error = 0;
58057c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
58067c478bd9Sstevel@tonic-gate 	while (stp->sd_flag & (IOCWAIT | IOCWAITNE)) {
58077c478bd9Sstevel@tonic-gate 		clock_t cv_rval;
58087c478bd9Sstevel@tonic-gate 
58097c478bd9Sstevel@tonic-gate 		TRACE_0(TR_FAC_STREAMS_FR,
58107c478bd9Sstevel@tonic-gate 			TR_STRDOIOCTL_WAIT,
58117c478bd9Sstevel@tonic-gate 			"strdoioctl sleeps - IOCWAIT");
58127c478bd9Sstevel@tonic-gate 		cv_rval = str_cv_wait(&stp->sd_iocmonitor, &stp->sd_lock,
58137c478bd9Sstevel@tonic-gate 		    STRTIMOUT, sigflag);
58147c478bd9Sstevel@tonic-gate 		if (cv_rval <= 0) {
58157c478bd9Sstevel@tonic-gate 			if (cv_rval == 0) {
58167c478bd9Sstevel@tonic-gate 				error = EINTR;
58177c478bd9Sstevel@tonic-gate 			} else {
58187c478bd9Sstevel@tonic-gate 				if (flag & STR_NOERROR) {
58197c478bd9Sstevel@tonic-gate 					/*
58207c478bd9Sstevel@tonic-gate 					 * Terminating current ioctl in
58217c478bd9Sstevel@tonic-gate 					 * progress -- assume it got lost and
58227c478bd9Sstevel@tonic-gate 					 * wake up the other thread so that the
58237c478bd9Sstevel@tonic-gate 					 * operation completes.
58247c478bd9Sstevel@tonic-gate 					 */
58257c478bd9Sstevel@tonic-gate 					if (!(stp->sd_flag & IOCWAITNE)) {
58267c478bd9Sstevel@tonic-gate 						stp->sd_flag |= IOCWAITNE;
58277c478bd9Sstevel@tonic-gate 						cv_broadcast(&stp->sd_monitor);
58287c478bd9Sstevel@tonic-gate 					}
58297c478bd9Sstevel@tonic-gate 					/*
58307c478bd9Sstevel@tonic-gate 					 * Otherwise, there's a running
58317c478bd9Sstevel@tonic-gate 					 * STR_NOERROR -- we have no choice
58327c478bd9Sstevel@tonic-gate 					 * here but to wait forever (or until
58337c478bd9Sstevel@tonic-gate 					 * interrupted).
58347c478bd9Sstevel@tonic-gate 					 */
58357c478bd9Sstevel@tonic-gate 				} else {
58367c478bd9Sstevel@tonic-gate 					/*
58377c478bd9Sstevel@tonic-gate 					 * pending ioctl has caused
58387c478bd9Sstevel@tonic-gate 					 * us to time out
58397c478bd9Sstevel@tonic-gate 					 */
58407c478bd9Sstevel@tonic-gate 					error = ETIME;
58417c478bd9Sstevel@tonic-gate 				}
58427c478bd9Sstevel@tonic-gate 			}
58437c478bd9Sstevel@tonic-gate 		} else if ((stp->sd_flag & errs)) {
58447c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, errs, 0);
58457c478bd9Sstevel@tonic-gate 		}
58467c478bd9Sstevel@tonic-gate 		if (error) {
58477c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
58487c478bd9Sstevel@tonic-gate 			freemsg(bp);
58497c478bd9Sstevel@tonic-gate 			crfree(crp);
58507c478bd9Sstevel@tonic-gate 			return (error);
58517c478bd9Sstevel@tonic-gate 		}
58527c478bd9Sstevel@tonic-gate 	}
58537c478bd9Sstevel@tonic-gate 
58547c478bd9Sstevel@tonic-gate 	/*
58557c478bd9Sstevel@tonic-gate 	 * Have control of ioctl mechanism.
58567c478bd9Sstevel@tonic-gate 	 * Send down ioctl packet and wait for response.
58577c478bd9Sstevel@tonic-gate 	 */
58587c478bd9Sstevel@tonic-gate 	if (stp->sd_iocblk != (mblk_t *)-1) {
58597c478bd9Sstevel@tonic-gate 		freemsg(stp->sd_iocblk);
58607c478bd9Sstevel@tonic-gate 	}
58617c478bd9Sstevel@tonic-gate 	stp->sd_iocblk = NULL;
58627c478bd9Sstevel@tonic-gate 
58637c478bd9Sstevel@tonic-gate 	/*
58647c478bd9Sstevel@tonic-gate 	 * If this is marked with 'noerror' (internal; mostly
58657c478bd9Sstevel@tonic-gate 	 * I_{P,}{UN,}LINK), then make sure nobody else is able to get
58667c478bd9Sstevel@tonic-gate 	 * in here by setting IOCWAITNE.
58677c478bd9Sstevel@tonic-gate 	 */
58687c478bd9Sstevel@tonic-gate 	waitflags = IOCWAIT;
58697c478bd9Sstevel@tonic-gate 	if (flag & STR_NOERROR)
58707c478bd9Sstevel@tonic-gate 		waitflags |= IOCWAITNE;
58717c478bd9Sstevel@tonic-gate 
58727c478bd9Sstevel@tonic-gate 	stp->sd_flag |= waitflags;
58737c478bd9Sstevel@tonic-gate 
58747c478bd9Sstevel@tonic-gate 	/*
58757c478bd9Sstevel@tonic-gate 	 * Assign sequence number.
58767c478bd9Sstevel@tonic-gate 	 */
58777c478bd9Sstevel@tonic-gate 	iocbp->ioc_id = stp->sd_iocid = getiocseqno();
58787c478bd9Sstevel@tonic-gate 
58797c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
58807c478bd9Sstevel@tonic-gate 
58817c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
58827c478bd9Sstevel@tonic-gate 		TR_STRDOIOCTL_PUT, "strdoioctl put: stp %p", stp);
58837c478bd9Sstevel@tonic-gate 	stream_willservice(stp);
58847c478bd9Sstevel@tonic-gate 	putnext(stp->sd_wrq, bp);
58857c478bd9Sstevel@tonic-gate 	stream_runservice(stp);
58867c478bd9Sstevel@tonic-gate 
58877c478bd9Sstevel@tonic-gate 	/*
58887c478bd9Sstevel@tonic-gate 	 * Timed wait for acknowledgment.  The wait time is limited by the
58897c478bd9Sstevel@tonic-gate 	 * timeout value, which must be a positive integer (number of
5890*c28749e9Skais 	 * milliseconds) to wait, or 0 (use default value of STRTIMOUT
58917c478bd9Sstevel@tonic-gate 	 * milliseconds), or -1 (wait forever).  This will be awakened
58927c478bd9Sstevel@tonic-gate 	 * either by an ACK/NAK message arriving, the timer expiring, or
58937c478bd9Sstevel@tonic-gate 	 * the timer expiring on another ioctl waiting for control of the
58947c478bd9Sstevel@tonic-gate 	 * mechanism.
58957c478bd9Sstevel@tonic-gate 	 */
58967c478bd9Sstevel@tonic-gate waitioc:
58977c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
58987c478bd9Sstevel@tonic-gate 
58997c478bd9Sstevel@tonic-gate 
59007c478bd9Sstevel@tonic-gate 	/*
59017c478bd9Sstevel@tonic-gate 	 * If the reply has already arrived, don't sleep.  If awakened from
59027c478bd9Sstevel@tonic-gate 	 * the sleep, fail only if the reply has not arrived by then.
59037c478bd9Sstevel@tonic-gate 	 * Otherwise, process the reply.
59047c478bd9Sstevel@tonic-gate 	 */
59057c478bd9Sstevel@tonic-gate 	while (!stp->sd_iocblk) {
59067c478bd9Sstevel@tonic-gate 		clock_t cv_rval;
59077c478bd9Sstevel@tonic-gate 
59087c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & errs) {
59097c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, errs, 0);
59107c478bd9Sstevel@tonic-gate 			if (error != 0) {
59117c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~waitflags;
59127c478bd9Sstevel@tonic-gate 				cv_broadcast(&stp->sd_iocmonitor);
59137c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
59147c478bd9Sstevel@tonic-gate 				crfree(crp);
59157c478bd9Sstevel@tonic-gate 				return (error);
59167c478bd9Sstevel@tonic-gate 			}
59177c478bd9Sstevel@tonic-gate 		}
59187c478bd9Sstevel@tonic-gate 
59197c478bd9Sstevel@tonic-gate 		TRACE_0(TR_FAC_STREAMS_FR,
59207c478bd9Sstevel@tonic-gate 			TR_STRDOIOCTL_WAIT2,
59217c478bd9Sstevel@tonic-gate 			"strdoioctl sleeps awaiting reply");
59227c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
59237c478bd9Sstevel@tonic-gate 
59247c478bd9Sstevel@tonic-gate 		cv_rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock,
59257c478bd9Sstevel@tonic-gate 		    (strioc->ic_timout ?
59267c478bd9Sstevel@tonic-gate 		    strioc->ic_timout * 1000 : STRTIMOUT), sigflag);
59277c478bd9Sstevel@tonic-gate 
59287c478bd9Sstevel@tonic-gate 		/*
59297c478bd9Sstevel@tonic-gate 		 * There are four possible cases here: interrupt, timeout,
59307c478bd9Sstevel@tonic-gate 		 * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
59317c478bd9Sstevel@tonic-gate 		 * valid M_IOCTL reply).
59327c478bd9Sstevel@tonic-gate 		 *
59337c478bd9Sstevel@tonic-gate 		 * If we've been awakened by a STR_NOERROR ioctl on some other
59347c478bd9Sstevel@tonic-gate 		 * thread, then sd_iocblk will still be NULL, and IOCWAITNE
59357c478bd9Sstevel@tonic-gate 		 * will be set.  Pretend as if we just timed out.  Note that
59367c478bd9Sstevel@tonic-gate 		 * this other thread waited at least STRTIMOUT before trying to
59377c478bd9Sstevel@tonic-gate 		 * awaken our thread, so this is indistinguishable (even for
59387c478bd9Sstevel@tonic-gate 		 * INFTIM) from the case where we failed with ETIME waiting on
59397c478bd9Sstevel@tonic-gate 		 * IOCWAIT in the prior loop.
59407c478bd9Sstevel@tonic-gate 		 */
59417c478bd9Sstevel@tonic-gate 		if (cv_rval > 0 && !(flag & STR_NOERROR) &&
59427c478bd9Sstevel@tonic-gate 		    stp->sd_iocblk == NULL && (stp->sd_flag & IOCWAITNE)) {
59437c478bd9Sstevel@tonic-gate 			cv_rval = -1;
59447c478bd9Sstevel@tonic-gate 		}
59457c478bd9Sstevel@tonic-gate 
59467c478bd9Sstevel@tonic-gate 		/*
59477c478bd9Sstevel@tonic-gate 		 * note: STR_NOERROR does not protect
59487c478bd9Sstevel@tonic-gate 		 * us here.. use ic_timout < 0
59497c478bd9Sstevel@tonic-gate 		 */
59507c478bd9Sstevel@tonic-gate 		if (cv_rval <= 0) {
59517c478bd9Sstevel@tonic-gate 			if (cv_rval == 0) {
59527c478bd9Sstevel@tonic-gate 				error = EINTR;
59537c478bd9Sstevel@tonic-gate 			} else {
59547c478bd9Sstevel@tonic-gate 				error =  ETIME;
59557c478bd9Sstevel@tonic-gate 			}
59567c478bd9Sstevel@tonic-gate 			/*
59577c478bd9Sstevel@tonic-gate 			 * A message could have come in after we were scheduled
59587c478bd9Sstevel@tonic-gate 			 * but before we were actually run.
59597c478bd9Sstevel@tonic-gate 			 */
59607c478bd9Sstevel@tonic-gate 			bp = stp->sd_iocblk;
59617c478bd9Sstevel@tonic-gate 			stp->sd_iocblk = NULL;
59627c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
59637c478bd9Sstevel@tonic-gate 				if ((bp->b_datap->db_type == M_COPYIN) ||
59647c478bd9Sstevel@tonic-gate 				    (bp->b_datap->db_type == M_COPYOUT)) {
59657c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
59667c478bd9Sstevel@tonic-gate 					if (bp->b_cont) {
59677c478bd9Sstevel@tonic-gate 						freemsg(bp->b_cont);
59687c478bd9Sstevel@tonic-gate 						bp->b_cont = NULL;
59697c478bd9Sstevel@tonic-gate 					}
59707c478bd9Sstevel@tonic-gate 					bp->b_datap->db_type = M_IOCDATA;
59717c478bd9Sstevel@tonic-gate 					bp->b_wptr = bp->b_rptr +
59727c478bd9Sstevel@tonic-gate 						sizeof (struct copyresp);
59737c478bd9Sstevel@tonic-gate 					resp = (struct copyresp *)bp->b_rptr;
59747c478bd9Sstevel@tonic-gate 					resp->cp_rval =
59757c478bd9Sstevel@tonic-gate 					    (caddr_t)1; /* failure */
59767c478bd9Sstevel@tonic-gate 					stream_willservice(stp);
59777c478bd9Sstevel@tonic-gate 					putnext(stp->sd_wrq, bp);
59787c478bd9Sstevel@tonic-gate 					stream_runservice(stp);
59797c478bd9Sstevel@tonic-gate 					mutex_enter(&stp->sd_lock);
59807c478bd9Sstevel@tonic-gate 				} else {
59817c478bd9Sstevel@tonic-gate 					freemsg(bp);
59827c478bd9Sstevel@tonic-gate 				}
59837c478bd9Sstevel@tonic-gate 			}
59847c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~waitflags;
59857c478bd9Sstevel@tonic-gate 			cv_broadcast(&stp->sd_iocmonitor);
59867c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
59877c478bd9Sstevel@tonic-gate 			crfree(crp);
59887c478bd9Sstevel@tonic-gate 			return (error);
59897c478bd9Sstevel@tonic-gate 		}
59907c478bd9Sstevel@tonic-gate 	}
59917c478bd9Sstevel@tonic-gate 	bp = stp->sd_iocblk;
59927c478bd9Sstevel@tonic-gate 	/*
59937c478bd9Sstevel@tonic-gate 	 * Note: it is strictly impossible to get here with sd_iocblk set to
59947c478bd9Sstevel@tonic-gate 	 * -1.  This is because the initial loop above doesn't allow any new
59957c478bd9Sstevel@tonic-gate 	 * ioctls into the fray until all others have passed this point.
59967c478bd9Sstevel@tonic-gate 	 */
59977c478bd9Sstevel@tonic-gate 	ASSERT(bp != NULL && bp != (mblk_t *)-1);
59987c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
59997c478bd9Sstevel@tonic-gate 		TR_STRDOIOCTL_ACK, "strdoioctl got reply: bp %p", bp);
60007c478bd9Sstevel@tonic-gate 	if ((bp->b_datap->db_type == M_IOCACK) ||
60017c478bd9Sstevel@tonic-gate 	    (bp->b_datap->db_type == M_IOCNAK)) {
60027c478bd9Sstevel@tonic-gate 		/* for detection of duplicate ioctl replies */
60037c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = (mblk_t *)-1;
60047c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~waitflags;
60057c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_iocmonitor);
60067c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
60077c478bd9Sstevel@tonic-gate 	} else {
60087c478bd9Sstevel@tonic-gate 		/*
60097c478bd9Sstevel@tonic-gate 		 * flags not cleared here because we're still doing
60107c478bd9Sstevel@tonic-gate 		 * copy in/out for ioctl.
60117c478bd9Sstevel@tonic-gate 		 */
60127c478bd9Sstevel@tonic-gate 		stp->sd_iocblk = NULL;
60137c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
60147c478bd9Sstevel@tonic-gate 	}
60157c478bd9Sstevel@tonic-gate 
60167c478bd9Sstevel@tonic-gate 
60177c478bd9Sstevel@tonic-gate 	/*
60187c478bd9Sstevel@tonic-gate 	 * Have received acknowledgment.
60197c478bd9Sstevel@tonic-gate 	 */
60207c478bd9Sstevel@tonic-gate 
60217c478bd9Sstevel@tonic-gate 	switch (bp->b_datap->db_type) {
60227c478bd9Sstevel@tonic-gate 	case M_IOCACK:
60237c478bd9Sstevel@tonic-gate 		/*
60247c478bd9Sstevel@tonic-gate 		 * Positive ack.
60257c478bd9Sstevel@tonic-gate 		 */
60267c478bd9Sstevel@tonic-gate 		iocbp = (struct iocblk *)bp->b_rptr;
60277c478bd9Sstevel@tonic-gate 
60287c478bd9Sstevel@tonic-gate 		/*
60297c478bd9Sstevel@tonic-gate 		 * Set error if indicated.
60307c478bd9Sstevel@tonic-gate 		 */
60317c478bd9Sstevel@tonic-gate 		if (iocbp->ioc_error) {
60327c478bd9Sstevel@tonic-gate 			error = iocbp->ioc_error;
60337c478bd9Sstevel@tonic-gate 			break;
60347c478bd9Sstevel@tonic-gate 		}
60357c478bd9Sstevel@tonic-gate 
60367c478bd9Sstevel@tonic-gate 		/*
60377c478bd9Sstevel@tonic-gate 		 * Set return value.
60387c478bd9Sstevel@tonic-gate 		 */
60397c478bd9Sstevel@tonic-gate 		*rvalp = iocbp->ioc_rval;
60407c478bd9Sstevel@tonic-gate 
60417c478bd9Sstevel@tonic-gate 		/*
60427c478bd9Sstevel@tonic-gate 		 * Data may have been returned in ACK message (ioc_count > 0).
60437c478bd9Sstevel@tonic-gate 		 * If so, copy it out to the user's buffer.
60447c478bd9Sstevel@tonic-gate 		 */
60457c478bd9Sstevel@tonic-gate 		if (iocbp->ioc_count && !transparent) {
60467c478bd9Sstevel@tonic-gate 			if (error = getiocd(bp, strioc->ic_dp, copyflag))
60477c478bd9Sstevel@tonic-gate 				break;
60487c478bd9Sstevel@tonic-gate 		}
60497c478bd9Sstevel@tonic-gate 		if (!transparent) {
60507c478bd9Sstevel@tonic-gate 			if (len)	/* an M_COPYOUT was used with I_STR */
60517c478bd9Sstevel@tonic-gate 				strioc->ic_len = len;
60527c478bd9Sstevel@tonic-gate 			else
60537c478bd9Sstevel@tonic-gate 				strioc->ic_len = (int)iocbp->ioc_count;
60547c478bd9Sstevel@tonic-gate 		}
60557c478bd9Sstevel@tonic-gate 		break;
60567c478bd9Sstevel@tonic-gate 
60577c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
60587c478bd9Sstevel@tonic-gate 		/*
60597c478bd9Sstevel@tonic-gate 		 * Negative ack.
60607c478bd9Sstevel@tonic-gate 		 *
60617c478bd9Sstevel@tonic-gate 		 * The only thing to do is set error as specified
60627c478bd9Sstevel@tonic-gate 		 * in neg ack packet.
60637c478bd9Sstevel@tonic-gate 		 */
60647c478bd9Sstevel@tonic-gate 		iocbp = (struct iocblk *)bp->b_rptr;
60657c478bd9Sstevel@tonic-gate 
60667c478bd9Sstevel@tonic-gate 		error = (iocbp->ioc_error ? iocbp->ioc_error : EINVAL);
60677c478bd9Sstevel@tonic-gate 		break;
60687c478bd9Sstevel@tonic-gate 
60697c478bd9Sstevel@tonic-gate 	case M_COPYIN:
60707c478bd9Sstevel@tonic-gate 		/*
60717c478bd9Sstevel@tonic-gate 		 * Driver or module has requested user ioctl data.
60727c478bd9Sstevel@tonic-gate 		 */
60737c478bd9Sstevel@tonic-gate 		reqp = (struct copyreq *)bp->b_rptr;
60747c478bd9Sstevel@tonic-gate 
60757c478bd9Sstevel@tonic-gate 		/*
60767c478bd9Sstevel@tonic-gate 		 * M_COPYIN should *never* have a message attached, though
60777c478bd9Sstevel@tonic-gate 		 * it's harmless if it does -- thus, panic on a DEBUG
60787c478bd9Sstevel@tonic-gate 		 * kernel and just free it on a non-DEBUG build.
60797c478bd9Sstevel@tonic-gate 		 */
60807c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_cont == NULL);
60817c478bd9Sstevel@tonic-gate 		if (bp->b_cont != NULL) {
60827c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
60837c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
60847c478bd9Sstevel@tonic-gate 		}
60857c478bd9Sstevel@tonic-gate 
60867c478bd9Sstevel@tonic-gate 		error = putiocd(bp, reqp->cq_addr, flag, crp);
60877c478bd9Sstevel@tonic-gate 		if (error && bp->b_cont) {
60887c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
60897c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
60907c478bd9Sstevel@tonic-gate 		}
60917c478bd9Sstevel@tonic-gate 
60927c478bd9Sstevel@tonic-gate 		bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
60937c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCDATA;
60947c478bd9Sstevel@tonic-gate 
60957c478bd9Sstevel@tonic-gate 		mblk_setcred(bp, crp);
60967c478bd9Sstevel@tonic-gate 		DB_CPID(bp) = curproc->p_pid;
60977c478bd9Sstevel@tonic-gate 		resp = (struct copyresp *)bp->b_rptr;
60987c478bd9Sstevel@tonic-gate 		resp->cp_rval = (caddr_t)(uintptr_t)error;
60997c478bd9Sstevel@tonic-gate 		resp->cp_flag = (fflags & FMODELS);
61007c478bd9Sstevel@tonic-gate 
61017c478bd9Sstevel@tonic-gate 		stream_willservice(stp);
61027c478bd9Sstevel@tonic-gate 		putnext(stp->sd_wrq, bp);
61037c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
61047c478bd9Sstevel@tonic-gate 
61057c478bd9Sstevel@tonic-gate 		if (error) {
61067c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
61077c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~waitflags;
61087c478bd9Sstevel@tonic-gate 			cv_broadcast(&stp->sd_iocmonitor);
61097c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
61107c478bd9Sstevel@tonic-gate 			crfree(crp);
61117c478bd9Sstevel@tonic-gate 			return (error);
61127c478bd9Sstevel@tonic-gate 		}
61137c478bd9Sstevel@tonic-gate 
61147c478bd9Sstevel@tonic-gate 		goto waitioc;
61157c478bd9Sstevel@tonic-gate 
61167c478bd9Sstevel@tonic-gate 	case M_COPYOUT:
61177c478bd9Sstevel@tonic-gate 		/*
61187c478bd9Sstevel@tonic-gate 		 * Driver or module has ioctl data for a user.
61197c478bd9Sstevel@tonic-gate 		 */
61207c478bd9Sstevel@tonic-gate 		reqp = (struct copyreq *)bp->b_rptr;
61217c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_cont != NULL);
61227c478bd9Sstevel@tonic-gate 
61237c478bd9Sstevel@tonic-gate 		/*
61247c478bd9Sstevel@tonic-gate 		 * Always (transparent or non-transparent )
61257c478bd9Sstevel@tonic-gate 		 * use the address specified in the request
61267c478bd9Sstevel@tonic-gate 		 */
61277c478bd9Sstevel@tonic-gate 		taddr = reqp->cq_addr;
61287c478bd9Sstevel@tonic-gate 		if (!transparent)
61297c478bd9Sstevel@tonic-gate 			len = (int)reqp->cq_size;
61307c478bd9Sstevel@tonic-gate 
61317c478bd9Sstevel@tonic-gate 		/* copyout data to the provided address */
61327c478bd9Sstevel@tonic-gate 		error = getiocd(bp, taddr, copyflag);
61337c478bd9Sstevel@tonic-gate 
61347c478bd9Sstevel@tonic-gate 		freemsg(bp->b_cont);
61357c478bd9Sstevel@tonic-gate 		bp->b_cont = NULL;
61367c478bd9Sstevel@tonic-gate 
61377c478bd9Sstevel@tonic-gate 		bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
61387c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = M_IOCDATA;
61397c478bd9Sstevel@tonic-gate 
61407c478bd9Sstevel@tonic-gate 		mblk_setcred(bp, crp);
61417c478bd9Sstevel@tonic-gate 		DB_CPID(bp) = curproc->p_pid;
61427c478bd9Sstevel@tonic-gate 		resp = (struct copyresp *)bp->b_rptr;
61437c478bd9Sstevel@tonic-gate 		resp->cp_rval = (caddr_t)(uintptr_t)error;
61447c478bd9Sstevel@tonic-gate 		resp->cp_flag = (fflags & FMODELS);
61457c478bd9Sstevel@tonic-gate 
61467c478bd9Sstevel@tonic-gate 		stream_willservice(stp);
61477c478bd9Sstevel@tonic-gate 		putnext(stp->sd_wrq, bp);
61487c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
61497c478bd9Sstevel@tonic-gate 
61507c478bd9Sstevel@tonic-gate 		if (error) {
61517c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
61527c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~waitflags;
61537c478bd9Sstevel@tonic-gate 			cv_broadcast(&stp->sd_iocmonitor);
61547c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
61557c478bd9Sstevel@tonic-gate 			crfree(crp);
61567c478bd9Sstevel@tonic-gate 			return (error);
61577c478bd9Sstevel@tonic-gate 		}
61587c478bd9Sstevel@tonic-gate 		goto waitioc;
61597c478bd9Sstevel@tonic-gate 
61607c478bd9Sstevel@tonic-gate 	default:
61617c478bd9Sstevel@tonic-gate 		ASSERT(0);
61627c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
61637c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~waitflags;
61647c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_iocmonitor);
61657c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
61667c478bd9Sstevel@tonic-gate 		break;
61677c478bd9Sstevel@tonic-gate 	}
61687c478bd9Sstevel@tonic-gate 
61697c478bd9Sstevel@tonic-gate 	freemsg(bp);
61707c478bd9Sstevel@tonic-gate 	crfree(crp);
61717c478bd9Sstevel@tonic-gate 	return (error);
61727c478bd9Sstevel@tonic-gate }
61737c478bd9Sstevel@tonic-gate 
61747c478bd9Sstevel@tonic-gate /*
61757c478bd9Sstevel@tonic-gate  * For the SunOS keyboard driver.
61767c478bd9Sstevel@tonic-gate  * Return the next available "ioctl" sequence number.
61777c478bd9Sstevel@tonic-gate  * Exported, so that streams modules can send "ioctl" messages
61787c478bd9Sstevel@tonic-gate  * downstream from their open routine.
61797c478bd9Sstevel@tonic-gate  */
61807c478bd9Sstevel@tonic-gate int
61817c478bd9Sstevel@tonic-gate getiocseqno(void)
61827c478bd9Sstevel@tonic-gate {
61837c478bd9Sstevel@tonic-gate 	int	i;
61847c478bd9Sstevel@tonic-gate 
61857c478bd9Sstevel@tonic-gate 	mutex_enter(&strresources);
61867c478bd9Sstevel@tonic-gate 	i = ++ioc_id;
61877c478bd9Sstevel@tonic-gate 	mutex_exit(&strresources);
61887c478bd9Sstevel@tonic-gate 	return (i);
61897c478bd9Sstevel@tonic-gate }
61907c478bd9Sstevel@tonic-gate 
61917c478bd9Sstevel@tonic-gate /*
61927c478bd9Sstevel@tonic-gate  * Get the next message from the read queue.  If the message is
61937c478bd9Sstevel@tonic-gate  * priority, STRPRI will have been set by strrput().  This flag
61947c478bd9Sstevel@tonic-gate  * should be reset only when the entire message at the front of the
61957c478bd9Sstevel@tonic-gate  * queue as been consumed.
61967c478bd9Sstevel@tonic-gate  *
61977c478bd9Sstevel@tonic-gate  * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
61987c478bd9Sstevel@tonic-gate  */
61997c478bd9Sstevel@tonic-gate int
62007c478bd9Sstevel@tonic-gate strgetmsg(
62017c478bd9Sstevel@tonic-gate 	struct vnode *vp,
62027c478bd9Sstevel@tonic-gate 	struct strbuf *mctl,
62037c478bd9Sstevel@tonic-gate 	struct strbuf *mdata,
62047c478bd9Sstevel@tonic-gate 	unsigned char *prip,
62057c478bd9Sstevel@tonic-gate 	int *flagsp,
62067c478bd9Sstevel@tonic-gate 	int fmode,
62077c478bd9Sstevel@tonic-gate 	rval_t *rvp)
62087c478bd9Sstevel@tonic-gate {
62097c478bd9Sstevel@tonic-gate 	struct stdata *stp;
62107c478bd9Sstevel@tonic-gate 	mblk_t *bp, *nbp;
62117c478bd9Sstevel@tonic-gate 	mblk_t *savemp = NULL;
62127c478bd9Sstevel@tonic-gate 	mblk_t *savemptail = NULL;
62137c478bd9Sstevel@tonic-gate 	uint_t old_sd_flag;
62147c478bd9Sstevel@tonic-gate 	int flg;
62157c478bd9Sstevel@tonic-gate 	int more = 0;
62167c478bd9Sstevel@tonic-gate 	int error = 0;
62177c478bd9Sstevel@tonic-gate 	char first = 1;
62187c478bd9Sstevel@tonic-gate 	uint_t mark;		/* Contains MSG*MARK and _LASTMARK */
62197c478bd9Sstevel@tonic-gate #define	_LASTMARK	0x8000	/* Distinct from MSG*MARK */
62207c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
62217c478bd9Sstevel@tonic-gate 	queue_t *q;
62227c478bd9Sstevel@tonic-gate 	int	pr = 0;			/* Partial read successful */
62237c478bd9Sstevel@tonic-gate 	struct uio uios;
62247c478bd9Sstevel@tonic-gate 	struct uio *uiop = &uios;
62257c478bd9Sstevel@tonic-gate 	struct iovec iovs;
62267c478bd9Sstevel@tonic-gate 	unsigned char type;
62277c478bd9Sstevel@tonic-gate 
62287c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_STRGETMSG_ENTER,
62297c478bd9Sstevel@tonic-gate 		"strgetmsg:%p", vp);
62307c478bd9Sstevel@tonic-gate 
62317c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
62327c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
62337c478bd9Sstevel@tonic-gate 	rvp->r_val1 = 0;
62347c478bd9Sstevel@tonic-gate 
62357c478bd9Sstevel@tonic-gate 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
62367c478bd9Sstevel@tonic-gate 		if (error = straccess(stp, JCREAD))
62377c478bd9Sstevel@tonic-gate 			return (error);
62387c478bd9Sstevel@tonic-gate 
62397c478bd9Sstevel@tonic-gate 	/* Fast check of flags before acquiring the lock */
62407c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STRDERR|STPLEX)) {
62417c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
62427c478bd9Sstevel@tonic-gate 		error = strgeterr(stp, STRDERR|STPLEX, 0);
62437c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
62447c478bd9Sstevel@tonic-gate 		if (error != 0)
62457c478bd9Sstevel@tonic-gate 			return (error);
62467c478bd9Sstevel@tonic-gate 	}
62477c478bd9Sstevel@tonic-gate 
62487c478bd9Sstevel@tonic-gate 	switch (*flagsp) {
62497c478bd9Sstevel@tonic-gate 	case MSG_HIPRI:
62507c478bd9Sstevel@tonic-gate 		if (*prip != 0)
62517c478bd9Sstevel@tonic-gate 			return (EINVAL);
62527c478bd9Sstevel@tonic-gate 		break;
62537c478bd9Sstevel@tonic-gate 
62547c478bd9Sstevel@tonic-gate 	case MSG_ANY:
62557c478bd9Sstevel@tonic-gate 	case MSG_BAND:
62567c478bd9Sstevel@tonic-gate 		break;
62577c478bd9Sstevel@tonic-gate 
62587c478bd9Sstevel@tonic-gate 	default:
62597c478bd9Sstevel@tonic-gate 		return (EINVAL);
62607c478bd9Sstevel@tonic-gate 	}
62617c478bd9Sstevel@tonic-gate 	/*
62627c478bd9Sstevel@tonic-gate 	 * Setup uio and iov for data part
62637c478bd9Sstevel@tonic-gate 	 */
62647c478bd9Sstevel@tonic-gate 	iovs.iov_base = mdata->buf;
62657c478bd9Sstevel@tonic-gate 	iovs.iov_len = mdata->maxlen;
62667c478bd9Sstevel@tonic-gate 	uios.uio_iov = &iovs;
62677c478bd9Sstevel@tonic-gate 	uios.uio_iovcnt = 1;
62687c478bd9Sstevel@tonic-gate 	uios.uio_loffset = 0;
62697c478bd9Sstevel@tonic-gate 	uios.uio_segflg = UIO_USERSPACE;
62707c478bd9Sstevel@tonic-gate 	uios.uio_fmode = 0;
62717c478bd9Sstevel@tonic-gate 	uios.uio_extflg = UIO_COPY_CACHED;
62727c478bd9Sstevel@tonic-gate 	uios.uio_resid = mdata->maxlen;
62737c478bd9Sstevel@tonic-gate 	uios.uio_offset = 0;
62747c478bd9Sstevel@tonic-gate 
62757c478bd9Sstevel@tonic-gate 	q = _RD(stp->sd_wrq);
62767c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
62777c478bd9Sstevel@tonic-gate 	old_sd_flag = stp->sd_flag;
62787c478bd9Sstevel@tonic-gate 	mark = 0;
62797c478bd9Sstevel@tonic-gate 	for (;;) {
62807c478bd9Sstevel@tonic-gate 		int done = 0;
62817c478bd9Sstevel@tonic-gate 		mblk_t *q_first = q->q_first;
62827c478bd9Sstevel@tonic-gate 
62837c478bd9Sstevel@tonic-gate 		/*
62847c478bd9Sstevel@tonic-gate 		 * Get the next message of appropriate priority
62857c478bd9Sstevel@tonic-gate 		 * from the stream head.  If the caller is interested
62867c478bd9Sstevel@tonic-gate 		 * in band or hipri messages, then they should already
62877c478bd9Sstevel@tonic-gate 		 * be enqueued at the stream head.  On the other hand
62887c478bd9Sstevel@tonic-gate 		 * if the caller wants normal (band 0) messages, they
62897c478bd9Sstevel@tonic-gate 		 * might be deferred in a synchronous stream and they
62907c478bd9Sstevel@tonic-gate 		 * will need to be pulled up.
62917c478bd9Sstevel@tonic-gate 		 *
62927c478bd9Sstevel@tonic-gate 		 * After we have dequeued a message, we might find that
62937c478bd9Sstevel@tonic-gate 		 * it was a deferred M_SIG that was enqueued at the
62947c478bd9Sstevel@tonic-gate 		 * stream head.  It must now be posted as part of the
62957c478bd9Sstevel@tonic-gate 		 * read by calling strsignal_nolock().
62967c478bd9Sstevel@tonic-gate 		 *
62977c478bd9Sstevel@tonic-gate 		 * Also note that strrput does not enqueue an M_PCSIG,
62987c478bd9Sstevel@tonic-gate 		 * and there cannot be more than one hipri message,
62997c478bd9Sstevel@tonic-gate 		 * so there was no need to have the M_PCSIG case.
63007c478bd9Sstevel@tonic-gate 		 *
63017c478bd9Sstevel@tonic-gate 		 * At some time it might be nice to try and wrap the
63027c478bd9Sstevel@tonic-gate 		 * functionality of kstrgetmsg() and strgetmsg() into
63037c478bd9Sstevel@tonic-gate 		 * a common routine so to reduce the amount of replicated
63047c478bd9Sstevel@tonic-gate 		 * code (since they are extremely similar).
63057c478bd9Sstevel@tonic-gate 		 */
63067c478bd9Sstevel@tonic-gate 		if (!(*flagsp & (MSG_HIPRI|MSG_BAND))) {
63077c478bd9Sstevel@tonic-gate 			/* Asking for normal, band0 data */
63087c478bd9Sstevel@tonic-gate 			bp = strget(stp, q, uiop, first, &error);
63097c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(&stp->sd_lock));
63107c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
63117c478bd9Sstevel@tonic-gate 				if (bp->b_datap->db_type == M_SIG) {
63127c478bd9Sstevel@tonic-gate 					strsignal_nolock(stp, *bp->b_rptr,
63137c478bd9Sstevel@tonic-gate 					    (int32_t)bp->b_band);
63147c478bd9Sstevel@tonic-gate 					continue;
63157c478bd9Sstevel@tonic-gate 				} else {
63167c478bd9Sstevel@tonic-gate 					break;
63177c478bd9Sstevel@tonic-gate 				}
63187c478bd9Sstevel@tonic-gate 			}
63197c478bd9Sstevel@tonic-gate 			if (error != 0) {
63207c478bd9Sstevel@tonic-gate 				goto getmout;
63217c478bd9Sstevel@tonic-gate 			}
63227c478bd9Sstevel@tonic-gate 
63237c478bd9Sstevel@tonic-gate 		/*
63247c478bd9Sstevel@tonic-gate 		 * We can't depend on the value of STRPRI here because
63257c478bd9Sstevel@tonic-gate 		 * the stream head may be in transit. Therefore, we
63267c478bd9Sstevel@tonic-gate 		 * must look at the type of the first message to
63277c478bd9Sstevel@tonic-gate 		 * determine if a high priority messages is waiting
63287c478bd9Sstevel@tonic-gate 		 */
63297c478bd9Sstevel@tonic-gate 		} else if ((*flagsp & MSG_HIPRI) && q_first != NULL &&
63307c478bd9Sstevel@tonic-gate 			    q_first->b_datap->db_type >= QPCTL &&
63317c478bd9Sstevel@tonic-gate 			    (bp = getq_noenab(q)) != NULL) {
63327c478bd9Sstevel@tonic-gate 			/* Asked for HIPRI and got one */
63337c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_datap->db_type >= QPCTL);
63347c478bd9Sstevel@tonic-gate 			break;
63357c478bd9Sstevel@tonic-gate 		} else if ((*flagsp & MSG_BAND) && q_first != NULL &&
63367c478bd9Sstevel@tonic-gate 			    ((q_first->b_band >= *prip) ||
63377c478bd9Sstevel@tonic-gate 			    q_first->b_datap->db_type >= QPCTL) &&
63387c478bd9Sstevel@tonic-gate 			    (bp = getq_noenab(q)) != NULL) {
63397c478bd9Sstevel@tonic-gate 			/*
63407c478bd9Sstevel@tonic-gate 			 * Asked for at least band "prip" and got either at
63417c478bd9Sstevel@tonic-gate 			 * least that band or a hipri message.
63427c478bd9Sstevel@tonic-gate 			 */
63437c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_band >= *prip ||
63447c478bd9Sstevel@tonic-gate 				bp->b_datap->db_type >= QPCTL);
63457c478bd9Sstevel@tonic-gate 			if (bp->b_datap->db_type == M_SIG) {
63467c478bd9Sstevel@tonic-gate 				strsignal_nolock(stp, *bp->b_rptr,
63477c478bd9Sstevel@tonic-gate 				    (int32_t)bp->b_band);
63487c478bd9Sstevel@tonic-gate 				continue;
63497c478bd9Sstevel@tonic-gate 			} else {
63507c478bd9Sstevel@tonic-gate 				break;
63517c478bd9Sstevel@tonic-gate 			}
63527c478bd9Sstevel@tonic-gate 		}
63537c478bd9Sstevel@tonic-gate 
63547c478bd9Sstevel@tonic-gate 		/* No data. Time to sleep? */
63557c478bd9Sstevel@tonic-gate 		qbackenable(q, 0);
63567c478bd9Sstevel@tonic-gate 
63577c478bd9Sstevel@tonic-gate 		/*
63587c478bd9Sstevel@tonic-gate 		 * If STRHUP or STREOF, return 0 length control and data.
63597c478bd9Sstevel@tonic-gate 		 * If resid is 0, then a read(fd,buf,0) was done. Do not
63607c478bd9Sstevel@tonic-gate 		 * sleep to satisfy this request because by default we have
63617c478bd9Sstevel@tonic-gate 		 * zero bytes to return.
63627c478bd9Sstevel@tonic-gate 		 */
63637c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & (STRHUP|STREOF)) || (mctl->maxlen == 0 &&
63647c478bd9Sstevel@tonic-gate 		    mdata->maxlen == 0)) {
63657c478bd9Sstevel@tonic-gate 			mctl->len = mdata->len = 0;
63667c478bd9Sstevel@tonic-gate 			*flagsp = 0;
63677c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
63687c478bd9Sstevel@tonic-gate 			return (0);
63697c478bd9Sstevel@tonic-gate 		}
63707c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_WAIT,
63717c478bd9Sstevel@tonic-gate 			"strgetmsg calls strwaitq:%p, %p",
63727c478bd9Sstevel@tonic-gate 			vp, uiop);
63737c478bd9Sstevel@tonic-gate 		if (((error = strwaitq(stp, GETWAIT, (ssize_t)0, fmode, -1,
63747c478bd9Sstevel@tonic-gate 		    &done)) != 0) || done) {
63757c478bd9Sstevel@tonic-gate 			TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_DONE,
63767c478bd9Sstevel@tonic-gate 				"strgetmsg error or done:%p, %p",
63777c478bd9Sstevel@tonic-gate 				vp, uiop);
63787c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
63797c478bd9Sstevel@tonic-gate 			return (error);
63807c478bd9Sstevel@tonic-gate 		}
63817c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_AWAKE,
63827c478bd9Sstevel@tonic-gate 			"strgetmsg awakes:%p, %p", vp, uiop);
63837c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO) {
63847c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
63857c478bd9Sstevel@tonic-gate 			if (error = straccess(stp, JCREAD))
63867c478bd9Sstevel@tonic-gate 				return (error);
63877c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
63887c478bd9Sstevel@tonic-gate 		}
63897c478bd9Sstevel@tonic-gate 		first = 0;
63907c478bd9Sstevel@tonic-gate 	}
63917c478bd9Sstevel@tonic-gate 	ASSERT(bp != NULL);
63927c478bd9Sstevel@tonic-gate 	/*
63937c478bd9Sstevel@tonic-gate 	 * Extract any mark information. If the message is not completely
63947c478bd9Sstevel@tonic-gate 	 * consumed this information will be put in the mblk
63957c478bd9Sstevel@tonic-gate 	 * that is putback.
63967c478bd9Sstevel@tonic-gate 	 * If MSGMARKNEXT is set and the message is completely consumed
63977c478bd9Sstevel@tonic-gate 	 * the STRATMARK flag will be set below. Likewise, if
63987c478bd9Sstevel@tonic-gate 	 * MSGNOTMARKNEXT is set and the message is
63997c478bd9Sstevel@tonic-gate 	 * completely consumed STRNOTATMARK will be set.
64007c478bd9Sstevel@tonic-gate 	 */
64017c478bd9Sstevel@tonic-gate 	mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
64027c478bd9Sstevel@tonic-gate 	ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
64037c478bd9Sstevel@tonic-gate 		(MSGMARKNEXT|MSGNOTMARKNEXT));
64047c478bd9Sstevel@tonic-gate 	if (mark != 0 && bp == stp->sd_mark) {
64057c478bd9Sstevel@tonic-gate 		mark |= _LASTMARK;
64067c478bd9Sstevel@tonic-gate 		stp->sd_mark = NULL;
64077c478bd9Sstevel@tonic-gate 	}
64087c478bd9Sstevel@tonic-gate 	/*
64097c478bd9Sstevel@tonic-gate 	 * keep track of the original message type and priority
64107c478bd9Sstevel@tonic-gate 	 */
64117c478bd9Sstevel@tonic-gate 	pri = bp->b_band;
64127c478bd9Sstevel@tonic-gate 	type = bp->b_datap->db_type;
64137c478bd9Sstevel@tonic-gate 	if (type == M_PASSFP) {
64147c478bd9Sstevel@tonic-gate 		if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
64157c478bd9Sstevel@tonic-gate 			stp->sd_mark = bp;
64167c478bd9Sstevel@tonic-gate 		bp->b_flag |= mark & ~_LASTMARK;
64177c478bd9Sstevel@tonic-gate 		putback(stp, q, bp, pri);
64187c478bd9Sstevel@tonic-gate 		qbackenable(q, pri);
64197c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
64207c478bd9Sstevel@tonic-gate 		return (EBADMSG);
64217c478bd9Sstevel@tonic-gate 	}
64227c478bd9Sstevel@tonic-gate 	ASSERT(type != M_SIG);
64237c478bd9Sstevel@tonic-gate 
64247c478bd9Sstevel@tonic-gate 	/*
64257c478bd9Sstevel@tonic-gate 	 * Set this flag so strrput will not generate signals. Need to
64267c478bd9Sstevel@tonic-gate 	 * make sure this flag is cleared before leaving this routine
64277c478bd9Sstevel@tonic-gate 	 * else signals will stop being sent.
64287c478bd9Sstevel@tonic-gate 	 */
64297c478bd9Sstevel@tonic-gate 	stp->sd_flag |= STRGETINPROG;
64307c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
64317c478bd9Sstevel@tonic-gate 
64327c478bd9Sstevel@tonic-gate 	if (STREAM_NEEDSERVICE(stp))
64337c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
64347c478bd9Sstevel@tonic-gate 
64357c478bd9Sstevel@tonic-gate 	/*
64367c478bd9Sstevel@tonic-gate 	 * Set HIPRI flag if message is priority.
64377c478bd9Sstevel@tonic-gate 	 */
64387c478bd9Sstevel@tonic-gate 	if (type >= QPCTL)
64397c478bd9Sstevel@tonic-gate 		flg = MSG_HIPRI;
64407c478bd9Sstevel@tonic-gate 	else
64417c478bd9Sstevel@tonic-gate 		flg = MSG_BAND;
64427c478bd9Sstevel@tonic-gate 
64437c478bd9Sstevel@tonic-gate 	/*
64447c478bd9Sstevel@tonic-gate 	 * First process PROTO or PCPROTO blocks, if any.
64457c478bd9Sstevel@tonic-gate 	 */
64467c478bd9Sstevel@tonic-gate 	if (mctl->maxlen >= 0 && type != M_DATA) {
64477c478bd9Sstevel@tonic-gate 		size_t	n, bcnt;
64487c478bd9Sstevel@tonic-gate 		char	*ubuf;
64497c478bd9Sstevel@tonic-gate 
64507c478bd9Sstevel@tonic-gate 		bcnt = mctl->maxlen;
64517c478bd9Sstevel@tonic-gate 		ubuf = mctl->buf;
64527c478bd9Sstevel@tonic-gate 		while (bp != NULL && bp->b_datap->db_type != M_DATA) {
64537c478bd9Sstevel@tonic-gate 			if ((n = MIN(bcnt, bp->b_wptr - bp->b_rptr)) != 0 &&
64547c478bd9Sstevel@tonic-gate 			    copyout(bp->b_rptr, ubuf, n)) {
64557c478bd9Sstevel@tonic-gate 				error = EFAULT;
64567c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
64577c478bd9Sstevel@tonic-gate 				/*
64587c478bd9Sstevel@tonic-gate 				 * clear stream head pri flag based on
64597c478bd9Sstevel@tonic-gate 				 * first message type
64607c478bd9Sstevel@tonic-gate 				 */
64617c478bd9Sstevel@tonic-gate 				if (type >= QPCTL) {
64627c478bd9Sstevel@tonic-gate 					ASSERT(type == M_PCPROTO);
64637c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
64647c478bd9Sstevel@tonic-gate 				}
64657c478bd9Sstevel@tonic-gate 				more = 0;
64667c478bd9Sstevel@tonic-gate 				freemsg(bp);
64677c478bd9Sstevel@tonic-gate 				goto getmout;
64687c478bd9Sstevel@tonic-gate 			}
64697c478bd9Sstevel@tonic-gate 			ubuf += n;
64707c478bd9Sstevel@tonic-gate 			bp->b_rptr += n;
64717c478bd9Sstevel@tonic-gate 			if (bp->b_rptr >= bp->b_wptr) {
64727c478bd9Sstevel@tonic-gate 				nbp = bp;
64737c478bd9Sstevel@tonic-gate 				bp = bp->b_cont;
64747c478bd9Sstevel@tonic-gate 				freeb(nbp);
64757c478bd9Sstevel@tonic-gate 			}
64767c478bd9Sstevel@tonic-gate 			ASSERT(n <= bcnt);
64777c478bd9Sstevel@tonic-gate 			bcnt -= n;
64787c478bd9Sstevel@tonic-gate 			if (bcnt == 0)
64797c478bd9Sstevel@tonic-gate 				break;
64807c478bd9Sstevel@tonic-gate 		}
64817c478bd9Sstevel@tonic-gate 		mctl->len = mctl->maxlen - bcnt;
64827c478bd9Sstevel@tonic-gate 	} else
64837c478bd9Sstevel@tonic-gate 		mctl->len = -1;
64847c478bd9Sstevel@tonic-gate 
64857c478bd9Sstevel@tonic-gate 	if (bp && bp->b_datap->db_type != M_DATA) {
64867c478bd9Sstevel@tonic-gate 		/*
64877c478bd9Sstevel@tonic-gate 		 * More PROTO blocks in msg.
64887c478bd9Sstevel@tonic-gate 		 */
64897c478bd9Sstevel@tonic-gate 		more |= MORECTL;
64907c478bd9Sstevel@tonic-gate 		savemp = bp;
64917c478bd9Sstevel@tonic-gate 		while (bp && bp->b_datap->db_type != M_DATA) {
64927c478bd9Sstevel@tonic-gate 			savemptail = bp;
64937c478bd9Sstevel@tonic-gate 			bp = bp->b_cont;
64947c478bd9Sstevel@tonic-gate 		}
64957c478bd9Sstevel@tonic-gate 		savemptail->b_cont = NULL;
64967c478bd9Sstevel@tonic-gate 	}
64977c478bd9Sstevel@tonic-gate 
64987c478bd9Sstevel@tonic-gate 	/*
64997c478bd9Sstevel@tonic-gate 	 * Now process DATA blocks, if any.
65007c478bd9Sstevel@tonic-gate 	 */
65017c478bd9Sstevel@tonic-gate 	if (mdata->maxlen >= 0 && bp) {
65027c478bd9Sstevel@tonic-gate 		/*
65037c478bd9Sstevel@tonic-gate 		 * struiocopyout will consume a potential zero-length
65047c478bd9Sstevel@tonic-gate 		 * M_DATA even if uio_resid is zero.
65057c478bd9Sstevel@tonic-gate 		 */
65067c478bd9Sstevel@tonic-gate 		size_t oldresid = uiop->uio_resid;
65077c478bd9Sstevel@tonic-gate 
65087c478bd9Sstevel@tonic-gate 		bp = struiocopyout(bp, uiop, &error);
65097c478bd9Sstevel@tonic-gate 		if (error != 0) {
65107c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
65117c478bd9Sstevel@tonic-gate 			/*
65127c478bd9Sstevel@tonic-gate 			 * clear stream head hi pri flag based on
65137c478bd9Sstevel@tonic-gate 			 * first message
65147c478bd9Sstevel@tonic-gate 			 */
65157c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
65167c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
65177c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
65187c478bd9Sstevel@tonic-gate 			}
65197c478bd9Sstevel@tonic-gate 			more = 0;
65207c478bd9Sstevel@tonic-gate 			freemsg(savemp);
65217c478bd9Sstevel@tonic-gate 			goto getmout;
65227c478bd9Sstevel@tonic-gate 		}
65237c478bd9Sstevel@tonic-gate 		/*
65247c478bd9Sstevel@tonic-gate 		 * (pr == 1) indicates a partial read.
65257c478bd9Sstevel@tonic-gate 		 */
65267c478bd9Sstevel@tonic-gate 		if (oldresid > uiop->uio_resid)
65277c478bd9Sstevel@tonic-gate 			pr = 1;
65287c478bd9Sstevel@tonic-gate 		mdata->len = mdata->maxlen - uiop->uio_resid;
65297c478bd9Sstevel@tonic-gate 	} else
65307c478bd9Sstevel@tonic-gate 		mdata->len = -1;
65317c478bd9Sstevel@tonic-gate 
65327c478bd9Sstevel@tonic-gate 	if (bp) {			/* more data blocks in msg */
65337c478bd9Sstevel@tonic-gate 		more |= MOREDATA;
65347c478bd9Sstevel@tonic-gate 		if (savemp)
65357c478bd9Sstevel@tonic-gate 			savemptail->b_cont = bp;
65367c478bd9Sstevel@tonic-gate 		else
65377c478bd9Sstevel@tonic-gate 			savemp = bp;
65387c478bd9Sstevel@tonic-gate 	}
65397c478bd9Sstevel@tonic-gate 
65407c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
65417c478bd9Sstevel@tonic-gate 	if (savemp) {
65427c478bd9Sstevel@tonic-gate 		if (pr && (savemp->b_datap->db_type == M_DATA) &&
65437c478bd9Sstevel@tonic-gate 		    msgnodata(savemp)) {
65447c478bd9Sstevel@tonic-gate 			/*
65457c478bd9Sstevel@tonic-gate 			 * Avoid queuing a zero-length tail part of
65467c478bd9Sstevel@tonic-gate 			 * a message. pr=1 indicates that we read some of
65477c478bd9Sstevel@tonic-gate 			 * the message.
65487c478bd9Sstevel@tonic-gate 			 */
65497c478bd9Sstevel@tonic-gate 			freemsg(savemp);
65507c478bd9Sstevel@tonic-gate 			more &= ~MOREDATA;
65517c478bd9Sstevel@tonic-gate 			/*
65527c478bd9Sstevel@tonic-gate 			 * clear stream head hi pri flag based on
65537c478bd9Sstevel@tonic-gate 			 * first message
65547c478bd9Sstevel@tonic-gate 			 */
65557c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
65567c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
65577c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
65587c478bd9Sstevel@tonic-gate 			}
65597c478bd9Sstevel@tonic-gate 		} else {
65607c478bd9Sstevel@tonic-gate 			savemp->b_band = pri;
65617c478bd9Sstevel@tonic-gate 			/*
65627c478bd9Sstevel@tonic-gate 			 * If the first message was HIPRI and the one we're
65637c478bd9Sstevel@tonic-gate 			 * putting back isn't, then clear STRPRI, otherwise
65647c478bd9Sstevel@tonic-gate 			 * set STRPRI again.  Note that we must set STRPRI
65657c478bd9Sstevel@tonic-gate 			 * again since the flush logic in strrput_nondata()
65667c478bd9Sstevel@tonic-gate 			 * may have cleared it while we had sd_lock dropped.
65677c478bd9Sstevel@tonic-gate 			 */
65687c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
65697c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
65707c478bd9Sstevel@tonic-gate 				if (queclass(savemp) < QPCTL)
65717c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
65727c478bd9Sstevel@tonic-gate 				else
65737c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRPRI;
65747c478bd9Sstevel@tonic-gate 			} else if (queclass(savemp) >= QPCTL) {
65757c478bd9Sstevel@tonic-gate 				/*
65767c478bd9Sstevel@tonic-gate 				 * The first message was not a HIPRI message,
65777c478bd9Sstevel@tonic-gate 				 * but the one we are about to putback is.
65787c478bd9Sstevel@tonic-gate 				 * For simplicitly, we do not allow for HIPRI
65797c478bd9Sstevel@tonic-gate 				 * messages to be embedded in the message
65807c478bd9Sstevel@tonic-gate 				 * body, so just force it to same type as
65817c478bd9Sstevel@tonic-gate 				 * first message.
65827c478bd9Sstevel@tonic-gate 				 */
65837c478bd9Sstevel@tonic-gate 				ASSERT(type == M_DATA || type == M_PROTO);
65847c478bd9Sstevel@tonic-gate 				ASSERT(savemp->b_datap->db_type == M_PCPROTO);
65857c478bd9Sstevel@tonic-gate 				savemp->b_datap->db_type = type;
65867c478bd9Sstevel@tonic-gate 			}
65877c478bd9Sstevel@tonic-gate 			if (mark != 0) {
65887c478bd9Sstevel@tonic-gate 				savemp->b_flag |= mark & ~_LASTMARK;
65897c478bd9Sstevel@tonic-gate 				if ((mark & _LASTMARK) &&
65907c478bd9Sstevel@tonic-gate 				    (stp->sd_mark == NULL)) {
65917c478bd9Sstevel@tonic-gate 					/*
65927c478bd9Sstevel@tonic-gate 					 * If another marked message arrived
65937c478bd9Sstevel@tonic-gate 					 * while sd_lock was not held sd_mark
65947c478bd9Sstevel@tonic-gate 					 * would be non-NULL.
65957c478bd9Sstevel@tonic-gate 					 */
65967c478bd9Sstevel@tonic-gate 					stp->sd_mark = savemp;
65977c478bd9Sstevel@tonic-gate 				}
65987c478bd9Sstevel@tonic-gate 			}
65997c478bd9Sstevel@tonic-gate 			putback(stp, q, savemp, pri);
66007c478bd9Sstevel@tonic-gate 		}
66017c478bd9Sstevel@tonic-gate 	} else {
66027c478bd9Sstevel@tonic-gate 		/*
66037c478bd9Sstevel@tonic-gate 		 * The complete message was consumed.
66047c478bd9Sstevel@tonic-gate 		 *
66057c478bd9Sstevel@tonic-gate 		 * If another M_PCPROTO arrived while sd_lock was not held
66067c478bd9Sstevel@tonic-gate 		 * it would have been discarded since STRPRI was still set.
66077c478bd9Sstevel@tonic-gate 		 *
66087c478bd9Sstevel@tonic-gate 		 * Move the MSG*MARKNEXT information
66097c478bd9Sstevel@tonic-gate 		 * to the stream head just in case
66107c478bd9Sstevel@tonic-gate 		 * the read queue becomes empty.
66117c478bd9Sstevel@tonic-gate 		 * clear stream head hi pri flag based on
66127c478bd9Sstevel@tonic-gate 		 * first message
66137c478bd9Sstevel@tonic-gate 		 *
66147c478bd9Sstevel@tonic-gate 		 * If the stream head was at the mark
66157c478bd9Sstevel@tonic-gate 		 * (STRATMARK) before we dropped sd_lock above
66167c478bd9Sstevel@tonic-gate 		 * and some data was consumed then we have
66177c478bd9Sstevel@tonic-gate 		 * moved past the mark thus STRATMARK is
66187c478bd9Sstevel@tonic-gate 		 * cleared. However, if a message arrived in
66197c478bd9Sstevel@tonic-gate 		 * strrput during the copyout above causing
66207c478bd9Sstevel@tonic-gate 		 * STRATMARK to be set we can not clear that
66217c478bd9Sstevel@tonic-gate 		 * flag.
66227c478bd9Sstevel@tonic-gate 		 */
66237c478bd9Sstevel@tonic-gate 		if (type >= QPCTL) {
66247c478bd9Sstevel@tonic-gate 			ASSERT(type == M_PCPROTO);
66257c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRPRI;
66267c478bd9Sstevel@tonic-gate 		}
66277c478bd9Sstevel@tonic-gate 		if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
66287c478bd9Sstevel@tonic-gate 			if (mark & MSGMARKNEXT) {
66297c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRNOTATMARK;
66307c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRATMARK;
66317c478bd9Sstevel@tonic-gate 			} else if (mark & MSGNOTMARKNEXT) {
66327c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRATMARK;
66337c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRNOTATMARK;
66347c478bd9Sstevel@tonic-gate 			} else {
66357c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
66367c478bd9Sstevel@tonic-gate 			}
66377c478bd9Sstevel@tonic-gate 		} else if (pr && (old_sd_flag & STRATMARK)) {
66387c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRATMARK;
66397c478bd9Sstevel@tonic-gate 		}
66407c478bd9Sstevel@tonic-gate 	}
66417c478bd9Sstevel@tonic-gate 
66427c478bd9Sstevel@tonic-gate 	*flagsp = flg;
66437c478bd9Sstevel@tonic-gate 	*prip = pri;
66447c478bd9Sstevel@tonic-gate 
66457c478bd9Sstevel@tonic-gate 	/*
66467c478bd9Sstevel@tonic-gate 	 * Getmsg cleanup processing - if the state of the queue has changed
66477c478bd9Sstevel@tonic-gate 	 * some signals may need to be sent and/or poll awakened.
66487c478bd9Sstevel@tonic-gate 	 */
66497c478bd9Sstevel@tonic-gate getmout:
66507c478bd9Sstevel@tonic-gate 	qbackenable(q, pri);
66517c478bd9Sstevel@tonic-gate 
66527c478bd9Sstevel@tonic-gate 	/*
66537c478bd9Sstevel@tonic-gate 	 * We dropped the stream head lock above. Send all M_SIG messages
66547c478bd9Sstevel@tonic-gate 	 * before processing stream head for SIGPOLL messages.
66557c478bd9Sstevel@tonic-gate 	 */
66567c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
66577c478bd9Sstevel@tonic-gate 	while ((bp = q->q_first) != NULL &&
66587c478bd9Sstevel@tonic-gate 	    (bp->b_datap->db_type == M_SIG)) {
66597c478bd9Sstevel@tonic-gate 		/*
66607c478bd9Sstevel@tonic-gate 		 * sd_lock is held so the content of the read queue can not
66617c478bd9Sstevel@tonic-gate 		 * change.
66627c478bd9Sstevel@tonic-gate 		 */
66637c478bd9Sstevel@tonic-gate 		bp = getq(q);
66647c478bd9Sstevel@tonic-gate 		ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
66657c478bd9Sstevel@tonic-gate 
66667c478bd9Sstevel@tonic-gate 		strsignal_nolock(stp, *bp->b_rptr, (int32_t)bp->b_band);
66677c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
66687c478bd9Sstevel@tonic-gate 		freemsg(bp);
66697c478bd9Sstevel@tonic-gate 		if (STREAM_NEEDSERVICE(stp))
66707c478bd9Sstevel@tonic-gate 			stream_runservice(stp);
66717c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
66727c478bd9Sstevel@tonic-gate 	}
66737c478bd9Sstevel@tonic-gate 
66747c478bd9Sstevel@tonic-gate 	/*
66757c478bd9Sstevel@tonic-gate 	 * stream head cannot change while we make the determination
66767c478bd9Sstevel@tonic-gate 	 * whether or not to send a signal. Drop the flag to allow strrput
66777c478bd9Sstevel@tonic-gate 	 * to send firstmsgsigs again.
66787c478bd9Sstevel@tonic-gate 	 */
66797c478bd9Sstevel@tonic-gate 	stp->sd_flag &= ~STRGETINPROG;
66807c478bd9Sstevel@tonic-gate 
66817c478bd9Sstevel@tonic-gate 	/*
66827c478bd9Sstevel@tonic-gate 	 * If the type of message at the front of the queue changed
66837c478bd9Sstevel@tonic-gate 	 * due to the receive the appropriate signals and pollwakeup events
66847c478bd9Sstevel@tonic-gate 	 * are generated. The type of changes are:
66857c478bd9Sstevel@tonic-gate 	 *	Processed a hipri message, q_first is not hipri.
66867c478bd9Sstevel@tonic-gate 	 *	Processed a band X message, and q_first is band Y.
66877c478bd9Sstevel@tonic-gate 	 * The generated signals and pollwakeups are identical to what
66887c478bd9Sstevel@tonic-gate 	 * strrput() generates should the message that is now on q_first
66897c478bd9Sstevel@tonic-gate 	 * arrive to an empty read queue.
66907c478bd9Sstevel@tonic-gate 	 *
66917c478bd9Sstevel@tonic-gate 	 * Note: only strrput will send a signal for a hipri message.
66927c478bd9Sstevel@tonic-gate 	 */
66937c478bd9Sstevel@tonic-gate 	if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
66947c478bd9Sstevel@tonic-gate 		strsigset_t signals = 0;
66957c478bd9Sstevel@tonic-gate 		strpollset_t pollwakeups = 0;
66967c478bd9Sstevel@tonic-gate 
66977c478bd9Sstevel@tonic-gate 		if (flg & MSG_HIPRI) {
66987c478bd9Sstevel@tonic-gate 			/*
66997c478bd9Sstevel@tonic-gate 			 * Removed a hipri message. Regular data at
67007c478bd9Sstevel@tonic-gate 			 * the front of  the queue.
67017c478bd9Sstevel@tonic-gate 			 */
67027c478bd9Sstevel@tonic-gate 			if (bp->b_band == 0) {
67037c478bd9Sstevel@tonic-gate 				signals = S_INPUT | S_RDNORM;
67047c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDNORM;
67057c478bd9Sstevel@tonic-gate 			} else {
67067c478bd9Sstevel@tonic-gate 				signals = S_INPUT | S_RDBAND;
67077c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDBAND;
67087c478bd9Sstevel@tonic-gate 			}
67097c478bd9Sstevel@tonic-gate 		} else if (pri != bp->b_band) {
67107c478bd9Sstevel@tonic-gate 			/*
67117c478bd9Sstevel@tonic-gate 			 * The band is different for the new q_first.
67127c478bd9Sstevel@tonic-gate 			 */
67137c478bd9Sstevel@tonic-gate 			if (bp->b_band == 0) {
67147c478bd9Sstevel@tonic-gate 				signals = S_RDNORM;
67157c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDNORM;
67167c478bd9Sstevel@tonic-gate 			} else {
67177c478bd9Sstevel@tonic-gate 				signals = S_RDBAND;
67187c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDBAND;
67197c478bd9Sstevel@tonic-gate 			}
67207c478bd9Sstevel@tonic-gate 		}
67217c478bd9Sstevel@tonic-gate 
67227c478bd9Sstevel@tonic-gate 		if (pollwakeups != 0) {
67237c478bd9Sstevel@tonic-gate 			if (pollwakeups == (POLLIN | POLLRDNORM)) {
67247c478bd9Sstevel@tonic-gate 				if (!(stp->sd_rput_opt & SR_POLLIN))
67257c478bd9Sstevel@tonic-gate 					goto no_pollwake;
67267c478bd9Sstevel@tonic-gate 				stp->sd_rput_opt &= ~SR_POLLIN;
67277c478bd9Sstevel@tonic-gate 			}
67287c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
67297c478bd9Sstevel@tonic-gate 			pollwakeup(&stp->sd_pollist, pollwakeups);
67307c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
67317c478bd9Sstevel@tonic-gate 		}
67327c478bd9Sstevel@tonic-gate no_pollwake:
67337c478bd9Sstevel@tonic-gate 
67347c478bd9Sstevel@tonic-gate 		if (stp->sd_sigflags & signals)
67357c478bd9Sstevel@tonic-gate 			strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
67367c478bd9Sstevel@tonic-gate 	}
67377c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
67387c478bd9Sstevel@tonic-gate 
67397c478bd9Sstevel@tonic-gate 	rvp->r_val1 = more;
67407c478bd9Sstevel@tonic-gate 	return (error);
67417c478bd9Sstevel@tonic-gate #undef	_LASTMARK
67427c478bd9Sstevel@tonic-gate }
67437c478bd9Sstevel@tonic-gate 
67447c478bd9Sstevel@tonic-gate /*
67457c478bd9Sstevel@tonic-gate  * Get the next message from the read queue.  If the message is
67467c478bd9Sstevel@tonic-gate  * priority, STRPRI will have been set by strrput().  This flag
67477c478bd9Sstevel@tonic-gate  * should be reset only when the entire message at the front of the
67487c478bd9Sstevel@tonic-gate  * queue as been consumed.
67497c478bd9Sstevel@tonic-gate  *
67507c478bd9Sstevel@tonic-gate  * If uiop is NULL all data is returned in mctlp.
67517c478bd9Sstevel@tonic-gate  * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
67527c478bd9Sstevel@tonic-gate  * not enabled.
67537c478bd9Sstevel@tonic-gate  * The timeout parameter is in milliseconds; -1 for infinity.
67547c478bd9Sstevel@tonic-gate  * This routine handles the consolidation private flags:
67557c478bd9Sstevel@tonic-gate  *	MSG_IGNERROR	Ignore any stream head error except STPLEX.
67567c478bd9Sstevel@tonic-gate  *	MSG_DELAYERROR	Defer the error check until the queue is empty.
67577c478bd9Sstevel@tonic-gate  *	MSG_HOLDSIG	Hold signals while waiting for data.
67587c478bd9Sstevel@tonic-gate  *	MSG_IPEEK	Only peek at messages.
67597c478bd9Sstevel@tonic-gate  *	MSG_DISCARDTAIL	Discard the tail M_DATA part of the message
67607c478bd9Sstevel@tonic-gate  *			that doesn't fit.
67617c478bd9Sstevel@tonic-gate  *	MSG_NOMARK	If the message is marked leave it on the queue.
67627c478bd9Sstevel@tonic-gate  *
67637c478bd9Sstevel@tonic-gate  * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
67647c478bd9Sstevel@tonic-gate  */
67657c478bd9Sstevel@tonic-gate int
67667c478bd9Sstevel@tonic-gate kstrgetmsg(
67677c478bd9Sstevel@tonic-gate 	struct vnode *vp,
67687c478bd9Sstevel@tonic-gate 	mblk_t **mctlp,
67697c478bd9Sstevel@tonic-gate 	struct uio *uiop,
67707c478bd9Sstevel@tonic-gate 	unsigned char *prip,
67717c478bd9Sstevel@tonic-gate 	int *flagsp,
67727c478bd9Sstevel@tonic-gate 	clock_t timout,
67737c478bd9Sstevel@tonic-gate 	rval_t *rvp)
67747c478bd9Sstevel@tonic-gate {
67757c478bd9Sstevel@tonic-gate 	struct stdata *stp;
67767c478bd9Sstevel@tonic-gate 	mblk_t *bp, *nbp;
67777c478bd9Sstevel@tonic-gate 	mblk_t *savemp = NULL;
67787c478bd9Sstevel@tonic-gate 	mblk_t *savemptail = NULL;
67797c478bd9Sstevel@tonic-gate 	int flags;
67807c478bd9Sstevel@tonic-gate 	uint_t old_sd_flag;
67817c478bd9Sstevel@tonic-gate 	int flg;
67827c478bd9Sstevel@tonic-gate 	int more = 0;
67837c478bd9Sstevel@tonic-gate 	int error = 0;
67847c478bd9Sstevel@tonic-gate 	char first = 1;
67857c478bd9Sstevel@tonic-gate 	uint_t mark;		/* Contains MSG*MARK and _LASTMARK */
67867c478bd9Sstevel@tonic-gate #define	_LASTMARK	0x8000	/* Distinct from MSG*MARK */
67877c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
67887c478bd9Sstevel@tonic-gate 	queue_t *q;
67897c478bd9Sstevel@tonic-gate 	int	pr = 0;			/* Partial read successful */
67907c478bd9Sstevel@tonic-gate 	unsigned char type;
67917c478bd9Sstevel@tonic-gate 
67927c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_ENTER,
67937c478bd9Sstevel@tonic-gate 		"kstrgetmsg:%p", vp);
67947c478bd9Sstevel@tonic-gate 
67957c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
67967c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
67977c478bd9Sstevel@tonic-gate 	rvp->r_val1 = 0;
67987c478bd9Sstevel@tonic-gate 
67997c478bd9Sstevel@tonic-gate 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
68007c478bd9Sstevel@tonic-gate 		if (error = straccess(stp, JCREAD))
68017c478bd9Sstevel@tonic-gate 			return (error);
68027c478bd9Sstevel@tonic-gate 
68037c478bd9Sstevel@tonic-gate 	flags = *flagsp;
68047c478bd9Sstevel@tonic-gate 	/* Fast check of flags before acquiring the lock */
68057c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STRDERR|STPLEX)) {
68067c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STPLEX) ||
68077c478bd9Sstevel@tonic-gate 		    (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == 0) {
68087c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
68097c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, STRDERR|STPLEX,
68107c478bd9Sstevel@tonic-gate 					(flags & MSG_IPEEK));
68117c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
68127c478bd9Sstevel@tonic-gate 			if (error != 0)
68137c478bd9Sstevel@tonic-gate 				return (error);
68147c478bd9Sstevel@tonic-gate 		}
68157c478bd9Sstevel@tonic-gate 	}
68167c478bd9Sstevel@tonic-gate 
68177c478bd9Sstevel@tonic-gate 	switch (flags & (MSG_HIPRI|MSG_ANY|MSG_BAND)) {
68187c478bd9Sstevel@tonic-gate 	case MSG_HIPRI:
68197c478bd9Sstevel@tonic-gate 		if (*prip != 0)
68207c478bd9Sstevel@tonic-gate 			return (EINVAL);
68217c478bd9Sstevel@tonic-gate 		break;
68227c478bd9Sstevel@tonic-gate 
68237c478bd9Sstevel@tonic-gate 	case MSG_ANY:
68247c478bd9Sstevel@tonic-gate 	case MSG_BAND:
68257c478bd9Sstevel@tonic-gate 		break;
68267c478bd9Sstevel@tonic-gate 
68277c478bd9Sstevel@tonic-gate 	default:
68287c478bd9Sstevel@tonic-gate 		return (EINVAL);
68297c478bd9Sstevel@tonic-gate 	}
68307c478bd9Sstevel@tonic-gate 
68317c478bd9Sstevel@tonic-gate retry:
68327c478bd9Sstevel@tonic-gate 	q = _RD(stp->sd_wrq);
68337c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
68347c478bd9Sstevel@tonic-gate 	old_sd_flag = stp->sd_flag;
68357c478bd9Sstevel@tonic-gate 	mark = 0;
68367c478bd9Sstevel@tonic-gate 	for (;;) {
68377c478bd9Sstevel@tonic-gate 		int done = 0;
68387c478bd9Sstevel@tonic-gate 		int waitflag;
68397c478bd9Sstevel@tonic-gate 		int fmode;
68407c478bd9Sstevel@tonic-gate 		mblk_t *q_first = q->q_first;
68417c478bd9Sstevel@tonic-gate 
68427c478bd9Sstevel@tonic-gate 		/*
68437c478bd9Sstevel@tonic-gate 		 * This section of the code operates just like the code
68447c478bd9Sstevel@tonic-gate 		 * in strgetmsg().  There is a comment there about what
68457c478bd9Sstevel@tonic-gate 		 * is going on here.
68467c478bd9Sstevel@tonic-gate 		 */
68477c478bd9Sstevel@tonic-gate 		if (!(flags & (MSG_HIPRI|MSG_BAND))) {
68487c478bd9Sstevel@tonic-gate 			/* Asking for normal, band0 data */
68497c478bd9Sstevel@tonic-gate 			bp = strget(stp, q, uiop, first, &error);
68507c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(&stp->sd_lock));
68517c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
68527c478bd9Sstevel@tonic-gate 				if (bp->b_datap->db_type == M_SIG) {
68537c478bd9Sstevel@tonic-gate 					strsignal_nolock(stp, *bp->b_rptr,
68547c478bd9Sstevel@tonic-gate 					    (int32_t)bp->b_band);
68557c478bd9Sstevel@tonic-gate 					continue;
68567c478bd9Sstevel@tonic-gate 				} else {
68577c478bd9Sstevel@tonic-gate 					break;
68587c478bd9Sstevel@tonic-gate 				}
68597c478bd9Sstevel@tonic-gate 			}
68607c478bd9Sstevel@tonic-gate 			if (error != 0) {
68617c478bd9Sstevel@tonic-gate 				goto getmout;
68627c478bd9Sstevel@tonic-gate 			}
68637c478bd9Sstevel@tonic-gate 		/*
68647c478bd9Sstevel@tonic-gate 		 * We can't depend on the value of STRPRI here because
68657c478bd9Sstevel@tonic-gate 		 * the stream head may be in transit. Therefore, we
68667c478bd9Sstevel@tonic-gate 		 * must look at the type of the first message to
68677c478bd9Sstevel@tonic-gate 		 * determine if a high priority messages is waiting
68687c478bd9Sstevel@tonic-gate 		 */
68697c478bd9Sstevel@tonic-gate 		} else if ((flags & MSG_HIPRI) && q_first != NULL &&
68707c478bd9Sstevel@tonic-gate 			    q_first->b_datap->db_type >= QPCTL &&
68717c478bd9Sstevel@tonic-gate 			    (bp = getq_noenab(q)) != NULL) {
68727c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_datap->db_type >= QPCTL);
68737c478bd9Sstevel@tonic-gate 			break;
68747c478bd9Sstevel@tonic-gate 		} else if ((flags & MSG_BAND) && q_first != NULL &&
68757c478bd9Sstevel@tonic-gate 			    ((q_first->b_band >= *prip) ||
68767c478bd9Sstevel@tonic-gate 			    q_first->b_datap->db_type >= QPCTL) &&
68777c478bd9Sstevel@tonic-gate 			    (bp = getq_noenab(q)) != NULL) {
68787c478bd9Sstevel@tonic-gate 			/*
68797c478bd9Sstevel@tonic-gate 			 * Asked for at least band "prip" and got either at
68807c478bd9Sstevel@tonic-gate 			 * least that band or a hipri message.
68817c478bd9Sstevel@tonic-gate 			 */
68827c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_band >= *prip ||
68837c478bd9Sstevel@tonic-gate 				bp->b_datap->db_type >= QPCTL);
68847c478bd9Sstevel@tonic-gate 			if (bp->b_datap->db_type == M_SIG) {
68857c478bd9Sstevel@tonic-gate 				strsignal_nolock(stp, *bp->b_rptr,
68867c478bd9Sstevel@tonic-gate 				    (int32_t)bp->b_band);
68877c478bd9Sstevel@tonic-gate 				continue;
68887c478bd9Sstevel@tonic-gate 			} else {
68897c478bd9Sstevel@tonic-gate 				break;
68907c478bd9Sstevel@tonic-gate 			}
68917c478bd9Sstevel@tonic-gate 		}
68927c478bd9Sstevel@tonic-gate 
68937c478bd9Sstevel@tonic-gate 		/* No data. Time to sleep? */
68947c478bd9Sstevel@tonic-gate 		qbackenable(q, 0);
68957c478bd9Sstevel@tonic-gate 
68967c478bd9Sstevel@tonic-gate 		/*
68977c478bd9Sstevel@tonic-gate 		 * Delayed error notification?
68987c478bd9Sstevel@tonic-gate 		 */
68997c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & (STRDERR|STPLEX)) &&
69007c478bd9Sstevel@tonic-gate 		    (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == MSG_DELAYERROR) {
69017c478bd9Sstevel@tonic-gate 			error = strgeterr(stp, STRDERR|STPLEX,
69027c478bd9Sstevel@tonic-gate 					(flags & MSG_IPEEK));
69037c478bd9Sstevel@tonic-gate 			if (error != 0) {
69047c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
69057c478bd9Sstevel@tonic-gate 				return (error);
69067c478bd9Sstevel@tonic-gate 			}
69077c478bd9Sstevel@tonic-gate 		}
69087c478bd9Sstevel@tonic-gate 
69097c478bd9Sstevel@tonic-gate 		/*
69107c478bd9Sstevel@tonic-gate 		 * If STRHUP or STREOF, return 0 length control and data.
69117c478bd9Sstevel@tonic-gate 		 * If a read(fd,buf,0) has been done, do not sleep, just
69127c478bd9Sstevel@tonic-gate 		 * return.
69137c478bd9Sstevel@tonic-gate 		 *
69147c478bd9Sstevel@tonic-gate 		 * If mctlp == NULL and uiop == NULL, then the code will
69157c478bd9Sstevel@tonic-gate 		 * do the strwaitq. This is an understood way of saying
69167c478bd9Sstevel@tonic-gate 		 * sleep "polling" until a message is received.
69177c478bd9Sstevel@tonic-gate 		 */
69187c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & (STRHUP|STREOF)) ||
69197c478bd9Sstevel@tonic-gate 		    (uiop != NULL && uiop->uio_resid == 0)) {
69207c478bd9Sstevel@tonic-gate 			if (mctlp != NULL)
69217c478bd9Sstevel@tonic-gate 				*mctlp = NULL;
69227c478bd9Sstevel@tonic-gate 			*flagsp = 0;
69237c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
69247c478bd9Sstevel@tonic-gate 			return (0);
69257c478bd9Sstevel@tonic-gate 		}
69267c478bd9Sstevel@tonic-gate 
69277c478bd9Sstevel@tonic-gate 		waitflag = GETWAIT;
69287c478bd9Sstevel@tonic-gate 		if (flags &
69297c478bd9Sstevel@tonic-gate 		    (MSG_HOLDSIG|MSG_IGNERROR|MSG_IPEEK|MSG_DELAYERROR)) {
69307c478bd9Sstevel@tonic-gate 			if (flags & MSG_HOLDSIG)
69317c478bd9Sstevel@tonic-gate 				waitflag |= STR_NOSIG;
69327c478bd9Sstevel@tonic-gate 			if (flags & MSG_IGNERROR)
69337c478bd9Sstevel@tonic-gate 				waitflag |= STR_NOERROR;
69347c478bd9Sstevel@tonic-gate 			if (flags & MSG_IPEEK)
69357c478bd9Sstevel@tonic-gate 				waitflag |= STR_PEEK;
69367c478bd9Sstevel@tonic-gate 			if (flags & MSG_DELAYERROR)
69377c478bd9Sstevel@tonic-gate 				waitflag |= STR_DELAYERR;
69387c478bd9Sstevel@tonic-gate 		}
69397c478bd9Sstevel@tonic-gate 		if (uiop != NULL)
69407c478bd9Sstevel@tonic-gate 			fmode = uiop->uio_fmode;
69417c478bd9Sstevel@tonic-gate 		else
69427c478bd9Sstevel@tonic-gate 			fmode = 0;
69437c478bd9Sstevel@tonic-gate 
69447c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_WAIT,
69457c478bd9Sstevel@tonic-gate 			"kstrgetmsg calls strwaitq:%p, %p",
69467c478bd9Sstevel@tonic-gate 			vp, uiop);
69477c478bd9Sstevel@tonic-gate 		if (((error = strwaitq(stp, waitflag, (ssize_t)0,
69487c478bd9Sstevel@tonic-gate 		    fmode, timout, &done)) != 0) || done) {
69497c478bd9Sstevel@tonic-gate 			TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_DONE,
69507c478bd9Sstevel@tonic-gate 				"kstrgetmsg error or done:%p, %p",
69517c478bd9Sstevel@tonic-gate 				vp, uiop);
69527c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
69537c478bd9Sstevel@tonic-gate 			return (error);
69547c478bd9Sstevel@tonic-gate 		}
69557c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_AWAKE,
69567c478bd9Sstevel@tonic-gate 			"kstrgetmsg awakes:%p, %p", vp, uiop);
69577c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO) {
69587c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
69597c478bd9Sstevel@tonic-gate 			if (error = straccess(stp, JCREAD))
69607c478bd9Sstevel@tonic-gate 				return (error);
69617c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
69627c478bd9Sstevel@tonic-gate 		}
69637c478bd9Sstevel@tonic-gate 		first = 0;
69647c478bd9Sstevel@tonic-gate 	}
69657c478bd9Sstevel@tonic-gate 	ASSERT(bp != NULL);
69667c478bd9Sstevel@tonic-gate 	/*
69677c478bd9Sstevel@tonic-gate 	 * Extract any mark information. If the message is not completely
69687c478bd9Sstevel@tonic-gate 	 * consumed this information will be put in the mblk
69697c478bd9Sstevel@tonic-gate 	 * that is putback.
69707c478bd9Sstevel@tonic-gate 	 * If MSGMARKNEXT is set and the message is completely consumed
69717c478bd9Sstevel@tonic-gate 	 * the STRATMARK flag will be set below. Likewise, if
69727c478bd9Sstevel@tonic-gate 	 * MSGNOTMARKNEXT is set and the message is
69737c478bd9Sstevel@tonic-gate 	 * completely consumed STRNOTATMARK will be set.
69747c478bd9Sstevel@tonic-gate 	 */
69757c478bd9Sstevel@tonic-gate 	mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
69767c478bd9Sstevel@tonic-gate 	ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
69777c478bd9Sstevel@tonic-gate 		(MSGMARKNEXT|MSGNOTMARKNEXT));
69787c478bd9Sstevel@tonic-gate 	pri = bp->b_band;
69797c478bd9Sstevel@tonic-gate 	if (mark != 0) {
69807c478bd9Sstevel@tonic-gate 		/*
69817c478bd9Sstevel@tonic-gate 		 * If the caller doesn't want the mark return.
69827c478bd9Sstevel@tonic-gate 		 * Used to implement MSG_WAITALL in sockets.
69837c478bd9Sstevel@tonic-gate 		 */
69847c478bd9Sstevel@tonic-gate 		if (flags & MSG_NOMARK) {
69857c478bd9Sstevel@tonic-gate 			putback(stp, q, bp, pri);
69867c478bd9Sstevel@tonic-gate 			qbackenable(q, pri);
69877c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
69887c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
69897c478bd9Sstevel@tonic-gate 		}
69907c478bd9Sstevel@tonic-gate 		if (bp == stp->sd_mark) {
69917c478bd9Sstevel@tonic-gate 			mark |= _LASTMARK;
69927c478bd9Sstevel@tonic-gate 			stp->sd_mark = NULL;
69937c478bd9Sstevel@tonic-gate 		}
69947c478bd9Sstevel@tonic-gate 	}
69957c478bd9Sstevel@tonic-gate 
69967c478bd9Sstevel@tonic-gate 	/*
69977c478bd9Sstevel@tonic-gate 	 * keep track of the first message type
69987c478bd9Sstevel@tonic-gate 	 */
69997c478bd9Sstevel@tonic-gate 	type = bp->b_datap->db_type;
70007c478bd9Sstevel@tonic-gate 
70017c478bd9Sstevel@tonic-gate 	if (bp->b_datap->db_type == M_PASSFP) {
70027c478bd9Sstevel@tonic-gate 		if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
70037c478bd9Sstevel@tonic-gate 			stp->sd_mark = bp;
70047c478bd9Sstevel@tonic-gate 		bp->b_flag |= mark & ~_LASTMARK;
70057c478bd9Sstevel@tonic-gate 		putback(stp, q, bp, pri);
70067c478bd9Sstevel@tonic-gate 		qbackenable(q, pri);
70077c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
70087c478bd9Sstevel@tonic-gate 		return (EBADMSG);
70097c478bd9Sstevel@tonic-gate 	}
70107c478bd9Sstevel@tonic-gate 	ASSERT(type != M_SIG);
70117c478bd9Sstevel@tonic-gate 
70127c478bd9Sstevel@tonic-gate 	if (flags & MSG_IPEEK) {
70137c478bd9Sstevel@tonic-gate 		/*
70147c478bd9Sstevel@tonic-gate 		 * Clear any struioflag - we do the uiomove over again
70157c478bd9Sstevel@tonic-gate 		 * when peeking since it simplifies the code.
70167c478bd9Sstevel@tonic-gate 		 *
70177c478bd9Sstevel@tonic-gate 		 * Dup the message and put the original back on the queue.
70187c478bd9Sstevel@tonic-gate 		 * If dupmsg() fails, try again with copymsg() to see if
70197c478bd9Sstevel@tonic-gate 		 * there is indeed a shortage of memory.  dupmsg() may fail
70207c478bd9Sstevel@tonic-gate 		 * if db_ref in any of the messages reaches its limit.
70217c478bd9Sstevel@tonic-gate 		 */
70227c478bd9Sstevel@tonic-gate 		if ((nbp = dupmsg(bp)) == NULL && (nbp = copymsg(bp)) == NULL) {
70237c478bd9Sstevel@tonic-gate 			/*
70247c478bd9Sstevel@tonic-gate 			 * Restore the state of the stream head since we
70257c478bd9Sstevel@tonic-gate 			 * need to drop sd_lock (strwaitbuf is sleeping).
70267c478bd9Sstevel@tonic-gate 			 */
70277c478bd9Sstevel@tonic-gate 			size_t size = msgdsize(bp);
70287c478bd9Sstevel@tonic-gate 
70297c478bd9Sstevel@tonic-gate 			if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
70307c478bd9Sstevel@tonic-gate 				stp->sd_mark = bp;
70317c478bd9Sstevel@tonic-gate 			bp->b_flag |= mark & ~_LASTMARK;
70327c478bd9Sstevel@tonic-gate 			putback(stp, q, bp, pri);
70337c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
70347c478bd9Sstevel@tonic-gate 			error = strwaitbuf(size, BPRI_HI);
70357c478bd9Sstevel@tonic-gate 			if (error) {
70367c478bd9Sstevel@tonic-gate 				/*
70377c478bd9Sstevel@tonic-gate 				 * There is no net change to the queue thus
70387c478bd9Sstevel@tonic-gate 				 * no need to qbackenable.
70397c478bd9Sstevel@tonic-gate 				 */
70407c478bd9Sstevel@tonic-gate 				return (error);
70417c478bd9Sstevel@tonic-gate 			}
70427c478bd9Sstevel@tonic-gate 			goto retry;
70437c478bd9Sstevel@tonic-gate 		}
70447c478bd9Sstevel@tonic-gate 
70457c478bd9Sstevel@tonic-gate 		if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
70467c478bd9Sstevel@tonic-gate 			stp->sd_mark = bp;
70477c478bd9Sstevel@tonic-gate 		bp->b_flag |= mark & ~_LASTMARK;
70487c478bd9Sstevel@tonic-gate 		putback(stp, q, bp, pri);
70497c478bd9Sstevel@tonic-gate 		bp = nbp;
70507c478bd9Sstevel@tonic-gate 	}
70517c478bd9Sstevel@tonic-gate 
70527c478bd9Sstevel@tonic-gate 	/*
70537c478bd9Sstevel@tonic-gate 	 * Set this flag so strrput will not generate signals. Need to
70547c478bd9Sstevel@tonic-gate 	 * make sure this flag is cleared before leaving this routine
70557c478bd9Sstevel@tonic-gate 	 * else signals will stop being sent.
70567c478bd9Sstevel@tonic-gate 	 */
70577c478bd9Sstevel@tonic-gate 	stp->sd_flag |= STRGETINPROG;
70587c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
70597c478bd9Sstevel@tonic-gate 
7060*c28749e9Skais 	if ((stp->sd_rputdatafunc != NULL) && (DB_TYPE(bp) == M_DATA) &&
7061*c28749e9Skais 	    (!(DB_FLAGS(bp) & DBLK_COOKED))) {
7062*c28749e9Skais 
7063*c28749e9Skais 		bp = (stp->sd_rputdatafunc)(
7064*c28749e9Skais 		    stp->sd_vnode, bp, NULL,
7065*c28749e9Skais 		    NULL, NULL, NULL);
7066*c28749e9Skais 
7067*c28749e9Skais 		if (bp == NULL)
7068*c28749e9Skais 			goto retry;
7069*c28749e9Skais 
7070*c28749e9Skais 		DB_FLAGS(bp) |= DBLK_COOKED;
7071*c28749e9Skais 	}
7072*c28749e9Skais 
70737c478bd9Sstevel@tonic-gate 	if (STREAM_NEEDSERVICE(stp))
70747c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
70757c478bd9Sstevel@tonic-gate 
70767c478bd9Sstevel@tonic-gate 	/*
70777c478bd9Sstevel@tonic-gate 	 * Set HIPRI flag if message is priority.
70787c478bd9Sstevel@tonic-gate 	 */
70797c478bd9Sstevel@tonic-gate 	if (type >= QPCTL)
70807c478bd9Sstevel@tonic-gate 		flg = MSG_HIPRI;
70817c478bd9Sstevel@tonic-gate 	else
70827c478bd9Sstevel@tonic-gate 		flg = MSG_BAND;
70837c478bd9Sstevel@tonic-gate 
70847c478bd9Sstevel@tonic-gate 	/*
70857c478bd9Sstevel@tonic-gate 	 * First process PROTO or PCPROTO blocks, if any.
70867c478bd9Sstevel@tonic-gate 	 */
70877c478bd9Sstevel@tonic-gate 	if (mctlp != NULL && type != M_DATA) {
70887c478bd9Sstevel@tonic-gate 		mblk_t *nbp;
70897c478bd9Sstevel@tonic-gate 
70907c478bd9Sstevel@tonic-gate 		*mctlp = bp;
70917c478bd9Sstevel@tonic-gate 		while (bp->b_cont && bp->b_cont->b_datap->db_type != M_DATA)
70927c478bd9Sstevel@tonic-gate 			bp = bp->b_cont;
70937c478bd9Sstevel@tonic-gate 		nbp = bp->b_cont;
70947c478bd9Sstevel@tonic-gate 		bp->b_cont = NULL;
70957c478bd9Sstevel@tonic-gate 		bp = nbp;
70967c478bd9Sstevel@tonic-gate 	}
70977c478bd9Sstevel@tonic-gate 
70987c478bd9Sstevel@tonic-gate 	if (bp && bp->b_datap->db_type != M_DATA) {
70997c478bd9Sstevel@tonic-gate 		/*
71007c478bd9Sstevel@tonic-gate 		 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
71017c478bd9Sstevel@tonic-gate 		 */
71027c478bd9Sstevel@tonic-gate 		more |= MORECTL;
71037c478bd9Sstevel@tonic-gate 		savemp = bp;
71047c478bd9Sstevel@tonic-gate 		while (bp && bp->b_datap->db_type != M_DATA) {
71057c478bd9Sstevel@tonic-gate 			savemptail = bp;
71067c478bd9Sstevel@tonic-gate 			bp = bp->b_cont;
71077c478bd9Sstevel@tonic-gate 		}
71087c478bd9Sstevel@tonic-gate 		savemptail->b_cont = NULL;
71097c478bd9Sstevel@tonic-gate 	}
71107c478bd9Sstevel@tonic-gate 
71117c478bd9Sstevel@tonic-gate 	/*
71127c478bd9Sstevel@tonic-gate 	 * Now process DATA blocks, if any.
71137c478bd9Sstevel@tonic-gate 	 */
71147c478bd9Sstevel@tonic-gate 	if (uiop == NULL) {
71157c478bd9Sstevel@tonic-gate 		/* Append data to tail of mctlp */
71167c478bd9Sstevel@tonic-gate 		if (mctlp != NULL) {
71177c478bd9Sstevel@tonic-gate 			mblk_t **mpp = mctlp;
71187c478bd9Sstevel@tonic-gate 
71197c478bd9Sstevel@tonic-gate 			while (*mpp != NULL)
71207c478bd9Sstevel@tonic-gate 				mpp = &((*mpp)->b_cont);
71217c478bd9Sstevel@tonic-gate 			*mpp = bp;
71227c478bd9Sstevel@tonic-gate 			bp = NULL;
71237c478bd9Sstevel@tonic-gate 		}
71247c478bd9Sstevel@tonic-gate 	} else if (uiop->uio_resid >= 0 && bp) {
71257c478bd9Sstevel@tonic-gate 		size_t oldresid = uiop->uio_resid;
71267c478bd9Sstevel@tonic-gate 
71277c478bd9Sstevel@tonic-gate 		/*
71287c478bd9Sstevel@tonic-gate 		 * If a streams message is likely to consist
71297c478bd9Sstevel@tonic-gate 		 * of many small mblks, it is pulled up into
71307c478bd9Sstevel@tonic-gate 		 * one continuous chunk of memory.
71317c478bd9Sstevel@tonic-gate 		 * see longer comment at top of page
71327c478bd9Sstevel@tonic-gate 		 * by mblk_pull_len declaration.
71337c478bd9Sstevel@tonic-gate 		 */
71347c478bd9Sstevel@tonic-gate 
71357c478bd9Sstevel@tonic-gate 		if (MBLKL(bp) < mblk_pull_len) {
71367c478bd9Sstevel@tonic-gate 			(void) pullupmsg(bp, -1);
71377c478bd9Sstevel@tonic-gate 		}
71387c478bd9Sstevel@tonic-gate 
71397c478bd9Sstevel@tonic-gate 		bp = struiocopyout(bp, uiop, &error);
71407c478bd9Sstevel@tonic-gate 		if (error != 0) {
71417c478bd9Sstevel@tonic-gate 			if (mctlp != NULL) {
71427c478bd9Sstevel@tonic-gate 				freemsg(*mctlp);
71437c478bd9Sstevel@tonic-gate 				*mctlp = NULL;
71447c478bd9Sstevel@tonic-gate 			} else
71457c478bd9Sstevel@tonic-gate 				freemsg(savemp);
71467c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
71477c478bd9Sstevel@tonic-gate 			/*
71487c478bd9Sstevel@tonic-gate 			 * clear stream head hi pri flag based on
71497c478bd9Sstevel@tonic-gate 			 * first message
71507c478bd9Sstevel@tonic-gate 			 */
71517c478bd9Sstevel@tonic-gate 			if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
71527c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
71537c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
71547c478bd9Sstevel@tonic-gate 			}
71557c478bd9Sstevel@tonic-gate 			more = 0;
71567c478bd9Sstevel@tonic-gate 			goto getmout;
71577c478bd9Sstevel@tonic-gate 		}
71587c478bd9Sstevel@tonic-gate 		/*
71597c478bd9Sstevel@tonic-gate 		 * (pr == 1) indicates a partial read.
71607c478bd9Sstevel@tonic-gate 		 */
71617c478bd9Sstevel@tonic-gate 		if (oldresid > uiop->uio_resid)
71627c478bd9Sstevel@tonic-gate 			pr = 1;
71637c478bd9Sstevel@tonic-gate 	}
71647c478bd9Sstevel@tonic-gate 
71657c478bd9Sstevel@tonic-gate 	if (bp) {			/* more data blocks in msg */
71667c478bd9Sstevel@tonic-gate 		more |= MOREDATA;
71677c478bd9Sstevel@tonic-gate 		if (savemp)
71687c478bd9Sstevel@tonic-gate 			savemptail->b_cont = bp;
71697c478bd9Sstevel@tonic-gate 		else
71707c478bd9Sstevel@tonic-gate 			savemp = bp;
71717c478bd9Sstevel@tonic-gate 	}
71727c478bd9Sstevel@tonic-gate 
71737c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
71747c478bd9Sstevel@tonic-gate 	if (savemp) {
71757c478bd9Sstevel@tonic-gate 		if (flags & (MSG_IPEEK|MSG_DISCARDTAIL)) {
71767c478bd9Sstevel@tonic-gate 			/*
71777c478bd9Sstevel@tonic-gate 			 * When MSG_DISCARDTAIL is set or
71787c478bd9Sstevel@tonic-gate 			 * when peeking discard any tail. When peeking this
71797c478bd9Sstevel@tonic-gate 			 * is the tail of the dup that was copied out - the
71807c478bd9Sstevel@tonic-gate 			 * message has already been putback on the queue.
71817c478bd9Sstevel@tonic-gate 			 * Return MOREDATA to the caller even though the data
71827c478bd9Sstevel@tonic-gate 			 * is discarded. This is used by sockets (to
71837c478bd9Sstevel@tonic-gate 			 * set MSG_TRUNC).
71847c478bd9Sstevel@tonic-gate 			 */
71857c478bd9Sstevel@tonic-gate 			freemsg(savemp);
71867c478bd9Sstevel@tonic-gate 			if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
71877c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
71887c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
71897c478bd9Sstevel@tonic-gate 			}
71907c478bd9Sstevel@tonic-gate 		} else if (pr && (savemp->b_datap->db_type == M_DATA) &&
71917c478bd9Sstevel@tonic-gate 			    msgnodata(savemp)) {
71927c478bd9Sstevel@tonic-gate 			/*
71937c478bd9Sstevel@tonic-gate 			 * Avoid queuing a zero-length tail part of
71947c478bd9Sstevel@tonic-gate 			 * a message. pr=1 indicates that we read some of
71957c478bd9Sstevel@tonic-gate 			 * the message.
71967c478bd9Sstevel@tonic-gate 			 */
71977c478bd9Sstevel@tonic-gate 			freemsg(savemp);
71987c478bd9Sstevel@tonic-gate 			more &= ~MOREDATA;
71997c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
72007c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
72017c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRPRI;
72027c478bd9Sstevel@tonic-gate 			}
72037c478bd9Sstevel@tonic-gate 		} else {
72047c478bd9Sstevel@tonic-gate 			savemp->b_band = pri;
72057c478bd9Sstevel@tonic-gate 			/*
72067c478bd9Sstevel@tonic-gate 			 * If the first message was HIPRI and the one we're
72077c478bd9Sstevel@tonic-gate 			 * putting back isn't, then clear STRPRI, otherwise
72087c478bd9Sstevel@tonic-gate 			 * set STRPRI again.  Note that we must set STRPRI
72097c478bd9Sstevel@tonic-gate 			 * again since the flush logic in strrput_nondata()
72107c478bd9Sstevel@tonic-gate 			 * may have cleared it while we had sd_lock dropped.
72117c478bd9Sstevel@tonic-gate 			 */
72127c478bd9Sstevel@tonic-gate 			if (type >= QPCTL) {
72137c478bd9Sstevel@tonic-gate 				ASSERT(type == M_PCPROTO);
72147c478bd9Sstevel@tonic-gate 				if (queclass(savemp) < QPCTL)
72157c478bd9Sstevel@tonic-gate 					stp->sd_flag &= ~STRPRI;
72167c478bd9Sstevel@tonic-gate 				else
72177c478bd9Sstevel@tonic-gate 					stp->sd_flag |= STRPRI;
72187c478bd9Sstevel@tonic-gate 			} else if (queclass(savemp) >= QPCTL) {
72197c478bd9Sstevel@tonic-gate 				/*
72207c478bd9Sstevel@tonic-gate 				 * The first message was not a HIPRI message,
72217c478bd9Sstevel@tonic-gate 				 * but the one we are about to putback is.
72227c478bd9Sstevel@tonic-gate 				 * For simplicitly, we do not allow for HIPRI
72237c478bd9Sstevel@tonic-gate 				 * messages to be embedded in the message
72247c478bd9Sstevel@tonic-gate 				 * body, so just force it to same type as
72257c478bd9Sstevel@tonic-gate 				 * first message.
72267c478bd9Sstevel@tonic-gate 				 */
72277c478bd9Sstevel@tonic-gate 				ASSERT(type == M_DATA || type == M_PROTO);
72287c478bd9Sstevel@tonic-gate 				ASSERT(savemp->b_datap->db_type == M_PCPROTO);
72297c478bd9Sstevel@tonic-gate 				savemp->b_datap->db_type = type;
72307c478bd9Sstevel@tonic-gate 			}
72317c478bd9Sstevel@tonic-gate 			if (mark != 0) {
72327c478bd9Sstevel@tonic-gate 				if ((mark & _LASTMARK) &&
72337c478bd9Sstevel@tonic-gate 				    (stp->sd_mark == NULL)) {
72347c478bd9Sstevel@tonic-gate 					/*
72357c478bd9Sstevel@tonic-gate 					 * If another marked message arrived
72367c478bd9Sstevel@tonic-gate 					 * while sd_lock was not held sd_mark
72377c478bd9Sstevel@tonic-gate 					 * would be non-NULL.
72387c478bd9Sstevel@tonic-gate 					 */
72397c478bd9Sstevel@tonic-gate 					stp->sd_mark = savemp;
72407c478bd9Sstevel@tonic-gate 				}
72417c478bd9Sstevel@tonic-gate 				savemp->b_flag |= mark & ~_LASTMARK;
72427c478bd9Sstevel@tonic-gate 			}
72437c478bd9Sstevel@tonic-gate 			putback(stp, q, savemp, pri);
72447c478bd9Sstevel@tonic-gate 		}
72457c478bd9Sstevel@tonic-gate 	} else if (!(flags & MSG_IPEEK)) {
72467c478bd9Sstevel@tonic-gate 		/*
72477c478bd9Sstevel@tonic-gate 		 * The complete message was consumed.
72487c478bd9Sstevel@tonic-gate 		 *
72497c478bd9Sstevel@tonic-gate 		 * If another M_PCPROTO arrived while sd_lock was not held
72507c478bd9Sstevel@tonic-gate 		 * it would have been discarded since STRPRI was still set.
72517c478bd9Sstevel@tonic-gate 		 *
72527c478bd9Sstevel@tonic-gate 		 * Move the MSG*MARKNEXT information
72537c478bd9Sstevel@tonic-gate 		 * to the stream head just in case
72547c478bd9Sstevel@tonic-gate 		 * the read queue becomes empty.
72557c478bd9Sstevel@tonic-gate 		 * clear stream head hi pri flag based on
72567c478bd9Sstevel@tonic-gate 		 * first message
72577c478bd9Sstevel@tonic-gate 		 *
72587c478bd9Sstevel@tonic-gate 		 * If the stream head was at the mark
72597c478bd9Sstevel@tonic-gate 		 * (STRATMARK) before we dropped sd_lock above
72607c478bd9Sstevel@tonic-gate 		 * and some data was consumed then we have
72617c478bd9Sstevel@tonic-gate 		 * moved past the mark thus STRATMARK is
72627c478bd9Sstevel@tonic-gate 		 * cleared. However, if a message arrived in
72637c478bd9Sstevel@tonic-gate 		 * strrput during the copyout above causing
72647c478bd9Sstevel@tonic-gate 		 * STRATMARK to be set we can not clear that
72657c478bd9Sstevel@tonic-gate 		 * flag.
72667c478bd9Sstevel@tonic-gate 		 * XXX A "perimeter" would help by single-threading strrput,
72677c478bd9Sstevel@tonic-gate 		 * strread, strgetmsg and kstrgetmsg.
72687c478bd9Sstevel@tonic-gate 		 */
72697c478bd9Sstevel@tonic-gate 		if (type >= QPCTL) {
72707c478bd9Sstevel@tonic-gate 			ASSERT(type == M_PCPROTO);
72717c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRPRI;
72727c478bd9Sstevel@tonic-gate 		}
72737c478bd9Sstevel@tonic-gate 		if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
72747c478bd9Sstevel@tonic-gate 			if (mark & MSGMARKNEXT) {
72757c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRNOTATMARK;
72767c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRATMARK;
72777c478bd9Sstevel@tonic-gate 			} else if (mark & MSGNOTMARKNEXT) {
72787c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRATMARK;
72797c478bd9Sstevel@tonic-gate 				stp->sd_flag |= STRNOTATMARK;
72807c478bd9Sstevel@tonic-gate 			} else {
72817c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
72827c478bd9Sstevel@tonic-gate 			}
72837c478bd9Sstevel@tonic-gate 		} else if (pr && (old_sd_flag & STRATMARK)) {
72847c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRATMARK;
72857c478bd9Sstevel@tonic-gate 		}
72867c478bd9Sstevel@tonic-gate 	}
72877c478bd9Sstevel@tonic-gate 
72887c478bd9Sstevel@tonic-gate 	*flagsp = flg;
72897c478bd9Sstevel@tonic-gate 	*prip = pri;
72907c478bd9Sstevel@tonic-gate 
72917c478bd9Sstevel@tonic-gate 	/*
72927c478bd9Sstevel@tonic-gate 	 * Getmsg cleanup processing - if the state of the queue has changed
72937c478bd9Sstevel@tonic-gate 	 * some signals may need to be sent and/or poll awakened.
72947c478bd9Sstevel@tonic-gate 	 */
72957c478bd9Sstevel@tonic-gate getmout:
72967c478bd9Sstevel@tonic-gate 	qbackenable(q, pri);
72977c478bd9Sstevel@tonic-gate 
72987c478bd9Sstevel@tonic-gate 	/*
72997c478bd9Sstevel@tonic-gate 	 * We dropped the stream head lock above. Send all M_SIG messages
73007c478bd9Sstevel@tonic-gate 	 * before processing stream head for SIGPOLL messages.
73017c478bd9Sstevel@tonic-gate 	 */
73027c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
73037c478bd9Sstevel@tonic-gate 	while ((bp = q->q_first) != NULL &&
73047c478bd9Sstevel@tonic-gate 	    (bp->b_datap->db_type == M_SIG)) {
73057c478bd9Sstevel@tonic-gate 		/*
73067c478bd9Sstevel@tonic-gate 		 * sd_lock is held so the content of the read queue can not
73077c478bd9Sstevel@tonic-gate 		 * change.
73087c478bd9Sstevel@tonic-gate 		 */
73097c478bd9Sstevel@tonic-gate 		bp = getq(q);
73107c478bd9Sstevel@tonic-gate 		ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
73117c478bd9Sstevel@tonic-gate 
73127c478bd9Sstevel@tonic-gate 		strsignal_nolock(stp, *bp->b_rptr, (int32_t)bp->b_band);
73137c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
73147c478bd9Sstevel@tonic-gate 		freemsg(bp);
73157c478bd9Sstevel@tonic-gate 		if (STREAM_NEEDSERVICE(stp))
73167c478bd9Sstevel@tonic-gate 			stream_runservice(stp);
73177c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
73187c478bd9Sstevel@tonic-gate 	}
73197c478bd9Sstevel@tonic-gate 
73207c478bd9Sstevel@tonic-gate 	/*
73217c478bd9Sstevel@tonic-gate 	 * stream head cannot change while we make the determination
73227c478bd9Sstevel@tonic-gate 	 * whether or not to send a signal. Drop the flag to allow strrput
73237c478bd9Sstevel@tonic-gate 	 * to send firstmsgsigs again.
73247c478bd9Sstevel@tonic-gate 	 */
73257c478bd9Sstevel@tonic-gate 	stp->sd_flag &= ~STRGETINPROG;
73267c478bd9Sstevel@tonic-gate 
73277c478bd9Sstevel@tonic-gate 	/*
73287c478bd9Sstevel@tonic-gate 	 * If the type of message at the front of the queue changed
73297c478bd9Sstevel@tonic-gate 	 * due to the receive the appropriate signals and pollwakeup events
73307c478bd9Sstevel@tonic-gate 	 * are generated. The type of changes are:
73317c478bd9Sstevel@tonic-gate 	 *	Processed a hipri message, q_first is not hipri.
73327c478bd9Sstevel@tonic-gate 	 *	Processed a band X message, and q_first is band Y.
73337c478bd9Sstevel@tonic-gate 	 * The generated signals and pollwakeups are identical to what
73347c478bd9Sstevel@tonic-gate 	 * strrput() generates should the message that is now on q_first
73357c478bd9Sstevel@tonic-gate 	 * arrive to an empty read queue.
73367c478bd9Sstevel@tonic-gate 	 *
73377c478bd9Sstevel@tonic-gate 	 * Note: only strrput will send a signal for a hipri message.
73387c478bd9Sstevel@tonic-gate 	 */
73397c478bd9Sstevel@tonic-gate 	if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
73407c478bd9Sstevel@tonic-gate 		strsigset_t signals = 0;
73417c478bd9Sstevel@tonic-gate 		strpollset_t pollwakeups = 0;
73427c478bd9Sstevel@tonic-gate 
73437c478bd9Sstevel@tonic-gate 		if (flg & MSG_HIPRI) {
73447c478bd9Sstevel@tonic-gate 			/*
73457c478bd9Sstevel@tonic-gate 			 * Removed a hipri message. Regular data at
73467c478bd9Sstevel@tonic-gate 			 * the front of  the queue.
73477c478bd9Sstevel@tonic-gate 			 */
73487c478bd9Sstevel@tonic-gate 			if (bp->b_band == 0) {
73497c478bd9Sstevel@tonic-gate 				signals = S_INPUT | S_RDNORM;
73507c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDNORM;
73517c478bd9Sstevel@tonic-gate 			} else {
73527c478bd9Sstevel@tonic-gate 				signals = S_INPUT | S_RDBAND;
73537c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDBAND;
73547c478bd9Sstevel@tonic-gate 			}
73557c478bd9Sstevel@tonic-gate 		} else if (pri != bp->b_band) {
73567c478bd9Sstevel@tonic-gate 			/*
73577c478bd9Sstevel@tonic-gate 			 * The band is different for the new q_first.
73587c478bd9Sstevel@tonic-gate 			 */
73597c478bd9Sstevel@tonic-gate 			if (bp->b_band == 0) {
73607c478bd9Sstevel@tonic-gate 				signals = S_RDNORM;
73617c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDNORM;
73627c478bd9Sstevel@tonic-gate 			} else {
73637c478bd9Sstevel@tonic-gate 				signals = S_RDBAND;
73647c478bd9Sstevel@tonic-gate 				pollwakeups = POLLIN | POLLRDBAND;
73657c478bd9Sstevel@tonic-gate 			}
73667c478bd9Sstevel@tonic-gate 		}
73677c478bd9Sstevel@tonic-gate 
73687c478bd9Sstevel@tonic-gate 		if (pollwakeups != 0) {
73697c478bd9Sstevel@tonic-gate 			if (pollwakeups == (POLLIN | POLLRDNORM)) {
73707c478bd9Sstevel@tonic-gate 				if (!(stp->sd_rput_opt & SR_POLLIN))
73717c478bd9Sstevel@tonic-gate 					goto no_pollwake;
73727c478bd9Sstevel@tonic-gate 				stp->sd_rput_opt &= ~SR_POLLIN;
73737c478bd9Sstevel@tonic-gate 			}
73747c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
73757c478bd9Sstevel@tonic-gate 			pollwakeup(&stp->sd_pollist, pollwakeups);
73767c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
73777c478bd9Sstevel@tonic-gate 		}
73787c478bd9Sstevel@tonic-gate no_pollwake:
73797c478bd9Sstevel@tonic-gate 
73807c478bd9Sstevel@tonic-gate 		if (stp->sd_sigflags & signals)
73817c478bd9Sstevel@tonic-gate 			strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
73827c478bd9Sstevel@tonic-gate 	}
73837c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
73847c478bd9Sstevel@tonic-gate 
73857c478bd9Sstevel@tonic-gate 	rvp->r_val1 = more;
73867c478bd9Sstevel@tonic-gate 	return (error);
73877c478bd9Sstevel@tonic-gate #undef	_LASTMARK
73887c478bd9Sstevel@tonic-gate }
73897c478bd9Sstevel@tonic-gate 
73907c478bd9Sstevel@tonic-gate /*
73917c478bd9Sstevel@tonic-gate  * Put a message downstream.
73927c478bd9Sstevel@tonic-gate  *
73937c478bd9Sstevel@tonic-gate  * NOTE: strputmsg and kstrputmsg have much of the logic in common.
73947c478bd9Sstevel@tonic-gate  */
73957c478bd9Sstevel@tonic-gate int
73967c478bd9Sstevel@tonic-gate strputmsg(
73977c478bd9Sstevel@tonic-gate 	struct vnode *vp,
73987c478bd9Sstevel@tonic-gate 	struct strbuf *mctl,
73997c478bd9Sstevel@tonic-gate 	struct strbuf *mdata,
74007c478bd9Sstevel@tonic-gate 	unsigned char pri,
74017c478bd9Sstevel@tonic-gate 	int flag,
74027c478bd9Sstevel@tonic-gate 	int fmode)
74037c478bd9Sstevel@tonic-gate {
74047c478bd9Sstevel@tonic-gate 	struct stdata *stp;
74057c478bd9Sstevel@tonic-gate 	queue_t *wqp;
74067c478bd9Sstevel@tonic-gate 	mblk_t *mp;
74077c478bd9Sstevel@tonic-gate 	ssize_t msgsize;
74087c478bd9Sstevel@tonic-gate 	ssize_t rmin, rmax;
74097c478bd9Sstevel@tonic-gate 	int error;
74107c478bd9Sstevel@tonic-gate 	struct uio uios;
74117c478bd9Sstevel@tonic-gate 	struct uio *uiop = &uios;
74127c478bd9Sstevel@tonic-gate 	struct iovec iovs;
74137c478bd9Sstevel@tonic-gate 	int xpg4 = 0;
74147c478bd9Sstevel@tonic-gate 
74157c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
74167c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
74177c478bd9Sstevel@tonic-gate 	wqp = stp->sd_wrq;
74187c478bd9Sstevel@tonic-gate 
74197c478bd9Sstevel@tonic-gate 	/*
74207c478bd9Sstevel@tonic-gate 	 * If it is an XPG4 application, we need to send
74217c478bd9Sstevel@tonic-gate 	 * SIGPIPE below
74227c478bd9Sstevel@tonic-gate 	 */
74237c478bd9Sstevel@tonic-gate 
74247c478bd9Sstevel@tonic-gate 	xpg4 = (flag & MSG_XPG4) ? 1 : 0;
74257c478bd9Sstevel@tonic-gate 	flag &= ~MSG_XPG4;
74267c478bd9Sstevel@tonic-gate 
74277c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
74287c478bd9Sstevel@tonic-gate 	if (audit_active)
74297c478bd9Sstevel@tonic-gate 		audit_strputmsg(vp, mctl, mdata, pri, flag, fmode);
74307c478bd9Sstevel@tonic-gate #endif
74317c478bd9Sstevel@tonic-gate 
74327c478bd9Sstevel@tonic-gate 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
74337c478bd9Sstevel@tonic-gate 		if (error = straccess(stp, JCWRITE))
74347c478bd9Sstevel@tonic-gate 			return (error);
74357c478bd9Sstevel@tonic-gate 
74367c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
74377c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
74387c478bd9Sstevel@tonic-gate 		error = strwriteable(stp, B_FALSE, xpg4);
74397c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
74407c478bd9Sstevel@tonic-gate 		if (error != 0)
74417c478bd9Sstevel@tonic-gate 			return (error);
74427c478bd9Sstevel@tonic-gate 	}
74437c478bd9Sstevel@tonic-gate 
74447c478bd9Sstevel@tonic-gate 	/*
74457c478bd9Sstevel@tonic-gate 	 * Check for legal flag value.
74467c478bd9Sstevel@tonic-gate 	 */
74477c478bd9Sstevel@tonic-gate 	switch (flag) {
74487c478bd9Sstevel@tonic-gate 	case MSG_HIPRI:
74497c478bd9Sstevel@tonic-gate 		if ((mctl->len < 0) || (pri != 0))
74507c478bd9Sstevel@tonic-gate 			return (EINVAL);
74517c478bd9Sstevel@tonic-gate 		break;
74527c478bd9Sstevel@tonic-gate 	case MSG_BAND:
74537c478bd9Sstevel@tonic-gate 		break;
74547c478bd9Sstevel@tonic-gate 
74557c478bd9Sstevel@tonic-gate 	default:
74567c478bd9Sstevel@tonic-gate 		return (EINVAL);
74577c478bd9Sstevel@tonic-gate 	}
74587c478bd9Sstevel@tonic-gate 
74597c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_IN,
74607c478bd9Sstevel@tonic-gate 		"strputmsg in:stp %p", stp);
74617c478bd9Sstevel@tonic-gate 
74627c478bd9Sstevel@tonic-gate 	/* get these values from those cached in the stream head */
74637c478bd9Sstevel@tonic-gate 	rmin = stp->sd_qn_minpsz;
74647c478bd9Sstevel@tonic-gate 	rmax = stp->sd_qn_maxpsz;
74657c478bd9Sstevel@tonic-gate 
74667c478bd9Sstevel@tonic-gate 	/*
74677c478bd9Sstevel@tonic-gate 	 * Make sure ctl and data sizes together fall within the
74687c478bd9Sstevel@tonic-gate 	 * limits of the max and min receive packet sizes and do
74697c478bd9Sstevel@tonic-gate 	 * not exceed system limit.
74707c478bd9Sstevel@tonic-gate 	 */
74717c478bd9Sstevel@tonic-gate 	ASSERT((rmax >= 0) || (rmax == INFPSZ));
74727c478bd9Sstevel@tonic-gate 	if (rmax == 0) {
74737c478bd9Sstevel@tonic-gate 		return (ERANGE);
74747c478bd9Sstevel@tonic-gate 	}
74757c478bd9Sstevel@tonic-gate 	/*
74767c478bd9Sstevel@tonic-gate 	 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
74777c478bd9Sstevel@tonic-gate 	 * Needed to prevent partial failures in the strmakedata loop.
74787c478bd9Sstevel@tonic-gate 	 */
74797c478bd9Sstevel@tonic-gate 	if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
74807c478bd9Sstevel@tonic-gate 		rmax = stp->sd_maxblk;
74817c478bd9Sstevel@tonic-gate 
74827c478bd9Sstevel@tonic-gate 	if ((msgsize = mdata->len) < 0) {
74837c478bd9Sstevel@tonic-gate 		msgsize = 0;
74847c478bd9Sstevel@tonic-gate 		rmin = 0;	/* no range check for NULL data part */
74857c478bd9Sstevel@tonic-gate 	}
74867c478bd9Sstevel@tonic-gate 	if ((msgsize < rmin) ||
74877c478bd9Sstevel@tonic-gate 	    ((msgsize > rmax) && (rmax != INFPSZ)) ||
74887c478bd9Sstevel@tonic-gate 	    (mctl->len > strctlsz)) {
74897c478bd9Sstevel@tonic-gate 		return (ERANGE);
74907c478bd9Sstevel@tonic-gate 	}
74917c478bd9Sstevel@tonic-gate 
74927c478bd9Sstevel@tonic-gate 	/*
74937c478bd9Sstevel@tonic-gate 	 * Setup uio and iov for data part
74947c478bd9Sstevel@tonic-gate 	 */
74957c478bd9Sstevel@tonic-gate 	iovs.iov_base = mdata->buf;
74967c478bd9Sstevel@tonic-gate 	iovs.iov_len = msgsize;
74977c478bd9Sstevel@tonic-gate 	uios.uio_iov = &iovs;
74987c478bd9Sstevel@tonic-gate 	uios.uio_iovcnt = 1;
74997c478bd9Sstevel@tonic-gate 	uios.uio_loffset = 0;
75007c478bd9Sstevel@tonic-gate 	uios.uio_segflg = UIO_USERSPACE;
75017c478bd9Sstevel@tonic-gate 	uios.uio_fmode = fmode;
75027c478bd9Sstevel@tonic-gate 	uios.uio_extflg = UIO_COPY_DEFAULT;
75037c478bd9Sstevel@tonic-gate 	uios.uio_resid = msgsize;
75047c478bd9Sstevel@tonic-gate 	uios.uio_offset = 0;
75057c478bd9Sstevel@tonic-gate 
75067c478bd9Sstevel@tonic-gate 	/* Ignore flow control in strput for HIPRI */
75077c478bd9Sstevel@tonic-gate 	if (flag & MSG_HIPRI)
75087c478bd9Sstevel@tonic-gate 		flag |= MSG_IGNFLOW;
75097c478bd9Sstevel@tonic-gate 
75107c478bd9Sstevel@tonic-gate 	for (;;) {
75117c478bd9Sstevel@tonic-gate 		int done = 0;
75127c478bd9Sstevel@tonic-gate 
75137c478bd9Sstevel@tonic-gate 		/*
75147c478bd9Sstevel@tonic-gate 		 * strput will always free the ctl mblk - even when strput
75157c478bd9Sstevel@tonic-gate 		 * fails.
75167c478bd9Sstevel@tonic-gate 		 */
75177c478bd9Sstevel@tonic-gate 		if ((error = strmakectl(mctl, flag, fmode, &mp)) != 0) {
75187c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
75197c478bd9Sstevel@tonic-gate 				"strputmsg out:stp %p out %d error %d",
75207c478bd9Sstevel@tonic-gate 				stp, 1, error);
75217c478bd9Sstevel@tonic-gate 			return (error);
75227c478bd9Sstevel@tonic-gate 		}
75237c478bd9Sstevel@tonic-gate 		/*
75247c478bd9Sstevel@tonic-gate 		 * Verify that the whole message can be transferred by
75257c478bd9Sstevel@tonic-gate 		 * strput.
75267c478bd9Sstevel@tonic-gate 		 */
75277c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_maxblk == INFPSZ ||
75287c478bd9Sstevel@tonic-gate 			stp->sd_maxblk >= mdata->len);
75297c478bd9Sstevel@tonic-gate 
75307c478bd9Sstevel@tonic-gate 		msgsize = mdata->len;
75317c478bd9Sstevel@tonic-gate 		error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
75327c478bd9Sstevel@tonic-gate 		mdata->len = msgsize;
75337c478bd9Sstevel@tonic-gate 
75347c478bd9Sstevel@tonic-gate 		if (error == 0)
75357c478bd9Sstevel@tonic-gate 			break;
75367c478bd9Sstevel@tonic-gate 
75377c478bd9Sstevel@tonic-gate 		if (error != EWOULDBLOCK)
75387c478bd9Sstevel@tonic-gate 			goto out;
75397c478bd9Sstevel@tonic-gate 
75407c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
75417c478bd9Sstevel@tonic-gate 		/*
75427c478bd9Sstevel@tonic-gate 		 * Check for a missed wakeup.
75437c478bd9Sstevel@tonic-gate 		 * Needed since strput did not hold sd_lock across
75447c478bd9Sstevel@tonic-gate 		 * the canputnext.
75457c478bd9Sstevel@tonic-gate 		 */
75467c478bd9Sstevel@tonic-gate 		if (bcanputnext(wqp, pri)) {
75477c478bd9Sstevel@tonic-gate 			/* Try again */
75487c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
75497c478bd9Sstevel@tonic-gate 			continue;
75507c478bd9Sstevel@tonic-gate 		}
75517c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAIT,
75527c478bd9Sstevel@tonic-gate 			"strputmsg wait:stp %p waits pri %d", stp, pri);
75537c478bd9Sstevel@tonic-gate 		if (((error = strwaitq(stp, WRITEWAIT, (ssize_t)0, fmode, -1,
75547c478bd9Sstevel@tonic-gate 		    &done)) != 0) || done) {
75557c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
75567c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
75577c478bd9Sstevel@tonic-gate 				"strputmsg out:q %p out %d error %d",
75587c478bd9Sstevel@tonic-gate 				stp, 0, error);
75597c478bd9Sstevel@tonic-gate 			return (error);
75607c478bd9Sstevel@tonic-gate 		}
75617c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAKE,
75627c478bd9Sstevel@tonic-gate 			"strputmsg wake:stp %p wakes", stp);
75637c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
75647c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
75657c478bd9Sstevel@tonic-gate 			if (error = straccess(stp, JCWRITE))
75667c478bd9Sstevel@tonic-gate 				return (error);
75677c478bd9Sstevel@tonic-gate 	}
75687c478bd9Sstevel@tonic-gate out:
75697c478bd9Sstevel@tonic-gate 	/*
75707c478bd9Sstevel@tonic-gate 	 * For historic reasons, applications expect EAGAIN
75717c478bd9Sstevel@tonic-gate 	 * when data mblk could not be allocated. so change
75727c478bd9Sstevel@tonic-gate 	 * ENOMEM back to EAGAIN
75737c478bd9Sstevel@tonic-gate 	 */
75747c478bd9Sstevel@tonic-gate 	if (error == ENOMEM)
75757c478bd9Sstevel@tonic-gate 		error = EAGAIN;
75767c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
75777c478bd9Sstevel@tonic-gate 		"strputmsg out:stp %p out %d error %d", stp, 2, error);
75787c478bd9Sstevel@tonic-gate 	return (error);
75797c478bd9Sstevel@tonic-gate }
75807c478bd9Sstevel@tonic-gate 
75817c478bd9Sstevel@tonic-gate /*
75827c478bd9Sstevel@tonic-gate  * Put a message downstream.
75837c478bd9Sstevel@tonic-gate  * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
75847c478bd9Sstevel@tonic-gate  * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
75857c478bd9Sstevel@tonic-gate  * and the fmode parameter.
75867c478bd9Sstevel@tonic-gate  *
75877c478bd9Sstevel@tonic-gate  * This routine handles the consolidation private flags:
75887c478bd9Sstevel@tonic-gate  *	MSG_IGNERROR	Ignore any stream head error except STPLEX.
75897c478bd9Sstevel@tonic-gate  *	MSG_HOLDSIG	Hold signals while waiting for data.
75907c478bd9Sstevel@tonic-gate  *	MSG_IGNFLOW	Don't check streams flow control.
75917c478bd9Sstevel@tonic-gate  *
75927c478bd9Sstevel@tonic-gate  * NOTE: strputmsg and kstrputmsg have much of the logic in common.
75937c478bd9Sstevel@tonic-gate  */
75947c478bd9Sstevel@tonic-gate int
75957c478bd9Sstevel@tonic-gate kstrputmsg(
75967c478bd9Sstevel@tonic-gate 	struct vnode *vp,
75977c478bd9Sstevel@tonic-gate 	mblk_t *mctl,
75987c478bd9Sstevel@tonic-gate 	struct uio *uiop,
75997c478bd9Sstevel@tonic-gate 	ssize_t msgsize,
76007c478bd9Sstevel@tonic-gate 	unsigned char pri,
76017c478bd9Sstevel@tonic-gate 	int flag,
76027c478bd9Sstevel@tonic-gate 	int fmode)
76037c478bd9Sstevel@tonic-gate {
76047c478bd9Sstevel@tonic-gate 	struct stdata *stp;
76057c478bd9Sstevel@tonic-gate 	queue_t *wqp;
76067c478bd9Sstevel@tonic-gate 	ssize_t rmin, rmax;
76077c478bd9Sstevel@tonic-gate 	int error;
76087c478bd9Sstevel@tonic-gate 
76097c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
76107c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
76117c478bd9Sstevel@tonic-gate 	wqp = stp->sd_wrq;
76127c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
76137c478bd9Sstevel@tonic-gate 	if (audit_active)
76147c478bd9Sstevel@tonic-gate 		audit_strputmsg(vp, NULL, NULL, pri, flag, fmode);
76157c478bd9Sstevel@tonic-gate #endif
7616e4506d67Smeem 	if (mctl == NULL)
7617e4506d67Smeem 		return (EINVAL);
76187c478bd9Sstevel@tonic-gate 
76197c478bd9Sstevel@tonic-gate 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO) {
76207c478bd9Sstevel@tonic-gate 		if (error = straccess(stp, JCWRITE)) {
76217c478bd9Sstevel@tonic-gate 			freemsg(mctl);
76227c478bd9Sstevel@tonic-gate 			return (error);
76237c478bd9Sstevel@tonic-gate 		}
76247c478bd9Sstevel@tonic-gate 	}
76257c478bd9Sstevel@tonic-gate 
76267c478bd9Sstevel@tonic-gate 	if ((stp->sd_flag & STPLEX) || !(flag & MSG_IGNERROR)) {
76277c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
76287c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
76297c478bd9Sstevel@tonic-gate 			error = strwriteable(stp, B_FALSE, B_TRUE);
76307c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
76317c478bd9Sstevel@tonic-gate 			if (error != 0) {
76327c478bd9Sstevel@tonic-gate 				freemsg(mctl);
76337c478bd9Sstevel@tonic-gate 				return (error);
76347c478bd9Sstevel@tonic-gate 			}
76357c478bd9Sstevel@tonic-gate 		}
76367c478bd9Sstevel@tonic-gate 	}
76377c478bd9Sstevel@tonic-gate 
76387c478bd9Sstevel@tonic-gate 	/*
76397c478bd9Sstevel@tonic-gate 	 * Check for legal flag value.
76407c478bd9Sstevel@tonic-gate 	 */
76417c478bd9Sstevel@tonic-gate 	switch (flag & (MSG_HIPRI|MSG_BAND|MSG_ANY)) {
76427c478bd9Sstevel@tonic-gate 	case MSG_HIPRI:
7643e4506d67Smeem 		if (pri != 0) {
76447c478bd9Sstevel@tonic-gate 			freemsg(mctl);
76457c478bd9Sstevel@tonic-gate 			return (EINVAL);
76467c478bd9Sstevel@tonic-gate 		}
76477c478bd9Sstevel@tonic-gate 		break;
76487c478bd9Sstevel@tonic-gate 	case MSG_BAND:
76497c478bd9Sstevel@tonic-gate 		break;
76507c478bd9Sstevel@tonic-gate 	default:
76517c478bd9Sstevel@tonic-gate 		freemsg(mctl);
76527c478bd9Sstevel@tonic-gate 		return (EINVAL);
76537c478bd9Sstevel@tonic-gate 	}
76547c478bd9Sstevel@tonic-gate 
76557c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_IN,
76567c478bd9Sstevel@tonic-gate 		"kstrputmsg in:stp %p", stp);
76577c478bd9Sstevel@tonic-gate 
76587c478bd9Sstevel@tonic-gate 	/* get these values from those cached in the stream head */
76597c478bd9Sstevel@tonic-gate 	rmin = stp->sd_qn_minpsz;
76607c478bd9Sstevel@tonic-gate 	rmax = stp->sd_qn_maxpsz;
76617c478bd9Sstevel@tonic-gate 
76627c478bd9Sstevel@tonic-gate 	/*
76637c478bd9Sstevel@tonic-gate 	 * Make sure ctl and data sizes together fall within the
76647c478bd9Sstevel@tonic-gate 	 * limits of the max and min receive packet sizes and do
76657c478bd9Sstevel@tonic-gate 	 * not exceed system limit.
76667c478bd9Sstevel@tonic-gate 	 */
76677c478bd9Sstevel@tonic-gate 	ASSERT((rmax >= 0) || (rmax == INFPSZ));
76687c478bd9Sstevel@tonic-gate 	if (rmax == 0) {
76697c478bd9Sstevel@tonic-gate 		freemsg(mctl);
76707c478bd9Sstevel@tonic-gate 		return (ERANGE);
76717c478bd9Sstevel@tonic-gate 	}
76727c478bd9Sstevel@tonic-gate 	/*
76737c478bd9Sstevel@tonic-gate 	 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
76747c478bd9Sstevel@tonic-gate 	 * Needed to prevent partial failures in the strmakedata loop.
76757c478bd9Sstevel@tonic-gate 	 */
76767c478bd9Sstevel@tonic-gate 	if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
76777c478bd9Sstevel@tonic-gate 		rmax = stp->sd_maxblk;
76787c478bd9Sstevel@tonic-gate 
76797c478bd9Sstevel@tonic-gate 	if (uiop == NULL) {
76807c478bd9Sstevel@tonic-gate 		msgsize = -1;
76817c478bd9Sstevel@tonic-gate 		rmin = -1;	/* no range check for NULL data part */
76827c478bd9Sstevel@tonic-gate 	} else {
76837c478bd9Sstevel@tonic-gate 		/* Use uio flags as well as the fmode parameter flags */
76847c478bd9Sstevel@tonic-gate 		fmode |= uiop->uio_fmode;
76857c478bd9Sstevel@tonic-gate 
76867c478bd9Sstevel@tonic-gate 		if ((msgsize < rmin) ||
76877c478bd9Sstevel@tonic-gate 		    ((msgsize > rmax) && (rmax != INFPSZ))) {
76887c478bd9Sstevel@tonic-gate 			freemsg(mctl);
76897c478bd9Sstevel@tonic-gate 			return (ERANGE);
76907c478bd9Sstevel@tonic-gate 		}
76917c478bd9Sstevel@tonic-gate 	}
76927c478bd9Sstevel@tonic-gate 
76937c478bd9Sstevel@tonic-gate 	/* Ignore flow control in strput for HIPRI */
76947c478bd9Sstevel@tonic-gate 	if (flag & MSG_HIPRI)
76957c478bd9Sstevel@tonic-gate 		flag |= MSG_IGNFLOW;
76967c478bd9Sstevel@tonic-gate 
76977c478bd9Sstevel@tonic-gate 	for (;;) {
76987c478bd9Sstevel@tonic-gate 		int done = 0;
76997c478bd9Sstevel@tonic-gate 		int waitflag;
77007c478bd9Sstevel@tonic-gate 		mblk_t *mp;
77017c478bd9Sstevel@tonic-gate 
77027c478bd9Sstevel@tonic-gate 		/*
77037c478bd9Sstevel@tonic-gate 		 * strput will always free the ctl mblk - even when strput
77047c478bd9Sstevel@tonic-gate 		 * fails. If MSG_IGNFLOW is set then any error returned
77057c478bd9Sstevel@tonic-gate 		 * will cause us to break the loop, so we don't need a copy
77067c478bd9Sstevel@tonic-gate 		 * of the message. If MSG_IGNFLOW is not set, then we can
77077c478bd9Sstevel@tonic-gate 		 * get hit by flow control and be forced to try again. In
77087c478bd9Sstevel@tonic-gate 		 * this case we need to have a copy of the message. We
77097c478bd9Sstevel@tonic-gate 		 * do this using copymsg since the message may get modified
77107c478bd9Sstevel@tonic-gate 		 * by something below us.
77117c478bd9Sstevel@tonic-gate 		 *
77127c478bd9Sstevel@tonic-gate 		 * We've observed that many TPI providers do not check db_ref
77137c478bd9Sstevel@tonic-gate 		 * on the control messages but blindly reuse them for the
77147c478bd9Sstevel@tonic-gate 		 * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
77157c478bd9Sstevel@tonic-gate 		 * friendly to such providers than using dupmsg. Also, note
77167c478bd9Sstevel@tonic-gate 		 * that sockfs uses MSG_IGNFLOW for all TPI control messages.
77177c478bd9Sstevel@tonic-gate 		 * Only data messages are subject to flow control, hence
77187c478bd9Sstevel@tonic-gate 		 * subject to this copymsg.
77197c478bd9Sstevel@tonic-gate 		 */
77207c478bd9Sstevel@tonic-gate 		if (flag & MSG_IGNFLOW) {
77217c478bd9Sstevel@tonic-gate 			mp = mctl;
77227c478bd9Sstevel@tonic-gate 			mctl = NULL;
77237c478bd9Sstevel@tonic-gate 		} else {
77247c478bd9Sstevel@tonic-gate 			do {
77257c478bd9Sstevel@tonic-gate 				/*
77267c478bd9Sstevel@tonic-gate 				 * If a message has a free pointer, the message
77277c478bd9Sstevel@tonic-gate 				 * must be dupmsg to maintain this pointer.
77287c478bd9Sstevel@tonic-gate 				 * Code using this facility must be sure
77297c478bd9Sstevel@tonic-gate 				 * that modules below will not change the
77307c478bd9Sstevel@tonic-gate 				 * contents of the dblk without checking db_ref
77317c478bd9Sstevel@tonic-gate 				 * first. If db_ref is > 1, then the module
77327c478bd9Sstevel@tonic-gate 				 * needs to do a copymsg first. Otherwise,
77337c478bd9Sstevel@tonic-gate 				 * the contents of the dblk may become
77347c478bd9Sstevel@tonic-gate 				 * inconsistent because the freesmg/freeb below
77357c478bd9Sstevel@tonic-gate 				 * may end up calling atomic_add_32_nv.
77367c478bd9Sstevel@tonic-gate 				 * The atomic_add_32_nv in freeb (accessing
77377c478bd9Sstevel@tonic-gate 				 * all of db_ref, db_type, db_flags, and
77387c478bd9Sstevel@tonic-gate 				 * db_struioflag) does not prevent other threads
77397c478bd9Sstevel@tonic-gate 				 * from concurrently trying to modify e.g.
77407c478bd9Sstevel@tonic-gate 				 * db_type.
77417c478bd9Sstevel@tonic-gate 				 */
77427c478bd9Sstevel@tonic-gate 				if (mctl->b_datap->db_frtnp != NULL)
77437c478bd9Sstevel@tonic-gate 					mp = dupmsg(mctl);
77447c478bd9Sstevel@tonic-gate 				else
77457c478bd9Sstevel@tonic-gate 					mp = copymsg(mctl);
77467c478bd9Sstevel@tonic-gate 
7747e4506d67Smeem 				if (mp != NULL)
77487c478bd9Sstevel@tonic-gate 					break;
77497c478bd9Sstevel@tonic-gate 
77507c478bd9Sstevel@tonic-gate 				error = strwaitbuf(msgdsize(mctl), BPRI_MED);
77517c478bd9Sstevel@tonic-gate 				if (error) {
77527c478bd9Sstevel@tonic-gate 					freemsg(mctl);
77537c478bd9Sstevel@tonic-gate 					return (error);
77547c478bd9Sstevel@tonic-gate 				}
77557c478bd9Sstevel@tonic-gate 			} while (mp == NULL);
77567c478bd9Sstevel@tonic-gate 		}
77577c478bd9Sstevel@tonic-gate 		/*
77587c478bd9Sstevel@tonic-gate 		 * Verify that all of msgsize can be transferred by
77597c478bd9Sstevel@tonic-gate 		 * strput.
77607c478bd9Sstevel@tonic-gate 		 */
7761e4506d67Smeem 		ASSERT(stp->sd_maxblk == INFPSZ || stp->sd_maxblk >= msgsize);
77627c478bd9Sstevel@tonic-gate 		error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
77637c478bd9Sstevel@tonic-gate 		if (error == 0)
77647c478bd9Sstevel@tonic-gate 			break;
77657c478bd9Sstevel@tonic-gate 
77667c478bd9Sstevel@tonic-gate 		if (error != EWOULDBLOCK)
77677c478bd9Sstevel@tonic-gate 			goto out;
77687c478bd9Sstevel@tonic-gate 
77697c478bd9Sstevel@tonic-gate 		/*
77707c478bd9Sstevel@tonic-gate 		 * IF MSG_IGNFLOW is set we should have broken out of loop
77717c478bd9Sstevel@tonic-gate 		 * above.
77727c478bd9Sstevel@tonic-gate 		 */
77737c478bd9Sstevel@tonic-gate 		ASSERT(!(flag & MSG_IGNFLOW));
77747c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
77757c478bd9Sstevel@tonic-gate 		/*
77767c478bd9Sstevel@tonic-gate 		 * Check for a missed wakeup.
77777c478bd9Sstevel@tonic-gate 		 * Needed since strput did not hold sd_lock across
77787c478bd9Sstevel@tonic-gate 		 * the canputnext.
77797c478bd9Sstevel@tonic-gate 		 */
77807c478bd9Sstevel@tonic-gate 		if (bcanputnext(wqp, pri)) {
77817c478bd9Sstevel@tonic-gate 			/* Try again */
77827c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
77837c478bd9Sstevel@tonic-gate 			continue;
77847c478bd9Sstevel@tonic-gate 		}
77857c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAIT,
77867c478bd9Sstevel@tonic-gate 			"kstrputmsg wait:stp %p waits pri %d", stp, pri);
77877c478bd9Sstevel@tonic-gate 
77887c478bd9Sstevel@tonic-gate 		waitflag = WRITEWAIT;
77897c478bd9Sstevel@tonic-gate 		if (flag & (MSG_HOLDSIG|MSG_IGNERROR)) {
77907c478bd9Sstevel@tonic-gate 			if (flag & MSG_HOLDSIG)
77917c478bd9Sstevel@tonic-gate 				waitflag |= STR_NOSIG;
77927c478bd9Sstevel@tonic-gate 			if (flag & MSG_IGNERROR)
77937c478bd9Sstevel@tonic-gate 				waitflag |= STR_NOERROR;
77947c478bd9Sstevel@tonic-gate 		}
77957c478bd9Sstevel@tonic-gate 		if (((error = strwaitq(stp, waitflag,
77967c478bd9Sstevel@tonic-gate 		    (ssize_t)0, fmode, -1, &done)) != 0) || done) {
77977c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
77987c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
77997c478bd9Sstevel@tonic-gate 				"kstrputmsg out:stp %p out %d error %d",
78007c478bd9Sstevel@tonic-gate 				stp, 0, error);
78017c478bd9Sstevel@tonic-gate 			freemsg(mctl);
78027c478bd9Sstevel@tonic-gate 			return (error);
78037c478bd9Sstevel@tonic-gate 		}
78047c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAKE,
78057c478bd9Sstevel@tonic-gate 			"kstrputmsg wake:stp %p wakes", stp);
78067c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
78077c478bd9Sstevel@tonic-gate 		if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO) {
78087c478bd9Sstevel@tonic-gate 			if (error = straccess(stp, JCWRITE)) {
78097c478bd9Sstevel@tonic-gate 				freemsg(mctl);
78107c478bd9Sstevel@tonic-gate 				return (error);
78117c478bd9Sstevel@tonic-gate 			}
78127c478bd9Sstevel@tonic-gate 		}
78137c478bd9Sstevel@tonic-gate 	}
78147c478bd9Sstevel@tonic-gate out:
78157c478bd9Sstevel@tonic-gate 	freemsg(mctl);
78167c478bd9Sstevel@tonic-gate 	/*
78177c478bd9Sstevel@tonic-gate 	 * For historic reasons, applications expect EAGAIN
78187c478bd9Sstevel@tonic-gate 	 * when data mblk could not be allocated. so change
78197c478bd9Sstevel@tonic-gate 	 * ENOMEM back to EAGAIN
78207c478bd9Sstevel@tonic-gate 	 */
78217c478bd9Sstevel@tonic-gate 	if (error == ENOMEM)
78227c478bd9Sstevel@tonic-gate 		error = EAGAIN;
78237c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
78247c478bd9Sstevel@tonic-gate 		"kstrputmsg out:stp %p out %d error %d", stp, 2, error);
78257c478bd9Sstevel@tonic-gate 	return (error);
78267c478bd9Sstevel@tonic-gate }
78277c478bd9Sstevel@tonic-gate 
78287c478bd9Sstevel@tonic-gate /*
78297c478bd9Sstevel@tonic-gate  * Determines whether the necessary conditions are set on a stream
78307c478bd9Sstevel@tonic-gate  * for it to be readable, writeable, or have exceptions.
78317c478bd9Sstevel@tonic-gate  *
78327c478bd9Sstevel@tonic-gate  * strpoll handles the consolidation private events:
78337c478bd9Sstevel@tonic-gate  *	POLLNOERR	Do not return POLLERR even if there are stream
78347c478bd9Sstevel@tonic-gate  *			head errors.
78357c478bd9Sstevel@tonic-gate  *			Used by sockfs.
78367c478bd9Sstevel@tonic-gate  *	POLLRDDATA	Do not return POLLIN unless at least one message on
78377c478bd9Sstevel@tonic-gate  *			the queue contains one or more M_DATA mblks. Thus
78387c478bd9Sstevel@tonic-gate  *			when this flag is set a queue with only
78397c478bd9Sstevel@tonic-gate  *			M_PROTO/M_PCPROTO mblks does not return POLLIN.
78407c478bd9Sstevel@tonic-gate  *			Used by sockfs to ignore T_EXDATA_IND messages.
78417c478bd9Sstevel@tonic-gate  *
78427c478bd9Sstevel@tonic-gate  * Note: POLLRDDATA assumes that synch streams only return messages with
78437c478bd9Sstevel@tonic-gate  * an M_DATA attached (i.e. not messages consisting of only
78447c478bd9Sstevel@tonic-gate  * an M_PROTO/M_PCPROTO part).
78457c478bd9Sstevel@tonic-gate  */
78467c478bd9Sstevel@tonic-gate int
78477c478bd9Sstevel@tonic-gate strpoll(
78487c478bd9Sstevel@tonic-gate 	struct stdata *stp,
78497c478bd9Sstevel@tonic-gate 	short events_arg,
78507c478bd9Sstevel@tonic-gate 	int anyyet,
78517c478bd9Sstevel@tonic-gate 	short *reventsp,
78527c478bd9Sstevel@tonic-gate 	struct pollhead **phpp)
78537c478bd9Sstevel@tonic-gate {
78547c478bd9Sstevel@tonic-gate 	int events = (ushort_t)events_arg;
78557c478bd9Sstevel@tonic-gate 	int retevents = 0;
78567c478bd9Sstevel@tonic-gate 	mblk_t *mp;
78577c478bd9Sstevel@tonic-gate 	qband_t *qbp;
78587c478bd9Sstevel@tonic-gate 	long sd_flags = stp->sd_flag;
78597c478bd9Sstevel@tonic-gate 	int headlocked = 0;
78607c478bd9Sstevel@tonic-gate 
78617c478bd9Sstevel@tonic-gate 	/*
78627c478bd9Sstevel@tonic-gate 	 * For performance, a single 'if' tests for most possible edge
78637c478bd9Sstevel@tonic-gate 	 * conditions in one shot
78647c478bd9Sstevel@tonic-gate 	 */
78657c478bd9Sstevel@tonic-gate 	if (sd_flags & (STPLEX | STRDERR | STWRERR)) {
78667c478bd9Sstevel@tonic-gate 		if (sd_flags & STPLEX) {
78677c478bd9Sstevel@tonic-gate 			*reventsp = POLLNVAL;
78687c478bd9Sstevel@tonic-gate 			return (EINVAL);
78697c478bd9Sstevel@tonic-gate 		}
78707c478bd9Sstevel@tonic-gate 		if (((events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) &&
78717c478bd9Sstevel@tonic-gate 		    (sd_flags & STRDERR)) ||
78727c478bd9Sstevel@tonic-gate 		    ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) &&
78737c478bd9Sstevel@tonic-gate 		    (sd_flags & STWRERR))) {
78747c478bd9Sstevel@tonic-gate 			if (!(events & POLLNOERR)) {
78757c478bd9Sstevel@tonic-gate 				*reventsp = POLLERR;
78767c478bd9Sstevel@tonic-gate 				return (0);
78777c478bd9Sstevel@tonic-gate 			}
78787c478bd9Sstevel@tonic-gate 		}
78797c478bd9Sstevel@tonic-gate 	}
78807c478bd9Sstevel@tonic-gate 	if (sd_flags & STRHUP) {
78817c478bd9Sstevel@tonic-gate 		retevents |= POLLHUP;
78827c478bd9Sstevel@tonic-gate 	} else if (events & (POLLWRNORM | POLLWRBAND)) {
78837c478bd9Sstevel@tonic-gate 		queue_t *tq;
78847c478bd9Sstevel@tonic-gate 		queue_t	*qp = stp->sd_wrq;
78857c478bd9Sstevel@tonic-gate 
78867c478bd9Sstevel@tonic-gate 		claimstr(qp);
78877c478bd9Sstevel@tonic-gate 		/* Find next module forward that has a service procedure */
78887c478bd9Sstevel@tonic-gate 		tq = qp->q_next->q_nfsrv;
78897c478bd9Sstevel@tonic-gate 		ASSERT(tq != NULL);
78907c478bd9Sstevel@tonic-gate 
78917c478bd9Sstevel@tonic-gate 		polllock(&stp->sd_pollist, QLOCK(tq));
78927c478bd9Sstevel@tonic-gate 		if (events & POLLWRNORM) {
78937c478bd9Sstevel@tonic-gate 			queue_t *sqp;
78947c478bd9Sstevel@tonic-gate 
78957c478bd9Sstevel@tonic-gate 			if (tq->q_flag & QFULL)
78967c478bd9Sstevel@tonic-gate 				/* ensure backq svc procedure runs */
78977c478bd9Sstevel@tonic-gate 				tq->q_flag |= QWANTW;
78987c478bd9Sstevel@tonic-gate 			else if ((sqp = stp->sd_struiowrq) != NULL) {
78997c478bd9Sstevel@tonic-gate 				/* Check sync stream barrier write q */
79007c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(tq));
79017c478bd9Sstevel@tonic-gate 				polllock(&stp->sd_pollist, QLOCK(sqp));
79027c478bd9Sstevel@tonic-gate 				if (sqp->q_flag & QFULL)
79037c478bd9Sstevel@tonic-gate 					/* ensure pollwakeup() is done */
79047c478bd9Sstevel@tonic-gate 					sqp->q_flag |= QWANTWSYNC;
79057c478bd9Sstevel@tonic-gate 				else
79067c478bd9Sstevel@tonic-gate 					retevents |= POLLOUT;
79077c478bd9Sstevel@tonic-gate 				/* More write events to process ??? */
79087c478bd9Sstevel@tonic-gate 				if (! (events & POLLWRBAND)) {
79097c478bd9Sstevel@tonic-gate 					mutex_exit(QLOCK(sqp));
79107c478bd9Sstevel@tonic-gate 					releasestr(qp);
79117c478bd9Sstevel@tonic-gate 					goto chkrd;
79127c478bd9Sstevel@tonic-gate 				}
79137c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(sqp));
79147c478bd9Sstevel@tonic-gate 				polllock(&stp->sd_pollist, QLOCK(tq));
79157c478bd9Sstevel@tonic-gate 			} else
79167c478bd9Sstevel@tonic-gate 				retevents |= POLLOUT;
79177c478bd9Sstevel@tonic-gate 		}
79187c478bd9Sstevel@tonic-gate 		if (events & POLLWRBAND) {
79197c478bd9Sstevel@tonic-gate 			qbp = tq->q_bandp;
79207c478bd9Sstevel@tonic-gate 			if (qbp) {
79217c478bd9Sstevel@tonic-gate 				while (qbp) {
79227c478bd9Sstevel@tonic-gate 					if (qbp->qb_flag & QB_FULL)
79237c478bd9Sstevel@tonic-gate 						qbp->qb_flag |= QB_WANTW;
79247c478bd9Sstevel@tonic-gate 					else
79257c478bd9Sstevel@tonic-gate 						retevents |= POLLWRBAND;
79267c478bd9Sstevel@tonic-gate 					qbp = qbp->qb_next;
79277c478bd9Sstevel@tonic-gate 				}
79287c478bd9Sstevel@tonic-gate 			} else {
79297c478bd9Sstevel@tonic-gate 				retevents |= POLLWRBAND;
79307c478bd9Sstevel@tonic-gate 			}
79317c478bd9Sstevel@tonic-gate 		}
79327c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(tq));
79337c478bd9Sstevel@tonic-gate 		releasestr(qp);
79347c478bd9Sstevel@tonic-gate 	}
79357c478bd9Sstevel@tonic-gate chkrd:
79367c478bd9Sstevel@tonic-gate 	if (sd_flags & STRPRI) {
79377c478bd9Sstevel@tonic-gate 		retevents |= (events & POLLPRI);
79387c478bd9Sstevel@tonic-gate 	} else if (events & (POLLRDNORM | POLLRDBAND | POLLIN)) {
79397c478bd9Sstevel@tonic-gate 		queue_t	*qp = _RD(stp->sd_wrq);
79407c478bd9Sstevel@tonic-gate 		int normevents = (events & (POLLIN | POLLRDNORM));
79417c478bd9Sstevel@tonic-gate 
79427c478bd9Sstevel@tonic-gate 		/*
79437c478bd9Sstevel@tonic-gate 		 * Note: Need to do polllock() here since ps_lock may be
79447c478bd9Sstevel@tonic-gate 		 * held. See bug 4191544.
79457c478bd9Sstevel@tonic-gate 		 */
79467c478bd9Sstevel@tonic-gate 		polllock(&stp->sd_pollist, &stp->sd_lock);
79477c478bd9Sstevel@tonic-gate 		headlocked = 1;
79487c478bd9Sstevel@tonic-gate 		mp = qp->q_first;
79497c478bd9Sstevel@tonic-gate 		while (mp) {
79507c478bd9Sstevel@tonic-gate 			/*
79517c478bd9Sstevel@tonic-gate 			 * For POLLRDDATA we scan b_cont and b_next until we
79527c478bd9Sstevel@tonic-gate 			 * find an M_DATA.
79537c478bd9Sstevel@tonic-gate 			 */
79547c478bd9Sstevel@tonic-gate 			if ((events & POLLRDDATA) &&
79557c478bd9Sstevel@tonic-gate 			    mp->b_datap->db_type != M_DATA) {
79567c478bd9Sstevel@tonic-gate 				mblk_t *nmp = mp->b_cont;
79577c478bd9Sstevel@tonic-gate 
79587c478bd9Sstevel@tonic-gate 				while (nmp != NULL &&
79597c478bd9Sstevel@tonic-gate 				    nmp->b_datap->db_type != M_DATA)
79607c478bd9Sstevel@tonic-gate 					nmp = nmp->b_cont;
79617c478bd9Sstevel@tonic-gate 				if (nmp == NULL) {
79627c478bd9Sstevel@tonic-gate 					mp = mp->b_next;
79637c478bd9Sstevel@tonic-gate 					continue;
79647c478bd9Sstevel@tonic-gate 				}
79657c478bd9Sstevel@tonic-gate 			}
79667c478bd9Sstevel@tonic-gate 			if (mp->b_band == 0)
79677c478bd9Sstevel@tonic-gate 				retevents |= normevents;
79687c478bd9Sstevel@tonic-gate 			else
79697c478bd9Sstevel@tonic-gate 				retevents |= (events & (POLLIN | POLLRDBAND));
79707c478bd9Sstevel@tonic-gate 			break;
79717c478bd9Sstevel@tonic-gate 		}
79727c478bd9Sstevel@tonic-gate 		if (! (retevents & normevents) &&
79737c478bd9Sstevel@tonic-gate 		    (stp->sd_wakeq & RSLEEP)) {
79747c478bd9Sstevel@tonic-gate 			/*
79757c478bd9Sstevel@tonic-gate 			 * Sync stream barrier read queue has data.
79767c478bd9Sstevel@tonic-gate 			 */
79777c478bd9Sstevel@tonic-gate 			retevents |= normevents;
79787c478bd9Sstevel@tonic-gate 		}
79797c478bd9Sstevel@tonic-gate 		/* Treat eof as normal data */
79807c478bd9Sstevel@tonic-gate 		if (sd_flags & STREOF)
79817c478bd9Sstevel@tonic-gate 			retevents |= normevents;
79827c478bd9Sstevel@tonic-gate 	}
79837c478bd9Sstevel@tonic-gate 
79847c478bd9Sstevel@tonic-gate 	*reventsp = (short)retevents;
79857c478bd9Sstevel@tonic-gate 	if (retevents) {
79867c478bd9Sstevel@tonic-gate 		if (headlocked)
79877c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
79887c478bd9Sstevel@tonic-gate 		return (0);
79897c478bd9Sstevel@tonic-gate 	}
79907c478bd9Sstevel@tonic-gate 
79917c478bd9Sstevel@tonic-gate 	/*
79927c478bd9Sstevel@tonic-gate 	 * If poll() has not found any events yet, set up event cell
79937c478bd9Sstevel@tonic-gate 	 * to wake up the poll if a requested event occurs on this
79947c478bd9Sstevel@tonic-gate 	 * stream.  Check for collisions with outstanding poll requests.
79957c478bd9Sstevel@tonic-gate 	 */
79967c478bd9Sstevel@tonic-gate 	if (!anyyet) {
79977c478bd9Sstevel@tonic-gate 		*phpp = &stp->sd_pollist;
79987c478bd9Sstevel@tonic-gate 		if (headlocked == 0) {
79997c478bd9Sstevel@tonic-gate 			polllock(&stp->sd_pollist, &stp->sd_lock);
80007c478bd9Sstevel@tonic-gate 			headlocked = 1;
80017c478bd9Sstevel@tonic-gate 		}
80027c478bd9Sstevel@tonic-gate 		stp->sd_rput_opt |= SR_POLLIN;
80037c478bd9Sstevel@tonic-gate 	}
80047c478bd9Sstevel@tonic-gate 	if (headlocked)
80057c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
80067c478bd9Sstevel@tonic-gate 	return (0);
80077c478bd9Sstevel@tonic-gate }
80087c478bd9Sstevel@tonic-gate 
80097c478bd9Sstevel@tonic-gate /*
80107c478bd9Sstevel@tonic-gate  * The purpose of putback() is to assure sleeping polls/reads
80117c478bd9Sstevel@tonic-gate  * are awakened when there are no new messages arriving at the,
80127c478bd9Sstevel@tonic-gate  * stream head, and a message is placed back on the read queue.
80137c478bd9Sstevel@tonic-gate  *
80147c478bd9Sstevel@tonic-gate  * sd_lock must be held when messages are placed back on stream
80157c478bd9Sstevel@tonic-gate  * head.  (getq() holds sd_lock when it removes messages from
80167c478bd9Sstevel@tonic-gate  * the queue)
80177c478bd9Sstevel@tonic-gate  */
80187c478bd9Sstevel@tonic-gate 
80197c478bd9Sstevel@tonic-gate static void
80207c478bd9Sstevel@tonic-gate putback(struct stdata *stp, queue_t *q, mblk_t *bp, int band)
80217c478bd9Sstevel@tonic-gate {
80227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
80237c478bd9Sstevel@tonic-gate 	(void) putbq(q, bp);
80247c478bd9Sstevel@tonic-gate 	/*
80257c478bd9Sstevel@tonic-gate 	 * A message may have come in when the sd_lock was dropped in the
80267c478bd9Sstevel@tonic-gate 	 * calling routine. If this is the case and STR*ATMARK info was
80277c478bd9Sstevel@tonic-gate 	 * received, need to move that from the stream head to the q_last
80287c478bd9Sstevel@tonic-gate 	 * so that SIOCATMARK can return the proper value.
80297c478bd9Sstevel@tonic-gate 	 */
80307c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STRATMARK | STRNOTATMARK)) {
80317c478bd9Sstevel@tonic-gate 		unsigned short *flagp = &q->q_last->b_flag;
80327c478bd9Sstevel@tonic-gate 		uint_t b_flag = (uint_t)*flagp;
80337c478bd9Sstevel@tonic-gate 
80347c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRATMARK) {
80357c478bd9Sstevel@tonic-gate 			b_flag &= ~MSGNOTMARKNEXT;
80367c478bd9Sstevel@tonic-gate 			b_flag |= MSGMARKNEXT;
80377c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRATMARK;
80387c478bd9Sstevel@tonic-gate 		} else {
80397c478bd9Sstevel@tonic-gate 			b_flag &= ~MSGMARKNEXT;
80407c478bd9Sstevel@tonic-gate 			b_flag |= MSGNOTMARKNEXT;
80417c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRNOTATMARK;
80427c478bd9Sstevel@tonic-gate 		}
80437c478bd9Sstevel@tonic-gate 		*flagp = (unsigned short) b_flag;
80447c478bd9Sstevel@tonic-gate 	}
80457c478bd9Sstevel@tonic-gate 
80467c478bd9Sstevel@tonic-gate #ifdef	DEBUG
80477c478bd9Sstevel@tonic-gate 	/*
80487c478bd9Sstevel@tonic-gate 	 * Make sure that the flags are not messed up.
80497c478bd9Sstevel@tonic-gate 	 */
80507c478bd9Sstevel@tonic-gate 	{
80517c478bd9Sstevel@tonic-gate 		mblk_t *mp;
80527c478bd9Sstevel@tonic-gate 		mp = q->q_last;
80537c478bd9Sstevel@tonic-gate 		while (mp != NULL) {
80547c478bd9Sstevel@tonic-gate 			ASSERT((mp->b_flag & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
80557c478bd9Sstevel@tonic-gate 			    (MSGMARKNEXT|MSGNOTMARKNEXT));
80567c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
80577c478bd9Sstevel@tonic-gate 		}
80587c478bd9Sstevel@tonic-gate 	}
80597c478bd9Sstevel@tonic-gate #endif
80607c478bd9Sstevel@tonic-gate 	if (q->q_first == bp) {
80617c478bd9Sstevel@tonic-gate 		short pollevents;
80627c478bd9Sstevel@tonic-gate 
80637c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & RSLEEP) {
80647c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~RSLEEP;
80657c478bd9Sstevel@tonic-gate 			cv_broadcast(&q->q_wait);
80667c478bd9Sstevel@tonic-gate 		}
80677c478bd9Sstevel@tonic-gate 		if (stp->sd_flag & STRPRI) {
80687c478bd9Sstevel@tonic-gate 			pollevents = POLLPRI;
80697c478bd9Sstevel@tonic-gate 		} else {
80707c478bd9Sstevel@tonic-gate 			if (band == 0) {
80717c478bd9Sstevel@tonic-gate 				if (!(stp->sd_rput_opt & SR_POLLIN))
80727c478bd9Sstevel@tonic-gate 					return;
80737c478bd9Sstevel@tonic-gate 				stp->sd_rput_opt &= ~SR_POLLIN;
80747c478bd9Sstevel@tonic-gate 				pollevents = POLLIN | POLLRDNORM;
80757c478bd9Sstevel@tonic-gate 			} else {
80767c478bd9Sstevel@tonic-gate 				pollevents = POLLIN | POLLRDBAND;
80777c478bd9Sstevel@tonic-gate 			}
80787c478bd9Sstevel@tonic-gate 		}
80797c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
80807c478bd9Sstevel@tonic-gate 		pollwakeup(&stp->sd_pollist, pollevents);
80817c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
80827c478bd9Sstevel@tonic-gate 	}
80837c478bd9Sstevel@tonic-gate }
80847c478bd9Sstevel@tonic-gate 
80857c478bd9Sstevel@tonic-gate /*
80867c478bd9Sstevel@tonic-gate  * Return the held vnode attached to the stream head of a
80877c478bd9Sstevel@tonic-gate  * given queue
80887c478bd9Sstevel@tonic-gate  * It is the responsibility of the calling routine to ensure
80897c478bd9Sstevel@tonic-gate  * that the queue does not go away (e.g. pop).
80907c478bd9Sstevel@tonic-gate  */
80917c478bd9Sstevel@tonic-gate vnode_t *
80927c478bd9Sstevel@tonic-gate strq2vp(queue_t *qp)
80937c478bd9Sstevel@tonic-gate {
80947c478bd9Sstevel@tonic-gate 	vnode_t *vp;
80957c478bd9Sstevel@tonic-gate 	vp = STREAM(qp)->sd_vnode;
80967c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
80977c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
80987c478bd9Sstevel@tonic-gate 	return (vp);
80997c478bd9Sstevel@tonic-gate }
81007c478bd9Sstevel@tonic-gate 
81017c478bd9Sstevel@tonic-gate /*
81027c478bd9Sstevel@tonic-gate  * return the stream head write queue for the given vp
81037c478bd9Sstevel@tonic-gate  * It is the responsibility of the calling routine to ensure
81047c478bd9Sstevel@tonic-gate  * that the stream or vnode do not close.
81057c478bd9Sstevel@tonic-gate  */
81067c478bd9Sstevel@tonic-gate queue_t *
81077c478bd9Sstevel@tonic-gate strvp2wq(vnode_t *vp)
81087c478bd9Sstevel@tonic-gate {
81097c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream != NULL);
81107c478bd9Sstevel@tonic-gate 	return (vp->v_stream->sd_wrq);
81117c478bd9Sstevel@tonic-gate }
81127c478bd9Sstevel@tonic-gate 
81137c478bd9Sstevel@tonic-gate /*
81147c478bd9Sstevel@tonic-gate  * pollwakeup stream head
81157c478bd9Sstevel@tonic-gate  * It is the responsibility of the calling routine to ensure
81167c478bd9Sstevel@tonic-gate  * that the stream or vnode do not close.
81177c478bd9Sstevel@tonic-gate  */
81187c478bd9Sstevel@tonic-gate void
81197c478bd9Sstevel@tonic-gate strpollwakeup(vnode_t *vp, short event)
81207c478bd9Sstevel@tonic-gate {
81217c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
81227c478bd9Sstevel@tonic-gate 	pollwakeup(&vp->v_stream->sd_pollist, event);
81237c478bd9Sstevel@tonic-gate }
81247c478bd9Sstevel@tonic-gate 
81257c478bd9Sstevel@tonic-gate /*
81267c478bd9Sstevel@tonic-gate  * Mate the stream heads of two vnodes together. If the two vnodes are the
81277c478bd9Sstevel@tonic-gate  * same, we just make the write-side point at the read-side -- otherwise,
81287c478bd9Sstevel@tonic-gate  * we do a full mate.  Only works on vnodes associated with streams that are
81297c478bd9Sstevel@tonic-gate  * still being built and thus have only a stream head.
81307c478bd9Sstevel@tonic-gate  */
81317c478bd9Sstevel@tonic-gate void
81327c478bd9Sstevel@tonic-gate strmate(vnode_t *vp1, vnode_t *vp2)
81337c478bd9Sstevel@tonic-gate {
81347c478bd9Sstevel@tonic-gate 	queue_t *wrq1 = strvp2wq(vp1);
81357c478bd9Sstevel@tonic-gate 	queue_t *wrq2 = strvp2wq(vp2);
81367c478bd9Sstevel@tonic-gate 
81377c478bd9Sstevel@tonic-gate 	/*
81387c478bd9Sstevel@tonic-gate 	 * Verify that there are no modules on the stream yet.  We also
81397c478bd9Sstevel@tonic-gate 	 * rely on the stream head always having a service procedure to
81407c478bd9Sstevel@tonic-gate 	 * avoid tweaking q_nfsrv.
81417c478bd9Sstevel@tonic-gate 	 */
81427c478bd9Sstevel@tonic-gate 	ASSERT(wrq1->q_next == NULL && wrq2->q_next == NULL);
81437c478bd9Sstevel@tonic-gate 	ASSERT(wrq1->q_qinfo->qi_srvp != NULL);
81447c478bd9Sstevel@tonic-gate 	ASSERT(wrq2->q_qinfo->qi_srvp != NULL);
81457c478bd9Sstevel@tonic-gate 
81467c478bd9Sstevel@tonic-gate 	/*
81477c478bd9Sstevel@tonic-gate 	 * If the queues are the same, just twist; otherwise do a full mate.
81487c478bd9Sstevel@tonic-gate 	 */
81497c478bd9Sstevel@tonic-gate 	if (wrq1 == wrq2) {
81507c478bd9Sstevel@tonic-gate 		wrq1->q_next = _RD(wrq1);
81517c478bd9Sstevel@tonic-gate 	} else {
81527c478bd9Sstevel@tonic-gate 		wrq1->q_next = _RD(wrq2);
81537c478bd9Sstevel@tonic-gate 		wrq2->q_next = _RD(wrq1);
81547c478bd9Sstevel@tonic-gate 		STREAM(wrq1)->sd_mate = STREAM(wrq2);
81557c478bd9Sstevel@tonic-gate 		STREAM(wrq1)->sd_flag |= STRMATE;
81567c478bd9Sstevel@tonic-gate 		STREAM(wrq2)->sd_mate = STREAM(wrq1);
81577c478bd9Sstevel@tonic-gate 		STREAM(wrq2)->sd_flag |= STRMATE;
81587c478bd9Sstevel@tonic-gate 	}
81597c478bd9Sstevel@tonic-gate }
81607c478bd9Sstevel@tonic-gate 
81617c478bd9Sstevel@tonic-gate /*
81627c478bd9Sstevel@tonic-gate  * XXX will go away when console is correctly fixed.
81637c478bd9Sstevel@tonic-gate  * Clean up the console PIDS, from previous I_SETSIG,
81647c478bd9Sstevel@tonic-gate  * called only for cnopen which never calls strclean().
81657c478bd9Sstevel@tonic-gate  */
81667c478bd9Sstevel@tonic-gate void
81677c478bd9Sstevel@tonic-gate str_cn_clean(struct vnode *vp)
81687c478bd9Sstevel@tonic-gate {
81697c478bd9Sstevel@tonic-gate 	strsig_t *ssp, *pssp, *tssp;
81707c478bd9Sstevel@tonic-gate 	struct stdata *stp;
81717c478bd9Sstevel@tonic-gate 	struct pid  *pidp;
81727c478bd9Sstevel@tonic-gate 	int update = 0;
81737c478bd9Sstevel@tonic-gate 
81747c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
81757c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
81767c478bd9Sstevel@tonic-gate 	pssp = NULL;
81777c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
81787c478bd9Sstevel@tonic-gate 	ssp = stp->sd_siglist;
81797c478bd9Sstevel@tonic-gate 	while (ssp) {
81807c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
81817c478bd9Sstevel@tonic-gate 		pidp = ssp->ss_pidp;
81827c478bd9Sstevel@tonic-gate 		/*
81837c478bd9Sstevel@tonic-gate 		 * Get rid of PID if the proc is gone.
81847c478bd9Sstevel@tonic-gate 		 */
81857c478bd9Sstevel@tonic-gate 		if (pidp->pid_prinactive) {
81867c478bd9Sstevel@tonic-gate 			tssp = ssp->ss_next;
81877c478bd9Sstevel@tonic-gate 			if (pssp)
81887c478bd9Sstevel@tonic-gate 				pssp->ss_next = tssp;
81897c478bd9Sstevel@tonic-gate 			else
81907c478bd9Sstevel@tonic-gate 				stp->sd_siglist = tssp;
81917c478bd9Sstevel@tonic-gate 			ASSERT(pidp->pid_ref <= 1);
81927c478bd9Sstevel@tonic-gate 			PID_RELE(ssp->ss_pidp);
81937c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
81947c478bd9Sstevel@tonic-gate 			kmem_free(ssp, sizeof (strsig_t));
81957c478bd9Sstevel@tonic-gate 			update = 1;
81967c478bd9Sstevel@tonic-gate 			ssp = tssp;
81977c478bd9Sstevel@tonic-gate 			continue;
81987c478bd9Sstevel@tonic-gate 		} else
81997c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
82007c478bd9Sstevel@tonic-gate 		pssp = ssp;
82017c478bd9Sstevel@tonic-gate 		ssp = ssp->ss_next;
82027c478bd9Sstevel@tonic-gate 	}
82037c478bd9Sstevel@tonic-gate 	if (update) {
82047c478bd9Sstevel@tonic-gate 		stp->sd_sigflags = 0;
82057c478bd9Sstevel@tonic-gate 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
82067c478bd9Sstevel@tonic-gate 			stp->sd_sigflags |= ssp->ss_events;
82077c478bd9Sstevel@tonic-gate 	}
82087c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
82097c478bd9Sstevel@tonic-gate }
82107c478bd9Sstevel@tonic-gate 
82117c478bd9Sstevel@tonic-gate /*
82127c478bd9Sstevel@tonic-gate  * Return B_TRUE if there is data in the message, B_FALSE otherwise.
82137c478bd9Sstevel@tonic-gate  */
82147c478bd9Sstevel@tonic-gate static boolean_t
82157c478bd9Sstevel@tonic-gate msghasdata(mblk_t *bp)
82167c478bd9Sstevel@tonic-gate {
82177c478bd9Sstevel@tonic-gate 	for (; bp; bp = bp->b_cont)
82187c478bd9Sstevel@tonic-gate 		if (bp->b_datap->db_type == M_DATA) {
82197c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_wptr >= bp->b_rptr);
82207c478bd9Sstevel@tonic-gate 			if (bp->b_wptr > bp->b_rptr)
82217c478bd9Sstevel@tonic-gate 				return (B_TRUE);
82227c478bd9Sstevel@tonic-gate 		}
82237c478bd9Sstevel@tonic-gate 	return (B_FALSE);
82247c478bd9Sstevel@tonic-gate }
8225