17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5facf4a8dSllai * Common Development and Distribution License (the "License").
6facf4a8dSllai * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22eb5a5c78SSurya Prakki * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
25e2b86177SToomas Soome /* All Rights Reserved */
267c478bd9Sstevel@tonic-gate
277d8deab2SAndy Fiddaman /*
287d8deab2SAndy Fiddaman * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
29*1fa2a664SJoshua M. Clulow * Copyright 2021 Oxide Computer Company
307d8deab2SAndy Fiddaman */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
33*1fa2a664SJoshua M. Clulow * PSEUDO-TERMINAL MANAGER DRIVER (PTM)
347c478bd9Sstevel@tonic-gate *
35*1fa2a664SJoshua M. Clulow * The pseudo-terminal subsystem simulates a terminal connection, where the
36*1fa2a664SJoshua M. Clulow * manager side represents the terminal and the subsidiary represents the user
37*1fa2a664SJoshua M. Clulow * process's special device end point. The manager device is set up as a
38*1fa2a664SJoshua M. Clulow * cloned device where its major device number is the major for the clone
39*1fa2a664SJoshua M. Clulow * device and its minor device number is the major for the ptm driver. There
40*1fa2a664SJoshua M. Clulow * are no nodes in the file system for manager devices. The manager pseudo
41*1fa2a664SJoshua M. Clulow * driver is opened using the open(2) system call with /dev/ptmx as the device
42*1fa2a664SJoshua M. Clulow * parameter. The clone open finds the next available minor device for the ptm
43*1fa2a664SJoshua M. Clulow * major device.
447c478bd9Sstevel@tonic-gate *
45*1fa2a664SJoshua M. Clulow * A manager device is available only if it and its corresponding subsidiary
46*1fa2a664SJoshua M. Clulow * device are not already open. When the manager device is opened, the
47*1fa2a664SJoshua M. Clulow * corresponding subsidiary device is automatically locked out. Only one open
48*1fa2a664SJoshua M. Clulow * is allowed on a manager device. Multiple opens are allowed on the
49*1fa2a664SJoshua M. Clulow * subsidiary device. After both the manager and subsidiary have been opened,
50*1fa2a664SJoshua M. Clulow * the user has two file descriptors which are the end points of a full duplex
51*1fa2a664SJoshua M. Clulow * connection composed of two streams which are automatically connected at the
52*1fa2a664SJoshua M. Clulow * manager and subsidiary drivers. The user may then push modules onto either
53*1fa2a664SJoshua M. Clulow * side of the stream pair.
547c478bd9Sstevel@tonic-gate *
55*1fa2a664SJoshua M. Clulow * The manager and subsidiary drivers pass all messages to their adjacent
56*1fa2a664SJoshua M. Clulow * queues. Only the M_FLUSH needs some processing. Because the read queue of
57*1fa2a664SJoshua M. Clulow * one side is connected to the write queue of the other, the FLUSHR flag is
58*1fa2a664SJoshua M. Clulow * changed to the FLUSHW flag and vice versa. When the manager device is
59*1fa2a664SJoshua M. Clulow * closed an M_HANGUP message is sent to the subsidiary device which will
60*1fa2a664SJoshua M. Clulow * render the device unusable. The process on the subsidiary side gets an EIO
61*1fa2a664SJoshua M. Clulow * error when attempting to write on that stream but it will be able to read
62*1fa2a664SJoshua M. Clulow * any data remaining on the stream head read queue. When all the data has
63*1fa2a664SJoshua M. Clulow * been read, read() returns 0 indicating that the stream can no longer be
64*1fa2a664SJoshua M. Clulow * used. On the last close of the subsidiary device, a 0-length message is
65*1fa2a664SJoshua M. Clulow * sent to the manager device. When the application on the manager side issues
66*1fa2a664SJoshua M. Clulow * a read() or getmsg() and 0 is returned, the user of the manager device
67*1fa2a664SJoshua M. Clulow * decides whether to issue a close() that dismantles the pseudo-terminal
68*1fa2a664SJoshua M. Clulow * subsystem. If the manager device is not closed, the pseudo-terminal
69*1fa2a664SJoshua M. Clulow * subsystem will be available to another user to open the subsidiary device.
707c478bd9Sstevel@tonic-gate *
71*1fa2a664SJoshua M. Clulow * If O_NONBLOCK or O_NDELAY is set, read on the manager side returns -1 with
727c478bd9Sstevel@tonic-gate * errno set to EAGAIN if no data is available, and write returns -1 with errno
737c478bd9Sstevel@tonic-gate * set to EAGAIN if there is internal flow control.
747c478bd9Sstevel@tonic-gate *
757c478bd9Sstevel@tonic-gate *
76*1fa2a664SJoshua M. Clulow * IOCTLS
777c478bd9Sstevel@tonic-gate *
78*1fa2a664SJoshua M. Clulow * ISPTM
79*1fa2a664SJoshua M. Clulow * Determines whether the file descriptor is that of an open
80*1fa2a664SJoshua M. Clulow * manager device. Return code of zero indicates that the file
81*1fa2a664SJoshua M. Clulow * descriptor represents a manager device.
827c478bd9Sstevel@tonic-gate *
83*1fa2a664SJoshua M. Clulow * UNLKPT
84*1fa2a664SJoshua M. Clulow * Unlocks the manager and subsidiary devices. It returns 0 on
85*1fa2a664SJoshua M. Clulow * success. On failure, the errno is set to EINVAL indicating that
86*1fa2a664SJoshua M. Clulow * the manager device is not open.
87facf4a8dSllai *
88*1fa2a664SJoshua M. Clulow * ZONEPT
89*1fa2a664SJoshua M. Clulow * Sets the zone membership of the associated subsidiary device.
907c478bd9Sstevel@tonic-gate *
91*1fa2a664SJoshua M. Clulow * GRPPT
92*1fa2a664SJoshua M. Clulow * Sets the group owner of the associated subsidiary device.
937c478bd9Sstevel@tonic-gate *
947c478bd9Sstevel@tonic-gate *
95*1fa2a664SJoshua M. Clulow * SYNCHRONIZATION
967c478bd9Sstevel@tonic-gate *
97*1fa2a664SJoshua M. Clulow * All global data synchronization between ptm/pts is done via global ptms_lock
98*1fa2a664SJoshua M. Clulow * mutex which is initialized at system boot time from ptms_initspace (called
99*1fa2a664SJoshua M. Clulow * from space.c).
1007c478bd9Sstevel@tonic-gate *
101*1fa2a664SJoshua M. Clulow * Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
102*1fa2a664SJoshua M. Clulow * pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
1037c478bd9Sstevel@tonic-gate *
104*1fa2a664SJoshua M. Clulow * PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
105*1fa2a664SJoshua M. Clulow * which allow reader locks to be reacquired by the same thread (usual
106*1fa2a664SJoshua M. Clulow * reader/writer locks can't be used for that purpose since it is illegal for a
107*1fa2a664SJoshua M. Clulow * thread to acquire a lock it already holds, even as a reader). The sole
108*1fa2a664SJoshua M. Clulow * purpose of these macros is to guarantee that the peer queue will not
109*1fa2a664SJoshua M. Clulow * disappear (due to closing peer) while it is used. It is safe to use
110*1fa2a664SJoshua M. Clulow * PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
111*1fa2a664SJoshua M. Clulow * they are not real locks but reference counts).
1127c478bd9Sstevel@tonic-gate *
113*1fa2a664SJoshua M. Clulow * PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in manager/subsidiary
114*1fa2a664SJoshua M. Clulow * open/close paths to modify ptm_rdq and pts_rdq fields. These fields should
115*1fa2a664SJoshua M. Clulow * be set to appropriate queues *after* qprocson() is called during open (to
116*1fa2a664SJoshua M. Clulow * prevent peer from accessing the queue with incomplete plumbing) and set to
117*1fa2a664SJoshua M. Clulow * NULL before qprocsoff() is called during close.
1187c478bd9Sstevel@tonic-gate *
119*1fa2a664SJoshua M. Clulow * The pt_nullmsg field is only used in open/close routines and it is also
120*1fa2a664SJoshua M. Clulow * protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
121*1fa2a664SJoshua M. Clulow * holds.
1227c478bd9Sstevel@tonic-gate *
123*1fa2a664SJoshua M. Clulow *
124*1fa2a664SJoshua M. Clulow * LOCK ORDERING
125*1fa2a664SJoshua M. Clulow *
126*1fa2a664SJoshua M. Clulow * If both ptms_lock and per-pty lock should be held, ptms_lock should always
127*1fa2a664SJoshua M. Clulow * be entered first, followed by per-pty lock.
128*1fa2a664SJoshua M. Clulow *
129*1fa2a664SJoshua M. Clulow * See ptms.h, pts.c, and ptms_conf.c for more information.
1307c478bd9Sstevel@tonic-gate */
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate #include <sys/types.h>
1337c478bd9Sstevel@tonic-gate #include <sys/param.h>
1347c478bd9Sstevel@tonic-gate #include <sys/file.h>
1357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1367c478bd9Sstevel@tonic-gate #include <sys/stream.h>
1377c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
1387c478bd9Sstevel@tonic-gate #include <sys/proc.h>
1397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1417c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1427c478bd9Sstevel@tonic-gate #include <sys/ptms.h>
1437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1447c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
1457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1467c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1477c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1487c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1497c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1507c478bd9Sstevel@tonic-gate #include <sys/zone.h>
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate #ifdef DEBUG
1537c478bd9Sstevel@tonic-gate int ptm_debug = 0;
1547c478bd9Sstevel@tonic-gate #define DBG(a) if (ptm_debug) cmn_err(CE_NOTE, a)
1557c478bd9Sstevel@tonic-gate #else
1567c478bd9Sstevel@tonic-gate #define DBG(a)
1577c478bd9Sstevel@tonic-gate #endif
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate static int ptmopen(queue_t *, dev_t *, int, int, cred_t *);
1607c478bd9Sstevel@tonic-gate static int ptmclose(queue_t *, int, cred_t *);
161e2b86177SToomas Soome static int ptmwput(queue_t *, mblk_t *);
162e2b86177SToomas Soome static int ptmrsrv(queue_t *);
163e2b86177SToomas Soome static int ptmwsrv(queue_t *);
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate static struct module_info ptm_info = {
1667c478bd9Sstevel@tonic-gate 0xdead,
1677c478bd9Sstevel@tonic-gate "ptm",
1687c478bd9Sstevel@tonic-gate 0,
1697c478bd9Sstevel@tonic-gate 512,
1707c478bd9Sstevel@tonic-gate 512,
1717c478bd9Sstevel@tonic-gate 128
1727c478bd9Sstevel@tonic-gate };
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate static struct qinit ptmrint = {
1757c478bd9Sstevel@tonic-gate NULL,
176e2b86177SToomas Soome ptmrsrv,
1777c478bd9Sstevel@tonic-gate ptmopen,
1787c478bd9Sstevel@tonic-gate ptmclose,
1797c478bd9Sstevel@tonic-gate NULL,
1807c478bd9Sstevel@tonic-gate &ptm_info,
1817c478bd9Sstevel@tonic-gate NULL
1827c478bd9Sstevel@tonic-gate };
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate static struct qinit ptmwint = {
185e2b86177SToomas Soome ptmwput,
186e2b86177SToomas Soome ptmwsrv,
1877c478bd9Sstevel@tonic-gate NULL,
1887c478bd9Sstevel@tonic-gate NULL,
1897c478bd9Sstevel@tonic-gate NULL,
1907c478bd9Sstevel@tonic-gate &ptm_info,
1917c478bd9Sstevel@tonic-gate NULL
1927c478bd9Sstevel@tonic-gate };
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate static struct streamtab ptminfo = {
1957c478bd9Sstevel@tonic-gate &ptmrint,
1967c478bd9Sstevel@tonic-gate &ptmwint,
1977c478bd9Sstevel@tonic-gate NULL,
1987c478bd9Sstevel@tonic-gate NULL
1997c478bd9Sstevel@tonic-gate };
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate static int ptm_attach(dev_info_t *, ddi_attach_cmd_t);
2027c478bd9Sstevel@tonic-gate static int ptm_detach(dev_info_t *, ddi_detach_cmd_t);
2037c478bd9Sstevel@tonic-gate static int ptm_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate static dev_info_t *ptm_dip; /* private devinfo pointer */
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate * this will define (struct cb_ops cb_ptm_ops) and (struct dev_ops ptm_ops)
2097c478bd9Sstevel@tonic-gate */
2107c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(ptm_ops, nulldev, nulldev, ptm_attach, ptm_detach,
21119397407SSherry Moore nodev, ptm_devinfo, D_MP, &ptminfo, ddi_quiesce_not_supported);
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate * Module linkage information for the kernel.
2157c478bd9Sstevel@tonic-gate */
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
218*1fa2a664SJoshua M. Clulow &mod_driverops,
219*1fa2a664SJoshua M. Clulow "Pseudo-Terminal Manager Driver",
220*1fa2a664SJoshua M. Clulow &ptm_ops,
2217c478bd9Sstevel@tonic-gate };
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2247c478bd9Sstevel@tonic-gate MODREV_1,
2257c478bd9Sstevel@tonic-gate &modldrv,
2267c478bd9Sstevel@tonic-gate NULL
2277c478bd9Sstevel@tonic-gate };
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate int
_init(void)2307c478bd9Sstevel@tonic-gate _init(void)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate int rc;
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate if ((rc = mod_install(&modlinkage)) == 0)
2357c478bd9Sstevel@tonic-gate ptms_init();
2367c478bd9Sstevel@tonic-gate return (rc);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate int
_fini(void)2407c478bd9Sstevel@tonic-gate _fini(void)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage));
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2467c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate static int
ptm_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2527c478bd9Sstevel@tonic-gate ptm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH)
2557c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "ptmajor", S_IFCHR,
258414e964cSToomas Soome 0, DDI_PSEUDO, 0) == DDI_FAILURE) {
2597c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2607c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "ptmx", S_IFCHR,
2637c478bd9Sstevel@tonic-gate 0, DDI_PSEUDO, CLONE_DEV) == DDI_FAILURE) {
2647c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2657c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate ptm_dip = devi;
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate static int
ptm_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2737c478bd9Sstevel@tonic-gate ptm_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH)
2767c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2797c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate static int
ptm_devinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2837c478bd9Sstevel@tonic-gate ptm_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2847c478bd9Sstevel@tonic-gate void **result)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate int error;
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate switch (infocmd) {
2897c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
2907c478bd9Sstevel@tonic-gate if (ptm_dip == NULL) {
2917c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
2927c478bd9Sstevel@tonic-gate } else {
2937c478bd9Sstevel@tonic-gate *result = (void *)ptm_dip;
2947c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate break;
2977c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
2987c478bd9Sstevel@tonic-gate *result = (void *)0;
2997c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
3007c478bd9Sstevel@tonic-gate break;
3017c478bd9Sstevel@tonic-gate default:
3027c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate return (error);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate /*
309*1fa2a664SJoshua M. Clulow * Open a minor of the manager device. Store the write queue pointer and set
310*1fa2a664SJoshua M. Clulow * the pt_state field to (PTMOPEN | PTLOCK).
3117c478bd9Sstevel@tonic-gate * This code will work properly with both clone opens and direct opens of the
312*1fa2a664SJoshua M. Clulow * manager device.
3137c478bd9Sstevel@tonic-gate */
3147c478bd9Sstevel@tonic-gate static int
ptmopen(queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)3157c478bd9Sstevel@tonic-gate ptmopen(
3167c478bd9Sstevel@tonic-gate queue_t *rqp, /* pointer to the read side queue */
3177c478bd9Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */
3187c478bd9Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */
3197c478bd9Sstevel@tonic-gate int sflag, /* open state flag */
3207c478bd9Sstevel@tonic-gate cred_t *credp) /* credentials */
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate struct pt_ttys *ptmp;
3237c478bd9Sstevel@tonic-gate mblk_t *mop; /* ptr to a setopts message block */
3247c478bd9Sstevel@tonic-gate struct stroptions *sop;
3257c478bd9Sstevel@tonic-gate minor_t dminor = getminor(*devp);
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate /* Allow reopen */
3287c478bd9Sstevel@tonic-gate if (rqp->q_ptr != NULL)
3297c478bd9Sstevel@tonic-gate return (0);
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate if (sflag & MODOPEN)
3327c478bd9Sstevel@tonic-gate return (ENXIO);
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate if (!(sflag & CLONEOPEN) && dminor != 0) {
3357c478bd9Sstevel@tonic-gate /*
336*1fa2a664SJoshua M. Clulow * This is a direct open to specific manager device through an
3377c478bd9Sstevel@tonic-gate * artificially created entry with specific minor in
338*1fa2a664SJoshua M. Clulow * /dev/directory. Such behavior is not supported.
3397c478bd9Sstevel@tonic-gate */
3407c478bd9Sstevel@tonic-gate return (ENXIO);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate /*
344*1fa2a664SJoshua M. Clulow * The manager open requires that the subsidiary be attached before it
345*1fa2a664SJoshua M. Clulow * returns so that attempts to open the subsidiary will succeeed
3467c478bd9Sstevel@tonic-gate */
347*1fa2a664SJoshua M. Clulow if (ptms_attach_subsidiary() != 0) {
348facf4a8dSllai return (ENXIO);
349facf4a8dSllai }
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate mop = allocb(sizeof (struct stroptions), BPRI_MED);
3527c478bd9Sstevel@tonic-gate if (mop == NULL) {
3537c478bd9Sstevel@tonic-gate DDBG("ptmopen(): mop allocation failed\n", 0);
3547c478bd9Sstevel@tonic-gate return (ENOMEM);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate if ((ptmp = pt_ttys_alloc()) == NULL) {
3587c478bd9Sstevel@tonic-gate DDBG("ptmopen(): pty allocation failed\n", 0);
3597c478bd9Sstevel@tonic-gate freemsg(mop);
3607c478bd9Sstevel@tonic-gate return (ENOMEM);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate dminor = ptmp->pt_minor;
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate DDBGP("ptmopen(): allocated ptmp %p\n", (uintptr_t)ptmp);
3667c478bd9Sstevel@tonic-gate DDBG("ptmopen(): allocated minor %d\n", dminor);
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = ptmp;
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate qprocson(rqp);
3717c478bd9Sstevel@tonic-gate
372*1fa2a664SJoshua M. Clulow /* Allow subsidiary to send messages to manager */
3737c478bd9Sstevel@tonic-gate PT_ENTER_WRITE(ptmp);
3747c478bd9Sstevel@tonic-gate ptmp->ptm_rdq = rqp;
3757c478bd9Sstevel@tonic-gate PT_EXIT_WRITE(ptmp);
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate * set up hi/lo water marks on stream head read queue
3797c478bd9Sstevel@tonic-gate * and add controlling tty if not set
3807c478bd9Sstevel@tonic-gate */
3817c478bd9Sstevel@tonic-gate mop->b_datap->db_type = M_SETOPTS;
3827c478bd9Sstevel@tonic-gate mop->b_wptr += sizeof (struct stroptions);
3837c478bd9Sstevel@tonic-gate sop = (struct stroptions *)mop->b_rptr;
3847c478bd9Sstevel@tonic-gate if (oflag & FNOCTTY)
3857c478bd9Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT;
3867c478bd9Sstevel@tonic-gate else
3877c478bd9Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
3882463e920SRichard Lowe sop->so_hiwat = _TTY_BUFSIZ;
3897c478bd9Sstevel@tonic-gate sop->so_lowat = 256;
3907c478bd9Sstevel@tonic-gate putnext(rqp, mop);
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate * The input, devp, is a major device number, the output is put
3947c478bd9Sstevel@tonic-gate * into the same parm as a major,minor pair.
3957c478bd9Sstevel@tonic-gate */
3967c478bd9Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), dminor);
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate return (0);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate /*
403*1fa2a664SJoshua M. Clulow * Find the address to private data identifying the subsidiary's write queue.
404*1fa2a664SJoshua M. Clulow * Send a hang-up message up the subsidiary's read queue to designate the
405*1fa2a664SJoshua M. Clulow * manager/subsidiary pair is tearing down. Uattach the manager and subsidiary
406*1fa2a664SJoshua M. Clulow * by nulling out the write queue fields in the private data structure.
407*1fa2a664SJoshua M. Clulow * Finally, unlock the manager/subsidiary pair and mark the manager as closed.
4087c478bd9Sstevel@tonic-gate */
4097c478bd9Sstevel@tonic-gate static int
ptmclose(queue_t * rqp,int flag,cred_t * credp)4107c478bd9Sstevel@tonic-gate ptmclose(queue_t *rqp, int flag, cred_t *credp)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate struct pt_ttys *ptmp;
4137c478bd9Sstevel@tonic-gate queue_t *pts_rdq;
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate ASSERT(rqp->q_ptr);
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate ptmp = (struct pt_ttys *)rqp->q_ptr;
4187c478bd9Sstevel@tonic-gate PT_ENTER_READ(ptmp);
4197c478bd9Sstevel@tonic-gate if (ptmp->pts_rdq) {
4207c478bd9Sstevel@tonic-gate pts_rdq = ptmp->pts_rdq;
4217c478bd9Sstevel@tonic-gate if (pts_rdq->q_next) {
422*1fa2a664SJoshua M. Clulow DBG(("send hangup message to subsidiary\n"));
4237c478bd9Sstevel@tonic-gate (void) putnextctl(pts_rdq, M_HANGUP);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate PT_EXIT_READ(ptmp);
4277c478bd9Sstevel@tonic-gate /*
4287c478bd9Sstevel@tonic-gate * ptm_rdq should be cleared before call to qprocsoff() to prevent pts
4297c478bd9Sstevel@tonic-gate * write procedure to attempt using ptm_rdq after qprocsoff.
4307c478bd9Sstevel@tonic-gate */
4317c478bd9Sstevel@tonic-gate PT_ENTER_WRITE(ptmp);
4327c478bd9Sstevel@tonic-gate ptmp->ptm_rdq = NULL;
4337c478bd9Sstevel@tonic-gate freemsg(ptmp->pt_nullmsg);
4347c478bd9Sstevel@tonic-gate ptmp->pt_nullmsg = NULL;
4357c478bd9Sstevel@tonic-gate /*
436*1fa2a664SJoshua M. Clulow * qenable subsidiary side write queue so that it can flush
437*1fa2a664SJoshua M. Clulow * its messages as manager's read queue is going away
4387c478bd9Sstevel@tonic-gate */
4397c478bd9Sstevel@tonic-gate if (ptmp->pts_rdq)
4407c478bd9Sstevel@tonic-gate qenable(WR(ptmp->pts_rdq));
4417c478bd9Sstevel@tonic-gate PT_EXIT_WRITE(ptmp);
4427c478bd9Sstevel@tonic-gate
4437c478bd9Sstevel@tonic-gate qprocsoff(rqp);
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate /* Finish the close */
4467c478bd9Sstevel@tonic-gate rqp->q_ptr = NULL;
4477c478bd9Sstevel@tonic-gate WR(rqp)->q_ptr = NULL;
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate ptms_close(ptmp, PTMOPEN | PTLOCK);
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate return (0);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate * The wput procedure will only handle ioctl and flush messages.
4567c478bd9Sstevel@tonic-gate */
457e2b86177SToomas Soome static int
ptmwput(queue_t * qp,mblk_t * mp)4587c478bd9Sstevel@tonic-gate ptmwput(queue_t *qp, mblk_t *mp)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate struct pt_ttys *ptmp;
4617c478bd9Sstevel@tonic-gate struct iocblk *iocp;
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate DBG(("entering ptmwput\n"));
4647c478bd9Sstevel@tonic-gate ASSERT(qp->q_ptr);
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate ptmp = (struct pt_ttys *)qp->q_ptr;
4677c478bd9Sstevel@tonic-gate PT_ENTER_READ(ptmp);
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate switch (mp->b_datap->db_type) {
4707c478bd9Sstevel@tonic-gate /*
471*1fa2a664SJoshua M. Clulow * If this is a write queue request, flush manager's write queue and
472*1fa2a664SJoshua M. Clulow * send FLUSHR up subsidiary side. If it is a read queue request,
473*1fa2a664SJoshua M. Clulow * convert to FLUSHW and putnext().
4747c478bd9Sstevel@tonic-gate */
4757c478bd9Sstevel@tonic-gate case M_FLUSH:
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate unsigned char flush_flg = 0;
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate DBG(("ptm got flush request\n"));
4807c478bd9Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) {
4817c478bd9Sstevel@tonic-gate DBG(("got FLUSHW, flush ptm write Q\n"));
482*1fa2a664SJoshua M. Clulow if (*mp->b_rptr & FLUSHBAND) {
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate * if it is a FLUSHBAND, do flushband.
4857c478bd9Sstevel@tonic-gate */
4867c478bd9Sstevel@tonic-gate flushband(qp, *(mp->b_rptr + 1),
4877c478bd9Sstevel@tonic-gate FLUSHDATA);
488*1fa2a664SJoshua M. Clulow } else {
4897c478bd9Sstevel@tonic-gate flushq(qp, FLUSHDATA);
490*1fa2a664SJoshua M. Clulow }
4917c478bd9Sstevel@tonic-gate flush_flg = (*mp->b_rptr & ~FLUSHW) | FLUSHR;
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) {
4947c478bd9Sstevel@tonic-gate DBG(("got FLUSHR, set FLUSHW\n"));
4957c478bd9Sstevel@tonic-gate flush_flg |= (*mp->b_rptr & ~FLUSHR) | FLUSHW;
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate if (flush_flg != 0 && ptmp->pts_rdq &&
4987c478bd9Sstevel@tonic-gate !(ptmp->pt_state & PTLOCK)) {
4997c478bd9Sstevel@tonic-gate DBG(("putnext to pts\n"));
5007c478bd9Sstevel@tonic-gate *mp->b_rptr = flush_flg;
5017c478bd9Sstevel@tonic-gate putnext(ptmp->pts_rdq, mp);
502*1fa2a664SJoshua M. Clulow } else {
5037c478bd9Sstevel@tonic-gate freemsg(mp);
504*1fa2a664SJoshua M. Clulow }
5057c478bd9Sstevel@tonic-gate break;
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate case M_IOCTL:
5097c478bd9Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
5107c478bd9Sstevel@tonic-gate switch (iocp->ioc_cmd) {
5117c478bd9Sstevel@tonic-gate default:
5127c478bd9Sstevel@tonic-gate if ((ptmp->pt_state & PTLOCK) ||
5137c478bd9Sstevel@tonic-gate (ptmp->pts_rdq == NULL)) {
514*1fa2a664SJoshua M. Clulow DBG(("got M_IOCTL but no subsidiary\n"));
5157c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
5167c478bd9Sstevel@tonic-gate PT_EXIT_READ(ptmp);
517e2b86177SToomas Soome return (0);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate (void) putq(qp, mp);
5207c478bd9Sstevel@tonic-gate break;
5217c478bd9Sstevel@tonic-gate case UNLKPT:
5227c478bd9Sstevel@tonic-gate mutex_enter(&ptmp->pt_lock);
5237c478bd9Sstevel@tonic-gate ptmp->pt_state &= ~PTLOCK;
5247c478bd9Sstevel@tonic-gate mutex_exit(&ptmp->pt_lock);
5257c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
5267c478bd9Sstevel@tonic-gate case ISPTM:
5277c478bd9Sstevel@tonic-gate DBG(("ack the UNLKPT/ISPTM\n"));
5287c478bd9Sstevel@tonic-gate miocack(qp, mp, 0, 0);
5297c478bd9Sstevel@tonic-gate break;
5307d8deab2SAndy Fiddaman case PTSSTTY:
5317d8deab2SAndy Fiddaman mutex_enter(&ptmp->pt_lock);
5327d8deab2SAndy Fiddaman ptmp->pt_state |= PTSTTY;
5337d8deab2SAndy Fiddaman mutex_exit(&ptmp->pt_lock);
5347d8deab2SAndy Fiddaman DBG(("ack PTSSTTY\n"));
5357d8deab2SAndy Fiddaman miocack(qp, mp, 0, 0);
5367d8deab2SAndy Fiddaman break;
5377c478bd9Sstevel@tonic-gate case ZONEPT:
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate zoneid_t z;
5407c478bd9Sstevel@tonic-gate int error;
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate if ((error = drv_priv(iocp->ioc_cr)) != 0) {
5437c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, error);
5447c478bd9Sstevel@tonic-gate break;
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate if ((error = miocpullup(mp, sizeof (zoneid_t))) != 0) {
5477c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, error);
5487c478bd9Sstevel@tonic-gate break;
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate z = *((zoneid_t *)mp->b_cont->b_rptr);
5517c478bd9Sstevel@tonic-gate if (z < MIN_ZONEID || z > MAX_ZONEID) {
5527c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
5537c478bd9Sstevel@tonic-gate break;
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate mutex_enter(&ptmp->pt_lock);
5577c478bd9Sstevel@tonic-gate ptmp->pt_zoneid = z;
5587c478bd9Sstevel@tonic-gate mutex_exit(&ptmp->pt_lock);
5597c478bd9Sstevel@tonic-gate miocack(qp, mp, 0, 0);
5607c478bd9Sstevel@tonic-gate break;
5617c478bd9Sstevel@tonic-gate }
56249e92448Svikram case OWNERPT:
563facf4a8dSllai {
564facf4a8dSllai pt_own_t *ptop;
565facf4a8dSllai int error;
566bda89588Sjp zone_t *zone;
567facf4a8dSllai
568facf4a8dSllai if ((error = miocpullup(mp, sizeof (pt_own_t))) != 0) {
569facf4a8dSllai miocnak(qp, mp, 0, error);
570facf4a8dSllai break;
571facf4a8dSllai }
572facf4a8dSllai
573bda89588Sjp zone = zone_find_by_id(ptmp->pt_zoneid);
574facf4a8dSllai ptop = (pt_own_t *)mp->b_cont->b_rptr;
575facf4a8dSllai
576bda89588Sjp if (!VALID_UID(ptop->pto_ruid, zone) ||
577bda89588Sjp !VALID_GID(ptop->pto_rgid, zone)) {
578bda89588Sjp zone_rele(zone);
579facf4a8dSllai miocnak(qp, mp, 0, EINVAL);
580facf4a8dSllai break;
581facf4a8dSllai }
582bda89588Sjp zone_rele(zone);
583facf4a8dSllai mutex_enter(&ptmp->pt_lock);
584facf4a8dSllai ptmp->pt_ruid = ptop->pto_ruid;
585facf4a8dSllai ptmp->pt_rgid = ptop->pto_rgid;
586facf4a8dSllai mutex_exit(&ptmp->pt_lock);
587facf4a8dSllai miocack(qp, mp, 0, 0);
588facf4a8dSllai break;
589facf4a8dSllai }
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate break;
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate case M_READ:
594*1fa2a664SJoshua M. Clulow /* Caused by ldterm - can not pass to subsidiary */
5957c478bd9Sstevel@tonic-gate freemsg(mp);
5967c478bd9Sstevel@tonic-gate break;
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate /*
599*1fa2a664SJoshua M. Clulow * Send other messages to the subsidiary:
6007c478bd9Sstevel@tonic-gate */
6017c478bd9Sstevel@tonic-gate default:
602*1fa2a664SJoshua M. Clulow if ((ptmp->pt_state & PTLOCK) || (ptmp->pts_rdq == NULL)) {
603*1fa2a664SJoshua M. Clulow DBG(("got msg. but no subsidiary\n"));
6047c478bd9Sstevel@tonic-gate mp = mexchange(NULL, mp, 2, M_ERROR, -1);
6057c478bd9Sstevel@tonic-gate if (mp != NULL) {
6067c478bd9Sstevel@tonic-gate mp->b_rptr[0] = NOERROR;
6077c478bd9Sstevel@tonic-gate mp->b_rptr[1] = EINVAL;
6087c478bd9Sstevel@tonic-gate qreply(qp, mp);
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate PT_EXIT_READ(ptmp);
611e2b86177SToomas Soome return (0);
6127c478bd9Sstevel@tonic-gate }
613*1fa2a664SJoshua M. Clulow DBG(("put msg on manager's write queue\n"));
6147c478bd9Sstevel@tonic-gate (void) putq(qp, mp);
6157c478bd9Sstevel@tonic-gate break;
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate DBG(("return from ptmwput()\n"));
6187c478bd9Sstevel@tonic-gate PT_EXIT_READ(ptmp);
619e2b86177SToomas Soome return (0);
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate /*
624*1fa2a664SJoshua M. Clulow * Enable the write side of the subsidiary. This triggers the subsidiary to
625*1fa2a664SJoshua M. Clulow * send any messages queued on its write side to the read side of this manager.
6267c478bd9Sstevel@tonic-gate */
627e2b86177SToomas Soome static int
ptmrsrv(queue_t * qp)6287c478bd9Sstevel@tonic-gate ptmrsrv(queue_t *qp)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate struct pt_ttys *ptmp;
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate DBG(("entering ptmrsrv\n"));
6337c478bd9Sstevel@tonic-gate ASSERT(qp->q_ptr);
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate ptmp = (struct pt_ttys *)qp->q_ptr;
6367c478bd9Sstevel@tonic-gate PT_ENTER_READ(ptmp);
6377c478bd9Sstevel@tonic-gate if (ptmp->pts_rdq) {
6387c478bd9Sstevel@tonic-gate qenable(WR(ptmp->pts_rdq));
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate PT_EXIT_READ(ptmp);
6417c478bd9Sstevel@tonic-gate DBG(("leaving ptmrsrv\n"));
642e2b86177SToomas Soome return (0);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate /*
647*1fa2a664SJoshua M. Clulow * If there are messages on this queue that can be sent to subsidiary, send
648*1fa2a664SJoshua M. Clulow * them via putnext(). Otherwise, if queued messages cannot be sent, leave
649*1fa2a664SJoshua M. Clulow * them on this queue. If priority messages on this queue, send them to the
650*1fa2a664SJoshua M. Clulow * subsidiary no matter what.
6517c478bd9Sstevel@tonic-gate */
652e2b86177SToomas Soome static int
ptmwsrv(queue_t * qp)6537c478bd9Sstevel@tonic-gate ptmwsrv(queue_t *qp)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate struct pt_ttys *ptmp;
656e2b86177SToomas Soome mblk_t *mp;
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate DBG(("entering ptmwsrv\n"));
6597c478bd9Sstevel@tonic-gate ASSERT(qp->q_ptr);
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate ptmp = (struct pt_ttys *)qp->q_ptr;
6629acbbeafSnn
6639acbbeafSnn if ((mp = getq(qp)) == NULL) {
6649acbbeafSnn /* If there are no messages there's nothing to do. */
6659acbbeafSnn DBG(("leaving ptmwsrv (no messages)\n"));
666e2b86177SToomas Soome return (0);
6679acbbeafSnn }
6689acbbeafSnn
6697c478bd9Sstevel@tonic-gate PT_ENTER_READ(ptmp);
6707c478bd9Sstevel@tonic-gate if ((ptmp->pt_state & PTLOCK) || (ptmp->pts_rdq == NULL)) {
671*1fa2a664SJoshua M. Clulow DBG(("in manager write srv proc but no subsidiary\n"));
6727c478bd9Sstevel@tonic-gate /*
6737c478bd9Sstevel@tonic-gate * Free messages on the write queue and send
6747c478bd9Sstevel@tonic-gate * NAK for any M_IOCTL type messages to wakeup
6757c478bd9Sstevel@tonic-gate * the user process waiting for ACK/NAK from
6767c478bd9Sstevel@tonic-gate * the ioctl invocation
6777c478bd9Sstevel@tonic-gate */
6789acbbeafSnn do {
6797c478bd9Sstevel@tonic-gate if (mp->b_datap->db_type == M_IOCTL)
6807c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
6817c478bd9Sstevel@tonic-gate else
6827c478bd9Sstevel@tonic-gate freemsg(mp);
6839acbbeafSnn } while ((mp = getq(qp)) != NULL);
6847c478bd9Sstevel@tonic-gate flushq(qp, FLUSHALL);
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate mp = mexchange(NULL, NULL, 2, M_ERROR, -1);
6877c478bd9Sstevel@tonic-gate if (mp != NULL) {
6887c478bd9Sstevel@tonic-gate mp->b_rptr[0] = NOERROR;
6897c478bd9Sstevel@tonic-gate mp->b_rptr[1] = EINVAL;
6907c478bd9Sstevel@tonic-gate qreply(qp, mp);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate PT_EXIT_READ(ptmp);
693e2b86177SToomas Soome return (0);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate /*
696*1fa2a664SJoshua M. Clulow * While there are messages on this write queue...
6977c478bd9Sstevel@tonic-gate */
6989acbbeafSnn do {
6997c478bd9Sstevel@tonic-gate /*
700*1fa2a664SJoshua M. Clulow * If this is not a control message, and we cannot put messages
701*1fa2a664SJoshua M. Clulow * on the subsidiary's read queue, put it back on this queue.
7027c478bd9Sstevel@tonic-gate */
7037c478bd9Sstevel@tonic-gate if (mp->b_datap->db_type <= QPCTL &&
7047c478bd9Sstevel@tonic-gate !bcanputnext(ptmp->pts_rdq, mp->b_band)) {
7057c478bd9Sstevel@tonic-gate DBG(("put msg. back on queue\n"));
7067c478bd9Sstevel@tonic-gate (void) putbq(qp, mp);
7077c478bd9Sstevel@tonic-gate break;
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate /*
710*1fa2a664SJoshua M. Clulow * Otherwise send the message up subsidiary's stream
7117c478bd9Sstevel@tonic-gate */
712*1fa2a664SJoshua M. Clulow DBG(("send message to subsidiary\n"));
7137c478bd9Sstevel@tonic-gate putnext(ptmp->pts_rdq, mp);
7149acbbeafSnn } while ((mp = getq(qp)) != NULL);
7157c478bd9Sstevel@tonic-gate DBG(("leaving ptmwsrv\n"));
7167c478bd9Sstevel@tonic-gate PT_EXIT_READ(ptmp);
717e2b86177SToomas Soome return (0);
7187c478bd9Sstevel@tonic-gate }
719