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