xref: /onnv-gate/usr/src/uts/common/io/ptm.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  * Pseudo Terminal Master Driver.
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * The pseudo-tty subsystem simulates a terminal connection, where the master
36*0Sstevel@tonic-gate  * side represents the terminal and the slave represents the user process's
37*0Sstevel@tonic-gate  * special device end point. The master device is set up as a cloned device
38*0Sstevel@tonic-gate  * where its major device number is the major for the clone device and its minor
39*0Sstevel@tonic-gate  * device number is the major for the ptm driver. There are no nodes in the file
40*0Sstevel@tonic-gate  * system for master devices. The master pseudo driver is opened using the
41*0Sstevel@tonic-gate  * open(2) system call with /dev/ptmx as the device parameter.  The clone open
42*0Sstevel@tonic-gate  * finds the next available minor device for the ptm major device.
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * A master device is available only if it and its corresponding slave device
45*0Sstevel@tonic-gate  * are not already open. When the master device is opened, the corresponding
46*0Sstevel@tonic-gate  * slave device is automatically locked out. Only one open is allowed on a
47*0Sstevel@tonic-gate  * master device.  Multiple opens are allowed on the slave device.  After both
48*0Sstevel@tonic-gate  * the master and slave have been opened, the user has two file descriptors
49*0Sstevel@tonic-gate  * which are the end points of a full duplex connection composed of two streams
50*0Sstevel@tonic-gate  * which are automatically connected at the master and slave drivers. The user
51*0Sstevel@tonic-gate  * may then push modules onto either side of the stream pair.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The master and slave drivers pass all messages to their adjacent queues.
54*0Sstevel@tonic-gate  * Only the M_FLUSH needs some processing.  Because the read queue of one side
55*0Sstevel@tonic-gate  * is connected to the write queue of the other, the FLUSHR flag is changed to
56*0Sstevel@tonic-gate  * the FLUSHW flag and vice versa. When the master device is closed an M_HANGUP
57*0Sstevel@tonic-gate  * message is sent to the slave device which will render the device
58*0Sstevel@tonic-gate  * unusable. The process on the slave side gets the EIO when attempting to write
59*0Sstevel@tonic-gate  * on that stream but it will be able to read any data remaining on the stream
60*0Sstevel@tonic-gate  * head read queue.  When all the data has been read, read() returns 0
61*0Sstevel@tonic-gate  * indicating that the stream can no longer be used.  On the last close of the
62*0Sstevel@tonic-gate  * slave device, a 0-length message is sent to the master device. When the
63*0Sstevel@tonic-gate  * application on the master side issues a read() or getmsg() and 0 is returned,
64*0Sstevel@tonic-gate  * the user of the master device decides whether to issue a close() that
65*0Sstevel@tonic-gate  * dismantles the pseudo-terminal subsystem. If the master device is not closed,
66*0Sstevel@tonic-gate  * the pseudo-tty subsystem will be available to another user to open the slave
67*0Sstevel@tonic-gate  * device.
68*0Sstevel@tonic-gate  *
69*0Sstevel@tonic-gate  * If O_NONBLOCK or O_NDELAY is set, read on the master side returns -1 with
70*0Sstevel@tonic-gate  * errno set to EAGAIN if no data is available, and write returns -1 with errno
71*0Sstevel@tonic-gate  * set to EAGAIN if there is internal flow control.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * IOCTLS:
74*0Sstevel@tonic-gate  *
75*0Sstevel@tonic-gate  *  ISPTM: determines whether the file descriptor is that of an open master
76*0Sstevel@tonic-gate  *	   device. Return code of zero indicates that the file descriptor
77*0Sstevel@tonic-gate  *	   represents master device.
78*0Sstevel@tonic-gate  *
79*0Sstevel@tonic-gate  *  UNLKPT: unlocks the master and slave devices.  It returns 0 on success. On
80*0Sstevel@tonic-gate  *	    failure, the errno is set to EINVAL indicating that the master
81*0Sstevel@tonic-gate  *	    device is not open.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  *  ZONEPT: sets the zone membership of ths associated pts device.
84*0Sstevel@tonic-gate  *
85*0Sstevel@tonic-gate  * Synchronization:
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  *   All global data synchronization between ptm/pts is done via global
88*0Sstevel@tonic-gate  *   ptms_lock mutex which is initialized at system boot time from
89*0Sstevel@tonic-gate  *   ptms_initspace (called from space.c).
90*0Sstevel@tonic-gate  *
91*0Sstevel@tonic-gate  *   Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
92*0Sstevel@tonic-gate  *   pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
93*0Sstevel@tonic-gate  *
94*0Sstevel@tonic-gate  *   PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
95*0Sstevel@tonic-gate  *   which allow reader locks to be reacquired by the same thread (usual
96*0Sstevel@tonic-gate  *   reader/writer locks can't be used for that purpose since it is illegal for
97*0Sstevel@tonic-gate  *   a thread to acquire a lock it already holds, even as a reader). The sole
98*0Sstevel@tonic-gate  *   purpose of these macros is to guarantee that the peer queue will not
99*0Sstevel@tonic-gate  *   disappear (due to closing peer) while it is used. It is safe to use
100*0Sstevel@tonic-gate  *   PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
101*0Sstevel@tonic-gate  *   they are not real locks but reference counts).
102*0Sstevel@tonic-gate  *
103*0Sstevel@tonic-gate  *   PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave
104*0Sstevel@tonic-gate  *   open/close paths to modify ptm_rdq and pts_rdq fields. These fields should
105*0Sstevel@tonic-gate  *   be set to appropriate queues *after* qprocson() is called during open (to
106*0Sstevel@tonic-gate  *   prevent peer from accessing the queue with incomplete plumbing) and set to
107*0Sstevel@tonic-gate  *   NULL before qprocsoff() is called during close.
108*0Sstevel@tonic-gate  *
109*0Sstevel@tonic-gate  *   The pt_nullmsg field is only used in open/close routines and it is also
110*0Sstevel@tonic-gate  *   protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
111*0Sstevel@tonic-gate  *   holds.
112*0Sstevel@tonic-gate  *
113*0Sstevel@tonic-gate  * Lock Ordering:
114*0Sstevel@tonic-gate  *
115*0Sstevel@tonic-gate  *   If both ptms_lock and per-pty lock should be held, ptms_lock should always
116*0Sstevel@tonic-gate  *   be entered first, followed by per-pty lock.
117*0Sstevel@tonic-gate  *
118*0Sstevel@tonic-gate  * See ptms.h, pts.c and ptms_conf.c for more information.
119*0Sstevel@tonic-gate  */
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate #include <sys/types.h>
122*0Sstevel@tonic-gate #include <sys/param.h>
123*0Sstevel@tonic-gate #include <sys/file.h>
124*0Sstevel@tonic-gate #include <sys/sysmacros.h>
125*0Sstevel@tonic-gate #include <sys/stream.h>
126*0Sstevel@tonic-gate #include <sys/stropts.h>
127*0Sstevel@tonic-gate #include <sys/proc.h>
128*0Sstevel@tonic-gate #include <sys/errno.h>
129*0Sstevel@tonic-gate #include <sys/debug.h>
130*0Sstevel@tonic-gate #include <sys/cmn_err.h>
131*0Sstevel@tonic-gate #include <sys/ptms.h>
132*0Sstevel@tonic-gate #include <sys/stat.h>
133*0Sstevel@tonic-gate #include <sys/strsun.h>
134*0Sstevel@tonic-gate #include <sys/systm.h>
135*0Sstevel@tonic-gate #include <sys/modctl.h>
136*0Sstevel@tonic-gate #include <sys/conf.h>
137*0Sstevel@tonic-gate #include <sys/ddi.h>
138*0Sstevel@tonic-gate #include <sys/sunddi.h>
139*0Sstevel@tonic-gate #include <sys/zone.h>
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate #ifdef DEBUG
142*0Sstevel@tonic-gate int ptm_debug = 0;
143*0Sstevel@tonic-gate #define	DBG(a)	 if (ptm_debug) cmn_err(CE_NOTE, a)
144*0Sstevel@tonic-gate #else
145*0Sstevel@tonic-gate #define	DBG(a)
146*0Sstevel@tonic-gate #endif
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate static int ptmopen(queue_t *, dev_t *, int, int, cred_t *);
149*0Sstevel@tonic-gate static int ptmclose(queue_t *, int, cred_t *);
150*0Sstevel@tonic-gate static void ptmwput(queue_t *, mblk_t *);
151*0Sstevel@tonic-gate static void ptmrsrv(queue_t *);
152*0Sstevel@tonic-gate static void ptmwsrv(queue_t *);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate /*
155*0Sstevel@tonic-gate  * Master Stream Pseudo Terminal Module: stream data structure definitions
156*0Sstevel@tonic-gate  */
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate static struct module_info ptm_info = {
159*0Sstevel@tonic-gate 	0xdead,
160*0Sstevel@tonic-gate 	"ptm",
161*0Sstevel@tonic-gate 	0,
162*0Sstevel@tonic-gate 	512,
163*0Sstevel@tonic-gate 	512,
164*0Sstevel@tonic-gate 	128
165*0Sstevel@tonic-gate };
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate static struct qinit ptmrint = {
168*0Sstevel@tonic-gate 	NULL,
169*0Sstevel@tonic-gate 	(int (*)()) ptmrsrv,
170*0Sstevel@tonic-gate 	ptmopen,
171*0Sstevel@tonic-gate 	ptmclose,
172*0Sstevel@tonic-gate 	NULL,
173*0Sstevel@tonic-gate 	&ptm_info,
174*0Sstevel@tonic-gate 	NULL
175*0Sstevel@tonic-gate };
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate static struct qinit ptmwint = {
178*0Sstevel@tonic-gate 	(int (*)()) ptmwput,
179*0Sstevel@tonic-gate 	(int (*)()) ptmwsrv,
180*0Sstevel@tonic-gate 	NULL,
181*0Sstevel@tonic-gate 	NULL,
182*0Sstevel@tonic-gate 	NULL,
183*0Sstevel@tonic-gate 	&ptm_info,
184*0Sstevel@tonic-gate 	NULL
185*0Sstevel@tonic-gate };
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate static struct streamtab ptminfo = {
188*0Sstevel@tonic-gate 	&ptmrint,
189*0Sstevel@tonic-gate 	&ptmwint,
190*0Sstevel@tonic-gate 	NULL,
191*0Sstevel@tonic-gate 	NULL
192*0Sstevel@tonic-gate };
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate static int ptm_attach(dev_info_t *, ddi_attach_cmd_t);
195*0Sstevel@tonic-gate static int ptm_detach(dev_info_t *, ddi_detach_cmd_t);
196*0Sstevel@tonic-gate static int ptm_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate static dev_info_t	*ptm_dip;		/* private devinfo pointer */
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate /*
201*0Sstevel@tonic-gate  * this will define (struct cb_ops cb_ptm_ops) and (struct dev_ops ptm_ops)
202*0Sstevel@tonic-gate  */
203*0Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(ptm_ops, nulldev, nulldev, ptm_attach, ptm_detach,
204*0Sstevel@tonic-gate     nodev, ptm_devinfo, D_MP, &ptminfo);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate  * Module linkage information for the kernel.
208*0Sstevel@tonic-gate  */
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate static struct modldrv modldrv = {
211*0Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
212*0Sstevel@tonic-gate 	"Master streams driver 'ptm' %I%",
213*0Sstevel@tonic-gate 	&ptm_ops,	/* driver ops */
214*0Sstevel@tonic-gate };
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
217*0Sstevel@tonic-gate 	MODREV_1,
218*0Sstevel@tonic-gate 	&modldrv,
219*0Sstevel@tonic-gate 	NULL
220*0Sstevel@tonic-gate };
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate int
223*0Sstevel@tonic-gate _init(void)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate 	int rc;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	if ((rc = mod_install(&modlinkage)) == 0)
228*0Sstevel@tonic-gate 		ptms_init();
229*0Sstevel@tonic-gate 	return (rc);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate int
233*0Sstevel@tonic-gate _fini(void)
234*0Sstevel@tonic-gate {
235*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate int
239*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
242*0Sstevel@tonic-gate }
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate static int
245*0Sstevel@tonic-gate ptm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
246*0Sstevel@tonic-gate {
247*0Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
248*0Sstevel@tonic-gate 		return (DDI_FAILURE);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "ptmajor", S_IFCHR,
251*0Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
252*0Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
253*0Sstevel@tonic-gate 		return (DDI_FAILURE);
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "ptmx", S_IFCHR,
256*0Sstevel@tonic-gate 	    0, DDI_PSEUDO, CLONE_DEV) == DDI_FAILURE) {
257*0Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
258*0Sstevel@tonic-gate 		return (DDI_FAILURE);
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 	ptm_dip = devi;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate static int
266*0Sstevel@tonic-gate ptm_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
267*0Sstevel@tonic-gate {
268*0Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
269*0Sstevel@tonic-gate 		return (DDI_FAILURE);
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
272*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate /*ARGSUSED*/
276*0Sstevel@tonic-gate static int
277*0Sstevel@tonic-gate ptm_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
278*0Sstevel@tonic-gate     void **result)
279*0Sstevel@tonic-gate {
280*0Sstevel@tonic-gate 	int error;
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	switch (infocmd) {
283*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
284*0Sstevel@tonic-gate 		if (ptm_dip == NULL) {
285*0Sstevel@tonic-gate 			error = DDI_FAILURE;
286*0Sstevel@tonic-gate 		} else {
287*0Sstevel@tonic-gate 			*result = (void *)ptm_dip;
288*0Sstevel@tonic-gate 			error = DDI_SUCCESS;
289*0Sstevel@tonic-gate 		}
290*0Sstevel@tonic-gate 		break;
291*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
292*0Sstevel@tonic-gate 		*result = (void *)0;
293*0Sstevel@tonic-gate 		error = DDI_SUCCESS;
294*0Sstevel@tonic-gate 		break;
295*0Sstevel@tonic-gate 	default:
296*0Sstevel@tonic-gate 		error = DDI_FAILURE;
297*0Sstevel@tonic-gate 	}
298*0Sstevel@tonic-gate 	return (error);
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate /* ARGSUSED */
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate  * Open a minor of the master device. Store the write queue pointer and set the
305*0Sstevel@tonic-gate  * pt_state field to (PTMOPEN | PTLOCK).
306*0Sstevel@tonic-gate  * This code will work properly with both clone opens and direct opens of the
307*0Sstevel@tonic-gate  * master device.
308*0Sstevel@tonic-gate  */
309*0Sstevel@tonic-gate static int
310*0Sstevel@tonic-gate ptmopen(
311*0Sstevel@tonic-gate 	queue_t *rqp,		/* pointer to the read side queue */
312*0Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
313*0Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
314*0Sstevel@tonic-gate 	int	sflag,		/* open state flag */
315*0Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
316*0Sstevel@tonic-gate {
317*0Sstevel@tonic-gate 	extern dev_info_t *pts_dip;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
320*0Sstevel@tonic-gate 	mblk_t		*mop;		/* ptr to a setopts message block */
321*0Sstevel@tonic-gate 	struct stroptions *sop;
322*0Sstevel@tonic-gate 	minor_t		dminor = getminor(*devp);
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	/* Allow reopen */
325*0Sstevel@tonic-gate 	if (rqp->q_ptr != NULL)
326*0Sstevel@tonic-gate 		return (0);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	if (sflag & MODOPEN)
329*0Sstevel@tonic-gate 		return (ENXIO);
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	if (!(sflag & CLONEOPEN) && dminor != 0) {
332*0Sstevel@tonic-gate 		/*
333*0Sstevel@tonic-gate 		 * This is a direct open to specific master device through an
334*0Sstevel@tonic-gate 		 * artificially created entry with specific minor in
335*0Sstevel@tonic-gate 		 * /dev/directory. Such behavior is not supported.
336*0Sstevel@tonic-gate 		 */
337*0Sstevel@tonic-gate 		return (ENXIO);
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	/*
341*0Sstevel@tonic-gate 	 * pts dependency: pt_ttys_alloc(), used below, really needs the pts
342*0Sstevel@tonic-gate 	 * driver (and pts_dip variable) to be initialized to successfully
343*0Sstevel@tonic-gate 	 * create device nodes.
344*0Sstevel@tonic-gate 	 */
345*0Sstevel@tonic-gate 	if (pts_dip == NULL)
346*0Sstevel@tonic-gate 		(void) i_ddi_attach_pseudo_node("pts");
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	mop = allocb(sizeof (struct stroptions), BPRI_MED);
349*0Sstevel@tonic-gate 	if (mop == NULL) {
350*0Sstevel@tonic-gate 		DDBG("ptmopen(): mop allocation failed\n", 0);
351*0Sstevel@tonic-gate 		return (ENOMEM);
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	if ((ptmp = pt_ttys_alloc()) == NULL) {
355*0Sstevel@tonic-gate 		DDBG("ptmopen(): pty allocation failed\n", 0);
356*0Sstevel@tonic-gate 		freemsg(mop);
357*0Sstevel@tonic-gate 		return (ENOMEM);
358*0Sstevel@tonic-gate 	}
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 	dminor = ptmp->pt_minor;
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	DDBGP("ptmopen(): allocated ptmp %p\n", (uintptr_t)ptmp);
363*0Sstevel@tonic-gate 	DDBG("ptmopen(): allocated minor %d\n", dminor);
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = ptmp;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	qprocson(rqp);
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	/* Allow slave to send messages to master */
370*0Sstevel@tonic-gate 	PT_ENTER_WRITE(ptmp);
371*0Sstevel@tonic-gate 	ptmp->ptm_rdq = rqp;
372*0Sstevel@tonic-gate 	PT_EXIT_WRITE(ptmp);
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	/*
375*0Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue
376*0Sstevel@tonic-gate 	 * and add controlling tty if not set
377*0Sstevel@tonic-gate 	 */
378*0Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
379*0Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
380*0Sstevel@tonic-gate 	sop = (struct stroptions *)mop->b_rptr;
381*0Sstevel@tonic-gate 	if (oflag & FNOCTTY)
382*0Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT;
383*0Sstevel@tonic-gate 	else
384*0Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
385*0Sstevel@tonic-gate 	sop->so_hiwat = 512;
386*0Sstevel@tonic-gate 	sop->so_lowat = 256;
387*0Sstevel@tonic-gate 	putnext(rqp, mop);
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	/*
390*0Sstevel@tonic-gate 	 * The input, devp, is a major device number, the output is put
391*0Sstevel@tonic-gate 	 * into the same parm as a major,minor pair.
392*0Sstevel@tonic-gate 	 */
393*0Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), dminor);
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	return (0);
396*0Sstevel@tonic-gate }
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate /*
400*0Sstevel@tonic-gate  * Find the address to private data identifying the slave's write queue.
401*0Sstevel@tonic-gate  * Send a hang-up message up the slave's read queue to designate the
402*0Sstevel@tonic-gate  * master/slave pair is tearing down. Uattach the master and slave by
403*0Sstevel@tonic-gate  * nulling out the write queue fields in the private data structure.
404*0Sstevel@tonic-gate  * Finally, unlock the master/slave pair and mark the master as closed.
405*0Sstevel@tonic-gate  */
406*0Sstevel@tonic-gate /*ARGSUSED1*/
407*0Sstevel@tonic-gate static int
408*0Sstevel@tonic-gate ptmclose(queue_t *rqp, int flag, cred_t *credp)
409*0Sstevel@tonic-gate {
410*0Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
411*0Sstevel@tonic-gate 	queue_t *pts_rdq;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	ASSERT(rqp->q_ptr);
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)rqp->q_ptr;
416*0Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
417*0Sstevel@tonic-gate 	if (ptmp->pts_rdq) {
418*0Sstevel@tonic-gate 		pts_rdq = ptmp->pts_rdq;
419*0Sstevel@tonic-gate 		if (pts_rdq->q_next) {
420*0Sstevel@tonic-gate 			DBG(("send hangup message to slave\n"));
421*0Sstevel@tonic-gate 			(void) putnextctl(pts_rdq, M_HANGUP);
422*0Sstevel@tonic-gate 		}
423*0Sstevel@tonic-gate 	}
424*0Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
425*0Sstevel@tonic-gate 	/*
426*0Sstevel@tonic-gate 	 * ptm_rdq should be cleared before call to qprocsoff() to prevent pts
427*0Sstevel@tonic-gate 	 * write procedure to attempt using ptm_rdq after qprocsoff.
428*0Sstevel@tonic-gate 	 */
429*0Sstevel@tonic-gate 	PT_ENTER_WRITE(ptmp);
430*0Sstevel@tonic-gate 	ptmp->ptm_rdq = NULL;
431*0Sstevel@tonic-gate 	freemsg(ptmp->pt_nullmsg);
432*0Sstevel@tonic-gate 	ptmp->pt_nullmsg = NULL;
433*0Sstevel@tonic-gate 	/*
434*0Sstevel@tonic-gate 	 * qenable slave side write queue so that it can flush
435*0Sstevel@tonic-gate 	 * its messages as master's read queue is going away
436*0Sstevel@tonic-gate 	 */
437*0Sstevel@tonic-gate 	if (ptmp->pts_rdq)
438*0Sstevel@tonic-gate 		qenable(WR(ptmp->pts_rdq));
439*0Sstevel@tonic-gate 	PT_EXIT_WRITE(ptmp);
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	qprocsoff(rqp);
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	/* Finish the close */
444*0Sstevel@tonic-gate 	rqp->q_ptr = NULL;
445*0Sstevel@tonic-gate 	WR(rqp)->q_ptr = NULL;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	ptms_close(ptmp, PTMOPEN | PTLOCK);
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	return (0);
450*0Sstevel@tonic-gate }
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate /*
453*0Sstevel@tonic-gate  * The wput procedure will only handle ioctl and flush messages.
454*0Sstevel@tonic-gate  */
455*0Sstevel@tonic-gate static void
456*0Sstevel@tonic-gate ptmwput(queue_t *qp, mblk_t *mp)
457*0Sstevel@tonic-gate {
458*0Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
459*0Sstevel@tonic-gate 	struct iocblk	*iocp;
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	DBG(("entering ptmwput\n"));
462*0Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)qp->q_ptr;
465*0Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
468*0Sstevel@tonic-gate 	/*
469*0Sstevel@tonic-gate 	 * if write queue request, flush master's write
470*0Sstevel@tonic-gate 	 * queue and send FLUSHR up slave side. If read
471*0Sstevel@tonic-gate 	 * queue request, convert to FLUSHW and putnext().
472*0Sstevel@tonic-gate 	 */
473*0Sstevel@tonic-gate 	case M_FLUSH:
474*0Sstevel@tonic-gate 		{
475*0Sstevel@tonic-gate 			unsigned char flush_flg = 0;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 			DBG(("ptm got flush request\n"));
478*0Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
479*0Sstevel@tonic-gate 				DBG(("got FLUSHW, flush ptm write Q\n"));
480*0Sstevel@tonic-gate 				if (*mp->b_rptr & FLUSHBAND)
481*0Sstevel@tonic-gate 					/*
482*0Sstevel@tonic-gate 					 * if it is a FLUSHBAND, do flushband.
483*0Sstevel@tonic-gate 					 */
484*0Sstevel@tonic-gate 					flushband(qp, *(mp->b_rptr + 1),
485*0Sstevel@tonic-gate 					    FLUSHDATA);
486*0Sstevel@tonic-gate 				else
487*0Sstevel@tonic-gate 					flushq(qp, FLUSHDATA);
488*0Sstevel@tonic-gate 				flush_flg = (*mp->b_rptr & ~FLUSHW) | FLUSHR;
489*0Sstevel@tonic-gate 			}
490*0Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHR) {
491*0Sstevel@tonic-gate 				DBG(("got FLUSHR, set FLUSHW\n"));
492*0Sstevel@tonic-gate 				flush_flg |= (*mp->b_rptr & ~FLUSHR) | FLUSHW;
493*0Sstevel@tonic-gate 			}
494*0Sstevel@tonic-gate 			if (flush_flg != 0 && ptmp->pts_rdq &&
495*0Sstevel@tonic-gate 			    !(ptmp->pt_state & PTLOCK)) {
496*0Sstevel@tonic-gate 				DBG(("putnext to pts\n"));
497*0Sstevel@tonic-gate 				*mp->b_rptr = flush_flg;
498*0Sstevel@tonic-gate 				putnext(ptmp->pts_rdq, mp);
499*0Sstevel@tonic-gate 			} else
500*0Sstevel@tonic-gate 				freemsg(mp);
501*0Sstevel@tonic-gate 			break;
502*0Sstevel@tonic-gate 		}
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	case M_IOCTL:
505*0Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
506*0Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
507*0Sstevel@tonic-gate 		default:
508*0Sstevel@tonic-gate 			if ((ptmp->pt_state & PTLOCK) ||
509*0Sstevel@tonic-gate 			    (ptmp->pts_rdq == NULL)) {
510*0Sstevel@tonic-gate 				DBG(("got M_IOCTL but no slave\n"));
511*0Sstevel@tonic-gate 				miocnak(qp, mp, 0, EINVAL);
512*0Sstevel@tonic-gate 				PT_EXIT_READ(ptmp);
513*0Sstevel@tonic-gate 				return;
514*0Sstevel@tonic-gate 			}
515*0Sstevel@tonic-gate 			(void) putq(qp, mp);
516*0Sstevel@tonic-gate 			break;
517*0Sstevel@tonic-gate 		case UNLKPT:
518*0Sstevel@tonic-gate 			mutex_enter(&ptmp->pt_lock);
519*0Sstevel@tonic-gate 			ptmp->pt_state &= ~PTLOCK;
520*0Sstevel@tonic-gate 			mutex_exit(&ptmp->pt_lock);
521*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
522*0Sstevel@tonic-gate 		case ISPTM:
523*0Sstevel@tonic-gate 			DBG(("ack the UNLKPT/ISPTM\n"));
524*0Sstevel@tonic-gate 			miocack(qp, mp, 0, 0);
525*0Sstevel@tonic-gate 			break;
526*0Sstevel@tonic-gate 		case ZONEPT:
527*0Sstevel@tonic-gate 		{
528*0Sstevel@tonic-gate 			zoneid_t z;
529*0Sstevel@tonic-gate 			int error;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 			if ((error = drv_priv(iocp->ioc_cr)) != 0) {
532*0Sstevel@tonic-gate 				miocnak(qp, mp, 0, error);
533*0Sstevel@tonic-gate 				break;
534*0Sstevel@tonic-gate 			}
535*0Sstevel@tonic-gate 			if ((error = miocpullup(mp, sizeof (zoneid_t))) != 0) {
536*0Sstevel@tonic-gate 				miocnak(qp, mp, 0, error);
537*0Sstevel@tonic-gate 				break;
538*0Sstevel@tonic-gate 			}
539*0Sstevel@tonic-gate 			z = *((zoneid_t *)mp->b_cont->b_rptr);
540*0Sstevel@tonic-gate 			if (z < MIN_ZONEID || z > MAX_ZONEID) {
541*0Sstevel@tonic-gate 				miocnak(qp, mp, 0, EINVAL);
542*0Sstevel@tonic-gate 				break;
543*0Sstevel@tonic-gate 			}
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 			mutex_enter(&ptmp->pt_lock);
546*0Sstevel@tonic-gate 			ptmp->pt_zoneid = z;
547*0Sstevel@tonic-gate 			mutex_exit(&ptmp->pt_lock);
548*0Sstevel@tonic-gate 			miocack(qp, mp, 0, 0);
549*0Sstevel@tonic-gate 			break;
550*0Sstevel@tonic-gate 		}
551*0Sstevel@tonic-gate 		}
552*0Sstevel@tonic-gate 		break;
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	case M_READ:
555*0Sstevel@tonic-gate 		/* Caused by ldterm - can not pass to slave */
556*0Sstevel@tonic-gate 		freemsg(mp);
557*0Sstevel@tonic-gate 		break;
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	/*
560*0Sstevel@tonic-gate 	 * send other messages to slave
561*0Sstevel@tonic-gate 	 */
562*0Sstevel@tonic-gate 	default:
563*0Sstevel@tonic-gate 		if ((ptmp->pt_state  & PTLOCK) || (ptmp->pts_rdq == NULL)) {
564*0Sstevel@tonic-gate 			DBG(("got msg. but no slave\n"));
565*0Sstevel@tonic-gate 			mp = mexchange(NULL, mp, 2, M_ERROR, -1);
566*0Sstevel@tonic-gate 			if (mp != NULL) {
567*0Sstevel@tonic-gate 				mp->b_rptr[0] = NOERROR;
568*0Sstevel@tonic-gate 				mp->b_rptr[1] = EINVAL;
569*0Sstevel@tonic-gate 				qreply(qp, mp);
570*0Sstevel@tonic-gate 			}
571*0Sstevel@tonic-gate 			PT_EXIT_READ(ptmp);
572*0Sstevel@tonic-gate 			return;
573*0Sstevel@tonic-gate 		}
574*0Sstevel@tonic-gate 		DBG(("put msg on master's write queue\n"));
575*0Sstevel@tonic-gate 		(void) putq(qp, mp);
576*0Sstevel@tonic-gate 		break;
577*0Sstevel@tonic-gate 	}
578*0Sstevel@tonic-gate 	DBG(("return from ptmwput()\n"));
579*0Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
580*0Sstevel@tonic-gate }
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate /*
584*0Sstevel@tonic-gate  * enable the write side of the slave. This triggers the
585*0Sstevel@tonic-gate  * slave to send any messages queued on its write side to
586*0Sstevel@tonic-gate  * the read side of this master.
587*0Sstevel@tonic-gate  */
588*0Sstevel@tonic-gate static void
589*0Sstevel@tonic-gate ptmrsrv(queue_t *qp)
590*0Sstevel@tonic-gate {
591*0Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 	DBG(("entering ptmrsrv\n"));
594*0Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)qp->q_ptr;
597*0Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
598*0Sstevel@tonic-gate 	if (ptmp->pts_rdq) {
599*0Sstevel@tonic-gate 		qenable(WR(ptmp->pts_rdq));
600*0Sstevel@tonic-gate 	}
601*0Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
602*0Sstevel@tonic-gate 	DBG(("leaving ptmrsrv\n"));
603*0Sstevel@tonic-gate }
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate /*
607*0Sstevel@tonic-gate  * If there are messages on this queue that can be sent to
608*0Sstevel@tonic-gate  * slave, send them via putnext(). Else, if queued messages
609*0Sstevel@tonic-gate  * cannot be sent, leave them on this queue. If priority
610*0Sstevel@tonic-gate  * messages on this queue, send them to slave no matter what.
611*0Sstevel@tonic-gate  */
612*0Sstevel@tonic-gate static void
613*0Sstevel@tonic-gate ptmwsrv(queue_t *qp)
614*0Sstevel@tonic-gate {
615*0Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
616*0Sstevel@tonic-gate 	mblk_t 		*mp;
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	DBG(("entering ptmwsrv\n"));
619*0Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)qp->q_ptr;
622*0Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
623*0Sstevel@tonic-gate 	if ((ptmp->pt_state  & PTLOCK) || (ptmp->pts_rdq == NULL)) {
624*0Sstevel@tonic-gate 		DBG(("in master write srv proc but no slave\n"));
625*0Sstevel@tonic-gate 		/*
626*0Sstevel@tonic-gate 		 * Free messages on the write queue and send
627*0Sstevel@tonic-gate 		 * NAK for any M_IOCTL type messages to wakeup
628*0Sstevel@tonic-gate 		 * the user process waiting for ACK/NAK from
629*0Sstevel@tonic-gate 		 * the ioctl invocation
630*0Sstevel@tonic-gate 		 */
631*0Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
632*0Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL)
633*0Sstevel@tonic-gate 				miocnak(qp, mp, 0, EINVAL);
634*0Sstevel@tonic-gate 			else
635*0Sstevel@tonic-gate 				freemsg(mp);
636*0Sstevel@tonic-gate 		}
637*0Sstevel@tonic-gate 		flushq(qp, FLUSHALL);
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		mp = mexchange(NULL, NULL, 2, M_ERROR, -1);
640*0Sstevel@tonic-gate 		if (mp != NULL) {
641*0Sstevel@tonic-gate 			mp->b_rptr[0] = NOERROR;
642*0Sstevel@tonic-gate 			mp->b_rptr[1] = EINVAL;
643*0Sstevel@tonic-gate 			qreply(qp, mp);
644*0Sstevel@tonic-gate 		}
645*0Sstevel@tonic-gate 		PT_EXIT_READ(ptmp);
646*0Sstevel@tonic-gate 		return;
647*0Sstevel@tonic-gate 	}
648*0Sstevel@tonic-gate 	/*
649*0Sstevel@tonic-gate 	 * while there are messages on this write queue...
650*0Sstevel@tonic-gate 	 */
651*0Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
652*0Sstevel@tonic-gate 		/*
653*0Sstevel@tonic-gate 		 * if don't have control message and cannot put
654*0Sstevel@tonic-gate 		 * msg. on slave's read queue, put it back on
655*0Sstevel@tonic-gate 		 * this queue.
656*0Sstevel@tonic-gate 		 */
657*0Sstevel@tonic-gate 		if (mp->b_datap->db_type <= QPCTL &&
658*0Sstevel@tonic-gate 		    !bcanputnext(ptmp->pts_rdq, mp->b_band)) {
659*0Sstevel@tonic-gate 			DBG(("put msg. back on queue\n"));
660*0Sstevel@tonic-gate 			(void) putbq(qp, mp);
661*0Sstevel@tonic-gate 			break;
662*0Sstevel@tonic-gate 		}
663*0Sstevel@tonic-gate 		/*
664*0Sstevel@tonic-gate 		 * else send the message up slave's stream
665*0Sstevel@tonic-gate 		 */
666*0Sstevel@tonic-gate 		DBG(("send message to slave\n"));
667*0Sstevel@tonic-gate 		putnext(ptmp->pts_rdq, mp);
668*0Sstevel@tonic-gate 	}
669*0Sstevel@tonic-gate 	DBG(("leaving ptmwsrv\n"));
670*0Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
671*0Sstevel@tonic-gate }
672