xref: /illumos-gate/usr/src/uts/common/io/pts.c (revision 1fa2a664)
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
5facf4a8dSllai  * Common Development and Distribution License (the "License").
6facf4a8dSllai  * 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 /*
2219397407SSherry 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	*/
262c26b085SToomas Soome /*	  All Rights Reserved	*/
277c478bd9Sstevel@tonic-gate 
287d8deab2SAndy Fiddaman /*
297d8deab2SAndy Fiddaman  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
30*1fa2a664SJoshua M. Clulow  * Copyright 2021 Oxide Computer Company
317d8deab2SAndy Fiddaman  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
34*1fa2a664SJoshua M. Clulow  * PSEUDO-TERMINAL SUBSIDIARY DRIVER (PTS)
357c478bd9Sstevel@tonic-gate  *
36*1fa2a664SJoshua M. Clulow  * The pseudo-terminal subsystem simulates a terminal connection, where the
37*1fa2a664SJoshua M. Clulow  * manager side represents the terminal and the subsidiary represents the user
38*1fa2a664SJoshua M. Clulow  * process's special device end point.  The manager device is set up as a
39*1fa2a664SJoshua M. Clulow  * cloned device where its major device number is the major for the clone
40*1fa2a664SJoshua M. Clulow  * device and its minor device number is the major for the ptm driver.  There
41*1fa2a664SJoshua M. Clulow  * are no nodes in the file system for manager devices.  The manager pseudo
42*1fa2a664SJoshua M. Clulow  * driver is opened using the open(2) system call with /dev/ptmx as the device
43*1fa2a664SJoshua M. Clulow  * parameter.  The clone open finds the next available minor device for the ptm
44*1fa2a664SJoshua M. Clulow  * major device.
457c478bd9Sstevel@tonic-gate  *
46*1fa2a664SJoshua M. Clulow  * A manager device is available only if it and its corresponding subsidiary
47*1fa2a664SJoshua M. Clulow  * device are not already open.  When the manager device is opened, the
48*1fa2a664SJoshua M. Clulow  * corresponding subsidiary device is automatically locked out.  Only one open
49*1fa2a664SJoshua M. Clulow  * is allowed on a manager device.  Multiple opens are allowed on the
50*1fa2a664SJoshua M. Clulow  * subsidiary device.  After both the manager and subsidiary have been opened,
51*1fa2a664SJoshua M. Clulow  * the user has two file descriptors which are the end points of a full duplex
52*1fa2a664SJoshua M. Clulow  * connection composed of two streams which are automatically connected at the
53*1fa2a664SJoshua M. Clulow  * manager and subsidiary drivers.  The user may then push modules onto either
54*1fa2a664SJoshua M. Clulow  * side of the stream pair.
557c478bd9Sstevel@tonic-gate  *
56*1fa2a664SJoshua M. Clulow  * The manager and subsidiary drivers pass all messages to their adjacent
57*1fa2a664SJoshua M. Clulow  * queues.  Only the M_FLUSH needs some processing.  Because the read queue of
58*1fa2a664SJoshua M. Clulow  * one side is connected to the write queue of the other, the FLUSHR flag is
59*1fa2a664SJoshua M. Clulow  * changed to the FLUSHW flag and vice versa.  When the manager device is
60*1fa2a664SJoshua M. Clulow  * closed an M_HANGUP message is sent to the subsidiary device which will
61*1fa2a664SJoshua M. Clulow  * render the device unusable.  The process on the subsidiary side gets the EIO
62*1fa2a664SJoshua M. Clulow  * when attempting to write on that stream but it will be able to read any data
63*1fa2a664SJoshua M. Clulow  * remaining on the stream head read queue.  When all the data has been read,
64*1fa2a664SJoshua M. Clulow  * read() returns 0 indicating that the stream can no longer be used.  On the
65*1fa2a664SJoshua M. Clulow  * last close of the subsidiary device, a 0-length message is sent to the
66*1fa2a664SJoshua M. Clulow  * manager device.  When the application on the manager side issues a read() or
67*1fa2a664SJoshua M. Clulow  * getmsg() and 0 is returned, the user of the manager device decides whether
68*1fa2a664SJoshua M. Clulow  * to issue a close() that dismantles the pseudo-terminal subsystem.  If the
69*1fa2a664SJoshua M. Clulow  * manager device is not closed, the pseudo-tty subsystem will be available to
70*1fa2a664SJoshua M. Clulow  * another user to open the subsidiary device.
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  *
73*1fa2a664SJoshua M. Clulow  * SYNCHRONIZATION
747c478bd9Sstevel@tonic-gate  *
75*1fa2a664SJoshua M. Clulow  * All global data synchronization between ptm/pts is done via global ptms_lock
76*1fa2a664SJoshua M. Clulow  * mutex which is initialized at system boot time from ptms_initspace (called
77*1fa2a664SJoshua M. Clulow  * from space.c).
787c478bd9Sstevel@tonic-gate  *
79*1fa2a664SJoshua M. Clulow  * Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
80*1fa2a664SJoshua M. Clulow  * pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
817c478bd9Sstevel@tonic-gate  *
82*1fa2a664SJoshua M. Clulow  * PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
83*1fa2a664SJoshua M. Clulow  * which allow reader locks to be reacquired by the same thread (usual
84*1fa2a664SJoshua M. Clulow  * reader/writer locks can't be used for that purpose since it is illegal for a
85*1fa2a664SJoshua M. Clulow  * thread to acquire a lock it already holds, even as a reader).  The sole
86*1fa2a664SJoshua M. Clulow  * purpose of these macros is to guarantee that the peer queue will not
87*1fa2a664SJoshua M. Clulow  * disappear (due to closing peer) while it is used.  It is safe to use
88*1fa2a664SJoshua M. Clulow  * PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
89*1fa2a664SJoshua M. Clulow  * they are not real locks but reference counts).
907c478bd9Sstevel@tonic-gate  *
91*1fa2a664SJoshua M. Clulow  * PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in manager/subsidiary
92*1fa2a664SJoshua M. Clulow  * open/close paths to modify ptm_rdq and pts_rdq fields.  These fields should
93*1fa2a664SJoshua M. Clulow  * be set to appropriate queues *after* qprocson() is called during open (to
94*1fa2a664SJoshua M. Clulow  * prevent peer from accessing the queue with incomplete plumbing) and set to
95*1fa2a664SJoshua M. Clulow  * NULL before qprocsoff() is called during close.
967c478bd9Sstevel@tonic-gate  *
97*1fa2a664SJoshua M. Clulow  * The pt_nullmsg field is only used in open/close routines and it is also
98*1fa2a664SJoshua M. Clulow  * protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
99*1fa2a664SJoshua M. Clulow  * holds.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  *
102*1fa2a664SJoshua M. Clulow  * LOCK ORDERING
103*1fa2a664SJoshua M. Clulow  *
104*1fa2a664SJoshua M. Clulow  * If both ptms_lock and per-pty lock should be held, ptms_lock should always
105*1fa2a664SJoshua M. Clulow  * be entered first, followed by per-pty lock.
1067c478bd9Sstevel@tonic-gate  *
107*1fa2a664SJoshua M. Clulow  * See ptms.h, ptm.c and ptms_conf.c fore more information.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #include <sys/types.h>
1117c478bd9Sstevel@tonic-gate #include <sys/param.h>
1127c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1137c478bd9Sstevel@tonic-gate #include <sys/stream.h>
1147c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
1157d8deab2SAndy Fiddaman #include <sys/strsubr.h>
1167c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1177c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1187c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1197c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1207c478bd9Sstevel@tonic-gate #include <sys/ptms.h>
1217c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1227c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1237c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1247c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1257c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1267c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1277c478bd9Sstevel@tonic-gate #include <sys/zone.h>
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #ifdef DEBUG
1307c478bd9Sstevel@tonic-gate int pts_debug = 0;
1317c478bd9Sstevel@tonic-gate #define	DBG(a)	 if (pts_debug) cmn_err(CE_NOTE, a)
1327c478bd9Sstevel@tonic-gate #else
1337c478bd9Sstevel@tonic-gate #define	DBG(a)
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static int ptsopen(queue_t *, dev_t *, int, int, cred_t *);
1377c478bd9Sstevel@tonic-gate static int ptsclose(queue_t *, int, cred_t *);
1382c26b085SToomas Soome static int ptswput(queue_t *, mblk_t *);
1392c26b085SToomas Soome static int ptsrsrv(queue_t *);
1402c26b085SToomas Soome static int ptswsrv(queue_t *);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static struct module_info pts_info = {
1437c478bd9Sstevel@tonic-gate 	0xface,
1447c478bd9Sstevel@tonic-gate 	"pts",
1457c478bd9Sstevel@tonic-gate 	0,
1462463e920SRichard Lowe 	_TTY_BUFSIZ,
1472463e920SRichard Lowe 	_TTY_BUFSIZ,
1487c478bd9Sstevel@tonic-gate 	128
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static struct qinit ptsrint = {
1527c478bd9Sstevel@tonic-gate 	NULL,
1532c26b085SToomas Soome 	ptsrsrv,
1547c478bd9Sstevel@tonic-gate 	ptsopen,
1557c478bd9Sstevel@tonic-gate 	ptsclose,
1567c478bd9Sstevel@tonic-gate 	NULL,
1577c478bd9Sstevel@tonic-gate 	&pts_info,
1587c478bd9Sstevel@tonic-gate 	NULL
1597c478bd9Sstevel@tonic-gate };
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static struct qinit ptswint = {
1622c26b085SToomas Soome 	ptswput,
1632c26b085SToomas Soome 	ptswsrv,
1647c478bd9Sstevel@tonic-gate 	NULL,
1657c478bd9Sstevel@tonic-gate 	NULL,
1667c478bd9Sstevel@tonic-gate 	NULL,
1677c478bd9Sstevel@tonic-gate 	&pts_info,
1687c478bd9Sstevel@tonic-gate 	NULL
1697c478bd9Sstevel@tonic-gate };
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static struct streamtab ptsinfo = {
1727c478bd9Sstevel@tonic-gate 	&ptsrint,
1737c478bd9Sstevel@tonic-gate 	&ptswint,
1747c478bd9Sstevel@tonic-gate 	NULL,
1757c478bd9Sstevel@tonic-gate 	NULL
1767c478bd9Sstevel@tonic-gate };
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static int pts_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
1797c478bd9Sstevel@tonic-gate static int pts_attach(dev_info_t *, ddi_attach_cmd_t);
1807c478bd9Sstevel@tonic-gate static int pts_detach(dev_info_t *, ddi_detach_cmd_t);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #define	PTS_CONF_FLAG	(D_NEW | D_MP)
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * this will define (struct cb_ops cb_pts_ops) and (struct dev_ops pts_ops)
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(pts_ops, nulldev, nulldev,	\
1882463e920SRichard Lowe     pts_attach, pts_detach, nodev,			\
1892463e920SRichard Lowe     pts_devinfo, PTS_CONF_FLAG, &ptsinfo, ddi_quiesce_not_supported);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
196*1fa2a664SJoshua M. Clulow 	&mod_driverops,
197*1fa2a664SJoshua M. Clulow 	"Pseudo-Terminal Subsidiary Driver",
198*1fa2a664SJoshua M. Clulow 	&pts_ops,
1997c478bd9Sstevel@tonic-gate };
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2027c478bd9Sstevel@tonic-gate 	MODREV_1,
2037c478bd9Sstevel@tonic-gate 	&modldrv,
2047c478bd9Sstevel@tonic-gate 	NULL
2057c478bd9Sstevel@tonic-gate };
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate int
_init(void)2087c478bd9Sstevel@tonic-gate _init(void)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	int rc;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if ((rc = mod_install(&modlinkage)) == 0)
2137c478bd9Sstevel@tonic-gate 		ptms_init();
2147c478bd9Sstevel@tonic-gate 	return (rc);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate int
_fini(void)2197c478bd9Sstevel@tonic-gate _fini(void)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2257c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static int
pts_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2317c478bd9Sstevel@tonic-gate pts_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2347c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2357c478bd9Sstevel@tonic-gate 
236facf4a8dSllai 	mutex_enter(&ptms_lock);
237facf4a8dSllai 	pts_dip = devi;
238facf4a8dSllai 	mutex_exit(&ptms_lock);
239facf4a8dSllai 
240facf4a8dSllai 	return (DDI_SUCCESS);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate static int
pts_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2447c478bd9Sstevel@tonic-gate pts_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2477c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2487c478bd9Sstevel@tonic-gate 
249facf4a8dSllai 	/*
250facf4a8dSllai 	 * For now, pts cannot be detached.
251facf4a8dSllai 	 */
252facf4a8dSllai 	return (DDI_FAILURE);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate static int
pts_devinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2567c478bd9Sstevel@tonic-gate pts_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2577c478bd9Sstevel@tonic-gate     void **result)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	int error;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2627c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2637c478bd9Sstevel@tonic-gate 		if (pts_dip == NULL) {
2647c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
2657c478bd9Sstevel@tonic-gate 		} else {
2667c478bd9Sstevel@tonic-gate 			*result = (void *)pts_dip;
2677c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 		break;
2707c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2717c478bd9Sstevel@tonic-gate 		*result = (void *)0;
2727c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
2737c478bd9Sstevel@tonic-gate 		break;
2747c478bd9Sstevel@tonic-gate 	default:
2757c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	return (error);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /* ARGSUSED */
2817c478bd9Sstevel@tonic-gate /*
282*1fa2a664SJoshua M. Clulow  * Open the subsidiary device. Reject a clone open and do not allow the
283*1fa2a664SJoshua M. Clulow  * driver to be pushed. If the subsidiary/manager pair is locked or if
284*1fa2a664SJoshua M. Clulow  * the manager is not open, return EACCESS.
2857c478bd9Sstevel@tonic-gate  * Upon success, store the write queue pointer in private data and
2867c478bd9Sstevel@tonic-gate  * set the PTSOPEN bit in the pt_state field.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate static int
ptsopen(queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)2897c478bd9Sstevel@tonic-gate ptsopen(
2907c478bd9Sstevel@tonic-gate 	queue_t *rqp,		/* pointer to the read side queue */
2917c478bd9Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
2927c478bd9Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
2937c478bd9Sstevel@tonic-gate 	int	sflag,		/* open state flag */
2947c478bd9Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptsp;
2977c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
2987c478bd9Sstevel@tonic-gate 	mblk_t		*mop;	/* ptr to a setopts message block */
2997c478bd9Sstevel@tonic-gate 	minor_t		dminor = getminor(*devp);
3007c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	DDBG("entering ptsopen(%d)", dminor);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if (sflag != 0) {
3057c478bd9Sstevel@tonic-gate 		return (EINVAL);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	mutex_enter(&ptms_lock);
3097c478bd9Sstevel@tonic-gate 	ptsp = ptms_minor2ptty(dminor);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if (ptsp == NULL) {
3127c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3137c478bd9Sstevel@tonic-gate 		return (ENXIO);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	mutex_enter(&ptsp->pt_lock);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/*
3187c478bd9Sstevel@tonic-gate 	 * Prevent opens from zones other than the one blessed by ptm.  We
3197c478bd9Sstevel@tonic-gate 	 * can't even allow the global zone to open all pts's, as it would
3207c478bd9Sstevel@tonic-gate 	 * otherwise inproperly be able to claim pts's already opened by zones.
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 	if (ptsp->pt_zoneid != getzoneid()) {
3237c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3247c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3257c478bd9Sstevel@tonic-gate 		return (EPERM);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * Allow reopen of this device.
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 	if (rqp->q_ptr != NULL) {
332fbe27353Sedp 		ASSERT(rqp->q_ptr == ptsp);
333fbe27353Sedp 		ASSERT(ptsp->pts_rdq == rqp);
3347c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3357c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3367c478bd9Sstevel@tonic-gate 		return (0);
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	DDBGP("ptsopen: p = %p\n", (uintptr_t)ptsp);
3407c478bd9Sstevel@tonic-gate 	DDBG("ptsopen: state = %x\n", ptsp->pt_state);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	ASSERT(ptsp->pt_minor == dminor);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if ((ptsp->pt_state & PTLOCK) || !(ptsp->pt_state & PTMOPEN)) {
3457c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3467c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3477c478bd9Sstevel@tonic-gate 		return (EAGAIN);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/*
3517d8deab2SAndy Fiddaman 	 * if already open, simply return...
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	if (ptsp->pt_state & PTSOPEN) {
354fbe27353Sedp 		ASSERT(rqp->q_ptr == ptsp);
355fbe27353Sedp 		ASSERT(ptsp->pts_rdq == rqp);
3567c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3577c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3587c478bd9Sstevel@tonic-gate 		return (0);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * Allocate message block for setting stream head options.
3637c478bd9Sstevel@tonic-gate 	 */
3647c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
3657c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3667c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3677c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/*
371*1fa2a664SJoshua M. Clulow 	 * Subsidiary should send zero-length message to a manager when it is
372*1fa2a664SJoshua M. Clulow 	 * closing.  If memory is low at that time, manager will not detect
373*1fa2a664SJoshua M. Clulow 	 * subsidiary closes, this pty will not be deallocated.  So,
374*1fa2a664SJoshua M. Clulow 	 * preallocate this zero-length message block early.
3757c478bd9Sstevel@tonic-gate 	 */
3767c478bd9Sstevel@tonic-gate 	if ((mp = allocb(0, BPRI_MED)) == NULL) {
3777c478bd9Sstevel@tonic-gate 		mutex_exit(&ptsp->pt_lock);
3787c478bd9Sstevel@tonic-gate 		mutex_exit(&ptms_lock);
3797c478bd9Sstevel@tonic-gate 		freemsg(mop);
3807c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	ptsp->pt_state |= PTSOPEN;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = ptsp;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	mutex_exit(&ptsp->pt_lock);
3887c478bd9Sstevel@tonic-gate 	mutex_exit(&ptms_lock);
3897c478bd9Sstevel@tonic-gate 
3907d8deab2SAndy Fiddaman 	if (ptsp->pt_state & PTSTTY)
3917d8deab2SAndy Fiddaman 		STREAM(rqp)->sd_flag |= STRXPG4TTY;
3927d8deab2SAndy Fiddaman 
3937c478bd9Sstevel@tonic-gate 	qprocson(rqp);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	/*
3967c478bd9Sstevel@tonic-gate 	 * After qprocson pts driver is fully plumbed into the stream and can
397*1fa2a664SJoshua M. Clulow 	 * send/receive messages.  Setting pts_rdq will allow manager side to
398*1fa2a664SJoshua M. Clulow 	 * send messages to the subsidiary.  This setting can't occur before
399*1fa2a664SJoshua M. Clulow 	 * qprocson() is finished because subsidiary is not ready to process
400*1fa2a664SJoshua M. Clulow 	 * them.
4017c478bd9Sstevel@tonic-gate 	 */
4027c478bd9Sstevel@tonic-gate 	PT_ENTER_WRITE(ptsp);
4037c478bd9Sstevel@tonic-gate 	ptsp->pts_rdq = rqp;
4047c478bd9Sstevel@tonic-gate 	ASSERT(ptsp->pt_nullmsg == NULL);
4057c478bd9Sstevel@tonic-gate 	ptsp->pt_nullmsg = mp;
4067c478bd9Sstevel@tonic-gate 	PT_EXIT_WRITE(ptsp);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue
4107c478bd9Sstevel@tonic-gate 	 * and add controlling tty if not set
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
4147c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
4157c478bd9Sstevel@tonic-gate 	sop = (struct stroptions *)mop->b_rptr;
4167c478bd9Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
4172463e920SRichard Lowe 	sop->so_hiwat = _TTY_BUFSIZ;
4187c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
4197c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	return (0);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate /*
425*1fa2a664SJoshua M. Clulow  * Find the address to private data identifying the subsidiary's write queue.
426*1fa2a664SJoshua M. Clulow  * Send a 0-length msg up the subsidiary's read queue to designate the manager
427*1fa2a664SJoshua M. Clulow  * is closing.  Uattach the manager from the subsidiary by nulling out
428*1fa2a664SJoshua M. Clulow  * manager's write queue field in private data.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate static int
ptsclose(queue_t * rqp,int flag,cred_t * credp)4317c478bd9Sstevel@tonic-gate ptsclose(queue_t *rqp, int flag, cred_t *credp)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptsp;
4347c478bd9Sstevel@tonic-gate 	queue_t *wqp;
4357c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
4367c478bd9Sstevel@tonic-gate 	mblk_t	*bp;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * q_ptr should never be NULL in the close routine and it is checked in
4407c478bd9Sstevel@tonic-gate 	 * DEBUG kernel by ASSERT. For non-DEBUG kernel the attempt is made to
4417c478bd9Sstevel@tonic-gate 	 * behave gracefully.
4427c478bd9Sstevel@tonic-gate 	 */
4437c478bd9Sstevel@tonic-gate 	ASSERT(rqp->q_ptr != NULL);
4447c478bd9Sstevel@tonic-gate 	if (rqp->q_ptr == NULL) {
4457c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
4467c478bd9Sstevel@tonic-gate 		return (0);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)rqp->q_ptr;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/*
452*1fa2a664SJoshua M. Clulow 	 * Subsidiary is going to close and doesn't want any new messages
453*1fa2a664SJoshua M. Clulow 	 * coming from the manager side, so set pts_rdq to NULL.  This should
454*1fa2a664SJoshua M. Clulow 	 * be done before call to qprocsoff() since subsidiary can't process
455*1fa2a664SJoshua M. Clulow 	 * additional messages from the manager after qprocsoff is called.
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	PT_ENTER_WRITE(ptsp);
4587c478bd9Sstevel@tonic-gate 	mp = ptsp->pt_nullmsg;
4597c478bd9Sstevel@tonic-gate 	ptsp->pt_nullmsg = NULL;
4607c478bd9Sstevel@tonic-gate 	ptsp->pts_rdq = NULL;
4617c478bd9Sstevel@tonic-gate 	PT_EXIT_WRITE(ptsp);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * Drain the ouput
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	wqp = WR(rqp);
4677c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
4687c478bd9Sstevel@tonic-gate 	while ((bp = getq(wqp)) != NULL) {
4697c478bd9Sstevel@tonic-gate 		if (ptsp->ptm_rdq) {
4707c478bd9Sstevel@tonic-gate 			putnext(ptsp->ptm_rdq, bp);
4717c478bd9Sstevel@tonic-gate 		} else if (bp->b_datap->db_type == M_IOCTL) {
4727c478bd9Sstevel@tonic-gate 			bp->b_datap->db_type = M_IOCNAK;
4737c478bd9Sstevel@tonic-gate 			freemsg(bp->b_cont);
4747c478bd9Sstevel@tonic-gate 			bp->b_cont = NULL;
4757c478bd9Sstevel@tonic-gate 			qreply(wqp, bp);
4767c478bd9Sstevel@tonic-gate 		} else {
4777c478bd9Sstevel@tonic-gate 			freemsg(bp);
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 	/*
481*1fa2a664SJoshua M. Clulow 	 * qenable manager side write queue so that it can flush its messages
482*1fa2a664SJoshua M. Clulow 	 * as subsidiarys's read queue is going away:
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	if (ptsp->ptm_rdq) {
4857c478bd9Sstevel@tonic-gate 		if (mp)
4867c478bd9Sstevel@tonic-gate 			putnext(ptsp->ptm_rdq, mp);
4877c478bd9Sstevel@tonic-gate 		else
4887c478bd9Sstevel@tonic-gate 			qenable(WR(ptsp->ptm_rdq));
4897c478bd9Sstevel@tonic-gate 	} else
4907c478bd9Sstevel@tonic-gate 		freemsg(mp);
4917c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	qprocsoff(rqp);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	rqp->q_ptr = NULL;
4967c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = NULL;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	ptms_close(ptsp, PTSOPEN | PTSTTY);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	return (0);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*
505*1fa2a664SJoshua M. Clulow  * The wput procedure will only handle flush messages.  All other messages are
506*1fa2a664SJoshua M. Clulow  * queued and the write side service procedure sends them off to the manager
507*1fa2a664SJoshua M. Clulow  * side.
5087c478bd9Sstevel@tonic-gate  */
5092c26b085SToomas Soome static int
ptswput(queue_t * qp,mblk_t * mp)5107c478bd9Sstevel@tonic-gate ptswput(queue_t *qp, mblk_t *mp)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	struct pt_ttys *ptsp;
5137c478bd9Sstevel@tonic-gate 	struct iocblk  *iocp;
5147c478bd9Sstevel@tonic-gate 	unsigned char type = mp->b_datap->db_type;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	DBG(("entering ptswput\n"));
5177c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)qp->q_ptr;
5207c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
5217c478bd9Sstevel@tonic-gate 	if (ptsp->ptm_rdq == NULL) {
522*1fa2a664SJoshua M. Clulow 		DBG(("in write put proc but no manager\n"));
5237c478bd9Sstevel@tonic-gate 		/*
524*1fa2a664SJoshua M. Clulow 		 * NAK ioctl as subsidiary side read queue is gone.
5257c478bd9Sstevel@tonic-gate 		 * Or else free the message.
5267c478bd9Sstevel@tonic-gate 		 */
5277c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
5287c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCNAK;
5297c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
5307c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
5317c478bd9Sstevel@tonic-gate 			qreply(qp, mp);
5327c478bd9Sstevel@tonic-gate 		} else
5337c478bd9Sstevel@tonic-gate 			freemsg(mp);
5347c478bd9Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
5352c26b085SToomas Soome 		return (0);
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (type >= QPCTL) {
53919397407SSherry Moore 		switch (type) {
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		/*
542*1fa2a664SJoshua M. Clulow 		 * if write queue request, flush subsidiary's write
5437c478bd9Sstevel@tonic-gate 		 * queue and send FLUSHR to ptm. If read queue
5447c478bd9Sstevel@tonic-gate 		 * request, send FLUSHR to ptm.
5457c478bd9Sstevel@tonic-gate 		 */
54619397407SSherry Moore 		case M_FLUSH:
5477c478bd9Sstevel@tonic-gate 		DBG(("pts got flush request\n"));
5487c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 			DBG(("got FLUSHW, flush pts write Q\n"));
5517c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHBAND)
5527c478bd9Sstevel@tonic-gate 				/*
5537c478bd9Sstevel@tonic-gate 				 * if it is a FLUSHBAND, do flushband.
5547c478bd9Sstevel@tonic-gate 				 */
5557c478bd9Sstevel@tonic-gate 				flushband(qp, *(mp->b_rptr + 1), FLUSHDATA);
5567c478bd9Sstevel@tonic-gate 			else
5577c478bd9Sstevel@tonic-gate 				flushq(qp, FLUSHDATA);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
5607c478bd9Sstevel@tonic-gate 			if ((*mp->b_rptr & FLUSHR) == 0) {
5617c478bd9Sstevel@tonic-gate 				/*
5627c478bd9Sstevel@tonic-gate 				 * FLUSHW only. Change to FLUSHR and putnext
5637c478bd9Sstevel@tonic-gate 				 * to ptm, then we are done.
5647c478bd9Sstevel@tonic-gate 				 */
5657c478bd9Sstevel@tonic-gate 				*mp->b_rptr |= FLUSHR;
5667c478bd9Sstevel@tonic-gate 				if (ptsp->ptm_rdq)
5677c478bd9Sstevel@tonic-gate 					putnext(ptsp->ptm_rdq, mp);
5687c478bd9Sstevel@tonic-gate 				break;
5697c478bd9Sstevel@tonic-gate 			} else {
5707c478bd9Sstevel@tonic-gate 				mblk_t *nmp;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 				/* It is a FLUSHRW. Duplicate the mblk */
5737c478bd9Sstevel@tonic-gate 				nmp = copyb(mp);
5747c478bd9Sstevel@tonic-gate 				if (nmp) {
5757c478bd9Sstevel@tonic-gate 					/*
5767c478bd9Sstevel@tonic-gate 					 * Change FLUSHW to FLUSHR before
5777c478bd9Sstevel@tonic-gate 					 * putnext to ptm.
5787c478bd9Sstevel@tonic-gate 					 */
5797c478bd9Sstevel@tonic-gate 					DBG(("putnext nmp(FLUSHR) to ptm\n"));
5807c478bd9Sstevel@tonic-gate 					*nmp->b_rptr |= FLUSHR;
5817c478bd9Sstevel@tonic-gate 					if (ptsp->ptm_rdq)
5827c478bd9Sstevel@tonic-gate 						putnext(ptsp->ptm_rdq, nmp);
5837c478bd9Sstevel@tonic-gate 				}
5847c478bd9Sstevel@tonic-gate 			}
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 		/*
587*1fa2a664SJoshua M. Clulow 		 * Since the packet module will toss any M_FLUSHES sent to the
588*1fa2a664SJoshua M. Clulow 		 * manager's stream head read queue, we simply turn it around
589*1fa2a664SJoshua M. Clulow 		 * here.
5907c478bd9Sstevel@tonic-gate 		 */
5917c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
5927c478bd9Sstevel@tonic-gate 			ASSERT(RD(qp)->q_first == NULL);
5937c478bd9Sstevel@tonic-gate 			DBG(("qreply(qp) turning FLUSHR around\n"));
5947c478bd9Sstevel@tonic-gate 			qreply(qp, mp);
5957c478bd9Sstevel@tonic-gate 		} else {
5967c478bd9Sstevel@tonic-gate 			freemsg(mp);
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 		break;
5997c478bd9Sstevel@tonic-gate 
60019397407SSherry Moore 		case M_READ:
601*1fa2a664SJoshua M. Clulow 		/* Caused by ldterm - can not pass to manager */
6027c478bd9Sstevel@tonic-gate 		freemsg(mp);
6037c478bd9Sstevel@tonic-gate 		break;
6047c478bd9Sstevel@tonic-gate 
60519397407SSherry Moore 		default:
6067c478bd9Sstevel@tonic-gate 		if (ptsp->ptm_rdq)
6077c478bd9Sstevel@tonic-gate 			putnext(ptsp->ptm_rdq, mp);
6087c478bd9Sstevel@tonic-gate 		break;
60919397407SSherry Moore 		}
61019397407SSherry Moore 		PT_EXIT_READ(ptsp);
6112c26b085SToomas Soome 		return (0);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	switch (type) {
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	case M_IOCTL:
6177c478bd9Sstevel@tonic-gate 		/*
6187c478bd9Sstevel@tonic-gate 		 * For case PTSSTTY set the flag PTSTTY and ACK
6197c478bd9Sstevel@tonic-gate 		 * the ioctl so that the user program can push
6207c478bd9Sstevel@tonic-gate 		 * the associated modules to get tty semantics.
6217c478bd9Sstevel@tonic-gate 		 * See bugid 4025044
6227c478bd9Sstevel@tonic-gate 		 */
6237c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
6247c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
6257c478bd9Sstevel@tonic-gate 		default:
6267c478bd9Sstevel@tonic-gate 			break;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		case PTSSTTY:
6297c478bd9Sstevel@tonic-gate 			if (ptsp->pt_state & PTSTTY) {
6307c478bd9Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCNAK;
6317c478bd9Sstevel@tonic-gate 				iocp->ioc_error = EEXIST;
6327c478bd9Sstevel@tonic-gate 			} else {
6337c478bd9Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
6347c478bd9Sstevel@tonic-gate 				mutex_enter(&ptsp->pt_lock);
6357c478bd9Sstevel@tonic-gate 				ptsp->pt_state |= PTSTTY;
6367c478bd9Sstevel@tonic-gate 				mutex_exit(&ptsp->pt_lock);
6377c478bd9Sstevel@tonic-gate 				iocp->ioc_error = 0;
6387c478bd9Sstevel@tonic-gate 			}
6397c478bd9Sstevel@tonic-gate 			iocp->ioc_count = 0;
6407c478bd9Sstevel@tonic-gate 			qreply(qp, mp);
6417c478bd9Sstevel@tonic-gate 			PT_EXIT_READ(ptsp);
6422c26b085SToomas Soome 			return (0);
6437c478bd9Sstevel@tonic-gate 		}
6442c26b085SToomas Soome 		/* FALLTHROUGH */
6457c478bd9Sstevel@tonic-gate 	default:
6467c478bd9Sstevel@tonic-gate 		/*
647*1fa2a664SJoshua M. Clulow 		 * send other messages to the manager
6487c478bd9Sstevel@tonic-gate 		 */
649*1fa2a664SJoshua M. Clulow 		DBG(("put msg on subsidiary's write queue\n"));
6507c478bd9Sstevel@tonic-gate 		(void) putq(qp, mp);
6517c478bd9Sstevel@tonic-gate 		break;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
6557c478bd9Sstevel@tonic-gate 	DBG(("return from ptswput()\n"));
6562c26b085SToomas Soome 	return (0);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*
661*1fa2a664SJoshua M. Clulow  * Enable the write side of the manager.  This triggers the manager to send any
662*1fa2a664SJoshua M. Clulow  * messages queued on its write side to the read side of this subsidiary.
6637c478bd9Sstevel@tonic-gate  */
6642c26b085SToomas Soome static int
ptsrsrv(queue_t * qp)6657c478bd9Sstevel@tonic-gate ptsrsrv(queue_t *qp)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	struct pt_ttys *ptsp;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	DBG(("entering ptsrsrv\n"));
6707c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)qp->q_ptr;
6737c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
6747c478bd9Sstevel@tonic-gate 	if (ptsp->ptm_rdq == NULL) {
675*1fa2a664SJoshua M. Clulow 		DBG(("in read srv proc but no manager\n"));
6767c478bd9Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
6772c26b085SToomas Soome 		return (0);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 	qenable(WR(ptsp->ptm_rdq));
6807c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
6817c478bd9Sstevel@tonic-gate 	DBG(("leaving ptsrsrv\n"));
6822c26b085SToomas Soome 	return (0);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate /*
686*1fa2a664SJoshua M. Clulow  * If there are messages on this queue that can be sent to manager, send them
687*1fa2a664SJoshua M. Clulow  * via putnext().  Otherwise, if queued messages cannot be sent, leave them on
688*1fa2a664SJoshua M. Clulow  * this queue.  If priority messages on this queue, send them to manager no
689*1fa2a664SJoshua M. Clulow  * matter what.
6907c478bd9Sstevel@tonic-gate  */
6912c26b085SToomas Soome static int
ptswsrv(queue_t * qp)6927c478bd9Sstevel@tonic-gate ptswsrv(queue_t *qp)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	struct pt_ttys *ptsp;
6957c478bd9Sstevel@tonic-gate 	queue_t *ptm_rdq;
6967c478bd9Sstevel@tonic-gate 	mblk_t *mp;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	DBG(("entering ptswsrv\n"));
6997c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	ptsp = (struct pt_ttys *)qp->q_ptr;
7027c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptsp);
7037c478bd9Sstevel@tonic-gate 	if (ptsp->ptm_rdq == NULL) {
704*1fa2a664SJoshua M. Clulow 		DBG(("in write srv proc but no manager\n"));
7057c478bd9Sstevel@tonic-gate 		/*
706*1fa2a664SJoshua M. Clulow 		 * Free messages on the write queue and send NAK for any
707*1fa2a664SJoshua M. Clulow 		 * M_IOCTL type messages to wakeup the user process waiting for
708*1fa2a664SJoshua M. Clulow 		 * ACK/NAK from the ioctl invocation
7097c478bd9Sstevel@tonic-gate 		 */
7107c478bd9Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
7117c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL) {
7127c478bd9Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCNAK;
7137c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
7147c478bd9Sstevel@tonic-gate 				mp->b_cont = NULL;
7157c478bd9Sstevel@tonic-gate 				qreply(qp, mp);
7167c478bd9Sstevel@tonic-gate 			} else
7177c478bd9Sstevel@tonic-gate 				freemsg(mp);
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 		PT_EXIT_READ(ptsp);
7202c26b085SToomas Soome 		return (0);
7217c478bd9Sstevel@tonic-gate 	} else {
7227c478bd9Sstevel@tonic-gate 		ptm_rdq = ptsp->ptm_rdq;
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	/*
726*1fa2a664SJoshua M. Clulow 	 * While there are messages on this write queue...
7277c478bd9Sstevel@tonic-gate 	 */
7287c478bd9Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
7297c478bd9Sstevel@tonic-gate 		/*
730*1fa2a664SJoshua M. Clulow 		 * If this is not a control message and we cannot put messages
731*1fa2a664SJoshua M. Clulow 		 * on the manager's read queue, put it back on this queue.
7327c478bd9Sstevel@tonic-gate 		 */
7337c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type <= QPCTL &&
7347c478bd9Sstevel@tonic-gate 		    !bcanputnext(ptm_rdq, mp->b_band)) {
7357c478bd9Sstevel@tonic-gate 			DBG(("put msg. back on Q\n"));
7367c478bd9Sstevel@tonic-gate 			(void) putbq(qp, mp);
7377c478bd9Sstevel@tonic-gate 			break;
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 		/*
740*1fa2a664SJoshua M. Clulow 		 * Otherwise, send the message up manager's stream:
7417c478bd9Sstevel@tonic-gate 		 */
742*1fa2a664SJoshua M. Clulow 		DBG(("send message to manager\n"));
7437c478bd9Sstevel@tonic-gate 		putnext(ptm_rdq, mp);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 	DBG(("leaving ptswsrv\n"));
7467c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptsp);
7472c26b085SToomas Soome 	return (0);
7487c478bd9Sstevel@tonic-gate }
749