xref: /onnv-gate/usr/src/uts/common/io/pts.c (revision 581:16ccc69108c3)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*581Sedp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVR4 1.13    */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Pseudo Terminal Slave Driver.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * The pseudo-tty subsystem simulates a terminal connection, where the master
360Sstevel@tonic-gate  * side represents the terminal and the slave represents the user process's
370Sstevel@tonic-gate  * special device end point. The master device is set up as a cloned device
380Sstevel@tonic-gate  * where its major device number is the major for the clone device and its minor
390Sstevel@tonic-gate  * device number is the major for the ptm driver. There are no nodes in the file
400Sstevel@tonic-gate  * system for master devices. The master pseudo driver is opened using the
410Sstevel@tonic-gate  * open(2) system call with /dev/ptmx as the device parameter.  The clone open
420Sstevel@tonic-gate  * finds the next available minor device for the ptm major device.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * A master device is available only if it and its corresponding slave device
450Sstevel@tonic-gate  * are not already open. When the master device is opened, the corresponding
460Sstevel@tonic-gate  * slave device is automatically locked out. Only one open is allowed on a
470Sstevel@tonic-gate  * master device.  Multiple opens are allowed on the slave device.  After both
480Sstevel@tonic-gate  * the master and slave have been opened, the user has two file descriptors
490Sstevel@tonic-gate  * which are the end points of a full duplex connection composed of two streams
500Sstevel@tonic-gate  * which are automatically connected at the master and slave drivers. The user
510Sstevel@tonic-gate  * may then push modules onto either side of the stream pair.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The master and slave drivers pass all messages to their adjacent queues.
540Sstevel@tonic-gate  * Only the M_FLUSH needs some processing.  Because the read queue of one side
550Sstevel@tonic-gate  * is connected to the write queue of the other, the FLUSHR flag is changed to
560Sstevel@tonic-gate  * the FLUSHW flag and vice versa. When the master device is closed an M_HANGUP
570Sstevel@tonic-gate  * message is sent to the slave device which will render the device
580Sstevel@tonic-gate  * unusable. The process on the slave side gets the EIO when attempting to write
590Sstevel@tonic-gate  * on that stream but it will be able to read any data remaining on the stream
600Sstevel@tonic-gate  * head read queue.  When all the data has been read, read() returns 0
610Sstevel@tonic-gate  * indicating that the stream can no longer be used.  On the last close of the
620Sstevel@tonic-gate  * slave device, a 0-length message is sent to the master device. When the
630Sstevel@tonic-gate  * application on the master side issues a read() or getmsg() and 0 is returned,
640Sstevel@tonic-gate  * the user of the master device decides whether to issue a close() that
650Sstevel@tonic-gate  * dismantles the pseudo-terminal subsystem. If the master device is not closed,
660Sstevel@tonic-gate  * the pseudo-tty subsystem will be available to another user to open the slave
670Sstevel@tonic-gate  * device.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Synchronization:
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  *   All global data synchronization between ptm/pts is done via global
720Sstevel@tonic-gate  *   ptms_lock mutex which is initialized at system boot time from
730Sstevel@tonic-gate  *   ptms_initspace (called from space.c).
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  *   Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
760Sstevel@tonic-gate  *   pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  *   PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
790Sstevel@tonic-gate  *   which allow reader locks to be reacquired by the same thread (usual
800Sstevel@tonic-gate  *   reader/writer locks can't be used for that purpose since it is illegal for
810Sstevel@tonic-gate  *   a thread to acquire a lock it already holds, even as a reader). The sole
820Sstevel@tonic-gate  *   purpose of these macros is to guarantee that the peer queue will not
830Sstevel@tonic-gate  *   disappear (due to closing peer) while it is used. It is safe to use
840Sstevel@tonic-gate  *   PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
850Sstevel@tonic-gate  *   they are not real locks but reference counts).
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  *   PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave
880Sstevel@tonic-gate  *   open/close paths to modify ptm_rdq and pts_rdq fields. These fields should
890Sstevel@tonic-gate  *   be set to appropriate queues *after* qprocson() is called during open (to
900Sstevel@tonic-gate  *   prevent peer from accessing the queue with incomplete plumbing) and set to
910Sstevel@tonic-gate  *   NULL before qprocsoff() is called during close.
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  *   The pt_nullmsg field is only used in open/close routines and it is also
940Sstevel@tonic-gate  *   protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
950Sstevel@tonic-gate  *   holds.
960Sstevel@tonic-gate  *
970Sstevel@tonic-gate  * Lock Ordering:
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  *   If both ptms_lock and per-pty lock should be held, ptms_lock should always
1000Sstevel@tonic-gate  *   be entered first, followed by per-pty lock.
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * See ptms.h, ptm.c and ptms_conf.c fore more information.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #include <sys/types.h>
1070Sstevel@tonic-gate #include <sys/param.h>
1080Sstevel@tonic-gate #include <sys/sysmacros.h>
1090Sstevel@tonic-gate #include <sys/stream.h>
1100Sstevel@tonic-gate #include <sys/stropts.h>
1110Sstevel@tonic-gate #include <sys/stat.h>
1120Sstevel@tonic-gate #include <sys/errno.h>
1130Sstevel@tonic-gate #include <sys/debug.h>
1140Sstevel@tonic-gate #include <sys/cmn_err.h>
1150Sstevel@tonic-gate #include <sys/ptms.h>
1160Sstevel@tonic-gate #include <sys/systm.h>
1170Sstevel@tonic-gate #include <sys/modctl.h>
1180Sstevel@tonic-gate #include <sys/conf.h>
1190Sstevel@tonic-gate #include <sys/ddi.h>
1200Sstevel@tonic-gate #include <sys/sunddi.h>
1210Sstevel@tonic-gate #include <sys/cred.h>
1220Sstevel@tonic-gate #include <sys/zone.h>
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #ifdef DEBUG
1250Sstevel@tonic-gate int pts_debug = 0;
1260Sstevel@tonic-gate #define	DBG(a)	 if (pts_debug) cmn_err(CE_NOTE, a)
1270Sstevel@tonic-gate #else
1280Sstevel@tonic-gate #define	DBG(a)
1290Sstevel@tonic-gate #endif
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate static int ptsopen(queue_t *, dev_t *, int, int, cred_t *);
1320Sstevel@tonic-gate static int ptsclose(queue_t *, int, cred_t *);
1330Sstevel@tonic-gate static void ptswput(queue_t *, mblk_t *);
1340Sstevel@tonic-gate static void ptsrsrv(queue_t *);
1350Sstevel@tonic-gate static void ptswsrv(queue_t *);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * Slave Stream Pseudo Terminal Module: stream data structure definitions
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate static struct module_info pts_info = {
1410Sstevel@tonic-gate 	0xface,
1420Sstevel@tonic-gate 	"pts",
1430Sstevel@tonic-gate 	0,
1440Sstevel@tonic-gate 	512,
1450Sstevel@tonic-gate 	512,
1460Sstevel@tonic-gate 	128
1470Sstevel@tonic-gate };
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static struct qinit ptsrint = {
1500Sstevel@tonic-gate 	NULL,
1510Sstevel@tonic-gate 	(int (*)()) ptsrsrv,
1520Sstevel@tonic-gate 	ptsopen,
1530Sstevel@tonic-gate 	ptsclose,
1540Sstevel@tonic-gate 	NULL,
1550Sstevel@tonic-gate 	&pts_info,
1560Sstevel@tonic-gate 	NULL
1570Sstevel@tonic-gate };
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static struct qinit ptswint = {
1600Sstevel@tonic-gate 	(int (*)()) ptswput,
1610Sstevel@tonic-gate 	(int (*)()) ptswsrv,
1620Sstevel@tonic-gate 	NULL,
1630Sstevel@tonic-gate 	NULL,
1640Sstevel@tonic-gate 	NULL,
1650Sstevel@tonic-gate 	&pts_info,
1660Sstevel@tonic-gate 	NULL
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static struct streamtab ptsinfo = {
1700Sstevel@tonic-gate 	&ptsrint,
1710Sstevel@tonic-gate 	&ptswint,
1720Sstevel@tonic-gate 	NULL,
1730Sstevel@tonic-gate 	NULL
1740Sstevel@tonic-gate };
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static int pts_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
1770Sstevel@tonic-gate static int pts_attach(dev_info_t *, ddi_attach_cmd_t);
1780Sstevel@tonic-gate static int pts_detach(dev_info_t *, ddi_detach_cmd_t);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate #define	PTS_CONF_FLAG	(D_NEW | D_MP)
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate  * this will define (struct cb_ops cb_pts_ops) and (struct dev_ops pts_ops)
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(pts_ops, nulldev, nulldev,	\
1860Sstevel@tonic-gate 	pts_attach, pts_detach, nodev,			\
1870Sstevel@tonic-gate 	pts_devinfo, PTS_CONF_FLAG, &ptsinfo);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * Module linkage information for the kernel.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static struct modldrv modldrv = {
1940Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
1950Sstevel@tonic-gate 	"Slave Stream Pseudo Terminal driver 'pts'",
1960Sstevel@tonic-gate 	&pts_ops,	/* driver ops */
1970Sstevel@tonic-gate };
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate static struct modlinkage modlinkage = {
2000Sstevel@tonic-gate 	MODREV_1,
2010Sstevel@tonic-gate 	&modldrv,
2020Sstevel@tonic-gate 	NULL
2030Sstevel@tonic-gate };
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate int
2060Sstevel@tonic-gate _init(void)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	int rc;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if ((rc = mod_install(&modlinkage)) == 0)
2110Sstevel@tonic-gate 		ptms_init();
2120Sstevel@tonic-gate 	return (rc);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate int
2170Sstevel@tonic-gate _fini(void)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate int
2230Sstevel@tonic-gate _info(struct modinfo *modinfop)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static int
2290Sstevel@tonic-gate pts_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2320Sstevel@tonic-gate 		return (DDI_FAILURE);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	return (ptms_create_pts_nodes(devi));
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate static int
2380Sstevel@tonic-gate pts_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2410Sstevel@tonic-gate 		return (DDI_FAILURE);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	return (ptms_destroy_pts_nodes(devi));
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*ARGSUSED*/
2470Sstevel@tonic-gate static int
2480Sstevel@tonic-gate pts_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2490Sstevel@tonic-gate     void **result)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	int error;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	switch (infocmd) {
2540Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2550Sstevel@tonic-gate 		if (pts_dip == NULL) {
2560Sstevel@tonic-gate 			error = DDI_FAILURE;
2570Sstevel@tonic-gate 		} else {
2580Sstevel@tonic-gate 			*result = (void *)pts_dip;
2590Sstevel@tonic-gate 			error = DDI_SUCCESS;
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 		break;
2620Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2630Sstevel@tonic-gate 		*result = (void *)0;
2640Sstevel@tonic-gate 		error = DDI_SUCCESS;
2650Sstevel@tonic-gate 		break;
2660Sstevel@tonic-gate 	default:
2670Sstevel@tonic-gate 		error = DDI_FAILURE;
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 	return (error);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /* ARGSUSED */
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * Open the slave device. Reject a clone open and do not allow the
2750Sstevel@tonic-gate  * driver to be pushed. If the slave/master pair is locked or if
2760Sstevel@tonic-gate  * the master is not open, return EACCESS.
2770Sstevel@tonic-gate  * Upon success, store the write queue pointer in private data and
2780Sstevel@tonic-gate  * set the PTSOPEN bit in the pt_state field.
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate static int
2810Sstevel@tonic-gate ptsopen(
2820Sstevel@tonic-gate 	queue_t *rqp,		/* pointer to the read side queue */
2830Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
2840Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
2850Sstevel@tonic-gate 	int	sflag,		/* open state flag */
2860Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	struct pt_ttys	*ptsp;
2890Sstevel@tonic-gate 	mblk_t		*mp;
2900Sstevel@tonic-gate 	mblk_t		*mop;	/* ptr to a setopts message block */
2910Sstevel@tonic-gate 	minor_t		dminor = getminor(*devp);
2920Sstevel@tonic-gate 	struct stroptions *sop;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	DDBG("entering ptsopen(%d)", dminor);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if (sflag != 0) {
2970Sstevel@tonic-gate 		return (EINVAL);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	mutex_enter(&ptms_lock);
3010Sstevel@tonic-gate 	ptsp = ptms_minor2ptty(dminor);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (ptsp == NULL) {
3040Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3050Sstevel@tonic-gate 		return (ENXIO);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 	mutex_enter(&ptsp->pt_lock);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	/*
3100Sstevel@tonic-gate 	 * Prevent opens from zones other than the one blessed by ptm.  We
3110Sstevel@tonic-gate 	 * can't even allow the global zone to open all pts's, as it would
3120Sstevel@tonic-gate 	 * otherwise inproperly be able to claim pts's already opened by zones.
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	if (ptsp->pt_zoneid != getzoneid()) {
3150Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3160Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3170Sstevel@tonic-gate 		return (EPERM);
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * Allow reopen of this device.
3220Sstevel@tonic-gate 	 */
3230Sstevel@tonic-gate 	if (rqp->q_ptr != NULL) {
324*581Sedp 		ASSERT(rqp->q_ptr == ptsp);
325*581Sedp 		ASSERT(ptsp->pts_rdq == rqp);
3260Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3270Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3280Sstevel@tonic-gate 		return (0);
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	DDBGP("ptsopen: p = %p\n", (uintptr_t)ptsp);
3320Sstevel@tonic-gate 	DDBG("ptsopen: state = %x\n", ptsp->pt_state);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	ASSERT(ptsp->pt_minor == dminor);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if ((ptsp->pt_state & PTLOCK) || !(ptsp->pt_state & PTMOPEN)) {
3380Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3390Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3400Sstevel@tonic-gate 		return (EAGAIN);
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * if already, open simply return...
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	if (ptsp->pt_state & PTSOPEN) {
347*581Sedp 		ASSERT(rqp->q_ptr == ptsp);
348*581Sedp 		ASSERT(ptsp->pts_rdq == rqp);
3490Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3500Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3510Sstevel@tonic-gate 		return (0);
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * Allocate message block for setting stream head options.
3560Sstevel@tonic-gate 	 */
3570Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
3580Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3590Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3600Sstevel@tonic-gate 		return (ENOMEM);
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/*
3640Sstevel@tonic-gate 	 * Slave should send zero-length message to a master when it is
3650Sstevel@tonic-gate 	 * closing. If memory is low at that time, master will not detect slave
3660Sstevel@tonic-gate 	 * closes, this pty will not be deallocated. So, preallocate this
3670Sstevel@tonic-gate 	 * zero-length message block early.
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	if ((mp = allocb(0, BPRI_MED)) == NULL) {
3700Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3710Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3720Sstevel@tonic-gate 		freemsg(mop);
3730Sstevel@tonic-gate 		return (ENOMEM);
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	ptsp->pt_state |= PTSOPEN;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = ptsp;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	mutex_exit(&ptsp->pt_lock);
3810Sstevel@tonic-gate 	mutex_exit(&ptms_lock);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	qprocson(rqp);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/*
3860Sstevel@tonic-gate 	 * After qprocson pts driver is fully plumbed into the stream and can
3870Sstevel@tonic-gate 	 * send/receive messages. Setting pts_rdq will allow master side to send
3880Sstevel@tonic-gate 	 * messages to the slave. This setting can't occur before qprocson() is
3890Sstevel@tonic-gate 	 * finished because slave is not ready to process them.
3900Sstevel@tonic-gate 	 */
3910Sstevel@tonic-gate 	PT_ENTER_WRITE(ptsp);
3920Sstevel@tonic-gate 	ptsp->pts_rdq = rqp;
3930Sstevel@tonic-gate 	ASSERT(ptsp->pt_nullmsg == NULL);
3940Sstevel@tonic-gate 	ptsp->pt_nullmsg = mp;
3950Sstevel@tonic-gate 	PT_EXIT_WRITE(ptsp);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue
3990Sstevel@tonic-gate 	 * and add controlling tty if not set
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
4030Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
4040Sstevel@tonic-gate 	sop = (struct stroptions *)mop->b_rptr;
4050Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
4060Sstevel@tonic-gate 	sop->so_hiwat = 512;
4070Sstevel@tonic-gate 	sop->so_lowat = 256;
4080Sstevel@tonic-gate 	putnext(rqp, mop);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	return (0);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate  * Find the address to private data identifying the slave's write
4170Sstevel@tonic-gate  * queue. Send a 0-length msg up the slave's read queue to designate
4180Sstevel@tonic-gate  * the master is closing. Uattach the master from the slave by nulling
4190Sstevel@tonic-gate  * out master's write queue field in private data.
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate /*ARGSUSED1*/
4220Sstevel@tonic-gate static int
4230Sstevel@tonic-gate ptsclose(queue_t *rqp, int flag, cred_t *credp)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	struct pt_ttys	*ptsp;
4260Sstevel@tonic-gate 	queue_t *wqp;
4270Sstevel@tonic-gate 	mblk_t	*mp;
4280Sstevel@tonic-gate 	mblk_t	*bp;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/*
4310Sstevel@tonic-gate 	 * q_ptr should never be NULL in the close routine and it is checked in
4320Sstevel@tonic-gate 	 * DEBUG kernel by ASSERT. For non-DEBUG kernel the attempt is made to
4330Sstevel@tonic-gate 	 * behave gracefully.
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	ASSERT(rqp->q_ptr != NULL);
4360Sstevel@tonic-gate 	if (rqp->q_ptr == NULL) {
4370Sstevel@tonic-gate 		qprocsoff(rqp);
4380Sstevel@tonic-gate 		return (0);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)rqp->q_ptr;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * Slave is going to close and doesn't want any new  messages coming
4450Sstevel@tonic-gate 	 * from the master side, so set pts_rdq to NULL. This should be done
4460Sstevel@tonic-gate 	 * before call to qprocsoff() since slave can't process additional
4470Sstevel@tonic-gate 	 * messages from the master after qprocsoff is called.
4480Sstevel@tonic-gate 	 */
4490Sstevel@tonic-gate 	PT_ENTER_WRITE(ptsp);
4500Sstevel@tonic-gate 	mp = ptsp->pt_nullmsg;
4510Sstevel@tonic-gate 	ptsp->pt_nullmsg = NULL;
4520Sstevel@tonic-gate 	ptsp->pts_rdq = NULL;
4530Sstevel@tonic-gate 	PT_EXIT_WRITE(ptsp);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/*
4560Sstevel@tonic-gate 	 * Drain the ouput
4570Sstevel@tonic-gate 	 */
4580Sstevel@tonic-gate 	wqp = WR(rqp);
4590Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
4600Sstevel@tonic-gate 	while ((bp = getq(wqp)) != NULL) {
4610Sstevel@tonic-gate 		if (ptsp->ptm_rdq) {
4620Sstevel@tonic-gate 			putnext(ptsp->ptm_rdq, bp);
4630Sstevel@tonic-gate 		} else if (bp->b_datap->db_type == M_IOCTL) {
4640Sstevel@tonic-gate 			bp->b_datap->db_type = M_IOCNAK;
4650Sstevel@tonic-gate 			freemsg(bp->b_cont);
4660Sstevel@tonic-gate 			bp->b_cont = NULL;
4670Sstevel@tonic-gate 			qreply(wqp, bp);
4680Sstevel@tonic-gate 		} else {
4690Sstevel@tonic-gate 			freemsg(bp);
4700Sstevel@tonic-gate 		}
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 	/*
4730Sstevel@tonic-gate 	 * qenable master side write queue so that it can flush
4740Sstevel@tonic-gate 	 * its messages as slaves's read queue is going away
4750Sstevel@tonic-gate 	 */
4760Sstevel@tonic-gate 	if (ptsp->ptm_rdq) {
4770Sstevel@tonic-gate 		if (mp)
4780Sstevel@tonic-gate 			putnext(ptsp->ptm_rdq, mp);
4790Sstevel@tonic-gate 		else
4800Sstevel@tonic-gate 			qenable(WR(ptsp->ptm_rdq));
4810Sstevel@tonic-gate 	} else
4820Sstevel@tonic-gate 		freemsg(mp);
4830Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	qprocsoff(rqp);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	rqp->q_ptr = NULL;
4880Sstevel@tonic-gate 	WR(rqp)->q_ptr = NULL;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	ptms_close(ptsp, PTSOPEN | PTSTTY);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	return (0);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate  * The wput procedure will only handle flush messages.
4980Sstevel@tonic-gate  * All other messages are queued and the write side
4990Sstevel@tonic-gate  * service procedure sends them off to the master side.
5000Sstevel@tonic-gate  */
5010Sstevel@tonic-gate static void
5020Sstevel@tonic-gate ptswput(queue_t *qp, mblk_t *mp)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	struct pt_ttys *ptsp;
5050Sstevel@tonic-gate 	struct iocblk  *iocp;
5060Sstevel@tonic-gate 	unsigned char type = mp->b_datap->db_type;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	DBG(("entering ptswput\n"));
5090Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)qp->q_ptr;
5120Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
5130Sstevel@tonic-gate 	if (ptsp->ptm_rdq == NULL) {
5140Sstevel@tonic-gate 		DBG(("in write put proc but no master\n"));
5150Sstevel@tonic-gate 		/*
5160Sstevel@tonic-gate 		 * NAK ioctl as slave side read queue is gone.
5170Sstevel@tonic-gate 		 * Or else free the message.
5180Sstevel@tonic-gate 		 */
5190Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
5200Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCNAK;
5210Sstevel@tonic-gate 			freemsg(mp->b_cont);
5220Sstevel@tonic-gate 			mp->b_cont = NULL;
5230Sstevel@tonic-gate 			qreply(qp, mp);
5240Sstevel@tonic-gate 		} else
5250Sstevel@tonic-gate 			freemsg(mp);
5260Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
5270Sstevel@tonic-gate 		return;
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	if (type >= QPCTL) {
5310Sstevel@tonic-gate 	    switch (type) {
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		/*
5340Sstevel@tonic-gate 		 * if write queue request, flush slave's write
5350Sstevel@tonic-gate 		 * queue and send FLUSHR to ptm. If read queue
5360Sstevel@tonic-gate 		 * request, send FLUSHR to ptm.
5370Sstevel@tonic-gate 		 */
5380Sstevel@tonic-gate 	    case M_FLUSH:
5390Sstevel@tonic-gate 		DBG(("pts got flush request\n"));
5400Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 			DBG(("got FLUSHW, flush pts write Q\n"));
5430Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHBAND)
5440Sstevel@tonic-gate 				/*
5450Sstevel@tonic-gate 				 * if it is a FLUSHBAND, do flushband.
5460Sstevel@tonic-gate 				 */
5470Sstevel@tonic-gate 				flushband(qp, *(mp->b_rptr + 1), FLUSHDATA);
5480Sstevel@tonic-gate 			else
5490Sstevel@tonic-gate 				flushq(qp, FLUSHDATA);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
5520Sstevel@tonic-gate 			if ((*mp->b_rptr & FLUSHR) == 0) {
5530Sstevel@tonic-gate 				/*
5540Sstevel@tonic-gate 				 * FLUSHW only. Change to FLUSHR and putnext
5550Sstevel@tonic-gate 				 * to ptm, then we are done.
5560Sstevel@tonic-gate 				 */
5570Sstevel@tonic-gate 				*mp->b_rptr |= FLUSHR;
5580Sstevel@tonic-gate 				if (ptsp->ptm_rdq)
5590Sstevel@tonic-gate 					putnext(ptsp->ptm_rdq, mp);
5600Sstevel@tonic-gate 				break;
5610Sstevel@tonic-gate 			} else {
5620Sstevel@tonic-gate 				mblk_t *nmp;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 				/* It is a FLUSHRW. Duplicate the mblk */
5650Sstevel@tonic-gate 				nmp = copyb(mp);
5660Sstevel@tonic-gate 				if (nmp) {
5670Sstevel@tonic-gate 					/*
5680Sstevel@tonic-gate 					 * Change FLUSHW to FLUSHR before
5690Sstevel@tonic-gate 					 * putnext to ptm.
5700Sstevel@tonic-gate 					 */
5710Sstevel@tonic-gate 					DBG(("putnext nmp(FLUSHR) to ptm\n"));
5720Sstevel@tonic-gate 					*nmp->b_rptr |= FLUSHR;
5730Sstevel@tonic-gate 					if (ptsp->ptm_rdq)
5740Sstevel@tonic-gate 						putnext(ptsp->ptm_rdq, nmp);
5750Sstevel@tonic-gate 				}
5760Sstevel@tonic-gate 			}
5770Sstevel@tonic-gate 		}
5780Sstevel@tonic-gate 		/*
5790Sstevel@tonic-gate 		 * Since the packet module will toss any
5800Sstevel@tonic-gate 		 * M_FLUSHES sent to the master's stream head
5810Sstevel@tonic-gate 		 * read queue, we simply turn it around here.
5820Sstevel@tonic-gate 		 */
5830Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
5840Sstevel@tonic-gate 			ASSERT(RD(qp)->q_first == NULL);
5850Sstevel@tonic-gate 			DBG(("qreply(qp) turning FLUSHR around\n"));
5860Sstevel@tonic-gate 			qreply(qp, mp);
5870Sstevel@tonic-gate 		} else {
5880Sstevel@tonic-gate 			freemsg(mp);
5890Sstevel@tonic-gate 		}
5900Sstevel@tonic-gate 		break;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	    case M_READ:
5930Sstevel@tonic-gate 		/* Caused by ldterm - can not pass to master */
5940Sstevel@tonic-gate 		freemsg(mp);
5950Sstevel@tonic-gate 		break;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	    default:
5980Sstevel@tonic-gate 		if (ptsp->ptm_rdq)
5990Sstevel@tonic-gate 			putnext(ptsp->ptm_rdq, mp);
6000Sstevel@tonic-gate 		break;
6010Sstevel@tonic-gate 	    }
6020Sstevel@tonic-gate 	    PT_EXIT_READ(ptsp);
6030Sstevel@tonic-gate 	    return;
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	switch (type) {
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	case M_IOCTL:
6090Sstevel@tonic-gate 		/*
6100Sstevel@tonic-gate 		 * For case PTSSTTY set the flag PTSTTY and ACK
6110Sstevel@tonic-gate 		 * the ioctl so that the user program can push
6120Sstevel@tonic-gate 		 * the associated modules to get tty semantics.
6130Sstevel@tonic-gate 		 * See bugid 4025044
6140Sstevel@tonic-gate 		 */
6150Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
6160Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
6170Sstevel@tonic-gate 		default:
6180Sstevel@tonic-gate 			break;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		case PTSSTTY:
6210Sstevel@tonic-gate 			if (ptsp->pt_state & PTSTTY) {
6220Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCNAK;
6230Sstevel@tonic-gate 				iocp->ioc_error = EEXIST;
6240Sstevel@tonic-gate 			} else {
6250Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
6260Sstevel@tonic-gate 				mutex_enter(&ptsp->pt_lock);
6270Sstevel@tonic-gate 				ptsp->pt_state |= PTSTTY;
6280Sstevel@tonic-gate 				mutex_exit(&ptsp->pt_lock);
6290Sstevel@tonic-gate 				iocp->ioc_error = 0;
6300Sstevel@tonic-gate 			}
6310Sstevel@tonic-gate 			iocp->ioc_count = 0;
6320Sstevel@tonic-gate 			qreply(qp, mp);
6330Sstevel@tonic-gate 			PT_EXIT_READ(ptsp);
6340Sstevel@tonic-gate 			return;
6350Sstevel@tonic-gate 		}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	default:
6380Sstevel@tonic-gate 		/*
6390Sstevel@tonic-gate 		 * send other messages to the master
6400Sstevel@tonic-gate 		 */
6410Sstevel@tonic-gate 		DBG(("put msg on slave's write queue\n"));
6420Sstevel@tonic-gate 		(void) putq(qp, mp);
6430Sstevel@tonic-gate 		break;
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
6470Sstevel@tonic-gate 	DBG(("return from ptswput()\n"));
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate  * enable the write side of the master. This triggers the
6530Sstevel@tonic-gate  * master to send any messages queued on its write side to
6540Sstevel@tonic-gate  * the read side of this slave.
6550Sstevel@tonic-gate  */
6560Sstevel@tonic-gate static void
6570Sstevel@tonic-gate ptsrsrv(queue_t *qp)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	struct pt_ttys *ptsp;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	DBG(("entering ptsrsrv\n"));
6620Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)qp->q_ptr;
6650Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
6660Sstevel@tonic-gate 	if (ptsp->ptm_rdq == NULL) {
6670Sstevel@tonic-gate 		DBG(("in read srv proc but no master\n"));
6680Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
6690Sstevel@tonic-gate 		return;
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 	qenable(WR(ptsp->ptm_rdq));
6720Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
6730Sstevel@tonic-gate 	DBG(("leaving ptsrsrv\n"));
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate  * If there are messages on this queue that can be sent to
6780Sstevel@tonic-gate  * master, send them via putnext(). Else, if queued messages
6790Sstevel@tonic-gate  * cannot be sent, leave them on this queue. If priority
6800Sstevel@tonic-gate  * messages on this queue, send them to master no matter what.
6810Sstevel@tonic-gate  */
6820Sstevel@tonic-gate static void
6830Sstevel@tonic-gate ptswsrv(queue_t *qp)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	struct pt_ttys *ptsp;
6860Sstevel@tonic-gate 	queue_t *ptm_rdq;
6870Sstevel@tonic-gate 	mblk_t *mp;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	DBG(("entering ptswsrv\n"));
6900Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)qp->q_ptr;
6930Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
6940Sstevel@tonic-gate 	if (ptsp->ptm_rdq == NULL) {
6950Sstevel@tonic-gate 		DBG(("in write srv proc but no master\n"));
6960Sstevel@tonic-gate 		/*
6970Sstevel@tonic-gate 		 * Free messages on the write queue and send
6980Sstevel@tonic-gate 		 * NAK for any M_IOCTL type messages to wakeup
6990Sstevel@tonic-gate 		 * the user process waiting for ACK/NAK from
7000Sstevel@tonic-gate 		 * the ioctl invocation
7010Sstevel@tonic-gate 		 */
7020Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
7030Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL) {
7040Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCNAK;
7050Sstevel@tonic-gate 				freemsg(mp->b_cont);
7060Sstevel@tonic-gate 				mp->b_cont = NULL;
7070Sstevel@tonic-gate 				qreply(qp, mp);
7080Sstevel@tonic-gate 			} else
7090Sstevel@tonic-gate 				freemsg(mp);
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
7120Sstevel@tonic-gate 		return;
7130Sstevel@tonic-gate 	} else {
7140Sstevel@tonic-gate 		ptm_rdq = ptsp->ptm_rdq;
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/*
7180Sstevel@tonic-gate 	 * while there are messages on this write queue...
7190Sstevel@tonic-gate 	 */
7200Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
7210Sstevel@tonic-gate 		/*
7220Sstevel@tonic-gate 		 * if don't have control message and cannot put
7230Sstevel@tonic-gate 		 * msg. on master's read queue, put it back on
7240Sstevel@tonic-gate 		 * this queue.
7250Sstevel@tonic-gate 		 */
7260Sstevel@tonic-gate 		if (mp->b_datap->db_type <= QPCTL &&
7270Sstevel@tonic-gate 		    !bcanputnext(ptm_rdq, mp->b_band)) {
7280Sstevel@tonic-gate 			DBG(("put msg. back on Q\n"));
7290Sstevel@tonic-gate 			(void) putbq(qp, mp);
7300Sstevel@tonic-gate 			break;
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 		/*
7330Sstevel@tonic-gate 		 * else send the message up master's stream
7340Sstevel@tonic-gate 		 */
7350Sstevel@tonic-gate 		DBG(("send message to master\n"));
7360Sstevel@tonic-gate 		putnext(ptm_rdq, mp);
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 	DBG(("leaving ptswsrv\n"));
7390Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
7400Sstevel@tonic-gate }
741