10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52621Sllai1 * Common Development and Distribution License (the "License").
62621Sllai1 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12613SSurya.Prakki@Sun.COM * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
250Sstevel@tonic-gate /* All Rights Reserved */
260Sstevel@tonic-gate
270Sstevel@tonic-gate
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Pseudo Terminal Master Driver.
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * The pseudo-tty subsystem simulates a terminal connection, where the master
330Sstevel@tonic-gate * side represents the terminal and the slave represents the user process's
340Sstevel@tonic-gate * special device end point. The master device is set up as a cloned device
350Sstevel@tonic-gate * where its major device number is the major for the clone device and its minor
360Sstevel@tonic-gate * device number is the major for the ptm driver. There are no nodes in the file
370Sstevel@tonic-gate * system for master devices. The master pseudo driver is opened using the
380Sstevel@tonic-gate * open(2) system call with /dev/ptmx as the device parameter. The clone open
390Sstevel@tonic-gate * finds the next available minor device for the ptm major device.
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * A master device is available only if it and its corresponding slave device
420Sstevel@tonic-gate * are not already open. When the master device is opened, the corresponding
430Sstevel@tonic-gate * slave device is automatically locked out. Only one open is allowed on a
440Sstevel@tonic-gate * master device. Multiple opens are allowed on the slave device. After both
450Sstevel@tonic-gate * the master and slave have been opened, the user has two file descriptors
460Sstevel@tonic-gate * which are the end points of a full duplex connection composed of two streams
470Sstevel@tonic-gate * which are automatically connected at the master and slave drivers. The user
480Sstevel@tonic-gate * may then push modules onto either side of the stream pair.
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * The master and slave drivers pass all messages to their adjacent queues.
510Sstevel@tonic-gate * Only the M_FLUSH needs some processing. Because the read queue of one side
520Sstevel@tonic-gate * is connected to the write queue of the other, the FLUSHR flag is changed to
530Sstevel@tonic-gate * the FLUSHW flag and vice versa. When the master device is closed an M_HANGUP
540Sstevel@tonic-gate * message is sent to the slave device which will render the device
550Sstevel@tonic-gate * unusable. The process on the slave side gets the EIO when attempting to write
560Sstevel@tonic-gate * on that stream but it will be able to read any data remaining on the stream
570Sstevel@tonic-gate * head read queue. When all the data has been read, read() returns 0
580Sstevel@tonic-gate * indicating that the stream can no longer be used. On the last close of the
590Sstevel@tonic-gate * slave device, a 0-length message is sent to the master device. When the
600Sstevel@tonic-gate * application on the master side issues a read() or getmsg() and 0 is returned,
610Sstevel@tonic-gate * the user of the master device decides whether to issue a close() that
620Sstevel@tonic-gate * dismantles the pseudo-terminal subsystem. If the master device is not closed,
630Sstevel@tonic-gate * the pseudo-tty subsystem will be available to another user to open the slave
640Sstevel@tonic-gate * device.
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * If O_NONBLOCK or O_NDELAY is set, read on the master side returns -1 with
670Sstevel@tonic-gate * errno set to EAGAIN if no data is available, and write returns -1 with errno
680Sstevel@tonic-gate * set to EAGAIN if there is internal flow control.
690Sstevel@tonic-gate *
700Sstevel@tonic-gate * IOCTLS:
710Sstevel@tonic-gate *
720Sstevel@tonic-gate * ISPTM: determines whether the file descriptor is that of an open master
730Sstevel@tonic-gate * device. Return code of zero indicates that the file descriptor
740Sstevel@tonic-gate * represents master device.
750Sstevel@tonic-gate *
760Sstevel@tonic-gate * UNLKPT: unlocks the master and slave devices. It returns 0 on success. On
770Sstevel@tonic-gate * failure, the errno is set to EINVAL indicating that the master
780Sstevel@tonic-gate * device is not open.
790Sstevel@tonic-gate *
802621Sllai1 * ZONEPT: sets the zone membership of the associated pts device.
812621Sllai1 *
822621Sllai1 * GRPPT: sets the group owner of the associated pts device.
830Sstevel@tonic-gate *
840Sstevel@tonic-gate * Synchronization:
850Sstevel@tonic-gate *
860Sstevel@tonic-gate * All global data synchronization between ptm/pts is done via global
870Sstevel@tonic-gate * ptms_lock mutex which is initialized at system boot time from
880Sstevel@tonic-gate * ptms_initspace (called from space.c).
890Sstevel@tonic-gate *
900Sstevel@tonic-gate * Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
910Sstevel@tonic-gate * pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
920Sstevel@tonic-gate *
930Sstevel@tonic-gate * PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
940Sstevel@tonic-gate * which allow reader locks to be reacquired by the same thread (usual
950Sstevel@tonic-gate * reader/writer locks can't be used for that purpose since it is illegal for
960Sstevel@tonic-gate * a thread to acquire a lock it already holds, even as a reader). The sole
970Sstevel@tonic-gate * purpose of these macros is to guarantee that the peer queue will not
980Sstevel@tonic-gate * disappear (due to closing peer) while it is used. It is safe to use
990Sstevel@tonic-gate * PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
1000Sstevel@tonic-gate * they are not real locks but reference counts).
1010Sstevel@tonic-gate *
1020Sstevel@tonic-gate * PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave
1030Sstevel@tonic-gate * open/close paths to modify ptm_rdq and pts_rdq fields. These fields should
1040Sstevel@tonic-gate * be set to appropriate queues *after* qprocson() is called during open (to
1050Sstevel@tonic-gate * prevent peer from accessing the queue with incomplete plumbing) and set to
1060Sstevel@tonic-gate * NULL before qprocsoff() is called during close.
1070Sstevel@tonic-gate *
1080Sstevel@tonic-gate * The pt_nullmsg field is only used in open/close routines and it is also
1090Sstevel@tonic-gate * protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
1100Sstevel@tonic-gate * holds.
1110Sstevel@tonic-gate *
1120Sstevel@tonic-gate * Lock Ordering:
1130Sstevel@tonic-gate *
1140Sstevel@tonic-gate * If both ptms_lock and per-pty lock should be held, ptms_lock should always
1150Sstevel@tonic-gate * be entered first, followed by per-pty lock.
1160Sstevel@tonic-gate *
1170Sstevel@tonic-gate * See ptms.h, pts.c and ptms_conf.c for more information.
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate #include <sys/types.h>
1210Sstevel@tonic-gate #include <sys/param.h>
1220Sstevel@tonic-gate #include <sys/file.h>
1230Sstevel@tonic-gate #include <sys/sysmacros.h>
1240Sstevel@tonic-gate #include <sys/stream.h>
1250Sstevel@tonic-gate #include <sys/stropts.h>
1260Sstevel@tonic-gate #include <sys/proc.h>
1270Sstevel@tonic-gate #include <sys/errno.h>
1280Sstevel@tonic-gate #include <sys/debug.h>
1290Sstevel@tonic-gate #include <sys/cmn_err.h>
1300Sstevel@tonic-gate #include <sys/ptms.h>
1310Sstevel@tonic-gate #include <sys/stat.h>
1320Sstevel@tonic-gate #include <sys/strsun.h>
1330Sstevel@tonic-gate #include <sys/systm.h>
1340Sstevel@tonic-gate #include <sys/modctl.h>
1350Sstevel@tonic-gate #include <sys/conf.h>
1360Sstevel@tonic-gate #include <sys/ddi.h>
1370Sstevel@tonic-gate #include <sys/sunddi.h>
1380Sstevel@tonic-gate #include <sys/zone.h>
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate #ifdef DEBUG
1410Sstevel@tonic-gate int ptm_debug = 0;
1420Sstevel@tonic-gate #define DBG(a) if (ptm_debug) cmn_err(CE_NOTE, a)
1430Sstevel@tonic-gate #else
1440Sstevel@tonic-gate #define DBG(a)
1450Sstevel@tonic-gate #endif
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate static int ptmopen(queue_t *, dev_t *, int, int, cred_t *);
1480Sstevel@tonic-gate static int ptmclose(queue_t *, int, cred_t *);
1490Sstevel@tonic-gate static void ptmwput(queue_t *, mblk_t *);
1500Sstevel@tonic-gate static void ptmrsrv(queue_t *);
1510Sstevel@tonic-gate static void ptmwsrv(queue_t *);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Master Stream Pseudo Terminal Module: stream data structure definitions
1550Sstevel@tonic-gate */
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate static struct module_info ptm_info = {
1580Sstevel@tonic-gate 0xdead,
1590Sstevel@tonic-gate "ptm",
1600Sstevel@tonic-gate 0,
1610Sstevel@tonic-gate 512,
1620Sstevel@tonic-gate 512,
1630Sstevel@tonic-gate 128
1640Sstevel@tonic-gate };
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate static struct qinit ptmrint = {
1670Sstevel@tonic-gate NULL,
1680Sstevel@tonic-gate (int (*)()) ptmrsrv,
1690Sstevel@tonic-gate ptmopen,
1700Sstevel@tonic-gate ptmclose,
1710Sstevel@tonic-gate NULL,
1720Sstevel@tonic-gate &ptm_info,
1730Sstevel@tonic-gate NULL
1740Sstevel@tonic-gate };
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate static struct qinit ptmwint = {
1770Sstevel@tonic-gate (int (*)()) ptmwput,
1780Sstevel@tonic-gate (int (*)()) ptmwsrv,
1790Sstevel@tonic-gate NULL,
1800Sstevel@tonic-gate NULL,
1810Sstevel@tonic-gate NULL,
1820Sstevel@tonic-gate &ptm_info,
1830Sstevel@tonic-gate NULL
1840Sstevel@tonic-gate };
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate static struct streamtab ptminfo = {
1870Sstevel@tonic-gate &ptmrint,
1880Sstevel@tonic-gate &ptmwint,
1890Sstevel@tonic-gate NULL,
1900Sstevel@tonic-gate NULL
1910Sstevel@tonic-gate };
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate static int ptm_attach(dev_info_t *, ddi_attach_cmd_t);
1940Sstevel@tonic-gate static int ptm_detach(dev_info_t *, ddi_detach_cmd_t);
1950Sstevel@tonic-gate static int ptm_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate static dev_info_t *ptm_dip; /* private devinfo pointer */
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate * this will define (struct cb_ops cb_ptm_ops) and (struct dev_ops ptm_ops)
2010Sstevel@tonic-gate */
2020Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(ptm_ops, nulldev, nulldev, ptm_attach, ptm_detach,
2037656SSherry.Moore@Sun.COM nodev, ptm_devinfo, D_MP, &ptminfo, ddi_quiesce_not_supported);
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * Module linkage information for the kernel.
2070Sstevel@tonic-gate */
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate static struct modldrv modldrv = {
2100Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a pseudo driver */
2117656SSherry.Moore@Sun.COM "Master streams driver 'ptm'",
2120Sstevel@tonic-gate &ptm_ops, /* driver ops */
2130Sstevel@tonic-gate };
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate static struct modlinkage modlinkage = {
2160Sstevel@tonic-gate MODREV_1,
2170Sstevel@tonic-gate &modldrv,
2180Sstevel@tonic-gate NULL
2190Sstevel@tonic-gate };
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate int
_init(void)2220Sstevel@tonic-gate _init(void)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate int rc;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate if ((rc = mod_install(&modlinkage)) == 0)
2270Sstevel@tonic-gate ptms_init();
2280Sstevel@tonic-gate return (rc);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate int
_fini(void)2320Sstevel@tonic-gate _fini(void)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate return (mod_remove(&modlinkage));
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2380Sstevel@tonic-gate _info(struct modinfo *modinfop)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate static int
ptm_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2440Sstevel@tonic-gate ptm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate if (cmd != DDI_ATTACH)
2470Sstevel@tonic-gate return (DDI_FAILURE);
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if (ddi_create_minor_node(devi, "ptmajor", S_IFCHR,
2500Sstevel@tonic-gate 0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2510Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2520Sstevel@tonic-gate return (DDI_FAILURE);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate if (ddi_create_minor_node(devi, "ptmx", S_IFCHR,
2550Sstevel@tonic-gate 0, DDI_PSEUDO, CLONE_DEV) == DDI_FAILURE) {
2560Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2570Sstevel@tonic-gate return (DDI_FAILURE);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate ptm_dip = devi;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate return (DDI_SUCCESS);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate static int
ptm_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2650Sstevel@tonic-gate ptm_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate if (cmd != DDI_DETACH)
2680Sstevel@tonic-gate return (DDI_FAILURE);
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2710Sstevel@tonic-gate return (DDI_SUCCESS);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate /*ARGSUSED*/
2750Sstevel@tonic-gate static int
ptm_devinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2760Sstevel@tonic-gate ptm_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2770Sstevel@tonic-gate void **result)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate int error;
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate switch (infocmd) {
2820Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
2830Sstevel@tonic-gate if (ptm_dip == NULL) {
2840Sstevel@tonic-gate error = DDI_FAILURE;
2850Sstevel@tonic-gate } else {
2860Sstevel@tonic-gate *result = (void *)ptm_dip;
2870Sstevel@tonic-gate error = DDI_SUCCESS;
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate break;
2900Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
2910Sstevel@tonic-gate *result = (void *)0;
2920Sstevel@tonic-gate error = DDI_SUCCESS;
2930Sstevel@tonic-gate break;
2940Sstevel@tonic-gate default:
2950Sstevel@tonic-gate error = DDI_FAILURE;
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate return (error);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /* ARGSUSED */
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate * Open a minor of the master device. Store the write queue pointer and set the
3040Sstevel@tonic-gate * pt_state field to (PTMOPEN | PTLOCK).
3050Sstevel@tonic-gate * This code will work properly with both clone opens and direct opens of the
3060Sstevel@tonic-gate * master device.
3070Sstevel@tonic-gate */
3080Sstevel@tonic-gate static int
ptmopen(queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)3090Sstevel@tonic-gate ptmopen(
3100Sstevel@tonic-gate queue_t *rqp, /* pointer to the read side queue */
3110Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */
3120Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */
3130Sstevel@tonic-gate int sflag, /* open state flag */
3140Sstevel@tonic-gate cred_t *credp) /* credentials */
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate struct pt_ttys *ptmp;
3170Sstevel@tonic-gate mblk_t *mop; /* ptr to a setopts message block */
3180Sstevel@tonic-gate struct stroptions *sop;
3190Sstevel@tonic-gate minor_t dminor = getminor(*devp);
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate /* Allow reopen */
3220Sstevel@tonic-gate if (rqp->q_ptr != NULL)
3230Sstevel@tonic-gate return (0);
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate if (sflag & MODOPEN)
3260Sstevel@tonic-gate return (ENXIO);
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate if (!(sflag & CLONEOPEN) && dminor != 0) {
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate * This is a direct open to specific master device through an
3310Sstevel@tonic-gate * artificially created entry with specific minor in
3320Sstevel@tonic-gate * /dev/directory. Such behavior is not supported.
3330Sstevel@tonic-gate */
3340Sstevel@tonic-gate return (ENXIO);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate /*
3382621Sllai1 * The master open requires that the slave be attached
3392621Sllai1 * before it returns so that attempts to open the slave will
3402621Sllai1 * succeeed
3410Sstevel@tonic-gate */
3422621Sllai1 if (ptms_attach_slave() != 0) {
3432621Sllai1 return (ENXIO);
3442621Sllai1 }
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate mop = allocb(sizeof (struct stroptions), BPRI_MED);
3470Sstevel@tonic-gate if (mop == NULL) {
3480Sstevel@tonic-gate DDBG("ptmopen(): mop allocation failed\n", 0);
3490Sstevel@tonic-gate return (ENOMEM);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate if ((ptmp = pt_ttys_alloc()) == NULL) {
3530Sstevel@tonic-gate DDBG("ptmopen(): pty allocation failed\n", 0);
3540Sstevel@tonic-gate freemsg(mop);
3550Sstevel@tonic-gate return (ENOMEM);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate dminor = ptmp->pt_minor;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate DDBGP("ptmopen(): allocated ptmp %p\n", (uintptr_t)ptmp);
3610Sstevel@tonic-gate DDBG("ptmopen(): allocated minor %d\n", dminor);
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = ptmp;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate qprocson(rqp);
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate /* Allow slave to send messages to master */
3680Sstevel@tonic-gate PT_ENTER_WRITE(ptmp);
3690Sstevel@tonic-gate ptmp->ptm_rdq = rqp;
3700Sstevel@tonic-gate PT_EXIT_WRITE(ptmp);
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate /*
3730Sstevel@tonic-gate * set up hi/lo water marks on stream head read queue
3740Sstevel@tonic-gate * and add controlling tty if not set
3750Sstevel@tonic-gate */
3760Sstevel@tonic-gate mop->b_datap->db_type = M_SETOPTS;
3770Sstevel@tonic-gate mop->b_wptr += sizeof (struct stroptions);
3780Sstevel@tonic-gate sop = (struct stroptions *)mop->b_rptr;
3790Sstevel@tonic-gate if (oflag & FNOCTTY)
3800Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT;
3810Sstevel@tonic-gate else
3820Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
3830Sstevel@tonic-gate sop->so_hiwat = 512;
3840Sstevel@tonic-gate sop->so_lowat = 256;
3850Sstevel@tonic-gate putnext(rqp, mop);
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate * The input, devp, is a major device number, the output is put
3890Sstevel@tonic-gate * into the same parm as a major,minor pair.
3900Sstevel@tonic-gate */
3910Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), dminor);
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate return (0);
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate * Find the address to private data identifying the slave's write queue.
3990Sstevel@tonic-gate * Send a hang-up message up the slave's read queue to designate the
4000Sstevel@tonic-gate * master/slave pair is tearing down. Uattach the master and slave by
4010Sstevel@tonic-gate * nulling out the write queue fields in the private data structure.
4020Sstevel@tonic-gate * Finally, unlock the master/slave pair and mark the master as closed.
4030Sstevel@tonic-gate */
4040Sstevel@tonic-gate /*ARGSUSED1*/
4050Sstevel@tonic-gate static int
ptmclose(queue_t * rqp,int flag,cred_t * credp)4060Sstevel@tonic-gate ptmclose(queue_t *rqp, int flag, cred_t *credp)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate struct pt_ttys *ptmp;
4090Sstevel@tonic-gate queue_t *pts_rdq;
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate ASSERT(rqp->q_ptr);
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate ptmp = (struct pt_ttys *)rqp->q_ptr;
4140Sstevel@tonic-gate PT_ENTER_READ(ptmp);
4150Sstevel@tonic-gate if (ptmp->pts_rdq) {
4160Sstevel@tonic-gate pts_rdq = ptmp->pts_rdq;
4170Sstevel@tonic-gate if (pts_rdq->q_next) {
4180Sstevel@tonic-gate DBG(("send hangup message to slave\n"));
4190Sstevel@tonic-gate (void) putnextctl(pts_rdq, M_HANGUP);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate PT_EXIT_READ(ptmp);
4230Sstevel@tonic-gate /*
4240Sstevel@tonic-gate * ptm_rdq should be cleared before call to qprocsoff() to prevent pts
4250Sstevel@tonic-gate * write procedure to attempt using ptm_rdq after qprocsoff.
4260Sstevel@tonic-gate */
4270Sstevel@tonic-gate PT_ENTER_WRITE(ptmp);
4280Sstevel@tonic-gate ptmp->ptm_rdq = NULL;
4290Sstevel@tonic-gate freemsg(ptmp->pt_nullmsg);
4300Sstevel@tonic-gate ptmp->pt_nullmsg = NULL;
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate * qenable slave side write queue so that it can flush
4330Sstevel@tonic-gate * its messages as master's read queue is going away
4340Sstevel@tonic-gate */
4350Sstevel@tonic-gate if (ptmp->pts_rdq)
4360Sstevel@tonic-gate qenable(WR(ptmp->pts_rdq));
4370Sstevel@tonic-gate PT_EXIT_WRITE(ptmp);
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate qprocsoff(rqp);
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate /* Finish the close */
4420Sstevel@tonic-gate rqp->q_ptr = NULL;
4430Sstevel@tonic-gate WR(rqp)->q_ptr = NULL;
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate ptms_close(ptmp, PTMOPEN | PTLOCK);
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate return (0);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate * The wput procedure will only handle ioctl and flush messages.
4520Sstevel@tonic-gate */
4530Sstevel@tonic-gate static void
ptmwput(queue_t * qp,mblk_t * mp)4540Sstevel@tonic-gate ptmwput(queue_t *qp, mblk_t *mp)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate struct pt_ttys *ptmp;
4570Sstevel@tonic-gate struct iocblk *iocp;
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate DBG(("entering ptmwput\n"));
4600Sstevel@tonic-gate ASSERT(qp->q_ptr);
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate ptmp = (struct pt_ttys *)qp->q_ptr;
4630Sstevel@tonic-gate PT_ENTER_READ(ptmp);
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate switch (mp->b_datap->db_type) {
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate * if write queue request, flush master's write
4680Sstevel@tonic-gate * queue and send FLUSHR up slave side. If read
4690Sstevel@tonic-gate * queue request, convert to FLUSHW and putnext().
4700Sstevel@tonic-gate */
4710Sstevel@tonic-gate case M_FLUSH:
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate unsigned char flush_flg = 0;
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate DBG(("ptm got flush request\n"));
4760Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) {
4770Sstevel@tonic-gate DBG(("got FLUSHW, flush ptm write Q\n"));
4780Sstevel@tonic-gate if (*mp->b_rptr & FLUSHBAND)
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate * if it is a FLUSHBAND, do flushband.
4810Sstevel@tonic-gate */
4820Sstevel@tonic-gate flushband(qp, *(mp->b_rptr + 1),
4830Sstevel@tonic-gate FLUSHDATA);
4840Sstevel@tonic-gate else
4850Sstevel@tonic-gate flushq(qp, FLUSHDATA);
4860Sstevel@tonic-gate flush_flg = (*mp->b_rptr & ~FLUSHW) | FLUSHR;
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) {
4890Sstevel@tonic-gate DBG(("got FLUSHR, set FLUSHW\n"));
4900Sstevel@tonic-gate flush_flg |= (*mp->b_rptr & ~FLUSHR) | FLUSHW;
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate if (flush_flg != 0 && ptmp->pts_rdq &&
4930Sstevel@tonic-gate !(ptmp->pt_state & PTLOCK)) {
4940Sstevel@tonic-gate DBG(("putnext to pts\n"));
4950Sstevel@tonic-gate *mp->b_rptr = flush_flg;
4960Sstevel@tonic-gate putnext(ptmp->pts_rdq, mp);
4970Sstevel@tonic-gate } else
4980Sstevel@tonic-gate freemsg(mp);
4990Sstevel@tonic-gate break;
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate case M_IOCTL:
5030Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
5040Sstevel@tonic-gate switch (iocp->ioc_cmd) {
5050Sstevel@tonic-gate default:
5060Sstevel@tonic-gate if ((ptmp->pt_state & PTLOCK) ||
5070Sstevel@tonic-gate (ptmp->pts_rdq == NULL)) {
5080Sstevel@tonic-gate DBG(("got M_IOCTL but no slave\n"));
5090Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
5100Sstevel@tonic-gate PT_EXIT_READ(ptmp);
5110Sstevel@tonic-gate return;
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate (void) putq(qp, mp);
5140Sstevel@tonic-gate break;
5150Sstevel@tonic-gate case UNLKPT:
5160Sstevel@tonic-gate mutex_enter(&ptmp->pt_lock);
5170Sstevel@tonic-gate ptmp->pt_state &= ~PTLOCK;
5180Sstevel@tonic-gate mutex_exit(&ptmp->pt_lock);
5190Sstevel@tonic-gate /*FALLTHROUGH*/
5200Sstevel@tonic-gate case ISPTM:
5210Sstevel@tonic-gate DBG(("ack the UNLKPT/ISPTM\n"));
5220Sstevel@tonic-gate miocack(qp, mp, 0, 0);
5230Sstevel@tonic-gate break;
5240Sstevel@tonic-gate case ZONEPT:
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate zoneid_t z;
5270Sstevel@tonic-gate int error;
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate if ((error = drv_priv(iocp->ioc_cr)) != 0) {
5300Sstevel@tonic-gate miocnak(qp, mp, 0, error);
5310Sstevel@tonic-gate break;
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate if ((error = miocpullup(mp, sizeof (zoneid_t))) != 0) {
5340Sstevel@tonic-gate miocnak(qp, mp, 0, error);
5350Sstevel@tonic-gate break;
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate z = *((zoneid_t *)mp->b_cont->b_rptr);
5380Sstevel@tonic-gate if (z < MIN_ZONEID || z > MAX_ZONEID) {
5390Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
5400Sstevel@tonic-gate break;
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate mutex_enter(&ptmp->pt_lock);
5440Sstevel@tonic-gate ptmp->pt_zoneid = z;
5450Sstevel@tonic-gate mutex_exit(&ptmp->pt_lock);
5460Sstevel@tonic-gate miocack(qp, mp, 0, 0);
5470Sstevel@tonic-gate break;
5480Sstevel@tonic-gate }
5493442Svikram case OWNERPT:
5502621Sllai1 {
5512621Sllai1 pt_own_t *ptop;
5522621Sllai1 int error;
5535771Sjp151216 zone_t *zone;
5542621Sllai1
5552621Sllai1 if ((error = miocpullup(mp, sizeof (pt_own_t))) != 0) {
5562621Sllai1 miocnak(qp, mp, 0, error);
5572621Sllai1 break;
5582621Sllai1 }
5592621Sllai1
5605771Sjp151216 zone = zone_find_by_id(ptmp->pt_zoneid);
5612621Sllai1 ptop = (pt_own_t *)mp->b_cont->b_rptr;
5622621Sllai1
5635771Sjp151216 if (!VALID_UID(ptop->pto_ruid, zone) ||
5645771Sjp151216 !VALID_GID(ptop->pto_rgid, zone)) {
5655771Sjp151216 zone_rele(zone);
5662621Sllai1 miocnak(qp, mp, 0, EINVAL);
5672621Sllai1 break;
5682621Sllai1 }
5695771Sjp151216 zone_rele(zone);
5702621Sllai1 mutex_enter(&ptmp->pt_lock);
5712621Sllai1 ptmp->pt_ruid = ptop->pto_ruid;
5722621Sllai1 ptmp->pt_rgid = ptop->pto_rgid;
5732621Sllai1 mutex_exit(&ptmp->pt_lock);
5742621Sllai1 miocack(qp, mp, 0, 0);
5752621Sllai1 break;
5762621Sllai1 }
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate break;
5790Sstevel@tonic-gate
5800Sstevel@tonic-gate case M_READ:
5810Sstevel@tonic-gate /* Caused by ldterm - can not pass to slave */
5820Sstevel@tonic-gate freemsg(mp);
5830Sstevel@tonic-gate break;
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate * send other messages to slave
5870Sstevel@tonic-gate */
5880Sstevel@tonic-gate default:
5890Sstevel@tonic-gate if ((ptmp->pt_state & PTLOCK) || (ptmp->pts_rdq == NULL)) {
5900Sstevel@tonic-gate DBG(("got msg. but no slave\n"));
5910Sstevel@tonic-gate mp = mexchange(NULL, mp, 2, M_ERROR, -1);
5920Sstevel@tonic-gate if (mp != NULL) {
5930Sstevel@tonic-gate mp->b_rptr[0] = NOERROR;
5940Sstevel@tonic-gate mp->b_rptr[1] = EINVAL;
5950Sstevel@tonic-gate qreply(qp, mp);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate PT_EXIT_READ(ptmp);
5980Sstevel@tonic-gate return;
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate DBG(("put msg on master's write queue\n"));
6010Sstevel@tonic-gate (void) putq(qp, mp);
6020Sstevel@tonic-gate break;
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate DBG(("return from ptmwput()\n"));
6050Sstevel@tonic-gate PT_EXIT_READ(ptmp);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate /*
6100Sstevel@tonic-gate * enable the write side of the slave. This triggers the
6110Sstevel@tonic-gate * slave to send any messages queued on its write side to
6120Sstevel@tonic-gate * the read side of this master.
6130Sstevel@tonic-gate */
6140Sstevel@tonic-gate static void
ptmrsrv(queue_t * qp)6150Sstevel@tonic-gate ptmrsrv(queue_t *qp)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate struct pt_ttys *ptmp;
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate DBG(("entering ptmrsrv\n"));
6200Sstevel@tonic-gate ASSERT(qp->q_ptr);
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate ptmp = (struct pt_ttys *)qp->q_ptr;
6230Sstevel@tonic-gate PT_ENTER_READ(ptmp);
6240Sstevel@tonic-gate if (ptmp->pts_rdq) {
6250Sstevel@tonic-gate qenable(WR(ptmp->pts_rdq));
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate PT_EXIT_READ(ptmp);
6280Sstevel@tonic-gate DBG(("leaving ptmrsrv\n"));
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate /*
6330Sstevel@tonic-gate * If there are messages on this queue that can be sent to
6340Sstevel@tonic-gate * slave, send them via putnext(). Else, if queued messages
6350Sstevel@tonic-gate * cannot be sent, leave them on this queue. If priority
6360Sstevel@tonic-gate * messages on this queue, send them to slave no matter what.
6370Sstevel@tonic-gate */
6380Sstevel@tonic-gate static void
ptmwsrv(queue_t * qp)6390Sstevel@tonic-gate ptmwsrv(queue_t *qp)
6400Sstevel@tonic-gate {
6410Sstevel@tonic-gate struct pt_ttys *ptmp;
6420Sstevel@tonic-gate mblk_t *mp;
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate DBG(("entering ptmwsrv\n"));
6450Sstevel@tonic-gate ASSERT(qp->q_ptr);
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate ptmp = (struct pt_ttys *)qp->q_ptr;
6482712Snn35248
6492712Snn35248 if ((mp = getq(qp)) == NULL) {
6502712Snn35248 /* If there are no messages there's nothing to do. */
6512712Snn35248 DBG(("leaving ptmwsrv (no messages)\n"));
6522712Snn35248 return;
6532712Snn35248 }
6542712Snn35248
6550Sstevel@tonic-gate PT_ENTER_READ(ptmp);
6560Sstevel@tonic-gate if ((ptmp->pt_state & PTLOCK) || (ptmp->pts_rdq == NULL)) {
6570Sstevel@tonic-gate DBG(("in master write srv proc but no slave\n"));
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate * Free messages on the write queue and send
6600Sstevel@tonic-gate * NAK for any M_IOCTL type messages to wakeup
6610Sstevel@tonic-gate * the user process waiting for ACK/NAK from
6620Sstevel@tonic-gate * the ioctl invocation
6630Sstevel@tonic-gate */
6642712Snn35248 do {
6650Sstevel@tonic-gate if (mp->b_datap->db_type == M_IOCTL)
6660Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
6670Sstevel@tonic-gate else
6680Sstevel@tonic-gate freemsg(mp);
6692712Snn35248 } while ((mp = getq(qp)) != NULL);
6700Sstevel@tonic-gate flushq(qp, FLUSHALL);
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate mp = mexchange(NULL, NULL, 2, M_ERROR, -1);
6730Sstevel@tonic-gate if (mp != NULL) {
6740Sstevel@tonic-gate mp->b_rptr[0] = NOERROR;
6750Sstevel@tonic-gate mp->b_rptr[1] = EINVAL;
6760Sstevel@tonic-gate qreply(qp, mp);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate PT_EXIT_READ(ptmp);
6790Sstevel@tonic-gate return;
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate /*
6820Sstevel@tonic-gate * while there are messages on this write queue...
6830Sstevel@tonic-gate */
6842712Snn35248 do {
6850Sstevel@tonic-gate /*
6860Sstevel@tonic-gate * if don't have control message and cannot put
6870Sstevel@tonic-gate * msg. on slave's read queue, put it back on
6880Sstevel@tonic-gate * this queue.
6890Sstevel@tonic-gate */
6900Sstevel@tonic-gate if (mp->b_datap->db_type <= QPCTL &&
6910Sstevel@tonic-gate !bcanputnext(ptmp->pts_rdq, mp->b_band)) {
6920Sstevel@tonic-gate DBG(("put msg. back on queue\n"));
6930Sstevel@tonic-gate (void) putbq(qp, mp);
6940Sstevel@tonic-gate break;
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate * else send the message up slave's stream
6980Sstevel@tonic-gate */
6990Sstevel@tonic-gate DBG(("send message to slave\n"));
7000Sstevel@tonic-gate putnext(ptmp->pts_rdq, mp);
7012712Snn35248 } while ((mp = getq(qp)) != NULL);
7020Sstevel@tonic-gate DBG(("leaving ptmwsrv\n"));
7030Sstevel@tonic-gate PT_EXIT_READ(ptmp);
7040Sstevel@tonic-gate }
705