xref: /titanic_50/usr/src/uts/common/io/pts.c (revision 193974072f41a843678abf5f61979c748687e66b)
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
5facf4a8dSllai1  * Common Development and Distribution License (the "License").
6facf4a8dSllai1  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Pseudo Terminal Slave Driver.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * The pseudo-tty subsystem simulates a terminal connection, where the master
347c478bd9Sstevel@tonic-gate  * side represents the terminal and the slave represents the user process's
357c478bd9Sstevel@tonic-gate  * special device end point. The master device is set up as a cloned device
367c478bd9Sstevel@tonic-gate  * where its major device number is the major for the clone device and its minor
377c478bd9Sstevel@tonic-gate  * device number is the major for the ptm driver. There are no nodes in the file
387c478bd9Sstevel@tonic-gate  * system for master devices. The master pseudo driver is opened using the
397c478bd9Sstevel@tonic-gate  * open(2) system call with /dev/ptmx as the device parameter.  The clone open
407c478bd9Sstevel@tonic-gate  * finds the next available minor device for the ptm major device.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * A master device is available only if it and its corresponding slave device
437c478bd9Sstevel@tonic-gate  * are not already open. When the master device is opened, the corresponding
447c478bd9Sstevel@tonic-gate  * slave device is automatically locked out. Only one open is allowed on a
457c478bd9Sstevel@tonic-gate  * master device.  Multiple opens are allowed on the slave device.  After both
467c478bd9Sstevel@tonic-gate  * the master and slave have been opened, the user has two file descriptors
477c478bd9Sstevel@tonic-gate  * which are the end points of a full duplex connection composed of two streams
487c478bd9Sstevel@tonic-gate  * which are automatically connected at the master and slave drivers. The user
497c478bd9Sstevel@tonic-gate  * may then push modules onto either side of the stream pair.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * The master and slave drivers pass all messages to their adjacent queues.
527c478bd9Sstevel@tonic-gate  * Only the M_FLUSH needs some processing.  Because the read queue of one side
537c478bd9Sstevel@tonic-gate  * is connected to the write queue of the other, the FLUSHR flag is changed to
547c478bd9Sstevel@tonic-gate  * the FLUSHW flag and vice versa. When the master device is closed an M_HANGUP
557c478bd9Sstevel@tonic-gate  * message is sent to the slave device which will render the device
567c478bd9Sstevel@tonic-gate  * unusable. The process on the slave side gets the EIO when attempting to write
577c478bd9Sstevel@tonic-gate  * on that stream but it will be able to read any data remaining on the stream
587c478bd9Sstevel@tonic-gate  * head read queue.  When all the data has been read, read() returns 0
597c478bd9Sstevel@tonic-gate  * indicating that the stream can no longer be used.  On the last close of the
607c478bd9Sstevel@tonic-gate  * slave device, a 0-length message is sent to the master device. When the
617c478bd9Sstevel@tonic-gate  * application on the master side issues a read() or getmsg() and 0 is returned,
627c478bd9Sstevel@tonic-gate  * the user of the master device decides whether to issue a close() that
637c478bd9Sstevel@tonic-gate  * dismantles the pseudo-terminal subsystem. If the master device is not closed,
647c478bd9Sstevel@tonic-gate  * the pseudo-tty subsystem will be available to another user to open the slave
657c478bd9Sstevel@tonic-gate  * device.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * Synchronization:
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  *   All global data synchronization between ptm/pts is done via global
707c478bd9Sstevel@tonic-gate  *   ptms_lock mutex which is initialized at system boot time from
717c478bd9Sstevel@tonic-gate  *   ptms_initspace (called from space.c).
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  *   Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
747c478bd9Sstevel@tonic-gate  *   pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  *   PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
777c478bd9Sstevel@tonic-gate  *   which allow reader locks to be reacquired by the same thread (usual
787c478bd9Sstevel@tonic-gate  *   reader/writer locks can't be used for that purpose since it is illegal for
797c478bd9Sstevel@tonic-gate  *   a thread to acquire a lock it already holds, even as a reader). The sole
807c478bd9Sstevel@tonic-gate  *   purpose of these macros is to guarantee that the peer queue will not
817c478bd9Sstevel@tonic-gate  *   disappear (due to closing peer) while it is used. It is safe to use
827c478bd9Sstevel@tonic-gate  *   PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
837c478bd9Sstevel@tonic-gate  *   they are not real locks but reference counts).
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  *   PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave
867c478bd9Sstevel@tonic-gate  *   open/close paths to modify ptm_rdq and pts_rdq fields. These fields should
877c478bd9Sstevel@tonic-gate  *   be set to appropriate queues *after* qprocson() is called during open (to
887c478bd9Sstevel@tonic-gate  *   prevent peer from accessing the queue with incomplete plumbing) and set to
897c478bd9Sstevel@tonic-gate  *   NULL before qprocsoff() is called during close.
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  *   The pt_nullmsg field is only used in open/close routines and it is also
927c478bd9Sstevel@tonic-gate  *   protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
937c478bd9Sstevel@tonic-gate  *   holds.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * Lock Ordering:
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *   If both ptms_lock and per-pty lock should be held, ptms_lock should always
987c478bd9Sstevel@tonic-gate  *   be entered first, followed by per-pty lock.
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  * See ptms.h, ptm.c and ptms_conf.c fore more information.
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #include <sys/types.h>
1057c478bd9Sstevel@tonic-gate #include <sys/param.h>
1067c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1077c478bd9Sstevel@tonic-gate #include <sys/stream.h>
1087c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
1097c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1107c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1117c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1127c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1137c478bd9Sstevel@tonic-gate #include <sys/ptms.h>
1147c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1157c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1167c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1177c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1187c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1197c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1207c478bd9Sstevel@tonic-gate #include <sys/zone.h>
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #ifdef DEBUG
1237c478bd9Sstevel@tonic-gate int pts_debug = 0;
1247c478bd9Sstevel@tonic-gate #define	DBG(a)	 if (pts_debug) cmn_err(CE_NOTE, a)
1257c478bd9Sstevel@tonic-gate #else
1267c478bd9Sstevel@tonic-gate #define	DBG(a)
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static int ptsopen(queue_t *, dev_t *, int, int, cred_t *);
1307c478bd9Sstevel@tonic-gate static int ptsclose(queue_t *, int, cred_t *);
1317c478bd9Sstevel@tonic-gate static void ptswput(queue_t *, mblk_t *);
1327c478bd9Sstevel@tonic-gate static void ptsrsrv(queue_t *);
1337c478bd9Sstevel@tonic-gate static void ptswsrv(queue_t *);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Slave Stream Pseudo Terminal Module: stream data structure definitions
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate static struct module_info pts_info = {
1397c478bd9Sstevel@tonic-gate 	0xface,
1407c478bd9Sstevel@tonic-gate 	"pts",
1417c478bd9Sstevel@tonic-gate 	0,
1427c478bd9Sstevel@tonic-gate 	512,
1437c478bd9Sstevel@tonic-gate 	512,
1447c478bd9Sstevel@tonic-gate 	128
1457c478bd9Sstevel@tonic-gate };
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static struct qinit ptsrint = {
1487c478bd9Sstevel@tonic-gate 	NULL,
1497c478bd9Sstevel@tonic-gate 	(int (*)()) ptsrsrv,
1507c478bd9Sstevel@tonic-gate 	ptsopen,
1517c478bd9Sstevel@tonic-gate 	ptsclose,
1527c478bd9Sstevel@tonic-gate 	NULL,
1537c478bd9Sstevel@tonic-gate 	&pts_info,
1547c478bd9Sstevel@tonic-gate 	NULL
1557c478bd9Sstevel@tonic-gate };
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static struct qinit ptswint = {
1587c478bd9Sstevel@tonic-gate 	(int (*)()) ptswput,
1597c478bd9Sstevel@tonic-gate 	(int (*)()) ptswsrv,
1607c478bd9Sstevel@tonic-gate 	NULL,
1617c478bd9Sstevel@tonic-gate 	NULL,
1627c478bd9Sstevel@tonic-gate 	NULL,
1637c478bd9Sstevel@tonic-gate 	&pts_info,
1647c478bd9Sstevel@tonic-gate 	NULL
1657c478bd9Sstevel@tonic-gate };
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static struct streamtab ptsinfo = {
1687c478bd9Sstevel@tonic-gate 	&ptsrint,
1697c478bd9Sstevel@tonic-gate 	&ptswint,
1707c478bd9Sstevel@tonic-gate 	NULL,
1717c478bd9Sstevel@tonic-gate 	NULL
1727c478bd9Sstevel@tonic-gate };
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static int pts_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
1757c478bd9Sstevel@tonic-gate static int pts_attach(dev_info_t *, ddi_attach_cmd_t);
1767c478bd9Sstevel@tonic-gate static int pts_detach(dev_info_t *, ddi_detach_cmd_t);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #define	PTS_CONF_FLAG	(D_NEW | D_MP)
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * this will define (struct cb_ops cb_pts_ops) and (struct dev_ops pts_ops)
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(pts_ops, nulldev, nulldev,	\
1847c478bd9Sstevel@tonic-gate 	pts_attach, pts_detach, nodev,			\
185*19397407SSherry Moore 	pts_devinfo, PTS_CONF_FLAG, &ptsinfo, ddi_quiesce_not_supported);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1927c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
1937c478bd9Sstevel@tonic-gate 	"Slave Stream Pseudo Terminal driver 'pts'",
1947c478bd9Sstevel@tonic-gate 	&pts_ops,	/* driver ops */
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1987c478bd9Sstevel@tonic-gate 	MODREV_1,
1997c478bd9Sstevel@tonic-gate 	&modldrv,
2007c478bd9Sstevel@tonic-gate 	NULL
2017c478bd9Sstevel@tonic-gate };
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate int
_init(void)2047c478bd9Sstevel@tonic-gate _init(void)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	int rc;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if ((rc = mod_install(&modlinkage)) == 0)
2097c478bd9Sstevel@tonic-gate 		ptms_init();
2107c478bd9Sstevel@tonic-gate 	return (rc);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate int
_fini(void)2157c478bd9Sstevel@tonic-gate _fini(void)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2217c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate static int
pts_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2277c478bd9Sstevel@tonic-gate pts_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2317c478bd9Sstevel@tonic-gate 
232facf4a8dSllai1 	mutex_enter(&ptms_lock);
233facf4a8dSllai1 	pts_dip = devi;
234facf4a8dSllai1 	mutex_exit(&ptms_lock);
235facf4a8dSllai1 
236facf4a8dSllai1 	return (DDI_SUCCESS);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
239facf4a8dSllai1 /*ARGSUSED*/
2407c478bd9Sstevel@tonic-gate static int
pts_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2417c478bd9Sstevel@tonic-gate pts_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2447c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2457c478bd9Sstevel@tonic-gate 
246facf4a8dSllai1 	/*
247facf4a8dSllai1 	 * For now, pts cannot be detached.
248facf4a8dSllai1 	 */
249facf4a8dSllai1 	return (DDI_FAILURE);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2537c478bd9Sstevel@tonic-gate static int
pts_devinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2547c478bd9Sstevel@tonic-gate pts_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2557c478bd9Sstevel@tonic-gate     void **result)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	int error;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2607c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2617c478bd9Sstevel@tonic-gate 		if (pts_dip == NULL) {
2627c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
2637c478bd9Sstevel@tonic-gate 		} else {
2647c478bd9Sstevel@tonic-gate 			*result = (void *)pts_dip;
2657c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 		break;
2687c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2697c478bd9Sstevel@tonic-gate 		*result = (void *)0;
2707c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
2717c478bd9Sstevel@tonic-gate 		break;
2727c478bd9Sstevel@tonic-gate 	default:
2737c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	return (error);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /* ARGSUSED */
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * Open the slave device. Reject a clone open and do not allow the
2817c478bd9Sstevel@tonic-gate  * driver to be pushed. If the slave/master pair is locked or if
2827c478bd9Sstevel@tonic-gate  * the master is not open, return EACCESS.
2837c478bd9Sstevel@tonic-gate  * Upon success, store the write queue pointer in private data and
2847c478bd9Sstevel@tonic-gate  * set the PTSOPEN bit in the pt_state field.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate static int
ptsopen(queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)2877c478bd9Sstevel@tonic-gate ptsopen(
2887c478bd9Sstevel@tonic-gate 	queue_t *rqp,		/* pointer to the read side queue */
2897c478bd9Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
2907c478bd9Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
2917c478bd9Sstevel@tonic-gate 	int	sflag,		/* open state flag */
2927c478bd9Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptsp;
2957c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
2967c478bd9Sstevel@tonic-gate 	mblk_t		*mop;	/* ptr to a setopts message block */
2977c478bd9Sstevel@tonic-gate 	minor_t		dminor = getminor(*devp);
2987c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	DDBG("entering ptsopen(%d)", dminor);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (sflag != 0) {
3037c478bd9Sstevel@tonic-gate 		return (EINVAL);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	mutex_enter(&ptms_lock);
3077c478bd9Sstevel@tonic-gate 	ptsp = ptms_minor2ptty(dminor);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (ptsp == NULL) {
3107c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3117c478bd9Sstevel@tonic-gate 		return (ENXIO);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	mutex_enter(&ptsp->pt_lock);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate 	 * Prevent opens from zones other than the one blessed by ptm.  We
3177c478bd9Sstevel@tonic-gate 	 * can't even allow the global zone to open all pts's, as it would
3187c478bd9Sstevel@tonic-gate 	 * otherwise inproperly be able to claim pts's already opened by zones.
3197c478bd9Sstevel@tonic-gate 	 */
3207c478bd9Sstevel@tonic-gate 	if (ptsp->pt_zoneid != getzoneid()) {
3217c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3227c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3237c478bd9Sstevel@tonic-gate 		return (EPERM);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Allow reopen of this device.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	if (rqp->q_ptr != NULL) {
330fbe27353Sedp 		ASSERT(rqp->q_ptr == ptsp);
331fbe27353Sedp 		ASSERT(ptsp->pts_rdq == rqp);
3327c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3337c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3347c478bd9Sstevel@tonic-gate 		return (0);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	DDBGP("ptsopen: p = %p\n", (uintptr_t)ptsp);
3387c478bd9Sstevel@tonic-gate 	DDBG("ptsopen: state = %x\n", ptsp->pt_state);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	ASSERT(ptsp->pt_minor == dminor);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if ((ptsp->pt_state & PTLOCK) || !(ptsp->pt_state & PTMOPEN)) {
3447c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3457c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3467c478bd9Sstevel@tonic-gate 		return (EAGAIN);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * if already, open simply return...
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	if (ptsp->pt_state & PTSOPEN) {
353fbe27353Sedp 		ASSERT(rqp->q_ptr == ptsp);
354fbe27353Sedp 		ASSERT(ptsp->pts_rdq == rqp);
3557c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3567c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3577c478bd9Sstevel@tonic-gate 		return (0);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/*
3617c478bd9Sstevel@tonic-gate 	 * Allocate message block for setting stream head options.
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
3647c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3657c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3667c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/*
3707c478bd9Sstevel@tonic-gate 	 * Slave should send zero-length message to a master when it is
3717c478bd9Sstevel@tonic-gate 	 * closing. If memory is low at that time, master will not detect slave
3727c478bd9Sstevel@tonic-gate 	 * closes, this pty will not be deallocated. So, preallocate this
3737c478bd9Sstevel@tonic-gate 	 * zero-length message block early.
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	if ((mp = allocb(0, BPRI_MED)) == NULL) {
3767c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3777c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3787c478bd9Sstevel@tonic-gate 		freemsg(mop);
3797c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	ptsp->pt_state |= PTSOPEN;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = ptsp;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	mutex_exit(&ptsp->pt_lock);
3877c478bd9Sstevel@tonic-gate 	mutex_exit(&ptms_lock);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	qprocson(rqp);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * After qprocson pts driver is fully plumbed into the stream and can
3937c478bd9Sstevel@tonic-gate 	 * send/receive messages. Setting pts_rdq will allow master side to send
3947c478bd9Sstevel@tonic-gate 	 * messages to the slave. This setting can't occur before qprocson() is
3957c478bd9Sstevel@tonic-gate 	 * finished because slave is not ready to process them.
3967c478bd9Sstevel@tonic-gate 	 */
3977c478bd9Sstevel@tonic-gate 	PT_ENTER_WRITE(ptsp);
3987c478bd9Sstevel@tonic-gate 	ptsp->pts_rdq = rqp;
3997c478bd9Sstevel@tonic-gate 	ASSERT(ptsp->pt_nullmsg == NULL);
4007c478bd9Sstevel@tonic-gate 	ptsp->pt_nullmsg = mp;
4017c478bd9Sstevel@tonic-gate 	PT_EXIT_WRITE(ptsp);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue
4057c478bd9Sstevel@tonic-gate 	 * and add controlling tty if not set
4067c478bd9Sstevel@tonic-gate 	 */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
4097c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
4107c478bd9Sstevel@tonic-gate 	sop = (struct stroptions *)mop->b_rptr;
4117c478bd9Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
4127c478bd9Sstevel@tonic-gate 	sop->so_hiwat = 512;
4137c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
4147c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	return (0);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate  * Find the address to private data identifying the slave's write
4237c478bd9Sstevel@tonic-gate  * queue. Send a 0-length msg up the slave's read queue to designate
4247c478bd9Sstevel@tonic-gate  * the master is closing. Uattach the master from the slave by nulling
4257c478bd9Sstevel@tonic-gate  * out master's write queue field in private data.
4267c478bd9Sstevel@tonic-gate  */
4277c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
4287c478bd9Sstevel@tonic-gate static int
ptsclose(queue_t * rqp,int flag,cred_t * credp)4297c478bd9Sstevel@tonic-gate ptsclose(queue_t *rqp, int flag, cred_t *credp)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptsp;
4327c478bd9Sstevel@tonic-gate 	queue_t *wqp;
4337c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
4347c478bd9Sstevel@tonic-gate 	mblk_t	*bp;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * q_ptr should never be NULL in the close routine and it is checked in
4387c478bd9Sstevel@tonic-gate 	 * DEBUG kernel by ASSERT. For non-DEBUG kernel the attempt is made to
4397c478bd9Sstevel@tonic-gate 	 * behave gracefully.
4407c478bd9Sstevel@tonic-gate 	 */
4417c478bd9Sstevel@tonic-gate 	ASSERT(rqp->q_ptr != NULL);
4427c478bd9Sstevel@tonic-gate 	if (rqp->q_ptr == NULL) {
4437c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
4447c478bd9Sstevel@tonic-gate 		return (0);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)rqp->q_ptr;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * Slave is going to close and doesn't want any new  messages coming
4517c478bd9Sstevel@tonic-gate 	 * from the master side, so set pts_rdq to NULL. This should be done
4527c478bd9Sstevel@tonic-gate 	 * before call to qprocsoff() since slave can't process additional
4537c478bd9Sstevel@tonic-gate 	 * messages from the master after qprocsoff is called.
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 	PT_ENTER_WRITE(ptsp);
4567c478bd9Sstevel@tonic-gate 	mp = ptsp->pt_nullmsg;
4577c478bd9Sstevel@tonic-gate 	ptsp->pt_nullmsg = NULL;
4587c478bd9Sstevel@tonic-gate 	ptsp->pts_rdq = NULL;
4597c478bd9Sstevel@tonic-gate 	PT_EXIT_WRITE(ptsp);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * Drain the ouput
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	wqp = WR(rqp);
4657c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
4667c478bd9Sstevel@tonic-gate 	while ((bp = getq(wqp)) != NULL) {
4677c478bd9Sstevel@tonic-gate 		if (ptsp->ptm_rdq) {
4687c478bd9Sstevel@tonic-gate 			putnext(ptsp->ptm_rdq, bp);
4697c478bd9Sstevel@tonic-gate 		} else if (bp->b_datap->db_type == M_IOCTL) {
4707c478bd9Sstevel@tonic-gate 			bp->b_datap->db_type = M_IOCNAK;
4717c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
4727c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
4737c478bd9Sstevel@tonic-gate 			qreply(wqp, bp);
4747c478bd9Sstevel@tonic-gate 		} else {
4757c478bd9Sstevel@tonic-gate 			freemsg(bp);
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 	/*
4797c478bd9Sstevel@tonic-gate 	 * qenable master side write queue so that it can flush
4807c478bd9Sstevel@tonic-gate 	 * its messages as slaves's read queue is going away
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	if (ptsp->ptm_rdq) {
4837c478bd9Sstevel@tonic-gate 		if (mp)
4847c478bd9Sstevel@tonic-gate 			putnext(ptsp->ptm_rdq, mp);
4857c478bd9Sstevel@tonic-gate 		else
4867c478bd9Sstevel@tonic-gate 			qenable(WR(ptsp->ptm_rdq));
4877c478bd9Sstevel@tonic-gate 	} else
4887c478bd9Sstevel@tonic-gate 		freemsg(mp);
4897c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	qprocsoff(rqp);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	rqp->q_ptr = NULL;
4947c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = NULL;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	ptms_close(ptsp, PTSOPEN | PTSTTY);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	return (0);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * The wput procedure will only handle flush messages.
5047c478bd9Sstevel@tonic-gate  * All other messages are queued and the write side
5057c478bd9Sstevel@tonic-gate  * service procedure sends them off to the master side.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate static void
ptswput(queue_t * qp,mblk_t * mp)5087c478bd9Sstevel@tonic-gate ptswput(queue_t *qp, mblk_t *mp)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	struct pt_ttys *ptsp;
5117c478bd9Sstevel@tonic-gate 	struct iocblk  *iocp;
5127c478bd9Sstevel@tonic-gate 	unsigned char type = mp->b_datap->db_type;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	DBG(("entering ptswput\n"));
5157c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)qp->q_ptr;
5187c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
5197c478bd9Sstevel@tonic-gate 	if (ptsp->ptm_rdq == NULL) {
5207c478bd9Sstevel@tonic-gate 		DBG(("in write put proc but no master\n"));
5217c478bd9Sstevel@tonic-gate 		/*
5227c478bd9Sstevel@tonic-gate 		 * NAK ioctl as slave side read queue is gone.
5237c478bd9Sstevel@tonic-gate 		 * Or else free the message.
5247c478bd9Sstevel@tonic-gate 		 */
5257c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
5267c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCNAK;
5277c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
5287c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
5297c478bd9Sstevel@tonic-gate 			qreply(qp, mp);
5307c478bd9Sstevel@tonic-gate 		} else
5317c478bd9Sstevel@tonic-gate 			freemsg(mp);
5327c478bd9Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
5337c478bd9Sstevel@tonic-gate 		return;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (type >= QPCTL) {
5377c478bd9Sstevel@tonic-gate 		switch (type) {
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		/*
5407c478bd9Sstevel@tonic-gate 		 * if write queue request, flush slave's write
5417c478bd9Sstevel@tonic-gate 		 * queue and send FLUSHR to ptm. If read queue
5427c478bd9Sstevel@tonic-gate 		 * request, send FLUSHR to ptm.
5437c478bd9Sstevel@tonic-gate 		 */
5447c478bd9Sstevel@tonic-gate 		case M_FLUSH:
5457c478bd9Sstevel@tonic-gate 		DBG(("pts got flush request\n"));
5467c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 			DBG(("got FLUSHW, flush pts write Q\n"));
5497c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHBAND)
5507c478bd9Sstevel@tonic-gate 				/*
5517c478bd9Sstevel@tonic-gate 				 * if it is a FLUSHBAND, do flushband.
5527c478bd9Sstevel@tonic-gate 				 */
5537c478bd9Sstevel@tonic-gate 				flushband(qp, *(mp->b_rptr + 1), FLUSHDATA);
5547c478bd9Sstevel@tonic-gate 			else
5557c478bd9Sstevel@tonic-gate 				flushq(qp, FLUSHDATA);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
5587c478bd9Sstevel@tonic-gate 			if ((*mp->b_rptr & FLUSHR) == 0) {
5597c478bd9Sstevel@tonic-gate 				/*
5607c478bd9Sstevel@tonic-gate 				 * FLUSHW only. Change to FLUSHR and putnext
5617c478bd9Sstevel@tonic-gate 				 * to ptm, then we are done.
5627c478bd9Sstevel@tonic-gate 				 */
5637c478bd9Sstevel@tonic-gate 				*mp->b_rptr |= FLUSHR;
5647c478bd9Sstevel@tonic-gate 				if (ptsp->ptm_rdq)
5657c478bd9Sstevel@tonic-gate 					putnext(ptsp->ptm_rdq, mp);
5667c478bd9Sstevel@tonic-gate 				break;
5677c478bd9Sstevel@tonic-gate 			} else {
5687c478bd9Sstevel@tonic-gate 				mblk_t *nmp;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 				/* It is a FLUSHRW. Duplicate the mblk */
5717c478bd9Sstevel@tonic-gate 				nmp = copyb(mp);
5727c478bd9Sstevel@tonic-gate 				if (nmp) {
5737c478bd9Sstevel@tonic-gate 					/*
5747c478bd9Sstevel@tonic-gate 					 * Change FLUSHW to FLUSHR before
5757c478bd9Sstevel@tonic-gate 					 * putnext to ptm.
5767c478bd9Sstevel@tonic-gate 					 */
5777c478bd9Sstevel@tonic-gate 					DBG(("putnext nmp(FLUSHR) to ptm\n"));
5787c478bd9Sstevel@tonic-gate 					*nmp->b_rptr |= FLUSHR;
5797c478bd9Sstevel@tonic-gate 					if (ptsp->ptm_rdq)
5807c478bd9Sstevel@tonic-gate 						putnext(ptsp->ptm_rdq, nmp);
5817c478bd9Sstevel@tonic-gate 				}
5827c478bd9Sstevel@tonic-gate 			}
5837c478bd9Sstevel@tonic-gate 		}
5847c478bd9Sstevel@tonic-gate 		/*
5857c478bd9Sstevel@tonic-gate 		 * Since the packet module will toss any
5867c478bd9Sstevel@tonic-gate 		 * M_FLUSHES sent to the master's stream head
5877c478bd9Sstevel@tonic-gate 		 * read queue, we simply turn it around here.
5887c478bd9Sstevel@tonic-gate 		 */
5897c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
5907c478bd9Sstevel@tonic-gate 			ASSERT(RD(qp)->q_first == NULL);
5917c478bd9Sstevel@tonic-gate 			DBG(("qreply(qp) turning FLUSHR around\n"));
5927c478bd9Sstevel@tonic-gate 			qreply(qp, mp);
5937c478bd9Sstevel@tonic-gate 		} else {
5947c478bd9Sstevel@tonic-gate 			freemsg(mp);
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 		break;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		case M_READ:
5997c478bd9Sstevel@tonic-gate 		/* Caused by ldterm - can not pass to master */
6007c478bd9Sstevel@tonic-gate 		freemsg(mp);
6017c478bd9Sstevel@tonic-gate 		break;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		default:
6047c478bd9Sstevel@tonic-gate 		if (ptsp->ptm_rdq)
6057c478bd9Sstevel@tonic-gate 			putnext(ptsp->ptm_rdq, mp);
6067c478bd9Sstevel@tonic-gate 		break;
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
6097c478bd9Sstevel@tonic-gate 		return;
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	switch (type) {
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	case M_IOCTL:
6157c478bd9Sstevel@tonic-gate 		/*
6167c478bd9Sstevel@tonic-gate 		 * For case PTSSTTY set the flag PTSTTY and ACK
6177c478bd9Sstevel@tonic-gate 		 * the ioctl so that the user program can push
6187c478bd9Sstevel@tonic-gate 		 * the associated modules to get tty semantics.
6197c478bd9Sstevel@tonic-gate 		 * See bugid 4025044
6207c478bd9Sstevel@tonic-gate 		 */
6217c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
6227c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
6237c478bd9Sstevel@tonic-gate 		default:
6247c478bd9Sstevel@tonic-gate 			break;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		case PTSSTTY:
6277c478bd9Sstevel@tonic-gate 			if (ptsp->pt_state & PTSTTY) {
6287c478bd9Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCNAK;
6297c478bd9Sstevel@tonic-gate 				iocp->ioc_error = EEXIST;
6307c478bd9Sstevel@tonic-gate 			} else {
6317c478bd9Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
6327c478bd9Sstevel@tonic-gate 				mutex_enter(&ptsp->pt_lock);
6337c478bd9Sstevel@tonic-gate 				ptsp->pt_state |= PTSTTY;
6347c478bd9Sstevel@tonic-gate 				mutex_exit(&ptsp->pt_lock);
6357c478bd9Sstevel@tonic-gate 				iocp->ioc_error = 0;
6367c478bd9Sstevel@tonic-gate 			}
6377c478bd9Sstevel@tonic-gate 			iocp->ioc_count = 0;
6387c478bd9Sstevel@tonic-gate 			qreply(qp, mp);
6397c478bd9Sstevel@tonic-gate 			PT_EXIT_READ(ptsp);
6407c478bd9Sstevel@tonic-gate 			return;
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	default:
6447c478bd9Sstevel@tonic-gate 		/*
6457c478bd9Sstevel@tonic-gate 		 * send other messages to the master
6467c478bd9Sstevel@tonic-gate 		 */
6477c478bd9Sstevel@tonic-gate 		DBG(("put msg on slave's write queue\n"));
6487c478bd9Sstevel@tonic-gate 		(void) putq(qp, mp);
6497c478bd9Sstevel@tonic-gate 		break;
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
6537c478bd9Sstevel@tonic-gate 	DBG(("return from ptswput()\n"));
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate  * enable the write side of the master. This triggers the
6597c478bd9Sstevel@tonic-gate  * master to send any messages queued on its write side to
6607c478bd9Sstevel@tonic-gate  * the read side of this slave.
6617c478bd9Sstevel@tonic-gate  */
6627c478bd9Sstevel@tonic-gate static void
ptsrsrv(queue_t * qp)6637c478bd9Sstevel@tonic-gate ptsrsrv(queue_t *qp)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	struct pt_ttys *ptsp;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	DBG(("entering ptsrsrv\n"));
6687c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)qp->q_ptr;
6717c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
6727c478bd9Sstevel@tonic-gate 	if (ptsp->ptm_rdq == NULL) {
6737c478bd9Sstevel@tonic-gate 		DBG(("in read srv proc but no master\n"));
6747c478bd9Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
6757c478bd9Sstevel@tonic-gate 		return;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 	qenable(WR(ptsp->ptm_rdq));
6787c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
6797c478bd9Sstevel@tonic-gate 	DBG(("leaving ptsrsrv\n"));
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate /*
6837c478bd9Sstevel@tonic-gate  * If there are messages on this queue that can be sent to
6847c478bd9Sstevel@tonic-gate  * master, send them via putnext(). Else, if queued messages
6857c478bd9Sstevel@tonic-gate  * cannot be sent, leave them on this queue. If priority
6867c478bd9Sstevel@tonic-gate  * messages on this queue, send them to master no matter what.
6877c478bd9Sstevel@tonic-gate  */
6887c478bd9Sstevel@tonic-gate static void
ptswsrv(queue_t * qp)6897c478bd9Sstevel@tonic-gate ptswsrv(queue_t *qp)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	struct pt_ttys *ptsp;
6927c478bd9Sstevel@tonic-gate 	queue_t *ptm_rdq;
6937c478bd9Sstevel@tonic-gate 	mblk_t *mp;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	DBG(("entering ptswsrv\n"));
6967c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)qp->q_ptr;
6997c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
7007c478bd9Sstevel@tonic-gate 	if (ptsp->ptm_rdq == NULL) {
7017c478bd9Sstevel@tonic-gate 		DBG(("in write srv proc but no master\n"));
7027c478bd9Sstevel@tonic-gate 		/*
7037c478bd9Sstevel@tonic-gate 		 * Free messages on the write queue and send
7047c478bd9Sstevel@tonic-gate 		 * NAK for any M_IOCTL type messages to wakeup
7057c478bd9Sstevel@tonic-gate 		 * the user process waiting for ACK/NAK from
7067c478bd9Sstevel@tonic-gate 		 * the ioctl invocation
7077c478bd9Sstevel@tonic-gate 		 */
7087c478bd9Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
7097c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL) {
7107c478bd9Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCNAK;
7117c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
7127c478bd9Sstevel@tonic-gate 				mp->b_cont = NULL;
7137c478bd9Sstevel@tonic-gate 				qreply(qp, mp);
7147c478bd9Sstevel@tonic-gate 			} else
7157c478bd9Sstevel@tonic-gate 				freemsg(mp);
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
7187c478bd9Sstevel@tonic-gate 		return;
7197c478bd9Sstevel@tonic-gate 	} else {
7207c478bd9Sstevel@tonic-gate 		ptm_rdq = ptsp->ptm_rdq;
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/*
7247c478bd9Sstevel@tonic-gate 	 * while there are messages on this write queue...
7257c478bd9Sstevel@tonic-gate 	 */
7267c478bd9Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
7277c478bd9Sstevel@tonic-gate 		/*
7287c478bd9Sstevel@tonic-gate 		 * if don't have control message and cannot put
7297c478bd9Sstevel@tonic-gate 		 * msg. on master's read queue, put it back on
7307c478bd9Sstevel@tonic-gate 		 * this queue.
7317c478bd9Sstevel@tonic-gate 		 */
7327c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type <= QPCTL &&
7337c478bd9Sstevel@tonic-gate 		    !bcanputnext(ptm_rdq, mp->b_band)) {
7347c478bd9Sstevel@tonic-gate 			DBG(("put msg. back on Q\n"));
7357c478bd9Sstevel@tonic-gate 			(void) putbq(qp, mp);
7367c478bd9Sstevel@tonic-gate 			break;
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 		/*
7397c478bd9Sstevel@tonic-gate 		 * else send the message up master's stream
7407c478bd9Sstevel@tonic-gate 		 */
7417c478bd9Sstevel@tonic-gate 		DBG(("send message to master\n"));
7427c478bd9Sstevel@tonic-gate 		putnext(ptm_rdq, mp);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	DBG(("leaving ptswsrv\n"));
7457c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
7467c478bd9Sstevel@tonic-gate }
747