pty.c revision 8876
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 3. All advertising materials mentioning features or use of this software 141541Srgrimes * must display the following acknowledgement: 151541Srgrimes * This product includes software developed by the University of 161541Srgrimes * California, Berkeley and its contributors. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 331541Srgrimes * @(#)tty_pty.c 8.2 (Berkeley) 9/23/93 348876Srgrimes * $Id: tty_pty.c,v 1.10 1995/04/10 01:45:43 ache Exp $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes/* 381541Srgrimes * Pseudo-teletype Driver 391541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw') 401541Srgrimes */ 411541Srgrimes#include "pty.h" /* XXX */ 421541Srgrimes 431541Srgrimes#include <sys/param.h> 441541Srgrimes#include <sys/systm.h> 451541Srgrimes#include <sys/ioctl.h> 461541Srgrimes#include <sys/proc.h> 471541Srgrimes#include <sys/tty.h> 481541Srgrimes#include <sys/conf.h> 491541Srgrimes#include <sys/file.h> 501541Srgrimes#include <sys/uio.h> 511541Srgrimes#include <sys/kernel.h> 521541Srgrimes#include <sys/vnode.h> 533308Sphk#include <sys/signalvar.h> 541541Srgrimes 551541Srgrimes#if NPTY == 1 561541Srgrimes#undef NPTY 571541Srgrimes#define NPTY 32 /* crude XXX */ 581541Srgrimes#endif 591541Srgrimes 601541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 611541Srgrimes 621541Srgrimes/* 631541Srgrimes * pts == /dev/tty[pqrs]? 641541Srgrimes * ptc == /dev/pty[pqrs]? 651541Srgrimes */ 661541Srgrimesstruct tty pt_tty[NPTY]; /* XXX */ 671541Srgrimesstruct pt_ioctl { 681541Srgrimes int pt_flags; 691541Srgrimes struct selinfo pt_selr, pt_selw; 701541Srgrimes u_char pt_send; 711541Srgrimes u_char pt_ucntl; 721541Srgrimes} pt_ioctl[NPTY]; /* XXX */ 731541Srgrimesint npty = NPTY; /* for pstat -t */ 741541Srgrimes 751541Srgrimes#define PF_PKT 0x08 /* packet mode */ 761541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 771541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 781541Srgrimes#define PF_NOSTOP 0x40 791541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 801541Srgrimes 811549Srgrimesvoid ptsstop __P((struct tty *, int)); 821549Srgrimesvoid ptcwakeup __P((struct tty *, int)); 831541Srgrimes 841541Srgrimes/* 851541Srgrimes * Establish n (or default if n is 1) ptys in the system. 861541Srgrimes * 871541Srgrimes * XXX cdevsw & pstat require the array `pty[]' to be an array 881541Srgrimes */ 891541Srgrimesvoid 901541Srgrimesptyattach(n) 911541Srgrimes int n; 921541Srgrimes{ 931541Srgrimes#ifdef notyet 941541Srgrimes char *mem; 951541Srgrimes register u_long ntb; 961541Srgrimes#define DEFAULT_NPTY 32 971541Srgrimes 981541Srgrimes /* maybe should allow 0 => none? */ 991541Srgrimes if (n <= 1) 1001541Srgrimes n = DEFAULT_NPTY; 1011541Srgrimes ntb = n * sizeof(struct tty); 1021541Srgrimes mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl), 1031541Srgrimes M_DEVBUF, M_WAITOK); 1041541Srgrimes pt_tty = (struct tty *)mem; 1051541Srgrimes mem = (char *)ALIGN(mem + ntb); 1061541Srgrimes pt_ioctl = (struct pt_ioctl *)mem; 1071541Srgrimes npty = n; 1081541Srgrimes#endif 1091541Srgrimes} 1101541Srgrimes 1111541Srgrimes/*ARGSUSED*/ 1121549Srgrimesint 1131541Srgrimesptsopen(dev, flag, devtype, p) 1141541Srgrimes dev_t dev; 1151541Srgrimes int flag, devtype; 1161541Srgrimes struct proc *p; 1171541Srgrimes{ 1181541Srgrimes register struct tty *tp; 1191541Srgrimes int error; 1201541Srgrimes 1211541Srgrimes if (minor(dev) >= npty) 1221541Srgrimes return (ENXIO); 1231541Srgrimes tp = &pt_tty[minor(dev)]; 1241541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 1251541Srgrimes tp->t_state |= TS_WOPEN; 1261541Srgrimes ttychars(tp); /* Set up default chars */ 1271541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 1281541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 1291541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 1301541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 1311541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 1321541Srgrimes ttsetwater(tp); /* would be done in xxparam() */ 1331541Srgrimes } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) 1341541Srgrimes return (EBUSY); 1351541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 1361541Srgrimes tp->t_state |= TS_CARR_ON; 1371541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 1381541Srgrimes tp->t_state |= TS_WOPEN; 1391541Srgrimes if (flag&FNONBLOCK) 1401541Srgrimes break; 1413308Sphk error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 1423308Sphk ttopen, 0); 1433308Sphk if (error) 1441541Srgrimes return (error); 1451541Srgrimes } 1461541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 1477724Sache if (error == 0) 1487724Sache ptcwakeup(tp, FREAD|FWRITE); 1491541Srgrimes return (error); 1501541Srgrimes} 1511541Srgrimes 1521549Srgrimesint 1531541Srgrimesptsclose(dev, flag, mode, p) 1541541Srgrimes dev_t dev; 1551541Srgrimes int flag, mode; 1561541Srgrimes struct proc *p; 1571541Srgrimes{ 1581541Srgrimes register struct tty *tp; 1591541Srgrimes int err; 1601541Srgrimes 1611541Srgrimes tp = &pt_tty[minor(dev)]; 1621541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 1637730Sache ptsstop(tp, FREAD|FWRITE); 1647724Sache (void) ttyclose(tp); 1651541Srgrimes return (err); 1661541Srgrimes} 1671541Srgrimes 1681549Srgrimesint 1691541Srgrimesptsread(dev, uio, flag) 1701541Srgrimes dev_t dev; 1711541Srgrimes struct uio *uio; 1721541Srgrimes int flag; 1731541Srgrimes{ 1741541Srgrimes struct proc *p = curproc; 1751541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 1761541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 1771541Srgrimes int error = 0; 1781541Srgrimes 1791541Srgrimesagain: 1801541Srgrimes if (pti->pt_flags & PF_REMOTE) { 1811541Srgrimes while (isbackground(p, tp)) { 1821541Srgrimes if ((p->p_sigignore & sigmask(SIGTTIN)) || 1831541Srgrimes (p->p_sigmask & sigmask(SIGTTIN)) || 1841541Srgrimes p->p_pgrp->pg_jobc == 0 || 1851541Srgrimes p->p_flag & P_PPWAIT) 1861541Srgrimes return (EIO); 1871541Srgrimes pgsignal(p->p_pgrp, SIGTTIN, 1); 1888876Srgrimes error = ttysleep(tp, (caddr_t)&lbolt, 1893308Sphk TTIPRI | PCATCH, ttybg, 0); 1903308Sphk if (error) 1911541Srgrimes return (error); 1921541Srgrimes } 1931541Srgrimes if (tp->t_canq.c_cc == 0) { 1941541Srgrimes if (flag & IO_NDELAY) 1951541Srgrimes return (EWOULDBLOCK); 1963308Sphk error = ttysleep(tp, (caddr_t)&tp->t_canq, 1973308Sphk TTIPRI | PCATCH, ttyin, 0); 1983308Sphk if (error) 1991541Srgrimes return (error); 2001541Srgrimes goto again; 2011541Srgrimes } 2021541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2031541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2041541Srgrimes error = EFAULT; 2051541Srgrimes break; 2061541Srgrimes } 2071541Srgrimes if (tp->t_canq.c_cc == 1) 2081541Srgrimes (void) getc(&tp->t_canq); 2091541Srgrimes if (tp->t_canq.c_cc) 2101541Srgrimes return (error); 2111541Srgrimes } else 2121541Srgrimes if (tp->t_oproc) 2131541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2141541Srgrimes ptcwakeup(tp, FWRITE); 2151541Srgrimes return (error); 2161541Srgrimes} 2171541Srgrimes 2181541Srgrimes/* 2191541Srgrimes * Write to pseudo-tty. 2201541Srgrimes * Wakeups of controlling tty will happen 2211541Srgrimes * indirectly, when tty driver calls ptsstart. 2221541Srgrimes */ 2231549Srgrimesint 2241541Srgrimesptswrite(dev, uio, flag) 2251541Srgrimes dev_t dev; 2261541Srgrimes struct uio *uio; 2271541Srgrimes int flag; 2281541Srgrimes{ 2291541Srgrimes register struct tty *tp; 2301541Srgrimes 2311541Srgrimes tp = &pt_tty[minor(dev)]; 2321541Srgrimes if (tp->t_oproc == 0) 2331541Srgrimes return (EIO); 2341541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2351541Srgrimes} 2361541Srgrimes 2371541Srgrimes/* 2381541Srgrimes * Start output on pseudo-tty. 2391541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 2401541Srgrimes */ 2411541Srgrimesvoid 2421541Srgrimesptsstart(tp) 2431541Srgrimes struct tty *tp; 2441541Srgrimes{ 2451541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 2461541Srgrimes 2471541Srgrimes if (tp->t_state & TS_TTSTOP) 2481541Srgrimes return; 2491541Srgrimes if (pti->pt_flags & PF_STOPPED) { 2501541Srgrimes pti->pt_flags &= ~PF_STOPPED; 2511541Srgrimes pti->pt_send = TIOCPKT_START; 2521541Srgrimes } 2531541Srgrimes ptcwakeup(tp, FREAD); 2541541Srgrimes} 2551541Srgrimes 2561549Srgrimesvoid 2571541Srgrimesptcwakeup(tp, flag) 2581541Srgrimes struct tty *tp; 2591541Srgrimes int flag; 2601541Srgrimes{ 2611541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 2621541Srgrimes 2631541Srgrimes if (flag & FREAD) { 2641541Srgrimes selwakeup(&pti->pt_selr); 2651541Srgrimes wakeup((caddr_t)&tp->t_outq.c_cf); 2661541Srgrimes } 2671541Srgrimes if (flag & FWRITE) { 2681541Srgrimes selwakeup(&pti->pt_selw); 2693998Sache wakeup((caddr_t)&tp->t_rawq.c_cl); 2701541Srgrimes } 2711541Srgrimes} 2721541Srgrimes 2731541Srgrimes/*ARGSUSED*/ 2741541Srgrimes#ifdef __STDC__ 2751549Srgrimesint 2761541Srgrimesptcopen(dev_t dev, int flag, int devtype, struct proc *p) 2771541Srgrimes#else 2781549Srgrimesint 2791541Srgrimesptcopen(dev, flag, devtype, p) 2801541Srgrimes dev_t dev; 2811541Srgrimes int flag, devtype; 2821541Srgrimes struct proc *p; 2831541Srgrimes#endif 2841541Srgrimes{ 2851541Srgrimes register struct tty *tp; 2861541Srgrimes struct pt_ioctl *pti; 2871541Srgrimes 2881541Srgrimes if (minor(dev) >= npty) 2891541Srgrimes return (ENXIO); 2901541Srgrimes tp = &pt_tty[minor(dev)]; 2911541Srgrimes if (tp->t_oproc) 2921541Srgrimes return (EIO); 2931541Srgrimes tp->t_oproc = ptsstart; 2941541Srgrimes#ifdef sun4c 2951541Srgrimes tp->t_stop = ptsstop; 2961541Srgrimes#endif 2971541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 2981541Srgrimes tp->t_lflag &= ~EXTPROC; 2991541Srgrimes pti = &pt_ioctl[minor(dev)]; 3001541Srgrimes pti->pt_flags = 0; 3011541Srgrimes pti->pt_send = 0; 3021541Srgrimes pti->pt_ucntl = 0; 3031541Srgrimes return (0); 3041541Srgrimes} 3051541Srgrimes 3061549Srgrimesint 3071541Srgrimesptcclose(dev) 3081541Srgrimes dev_t dev; 3091541Srgrimes{ 3101541Srgrimes register struct tty *tp; 3111541Srgrimes 3121541Srgrimes tp = &pt_tty[minor(dev)]; 3131541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3141541Srgrimes tp->t_state &= ~TS_CARR_ON; 3151541Srgrimes tp->t_oproc = 0; /* mark closed */ 3161541Srgrimes tp->t_session = 0; 3171541Srgrimes return (0); 3181541Srgrimes} 3191541Srgrimes 3201549Srgrimesint 3211541Srgrimesptcread(dev, uio, flag) 3221541Srgrimes dev_t dev; 3231541Srgrimes struct uio *uio; 3241541Srgrimes int flag; 3251541Srgrimes{ 3261541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 3271541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 3281541Srgrimes char buf[BUFSIZ]; 3291541Srgrimes int error = 0, cc; 3301541Srgrimes 3311541Srgrimes /* 3321541Srgrimes * We want to block until the slave 3331541Srgrimes * is open, and there's something to read; 3341541Srgrimes * but if we lost the slave or we're NBIO, 3351541Srgrimes * then return the appropriate error instead. 3361541Srgrimes */ 3371541Srgrimes for (;;) { 3381541Srgrimes if (tp->t_state&TS_ISOPEN) { 3391541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 3401541Srgrimes error = ureadc((int)pti->pt_send, uio); 3411541Srgrimes if (error) 3421541Srgrimes return (error); 3431541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 3441541Srgrimes cc = min(uio->uio_resid, 3451541Srgrimes sizeof(tp->t_termios)); 3462807Sbde uiomove((caddr_t)&tp->t_termios, cc, 3472807Sbde uio); 3481541Srgrimes } 3491541Srgrimes pti->pt_send = 0; 3501541Srgrimes return (0); 3511541Srgrimes } 3521541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 3531541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 3541541Srgrimes if (error) 3551541Srgrimes return (error); 3561541Srgrimes pti->pt_ucntl = 0; 3571541Srgrimes return (0); 3581541Srgrimes } 3591541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 3601541Srgrimes break; 3611541Srgrimes } 3621541Srgrimes if ((tp->t_state&TS_CARR_ON) == 0) 3631541Srgrimes return (0); /* EOF */ 3641541Srgrimes if (flag & IO_NDELAY) 3651541Srgrimes return (EWOULDBLOCK); 3663308Sphk error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH, 3673308Sphk ttyin, 0); 3683308Sphk if (error) 3691541Srgrimes return (error); 3701541Srgrimes } 3711541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 3721541Srgrimes error = ureadc(0, uio); 3731541Srgrimes while (uio->uio_resid > 0 && error == 0) { 3741541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 3751541Srgrimes if (cc <= 0) 3761541Srgrimes break; 3771541Srgrimes error = uiomove(buf, cc, uio); 3781541Srgrimes } 3791541Srgrimes if (tp->t_outq.c_cc <= tp->t_lowat) { 3801541Srgrimes if (tp->t_state&TS_ASLEEP) { 3811541Srgrimes tp->t_state &= ~TS_ASLEEP; 3821541Srgrimes wakeup((caddr_t)&tp->t_outq); 3831541Srgrimes } 3841541Srgrimes selwakeup(&tp->t_wsel); 3851541Srgrimes } 3861541Srgrimes return (error); 3871541Srgrimes} 3881541Srgrimes 3891541Srgrimesvoid 3901541Srgrimesptsstop(tp, flush) 3911541Srgrimes register struct tty *tp; 3921541Srgrimes int flush; 3931541Srgrimes{ 3941541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 3951541Srgrimes int flag; 3961541Srgrimes 3971541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 3981541Srgrimes if (flush == 0) { 3991541Srgrimes flush = TIOCPKT_STOP; 4001541Srgrimes pti->pt_flags |= PF_STOPPED; 4011541Srgrimes } else 4021541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4031541Srgrimes pti->pt_send |= flush; 4041541Srgrimes /* change of perspective */ 4051541Srgrimes flag = 0; 4061541Srgrimes if (flush & FREAD) 4071541Srgrimes flag |= FWRITE; 4081541Srgrimes if (flush & FWRITE) 4091541Srgrimes flag |= FREAD; 4101541Srgrimes ptcwakeup(tp, flag); 4111541Srgrimes} 4121541Srgrimes 4131549Srgrimesint 4141541Srgrimesptcselect(dev, rw, p) 4151541Srgrimes dev_t dev; 4161541Srgrimes int rw; 4171541Srgrimes struct proc *p; 4181541Srgrimes{ 4191541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 4201541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 4211541Srgrimes int s; 4221541Srgrimes 4231541Srgrimes if ((tp->t_state&TS_CARR_ON) == 0) 4241541Srgrimes return (1); 4251541Srgrimes switch (rw) { 4261541Srgrimes 4271541Srgrimes case FREAD: 4281541Srgrimes /* 4291541Srgrimes * Need to block timeouts (ttrstart). 4301541Srgrimes */ 4311541Srgrimes s = spltty(); 4321541Srgrimes if ((tp->t_state&TS_ISOPEN) && 4331541Srgrimes tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) { 4341541Srgrimes splx(s); 4351541Srgrimes return (1); 4361541Srgrimes } 4371541Srgrimes splx(s); 4381541Srgrimes /* FALLTHROUGH */ 4391541Srgrimes 4401541Srgrimes case 0: /* exceptional */ 4411541Srgrimes if ((tp->t_state&TS_ISOPEN) && 4423308Sphk ((pti->pt_flags&PF_PKT && pti->pt_send) || 4433308Sphk (pti->pt_flags&PF_UCNTL && pti->pt_ucntl))) 4441541Srgrimes return (1); 4451541Srgrimes selrecord(p, &pti->pt_selr); 4461541Srgrimes break; 4471541Srgrimes 4481541Srgrimes 4491541Srgrimes case FWRITE: 4501541Srgrimes if (tp->t_state&TS_ISOPEN) { 4511541Srgrimes if (pti->pt_flags & PF_REMOTE) { 4521541Srgrimes if (tp->t_canq.c_cc == 0) 4531541Srgrimes return (1); 4541541Srgrimes } else { 4551541Srgrimes if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) 4561541Srgrimes return (1); 4571541Srgrimes if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON)) 4581541Srgrimes return (1); 4591541Srgrimes } 4601541Srgrimes } 4611541Srgrimes selrecord(p, &pti->pt_selw); 4621541Srgrimes break; 4631541Srgrimes 4641541Srgrimes } 4651541Srgrimes return (0); 4661541Srgrimes} 4671541Srgrimes 4681549Srgrimesint 4691541Srgrimesptcwrite(dev, uio, flag) 4701541Srgrimes dev_t dev; 4711541Srgrimes register struct uio *uio; 4721541Srgrimes int flag; 4731541Srgrimes{ 4741541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 4751549Srgrimes register u_char *cp = 0; 4761541Srgrimes register int cc = 0; 4771541Srgrimes u_char locbuf[BUFSIZ]; 4781541Srgrimes int cnt = 0; 4791541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 4801541Srgrimes int error = 0; 4811541Srgrimes 4821541Srgrimesagain: 4831541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 4841541Srgrimes goto block; 4851541Srgrimes if (pti->pt_flags & PF_REMOTE) { 4861541Srgrimes if (tp->t_canq.c_cc) 4871541Srgrimes goto block; 4881541Srgrimes while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) { 4891541Srgrimes if (cc == 0) { 4901541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 4911541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 4921541Srgrimes cp = locbuf; 4931541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 4941541Srgrimes if (error) 4951541Srgrimes return (error); 4961541Srgrimes /* check again for safety */ 4971541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 4981541Srgrimes return (EIO); 4991541Srgrimes } 5001541Srgrimes if (cc) 5011541Srgrimes (void) b_to_q((char *)cp, cc, &tp->t_canq); 5021541Srgrimes cc = 0; 5031541Srgrimes } 5041541Srgrimes (void) putc(0, &tp->t_canq); 5051541Srgrimes ttwakeup(tp); 5061541Srgrimes wakeup((caddr_t)&tp->t_canq); 5071541Srgrimes return (0); 5081541Srgrimes } 5091541Srgrimes while (uio->uio_resid > 0) { 5101541Srgrimes if (cc == 0) { 5111541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5121541Srgrimes cp = locbuf; 5131541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5141541Srgrimes if (error) 5151541Srgrimes return (error); 5161541Srgrimes /* check again for safety */ 5171541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 5181541Srgrimes return (EIO); 5191541Srgrimes } 5201541Srgrimes while (cc > 0) { 5211541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 5221541Srgrimes (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 5231541Srgrimes wakeup((caddr_t)&tp->t_rawq); 5241541Srgrimes goto block; 5251541Srgrimes } 5261541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 5271541Srgrimes cnt++; 5281541Srgrimes cc--; 5291541Srgrimes } 5301541Srgrimes cc = 0; 5311541Srgrimes } 5321541Srgrimes return (0); 5331541Srgrimesblock: 5341541Srgrimes /* 5351541Srgrimes * Come here to wait for slave to open, for space 5361541Srgrimes * in outq, or space in rawq. 5371541Srgrimes */ 5381541Srgrimes if ((tp->t_state&TS_CARR_ON) == 0) 5391541Srgrimes return (EIO); 5401541Srgrimes if (flag & IO_NDELAY) { 5411541Srgrimes /* adjust for data copied in but not written */ 5421541Srgrimes uio->uio_resid += cc; 5431541Srgrimes if (cnt == 0) 5441541Srgrimes return (EWOULDBLOCK); 5451541Srgrimes return (0); 5461541Srgrimes } 5473998Sache error = tsleep((caddr_t)&tp->t_rawq.c_cl, TTOPRI | PCATCH, ttyout, 0); 5483308Sphk if (error) { 5491541Srgrimes /* adjust for data copied in but not written */ 5501541Srgrimes uio->uio_resid += cc; 5511541Srgrimes return (error); 5521541Srgrimes } 5531541Srgrimes goto again; 5541541Srgrimes} 5551541Srgrimes 5566712Spststruct tty * 5576712Spstptydevtotty(dev) 5586712Spst dev_t dev; 5596712Spst{ 5606712Spst if (minor(dev) >= npty) 5616712Spst return (NULL); 5626712Spst 5636712Spst return &pt_tty[minor(dev)]; 5646712Spst} 5656712Spst 5661541Srgrimes/*ARGSUSED*/ 5671549Srgrimesint 5681541Srgrimesptyioctl(dev, cmd, data, flag, p) 5691541Srgrimes dev_t dev; 5701541Srgrimes int cmd; 5711541Srgrimes caddr_t data; 5721541Srgrimes int flag; 5731541Srgrimes struct proc *p; 5741541Srgrimes{ 5751541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 5761541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 5771541Srgrimes register u_char *cc = tp->t_cc; 5781541Srgrimes int stop, error; 5791541Srgrimes 5801541Srgrimes /* 5811541Srgrimes * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 5821541Srgrimes * ttywflush(tp) will hang if there are characters in the outq. 5831541Srgrimes */ 5841541Srgrimes if (cmd == TIOCEXT) { 5851541Srgrimes /* 5861541Srgrimes * When the EXTPROC bit is being toggled, we need 5871541Srgrimes * to send an TIOCPKT_IOCTL if the packet driver 5881541Srgrimes * is turned on. 5891541Srgrimes */ 5901541Srgrimes if (*(int *)data) { 5911541Srgrimes if (pti->pt_flags & PF_PKT) { 5921541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 5931541Srgrimes ptcwakeup(tp, FREAD); 5941541Srgrimes } 5951541Srgrimes tp->t_lflag |= EXTPROC; 5961541Srgrimes } else { 5971541Srgrimes if ((tp->t_state & EXTPROC) && 5981541Srgrimes (pti->pt_flags & PF_PKT)) { 5991541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 6001541Srgrimes ptcwakeup(tp, FREAD); 6011541Srgrimes } 6021541Srgrimes tp->t_lflag &= ~EXTPROC; 6031541Srgrimes } 6041541Srgrimes return(0); 6051541Srgrimes } else 6061541Srgrimes if (cdevsw[major(dev)].d_open == ptcopen) 6071541Srgrimes switch (cmd) { 6081541Srgrimes 6091541Srgrimes case TIOCGPGRP: 6101541Srgrimes /* 6111541Srgrimes * We aviod calling ttioctl on the controller since, 6121541Srgrimes * in that case, tp must be the controlling terminal. 6131541Srgrimes */ 6141541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6151541Srgrimes return (0); 6161541Srgrimes 6171541Srgrimes case TIOCPKT: 6181541Srgrimes if (*(int *)data) { 6191541Srgrimes if (pti->pt_flags & PF_UCNTL) 6201541Srgrimes return (EINVAL); 6211541Srgrimes pti->pt_flags |= PF_PKT; 6221541Srgrimes } else 6231541Srgrimes pti->pt_flags &= ~PF_PKT; 6241541Srgrimes return (0); 6251541Srgrimes 6261541Srgrimes case TIOCUCNTL: 6271541Srgrimes if (*(int *)data) { 6281541Srgrimes if (pti->pt_flags & PF_PKT) 6291541Srgrimes return (EINVAL); 6301541Srgrimes pti->pt_flags |= PF_UCNTL; 6311541Srgrimes } else 6321541Srgrimes pti->pt_flags &= ~PF_UCNTL; 6331541Srgrimes return (0); 6341541Srgrimes 6351541Srgrimes case TIOCREMOTE: 6361541Srgrimes if (*(int *)data) 6371541Srgrimes pti->pt_flags |= PF_REMOTE; 6381541Srgrimes else 6391541Srgrimes pti->pt_flags &= ~PF_REMOTE; 6401541Srgrimes ttyflush(tp, FREAD|FWRITE); 6411541Srgrimes return (0); 6421541Srgrimes 6431541Srgrimes#ifdef COMPAT_43 6448876Srgrimes case TIOCSETP: 6451541Srgrimes case TIOCSETN: 6461541Srgrimes#endif 6471541Srgrimes case TIOCSETD: 6481541Srgrimes case TIOCSETA: 6491541Srgrimes case TIOCSETAW: 6501541Srgrimes case TIOCSETAF: 6511541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 6521541Srgrimes break; 6531541Srgrimes 6541541Srgrimes case TIOCSIG: 6551541Srgrimes if (*(unsigned int *)data >= NSIG) 6561541Srgrimes return(EINVAL); 6571541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 6581541Srgrimes ttyflush(tp, FREAD|FWRITE); 6591541Srgrimes pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 6601541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 6611541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 6621541Srgrimes ttyinfo(tp); 6631541Srgrimes return(0); 6641541Srgrimes } 6651541Srgrimes error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 6661541Srgrimes if (error < 0) 6671541Srgrimes error = ttioctl(tp, cmd, data, flag); 6681541Srgrimes if (error < 0) { 6691541Srgrimes if (pti->pt_flags & PF_UCNTL && 6701541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 6711541Srgrimes if (cmd & 0xff) { 6721541Srgrimes pti->pt_ucntl = (u_char)cmd; 6731541Srgrimes ptcwakeup(tp, FREAD); 6741541Srgrimes } 6751541Srgrimes return (0); 6761541Srgrimes } 6771541Srgrimes error = ENOTTY; 6781541Srgrimes } 6791541Srgrimes /* 6801541Srgrimes * If external processing and packet mode send ioctl packet. 6811541Srgrimes */ 6821541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 6831541Srgrimes switch(cmd) { 6841541Srgrimes case TIOCSETA: 6851541Srgrimes case TIOCSETAW: 6861541Srgrimes case TIOCSETAF: 6871541Srgrimes#ifdef COMPAT_43 6881541Srgrimes case TIOCSETP: 6891541Srgrimes case TIOCSETN: 6901541Srgrimes#endif 6911541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 6921541Srgrimes case TIOCSETC: 6931541Srgrimes case TIOCSLTC: 6941541Srgrimes case TIOCLBIS: 6951541Srgrimes case TIOCLBIC: 6961541Srgrimes case TIOCLSET: 6971541Srgrimes#endif 6981541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 6991541Srgrimes ptcwakeup(tp, FREAD); 7001541Srgrimes default: 7011541Srgrimes break; 7021541Srgrimes } 7031541Srgrimes } 7048876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 7051541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 7061541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 7071541Srgrimes if (stop) { 7081541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 7091541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 7101541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 7111541Srgrimes ptcwakeup(tp, FREAD); 7121541Srgrimes } 7131541Srgrimes } else { 7141541Srgrimes if (!stop) { 7151541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 7161541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 7171541Srgrimes pti->pt_flags |= PF_NOSTOP; 7181541Srgrimes ptcwakeup(tp, FREAD); 7191541Srgrimes } 7201541Srgrimes } 7211541Srgrimes return (error); 7221541Srgrimes} 723