xref: /onnv-gate/usr/src/uts/common/io/tty_pts.c (revision 8002:a17a6053f76c)
10Sstevel@tonic-gate /*
27656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
80Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
90Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
100Sstevel@tonic-gate  */
110Sstevel@tonic-gate 
120Sstevel@tonic-gate /*
130Sstevel@tonic-gate  * PTY - Stream "pseudo-tty" device.
140Sstevel@tonic-gate  * This is the "slave" side.
150Sstevel@tonic-gate  */
160Sstevel@tonic-gate 
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #include <sys/param.h>
190Sstevel@tonic-gate #include <sys/systm.h>
200Sstevel@tonic-gate #include <sys/filio.h>
210Sstevel@tonic-gate #include <sys/ioccom.h>
220Sstevel@tonic-gate #include <sys/termios.h>
230Sstevel@tonic-gate #include <sys/termio.h>
240Sstevel@tonic-gate #include <sys/ttold.h>
250Sstevel@tonic-gate #include <sys/stropts.h>
260Sstevel@tonic-gate #include <sys/stream.h>
270Sstevel@tonic-gate #include <sys/strsun.h>
280Sstevel@tonic-gate #include <sys/tty.h>
290Sstevel@tonic-gate #include <sys/user.h>
300Sstevel@tonic-gate #include <sys/conf.h>
310Sstevel@tonic-gate #include <sys/file.h>
320Sstevel@tonic-gate #include <sys/vnode.h>	/* 1/0 on the vomit meter */
330Sstevel@tonic-gate #include <sys/proc.h>
340Sstevel@tonic-gate #include <sys/uio.h>
350Sstevel@tonic-gate #include <sys/errno.h>
360Sstevel@tonic-gate #include <sys/strsubr.h>
370Sstevel@tonic-gate #include <sys/poll.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/procset.h>
410Sstevel@tonic-gate #include <sys/cred.h>
420Sstevel@tonic-gate #include <sys/ptyvar.h>
430Sstevel@tonic-gate #include <sys/suntty.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <sys/policy.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/conf.h>
480Sstevel@tonic-gate #include <sys/ddi.h>
490Sstevel@tonic-gate #include <sys/sunddi.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate extern void gsignal(int pid, int sig);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate extern	int npty;	/* number of pseudo-ttys configured in */
540Sstevel@tonic-gate extern struct pty *pty_softc;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate extern struct pollhead	ptcph;	/* poll head for ptcpoll() use */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	IFLAGS	(CS7|CREAD|PARENB)
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Most of these should be "void", but the people who defined the "streams"
630Sstevel@tonic-gate  * data structure for S5 didn't understand data types.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Slave side.  This is a streams device.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate static int ptslopen(queue_t *, dev_t *, int flag, int, cred_t *);
700Sstevel@tonic-gate static int ptslclose(queue_t *, int, cred_t *);
710Sstevel@tonic-gate static int ptslrserv(queue_t *);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * To save instructions, since STREAMS ignores the return value
750Sstevel@tonic-gate  * from this function, it is defined as void here. Kind of icky, but...
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static void ptslwput(queue_t *q, mblk_t *mp);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static struct module_info ptslm_info = {
810Sstevel@tonic-gate 	0,
820Sstevel@tonic-gate 	"ptys",
830Sstevel@tonic-gate 	0,
840Sstevel@tonic-gate 	INFPSZ,
850Sstevel@tonic-gate 	2048,
860Sstevel@tonic-gate 	200
870Sstevel@tonic-gate };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static struct qinit ptslrinit = {
900Sstevel@tonic-gate 	putq,
910Sstevel@tonic-gate 	ptslrserv,
920Sstevel@tonic-gate 	ptslopen,
930Sstevel@tonic-gate 	ptslclose,
940Sstevel@tonic-gate 	NULL,
950Sstevel@tonic-gate 	&ptslm_info,
960Sstevel@tonic-gate 	NULL
970Sstevel@tonic-gate };
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static struct qinit ptslwinit = {
1000Sstevel@tonic-gate 	(int (*)())ptslwput,
1010Sstevel@tonic-gate 	NULL,
1020Sstevel@tonic-gate 	NULL,
1030Sstevel@tonic-gate 	NULL,
1040Sstevel@tonic-gate 	NULL,
1050Sstevel@tonic-gate 	&ptslm_info,
1060Sstevel@tonic-gate 	NULL
1070Sstevel@tonic-gate };
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate struct	streamtab ptysinfo = {
1100Sstevel@tonic-gate 	&ptslrinit,
1110Sstevel@tonic-gate 	&ptslwinit,
1120Sstevel@tonic-gate 	NULL,
1130Sstevel@tonic-gate 	NULL
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static void	ptslreioctl(void *);
1170Sstevel@tonic-gate static void	ptslioctl(struct pty *, queue_t *, mblk_t *);
1180Sstevel@tonic-gate static void	pt_sendstop(struct pty *);
1190Sstevel@tonic-gate static void	ptcpollwakeup(struct pty *, int);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static int ptsl_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
1230Sstevel@tonic-gate static int ptsl_attach(dev_info_t *, ddi_attach_cmd_t);
1240Sstevel@tonic-gate static dev_info_t *ptsl_dip;	/* for dev-to-dip conversions */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(ptsl_ops, nulldev, nulldev,
1277656SSherry.Moore@Sun.COM     ptsl_attach, nodev, nodev, ptsl_info, D_MP, &ptysinfo,
1287656SSherry.Moore@Sun.COM     ddi_quiesce_not_supported);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #include <sys/types.h>
1310Sstevel@tonic-gate #include <sys/conf.h>
1320Sstevel@tonic-gate #include <sys/param.h>
1330Sstevel@tonic-gate #include <sys/systm.h>
1340Sstevel@tonic-gate #include <sys/errno.h>
1350Sstevel@tonic-gate #include <sys/modctl.h>
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate char _depends_on[] = "drv/ptc";
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * Module linkage information for the kernel.
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static struct modldrv modldrv = {
1440Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
1457656SSherry.Moore@Sun.COM 	"tty pseudo driver slave 'ptsl'",
1460Sstevel@tonic-gate 	&ptsl_ops,	/* driver ops */
1470Sstevel@tonic-gate };
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static struct modlinkage modlinkage = {
1500Sstevel@tonic-gate 	MODREV_1,
1510Sstevel@tonic-gate 	&modldrv,
1520Sstevel@tonic-gate 	NULL
1530Sstevel@tonic-gate };
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate int
_init(void)1560Sstevel@tonic-gate _init(void)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate int
_fini(void)1620Sstevel@tonic-gate _fini(void)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1680Sstevel@tonic-gate _info(struct modinfo *modinfop)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static char	*tty_banks = PTY_BANKS;
1740Sstevel@tonic-gate static char	*tty_digits = PTY_DIGITS;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /* ARGSUSED */
1770Sstevel@tonic-gate static int
ptsl_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1780Sstevel@tonic-gate ptsl_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	char	name[8];
1810Sstevel@tonic-gate 	int	tty_num;
1820Sstevel@tonic-gate 	char	*tty_digit = tty_digits;
1830Sstevel@tonic-gate 	char	*tty_bank = tty_banks;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	for (tty_num = 0; tty_num < npty; tty_num++) {
1860Sstevel@tonic-gate 		(void) sprintf(name, "tty%c%c", *tty_bank, *tty_digit);
1870Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, name, S_IFCHR,
1887656SSherry.Moore@Sun.COM 		    tty_num, DDI_PSEUDO, NULL) == DDI_FAILURE) {
1890Sstevel@tonic-gate 			ddi_remove_minor_node(devi, NULL);
1900Sstevel@tonic-gate 			return (-1);
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 		if (*(++tty_digit) == '\0') {
1930Sstevel@tonic-gate 			tty_digit = tty_digits;
1940Sstevel@tonic-gate 			if (*(++tty_bank) == '\0')
1950Sstevel@tonic-gate 				break;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	ptsl_dip = devi;
1990Sstevel@tonic-gate 	return (DDI_SUCCESS);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /* ARGSUSED */
2030Sstevel@tonic-gate static int
ptsl_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2040Sstevel@tonic-gate ptsl_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2050Sstevel@tonic-gate     void **result)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	int error;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	switch (infocmd) {
2100Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2110Sstevel@tonic-gate 		if (ptsl_dip == NULL) {
2120Sstevel@tonic-gate 			error = DDI_FAILURE;
2130Sstevel@tonic-gate 		} else {
2140Sstevel@tonic-gate 			*result = (void *)ptsl_dip;
2150Sstevel@tonic-gate 			error = DDI_SUCCESS;
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 		break;
2180Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2190Sstevel@tonic-gate 		*result = (void *)0;
2200Sstevel@tonic-gate 		error = DDI_SUCCESS;
2210Sstevel@tonic-gate 		break;
2220Sstevel@tonic-gate 	default:
2230Sstevel@tonic-gate 		error = DDI_FAILURE;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	return (error);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * Open the slave side of a pty.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate /*ARGSUSED*/
2330Sstevel@tonic-gate static int
ptslopen(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * cred)2340Sstevel@tonic-gate ptslopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *cred)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	minor_t unit;
2370Sstevel@tonic-gate 	dev_t dev = *devp;
2380Sstevel@tonic-gate 	struct pty *pty;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	unit = getminor(dev);
2410Sstevel@tonic-gate 	if (unit >= npty)
2420Sstevel@tonic-gate 		return (ENXIO);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	pty = &pty_softc[unit];
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
2470Sstevel@tonic-gate 	/*
2480Sstevel@tonic-gate 	 * Block waiting for controller to open, unless this is a no-delay
2490Sstevel@tonic-gate 	 * open.
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate again:
2520Sstevel@tonic-gate 	if (pty->pt_ttycommon.t_writeq == NULL) {
2530Sstevel@tonic-gate 		pty->pt_ttycommon.t_iflag = 0;
2540Sstevel@tonic-gate 		pty->pt_ttycommon.t_cflag = (B38400 << IBSHIFT)|B38400|IFLAGS;
2550Sstevel@tonic-gate 		pty->pt_ttycommon.t_iocpending = NULL;
2560Sstevel@tonic-gate 		pty->pt_wbufcid = 0;
2570Sstevel@tonic-gate 		pty->pt_ttycommon.t_size.ws_row = 0;
2580Sstevel@tonic-gate 		pty->pt_ttycommon.t_size.ws_col = 0;
2590Sstevel@tonic-gate 		pty->pt_ttycommon.t_size.ws_xpixel = 0;
2600Sstevel@tonic-gate 		pty->pt_ttycommon.t_size.ws_ypixel = 0;
2610Sstevel@tonic-gate 	} else if ((pty->pt_ttycommon.t_flags & TS_XCLUDE) &&
2620Sstevel@tonic-gate 	    secpolicy_excl_open(cred) != 0) {
2630Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
2640Sstevel@tonic-gate 		return (EBUSY);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 	if (!(flag & (FNONBLOCK|FNDELAY)) &&
2670Sstevel@tonic-gate 	    !(pty->pt_ttycommon.t_cflag & CLOCAL)) {
2680Sstevel@tonic-gate 		if (!(pty->pt_flags & PF_CARR_ON)) {
2690Sstevel@tonic-gate 			pty->pt_flags |= PF_WOPEN;
2700Sstevel@tonic-gate 			if (!cv_wait_sig(&pty->pt_cv_flags, &pty->ptc_lock)) {
2710Sstevel@tonic-gate 				pty->pt_flags &= ~PF_WOPEN;
2720Sstevel@tonic-gate 				mutex_exit(&pty->ptc_lock);
2730Sstevel@tonic-gate 				return (EINTR);
2740Sstevel@tonic-gate 			}
2750Sstevel@tonic-gate 			goto again;
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	pty->pt_sdev = dev;
2800Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = pty;
2810Sstevel@tonic-gate 	pty->pt_flags &= ~PF_SLAVEGONE;
2820Sstevel@tonic-gate 	pty->pt_ttycommon.t_readq = pty->pt_ttycommon.t_writeq = NULL;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/*
2850Sstevel@tonic-gate 	 * Slave is ready to accept messages but master still can't send
2860Sstevel@tonic-gate 	 * messages to the slave queue since it is not plumbed
2870Sstevel@tonic-gate 	 * yet. So do qprocson() and finish slave initialization.
2880Sstevel@tonic-gate 	 */
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	qprocson(q);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/*
2950Sstevel@tonic-gate 	 * Now it is safe to send messages to q, so wakeup master possibly
2960Sstevel@tonic-gate 	 * waiting for slave queue to finish open.
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
299*8002Sanil.udupa@sun.com 	/*
300*8002Sanil.udupa@sun.com 	 * queue has already been setup with a pointer to
301*8002Sanil.udupa@sun.com 	 * the stream head that is being referenced
302*8002Sanil.udupa@sun.com 	 */
303*8002Sanil.udupa@sun.com 	pty->pt_vnode = strq2vp(q);
304*8002Sanil.udupa@sun.com 	VN_RELE(pty->pt_vnode);
3050Sstevel@tonic-gate 	pty->pt_ttycommon.t_readq = q;
3060Sstevel@tonic-gate 	pty->pt_ttycommon.t_writeq = WR(q);
3070Sstevel@tonic-gate 	/* tell master device that slave is ready for writing */
3080Sstevel@tonic-gate 	if (pty->pt_flags & PF_CARR_ON)
3090Sstevel@tonic-gate 		cv_broadcast(&pty->pt_cv_readq);
3100Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	return (0);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static int
ptslclose(queue_t * q,int flag,cred_t * cred)3160Sstevel@tonic-gate ptslclose(queue_t *q, int flag, cred_t *cred)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	struct pty *pty;
3190Sstevel@tonic-gate 	bufcall_id_t pt_wbufcid = 0;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate #ifdef lint
3220Sstevel@tonic-gate 	flag = flag;
3230Sstevel@tonic-gate 	cred = cred;
3240Sstevel@tonic-gate #endif
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if ((pty = (struct pty *)q->q_ptr) == NULL)
3270Sstevel@tonic-gate 		return (ENODEV);	/* already been closed once */
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/*
3300Sstevel@tonic-gate 	 * Prevent the queues from being uses by master device.
3310Sstevel@tonic-gate 	 * This should be done before qprocsoff or writer may attempt
3320Sstevel@tonic-gate 	 * to use the slave queue after qprocsoff removed it from the stream and
3330Sstevel@tonic-gate 	 * before entering mutex_enter().
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
3360Sstevel@tonic-gate 	pty->pt_ttycommon.t_readq = NULL;
3370Sstevel@tonic-gate 	pty->pt_ttycommon.t_writeq = NULL;
338*8002Sanil.udupa@sun.com 	while (pty->pt_flags & PF_IOCTL) {
339*8002Sanil.udupa@sun.com 		pty->pt_flags |= PF_WAIT;
340*8002Sanil.udupa@sun.com 		cv_wait(&pty->pt_cv_flags, &pty->ptc_lock);
341*8002Sanil.udupa@sun.com 	}
342*8002Sanil.udupa@sun.com 	pty->pt_vnode = NULL;
3430Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	qprocsoff(q);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
3480Sstevel@tonic-gate 	/*
3490Sstevel@tonic-gate 	 * ptc_lock mutex is not dropped across
3500Sstevel@tonic-gate 	 * the call to the routine ttycommon_close
3510Sstevel@tonic-gate 	 */
3520Sstevel@tonic-gate 	ttycommon_close(&pty->pt_ttycommon);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * Cancel outstanding "bufcall" request.
3560Sstevel@tonic-gate 	 */
3570Sstevel@tonic-gate 	if (pty->pt_wbufcid) {
3580Sstevel@tonic-gate 		pt_wbufcid = pty->pt_wbufcid;
3590Sstevel@tonic-gate 		pty->pt_wbufcid = 0;
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/*
3630Sstevel@tonic-gate 	 * Clear out all the slave-side state.
3640Sstevel@tonic-gate 	 */
3650Sstevel@tonic-gate 	pty->pt_flags &= ~(PF_WOPEN|PF_STOPPED|PF_NOSTOP);
3660Sstevel@tonic-gate 	if (pty->pt_flags & PF_CARR_ON) {
3670Sstevel@tonic-gate 		pty->pt_flags |= PF_SLAVEGONE;	/* let the controller know */
3680Sstevel@tonic-gate 		ptcpollwakeup(pty, 0);	/* wake up readers/selectors */
3690Sstevel@tonic-gate 		ptcpollwakeup(pty, FWRITE);	/* wake up writers/selectors */
3700Sstevel@tonic-gate 		cv_broadcast(&pty->pt_cv_flags);
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 	pty->pt_sdev = 0;
3730Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
3740Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	if (pt_wbufcid)
3770Sstevel@tonic-gate 		unbufcall(pt_wbufcid);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	return (0);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate  * Put procedure for write queue.
3840Sstevel@tonic-gate  * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
3850Sstevel@tonic-gate  * queue up M_DATA messages for processing by the controller "read"
3860Sstevel@tonic-gate  * routine; discard everything else.
3870Sstevel@tonic-gate  */
3880Sstevel@tonic-gate static void
ptslwput(queue_t * q,mblk_t * mp)3890Sstevel@tonic-gate ptslwput(queue_t *q, mblk_t *mp)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate 	struct pty *pty;
3920Sstevel@tonic-gate 	mblk_t *bp;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	pty = (struct pty *)q->q_ptr;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	case M_STOP:
4010Sstevel@tonic-gate 		if (!(pty->pt_flags & PF_STOPPED)) {
4020Sstevel@tonic-gate 			pty->pt_flags |= PF_STOPPED;
4030Sstevel@tonic-gate 			pty->pt_send |= TIOCPKT_STOP;
4040Sstevel@tonic-gate 			ptcpollwakeup(pty, 0);
4050Sstevel@tonic-gate 		}
4060Sstevel@tonic-gate 		freemsg(mp);
4070Sstevel@tonic-gate 		break;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	case M_START:
4100Sstevel@tonic-gate 		if (pty->pt_flags & PF_STOPPED) {
4110Sstevel@tonic-gate 			pty->pt_flags &= ~PF_STOPPED;
4120Sstevel@tonic-gate 			pty->pt_send = TIOCPKT_START;
4130Sstevel@tonic-gate 			ptcpollwakeup(pty, 0);
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 		ptcpollwakeup(pty, FREAD);	/* permit controller to read */
4160Sstevel@tonic-gate 		freemsg(mp);
4170Sstevel@tonic-gate 		break;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	case M_IOCTL:
4200Sstevel@tonic-gate 		ptslioctl(pty, q, mp);
4210Sstevel@tonic-gate 		break;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	case M_FLUSH:
4240Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
4250Sstevel@tonic-gate 			/*
4260Sstevel@tonic-gate 			 * Set the "flush write" flag, so that we
4270Sstevel@tonic-gate 			 * notify the controller if they're in packet
4280Sstevel@tonic-gate 			 * or user control mode.
4290Sstevel@tonic-gate 			 */
4300Sstevel@tonic-gate 			if (!(pty->pt_send & TIOCPKT_FLUSHWRITE)) {
4310Sstevel@tonic-gate 				pty->pt_send |= TIOCPKT_FLUSHWRITE;
4320Sstevel@tonic-gate 				ptcpollwakeup(pty, 0);
4330Sstevel@tonic-gate 			}
4340Sstevel@tonic-gate 			/*
4350Sstevel@tonic-gate 			 * Flush our write queue.
4360Sstevel@tonic-gate 			 */
4370Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
4380Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;	/* it has been flushed */
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
4410Sstevel@tonic-gate 			/*
4420Sstevel@tonic-gate 			 * Set the "flush read" flag, so that we
4430Sstevel@tonic-gate 			 * notify the controller if they're in packet
4440Sstevel@tonic-gate 			 * mode.
4450Sstevel@tonic-gate 			 */
4460Sstevel@tonic-gate 			if (!(pty->pt_send & TIOCPKT_FLUSHREAD)) {
4470Sstevel@tonic-gate 				pty->pt_send |= TIOCPKT_FLUSHREAD;
4480Sstevel@tonic-gate 				ptcpollwakeup(pty, 0);
4490Sstevel@tonic-gate 			}
4500Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
4510Sstevel@tonic-gate 			mutex_exit(&pty->ptc_lock);
4520Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
4530Sstevel@tonic-gate 			return;
4540Sstevel@tonic-gate 		} else
4550Sstevel@tonic-gate 			freemsg(mp);
4560Sstevel@tonic-gate 		break;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	case M_DATA:
4590Sstevel@tonic-gate 		/*
4600Sstevel@tonic-gate 		 * Throw away any leading zero-length blocks, and queue it up
4610Sstevel@tonic-gate 		 * for the controller to read.
4620Sstevel@tonic-gate 		 */
4630Sstevel@tonic-gate 		if (pty->pt_flags & PF_CARR_ON) {
4640Sstevel@tonic-gate 			bp = mp;
4650Sstevel@tonic-gate 			while ((bp->b_wptr - bp->b_rptr) == 0) {
4660Sstevel@tonic-gate 				mp = bp->b_cont;
4670Sstevel@tonic-gate 				freeb(bp);
4680Sstevel@tonic-gate 				if (mp == NULL) {
4690Sstevel@tonic-gate 					mutex_exit(&pty->ptc_lock);
4700Sstevel@tonic-gate 					return;	/* damp squib of a message */
4710Sstevel@tonic-gate 				}
4720Sstevel@tonic-gate 				bp = mp;
4730Sstevel@tonic-gate 			}
4740Sstevel@tonic-gate 			(void) putq(q, mp);
4750Sstevel@tonic-gate 			ptcpollwakeup(pty, FREAD);	/* soup's on! */
4760Sstevel@tonic-gate 		} else
4770Sstevel@tonic-gate 			freemsg(mp);	/* nobody listening */
4780Sstevel@tonic-gate 		break;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	case M_CTL:
4810Sstevel@tonic-gate 		if ((*(int *)mp->b_rptr) == MC_CANONQUERY) {
4820Sstevel@tonic-gate 			/*
4830Sstevel@tonic-gate 			 * We're being asked whether we do canonicalization
4840Sstevel@tonic-gate 			 * or not.  Send a reply back up indicating whether
4850Sstevel@tonic-gate 			 * we do or not.
4860Sstevel@tonic-gate 			 */
4870Sstevel@tonic-gate 			(void) putctl1(RD(q), M_CTL,
4880Sstevel@tonic-gate 			    (pty->pt_flags & PF_REMOTE) ?
4897656SSherry.Moore@Sun.COM 			    MC_NOCANON : MC_DOCANON);
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 		freemsg(mp);
4920Sstevel@tonic-gate 		break;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	default:
4950Sstevel@tonic-gate 		/*
4960Sstevel@tonic-gate 		 * "No, I don't want a subscription to Chain Store Age,
4970Sstevel@tonic-gate 		 * thank you anyway."
4980Sstevel@tonic-gate 		 */
4990Sstevel@tonic-gate 		freemsg(mp);
5000Sstevel@tonic-gate 		break;
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate  * Retry an "ioctl", now that "bufcall" claims we may be able to allocate
5070Sstevel@tonic-gate  * the buffer we need.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static void
ptslreioctl(void * arg)5100Sstevel@tonic-gate ptslreioctl(void *arg)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	struct pty *pty = arg;
5130Sstevel@tonic-gate 	queue_t *q;
5140Sstevel@tonic-gate 	mblk_t *mp;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
5170Sstevel@tonic-gate 	/*
5180Sstevel@tonic-gate 	 * The bufcall is no longer pending.
5190Sstevel@tonic-gate 	 */
5200Sstevel@tonic-gate 	if (pty->pt_wbufcid == 0) {
5210Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
5220Sstevel@tonic-gate 		return;
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	pty->pt_wbufcid = 0;
5260Sstevel@tonic-gate 	if ((q = pty->pt_ttycommon.t_writeq) == NULL) {
5270Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
5280Sstevel@tonic-gate 		return;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 	if ((mp = pty->pt_ttycommon.t_iocpending) != NULL) {
5310Sstevel@tonic-gate 		/* It's not pending any more. */
5320Sstevel@tonic-gate 		pty->pt_ttycommon.t_iocpending = NULL;
5330Sstevel@tonic-gate 		ptslioctl(pty, q, mp);
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
5400Sstevel@tonic-gate  * Drops pty's ptc_lock mutex and then reacquire
5410Sstevel@tonic-gate  */
5420Sstevel@tonic-gate static void
ptslioctl(struct pty * pty,queue_t * q,mblk_t * mp)5430Sstevel@tonic-gate ptslioctl(struct pty *pty, queue_t *q, mblk_t *mp)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	struct iocblk *iocp;
5460Sstevel@tonic-gate 	int cmd;
5470Sstevel@tonic-gate 	size_t datasize;
5480Sstevel@tonic-gate 	int error = 0;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pty->ptc_lock));
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
5530Sstevel@tonic-gate 	cmd = iocp->ioc_cmd;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	switch (cmd) {
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	case TIOCSTI: {
5580Sstevel@tonic-gate 		/*
5590Sstevel@tonic-gate 		 * The permission checking has already been done at the stream
5600Sstevel@tonic-gate 		 * head, since it has to be done in the context of the process
5610Sstevel@tonic-gate 		 * doing the call.
5620Sstevel@tonic-gate 		 */
5630Sstevel@tonic-gate 		mblk_t *bp;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (char));
5660Sstevel@tonic-gate 		if (error != 0)
5670Sstevel@tonic-gate 			goto out;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		/*
5700Sstevel@tonic-gate 		 * Simulate typing of a character at the terminal.
5710Sstevel@tonic-gate 		 */
5720Sstevel@tonic-gate 		if ((bp = allocb(1, BPRI_MED)) != NULL) {
5730Sstevel@tonic-gate 			*bp->b_wptr++ = *mp->b_cont->b_rptr;
5740Sstevel@tonic-gate 			if (!(pty->pt_flags & PF_REMOTE)) {
5750Sstevel@tonic-gate 				if (!canput(pty->pt_ttycommon.t_readq)) {
5760Sstevel@tonic-gate 					mutex_exit(&pty->ptc_lock);
5770Sstevel@tonic-gate 					ttycommon_qfull(&pty->pt_ttycommon, q);
5780Sstevel@tonic-gate 					mutex_enter(&pty->ptc_lock);
5790Sstevel@tonic-gate 					freemsg(bp);
5800Sstevel@tonic-gate 					error = EAGAIN;
5810Sstevel@tonic-gate 					goto out;
5820Sstevel@tonic-gate 				} else
5830Sstevel@tonic-gate 					(void) putq(
5840Sstevel@tonic-gate 					    pty->pt_ttycommon.t_readq, bp);
5850Sstevel@tonic-gate 			} else {
5860Sstevel@tonic-gate 				if (pty->pt_flags & PF_UCNTL) {
5870Sstevel@tonic-gate 					/*
5880Sstevel@tonic-gate 					 * XXX - flow control; don't overflow
5890Sstevel@tonic-gate 					 * this "queue".
5900Sstevel@tonic-gate 					 */
5910Sstevel@tonic-gate 					if (pty->pt_stuffqfirst != NULL) {
5920Sstevel@tonic-gate 						pty->pt_stuffqlast->b_next = bp;
5930Sstevel@tonic-gate 						bp->b_prev = pty->pt_stuffqlast;
5940Sstevel@tonic-gate 					} else {
5950Sstevel@tonic-gate 						pty->pt_stuffqfirst = bp;
5960Sstevel@tonic-gate 						bp->b_prev = NULL;
5970Sstevel@tonic-gate 					}
5980Sstevel@tonic-gate 					bp->b_next = NULL;
5990Sstevel@tonic-gate 					pty->pt_stuffqlast = bp;
6000Sstevel@tonic-gate 					pty->pt_stuffqlen++;
6010Sstevel@tonic-gate 					ptcpollwakeup(pty, 0);
6020Sstevel@tonic-gate 				}
6030Sstevel@tonic-gate 			}
6040Sstevel@tonic-gate 		} else {
6050Sstevel@tonic-gate 			error = EAGAIN;
6060Sstevel@tonic-gate 			goto out;
6070Sstevel@tonic-gate 		}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 		/*
6100Sstevel@tonic-gate 		 * Turn the ioctl message into an ioctl ACK message.
6110Sstevel@tonic-gate 		 */
6120Sstevel@tonic-gate 		iocp->ioc_count = 0;	/* no data returned */
6130Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
6140Sstevel@tonic-gate 		goto out;
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	case TIOCSSIZE: {
6180Sstevel@tonic-gate 		tty_common_t *tc = &pty->pt_ttycommon;
6190Sstevel@tonic-gate 		struct ttysize *tp;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct ttysize));
6220Sstevel@tonic-gate 		if (error != 0)
6230Sstevel@tonic-gate 			goto out;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * Set the window size, but don't send a SIGWINCH.
6270Sstevel@tonic-gate 		 */
6280Sstevel@tonic-gate 		tp = (struct ttysize *)mp->b_cont->b_rptr;
6290Sstevel@tonic-gate 		tc->t_size.ws_row = tp->ts_lines;
6300Sstevel@tonic-gate 		tc->t_size.ws_col = tp->ts_cols;
6310Sstevel@tonic-gate 		tc->t_size.ws_xpixel = 0;
6320Sstevel@tonic-gate 		tc->t_size.ws_ypixel = 0;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 		/*
6350Sstevel@tonic-gate 		 * Send an ACK back.
6360Sstevel@tonic-gate 		 */
6370Sstevel@tonic-gate 		iocp->ioc_count = 0;	/* no data returned */
6380Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
6390Sstevel@tonic-gate 		goto out;
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	case TIOCGSIZE: {
6430Sstevel@tonic-gate 		tty_common_t *tc = &pty->pt_ttycommon;
6440Sstevel@tonic-gate 		mblk_t *datap;
6450Sstevel@tonic-gate 		struct ttysize *tp;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 		if ((datap = allocb(sizeof (struct ttysize),
6480Sstevel@tonic-gate 		    BPRI_HI)) == NULL) {
6490Sstevel@tonic-gate 			if (pty->pt_wbufcid) {
6500Sstevel@tonic-gate 				if (pty->pt_ttycommon.t_iocpending)
6510Sstevel@tonic-gate 					freemsg(pty->pt_ttycommon.t_iocpending);
6520Sstevel@tonic-gate 				pty->pt_ttycommon.t_iocpending = mp;
6530Sstevel@tonic-gate 				return;
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 			pty->pt_wbufcid = bufcall(sizeof (struct ttysize),
6560Sstevel@tonic-gate 			    BPRI_HI, ptslreioctl, pty);
6570Sstevel@tonic-gate 			if (pty->pt_wbufcid == 0) {
6580Sstevel@tonic-gate 				error = ENOMEM;
6590Sstevel@tonic-gate 				goto out;
6600Sstevel@tonic-gate 			}
6610Sstevel@tonic-gate 			pty->pt_ttycommon.t_iocpending = mp;
6620Sstevel@tonic-gate 			return;
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 		/*
6650Sstevel@tonic-gate 		 * Return the current size.
6660Sstevel@tonic-gate 		 */
6670Sstevel@tonic-gate 		tp = (struct ttysize *)datap->b_wptr;
6680Sstevel@tonic-gate 		tp->ts_lines = tc->t_size.ws_row;
6690Sstevel@tonic-gate 		tp->ts_cols = tc->t_size.ws_col;
6700Sstevel@tonic-gate 		datap->b_wptr += sizeof (struct ttysize);
6710Sstevel@tonic-gate 		iocp->ioc_count = sizeof (struct ttysize);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		if (mp->b_cont != NULL)
6740Sstevel@tonic-gate 			freemsg(mp->b_cont);
6750Sstevel@tonic-gate 		mp->b_cont = datap;
6760Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
6770Sstevel@tonic-gate 		goto out;
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * Imported from ttycommon_ioctl routine
6820Sstevel@tonic-gate 	 */
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	case TCSETSF: {
6850Sstevel@tonic-gate 		tty_common_t *tc = &pty->pt_ttycommon;
6860Sstevel@tonic-gate 		struct termios *cb;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct termios));
6890Sstevel@tonic-gate 		if (error != 0)
6900Sstevel@tonic-gate 			goto out;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 		cb = (struct termios *)mp->b_cont->b_rptr;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		flushq(RD(q), FLUSHDATA);
6950Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
6960Sstevel@tonic-gate 		(void) putnextctl1(RD(q), M_FLUSH, FLUSHR);
6970Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
6980Sstevel@tonic-gate 		mutex_enter(&tc->t_excl);
6990Sstevel@tonic-gate 		tc->t_iflag = cb->c_iflag;
7000Sstevel@tonic-gate 		tc->t_cflag = cb->c_cflag;
7010Sstevel@tonic-gate 		tc->t_stopc = cb->c_cc[VSTOP];
7020Sstevel@tonic-gate 		tc->t_startc = cb->c_cc[VSTART];
7030Sstevel@tonic-gate 		mutex_exit(&tc->t_excl);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		/*
7060Sstevel@tonic-gate 		 * Turn the ioctl message into an ioctl ACK message.
7070Sstevel@tonic-gate 		 */
7080Sstevel@tonic-gate 		iocp->ioc_count = 0;	/* no data returned */
7090Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
7100Sstevel@tonic-gate 		goto ioctldone;
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	case TCSETAF: {
7140Sstevel@tonic-gate 		tty_common_t *tc = &pty->pt_ttycommon;
7150Sstevel@tonic-gate 		struct termios *cb;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct termios));
7180Sstevel@tonic-gate 		if (error != 0)
7190Sstevel@tonic-gate 			goto out;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		cb = (struct termios *)mp->b_cont->b_rptr;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		flushq(RD(q), FLUSHDATA);
7240Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
7250Sstevel@tonic-gate 		(void) putnextctl1(RD(q), M_FLUSH, FLUSHR);
7260Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
7270Sstevel@tonic-gate 		mutex_enter(&tc->t_excl);
7280Sstevel@tonic-gate 		tc->t_iflag = (tc->t_iflag & 0xffff0000 | cb->c_iflag);
7290Sstevel@tonic-gate 		tc->t_cflag = (tc->t_cflag & 0xffff0000 | cb->c_cflag);
7300Sstevel@tonic-gate 		mutex_exit(&tc->t_excl);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 		/*
7330Sstevel@tonic-gate 		 * Turn the ioctl message into an ioctl ACK message.
7340Sstevel@tonic-gate 		 */
7350Sstevel@tonic-gate 		iocp->ioc_count = 0;	/* no data returned */
7360Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
7370Sstevel@tonic-gate 		goto ioctldone;
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	case TIOCSWINSZ: {
7410Sstevel@tonic-gate 		tty_common_t *tc = &pty->pt_ttycommon;
7420Sstevel@tonic-gate 		struct winsize *ws;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct winsize));
7450Sstevel@tonic-gate 		if (error != 0)
7460Sstevel@tonic-gate 			goto out;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		ws = (struct winsize *)mp->b_cont->b_rptr;
7490Sstevel@tonic-gate 		/*
7500Sstevel@tonic-gate 		 * If the window size changed, send a SIGWINCH.
7510Sstevel@tonic-gate 		 */
7520Sstevel@tonic-gate 		mutex_enter(&tc->t_excl);
7530Sstevel@tonic-gate 		if (bcmp(&tc->t_size, ws, sizeof (struct winsize))) {
7540Sstevel@tonic-gate 			tc->t_size = *ws;
7550Sstevel@tonic-gate 			mutex_exit(&tc->t_excl);
7560Sstevel@tonic-gate 			mutex_exit(&pty->ptc_lock);
7570Sstevel@tonic-gate 			(void) putnextctl1(RD(q), M_PCSIG, SIGWINCH);
7580Sstevel@tonic-gate 			mutex_enter(&pty->ptc_lock);
7590Sstevel@tonic-gate 		} else
7600Sstevel@tonic-gate 			mutex_exit(&tc->t_excl);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 		/*
7630Sstevel@tonic-gate 		 * Turn the ioctl message into an ioctl ACK message.
7640Sstevel@tonic-gate 		 */
7650Sstevel@tonic-gate 		iocp->ioc_count = 0;	/* no data returned */
7660Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
7670Sstevel@tonic-gate 		goto ioctldone;
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	/*
7710Sstevel@tonic-gate 	 * If they were just trying to drain output, that's OK.
7720Sstevel@tonic-gate 	 * If they are actually trying to send a break it's an error.
7730Sstevel@tonic-gate 	 */
7740Sstevel@tonic-gate 	case TCSBRK:
7750Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
7760Sstevel@tonic-gate 		if (error != 0)
7770Sstevel@tonic-gate 			goto out;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		if (*(int *)mp->b_cont->b_rptr != 0) {
7800Sstevel@tonic-gate 			/*
7810Sstevel@tonic-gate 			 * Turn the ioctl message into an ioctl ACK message.
7820Sstevel@tonic-gate 			 */
7830Sstevel@tonic-gate 			iocp->ioc_count = 0;	/* no data returned */
7840Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
7850Sstevel@tonic-gate 		} else {
7860Sstevel@tonic-gate 			error = ENOTTY;
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 		goto out;
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	/*
7920Sstevel@tonic-gate 	 * The only way in which "ttycommon_ioctl" can fail is if the "ioctl"
7930Sstevel@tonic-gate 	 * requires a response containing data to be returned to the user,
7940Sstevel@tonic-gate 	 * and no mblk could be allocated for the data.
7950Sstevel@tonic-gate 	 * No such "ioctl" alters our state.  Thus, we always go ahead and
7960Sstevel@tonic-gate 	 * do any state-changes the "ioctl" calls for.  If we couldn't allocate
7970Sstevel@tonic-gate 	 * the data, "ttycommon_ioctl" has stashed the "ioctl" away safely, so
7980Sstevel@tonic-gate 	 * we just call "bufcall" to request that we be called back when we
7990Sstevel@tonic-gate 	 * stand a better chance of allocating the data.
8000Sstevel@tonic-gate 	 */
8010Sstevel@tonic-gate 	if ((datasize =
8020Sstevel@tonic-gate 	    ttycommon_ioctl(&pty->pt_ttycommon, q, mp, &error)) != 0) {
8030Sstevel@tonic-gate 		if (pty->pt_wbufcid) {
8040Sstevel@tonic-gate 			if (pty->pt_ttycommon.t_iocpending)
8050Sstevel@tonic-gate 				freemsg(pty->pt_ttycommon.t_iocpending);
8060Sstevel@tonic-gate 			pty->pt_ttycommon.t_iocpending = mp;
8070Sstevel@tonic-gate 			return;
8080Sstevel@tonic-gate 		}
8090Sstevel@tonic-gate 		pty->pt_wbufcid = bufcall(datasize, BPRI_HI, ptslreioctl, pty);
8100Sstevel@tonic-gate 		if (pty->pt_wbufcid == 0) {
8110Sstevel@tonic-gate 			error = ENOMEM;
8120Sstevel@tonic-gate 			goto out;
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 		pty->pt_ttycommon.t_iocpending = mp;
8150Sstevel@tonic-gate 		return;
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate ioctldone:
8190Sstevel@tonic-gate 	if (error == 0) {
8200Sstevel@tonic-gate 		/*
8210Sstevel@tonic-gate 		 * "ttycommon_ioctl" did most of the work; we just use the
8220Sstevel@tonic-gate 		 * data it set up.
8230Sstevel@tonic-gate 		 */
8240Sstevel@tonic-gate 		switch (cmd) {
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 		case TCSETSF:
8270Sstevel@tonic-gate 		case TCSETAF:
8280Sstevel@tonic-gate 			/*
8290Sstevel@tonic-gate 			 * Set the "flush read" flag, so that we
8300Sstevel@tonic-gate 			 * notify the controller if they're in packet
8310Sstevel@tonic-gate 			 * mode.
8320Sstevel@tonic-gate 			 */
8330Sstevel@tonic-gate 			if (!(pty->pt_send & TIOCPKT_FLUSHREAD)) {
8340Sstevel@tonic-gate 				pty->pt_send |= TIOCPKT_FLUSHREAD;
8350Sstevel@tonic-gate 				ptcpollwakeup(pty, 0);
8360Sstevel@tonic-gate 			}
8370Sstevel@tonic-gate 			/*FALLTHROUGH*/
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		case TCSETSW:
8400Sstevel@tonic-gate 		case TCSETAW:
8410Sstevel@tonic-gate 			cmd = TIOCSETP;	/* map backwards to old codes */
8420Sstevel@tonic-gate 			pt_sendstop(pty);
8430Sstevel@tonic-gate 			break;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 		case TCSETS:
8460Sstevel@tonic-gate 		case TCSETA:
8470Sstevel@tonic-gate 			cmd = TIOCSETN;	/* map backwards to old codes */
8480Sstevel@tonic-gate 			pt_sendstop(pty);
8490Sstevel@tonic-gate 			break;
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (pty->pt_flags & PF_43UCNTL) {
8540Sstevel@tonic-gate 		if (error < 0) {
8550Sstevel@tonic-gate 			if ((cmd & ~0xff) == _IO('u', 0)) {
8560Sstevel@tonic-gate 				if (cmd & 0xff) {
8570Sstevel@tonic-gate 					pty->pt_ucntl = (uchar_t)cmd & 0xff;
8580Sstevel@tonic-gate 					ptcpollwakeup(pty, FREAD);
8590Sstevel@tonic-gate 				}
8600Sstevel@tonic-gate 				error = 0; /* XXX */
8610Sstevel@tonic-gate 				goto out;
8620Sstevel@tonic-gate 			}
8630Sstevel@tonic-gate 			error = ENOTTY;
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 	} else {
8660Sstevel@tonic-gate 		if ((pty->pt_flags & PF_UCNTL) &&
8670Sstevel@tonic-gate 		    (cmd & (IOC_INOUT | 0xff00)) == (IOC_IN|('t'<<8)) &&
8680Sstevel@tonic-gate 		    (cmd & 0xff)) {
8690Sstevel@tonic-gate 			pty->pt_ucntl = (uchar_t)cmd & 0xff;
8700Sstevel@tonic-gate 			ptcpollwakeup(pty, FREAD);
8710Sstevel@tonic-gate 			goto out;
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 		if (error < 0)
8740Sstevel@tonic-gate 			error = ENOTTY;
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate out:
8780Sstevel@tonic-gate 	if (error != 0) {
8790Sstevel@tonic-gate 		((struct iocblk *)mp->b_rptr)->ioc_error = error;
8800Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
8840Sstevel@tonic-gate 	qreply(q, mp);
8850Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate /*
8890Sstevel@tonic-gate  * Service routine for read queue.
8900Sstevel@tonic-gate  * Just wakes the controller side up so it can write some more data
8910Sstevel@tonic-gate  * to that queue.
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate static int
ptslrserv(queue_t * q)8940Sstevel@tonic-gate ptslrserv(queue_t *q)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	struct pty *pty = (struct pty *)q->q_ptr;
8970Sstevel@tonic-gate 	mblk_t *mp;
8980Sstevel@tonic-gate 	mblk_t *head = NULL, *tail = NULL;
8990Sstevel@tonic-gate 	/*
9000Sstevel@tonic-gate 	 * Build up the link list of messages, then drop
9010Sstevel@tonic-gate 	 * drop the lock and do putnext()
9020Sstevel@tonic-gate 	 */
9030Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
9060Sstevel@tonic-gate 		if ((mp->b_datap->db_type < QPCTL) && !canputnext(q)) {
9070Sstevel@tonic-gate 			(void) putbq(q, mp);
9080Sstevel@tonic-gate 			break;
9090Sstevel@tonic-gate 		}
9100Sstevel@tonic-gate 		if (!head) {
9110Sstevel@tonic-gate 			head = mp;
9120Sstevel@tonic-gate 			tail = mp;
9130Sstevel@tonic-gate 		} else {
9140Sstevel@tonic-gate 			tail->b_next = mp;
9150Sstevel@tonic-gate 			tail = mp;
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 	}
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	if (q->q_count <= q->q_lowat)
9200Sstevel@tonic-gate 		ptcpollwakeup((struct pty *)q->q_ptr, FWRITE);
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	while (head) {
9250Sstevel@tonic-gate 		mp = head;
9260Sstevel@tonic-gate 		head = mp->b_next;
9270Sstevel@tonic-gate 		mp->b_next = NULL;
9280Sstevel@tonic-gate 		putnext(q, mp);
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	return (0);
9320Sstevel@tonic-gate }
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate static void
pt_sendstop(struct pty * pty)9350Sstevel@tonic-gate pt_sendstop(struct pty *pty)
9360Sstevel@tonic-gate {
9370Sstevel@tonic-gate 	int stop;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pty->ptc_lock));
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	if ((pty->pt_ttycommon.t_cflag&CBAUD) == 0) {
9420Sstevel@tonic-gate 		if (pty->pt_flags & PF_CARR_ON) {
9430Sstevel@tonic-gate 			/*
9440Sstevel@tonic-gate 			 * Let the controller know, then wake up
9450Sstevel@tonic-gate 			 * readers/selectors and writers/selectors.
9460Sstevel@tonic-gate 			 */
9470Sstevel@tonic-gate 			pty->pt_flags |= PF_SLAVEGONE;
9480Sstevel@tonic-gate 			ptcpollwakeup(pty, 0);
9490Sstevel@tonic-gate 			ptcpollwakeup(pty, FWRITE);
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	stop = (pty->pt_ttycommon.t_iflag & IXON) &&
9540Sstevel@tonic-gate 	    pty->pt_ttycommon.t_stopc == CTRL('s') &&
9550Sstevel@tonic-gate 	    pty->pt_ttycommon.t_startc == CTRL('q');
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	if (pty->pt_flags & PF_NOSTOP) {
9580Sstevel@tonic-gate 		if (stop) {
9590Sstevel@tonic-gate 			pty->pt_send &= ~TIOCPKT_NOSTOP;
9600Sstevel@tonic-gate 			pty->pt_send |= TIOCPKT_DOSTOP;
9610Sstevel@tonic-gate 			pty->pt_flags &= ~PF_NOSTOP;
9620Sstevel@tonic-gate 			ptcpollwakeup(pty, 0);
9630Sstevel@tonic-gate 		}
9640Sstevel@tonic-gate 	} else {
9650Sstevel@tonic-gate 		if (!stop) {
9660Sstevel@tonic-gate 			pty->pt_send &= ~TIOCPKT_DOSTOP;
9670Sstevel@tonic-gate 			pty->pt_send |= TIOCPKT_NOSTOP;
9680Sstevel@tonic-gate 			pty->pt_flags |= PF_NOSTOP;
9690Sstevel@tonic-gate 			ptcpollwakeup(pty, 0);
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate /*
9750Sstevel@tonic-gate  * Wake up controller side.  "flag" is 0 if a special packet or
9760Sstevel@tonic-gate  * user control mode message has been queued up (this data is readable,
9770Sstevel@tonic-gate  * so we also treat it as a regular data event; should we send SIGIO,
9780Sstevel@tonic-gate  * though?), FREAD if regular data has been queued up, or FWRITE if
9790Sstevel@tonic-gate  * the slave's read queue has drained sufficiently to allow writing.
9800Sstevel@tonic-gate  */
9810Sstevel@tonic-gate static void
ptcpollwakeup(struct pty * pty,int flag)9820Sstevel@tonic-gate ptcpollwakeup(struct pty *pty, int flag)
9830Sstevel@tonic-gate {
9840Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pty->ptc_lock));
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	if (flag == 0) {
9870Sstevel@tonic-gate 		/*
9880Sstevel@tonic-gate 		 * "Exceptional condition" occurred.  This means that
9890Sstevel@tonic-gate 		 * a "read" is now possible, so do a "read" wakeup.
9900Sstevel@tonic-gate 		 */
9910Sstevel@tonic-gate 		flag = FREAD;
9920Sstevel@tonic-gate 		pollwakeup(&ptcph, POLLIN | POLLRDBAND);
9930Sstevel@tonic-gate 		if (pty->pt_flags & PF_ASYNC)
9940Sstevel@tonic-gate 			gsignal(pty->pt_pgrp, SIGURG);
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 	if (flag & FREAD) {
9970Sstevel@tonic-gate 		/*
9980Sstevel@tonic-gate 		 * Wake up the parent process as there is regular
9990Sstevel@tonic-gate 		 * data to read from slave's write queue
10000Sstevel@tonic-gate 		 */
10010Sstevel@tonic-gate 		pollwakeup(&ptcph, POLLIN | POLLRDNORM);
10020Sstevel@tonic-gate 		cv_broadcast(&pty->pt_cv_writeq);
10030Sstevel@tonic-gate 		if (pty->pt_flags & PF_ASYNC)
10040Sstevel@tonic-gate 			gsignal(pty->pt_pgrp, SIGIO);
10050Sstevel@tonic-gate 	}
10060Sstevel@tonic-gate 	if (flag & FWRITE) {
10070Sstevel@tonic-gate 		/*
10080Sstevel@tonic-gate 		 * Wake up the parent process to write
10090Sstevel@tonic-gate 		 * data into slave's read queue as the
10100Sstevel@tonic-gate 		 * read queue has drained enough
10110Sstevel@tonic-gate 		 */
10120Sstevel@tonic-gate 		pollwakeup(&ptcph, POLLOUT | POLLWRNORM);
10130Sstevel@tonic-gate 		cv_broadcast(&pty->pt_cv_readq);
10140Sstevel@tonic-gate 		if (pty->pt_flags & PF_ASYNC)
10150Sstevel@tonic-gate 			gsignal(pty->pt_pgrp, SIGIO);
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate }
1018