xref: /titanic_44/usr/src/uts/common/io/ptm.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
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 /*
22facf4a8dSllai1  * Copyright 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Pseudo Terminal Master Driver.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * The pseudo-tty subsystem simulates a terminal connection, where the master
357c478bd9Sstevel@tonic-gate  * side represents the terminal and the slave represents the user process's
367c478bd9Sstevel@tonic-gate  * special device end point. The master device is set up as a cloned device
377c478bd9Sstevel@tonic-gate  * where its major device number is the major for the clone device and its minor
387c478bd9Sstevel@tonic-gate  * device number is the major for the ptm driver. There are no nodes in the file
397c478bd9Sstevel@tonic-gate  * system for master devices. The master pseudo driver is opened using the
407c478bd9Sstevel@tonic-gate  * open(2) system call with /dev/ptmx as the device parameter.  The clone open
417c478bd9Sstevel@tonic-gate  * finds the next available minor device for the ptm major device.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * A master device is available only if it and its corresponding slave device
447c478bd9Sstevel@tonic-gate  * are not already open. When the master device is opened, the corresponding
457c478bd9Sstevel@tonic-gate  * slave device is automatically locked out. Only one open is allowed on a
467c478bd9Sstevel@tonic-gate  * master device.  Multiple opens are allowed on the slave device.  After both
477c478bd9Sstevel@tonic-gate  * the master and slave have been opened, the user has two file descriptors
487c478bd9Sstevel@tonic-gate  * which are the end points of a full duplex connection composed of two streams
497c478bd9Sstevel@tonic-gate  * which are automatically connected at the master and slave drivers. The user
507c478bd9Sstevel@tonic-gate  * may then push modules onto either side of the stream pair.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * The master and slave drivers pass all messages to their adjacent queues.
537c478bd9Sstevel@tonic-gate  * Only the M_FLUSH needs some processing.  Because the read queue of one side
547c478bd9Sstevel@tonic-gate  * is connected to the write queue of the other, the FLUSHR flag is changed to
557c478bd9Sstevel@tonic-gate  * the FLUSHW flag and vice versa. When the master device is closed an M_HANGUP
567c478bd9Sstevel@tonic-gate  * message is sent to the slave device which will render the device
577c478bd9Sstevel@tonic-gate  * unusable. The process on the slave side gets the EIO when attempting to write
587c478bd9Sstevel@tonic-gate  * on that stream but it will be able to read any data remaining on the stream
597c478bd9Sstevel@tonic-gate  * head read queue.  When all the data has been read, read() returns 0
607c478bd9Sstevel@tonic-gate  * indicating that the stream can no longer be used.  On the last close of the
617c478bd9Sstevel@tonic-gate  * slave device, a 0-length message is sent to the master device. When the
627c478bd9Sstevel@tonic-gate  * application on the master side issues a read() or getmsg() and 0 is returned,
637c478bd9Sstevel@tonic-gate  * the user of the master device decides whether to issue a close() that
647c478bd9Sstevel@tonic-gate  * dismantles the pseudo-terminal subsystem. If the master device is not closed,
657c478bd9Sstevel@tonic-gate  * the pseudo-tty subsystem will be available to another user to open the slave
667c478bd9Sstevel@tonic-gate  * device.
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * If O_NONBLOCK or O_NDELAY is set, read on the master side returns -1 with
697c478bd9Sstevel@tonic-gate  * errno set to EAGAIN if no data is available, and write returns -1 with errno
707c478bd9Sstevel@tonic-gate  * set to EAGAIN if there is internal flow control.
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  * IOCTLS:
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *  ISPTM: determines whether the file descriptor is that of an open master
757c478bd9Sstevel@tonic-gate  *	   device. Return code of zero indicates that the file descriptor
767c478bd9Sstevel@tonic-gate  *	   represents master device.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *  UNLKPT: unlocks the master and slave devices.  It returns 0 on success. On
797c478bd9Sstevel@tonic-gate  *	    failure, the errno is set to EINVAL indicating that the master
807c478bd9Sstevel@tonic-gate  *	    device is not open.
817c478bd9Sstevel@tonic-gate  *
82facf4a8dSllai1  *  ZONEPT: sets the zone membership of the associated pts device.
83facf4a8dSllai1  *
84facf4a8dSllai1  *  GRPPT:  sets the group owner of the associated pts device.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * Synchronization:
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  *   All global data synchronization between ptm/pts is done via global
897c478bd9Sstevel@tonic-gate  *   ptms_lock mutex which is initialized at system boot time from
907c478bd9Sstevel@tonic-gate  *   ptms_initspace (called from space.c).
917c478bd9Sstevel@tonic-gate  *
927c478bd9Sstevel@tonic-gate  *   Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
937c478bd9Sstevel@tonic-gate  *   pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  *   PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
967c478bd9Sstevel@tonic-gate  *   which allow reader locks to be reacquired by the same thread (usual
977c478bd9Sstevel@tonic-gate  *   reader/writer locks can't be used for that purpose since it is illegal for
987c478bd9Sstevel@tonic-gate  *   a thread to acquire a lock it already holds, even as a reader). The sole
997c478bd9Sstevel@tonic-gate  *   purpose of these macros is to guarantee that the peer queue will not
1007c478bd9Sstevel@tonic-gate  *   disappear (due to closing peer) while it is used. It is safe to use
1017c478bd9Sstevel@tonic-gate  *   PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
1027c478bd9Sstevel@tonic-gate  *   they are not real locks but reference counts).
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  *   PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave
1057c478bd9Sstevel@tonic-gate  *   open/close paths to modify ptm_rdq and pts_rdq fields. These fields should
1067c478bd9Sstevel@tonic-gate  *   be set to appropriate queues *after* qprocson() is called during open (to
1077c478bd9Sstevel@tonic-gate  *   prevent peer from accessing the queue with incomplete plumbing) and set to
1087c478bd9Sstevel@tonic-gate  *   NULL before qprocsoff() is called during close.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  *   The pt_nullmsg field is only used in open/close routines and it is also
1117c478bd9Sstevel@tonic-gate  *   protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
1127c478bd9Sstevel@tonic-gate  *   holds.
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * Lock Ordering:
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  *   If both ptms_lock and per-pty lock should be held, ptms_lock should always
1177c478bd9Sstevel@tonic-gate  *   be entered first, followed by per-pty lock.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * See ptms.h, pts.c and ptms_conf.c for more information.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #include <sys/types.h>
1237c478bd9Sstevel@tonic-gate #include <sys/param.h>
1247c478bd9Sstevel@tonic-gate #include <sys/file.h>
1257c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1267c478bd9Sstevel@tonic-gate #include <sys/stream.h>
1277c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
1287c478bd9Sstevel@tonic-gate #include <sys/proc.h>
1297c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1307c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1317c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1327c478bd9Sstevel@tonic-gate #include <sys/ptms.h>
1337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1347c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
1357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1367c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1377c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1387c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1407c478bd9Sstevel@tonic-gate #include <sys/zone.h>
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #ifdef DEBUG
1437c478bd9Sstevel@tonic-gate int ptm_debug = 0;
1447c478bd9Sstevel@tonic-gate #define	DBG(a)	 if (ptm_debug) cmn_err(CE_NOTE, a)
1457c478bd9Sstevel@tonic-gate #else
1467c478bd9Sstevel@tonic-gate #define	DBG(a)
1477c478bd9Sstevel@tonic-gate #endif
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static int ptmopen(queue_t *, dev_t *, int, int, cred_t *);
1507c478bd9Sstevel@tonic-gate static int ptmclose(queue_t *, int, cred_t *);
1517c478bd9Sstevel@tonic-gate static void ptmwput(queue_t *, mblk_t *);
1527c478bd9Sstevel@tonic-gate static void ptmrsrv(queue_t *);
1537c478bd9Sstevel@tonic-gate static void ptmwsrv(queue_t *);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Master Stream Pseudo Terminal Module: stream data structure definitions
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static struct module_info ptm_info = {
1607c478bd9Sstevel@tonic-gate 	0xdead,
1617c478bd9Sstevel@tonic-gate 	"ptm",
1627c478bd9Sstevel@tonic-gate 	0,
1637c478bd9Sstevel@tonic-gate 	512,
1647c478bd9Sstevel@tonic-gate 	512,
1657c478bd9Sstevel@tonic-gate 	128
1667c478bd9Sstevel@tonic-gate };
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static struct qinit ptmrint = {
1697c478bd9Sstevel@tonic-gate 	NULL,
1707c478bd9Sstevel@tonic-gate 	(int (*)()) ptmrsrv,
1717c478bd9Sstevel@tonic-gate 	ptmopen,
1727c478bd9Sstevel@tonic-gate 	ptmclose,
1737c478bd9Sstevel@tonic-gate 	NULL,
1747c478bd9Sstevel@tonic-gate 	&ptm_info,
1757c478bd9Sstevel@tonic-gate 	NULL
1767c478bd9Sstevel@tonic-gate };
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static struct qinit ptmwint = {
1797c478bd9Sstevel@tonic-gate 	(int (*)()) ptmwput,
1807c478bd9Sstevel@tonic-gate 	(int (*)()) ptmwsrv,
1817c478bd9Sstevel@tonic-gate 	NULL,
1827c478bd9Sstevel@tonic-gate 	NULL,
1837c478bd9Sstevel@tonic-gate 	NULL,
1847c478bd9Sstevel@tonic-gate 	&ptm_info,
1857c478bd9Sstevel@tonic-gate 	NULL
1867c478bd9Sstevel@tonic-gate };
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static struct streamtab ptminfo = {
1897c478bd9Sstevel@tonic-gate 	&ptmrint,
1907c478bd9Sstevel@tonic-gate 	&ptmwint,
1917c478bd9Sstevel@tonic-gate 	NULL,
1927c478bd9Sstevel@tonic-gate 	NULL
1937c478bd9Sstevel@tonic-gate };
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static int ptm_attach(dev_info_t *, ddi_attach_cmd_t);
1967c478bd9Sstevel@tonic-gate static int ptm_detach(dev_info_t *, ddi_detach_cmd_t);
1977c478bd9Sstevel@tonic-gate static int ptm_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static dev_info_t	*ptm_dip;		/* private devinfo pointer */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * this will define (struct cb_ops cb_ptm_ops) and (struct dev_ops ptm_ops)
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(ptm_ops, nulldev, nulldev, ptm_attach, ptm_detach,
2057c478bd9Sstevel@tonic-gate     nodev, ptm_devinfo, D_MP, &ptminfo);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2127c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
2137c478bd9Sstevel@tonic-gate 	"Master streams driver 'ptm' %I%",
2147c478bd9Sstevel@tonic-gate 	&ptm_ops,	/* driver ops */
2157c478bd9Sstevel@tonic-gate };
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2187c478bd9Sstevel@tonic-gate 	MODREV_1,
2197c478bd9Sstevel@tonic-gate 	&modldrv,
2207c478bd9Sstevel@tonic-gate 	NULL
2217c478bd9Sstevel@tonic-gate };
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate int
2247c478bd9Sstevel@tonic-gate _init(void)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	int rc;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if ((rc = mod_install(&modlinkage)) == 0)
2297c478bd9Sstevel@tonic-gate 		ptms_init();
2307c478bd9Sstevel@tonic-gate 	return (rc);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate int
2347c478bd9Sstevel@tonic-gate _fini(void)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate int
2407c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate static int
2467c478bd9Sstevel@tonic-gate ptm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "ptmajor", S_IFCHR,
2527c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2537c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
2547c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "ptmx", S_IFCHR,
2577c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, CLONE_DEV) == DDI_FAILURE) {
2587c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
2597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	ptm_dip = devi;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static int
2677c478bd9Sstevel@tonic-gate ptm_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2707c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
2737c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2777c478bd9Sstevel@tonic-gate static int
2787c478bd9Sstevel@tonic-gate ptm_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2797c478bd9Sstevel@tonic-gate     void **result)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	int error;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2847c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2857c478bd9Sstevel@tonic-gate 		if (ptm_dip == NULL) {
2867c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
2877c478bd9Sstevel@tonic-gate 		} else {
2887c478bd9Sstevel@tonic-gate 			*result = (void *)ptm_dip;
2897c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 		break;
2927c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2937c478bd9Sstevel@tonic-gate 		*result = (void *)0;
2947c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
2957c478bd9Sstevel@tonic-gate 		break;
2967c478bd9Sstevel@tonic-gate 	default:
2977c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	return (error);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /* ARGSUSED */
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate  * Open a minor of the master device. Store the write queue pointer and set the
3067c478bd9Sstevel@tonic-gate  * pt_state field to (PTMOPEN | PTLOCK).
3077c478bd9Sstevel@tonic-gate  * This code will work properly with both clone opens and direct opens of the
3087c478bd9Sstevel@tonic-gate  * master device.
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate static int
3117c478bd9Sstevel@tonic-gate ptmopen(
3127c478bd9Sstevel@tonic-gate 	queue_t *rqp,		/* pointer to the read side queue */
3137c478bd9Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
3147c478bd9Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
3157c478bd9Sstevel@tonic-gate 	int	sflag,		/* open state flag */
3167c478bd9Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
3197c478bd9Sstevel@tonic-gate 	mblk_t		*mop;		/* ptr to a setopts message block */
3207c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
3217c478bd9Sstevel@tonic-gate 	minor_t		dminor = getminor(*devp);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/* Allow reopen */
3247c478bd9Sstevel@tonic-gate 	if (rqp->q_ptr != NULL)
3257c478bd9Sstevel@tonic-gate 		return (0);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (sflag & MODOPEN)
3287c478bd9Sstevel@tonic-gate 		return (ENXIO);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (!(sflag & CLONEOPEN) && dminor != 0) {
3317c478bd9Sstevel@tonic-gate 		/*
3327c478bd9Sstevel@tonic-gate 		 * This is a direct open to specific master device through an
3337c478bd9Sstevel@tonic-gate 		 * artificially created entry with specific minor in
3347c478bd9Sstevel@tonic-gate 		 * /dev/directory. Such behavior is not supported.
3357c478bd9Sstevel@tonic-gate 		 */
3367c478bd9Sstevel@tonic-gate 		return (ENXIO);
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/*
340facf4a8dSllai1 	 * The master open requires that the slave be attached
341facf4a8dSllai1 	 * before it returns so that attempts to open the slave will
342facf4a8dSllai1 	 * succeeed
3437c478bd9Sstevel@tonic-gate 	 */
344facf4a8dSllai1 	if (ptms_attach_slave() != 0) {
345facf4a8dSllai1 		return (ENXIO);
346facf4a8dSllai1 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	mop = allocb(sizeof (struct stroptions), BPRI_MED);
3497c478bd9Sstevel@tonic-gate 	if (mop == NULL) {
3507c478bd9Sstevel@tonic-gate 		DDBG("ptmopen(): mop allocation failed\n", 0);
3517c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if ((ptmp = pt_ttys_alloc()) == NULL) {
3557c478bd9Sstevel@tonic-gate 		DDBG("ptmopen(): pty allocation failed\n", 0);
3567c478bd9Sstevel@tonic-gate 		freemsg(mop);
3577c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	dminor = ptmp->pt_minor;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	DDBGP("ptmopen(): allocated ptmp %p\n", (uintptr_t)ptmp);
3637c478bd9Sstevel@tonic-gate 	DDBG("ptmopen(): allocated minor %d\n", dminor);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = ptmp;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	qprocson(rqp);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/* Allow slave to send messages to master */
3707c478bd9Sstevel@tonic-gate 	PT_ENTER_WRITE(ptmp);
3717c478bd9Sstevel@tonic-gate 	ptmp->ptm_rdq = rqp;
3727c478bd9Sstevel@tonic-gate 	PT_EXIT_WRITE(ptmp);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue
3767c478bd9Sstevel@tonic-gate 	 * and add controlling tty if not set
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
3797c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
3807c478bd9Sstevel@tonic-gate 	sop = (struct stroptions *)mop->b_rptr;
3817c478bd9Sstevel@tonic-gate 	if (oflag & FNOCTTY)
3827c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT;
3837c478bd9Sstevel@tonic-gate 	else
3847c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
3857c478bd9Sstevel@tonic-gate 	sop->so_hiwat = 512;
3867c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
3877c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	/*
3907c478bd9Sstevel@tonic-gate 	 * The input, devp, is a major device number, the output is put
3917c478bd9Sstevel@tonic-gate 	 * into the same parm as a major,minor pair.
3927c478bd9Sstevel@tonic-gate 	 */
3937c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), dminor);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	return (0);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  * Find the address to private data identifying the slave's write queue.
4017c478bd9Sstevel@tonic-gate  * Send a hang-up message up the slave's read queue to designate the
4027c478bd9Sstevel@tonic-gate  * master/slave pair is tearing down. Uattach the master and slave by
4037c478bd9Sstevel@tonic-gate  * nulling out the write queue fields in the private data structure.
4047c478bd9Sstevel@tonic-gate  * Finally, unlock the master/slave pair and mark the master as closed.
4057c478bd9Sstevel@tonic-gate  */
4067c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
4077c478bd9Sstevel@tonic-gate static int
4087c478bd9Sstevel@tonic-gate ptmclose(queue_t *rqp, int flag, cred_t *credp)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
4117c478bd9Sstevel@tonic-gate 	queue_t *pts_rdq;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	ASSERT(rqp->q_ptr);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)rqp->q_ptr;
4167c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
4177c478bd9Sstevel@tonic-gate 	if (ptmp->pts_rdq) {
4187c478bd9Sstevel@tonic-gate 		pts_rdq = ptmp->pts_rdq;
4197c478bd9Sstevel@tonic-gate 		if (pts_rdq->q_next) {
4207c478bd9Sstevel@tonic-gate 			DBG(("send hangup message to slave\n"));
4217c478bd9Sstevel@tonic-gate 			(void) putnextctl(pts_rdq, M_HANGUP);
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * ptm_rdq should be cleared before call to qprocsoff() to prevent pts
4277c478bd9Sstevel@tonic-gate 	 * write procedure to attempt using ptm_rdq after qprocsoff.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	PT_ENTER_WRITE(ptmp);
4307c478bd9Sstevel@tonic-gate 	ptmp->ptm_rdq = NULL;
4317c478bd9Sstevel@tonic-gate 	freemsg(ptmp->pt_nullmsg);
4327c478bd9Sstevel@tonic-gate 	ptmp->pt_nullmsg = NULL;
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * qenable slave side write queue so that it can flush
4357c478bd9Sstevel@tonic-gate 	 * its messages as master's read queue is going away
4367c478bd9Sstevel@tonic-gate 	 */
4377c478bd9Sstevel@tonic-gate 	if (ptmp->pts_rdq)
4387c478bd9Sstevel@tonic-gate 		qenable(WR(ptmp->pts_rdq));
4397c478bd9Sstevel@tonic-gate 	PT_EXIT_WRITE(ptmp);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	qprocsoff(rqp);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	/* Finish the close */
4447c478bd9Sstevel@tonic-gate 	rqp->q_ptr = NULL;
4457c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = NULL;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	ptms_close(ptmp, PTMOPEN | PTLOCK);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	return (0);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
452*9acbbeafSnn35248 static boolean_t
453*9acbbeafSnn35248 ptmptsopencb(ptmptsopencb_arg_t arg)
454*9acbbeafSnn35248 {
455*9acbbeafSnn35248 	struct pt_ttys	*ptmp = (struct pt_ttys *)arg;
456*9acbbeafSnn35248 	boolean_t rval;
457*9acbbeafSnn35248 
458*9acbbeafSnn35248 	PT_ENTER_READ(ptmp);
459*9acbbeafSnn35248 	rval = (ptmp->pt_nullmsg != NULL);
460*9acbbeafSnn35248 	PT_EXIT_READ(ptmp);
461*9acbbeafSnn35248 	return (rval);
462*9acbbeafSnn35248 }
463*9acbbeafSnn35248 
4647c478bd9Sstevel@tonic-gate /*
4657c478bd9Sstevel@tonic-gate  * The wput procedure will only handle ioctl and flush messages.
4667c478bd9Sstevel@tonic-gate  */
4677c478bd9Sstevel@tonic-gate static void
4687c478bd9Sstevel@tonic-gate ptmwput(queue_t *qp, mblk_t *mp)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
4717c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	DBG(("entering ptmwput\n"));
4747c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)qp->q_ptr;
4777c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * if write queue request, flush master's write
4827c478bd9Sstevel@tonic-gate 	 * queue and send FLUSHR up slave side. If read
4837c478bd9Sstevel@tonic-gate 	 * queue request, convert to FLUSHW and putnext().
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 	case M_FLUSH:
4867c478bd9Sstevel@tonic-gate 		{
4877c478bd9Sstevel@tonic-gate 			unsigned char flush_flg = 0;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 			DBG(("ptm got flush request\n"));
4907c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
4917c478bd9Sstevel@tonic-gate 				DBG(("got FLUSHW, flush ptm write Q\n"));
4927c478bd9Sstevel@tonic-gate 				if (*mp->b_rptr & FLUSHBAND)
4937c478bd9Sstevel@tonic-gate 					/*
4947c478bd9Sstevel@tonic-gate 					 * if it is a FLUSHBAND, do flushband.
4957c478bd9Sstevel@tonic-gate 					 */
4967c478bd9Sstevel@tonic-gate 					flushband(qp, *(mp->b_rptr + 1),
4977c478bd9Sstevel@tonic-gate 					    FLUSHDATA);
4987c478bd9Sstevel@tonic-gate 				else
4997c478bd9Sstevel@tonic-gate 					flushq(qp, FLUSHDATA);
5007c478bd9Sstevel@tonic-gate 				flush_flg = (*mp->b_rptr & ~FLUSHW) | FLUSHR;
5017c478bd9Sstevel@tonic-gate 			}
5027c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHR) {
5037c478bd9Sstevel@tonic-gate 				DBG(("got FLUSHR, set FLUSHW\n"));
5047c478bd9Sstevel@tonic-gate 				flush_flg |= (*mp->b_rptr & ~FLUSHR) | FLUSHW;
5057c478bd9Sstevel@tonic-gate 			}
5067c478bd9Sstevel@tonic-gate 			if (flush_flg != 0 && ptmp->pts_rdq &&
5077c478bd9Sstevel@tonic-gate 			    !(ptmp->pt_state & PTLOCK)) {
5087c478bd9Sstevel@tonic-gate 				DBG(("putnext to pts\n"));
5097c478bd9Sstevel@tonic-gate 				*mp->b_rptr = flush_flg;
5107c478bd9Sstevel@tonic-gate 				putnext(ptmp->pts_rdq, mp);
5117c478bd9Sstevel@tonic-gate 			} else
5127c478bd9Sstevel@tonic-gate 				freemsg(mp);
5137c478bd9Sstevel@tonic-gate 			break;
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	case M_IOCTL:
5177c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
5187c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
5197c478bd9Sstevel@tonic-gate 		default:
5207c478bd9Sstevel@tonic-gate 			if ((ptmp->pt_state & PTLOCK) ||
5217c478bd9Sstevel@tonic-gate 			    (ptmp->pts_rdq == NULL)) {
5227c478bd9Sstevel@tonic-gate 				DBG(("got M_IOCTL but no slave\n"));
5237c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, EINVAL);
5247c478bd9Sstevel@tonic-gate 				PT_EXIT_READ(ptmp);
5257c478bd9Sstevel@tonic-gate 				return;
5267c478bd9Sstevel@tonic-gate 			}
5277c478bd9Sstevel@tonic-gate 			(void) putq(qp, mp);
5287c478bd9Sstevel@tonic-gate 			break;
5297c478bd9Sstevel@tonic-gate 		case UNLKPT:
5307c478bd9Sstevel@tonic-gate 			mutex_enter(&ptmp->pt_lock);
5317c478bd9Sstevel@tonic-gate 			ptmp->pt_state &= ~PTLOCK;
5327c478bd9Sstevel@tonic-gate 			mutex_exit(&ptmp->pt_lock);
5337c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
5347c478bd9Sstevel@tonic-gate 		case ISPTM:
5357c478bd9Sstevel@tonic-gate 			DBG(("ack the UNLKPT/ISPTM\n"));
5367c478bd9Sstevel@tonic-gate 			miocack(qp, mp, 0, 0);
5377c478bd9Sstevel@tonic-gate 			break;
5387c478bd9Sstevel@tonic-gate 		case ZONEPT:
5397c478bd9Sstevel@tonic-gate 		{
5407c478bd9Sstevel@tonic-gate 			zoneid_t z;
5417c478bd9Sstevel@tonic-gate 			int error;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 			if ((error = drv_priv(iocp->ioc_cr)) != 0) {
5447c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, error);
5457c478bd9Sstevel@tonic-gate 				break;
5467c478bd9Sstevel@tonic-gate 			}
5477c478bd9Sstevel@tonic-gate 			if ((error = miocpullup(mp, sizeof (zoneid_t))) != 0) {
5487c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, error);
5497c478bd9Sstevel@tonic-gate 				break;
5507c478bd9Sstevel@tonic-gate 			}
5517c478bd9Sstevel@tonic-gate 			z = *((zoneid_t *)mp->b_cont->b_rptr);
5527c478bd9Sstevel@tonic-gate 			if (z < MIN_ZONEID || z > MAX_ZONEID) {
5537c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, EINVAL);
5547c478bd9Sstevel@tonic-gate 				break;
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 			mutex_enter(&ptmp->pt_lock);
5587c478bd9Sstevel@tonic-gate 			ptmp->pt_zoneid = z;
5597c478bd9Sstevel@tonic-gate 			mutex_exit(&ptmp->pt_lock);
5607c478bd9Sstevel@tonic-gate 			miocack(qp, mp, 0, 0);
5617c478bd9Sstevel@tonic-gate 			break;
5627c478bd9Sstevel@tonic-gate 		}
563facf4a8dSllai1 		case PT_OWNER:
564facf4a8dSllai1 		{
565facf4a8dSllai1 			pt_own_t *ptop;
566facf4a8dSllai1 			int error;
567facf4a8dSllai1 
568facf4a8dSllai1 			if ((error = miocpullup(mp, sizeof (pt_own_t))) != 0) {
569facf4a8dSllai1 				miocnak(qp, mp, 0, error);
570facf4a8dSllai1 				break;
571facf4a8dSllai1 			}
572facf4a8dSllai1 
573facf4a8dSllai1 			ptop = (pt_own_t *)mp->b_cont->b_rptr;
574facf4a8dSllai1 
575facf4a8dSllai1 			if (ptop->pto_ruid < 0 || ptop->pto_rgid < 0) {
576facf4a8dSllai1 				miocnak(qp, mp, 0, EINVAL);
577facf4a8dSllai1 				break;
578facf4a8dSllai1 			}
579facf4a8dSllai1 
580facf4a8dSllai1 			mutex_enter(&ptmp->pt_lock);
581facf4a8dSllai1 			ptmp->pt_ruid = ptop->pto_ruid;
582facf4a8dSllai1 			ptmp->pt_rgid = ptop->pto_rgid;
583facf4a8dSllai1 			mutex_exit(&ptmp->pt_lock);
584facf4a8dSllai1 			miocack(qp, mp, 0, 0);
585facf4a8dSllai1 			break;
586facf4a8dSllai1 		}
587*9acbbeafSnn35248 		case PTMPTSOPENCB:
588*9acbbeafSnn35248 		{
589*9acbbeafSnn35248 			mblk_t		*dp;	/* ioctl reply data */
590*9acbbeafSnn35248 			ptmptsopencb_t	*ppocb;
591*9acbbeafSnn35248 
592*9acbbeafSnn35248 			/* only allow the kernel to invoke this ioctl */
593*9acbbeafSnn35248 			if (iocp->ioc_cr != kcred) {
594*9acbbeafSnn35248 				miocnak(qp, mp, 0, EINVAL);
595*9acbbeafSnn35248 				break;
596*9acbbeafSnn35248 			}
597*9acbbeafSnn35248 
598*9acbbeafSnn35248 			/* we don't support transparent ioctls */
599*9acbbeafSnn35248 			ASSERT(iocp->ioc_count != TRANSPARENT);
600*9acbbeafSnn35248 			if (iocp->ioc_count == TRANSPARENT) {
601*9acbbeafSnn35248 				miocnak(qp, mp, 0, EINVAL);
602*9acbbeafSnn35248 				break;
603*9acbbeafSnn35248 			}
604*9acbbeafSnn35248 
605*9acbbeafSnn35248 			/* allocate a response message */
606*9acbbeafSnn35248 			dp = allocb(sizeof (ptmptsopencb_t), BPRI_MED);
607*9acbbeafSnn35248 			if (dp == NULL) {
608*9acbbeafSnn35248 				miocnak(qp, mp, 0, EAGAIN);
609*9acbbeafSnn35248 				break;
610*9acbbeafSnn35248 			}
611*9acbbeafSnn35248 
612*9acbbeafSnn35248 			/* initialize the ioctl results */
613*9acbbeafSnn35248 			ppocb = (ptmptsopencb_t *)dp->b_rptr;
614*9acbbeafSnn35248 			ppocb->ppocb_func = ptmptsopencb;
615*9acbbeafSnn35248 			ppocb->ppocb_arg = (ptmptsopencb_arg_t)ptmp;
616*9acbbeafSnn35248 
617*9acbbeafSnn35248 			/* send the reply data */
618*9acbbeafSnn35248 			mioc2ack(mp, dp, sizeof (ptmptsopencb_t), 0);
619*9acbbeafSnn35248 			qreply(qp, mp);
620*9acbbeafSnn35248 			break;
621*9acbbeafSnn35248 		}
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 		break;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	case M_READ:
6267c478bd9Sstevel@tonic-gate 		/* Caused by ldterm - can not pass to slave */
6277c478bd9Sstevel@tonic-gate 		freemsg(mp);
6287c478bd9Sstevel@tonic-gate 		break;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/*
6317c478bd9Sstevel@tonic-gate 	 * send other messages to slave
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	default:
6347c478bd9Sstevel@tonic-gate 		if ((ptmp->pt_state  & PTLOCK) || (ptmp->pts_rdq == NULL)) {
6357c478bd9Sstevel@tonic-gate 			DBG(("got msg. but no slave\n"));
6367c478bd9Sstevel@tonic-gate 			mp = mexchange(NULL, mp, 2, M_ERROR, -1);
6377c478bd9Sstevel@tonic-gate 			if (mp != NULL) {
6387c478bd9Sstevel@tonic-gate 				mp->b_rptr[0] = NOERROR;
6397c478bd9Sstevel@tonic-gate 				mp->b_rptr[1] = EINVAL;
6407c478bd9Sstevel@tonic-gate 				qreply(qp, mp);
6417c478bd9Sstevel@tonic-gate 			}
6427c478bd9Sstevel@tonic-gate 			PT_EXIT_READ(ptmp);
6437c478bd9Sstevel@tonic-gate 			return;
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 		DBG(("put msg on master's write queue\n"));
6467c478bd9Sstevel@tonic-gate 		(void) putq(qp, mp);
6477c478bd9Sstevel@tonic-gate 		break;
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	DBG(("return from ptmwput()\n"));
6507c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate  * enable the write side of the slave. This triggers the
6567c478bd9Sstevel@tonic-gate  * slave to send any messages queued on its write side to
6577c478bd9Sstevel@tonic-gate  * the read side of this master.
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate static void
6607c478bd9Sstevel@tonic-gate ptmrsrv(queue_t *qp)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	DBG(("entering ptmrsrv\n"));
6657c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)qp->q_ptr;
6687c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
6697c478bd9Sstevel@tonic-gate 	if (ptmp->pts_rdq) {
6707c478bd9Sstevel@tonic-gate 		qenable(WR(ptmp->pts_rdq));
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
6737c478bd9Sstevel@tonic-gate 	DBG(("leaving ptmrsrv\n"));
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate /*
6787c478bd9Sstevel@tonic-gate  * If there are messages on this queue that can be sent to
6797c478bd9Sstevel@tonic-gate  * slave, send them via putnext(). Else, if queued messages
6807c478bd9Sstevel@tonic-gate  * cannot be sent, leave them on this queue. If priority
6817c478bd9Sstevel@tonic-gate  * messages on this queue, send them to slave no matter what.
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate static void
6847c478bd9Sstevel@tonic-gate ptmwsrv(queue_t *qp)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
6877c478bd9Sstevel@tonic-gate 	mblk_t 		*mp;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	DBG(("entering ptmwsrv\n"));
6907c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)qp->q_ptr;
693*9acbbeafSnn35248 
694*9acbbeafSnn35248 	if ((mp = getq(qp)) == NULL) {
695*9acbbeafSnn35248 		/* If there are no messages there's nothing to do. */
696*9acbbeafSnn35248 		DBG(("leaving ptmwsrv (no messages)\n"));
697*9acbbeafSnn35248 		return;
698*9acbbeafSnn35248 	}
699*9acbbeafSnn35248 
7007c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
7017c478bd9Sstevel@tonic-gate 	if ((ptmp->pt_state  & PTLOCK) || (ptmp->pts_rdq == NULL)) {
7027c478bd9Sstevel@tonic-gate 		DBG(("in master write srv proc but no slave\n"));
7037c478bd9Sstevel@tonic-gate 		/*
7047c478bd9Sstevel@tonic-gate 		 * Free messages on the write queue and send
7057c478bd9Sstevel@tonic-gate 		 * NAK for any M_IOCTL type messages to wakeup
7067c478bd9Sstevel@tonic-gate 		 * the user process waiting for ACK/NAK from
7077c478bd9Sstevel@tonic-gate 		 * the ioctl invocation
7087c478bd9Sstevel@tonic-gate 		 */
709*9acbbeafSnn35248 		do {
7107c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL)
7117c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, EINVAL);
7127c478bd9Sstevel@tonic-gate 			else
7137c478bd9Sstevel@tonic-gate 				freemsg(mp);
714*9acbbeafSnn35248 		} while ((mp = getq(qp)) != NULL);
7157c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHALL);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		mp = mexchange(NULL, NULL, 2, M_ERROR, -1);
7187c478bd9Sstevel@tonic-gate 		if (mp != NULL) {
7197c478bd9Sstevel@tonic-gate 			mp->b_rptr[0] = NOERROR;
7207c478bd9Sstevel@tonic-gate 			mp->b_rptr[1] = EINVAL;
7217c478bd9Sstevel@tonic-gate 			qreply(qp, mp);
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 		PT_EXIT_READ(ptmp);
7247c478bd9Sstevel@tonic-gate 		return;
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 	/*
7277c478bd9Sstevel@tonic-gate 	 * while there are messages on this write queue...
7287c478bd9Sstevel@tonic-gate 	 */
729*9acbbeafSnn35248 	do {
7307c478bd9Sstevel@tonic-gate 		/*
7317c478bd9Sstevel@tonic-gate 		 * if don't have control message and cannot put
7327c478bd9Sstevel@tonic-gate 		 * msg. on slave's read queue, put it back on
7337c478bd9Sstevel@tonic-gate 		 * this queue.
7347c478bd9Sstevel@tonic-gate 		 */
7357c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type <= QPCTL &&
7367c478bd9Sstevel@tonic-gate 		    !bcanputnext(ptmp->pts_rdq, mp->b_band)) {
7377c478bd9Sstevel@tonic-gate 			DBG(("put msg. back on queue\n"));
7387c478bd9Sstevel@tonic-gate 			(void) putbq(qp, mp);
7397c478bd9Sstevel@tonic-gate 			break;
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 		/*
7427c478bd9Sstevel@tonic-gate 		 * else send the message up slave's stream
7437c478bd9Sstevel@tonic-gate 		 */
7447c478bd9Sstevel@tonic-gate 		DBG(("send message to slave\n"));
7457c478bd9Sstevel@tonic-gate 		putnext(ptmp->pts_rdq, mp);
746*9acbbeafSnn35248 	} while ((mp = getq(qp)) != NULL);
7477c478bd9Sstevel@tonic-gate 	DBG(("leaving ptmwsrv\n"));
7487c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
7497c478bd9Sstevel@tonic-gate }
750