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*5771Sjp151216 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 260Sstevel@tonic-gate /* All Rights Reserved */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate 290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* 320Sstevel@tonic-gate * Pseudo Terminal Master Driver. 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * The pseudo-tty subsystem simulates a terminal connection, where the master 350Sstevel@tonic-gate * side represents the terminal and the slave represents the user process's 360Sstevel@tonic-gate * special device end point. The master device is set up as a cloned device 370Sstevel@tonic-gate * where its major device number is the major for the clone device and its minor 380Sstevel@tonic-gate * device number is the major for the ptm driver. There are no nodes in the file 390Sstevel@tonic-gate * system for master devices. The master pseudo driver is opened using the 400Sstevel@tonic-gate * open(2) system call with /dev/ptmx as the device parameter. The clone open 410Sstevel@tonic-gate * finds the next available minor device for the ptm major device. 420Sstevel@tonic-gate * 430Sstevel@tonic-gate * A master device is available only if it and its corresponding slave device 440Sstevel@tonic-gate * are not already open. When the master device is opened, the corresponding 450Sstevel@tonic-gate * slave device is automatically locked out. Only one open is allowed on a 460Sstevel@tonic-gate * master device. Multiple opens are allowed on the slave device. After both 470Sstevel@tonic-gate * the master and slave have been opened, the user has two file descriptors 480Sstevel@tonic-gate * which are the end points of a full duplex connection composed of two streams 490Sstevel@tonic-gate * which are automatically connected at the master and slave drivers. The user 500Sstevel@tonic-gate * may then push modules onto either side of the stream pair. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * The master and slave drivers pass all messages to their adjacent queues. 530Sstevel@tonic-gate * Only the M_FLUSH needs some processing. Because the read queue of one side 540Sstevel@tonic-gate * is connected to the write queue of the other, the FLUSHR flag is changed to 550Sstevel@tonic-gate * the FLUSHW flag and vice versa. When the master device is closed an M_HANGUP 560Sstevel@tonic-gate * message is sent to the slave device which will render the device 570Sstevel@tonic-gate * unusable. The process on the slave side gets the EIO when attempting to write 580Sstevel@tonic-gate * on that stream but it will be able to read any data remaining on the stream 590Sstevel@tonic-gate * head read queue. When all the data has been read, read() returns 0 600Sstevel@tonic-gate * indicating that the stream can no longer be used. On the last close of the 610Sstevel@tonic-gate * slave device, a 0-length message is sent to the master device. When the 620Sstevel@tonic-gate * application on the master side issues a read() or getmsg() and 0 is returned, 630Sstevel@tonic-gate * the user of the master device decides whether to issue a close() that 640Sstevel@tonic-gate * dismantles the pseudo-terminal subsystem. If the master device is not closed, 650Sstevel@tonic-gate * the pseudo-tty subsystem will be available to another user to open the slave 660Sstevel@tonic-gate * device. 670Sstevel@tonic-gate * 680Sstevel@tonic-gate * If O_NONBLOCK or O_NDELAY is set, read on the master side returns -1 with 690Sstevel@tonic-gate * errno set to EAGAIN if no data is available, and write returns -1 with errno 700Sstevel@tonic-gate * set to EAGAIN if there is internal flow control. 710Sstevel@tonic-gate * 720Sstevel@tonic-gate * IOCTLS: 730Sstevel@tonic-gate * 740Sstevel@tonic-gate * ISPTM: determines whether the file descriptor is that of an open master 750Sstevel@tonic-gate * device. Return code of zero indicates that the file descriptor 760Sstevel@tonic-gate * represents master device. 770Sstevel@tonic-gate * 780Sstevel@tonic-gate * UNLKPT: unlocks the master and slave devices. It returns 0 on success. On 790Sstevel@tonic-gate * failure, the errno is set to EINVAL indicating that the master 800Sstevel@tonic-gate * device is not open. 810Sstevel@tonic-gate * 822621Sllai1 * ZONEPT: sets the zone membership of the associated pts device. 832621Sllai1 * 842621Sllai1 * GRPPT: sets the group owner of the associated pts device. 850Sstevel@tonic-gate * 860Sstevel@tonic-gate * Synchronization: 870Sstevel@tonic-gate * 880Sstevel@tonic-gate * All global data synchronization between ptm/pts is done via global 890Sstevel@tonic-gate * ptms_lock mutex which is initialized at system boot time from 900Sstevel@tonic-gate * ptms_initspace (called from space.c). 910Sstevel@tonic-gate * 920Sstevel@tonic-gate * Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and 930Sstevel@tonic-gate * pt_nullmsg) are protected by pt_ttys.pt_lock mutex. 940Sstevel@tonic-gate * 950Sstevel@tonic-gate * PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks 960Sstevel@tonic-gate * which allow reader locks to be reacquired by the same thread (usual 970Sstevel@tonic-gate * reader/writer locks can't be used for that purpose since it is illegal for 980Sstevel@tonic-gate * a thread to acquire a lock it already holds, even as a reader). The sole 990Sstevel@tonic-gate * purpose of these macros is to guarantee that the peer queue will not 1000Sstevel@tonic-gate * disappear (due to closing peer) while it is used. It is safe to use 1010Sstevel@tonic-gate * PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since 1020Sstevel@tonic-gate * they are not real locks but reference counts). 1030Sstevel@tonic-gate * 1040Sstevel@tonic-gate * PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave 1050Sstevel@tonic-gate * open/close paths to modify ptm_rdq and pts_rdq fields. These fields should 1060Sstevel@tonic-gate * be set to appropriate queues *after* qprocson() is called during open (to 1070Sstevel@tonic-gate * prevent peer from accessing the queue with incomplete plumbing) and set to 1080Sstevel@tonic-gate * NULL before qprocsoff() is called during close. 1090Sstevel@tonic-gate * 1100Sstevel@tonic-gate * The pt_nullmsg field is only used in open/close routines and it is also 1110Sstevel@tonic-gate * protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex 1120Sstevel@tonic-gate * holds. 1130Sstevel@tonic-gate * 1140Sstevel@tonic-gate * Lock Ordering: 1150Sstevel@tonic-gate * 1160Sstevel@tonic-gate * If both ptms_lock and per-pty lock should be held, ptms_lock should always 1170Sstevel@tonic-gate * be entered first, followed by per-pty lock. 1180Sstevel@tonic-gate * 1190Sstevel@tonic-gate * See ptms.h, pts.c and ptms_conf.c for more information. 1200Sstevel@tonic-gate */ 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate #include <sys/types.h> 1230Sstevel@tonic-gate #include <sys/param.h> 1240Sstevel@tonic-gate #include <sys/file.h> 1250Sstevel@tonic-gate #include <sys/sysmacros.h> 1260Sstevel@tonic-gate #include <sys/stream.h> 1270Sstevel@tonic-gate #include <sys/stropts.h> 1280Sstevel@tonic-gate #include <sys/proc.h> 1290Sstevel@tonic-gate #include <sys/errno.h> 1300Sstevel@tonic-gate #include <sys/debug.h> 1310Sstevel@tonic-gate #include <sys/cmn_err.h> 1320Sstevel@tonic-gate #include <sys/ptms.h> 1330Sstevel@tonic-gate #include <sys/stat.h> 1340Sstevel@tonic-gate #include <sys/strsun.h> 1350Sstevel@tonic-gate #include <sys/systm.h> 1360Sstevel@tonic-gate #include <sys/modctl.h> 1370Sstevel@tonic-gate #include <sys/conf.h> 1380Sstevel@tonic-gate #include <sys/ddi.h> 1390Sstevel@tonic-gate #include <sys/sunddi.h> 1400Sstevel@tonic-gate #include <sys/zone.h> 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate #ifdef DEBUG 1430Sstevel@tonic-gate int ptm_debug = 0; 1440Sstevel@tonic-gate #define DBG(a) if (ptm_debug) cmn_err(CE_NOTE, a) 1450Sstevel@tonic-gate #else 1460Sstevel@tonic-gate #define DBG(a) 1470Sstevel@tonic-gate #endif 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate static int ptmopen(queue_t *, dev_t *, int, int, cred_t *); 1500Sstevel@tonic-gate static int ptmclose(queue_t *, int, cred_t *); 1510Sstevel@tonic-gate static void ptmwput(queue_t *, mblk_t *); 1520Sstevel@tonic-gate static void ptmrsrv(queue_t *); 1530Sstevel@tonic-gate static void ptmwsrv(queue_t *); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate /* 1560Sstevel@tonic-gate * Master Stream Pseudo Terminal Module: stream data structure definitions 1570Sstevel@tonic-gate */ 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static struct module_info ptm_info = { 1600Sstevel@tonic-gate 0xdead, 1610Sstevel@tonic-gate "ptm", 1620Sstevel@tonic-gate 0, 1630Sstevel@tonic-gate 512, 1640Sstevel@tonic-gate 512, 1650Sstevel@tonic-gate 128 1660Sstevel@tonic-gate }; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate static struct qinit ptmrint = { 1690Sstevel@tonic-gate NULL, 1700Sstevel@tonic-gate (int (*)()) ptmrsrv, 1710Sstevel@tonic-gate ptmopen, 1720Sstevel@tonic-gate ptmclose, 1730Sstevel@tonic-gate NULL, 1740Sstevel@tonic-gate &ptm_info, 1750Sstevel@tonic-gate NULL 1760Sstevel@tonic-gate }; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate static struct qinit ptmwint = { 1790Sstevel@tonic-gate (int (*)()) ptmwput, 1800Sstevel@tonic-gate (int (*)()) ptmwsrv, 1810Sstevel@tonic-gate NULL, 1820Sstevel@tonic-gate NULL, 1830Sstevel@tonic-gate NULL, 1840Sstevel@tonic-gate &ptm_info, 1850Sstevel@tonic-gate NULL 1860Sstevel@tonic-gate }; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate static struct streamtab ptminfo = { 1890Sstevel@tonic-gate &ptmrint, 1900Sstevel@tonic-gate &ptmwint, 1910Sstevel@tonic-gate NULL, 1920Sstevel@tonic-gate NULL 1930Sstevel@tonic-gate }; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate static int ptm_attach(dev_info_t *, ddi_attach_cmd_t); 1960Sstevel@tonic-gate static int ptm_detach(dev_info_t *, ddi_detach_cmd_t); 1970Sstevel@tonic-gate static int ptm_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate static dev_info_t *ptm_dip; /* private devinfo pointer */ 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate /* 2020Sstevel@tonic-gate * this will define (struct cb_ops cb_ptm_ops) and (struct dev_ops ptm_ops) 2030Sstevel@tonic-gate */ 2040Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(ptm_ops, nulldev, nulldev, ptm_attach, ptm_detach, 2050Sstevel@tonic-gate nodev, ptm_devinfo, D_MP, &ptminfo); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* 2080Sstevel@tonic-gate * Module linkage information for the kernel. 2090Sstevel@tonic-gate */ 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate static struct modldrv modldrv = { 2120Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a pseudo driver */ 2130Sstevel@tonic-gate "Master streams driver 'ptm' %I%", 2140Sstevel@tonic-gate &ptm_ops, /* driver ops */ 2150Sstevel@tonic-gate }; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate static struct modlinkage modlinkage = { 2180Sstevel@tonic-gate MODREV_1, 2190Sstevel@tonic-gate &modldrv, 2200Sstevel@tonic-gate NULL 2210Sstevel@tonic-gate }; 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate int 2240Sstevel@tonic-gate _init(void) 2250Sstevel@tonic-gate { 2260Sstevel@tonic-gate int rc; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate if ((rc = mod_install(&modlinkage)) == 0) 2290Sstevel@tonic-gate ptms_init(); 2300Sstevel@tonic-gate return (rc); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate int 2340Sstevel@tonic-gate _fini(void) 2350Sstevel@tonic-gate { 2360Sstevel@tonic-gate return (mod_remove(&modlinkage)); 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate int 2400Sstevel@tonic-gate _info(struct modinfo *modinfop) 2410Sstevel@tonic-gate { 2420Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate static int 2460Sstevel@tonic-gate ptm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 2470Sstevel@tonic-gate { 2480Sstevel@tonic-gate if (cmd != DDI_ATTACH) 2490Sstevel@tonic-gate return (DDI_FAILURE); 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate if (ddi_create_minor_node(devi, "ptmajor", S_IFCHR, 2520Sstevel@tonic-gate 0, DDI_PSEUDO, NULL) == DDI_FAILURE) { 2530Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 2540Sstevel@tonic-gate return (DDI_FAILURE); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate if (ddi_create_minor_node(devi, "ptmx", S_IFCHR, 2570Sstevel@tonic-gate 0, DDI_PSEUDO, CLONE_DEV) == DDI_FAILURE) { 2580Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 2590Sstevel@tonic-gate return (DDI_FAILURE); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate ptm_dip = devi; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate return (DDI_SUCCESS); 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate static int 2670Sstevel@tonic-gate ptm_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 2680Sstevel@tonic-gate { 2690Sstevel@tonic-gate if (cmd != DDI_DETACH) 2700Sstevel@tonic-gate return (DDI_FAILURE); 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 2730Sstevel@tonic-gate return (DDI_SUCCESS); 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate /*ARGSUSED*/ 2770Sstevel@tonic-gate static int 2780Sstevel@tonic-gate ptm_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 2790Sstevel@tonic-gate void **result) 2800Sstevel@tonic-gate { 2810Sstevel@tonic-gate int error; 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate switch (infocmd) { 2840Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 2850Sstevel@tonic-gate if (ptm_dip == NULL) { 2860Sstevel@tonic-gate error = DDI_FAILURE; 2870Sstevel@tonic-gate } else { 2880Sstevel@tonic-gate *result = (void *)ptm_dip; 2890Sstevel@tonic-gate error = DDI_SUCCESS; 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate break; 2920Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 2930Sstevel@tonic-gate *result = (void *)0; 2940Sstevel@tonic-gate error = DDI_SUCCESS; 2950Sstevel@tonic-gate break; 2960Sstevel@tonic-gate default: 2970Sstevel@tonic-gate error = DDI_FAILURE; 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate return (error); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /* ARGSUSED */ 3040Sstevel@tonic-gate /* 3050Sstevel@tonic-gate * Open a minor of the master device. Store the write queue pointer and set the 3060Sstevel@tonic-gate * pt_state field to (PTMOPEN | PTLOCK). 3070Sstevel@tonic-gate * This code will work properly with both clone opens and direct opens of the 3080Sstevel@tonic-gate * master device. 3090Sstevel@tonic-gate */ 3100Sstevel@tonic-gate static int 3110Sstevel@tonic-gate ptmopen( 3120Sstevel@tonic-gate queue_t *rqp, /* pointer to the read side queue */ 3130Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 3140Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 3150Sstevel@tonic-gate int sflag, /* open state flag */ 3160Sstevel@tonic-gate cred_t *credp) /* credentials */ 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate struct pt_ttys *ptmp; 3190Sstevel@tonic-gate mblk_t *mop; /* ptr to a setopts message block */ 3200Sstevel@tonic-gate struct stroptions *sop; 3210Sstevel@tonic-gate minor_t dminor = getminor(*devp); 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate /* Allow reopen */ 3240Sstevel@tonic-gate if (rqp->q_ptr != NULL) 3250Sstevel@tonic-gate return (0); 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate if (sflag & MODOPEN) 3280Sstevel@tonic-gate return (ENXIO); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate if (!(sflag & CLONEOPEN) && dminor != 0) { 3310Sstevel@tonic-gate /* 3320Sstevel@tonic-gate * This is a direct open to specific master device through an 3330Sstevel@tonic-gate * artificially created entry with specific minor in 3340Sstevel@tonic-gate * /dev/directory. Such behavior is not supported. 3350Sstevel@tonic-gate */ 3360Sstevel@tonic-gate return (ENXIO); 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate /* 3402621Sllai1 * The master open requires that the slave be attached 3412621Sllai1 * before it returns so that attempts to open the slave will 3422621Sllai1 * succeeed 3430Sstevel@tonic-gate */ 3442621Sllai1 if (ptms_attach_slave() != 0) { 3452621Sllai1 return (ENXIO); 3462621Sllai1 } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate mop = allocb(sizeof (struct stroptions), BPRI_MED); 3490Sstevel@tonic-gate if (mop == NULL) { 3500Sstevel@tonic-gate DDBG("ptmopen(): mop allocation failed\n", 0); 3510Sstevel@tonic-gate return (ENOMEM); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate if ((ptmp = pt_ttys_alloc()) == NULL) { 3550Sstevel@tonic-gate DDBG("ptmopen(): pty allocation failed\n", 0); 3560Sstevel@tonic-gate freemsg(mop); 3570Sstevel@tonic-gate return (ENOMEM); 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate dminor = ptmp->pt_minor; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate DDBGP("ptmopen(): allocated ptmp %p\n", (uintptr_t)ptmp); 3630Sstevel@tonic-gate DDBG("ptmopen(): allocated minor %d\n", dminor); 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = ptmp; 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate qprocson(rqp); 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* Allow slave to send messages to master */ 3700Sstevel@tonic-gate PT_ENTER_WRITE(ptmp); 3710Sstevel@tonic-gate ptmp->ptm_rdq = rqp; 3720Sstevel@tonic-gate PT_EXIT_WRITE(ptmp); 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate /* 3750Sstevel@tonic-gate * set up hi/lo water marks on stream head read queue 3760Sstevel@tonic-gate * and add controlling tty if not set 3770Sstevel@tonic-gate */ 3780Sstevel@tonic-gate mop->b_datap->db_type = M_SETOPTS; 3790Sstevel@tonic-gate mop->b_wptr += sizeof (struct stroptions); 3800Sstevel@tonic-gate sop = (struct stroptions *)mop->b_rptr; 3810Sstevel@tonic-gate if (oflag & FNOCTTY) 3820Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT; 3830Sstevel@tonic-gate else 3840Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY; 3850Sstevel@tonic-gate sop->so_hiwat = 512; 3860Sstevel@tonic-gate sop->so_lowat = 256; 3870Sstevel@tonic-gate putnext(rqp, mop); 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate /* 3900Sstevel@tonic-gate * The input, devp, is a major device number, the output is put 3910Sstevel@tonic-gate * into the same parm as a major,minor pair. 3920Sstevel@tonic-gate */ 3930Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), dminor); 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate return (0); 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate /* 4000Sstevel@tonic-gate * Find the address to private data identifying the slave's write queue. 4010Sstevel@tonic-gate * Send a hang-up message up the slave's read queue to designate the 4020Sstevel@tonic-gate * master/slave pair is tearing down. Uattach the master and slave by 4030Sstevel@tonic-gate * nulling out the write queue fields in the private data structure. 4040Sstevel@tonic-gate * Finally, unlock the master/slave pair and mark the master as closed. 4050Sstevel@tonic-gate */ 4060Sstevel@tonic-gate /*ARGSUSED1*/ 4070Sstevel@tonic-gate static int 4080Sstevel@tonic-gate ptmclose(queue_t *rqp, int flag, cred_t *credp) 4090Sstevel@tonic-gate { 4100Sstevel@tonic-gate struct pt_ttys *ptmp; 4110Sstevel@tonic-gate queue_t *pts_rdq; 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate ASSERT(rqp->q_ptr); 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate ptmp = (struct pt_ttys *)rqp->q_ptr; 4160Sstevel@tonic-gate PT_ENTER_READ(ptmp); 4170Sstevel@tonic-gate if (ptmp->pts_rdq) { 4180Sstevel@tonic-gate pts_rdq = ptmp->pts_rdq; 4190Sstevel@tonic-gate if (pts_rdq->q_next) { 4200Sstevel@tonic-gate DBG(("send hangup message to slave\n")); 4210Sstevel@tonic-gate (void) putnextctl(pts_rdq, M_HANGUP); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate PT_EXIT_READ(ptmp); 4250Sstevel@tonic-gate /* 4260Sstevel@tonic-gate * ptm_rdq should be cleared before call to qprocsoff() to prevent pts 4270Sstevel@tonic-gate * write procedure to attempt using ptm_rdq after qprocsoff. 4280Sstevel@tonic-gate */ 4290Sstevel@tonic-gate PT_ENTER_WRITE(ptmp); 4300Sstevel@tonic-gate ptmp->ptm_rdq = NULL; 4310Sstevel@tonic-gate freemsg(ptmp->pt_nullmsg); 4320Sstevel@tonic-gate ptmp->pt_nullmsg = NULL; 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * qenable slave side write queue so that it can flush 4350Sstevel@tonic-gate * its messages as master's read queue is going away 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate if (ptmp->pts_rdq) 4380Sstevel@tonic-gate qenable(WR(ptmp->pts_rdq)); 4390Sstevel@tonic-gate PT_EXIT_WRITE(ptmp); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate qprocsoff(rqp); 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* Finish the close */ 4440Sstevel@tonic-gate rqp->q_ptr = NULL; 4450Sstevel@tonic-gate WR(rqp)->q_ptr = NULL; 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate ptms_close(ptmp, PTMOPEN | PTLOCK); 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate return (0); 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate 4522712Snn35248 static boolean_t 4532712Snn35248 ptmptsopencb(ptmptsopencb_arg_t arg) 4542712Snn35248 { 4552712Snn35248 struct pt_ttys *ptmp = (struct pt_ttys *)arg; 4562712Snn35248 boolean_t rval; 4572712Snn35248 4582712Snn35248 PT_ENTER_READ(ptmp); 4592712Snn35248 rval = (ptmp->pt_nullmsg != NULL); 4602712Snn35248 PT_EXIT_READ(ptmp); 4612712Snn35248 return (rval); 4622712Snn35248 } 4632712Snn35248 4640Sstevel@tonic-gate /* 4650Sstevel@tonic-gate * The wput procedure will only handle ioctl and flush messages. 4660Sstevel@tonic-gate */ 4670Sstevel@tonic-gate static void 4680Sstevel@tonic-gate ptmwput(queue_t *qp, mblk_t *mp) 4690Sstevel@tonic-gate { 4700Sstevel@tonic-gate struct pt_ttys *ptmp; 4710Sstevel@tonic-gate struct iocblk *iocp; 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate DBG(("entering ptmwput\n")); 4740Sstevel@tonic-gate ASSERT(qp->q_ptr); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate ptmp = (struct pt_ttys *)qp->q_ptr; 4770Sstevel@tonic-gate PT_ENTER_READ(ptmp); 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate switch (mp->b_datap->db_type) { 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * if write queue request, flush master's write 4820Sstevel@tonic-gate * queue and send FLUSHR up slave side. If read 4830Sstevel@tonic-gate * queue request, convert to FLUSHW and putnext(). 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate case M_FLUSH: 4860Sstevel@tonic-gate { 4870Sstevel@tonic-gate unsigned char flush_flg = 0; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate DBG(("ptm got flush request\n")); 4900Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 4910Sstevel@tonic-gate DBG(("got FLUSHW, flush ptm write Q\n")); 4920Sstevel@tonic-gate if (*mp->b_rptr & FLUSHBAND) 4930Sstevel@tonic-gate /* 4940Sstevel@tonic-gate * if it is a FLUSHBAND, do flushband. 4950Sstevel@tonic-gate */ 4960Sstevel@tonic-gate flushband(qp, *(mp->b_rptr + 1), 4970Sstevel@tonic-gate FLUSHDATA); 4980Sstevel@tonic-gate else 4990Sstevel@tonic-gate flushq(qp, FLUSHDATA); 5000Sstevel@tonic-gate flush_flg = (*mp->b_rptr & ~FLUSHW) | FLUSHR; 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 5030Sstevel@tonic-gate DBG(("got FLUSHR, set FLUSHW\n")); 5040Sstevel@tonic-gate flush_flg |= (*mp->b_rptr & ~FLUSHR) | FLUSHW; 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate if (flush_flg != 0 && ptmp->pts_rdq && 5070Sstevel@tonic-gate !(ptmp->pt_state & PTLOCK)) { 5080Sstevel@tonic-gate DBG(("putnext to pts\n")); 5090Sstevel@tonic-gate *mp->b_rptr = flush_flg; 5100Sstevel@tonic-gate putnext(ptmp->pts_rdq, mp); 5110Sstevel@tonic-gate } else 5120Sstevel@tonic-gate freemsg(mp); 5130Sstevel@tonic-gate break; 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate case M_IOCTL: 5170Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 5180Sstevel@tonic-gate switch (iocp->ioc_cmd) { 5190Sstevel@tonic-gate default: 5200Sstevel@tonic-gate if ((ptmp->pt_state & PTLOCK) || 5210Sstevel@tonic-gate (ptmp->pts_rdq == NULL)) { 5220Sstevel@tonic-gate DBG(("got M_IOCTL but no slave\n")); 5230Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 5240Sstevel@tonic-gate PT_EXIT_READ(ptmp); 5250Sstevel@tonic-gate return; 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate (void) putq(qp, mp); 5280Sstevel@tonic-gate break; 5290Sstevel@tonic-gate case UNLKPT: 5300Sstevel@tonic-gate mutex_enter(&ptmp->pt_lock); 5310Sstevel@tonic-gate ptmp->pt_state &= ~PTLOCK; 5320Sstevel@tonic-gate mutex_exit(&ptmp->pt_lock); 5330Sstevel@tonic-gate /*FALLTHROUGH*/ 5340Sstevel@tonic-gate case ISPTM: 5350Sstevel@tonic-gate DBG(("ack the UNLKPT/ISPTM\n")); 5360Sstevel@tonic-gate miocack(qp, mp, 0, 0); 5370Sstevel@tonic-gate break; 5380Sstevel@tonic-gate case ZONEPT: 5390Sstevel@tonic-gate { 5400Sstevel@tonic-gate zoneid_t z; 5410Sstevel@tonic-gate int error; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate if ((error = drv_priv(iocp->ioc_cr)) != 0) { 5440Sstevel@tonic-gate miocnak(qp, mp, 0, error); 5450Sstevel@tonic-gate break; 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate if ((error = miocpullup(mp, sizeof (zoneid_t))) != 0) { 5480Sstevel@tonic-gate miocnak(qp, mp, 0, error); 5490Sstevel@tonic-gate break; 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate z = *((zoneid_t *)mp->b_cont->b_rptr); 5520Sstevel@tonic-gate if (z < MIN_ZONEID || z > MAX_ZONEID) { 5530Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 5540Sstevel@tonic-gate break; 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate mutex_enter(&ptmp->pt_lock); 5580Sstevel@tonic-gate ptmp->pt_zoneid = z; 5590Sstevel@tonic-gate mutex_exit(&ptmp->pt_lock); 5600Sstevel@tonic-gate miocack(qp, mp, 0, 0); 5610Sstevel@tonic-gate break; 5620Sstevel@tonic-gate } 5633442Svikram case OWNERPT: 5642621Sllai1 { 5652621Sllai1 pt_own_t *ptop; 5662621Sllai1 int error; 567*5771Sjp151216 zone_t *zone; 5682621Sllai1 5692621Sllai1 if ((error = miocpullup(mp, sizeof (pt_own_t))) != 0) { 5702621Sllai1 miocnak(qp, mp, 0, error); 5712621Sllai1 break; 5722621Sllai1 } 5732621Sllai1 574*5771Sjp151216 zone = zone_find_by_id(ptmp->pt_zoneid); 5752621Sllai1 ptop = (pt_own_t *)mp->b_cont->b_rptr; 5762621Sllai1 577*5771Sjp151216 if (!VALID_UID(ptop->pto_ruid, zone) || 578*5771Sjp151216 !VALID_GID(ptop->pto_rgid, zone)) { 579*5771Sjp151216 zone_rele(zone); 5802621Sllai1 miocnak(qp, mp, 0, EINVAL); 5812621Sllai1 break; 5822621Sllai1 } 583*5771Sjp151216 zone_rele(zone); 5842621Sllai1 mutex_enter(&ptmp->pt_lock); 5852621Sllai1 ptmp->pt_ruid = ptop->pto_ruid; 5862621Sllai1 ptmp->pt_rgid = ptop->pto_rgid; 5872621Sllai1 mutex_exit(&ptmp->pt_lock); 5882621Sllai1 miocack(qp, mp, 0, 0); 5892621Sllai1 break; 5902621Sllai1 } 5912712Snn35248 case PTMPTSOPENCB: 5922712Snn35248 { 5932712Snn35248 mblk_t *dp; /* ioctl reply data */ 5942712Snn35248 ptmptsopencb_t *ppocb; 5952712Snn35248 5962712Snn35248 /* only allow the kernel to invoke this ioctl */ 5972712Snn35248 if (iocp->ioc_cr != kcred) { 5982712Snn35248 miocnak(qp, mp, 0, EINVAL); 5992712Snn35248 break; 6002712Snn35248 } 6012712Snn35248 6022712Snn35248 /* we don't support transparent ioctls */ 6032712Snn35248 ASSERT(iocp->ioc_count != TRANSPARENT); 6042712Snn35248 if (iocp->ioc_count == TRANSPARENT) { 6052712Snn35248 miocnak(qp, mp, 0, EINVAL); 6062712Snn35248 break; 6072712Snn35248 } 6082712Snn35248 6092712Snn35248 /* allocate a response message */ 6102712Snn35248 dp = allocb(sizeof (ptmptsopencb_t), BPRI_MED); 6112712Snn35248 if (dp == NULL) { 6122712Snn35248 miocnak(qp, mp, 0, EAGAIN); 6132712Snn35248 break; 6142712Snn35248 } 6152712Snn35248 6162712Snn35248 /* initialize the ioctl results */ 6172712Snn35248 ppocb = (ptmptsopencb_t *)dp->b_rptr; 6182712Snn35248 ppocb->ppocb_func = ptmptsopencb; 6192712Snn35248 ppocb->ppocb_arg = (ptmptsopencb_arg_t)ptmp; 6202712Snn35248 6212712Snn35248 /* send the reply data */ 6222712Snn35248 mioc2ack(mp, dp, sizeof (ptmptsopencb_t), 0); 6232712Snn35248 qreply(qp, mp); 6242712Snn35248 break; 6252712Snn35248 } 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate break; 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate case M_READ: 6300Sstevel@tonic-gate /* Caused by ldterm - can not pass to slave */ 6310Sstevel@tonic-gate freemsg(mp); 6320Sstevel@tonic-gate break; 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate /* 6350Sstevel@tonic-gate * send other messages to slave 6360Sstevel@tonic-gate */ 6370Sstevel@tonic-gate default: 6380Sstevel@tonic-gate if ((ptmp->pt_state & PTLOCK) || (ptmp->pts_rdq == NULL)) { 6390Sstevel@tonic-gate DBG(("got msg. but no slave\n")); 6400Sstevel@tonic-gate mp = mexchange(NULL, mp, 2, M_ERROR, -1); 6410Sstevel@tonic-gate if (mp != NULL) { 6420Sstevel@tonic-gate mp->b_rptr[0] = NOERROR; 6430Sstevel@tonic-gate mp->b_rptr[1] = EINVAL; 6440Sstevel@tonic-gate qreply(qp, mp); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate PT_EXIT_READ(ptmp); 6470Sstevel@tonic-gate return; 6480Sstevel@tonic-gate } 6490Sstevel@tonic-gate DBG(("put msg on master's write queue\n")); 6500Sstevel@tonic-gate (void) putq(qp, mp); 6510Sstevel@tonic-gate break; 6520Sstevel@tonic-gate } 6530Sstevel@tonic-gate DBG(("return from ptmwput()\n")); 6540Sstevel@tonic-gate PT_EXIT_READ(ptmp); 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /* 6590Sstevel@tonic-gate * enable the write side of the slave. This triggers the 6600Sstevel@tonic-gate * slave to send any messages queued on its write side to 6610Sstevel@tonic-gate * the read side of this master. 6620Sstevel@tonic-gate */ 6630Sstevel@tonic-gate static void 6640Sstevel@tonic-gate ptmrsrv(queue_t *qp) 6650Sstevel@tonic-gate { 6660Sstevel@tonic-gate struct pt_ttys *ptmp; 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate DBG(("entering ptmrsrv\n")); 6690Sstevel@tonic-gate ASSERT(qp->q_ptr); 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate ptmp = (struct pt_ttys *)qp->q_ptr; 6720Sstevel@tonic-gate PT_ENTER_READ(ptmp); 6730Sstevel@tonic-gate if (ptmp->pts_rdq) { 6740Sstevel@tonic-gate qenable(WR(ptmp->pts_rdq)); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate PT_EXIT_READ(ptmp); 6770Sstevel@tonic-gate DBG(("leaving ptmrsrv\n")); 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate /* 6820Sstevel@tonic-gate * If there are messages on this queue that can be sent to 6830Sstevel@tonic-gate * slave, send them via putnext(). Else, if queued messages 6840Sstevel@tonic-gate * cannot be sent, leave them on this queue. If priority 6850Sstevel@tonic-gate * messages on this queue, send them to slave no matter what. 6860Sstevel@tonic-gate */ 6870Sstevel@tonic-gate static void 6880Sstevel@tonic-gate ptmwsrv(queue_t *qp) 6890Sstevel@tonic-gate { 6900Sstevel@tonic-gate struct pt_ttys *ptmp; 6910Sstevel@tonic-gate mblk_t *mp; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate DBG(("entering ptmwsrv\n")); 6940Sstevel@tonic-gate ASSERT(qp->q_ptr); 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate ptmp = (struct pt_ttys *)qp->q_ptr; 6972712Snn35248 6982712Snn35248 if ((mp = getq(qp)) == NULL) { 6992712Snn35248 /* If there are no messages there's nothing to do. */ 7002712Snn35248 DBG(("leaving ptmwsrv (no messages)\n")); 7012712Snn35248 return; 7022712Snn35248 } 7032712Snn35248 7040Sstevel@tonic-gate PT_ENTER_READ(ptmp); 7050Sstevel@tonic-gate if ((ptmp->pt_state & PTLOCK) || (ptmp->pts_rdq == NULL)) { 7060Sstevel@tonic-gate DBG(("in master write srv proc but no slave\n")); 7070Sstevel@tonic-gate /* 7080Sstevel@tonic-gate * Free messages on the write queue and send 7090Sstevel@tonic-gate * NAK for any M_IOCTL type messages to wakeup 7100Sstevel@tonic-gate * the user process waiting for ACK/NAK from 7110Sstevel@tonic-gate * the ioctl invocation 7120Sstevel@tonic-gate */ 7132712Snn35248 do { 7140Sstevel@tonic-gate if (mp->b_datap->db_type == M_IOCTL) 7150Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 7160Sstevel@tonic-gate else 7170Sstevel@tonic-gate freemsg(mp); 7182712Snn35248 } while ((mp = getq(qp)) != NULL); 7190Sstevel@tonic-gate flushq(qp, FLUSHALL); 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate mp = mexchange(NULL, NULL, 2, M_ERROR, -1); 7220Sstevel@tonic-gate if (mp != NULL) { 7230Sstevel@tonic-gate mp->b_rptr[0] = NOERROR; 7240Sstevel@tonic-gate mp->b_rptr[1] = EINVAL; 7250Sstevel@tonic-gate qreply(qp, mp); 7260Sstevel@tonic-gate } 7270Sstevel@tonic-gate PT_EXIT_READ(ptmp); 7280Sstevel@tonic-gate return; 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate /* 7310Sstevel@tonic-gate * while there are messages on this write queue... 7320Sstevel@tonic-gate */ 7332712Snn35248 do { 7340Sstevel@tonic-gate /* 7350Sstevel@tonic-gate * if don't have control message and cannot put 7360Sstevel@tonic-gate * msg. on slave's read queue, put it back on 7370Sstevel@tonic-gate * this queue. 7380Sstevel@tonic-gate */ 7390Sstevel@tonic-gate if (mp->b_datap->db_type <= QPCTL && 7400Sstevel@tonic-gate !bcanputnext(ptmp->pts_rdq, mp->b_band)) { 7410Sstevel@tonic-gate DBG(("put msg. back on queue\n")); 7420Sstevel@tonic-gate (void) putbq(qp, mp); 7430Sstevel@tonic-gate break; 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate /* 7460Sstevel@tonic-gate * else send the message up slave's stream 7470Sstevel@tonic-gate */ 7480Sstevel@tonic-gate DBG(("send message to slave\n")); 7490Sstevel@tonic-gate putnext(ptmp->pts_rdq, mp); 7502712Snn35248 } while ((mp = getq(qp)) != NULL); 7510Sstevel@tonic-gate DBG(("leaving ptmwsrv\n")); 7520Sstevel@tonic-gate PT_EXIT_READ(ptmp); 7530Sstevel@tonic-gate } 754