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