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