xref: /onnv-gate/usr/src/uts/common/io/tty_pty.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
2*7656SSherry.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.  For each "controller" side
140Sstevel@tonic-gate  * it connects to a "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/tty.h>
280Sstevel@tonic-gate #include <sys/user.h>
290Sstevel@tonic-gate #include <sys/conf.h>
300Sstevel@tonic-gate #include <sys/file.h>
310Sstevel@tonic-gate #include <sys/vnode.h>	/* 1/0 on the vomit meter */
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/uio.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/strsubr.h>
360Sstevel@tonic-gate #include <sys/poll.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/procset.h>
400Sstevel@tonic-gate #include <sys/cred.h>
410Sstevel@tonic-gate #include <sys/ptyvar.h>
420Sstevel@tonic-gate #include <sys/suntty.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/conf.h>
460Sstevel@tonic-gate #include <sys/ddi.h>
470Sstevel@tonic-gate #include <sys/sunddi.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate extern int npty;	/* number of pseudo-ttys configured in */
500Sstevel@tonic-gate extern struct pty *pty_softc;
510Sstevel@tonic-gate extern struct pollhead	ptcph;	/* poll head for ptcpoll() use */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int ptcopen(dev_t *, int, int, struct cred *);
540Sstevel@tonic-gate int ptcclose(dev_t, int, int, struct cred *);
550Sstevel@tonic-gate int ptcwrite(dev_t, struct uio *, struct cred *);
560Sstevel@tonic-gate int ptcread(dev_t, struct uio *, struct cred *);
570Sstevel@tonic-gate int ptcioctl(dev_t, int, intptr_t, int, struct cred *, int *);
580Sstevel@tonic-gate int ptcpoll(dev_t, short, int, short *, struct pollhead **);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static int ptc_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
610Sstevel@tonic-gate static int ptc_attach(dev_info_t *, ddi_attach_cmd_t);
620Sstevel@tonic-gate static dev_info_t *ptc_dip;	/* for dev-to-dip conversions */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static void ptc_init(void), ptc_uninit(void);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static int makemsg(ssize_t count, struct uio *uiop,
670Sstevel@tonic-gate     struct pty *pty, mblk_t **mpp);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate struct cb_ops	ptc_cb_ops = {
700Sstevel@tonic-gate 	ptcopen,		/* open */
710Sstevel@tonic-gate 	ptcclose,		/* close */
720Sstevel@tonic-gate 	nodev,			/* strategy */
730Sstevel@tonic-gate 	nodev,			/* print */
740Sstevel@tonic-gate 	nodev,			/* dump */
750Sstevel@tonic-gate 	ptcread,		/* read */
760Sstevel@tonic-gate 	ptcwrite,		/* write */
770Sstevel@tonic-gate 	ptcioctl, 		/* ioctl */
780Sstevel@tonic-gate 	nodev,			/* devmap */
790Sstevel@tonic-gate 	nodev,			/* mmap */
800Sstevel@tonic-gate 	nodev,			/* segmap */
810Sstevel@tonic-gate 	ptcpoll,		/* poll */
820Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
830Sstevel@tonic-gate 	0,			/* streamtab */
840Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
850Sstevel@tonic-gate };
860Sstevel@tonic-gate 
870Sstevel@tonic-gate struct dev_ops	ptc_ops = {
880Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
890Sstevel@tonic-gate 	0,			/* refcnt */
900Sstevel@tonic-gate 	ptc_info,		/* info */
910Sstevel@tonic-gate 	nulldev,		/* identify */
920Sstevel@tonic-gate 	nulldev,		/* probe */
930Sstevel@tonic-gate 	ptc_attach,		/* attach */
940Sstevel@tonic-gate 	nodev,			/* detach */
950Sstevel@tonic-gate 	nodev,			/* reset */
960Sstevel@tonic-gate 	&ptc_cb_ops,		/* driver operations */
97*7656SSherry.Moore@Sun.COM 	(struct bus_ops *)0,	/* bus operations */
98*7656SSherry.Moore@Sun.COM 	NULL,			/* power */
99*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #include <sys/types.h>
1030Sstevel@tonic-gate #include <sys/conf.h>
1040Sstevel@tonic-gate #include <sys/param.h>
1050Sstevel@tonic-gate #include <sys/systm.h>
1060Sstevel@tonic-gate #include <sys/errno.h>
1070Sstevel@tonic-gate #include <sys/modctl.h>
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate extern int dseekneg_flag;
1100Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1110Sstevel@tonic-gate extern struct dev_ops ptc_ops;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * Module linkage information for the kernel.
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static struct modldrv modldrv = {
1180Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
119*7656SSherry.Moore@Sun.COM 	"tty pseudo driver control 'ptc'",
1200Sstevel@tonic-gate 	&ptc_ops,	/* driver ops */
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate static struct modlinkage modlinkage = {
1240Sstevel@tonic-gate 	MODREV_1,
1250Sstevel@tonic-gate 	&modldrv,
1260Sstevel@tonic-gate 	NULL
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate int
_init()1300Sstevel@tonic-gate _init()
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	int rc;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if ((rc = mod_install(&modlinkage)) == 0)
1350Sstevel@tonic-gate 		ptc_init();
1360Sstevel@tonic-gate 	return (rc);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate int
_fini()1410Sstevel@tonic-gate _fini()
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	int rc;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if ((rc = mod_remove(&modlinkage)) == 0)
1460Sstevel@tonic-gate 		ptc_uninit();
1470Sstevel@tonic-gate 	return (rc);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1510Sstevel@tonic-gate _info(struct modinfo *modinfop)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static char	*pty_banks = PTY_BANKS;
1570Sstevel@tonic-gate static char	*pty_digits = PTY_DIGITS;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /* ARGSUSED */
1600Sstevel@tonic-gate static int
ptc_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1610Sstevel@tonic-gate ptc_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	char	name[8];
1640Sstevel@tonic-gate 	int	pty_num;
1650Sstevel@tonic-gate 	char	*pty_digit = pty_digits;
1660Sstevel@tonic-gate 	char	*pty_bank = pty_banks;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	for (pty_num = 0; pty_num < npty; pty_num++) {
1690Sstevel@tonic-gate 		(void) sprintf(name, "pty%c%c", *pty_bank, *pty_digit);
1700Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, name, S_IFCHR,
171*7656SSherry.Moore@Sun.COM 		    pty_num, DDI_PSEUDO, NULL) == DDI_FAILURE) {
1720Sstevel@tonic-gate 			ddi_remove_minor_node(devi, NULL);
1730Sstevel@tonic-gate 			return (-1);
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 		if (*(++pty_digit) == '\0') {
1760Sstevel@tonic-gate 			pty_digit = pty_digits;
1770Sstevel@tonic-gate 			if (*(++pty_bank) == '\0')
1780Sstevel@tonic-gate 				break;
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 	ptc_dip = devi;
1820Sstevel@tonic-gate 	return (DDI_SUCCESS);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /* ARGSUSED */
1860Sstevel@tonic-gate static int
ptc_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1870Sstevel@tonic-gate ptc_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	int error;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	switch (infocmd) {
1920Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1930Sstevel@tonic-gate 		if (ptc_dip == NULL) {
1940Sstevel@tonic-gate 			*result = (void *)NULL;
1950Sstevel@tonic-gate 			error = DDI_FAILURE;
1960Sstevel@tonic-gate 		} else {
1970Sstevel@tonic-gate 			*result = (void *) ptc_dip;
1980Sstevel@tonic-gate 			error = DDI_SUCCESS;
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 		break;
2010Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2020Sstevel@tonic-gate 		*result = (void *)0;
2030Sstevel@tonic-gate 		error = DDI_SUCCESS;
2040Sstevel@tonic-gate 		break;
2050Sstevel@tonic-gate 	default:
2060Sstevel@tonic-gate 		error = DDI_FAILURE;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 	return (error);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static void
ptc_init(void)2120Sstevel@tonic-gate ptc_init(void)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	minor_t dev;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	for (dev = 0; dev < npty; dev++) {
2170Sstevel@tonic-gate 		cv_init(&pty_softc[dev].pt_cv_flags, NULL, CV_DEFAULT, NULL);
2180Sstevel@tonic-gate 		cv_init(&pty_softc[dev].pt_cv_readq, NULL, CV_DEFAULT, NULL);
2190Sstevel@tonic-gate 		cv_init(&pty_softc[dev].pt_cv_writeq, NULL, CV_DEFAULT, NULL);
2200Sstevel@tonic-gate 		mutex_init(&pty_softc[dev].ptc_lock, NULL, MUTEX_DEFAULT, NULL);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate static void
ptc_uninit(void)2250Sstevel@tonic-gate ptc_uninit(void)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	minor_t dev;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	for (dev = 0; dev < npty; dev++) {
2300Sstevel@tonic-gate 		cv_destroy(&pty_softc[dev].pt_cv_flags);
2310Sstevel@tonic-gate 		cv_destroy(&pty_softc[dev].pt_cv_readq);
2320Sstevel@tonic-gate 		cv_destroy(&pty_softc[dev].pt_cv_writeq);
2330Sstevel@tonic-gate 		mutex_destroy(&pty_softc[dev].ptc_lock);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  * Controller side.  This is not, alas, a streams device; there are too
2390Sstevel@tonic-gate  * many old features that we must support and that don't work well
2400Sstevel@tonic-gate  * with streams.
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*ARGSUSED*/
2440Sstevel@tonic-gate int
ptcopen(dev_t * devp,int flag,int otyp,struct cred * cred)2450Sstevel@tonic-gate ptcopen(dev_t *devp, int flag, int otyp, struct cred *cred)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	dev_t dev = *devp;
2480Sstevel@tonic-gate 	struct pty *pty;
2490Sstevel@tonic-gate 	queue_t *q;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (getminor(dev) >= npty) {
2520Sstevel@tonic-gate 		return (ENXIO);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 	pty = &pty_softc[getminor(dev)];
2550Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
2560Sstevel@tonic-gate 	if (pty->pt_flags & PF_CARR_ON) {
2570Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
2580Sstevel@tonic-gate 		return (EIO);	/* controller is exclusive use */
2590Sstevel@tonic-gate 				/* XXX - should be EBUSY! */
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 	if (pty->pt_flags & PF_WOPEN) {
2620Sstevel@tonic-gate 		pty->pt_flags &= ~PF_WOPEN;
2630Sstevel@tonic-gate 		cv_broadcast(&pty->pt_cv_flags);
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if ((q = pty->pt_ttycommon.t_readq) != NULL) {
2670Sstevel@tonic-gate 		/*
2680Sstevel@tonic-gate 		 * Send an un-hangup to the slave, since "carrier" is
2690Sstevel@tonic-gate 		 * coming back up.  Make sure we're doing canonicalization.
2700Sstevel@tonic-gate 		 */
2710Sstevel@tonic-gate 		(void) putctl(q, M_UNHANGUP);
2720Sstevel@tonic-gate 		(void) putctl1(q, M_CTL, MC_DOCANON);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	pty->pt_flags |= PF_CARR_ON;
2750Sstevel@tonic-gate 	pty->pt_send = 0;
2760Sstevel@tonic-gate 	pty->pt_ucntl = 0;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
2790Sstevel@tonic-gate 	return (0);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*ARGSUSED1*/
2830Sstevel@tonic-gate int
ptcclose(dev_t dev,int flag,int otyp,struct cred * cred)2840Sstevel@tonic-gate ptcclose(dev_t dev, int flag, int otyp, struct cred *cred)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	struct pty *pty;
2870Sstevel@tonic-gate 	mblk_t *bp;
2880Sstevel@tonic-gate 	queue_t *q;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	pty = &pty_softc[getminor(dev)];
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
2930Sstevel@tonic-gate 	if ((q = pty->pt_ttycommon.t_readq) != NULL) {
2940Sstevel@tonic-gate 		/*
2950Sstevel@tonic-gate 		 * Send a hangup to the slave, since "carrier" is dropping.
2960Sstevel@tonic-gate 		 */
2970Sstevel@tonic-gate 		(void) putctl(q, M_HANGUP);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/*
3010Sstevel@tonic-gate 	 * Clear out all the controller-side state.  This also
3020Sstevel@tonic-gate 	 * clears PF_CARR_ON, which is correct because the
3030Sstevel@tonic-gate 	 * "carrier" is dropping since the controller process
3040Sstevel@tonic-gate 	 * is going away.
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate 	pty->pt_flags &= (PF_WOPEN|PF_STOPPED|PF_NOSTOP);
3070Sstevel@tonic-gate 	while ((bp = pty->pt_stuffqfirst) != NULL) {
3080Sstevel@tonic-gate 		if ((pty->pt_stuffqfirst = bp->b_next) == NULL)
3090Sstevel@tonic-gate 			pty->pt_stuffqlast = NULL;
3100Sstevel@tonic-gate 		else
3110Sstevel@tonic-gate 			pty->pt_stuffqfirst->b_prev = NULL;
3120Sstevel@tonic-gate 		pty->pt_stuffqlen--;
3130Sstevel@tonic-gate 		bp->b_next = bp->b_prev = NULL;
3140Sstevel@tonic-gate 		freemsg(bp);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
3170Sstevel@tonic-gate 	return (0);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate int
ptcread(dev_t dev,struct uio * uio,struct cred * cred)3210Sstevel@tonic-gate ptcread(dev_t dev, struct uio *uio, struct cred *cred)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	struct pty *pty = &pty_softc[getminor(dev)];
3240Sstevel@tonic-gate 	mblk_t *bp, *nbp;
3250Sstevel@tonic-gate 	queue_t *q;
3260Sstevel@tonic-gate 	unsigned char tmp;
3270Sstevel@tonic-gate 	ssize_t cc;
3280Sstevel@tonic-gate 	int error;
3290Sstevel@tonic-gate 	off_t off;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate #ifdef lint
3320Sstevel@tonic-gate 	cred = cred;
3330Sstevel@tonic-gate #endif
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	off = uio->uio_offset;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	for (;;) {
3400Sstevel@tonic-gate 		while (pty->pt_flags & PF_READ) {
3410Sstevel@tonic-gate 			pty->pt_flags |= PF_WREAD;
3420Sstevel@tonic-gate 			cv_wait(&pty->pt_cv_flags, &pty->ptc_lock);
3430Sstevel@tonic-gate 		}
3440Sstevel@tonic-gate 		pty->pt_flags |= PF_READ;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		/*
3470Sstevel@tonic-gate 		 * If there's a TIOCPKT packet waiting, pass it back.
3480Sstevel@tonic-gate 		 */
3490Sstevel@tonic-gate 		while (pty->pt_flags&(PF_PKT|PF_UCNTL) && pty->pt_send) {
3500Sstevel@tonic-gate 			tmp = pty->pt_send;
3510Sstevel@tonic-gate 			pty->pt_send = 0;
3520Sstevel@tonic-gate 			mutex_exit(&pty->ptc_lock);
3530Sstevel@tonic-gate 			error = ureadc((int)tmp, uio);
3540Sstevel@tonic-gate 			uio->uio_offset = off;
3550Sstevel@tonic-gate 			mutex_enter(&pty->ptc_lock);
3560Sstevel@tonic-gate 			if (error) {
3570Sstevel@tonic-gate 				pty->pt_send |= tmp;
3580Sstevel@tonic-gate 				goto out;
3590Sstevel@tonic-gate 			}
3600Sstevel@tonic-gate 			if (pty->pt_send == 0)
3610Sstevel@tonic-gate 				goto out;
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		/*
3650Sstevel@tonic-gate 		 * If there's a user-control packet waiting, pass the
3660Sstevel@tonic-gate 		 * "ioctl" code back.
3670Sstevel@tonic-gate 		 */
3680Sstevel@tonic-gate 		while ((pty->pt_flags & (PF_UCNTL|PF_43UCNTL)) &&
3690Sstevel@tonic-gate 		    pty->pt_ucntl) {
3700Sstevel@tonic-gate 			tmp = pty->pt_ucntl;
3710Sstevel@tonic-gate 			pty->pt_ucntl = 0;
3720Sstevel@tonic-gate 			mutex_exit(&pty->ptc_lock);
3730Sstevel@tonic-gate 			error = ureadc((int)tmp, uio);
3740Sstevel@tonic-gate 			uio->uio_offset = off;
3750Sstevel@tonic-gate 			mutex_enter(&pty->ptc_lock);
3760Sstevel@tonic-gate 			if (error) {
3770Sstevel@tonic-gate 				if (pty->pt_ucntl == 0)
3780Sstevel@tonic-gate 					pty->pt_ucntl = tmp;
3790Sstevel@tonic-gate 				goto out;
3800Sstevel@tonic-gate 			}
3810Sstevel@tonic-gate 			if (pty->pt_ucntl == 0)
3820Sstevel@tonic-gate 				goto out;
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 		/*
3860Sstevel@tonic-gate 		 * If there's any data waiting, pass it back.
3870Sstevel@tonic-gate 		 */
3880Sstevel@tonic-gate 		if ((q = pty->pt_ttycommon.t_writeq) != NULL &&
3890Sstevel@tonic-gate 		    q->q_first != NULL &&
3900Sstevel@tonic-gate 		    !(pty->pt_flags & PF_STOPPED)) {
3910Sstevel@tonic-gate 			if (pty->pt_flags & (PF_PKT|PF_UCNTL|PF_43UCNTL)) {
3920Sstevel@tonic-gate 				/*
3930Sstevel@tonic-gate 				 * We're about to begin a move in packet or
3940Sstevel@tonic-gate 				 * user-control mode; precede the data with a
3950Sstevel@tonic-gate 				 * data header.
3960Sstevel@tonic-gate 				 */
3970Sstevel@tonic-gate 				mutex_exit(&pty->ptc_lock);
3980Sstevel@tonic-gate 				error = ureadc(TIOCPKT_DATA, uio);
3990Sstevel@tonic-gate 				uio->uio_offset = off;
4000Sstevel@tonic-gate 				mutex_enter(&pty->ptc_lock);
4010Sstevel@tonic-gate 				if (error != 0)
4020Sstevel@tonic-gate 					goto out;
4030Sstevel@tonic-gate 				if ((q = pty->pt_ttycommon.t_writeq) == NULL)
4040Sstevel@tonic-gate 					goto out;
4050Sstevel@tonic-gate 			}
4060Sstevel@tonic-gate 			if ((bp = getq(q)) == NULL)
4070Sstevel@tonic-gate 				goto out;
4080Sstevel@tonic-gate 			while (uio->uio_resid > 0) {
4090Sstevel@tonic-gate 				while ((cc = bp->b_wptr - bp->b_rptr) == 0) {
4100Sstevel@tonic-gate 					nbp = bp->b_cont;
4110Sstevel@tonic-gate 					freeb(bp);
4120Sstevel@tonic-gate 					if ((bp = nbp) == NULL) {
4130Sstevel@tonic-gate 						if ((q == NULL) ||
4140Sstevel@tonic-gate 						    (bp = getq(q)) == NULL)
4150Sstevel@tonic-gate 							goto out;
4160Sstevel@tonic-gate 					}
4170Sstevel@tonic-gate 				}
4180Sstevel@tonic-gate 				cc = MIN(cc, uio->uio_resid);
4190Sstevel@tonic-gate 				mutex_exit(&pty->ptc_lock);
4200Sstevel@tonic-gate 				error = uiomove((caddr_t)bp->b_rptr,
4210Sstevel@tonic-gate 				    cc, UIO_READ, uio);
4220Sstevel@tonic-gate 				uio->uio_offset = off;
4230Sstevel@tonic-gate 				mutex_enter(&pty->ptc_lock);
4240Sstevel@tonic-gate 				if (error != 0) {
4250Sstevel@tonic-gate 					freemsg(bp);
4260Sstevel@tonic-gate 					goto out;
4270Sstevel@tonic-gate 				}
4280Sstevel@tonic-gate 				q = pty->pt_ttycommon.t_writeq;
4290Sstevel@tonic-gate 				bp->b_rptr += cc;
4300Sstevel@tonic-gate 			}
4310Sstevel@tonic-gate 			/*
4320Sstevel@tonic-gate 			 * Strip off zero-length blocks from the front of
4330Sstevel@tonic-gate 			 * what we're putting back on the queue.
4340Sstevel@tonic-gate 			 */
4350Sstevel@tonic-gate 			while ((bp->b_wptr - bp->b_rptr) == 0) {
4360Sstevel@tonic-gate 				nbp = bp->b_cont;
4370Sstevel@tonic-gate 				freeb(bp);
4380Sstevel@tonic-gate 				if ((bp = nbp) == NULL)
4390Sstevel@tonic-gate 					goto out;	/* nothing left */
4400Sstevel@tonic-gate 			}
4410Sstevel@tonic-gate 			if (q != NULL)
4420Sstevel@tonic-gate 				(void) putbq(q, bp);
4430Sstevel@tonic-gate 			else
4440Sstevel@tonic-gate 				freemsg(bp);
4450Sstevel@tonic-gate 			goto out;
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 		/*
4490Sstevel@tonic-gate 		 * If there's any TIOCSTI-stuffed characters, pass
4500Sstevel@tonic-gate 		 * them back.  (They currently arrive after all output;
4510Sstevel@tonic-gate 		 * is this correct?)
4520Sstevel@tonic-gate 		 */
4530Sstevel@tonic-gate 		if (pty->pt_flags&PF_UCNTL && pty->pt_stuffqfirst != NULL) {
4540Sstevel@tonic-gate 			mutex_exit(&pty->ptc_lock);
4550Sstevel@tonic-gate 			error = ureadc(TIOCSTI&0xff, uio);
4560Sstevel@tonic-gate 			mutex_enter(&pty->ptc_lock);
4570Sstevel@tonic-gate 			while (error == 0 &&
4580Sstevel@tonic-gate 			    (bp = pty->pt_stuffqfirst) != NULL &&
4590Sstevel@tonic-gate 			    uio->uio_resid > 0) {
4600Sstevel@tonic-gate 				pty->pt_stuffqlen--;
4610Sstevel@tonic-gate 				if ((pty->pt_stuffqfirst = bp->b_next) == NULL)
4620Sstevel@tonic-gate 					pty->pt_stuffqlast = NULL;
4630Sstevel@tonic-gate 				else
4640Sstevel@tonic-gate 					pty->pt_stuffqfirst->b_prev = NULL;
4650Sstevel@tonic-gate 				mutex_exit(&pty->ptc_lock);
4660Sstevel@tonic-gate 				error = ureadc((int)*bp->b_rptr, uio);
4670Sstevel@tonic-gate 				bp->b_next = bp->b_prev = NULL;
4680Sstevel@tonic-gate 				freemsg(bp);
4690Sstevel@tonic-gate 				mutex_enter(&pty->ptc_lock);
4700Sstevel@tonic-gate 			}
4710Sstevel@tonic-gate 			uio->uio_offset = off;
4720Sstevel@tonic-gate 			goto out;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		/*
4760Sstevel@tonic-gate 		 * There's no data available.
4770Sstevel@tonic-gate 		 * We want to block until the slave is open, and there's
4780Sstevel@tonic-gate 		 * something to read; but if we lost the slave or we're NBIO,
4790Sstevel@tonic-gate 		 * then return the appropriate error instead.  POSIX-style
4800Sstevel@tonic-gate 		 * non-block has top billing and gives -1 with errno = EAGAIN,
4810Sstevel@tonic-gate 		 * BSD-style comes next and gives -1 with errno = EWOULDBLOCK,
4820Sstevel@tonic-gate 		 * SVID-style comes last and gives 0.
4830Sstevel@tonic-gate 		 */
4840Sstevel@tonic-gate 		if (pty->pt_flags & PF_SLAVEGONE) {
4850Sstevel@tonic-gate 			error = EIO;
4860Sstevel@tonic-gate 			goto out;
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 		if (uio->uio_fmode & FNONBLOCK) {
4890Sstevel@tonic-gate 			error = EAGAIN;
4900Sstevel@tonic-gate 			goto out;
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 		if (pty->pt_flags & PF_NBIO) {
4930Sstevel@tonic-gate 			error = EWOULDBLOCK;
4940Sstevel@tonic-gate 			goto out;
4950Sstevel@tonic-gate 		}
4960Sstevel@tonic-gate 		if (uio->uio_fmode & FNDELAY)
4970Sstevel@tonic-gate 			goto out;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		if (pty->pt_flags & PF_WREAD)
5000Sstevel@tonic-gate 			cv_broadcast(&pty->pt_cv_flags);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		pty->pt_flags &= ~(PF_READ | PF_WREAD);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 		if (!cv_wait_sig(&pty->pt_cv_writeq, &pty->ptc_lock)) {
5060Sstevel@tonic-gate 			mutex_exit(&pty->ptc_lock);
5070Sstevel@tonic-gate 			return (EINTR);
5080Sstevel@tonic-gate 		}
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate out:
5120Sstevel@tonic-gate 	if (pty->pt_flags & PF_WREAD)
5130Sstevel@tonic-gate 		cv_broadcast(&pty->pt_cv_flags);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	pty->pt_flags &= ~(PF_READ | PF_WREAD);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
5180Sstevel@tonic-gate 	return (error);
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate int
ptcwrite(dev_t dev,struct uio * uio,struct cred * cred)5220Sstevel@tonic-gate ptcwrite(dev_t dev, struct uio *uio, struct cred *cred)
5230Sstevel@tonic-gate {
5240Sstevel@tonic-gate 	struct pty *pty = &pty_softc[getminor(dev)];
5250Sstevel@tonic-gate 	queue_t *q;
5260Sstevel@tonic-gate 	int written;
5270Sstevel@tonic-gate 	mblk_t *mp;
5280Sstevel@tonic-gate 	int fmode = 0;
5290Sstevel@tonic-gate 	int error = 0;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	off_t off;
5320Sstevel@tonic-gate 	off = uio->uio_offset;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate #ifdef lint
5350Sstevel@tonic-gate 	cred = cred;
5360Sstevel@tonic-gate #endif
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	mutex_enter(&pty->ptc_lock);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate again:
5420Sstevel@tonic-gate 	while (pty->pt_flags & PF_WRITE) {
5430Sstevel@tonic-gate 		pty->pt_flags |= PF_WWRITE;
5440Sstevel@tonic-gate 		cv_wait(&pty->pt_cv_flags, &pty->ptc_lock);
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	pty->pt_flags |= PF_WRITE;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if ((q = pty->pt_ttycommon.t_readq) == NULL) {
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * Wait for slave to open.
5530Sstevel@tonic-gate 		 */
5540Sstevel@tonic-gate 		if (pty->pt_flags & PF_SLAVEGONE) {
5550Sstevel@tonic-gate 			error = EIO;
5560Sstevel@tonic-gate 			goto out;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 		if (uio->uio_fmode & FNONBLOCK) {
5590Sstevel@tonic-gate 			error = EAGAIN;
5600Sstevel@tonic-gate 			goto out;
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 		if (pty->pt_flags & PF_NBIO) {
5630Sstevel@tonic-gate 			error = EWOULDBLOCK;
5640Sstevel@tonic-gate 			goto out;
5650Sstevel@tonic-gate 		}
5660Sstevel@tonic-gate 		if (uio->uio_fmode & FNDELAY)
5670Sstevel@tonic-gate 			goto out;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		if (pty->pt_flags & PF_WWRITE)
5700Sstevel@tonic-gate 			cv_broadcast(&pty->pt_cv_flags);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		pty->pt_flags &= ~(PF_WRITE | PF_WWRITE);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 		if (!cv_wait_sig(&pty->pt_cv_readq, &pty->ptc_lock)) {
5750Sstevel@tonic-gate 			mutex_exit(&pty->ptc_lock);
5760Sstevel@tonic-gate 			return (EINTR);
5770Sstevel@tonic-gate 		}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		goto again;
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	/*
5830Sstevel@tonic-gate 	 * If in remote mode, even zero-length writes generate messages.
5840Sstevel@tonic-gate 	 */
5850Sstevel@tonic-gate 	written = 0;
5860Sstevel@tonic-gate 	if ((pty->pt_flags & PF_REMOTE) || uio->uio_resid > 0) {
5870Sstevel@tonic-gate 		do {
5880Sstevel@tonic-gate 			while (!canput(q)) {
5890Sstevel@tonic-gate 				/*
5900Sstevel@tonic-gate 				 * Wait for slave's read queue to unclog.
5910Sstevel@tonic-gate 				 */
5920Sstevel@tonic-gate 				if (pty->pt_flags & PF_SLAVEGONE) {
5930Sstevel@tonic-gate 					error = EIO;
5940Sstevel@tonic-gate 					goto out;
5950Sstevel@tonic-gate 				}
5960Sstevel@tonic-gate 				if (uio->uio_fmode & FNONBLOCK) {
5970Sstevel@tonic-gate 					if (!written)
5980Sstevel@tonic-gate 						error = EAGAIN;
5990Sstevel@tonic-gate 					goto out;
6000Sstevel@tonic-gate 				}
6010Sstevel@tonic-gate 				if (pty->pt_flags & PF_NBIO) {
6020Sstevel@tonic-gate 					if (!written)
6030Sstevel@tonic-gate 						error = EWOULDBLOCK;
6040Sstevel@tonic-gate 					goto out;
6050Sstevel@tonic-gate 				}
6060Sstevel@tonic-gate 				if (uio->uio_fmode & FNDELAY)
6070Sstevel@tonic-gate 					goto out;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 				if (pty->pt_flags & PF_WWRITE)
6100Sstevel@tonic-gate 					cv_broadcast(&pty->pt_cv_flags);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 				pty->pt_flags &= ~(PF_WRITE | PF_WWRITE);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 				if (!cv_wait_sig(&pty->pt_cv_readq,
6150Sstevel@tonic-gate 				    &pty->ptc_lock)) {
6160Sstevel@tonic-gate 					mutex_exit(&pty->ptc_lock);
6170Sstevel@tonic-gate 					return (EINTR);
6180Sstevel@tonic-gate 				}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 				while (pty->pt_flags & PF_WRITE) {
6210Sstevel@tonic-gate 					pty->pt_flags |= PF_WWRITE;
6220Sstevel@tonic-gate 					cv_wait(&pty->pt_cv_flags,
6230Sstevel@tonic-gate 					    &pty->ptc_lock);
6240Sstevel@tonic-gate 				}
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 				pty->pt_flags |= PF_WRITE;
6270Sstevel@tonic-gate 			}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 			if ((pty->pt_flags & PF_NBIO) &&
6300Sstevel@tonic-gate 			    !(uio->uio_fmode & FNONBLOCK)) {
6310Sstevel@tonic-gate 				fmode = uio->uio_fmode;
6320Sstevel@tonic-gate 				uio->uio_fmode |= FNONBLOCK;
6330Sstevel@tonic-gate 			}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 			error = makemsg(uio->uio_resid, uio, pty, &mp);
6360Sstevel@tonic-gate 			uio->uio_offset = off;
6370Sstevel@tonic-gate 			if (fmode)
6380Sstevel@tonic-gate 				uio->uio_fmode = fmode;
6390Sstevel@tonic-gate 			if (error != 0) {
6400Sstevel@tonic-gate 				if (error != EAGAIN && error != EWOULDBLOCK)
6410Sstevel@tonic-gate 					goto out;
6420Sstevel@tonic-gate 				if (uio->uio_fmode & FNONBLOCK) {
6430Sstevel@tonic-gate 					if (!written)
6440Sstevel@tonic-gate 						error = EAGAIN;
6450Sstevel@tonic-gate 					goto out;
6460Sstevel@tonic-gate 				}
6470Sstevel@tonic-gate 				if (pty->pt_flags & PF_NBIO) {
6480Sstevel@tonic-gate 					if (!written)
6490Sstevel@tonic-gate 						error = EWOULDBLOCK;
6500Sstevel@tonic-gate 					goto out;
6510Sstevel@tonic-gate 				}
6520Sstevel@tonic-gate 				if (uio->uio_fmode & FNDELAY)
6530Sstevel@tonic-gate 					goto out;
6540Sstevel@tonic-gate 				cmn_err(CE_PANIC,
6550Sstevel@tonic-gate 				    "ptcwrite: non null return from"
656*7656SSherry.Moore@Sun.COM 				    " makemsg");
6570Sstevel@tonic-gate 			}
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 			/*
6600Sstevel@tonic-gate 			 * Check again for safety; since "uiomove" can take a
6610Sstevel@tonic-gate 			 * page fault, there's no guarantee that "pt_flags"
6620Sstevel@tonic-gate 			 * didn't change while it was happening.
6630Sstevel@tonic-gate 			 */
6640Sstevel@tonic-gate 			if ((q = pty->pt_ttycommon.t_readq) == NULL) {
6650Sstevel@tonic-gate 				if (mp)
6660Sstevel@tonic-gate 					freemsg(mp);
6670Sstevel@tonic-gate 				error = EIO;
6680Sstevel@tonic-gate 				goto out;
6690Sstevel@tonic-gate 			}
6700Sstevel@tonic-gate 			if (mp)
6710Sstevel@tonic-gate 				(void) putq(q, mp);
6720Sstevel@tonic-gate 			written = 1;
6730Sstevel@tonic-gate 		} while (uio->uio_resid > 0);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate out:
6760Sstevel@tonic-gate 	if (pty->pt_flags & PF_WWRITE)
6770Sstevel@tonic-gate 		cv_broadcast(&pty->pt_cv_flags);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	pty->pt_flags &= ~(PF_WRITE | PF_WWRITE);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
6820Sstevel@tonic-gate 	return (error);
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate #define	copy_in(data, d_arg) \
6860Sstevel@tonic-gate 	if (copyin((caddr_t)data, &d_arg, sizeof (int)) != 0) \
6870Sstevel@tonic-gate 		return (EFAULT)
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate #define	copy_out(d_arg, data) \
6900Sstevel@tonic-gate 	if (copyout(&d_arg, (caddr_t)data, sizeof (int)) != 0) \
6910Sstevel@tonic-gate 		return (EFAULT)
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate int
ptcioctl(dev_t dev,int cmd,intptr_t data,int flag,struct cred * cred,int * rvalp)6940Sstevel@tonic-gate ptcioctl(dev_t dev, int cmd, intptr_t data, int flag, struct cred *cred,
6950Sstevel@tonic-gate     int *rvalp)
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate 	struct pty *pty = &pty_softc[getminor(dev)];
6980Sstevel@tonic-gate 	queue_t *q;
6990Sstevel@tonic-gate 	struct ttysize tty_arg;
7000Sstevel@tonic-gate 	struct winsize win_arg;
7010Sstevel@tonic-gate 	int d_arg;
7020Sstevel@tonic-gate 	int err;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	switch (cmd) {
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	case TIOCPKT:
7070Sstevel@tonic-gate 		copy_in(data, d_arg);
7080Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
7090Sstevel@tonic-gate 		if (d_arg) {
7100Sstevel@tonic-gate 			if (pty->pt_flags & (PF_UCNTL|PF_43UCNTL)) {
7110Sstevel@tonic-gate 				mutex_exit(&pty->ptc_lock);
7120Sstevel@tonic-gate 				return (EINVAL);
7130Sstevel@tonic-gate 			}
7140Sstevel@tonic-gate 			pty->pt_flags |= PF_PKT;
7150Sstevel@tonic-gate 		} else
7160Sstevel@tonic-gate 			pty->pt_flags &= ~PF_PKT;
7170Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
7180Sstevel@tonic-gate 		break;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	case TIOCUCNTL:
7210Sstevel@tonic-gate 		copy_in(data, d_arg);
7220Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
7230Sstevel@tonic-gate 		if (d_arg) {
7240Sstevel@tonic-gate 			if (pty->pt_flags & (PF_PKT|PF_UCNTL)) {
7250Sstevel@tonic-gate 				mutex_exit(&pty->ptc_lock);
7260Sstevel@tonic-gate 				return (EINVAL);
7270Sstevel@tonic-gate 			}
7280Sstevel@tonic-gate 			pty->pt_flags |= PF_43UCNTL;
7290Sstevel@tonic-gate 		} else
7300Sstevel@tonic-gate 			pty->pt_flags &= ~PF_43UCNTL;
7310Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
7320Sstevel@tonic-gate 		break;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	case TIOCTCNTL:
7350Sstevel@tonic-gate 		copy_in(data, d_arg);
7360Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
7370Sstevel@tonic-gate 		if (d_arg) {
7380Sstevel@tonic-gate 			if (pty->pt_flags & PF_PKT) {
7390Sstevel@tonic-gate 				mutex_exit(&pty->ptc_lock);
7400Sstevel@tonic-gate 				return (EINVAL);
7410Sstevel@tonic-gate 			}
7420Sstevel@tonic-gate 			pty->pt_flags |= PF_UCNTL;
7430Sstevel@tonic-gate 		} else
7440Sstevel@tonic-gate 			pty->pt_flags &= ~PF_UCNTL;
7450Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
7460Sstevel@tonic-gate 		break;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	case TIOCREMOTE:
7490Sstevel@tonic-gate 		copy_in(data, d_arg);
7500Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
7510Sstevel@tonic-gate 		if (d_arg) {
7520Sstevel@tonic-gate 			if ((q = pty->pt_ttycommon.t_readq) != NULL)
7530Sstevel@tonic-gate 				(void) putctl1(q, M_CTL, MC_NOCANON);
7540Sstevel@tonic-gate 			pty->pt_flags |= PF_REMOTE;
7550Sstevel@tonic-gate 		} else {
7560Sstevel@tonic-gate 			if ((q = pty->pt_ttycommon.t_readq) != NULL)
7570Sstevel@tonic-gate 				(void) putctl1(q, M_CTL, MC_DOCANON);
7580Sstevel@tonic-gate 			pty->pt_flags &= ~PF_REMOTE;
7590Sstevel@tonic-gate 		}
7600Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
7610Sstevel@tonic-gate 		break;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	case TIOCSIGNAL:
7640Sstevel@tonic-gate 		/*
7650Sstevel@tonic-gate 		 * Blast a M_PCSIG message up the slave stream; the
7660Sstevel@tonic-gate 		 * signal number is the argument to the "ioctl".
7670Sstevel@tonic-gate 		 */
7680Sstevel@tonic-gate 		copy_in(data, d_arg);
7690Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
7700Sstevel@tonic-gate 		if ((q = pty->pt_ttycommon.t_readq) != NULL)
7710Sstevel@tonic-gate 			(void) putctl1(q, M_PCSIG, (int)d_arg);
7720Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
7730Sstevel@tonic-gate 		break;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	case FIONBIO:
7760Sstevel@tonic-gate 		copy_in(data, d_arg);
7770Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
7780Sstevel@tonic-gate 		if (d_arg)
7790Sstevel@tonic-gate 			pty->pt_flags |= PF_NBIO;
7800Sstevel@tonic-gate 		else
7810Sstevel@tonic-gate 			pty->pt_flags &= ~PF_NBIO;
7820Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
7830Sstevel@tonic-gate 		break;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	case FIOASYNC:
7860Sstevel@tonic-gate 		copy_in(data, d_arg);
7870Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
7880Sstevel@tonic-gate 		if (d_arg)
7890Sstevel@tonic-gate 			pty->pt_flags |= PF_ASYNC;
7900Sstevel@tonic-gate 		else
7910Sstevel@tonic-gate 			pty->pt_flags &= ~PF_ASYNC;
7920Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
7930Sstevel@tonic-gate 		break;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	/*
7960Sstevel@tonic-gate 	 * These, at least, can work on the controller-side process
7970Sstevel@tonic-gate 	 * group.
7980Sstevel@tonic-gate 	 */
7990Sstevel@tonic-gate 	case FIOGETOWN:
8000Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
8010Sstevel@tonic-gate 		d_arg = -pty->pt_pgrp;
8020Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
8030Sstevel@tonic-gate 		copy_out(d_arg, data);
8040Sstevel@tonic-gate 		break;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	case FIOSETOWN:
8070Sstevel@tonic-gate 		copy_in(data, d_arg);
8080Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
8090Sstevel@tonic-gate 		pty->pt_pgrp = (short)(-d_arg);
8100Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
8110Sstevel@tonic-gate 		break;
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	case FIONREAD: {
8140Sstevel@tonic-gate 		/*
8150Sstevel@tonic-gate 		 * Return the total number of bytes of data in all messages
8160Sstevel@tonic-gate 		 * in slave write queue, which is master read queue, unless a
8170Sstevel@tonic-gate 		 * special message would be read.
8180Sstevel@tonic-gate 		 */
8190Sstevel@tonic-gate 		mblk_t *mp;
8200Sstevel@tonic-gate 		size_t count = 0;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
8230Sstevel@tonic-gate 		if (pty->pt_flags&(PF_PKT|PF_UCNTL) && pty->pt_send)
8240Sstevel@tonic-gate 			count = 1;	/* will return 1 byte */
8250Sstevel@tonic-gate 		else if ((pty->pt_flags & (PF_UCNTL|PF_43UCNTL)) &&
8260Sstevel@tonic-gate 		    pty->pt_ucntl)
8270Sstevel@tonic-gate 			count = 1;	/* will return 1 byte */
8280Sstevel@tonic-gate 		else if ((q = pty->pt_ttycommon.t_writeq) != NULL &&
8290Sstevel@tonic-gate 		    q->q_first != NULL && !(pty->pt_flags & PF_STOPPED)) {
8300Sstevel@tonic-gate 			/*
8310Sstevel@tonic-gate 			 * Will return whatever data is queued up.
8320Sstevel@tonic-gate 			 */
8330Sstevel@tonic-gate 			for (mp = q->q_first; mp != NULL; mp = mp->b_next)
8340Sstevel@tonic-gate 				count += msgdsize(mp);
8350Sstevel@tonic-gate 		} else if ((pty->pt_flags & PF_UCNTL) &&
8360Sstevel@tonic-gate 		    pty->pt_stuffqfirst != NULL) {
8370Sstevel@tonic-gate 			/*
8380Sstevel@tonic-gate 			 * Will return STI'ed data.
8390Sstevel@tonic-gate 			 */
8400Sstevel@tonic-gate 			count = pty->pt_stuffqlen + 1;
8410Sstevel@tonic-gate 		}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 		/*
8440Sstevel@tonic-gate 		 * Under LP64 we could have more than INT_MAX bytes to report,
8450Sstevel@tonic-gate 		 * but the interface is defined in terms of int, so we cap it.
8460Sstevel@tonic-gate 		 */
8470Sstevel@tonic-gate 		d_arg = MIN(count, INT_MAX);
8480Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
8490Sstevel@tonic-gate 		copy_out(d_arg, data);
8500Sstevel@tonic-gate 		break;
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	case TIOCSWINSZ:
8540Sstevel@tonic-gate 		/*
8550Sstevel@tonic-gate 		 * Unfortunately, TIOCSWINSZ and the old TIOCSSIZE "ioctl"s
8560Sstevel@tonic-gate 		 * share the same code.  If the upper 16 bits of the number
8570Sstevel@tonic-gate 		 * of lines is non-zero, it was probably a TIOCSWINSZ,
8580Sstevel@tonic-gate 		 * with both "ws_row" and "ws_col" non-zero.
8590Sstevel@tonic-gate 		 */
8600Sstevel@tonic-gate 		if (copyin((caddr_t)data,
8610Sstevel@tonic-gate 		    &tty_arg, sizeof (struct ttysize)) != 0)
8620Sstevel@tonic-gate 			return (EFAULT);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		if ((tty_arg.ts_lines & 0xffff0000) != 0) {
8650Sstevel@tonic-gate 			/*
8660Sstevel@tonic-gate 			 * It's a TIOCSWINSZ.
8670Sstevel@tonic-gate 			 */
8680Sstevel@tonic-gate 			win_arg = *(struct winsize *)&tty_arg;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 			mutex_enter(&pty->ptc_lock);
8710Sstevel@tonic-gate 			/*
8720Sstevel@tonic-gate 			 * If the window size changed, send a SIGWINCH.
8730Sstevel@tonic-gate 			 */
8740Sstevel@tonic-gate 			if (bcmp(&pty->pt_ttycommon.t_size,
8750Sstevel@tonic-gate 			    &win_arg, sizeof (struct winsize))) {
8760Sstevel@tonic-gate 				pty->pt_ttycommon.t_size = win_arg;
8770Sstevel@tonic-gate 				if ((q = pty->pt_ttycommon.t_readq) != NULL)
8780Sstevel@tonic-gate 					(void) putctl1(q, M_PCSIG, SIGWINCH);
8790Sstevel@tonic-gate 			}
8800Sstevel@tonic-gate 			mutex_exit(&pty->ptc_lock);
8810Sstevel@tonic-gate 			break;
8820Sstevel@tonic-gate 		}
8830Sstevel@tonic-gate 		/* FALLTHROUGH */
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	case TIOCSSIZE:
8860Sstevel@tonic-gate 		if (copyin((caddr_t)data,
8870Sstevel@tonic-gate 		    &tty_arg, sizeof (struct ttysize)) != 0)
8880Sstevel@tonic-gate 			return (EFAULT);
8890Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
8900Sstevel@tonic-gate 		pty->pt_ttycommon.t_size.ws_row = (ushort_t)tty_arg.ts_lines;
8910Sstevel@tonic-gate 		pty->pt_ttycommon.t_size.ws_col = (ushort_t)tty_arg.ts_cols;
8920Sstevel@tonic-gate 		pty->pt_ttycommon.t_size.ws_xpixel = 0;
8930Sstevel@tonic-gate 		pty->pt_ttycommon.t_size.ws_ypixel = 0;
8940Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
8950Sstevel@tonic-gate 		break;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	case TIOCGWINSZ:
8980Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
8990Sstevel@tonic-gate 		win_arg = pty->pt_ttycommon.t_size;
9000Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
9010Sstevel@tonic-gate 		if (copyout(&win_arg, (caddr_t)data,
9020Sstevel@tonic-gate 		    sizeof (struct winsize)) != 0)
9030Sstevel@tonic-gate 			return (EFAULT);
9040Sstevel@tonic-gate 		break;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	case TIOCGSIZE:
9070Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
9080Sstevel@tonic-gate 		tty_arg.ts_lines = pty->pt_ttycommon.t_size.ws_row;
9090Sstevel@tonic-gate 		tty_arg.ts_cols = pty->pt_ttycommon.t_size.ws_col;
9100Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
9110Sstevel@tonic-gate 		if (copyout(&tty_arg, (caddr_t)data,
9120Sstevel@tonic-gate 		    sizeof (struct ttysize)) != 0)
9130Sstevel@tonic-gate 			return (EFAULT);
9140Sstevel@tonic-gate 		break;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	/*
9170Sstevel@tonic-gate 	 * XXX These should not be here.  The only reason why an
9180Sstevel@tonic-gate 	 * "ioctl" on the controller side should get the
9190Sstevel@tonic-gate 	 * slave side's process group is so that the process on
9200Sstevel@tonic-gate 	 * the controller side can send a signal to the slave
9210Sstevel@tonic-gate 	 * side's process group; however, this is better done
9220Sstevel@tonic-gate 	 * with TIOCSIGNAL, both because it doesn't require us
9230Sstevel@tonic-gate 	 * to know about the slave side's process group and because
9240Sstevel@tonic-gate 	 * the controller side process may not have permission to
9250Sstevel@tonic-gate 	 * send that signal to the entire process group.
9260Sstevel@tonic-gate 	 *
9270Sstevel@tonic-gate 	 * However, since vanilla 4BSD doesn't provide TIOCSIGNAL,
9280Sstevel@tonic-gate 	 * we can't just get rid of them.
9290Sstevel@tonic-gate 	 */
9300Sstevel@tonic-gate 	case TIOCGPGRP:
9310Sstevel@tonic-gate 	case TIOCSPGRP:
9320Sstevel@tonic-gate 	/*
9330Sstevel@tonic-gate 	 * This is amazingly disgusting, but the stupid semantics of
9340Sstevel@tonic-gate 	 * 4BSD pseudo-ttys makes us do it.  If we do one of these guys
9350Sstevel@tonic-gate 	 * on the controller side, it really applies to the slave-side
9360Sstevel@tonic-gate 	 * stream.  It should NEVER have been possible to do ANY sort
9370Sstevel@tonic-gate 	 * of tty operations on the controller side, but it's too late
9380Sstevel@tonic-gate 	 * to fix that now.  However, we won't waste our time implementing
9390Sstevel@tonic-gate 	 * anything that the original pseudo-tty driver didn't handle.
9400Sstevel@tonic-gate 	 */
9410Sstevel@tonic-gate 	case TIOCGETP:
9420Sstevel@tonic-gate 	case TIOCSETP:
9430Sstevel@tonic-gate 	case TIOCSETN:
9440Sstevel@tonic-gate 	case TIOCGETC:
9450Sstevel@tonic-gate 	case TIOCSETC:
9460Sstevel@tonic-gate 	case TIOCGLTC:
9470Sstevel@tonic-gate 	case TIOCSLTC:
9480Sstevel@tonic-gate 	case TIOCLGET:
9490Sstevel@tonic-gate 	case TIOCLSET:
9500Sstevel@tonic-gate 	case TIOCLBIS:
9510Sstevel@tonic-gate 	case TIOCLBIC:
9520Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
9530Sstevel@tonic-gate 		if (pty->pt_vnode == NULL) {
9540Sstevel@tonic-gate 			mutex_exit(&pty->ptc_lock);
9550Sstevel@tonic-gate 			return (EIO);
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 		pty->pt_flags |= PF_IOCTL;
9580Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
9590Sstevel@tonic-gate 		err = strioctl(pty->pt_vnode, cmd, data, flag,
9600Sstevel@tonic-gate 		    U_TO_K, cred, rvalp);
9610Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
9620Sstevel@tonic-gate 		if (pty->pt_flags & PF_WAIT)
9630Sstevel@tonic-gate 			cv_signal(&pty->pt_cv_flags);
9640Sstevel@tonic-gate 		pty->pt_flags &= ~(PF_IOCTL|PF_WAIT);
9650Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
9660Sstevel@tonic-gate 		return (err);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	default:
9690Sstevel@tonic-gate 		return (ENOTTY);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	return (0);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate int
ptcpoll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** phpp)9770Sstevel@tonic-gate ptcpoll(dev_t dev,
9780Sstevel@tonic-gate 	short events,
9790Sstevel@tonic-gate 	int anyyet,
9800Sstevel@tonic-gate 	short *reventsp,
9810Sstevel@tonic-gate 	struct pollhead **phpp)
9820Sstevel@tonic-gate {
9830Sstevel@tonic-gate 	struct pty *pty = &pty_softc[getminor(dev)];
9840Sstevel@tonic-gate 	pollhead_t *php = &ptcph;
9850Sstevel@tonic-gate 	queue_t *q;
9860Sstevel@tonic-gate 	int pos = 0;
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate #ifdef lint
9890Sstevel@tonic-gate 	anyyet = anyyet;
9900Sstevel@tonic-gate #endif
9910Sstevel@tonic-gate 	polllock(php, &pty->ptc_lock);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pty->ptc_lock));
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	*reventsp = 0;
9960Sstevel@tonic-gate 	if (pty->pt_flags & PF_SLAVEGONE) {
9970Sstevel@tonic-gate 		if (events & (POLLIN|POLLRDNORM))
9980Sstevel@tonic-gate 			*reventsp |= (events & (POLLIN|POLLRDNORM));
9990Sstevel@tonic-gate 		if (events & (POLLOUT|POLLWRNORM))
10000Sstevel@tonic-gate 			*reventsp |= (events & (POLLOUT|POLLWRNORM));
10010Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
10020Sstevel@tonic-gate 		/*
10030Sstevel@tonic-gate 		 * A non NULL pollhead pointer should be returned in case
10040Sstevel@tonic-gate 		 * user polls for 0 events.
10050Sstevel@tonic-gate 		 */
10060Sstevel@tonic-gate 		*phpp = !anyyet && !*reventsp ? php : (struct pollhead *)NULL;
10070Sstevel@tonic-gate 		return (0);
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 	if (events & (POLLIN|POLLRDNORM)) {
10100Sstevel@tonic-gate 		if ((q = pty->pt_ttycommon.t_writeq) != NULL &&
10110Sstevel@tonic-gate 		    q->q_first != NULL && !(pty->pt_flags & PF_STOPPED)) {
10120Sstevel@tonic-gate 			/*
10130Sstevel@tonic-gate 			 * Regular data is available.
10140Sstevel@tonic-gate 			 */
10150Sstevel@tonic-gate 			*reventsp |= (events & (POLLIN|POLLRDNORM));
10160Sstevel@tonic-gate 			pos++;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 		if (pty->pt_flags & (PF_PKT|PF_UCNTL) && pty->pt_send) {
10190Sstevel@tonic-gate 			/*
10200Sstevel@tonic-gate 			 * A control packet is available.
10210Sstevel@tonic-gate 			 */
10220Sstevel@tonic-gate 			*reventsp |= (events & (POLLIN|POLLRDNORM));
10230Sstevel@tonic-gate 			pos++;
10240Sstevel@tonic-gate 		}
10250Sstevel@tonic-gate 		if ((pty->pt_flags & PF_UCNTL) &&
10260Sstevel@tonic-gate 		    (pty->pt_ucntl || pty->pt_stuffqfirst != NULL)) {
10270Sstevel@tonic-gate 			/*
10280Sstevel@tonic-gate 			 * "ioctl" or TIOCSTI data is available.
10290Sstevel@tonic-gate 			 */
10300Sstevel@tonic-gate 			*reventsp |= (events & (POLLIN|POLLRDNORM));
10310Sstevel@tonic-gate 			pos++;
10320Sstevel@tonic-gate 		}
10330Sstevel@tonic-gate 		if ((pty->pt_flags & PF_43UCNTL) && pty->pt_ucntl) {
10340Sstevel@tonic-gate 			*reventsp |= (events & (POLLIN|POLLRDNORM));
10350Sstevel@tonic-gate 			pos++;
10360Sstevel@tonic-gate 		}
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 	if (events & (POLLOUT|POLLWRNORM)) {
10390Sstevel@tonic-gate 		if ((q = pty->pt_ttycommon.t_readq) != NULL &&
10400Sstevel@tonic-gate 		    canput(q)) {
10410Sstevel@tonic-gate 			*reventsp |= (events & (POLLOUT|POLLWRNORM));
10420Sstevel@tonic-gate 			pos++;
10430Sstevel@tonic-gate 		}
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate 	if (events & POLLERR) {
10460Sstevel@tonic-gate 		*reventsp |= POLLERR;
10470Sstevel@tonic-gate 		pos++;
10480Sstevel@tonic-gate 	}
10490Sstevel@tonic-gate 	if (events == 0) {	/* "exceptional conditions" */
10500Sstevel@tonic-gate 		if (((pty->pt_flags & (PF_PKT|PF_UCNTL)) && pty->pt_send) ||
10510Sstevel@tonic-gate 		    ((pty->pt_flags & PF_UCNTL) &&
10520Sstevel@tonic-gate 		    (pty->pt_ucntl || pty->pt_stuffqfirst != NULL))) {
10530Sstevel@tonic-gate 			pos++;
10540Sstevel@tonic-gate 		}
10550Sstevel@tonic-gate 		if ((pty->pt_flags & PF_43UCNTL) && pty->pt_ucntl) {
10560Sstevel@tonic-gate 			pos++;
10570Sstevel@tonic-gate 		}
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	/*
10610Sstevel@tonic-gate 	 * Arrange to have poll waken up when event occurs.
10620Sstevel@tonic-gate 	 * if (!anyyet)
10630Sstevel@tonic-gate 	 */
10640Sstevel@tonic-gate 	if (!pos) {
10650Sstevel@tonic-gate 		*phpp = php;
10660Sstevel@tonic-gate 		*reventsp = 0;
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	mutex_exit(&pty->ptc_lock);
10700Sstevel@tonic-gate 	return (0);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate void
gsignal(int pid,int sig)10740Sstevel@tonic-gate gsignal(int pid, int sig)
10750Sstevel@tonic-gate {
10760Sstevel@tonic-gate 	procset_t set;
10770Sstevel@tonic-gate 	sigsend_t v;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	bzero(&v, sizeof (v));
10800Sstevel@tonic-gate 	v.sig = sig;
10810Sstevel@tonic-gate 	v.perm = 0;
10820Sstevel@tonic-gate 	v.checkperm = 1;
10830Sstevel@tonic-gate 	v.value.sival_ptr = NULL;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	setprocset(&set, POP_AND, P_PGID, -pid, P_ALL, P_MYID);
10860Sstevel@tonic-gate 	(void) sigsendset(&set, &v);
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate static int
makemsg(ssize_t count,struct uio * uiop,struct pty * pty,mblk_t ** mpp)10900Sstevel@tonic-gate makemsg(ssize_t count, struct uio *uiop, struct pty *pty, mblk_t **mpp)
10910Sstevel@tonic-gate {
10920Sstevel@tonic-gate 	int pri = BPRI_LO;
10930Sstevel@tonic-gate 	int error;
10940Sstevel@tonic-gate 	mblk_t *bp = NULL;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pty->ptc_lock));
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	*mpp = NULL;
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	/*
11010Sstevel@tonic-gate 	 * Create data part of message, if any.
11020Sstevel@tonic-gate 	 */
11030Sstevel@tonic-gate 	if (count >= 0) {
11040Sstevel@tonic-gate 		if ((bp = allocb(count, pri)) == NULL)
11050Sstevel@tonic-gate 			return (ENOSR);
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		mutex_exit(&pty->ptc_lock);
11080Sstevel@tonic-gate 		error = uiomove((caddr_t)bp->b_wptr, count, UIO_WRITE, uiop);
11090Sstevel@tonic-gate 		mutex_enter(&pty->ptc_lock);
11100Sstevel@tonic-gate 		if (error) {
11110Sstevel@tonic-gate 			freeb(bp);
11120Sstevel@tonic-gate 			return (error);
11130Sstevel@tonic-gate 		}
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 		bp->b_wptr += count;
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	*mpp = bp;
11190Sstevel@tonic-gate 	return (0);
11200Sstevel@tonic-gate }
1121