pty.c revision 3308
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 343308Sphk * $Id: tty_pty.c,v 1.4 1994/09/15 19:47:16 bde 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); 1471541Srgrimes ptcwakeup(tp, FREAD|FWRITE); 1481541Srgrimes return (error); 1491541Srgrimes} 1501541Srgrimes 1511549Srgrimesint 1521541Srgrimesptsclose(dev, flag, mode, p) 1531541Srgrimes dev_t dev; 1541541Srgrimes int flag, mode; 1551541Srgrimes struct proc *p; 1561541Srgrimes{ 1571541Srgrimes register struct tty *tp; 1581541Srgrimes int err; 1591541Srgrimes 1601541Srgrimes tp = &pt_tty[minor(dev)]; 1611541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 1621541Srgrimes err |= ttyclose(tp); 1631541Srgrimes ptcwakeup(tp, FREAD|FWRITE); 1641541Srgrimes return (err); 1651541Srgrimes} 1661541Srgrimes 1671549Srgrimesint 1681541Srgrimesptsread(dev, uio, flag) 1691541Srgrimes dev_t dev; 1701541Srgrimes struct uio *uio; 1711541Srgrimes int flag; 1721541Srgrimes{ 1731541Srgrimes struct proc *p = curproc; 1741541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 1751541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 1761541Srgrimes int error = 0; 1771541Srgrimes 1781541Srgrimesagain: 1791541Srgrimes if (pti->pt_flags & PF_REMOTE) { 1801541Srgrimes while (isbackground(p, tp)) { 1811541Srgrimes if ((p->p_sigignore & sigmask(SIGTTIN)) || 1821541Srgrimes (p->p_sigmask & sigmask(SIGTTIN)) || 1831541Srgrimes p->p_pgrp->pg_jobc == 0 || 1841541Srgrimes p->p_flag & P_PPWAIT) 1851541Srgrimes return (EIO); 1861541Srgrimes pgsignal(p->p_pgrp, SIGTTIN, 1); 1873308Sphk error = ttysleep(tp, (caddr_t)&lbolt, 1883308Sphk TTIPRI | PCATCH, ttybg, 0); 1893308Sphk if (error) 1901541Srgrimes return (error); 1911541Srgrimes } 1921541Srgrimes if (tp->t_canq.c_cc == 0) { 1931541Srgrimes if (flag & IO_NDELAY) 1941541Srgrimes return (EWOULDBLOCK); 1953308Sphk error = ttysleep(tp, (caddr_t)&tp->t_canq, 1963308Sphk TTIPRI | PCATCH, ttyin, 0); 1973308Sphk if (error) 1981541Srgrimes return (error); 1991541Srgrimes goto again; 2001541Srgrimes } 2011541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2021541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2031541Srgrimes error = EFAULT; 2041541Srgrimes break; 2051541Srgrimes } 2061541Srgrimes if (tp->t_canq.c_cc == 1) 2071541Srgrimes (void) getc(&tp->t_canq); 2081541Srgrimes if (tp->t_canq.c_cc) 2091541Srgrimes return (error); 2101541Srgrimes } else 2111541Srgrimes if (tp->t_oproc) 2121541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2131541Srgrimes ptcwakeup(tp, FWRITE); 2141541Srgrimes return (error); 2151541Srgrimes} 2161541Srgrimes 2171541Srgrimes/* 2181541Srgrimes * Write to pseudo-tty. 2191541Srgrimes * Wakeups of controlling tty will happen 2201541Srgrimes * indirectly, when tty driver calls ptsstart. 2211541Srgrimes */ 2221549Srgrimesint 2231541Srgrimesptswrite(dev, uio, flag) 2241541Srgrimes dev_t dev; 2251541Srgrimes struct uio *uio; 2261541Srgrimes int flag; 2271541Srgrimes{ 2281541Srgrimes register struct tty *tp; 2291541Srgrimes 2301541Srgrimes tp = &pt_tty[minor(dev)]; 2311541Srgrimes if (tp->t_oproc == 0) 2321541Srgrimes return (EIO); 2331541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2341541Srgrimes} 2351541Srgrimes 2361541Srgrimes/* 2371541Srgrimes * Start output on pseudo-tty. 2381541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 2391541Srgrimes */ 2401541Srgrimesvoid 2411541Srgrimesptsstart(tp) 2421541Srgrimes struct tty *tp; 2431541Srgrimes{ 2441541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 2451541Srgrimes 2461541Srgrimes if (tp->t_state & TS_TTSTOP) 2471541Srgrimes return; 2481541Srgrimes if (pti->pt_flags & PF_STOPPED) { 2491541Srgrimes pti->pt_flags &= ~PF_STOPPED; 2501541Srgrimes pti->pt_send = TIOCPKT_START; 2511541Srgrimes } 2521541Srgrimes ptcwakeup(tp, FREAD); 2531541Srgrimes} 2541541Srgrimes 2551549Srgrimesvoid 2561541Srgrimesptcwakeup(tp, flag) 2571541Srgrimes struct tty *tp; 2581541Srgrimes int flag; 2591541Srgrimes{ 2601541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 2611541Srgrimes 2621541Srgrimes if (flag & FREAD) { 2631541Srgrimes selwakeup(&pti->pt_selr); 2641541Srgrimes wakeup((caddr_t)&tp->t_outq.c_cf); 2651541Srgrimes } 2661541Srgrimes if (flag & FWRITE) { 2671541Srgrimes selwakeup(&pti->pt_selw); 2681541Srgrimes wakeup((caddr_t)&tp->t_rawq.c_cf); 2691541Srgrimes } 2701541Srgrimes} 2711541Srgrimes 2721541Srgrimes/*ARGSUSED*/ 2731541Srgrimes#ifdef __STDC__ 2741549Srgrimesint 2751541Srgrimesptcopen(dev_t dev, int flag, int devtype, struct proc *p) 2761541Srgrimes#else 2771549Srgrimesint 2781541Srgrimesptcopen(dev, flag, devtype, p) 2791541Srgrimes dev_t dev; 2801541Srgrimes int flag, devtype; 2811541Srgrimes struct proc *p; 2821541Srgrimes#endif 2831541Srgrimes{ 2841541Srgrimes register struct tty *tp; 2851541Srgrimes struct pt_ioctl *pti; 2861541Srgrimes 2871541Srgrimes if (minor(dev) >= npty) 2881541Srgrimes return (ENXIO); 2891541Srgrimes tp = &pt_tty[minor(dev)]; 2901541Srgrimes if (tp->t_oproc) 2911541Srgrimes return (EIO); 2921541Srgrimes tp->t_oproc = ptsstart; 2931541Srgrimes#ifdef sun4c 2941541Srgrimes tp->t_stop = ptsstop; 2951541Srgrimes#endif 2961541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 2971541Srgrimes tp->t_lflag &= ~EXTPROC; 2981541Srgrimes pti = &pt_ioctl[minor(dev)]; 2991541Srgrimes pti->pt_flags = 0; 3001541Srgrimes pti->pt_send = 0; 3011541Srgrimes pti->pt_ucntl = 0; 3021541Srgrimes return (0); 3031541Srgrimes} 3041541Srgrimes 3051549Srgrimesint 3061541Srgrimesptcclose(dev) 3071541Srgrimes dev_t dev; 3081541Srgrimes{ 3091541Srgrimes register struct tty *tp; 3101541Srgrimes 3111541Srgrimes tp = &pt_tty[minor(dev)]; 3121541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3131541Srgrimes tp->t_state &= ~TS_CARR_ON; 3141541Srgrimes tp->t_oproc = 0; /* mark closed */ 3151541Srgrimes tp->t_session = 0; 3161541Srgrimes return (0); 3171541Srgrimes} 3181541Srgrimes 3191549Srgrimesint 3201541Srgrimesptcread(dev, uio, flag) 3211541Srgrimes dev_t dev; 3221541Srgrimes struct uio *uio; 3231541Srgrimes int flag; 3241541Srgrimes{ 3251541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 3261541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 3271541Srgrimes char buf[BUFSIZ]; 3281541Srgrimes int error = 0, cc; 3291541Srgrimes 3301541Srgrimes /* 3311541Srgrimes * We want to block until the slave 3321541Srgrimes * is open, and there's something to read; 3331541Srgrimes * but if we lost the slave or we're NBIO, 3341541Srgrimes * then return the appropriate error instead. 3351541Srgrimes */ 3361541Srgrimes for (;;) { 3371541Srgrimes if (tp->t_state&TS_ISOPEN) { 3381541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 3391541Srgrimes error = ureadc((int)pti->pt_send, uio); 3401541Srgrimes if (error) 3411541Srgrimes return (error); 3421541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 3431541Srgrimes cc = min(uio->uio_resid, 3441541Srgrimes sizeof(tp->t_termios)); 3452807Sbde uiomove((caddr_t)&tp->t_termios, cc, 3462807Sbde uio); 3471541Srgrimes } 3481541Srgrimes pti->pt_send = 0; 3491541Srgrimes return (0); 3501541Srgrimes } 3511541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 3521541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 3531541Srgrimes if (error) 3541541Srgrimes return (error); 3551541Srgrimes pti->pt_ucntl = 0; 3561541Srgrimes return (0); 3571541Srgrimes } 3581541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 3591541Srgrimes break; 3601541Srgrimes } 3611541Srgrimes if ((tp->t_state&TS_CARR_ON) == 0) 3621541Srgrimes return (0); /* EOF */ 3631541Srgrimes if (flag & IO_NDELAY) 3641541Srgrimes return (EWOULDBLOCK); 3653308Sphk error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH, 3663308Sphk ttyin, 0); 3673308Sphk if (error) 3681541Srgrimes return (error); 3691541Srgrimes } 3701541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 3711541Srgrimes error = ureadc(0, uio); 3721541Srgrimes while (uio->uio_resid > 0 && error == 0) { 3731541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 3741541Srgrimes if (cc <= 0) 3751541Srgrimes break; 3761541Srgrimes error = uiomove(buf, cc, uio); 3771541Srgrimes } 3781541Srgrimes if (tp->t_outq.c_cc <= tp->t_lowat) { 3791541Srgrimes if (tp->t_state&TS_ASLEEP) { 3801541Srgrimes tp->t_state &= ~TS_ASLEEP; 3811541Srgrimes wakeup((caddr_t)&tp->t_outq); 3821541Srgrimes } 3831541Srgrimes selwakeup(&tp->t_wsel); 3841541Srgrimes } 3851541Srgrimes return (error); 3861541Srgrimes} 3871541Srgrimes 3881541Srgrimesvoid 3891541Srgrimesptsstop(tp, flush) 3901541Srgrimes register struct tty *tp; 3911541Srgrimes int flush; 3921541Srgrimes{ 3931541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 3941541Srgrimes int flag; 3951541Srgrimes 3961541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 3971541Srgrimes if (flush == 0) { 3981541Srgrimes flush = TIOCPKT_STOP; 3991541Srgrimes pti->pt_flags |= PF_STOPPED; 4001541Srgrimes } else 4011541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4021541Srgrimes pti->pt_send |= flush; 4031541Srgrimes /* change of perspective */ 4041541Srgrimes flag = 0; 4051541Srgrimes if (flush & FREAD) 4061541Srgrimes flag |= FWRITE; 4071541Srgrimes if (flush & FWRITE) 4081541Srgrimes flag |= FREAD; 4091541Srgrimes ptcwakeup(tp, flag); 4101541Srgrimes} 4111541Srgrimes 4121549Srgrimesint 4131541Srgrimesptcselect(dev, rw, p) 4141541Srgrimes dev_t dev; 4151541Srgrimes int rw; 4161541Srgrimes struct proc *p; 4171541Srgrimes{ 4181541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 4191541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 4201541Srgrimes int s; 4211541Srgrimes 4221541Srgrimes if ((tp->t_state&TS_CARR_ON) == 0) 4231541Srgrimes return (1); 4241541Srgrimes switch (rw) { 4251541Srgrimes 4261541Srgrimes case FREAD: 4271541Srgrimes /* 4281541Srgrimes * Need to block timeouts (ttrstart). 4291541Srgrimes */ 4301541Srgrimes s = spltty(); 4311541Srgrimes if ((tp->t_state&TS_ISOPEN) && 4321541Srgrimes tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) { 4331541Srgrimes splx(s); 4341541Srgrimes return (1); 4351541Srgrimes } 4361541Srgrimes splx(s); 4371541Srgrimes /* FALLTHROUGH */ 4381541Srgrimes 4391541Srgrimes case 0: /* exceptional */ 4401541Srgrimes if ((tp->t_state&TS_ISOPEN) && 4413308Sphk ((pti->pt_flags&PF_PKT && pti->pt_send) || 4423308Sphk (pti->pt_flags&PF_UCNTL && pti->pt_ucntl))) 4431541Srgrimes return (1); 4441541Srgrimes selrecord(p, &pti->pt_selr); 4451541Srgrimes break; 4461541Srgrimes 4471541Srgrimes 4481541Srgrimes case FWRITE: 4491541Srgrimes if (tp->t_state&TS_ISOPEN) { 4501541Srgrimes if (pti->pt_flags & PF_REMOTE) { 4511541Srgrimes if (tp->t_canq.c_cc == 0) 4521541Srgrimes return (1); 4531541Srgrimes } else { 4541541Srgrimes if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) 4551541Srgrimes return (1); 4561541Srgrimes if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON)) 4571541Srgrimes return (1); 4581541Srgrimes } 4591541Srgrimes } 4601541Srgrimes selrecord(p, &pti->pt_selw); 4611541Srgrimes break; 4621541Srgrimes 4631541Srgrimes } 4641541Srgrimes return (0); 4651541Srgrimes} 4661541Srgrimes 4671549Srgrimesint 4681541Srgrimesptcwrite(dev, uio, flag) 4691541Srgrimes dev_t dev; 4701541Srgrimes register struct uio *uio; 4711541Srgrimes int flag; 4721541Srgrimes{ 4731541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 4741549Srgrimes register u_char *cp = 0; 4751541Srgrimes register int cc = 0; 4761541Srgrimes u_char locbuf[BUFSIZ]; 4771541Srgrimes int cnt = 0; 4781541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 4791541Srgrimes int error = 0; 4801541Srgrimes 4811541Srgrimesagain: 4821541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 4831541Srgrimes goto block; 4841541Srgrimes if (pti->pt_flags & PF_REMOTE) { 4851541Srgrimes if (tp->t_canq.c_cc) 4861541Srgrimes goto block; 4871541Srgrimes while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) { 4881541Srgrimes if (cc == 0) { 4891541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 4901541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 4911541Srgrimes cp = locbuf; 4921541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 4931541Srgrimes if (error) 4941541Srgrimes return (error); 4951541Srgrimes /* check again for safety */ 4961541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 4971541Srgrimes return (EIO); 4981541Srgrimes } 4991541Srgrimes if (cc) 5001541Srgrimes (void) b_to_q((char *)cp, cc, &tp->t_canq); 5011541Srgrimes cc = 0; 5021541Srgrimes } 5031541Srgrimes (void) putc(0, &tp->t_canq); 5041541Srgrimes ttwakeup(tp); 5051541Srgrimes wakeup((caddr_t)&tp->t_canq); 5061541Srgrimes return (0); 5071541Srgrimes } 5081541Srgrimes while (uio->uio_resid > 0) { 5091541Srgrimes if (cc == 0) { 5101541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5111541Srgrimes cp = locbuf; 5121541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5131541Srgrimes if (error) 5141541Srgrimes return (error); 5151541Srgrimes /* check again for safety */ 5161541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 5171541Srgrimes return (EIO); 5181541Srgrimes } 5191541Srgrimes while (cc > 0) { 5201541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 5211541Srgrimes (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 5221541Srgrimes wakeup((caddr_t)&tp->t_rawq); 5231541Srgrimes goto block; 5241541Srgrimes } 5251541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 5261541Srgrimes cnt++; 5271541Srgrimes cc--; 5281541Srgrimes } 5291541Srgrimes cc = 0; 5301541Srgrimes } 5311541Srgrimes return (0); 5321541Srgrimesblock: 5331541Srgrimes /* 5341541Srgrimes * Come here to wait for slave to open, for space 5351541Srgrimes * in outq, or space in rawq. 5361541Srgrimes */ 5371541Srgrimes if ((tp->t_state&TS_CARR_ON) == 0) 5381541Srgrimes return (EIO); 5391541Srgrimes if (flag & IO_NDELAY) { 5401541Srgrimes /* adjust for data copied in but not written */ 5411541Srgrimes uio->uio_resid += cc; 5421541Srgrimes if (cnt == 0) 5431541Srgrimes return (EWOULDBLOCK); 5441541Srgrimes return (0); 5451541Srgrimes } 5463308Sphk error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH, ttyout, 0); 5473308Sphk if (error) { 5481541Srgrimes /* adjust for data copied in but not written */ 5491541Srgrimes uio->uio_resid += cc; 5501541Srgrimes return (error); 5511541Srgrimes } 5521541Srgrimes goto again; 5531541Srgrimes} 5541541Srgrimes 5551541Srgrimes/*ARGSUSED*/ 5561549Srgrimesint 5571541Srgrimesptyioctl(dev, cmd, data, flag, p) 5581541Srgrimes dev_t dev; 5591541Srgrimes int cmd; 5601541Srgrimes caddr_t data; 5611541Srgrimes int flag; 5621541Srgrimes struct proc *p; 5631541Srgrimes{ 5641541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 5651541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 5661541Srgrimes register u_char *cc = tp->t_cc; 5671541Srgrimes int stop, error; 5681541Srgrimes 5691541Srgrimes /* 5701541Srgrimes * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 5711541Srgrimes * ttywflush(tp) will hang if there are characters in the outq. 5721541Srgrimes */ 5731541Srgrimes if (cmd == TIOCEXT) { 5741541Srgrimes /* 5751541Srgrimes * When the EXTPROC bit is being toggled, we need 5761541Srgrimes * to send an TIOCPKT_IOCTL if the packet driver 5771541Srgrimes * is turned on. 5781541Srgrimes */ 5791541Srgrimes if (*(int *)data) { 5801541Srgrimes if (pti->pt_flags & PF_PKT) { 5811541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 5821541Srgrimes ptcwakeup(tp, FREAD); 5831541Srgrimes } 5841541Srgrimes tp->t_lflag |= EXTPROC; 5851541Srgrimes } else { 5861541Srgrimes if ((tp->t_state & EXTPROC) && 5871541Srgrimes (pti->pt_flags & PF_PKT)) { 5881541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 5891541Srgrimes ptcwakeup(tp, FREAD); 5901541Srgrimes } 5911541Srgrimes tp->t_lflag &= ~EXTPROC; 5921541Srgrimes } 5931541Srgrimes return(0); 5941541Srgrimes } else 5951541Srgrimes if (cdevsw[major(dev)].d_open == ptcopen) 5961541Srgrimes switch (cmd) { 5971541Srgrimes 5981541Srgrimes case TIOCGPGRP: 5991541Srgrimes /* 6001541Srgrimes * We aviod calling ttioctl on the controller since, 6011541Srgrimes * in that case, tp must be the controlling terminal. 6021541Srgrimes */ 6031541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6041541Srgrimes return (0); 6051541Srgrimes 6061541Srgrimes case TIOCPKT: 6071541Srgrimes if (*(int *)data) { 6081541Srgrimes if (pti->pt_flags & PF_UCNTL) 6091541Srgrimes return (EINVAL); 6101541Srgrimes pti->pt_flags |= PF_PKT; 6111541Srgrimes } else 6121541Srgrimes pti->pt_flags &= ~PF_PKT; 6131541Srgrimes return (0); 6141541Srgrimes 6151541Srgrimes case TIOCUCNTL: 6161541Srgrimes if (*(int *)data) { 6171541Srgrimes if (pti->pt_flags & PF_PKT) 6181541Srgrimes return (EINVAL); 6191541Srgrimes pti->pt_flags |= PF_UCNTL; 6201541Srgrimes } else 6211541Srgrimes pti->pt_flags &= ~PF_UCNTL; 6221541Srgrimes return (0); 6231541Srgrimes 6241541Srgrimes case TIOCREMOTE: 6251541Srgrimes if (*(int *)data) 6261541Srgrimes pti->pt_flags |= PF_REMOTE; 6271541Srgrimes else 6281541Srgrimes pti->pt_flags &= ~PF_REMOTE; 6291541Srgrimes ttyflush(tp, FREAD|FWRITE); 6301541Srgrimes return (0); 6311541Srgrimes 6321541Srgrimes#ifdef COMPAT_43 6331541Srgrimes case TIOCSETP: 6341541Srgrimes case TIOCSETN: 6351541Srgrimes#endif 6361541Srgrimes case TIOCSETD: 6371541Srgrimes case TIOCSETA: 6381541Srgrimes case TIOCSETAW: 6391541Srgrimes case TIOCSETAF: 6401541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 6411541Srgrimes break; 6421541Srgrimes 6431541Srgrimes case TIOCSIG: 6441541Srgrimes if (*(unsigned int *)data >= NSIG) 6451541Srgrimes return(EINVAL); 6461541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 6471541Srgrimes ttyflush(tp, FREAD|FWRITE); 6481541Srgrimes pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 6491541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 6501541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 6511541Srgrimes ttyinfo(tp); 6521541Srgrimes return(0); 6531541Srgrimes } 6541541Srgrimes error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 6551541Srgrimes if (error < 0) 6561541Srgrimes error = ttioctl(tp, cmd, data, flag); 6571541Srgrimes if (error < 0) { 6581541Srgrimes if (pti->pt_flags & PF_UCNTL && 6591541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 6601541Srgrimes if (cmd & 0xff) { 6611541Srgrimes pti->pt_ucntl = (u_char)cmd; 6621541Srgrimes ptcwakeup(tp, FREAD); 6631541Srgrimes } 6641541Srgrimes return (0); 6651541Srgrimes } 6661541Srgrimes error = ENOTTY; 6671541Srgrimes } 6681541Srgrimes /* 6691541Srgrimes * If external processing and packet mode send ioctl packet. 6701541Srgrimes */ 6711541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 6721541Srgrimes switch(cmd) { 6731541Srgrimes case TIOCSETA: 6741541Srgrimes case TIOCSETAW: 6751541Srgrimes case TIOCSETAF: 6761541Srgrimes#ifdef COMPAT_43 6771541Srgrimes case TIOCSETP: 6781541Srgrimes case TIOCSETN: 6791541Srgrimes#endif 6801541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 6811541Srgrimes case TIOCSETC: 6821541Srgrimes case TIOCSLTC: 6831541Srgrimes case TIOCLBIS: 6841541Srgrimes case TIOCLBIC: 6851541Srgrimes case TIOCLSET: 6861541Srgrimes#endif 6871541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 6881541Srgrimes ptcwakeup(tp, FREAD); 6891541Srgrimes default: 6901541Srgrimes break; 6911541Srgrimes } 6921541Srgrimes } 6931541Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 6941541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 6951541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 6961541Srgrimes if (stop) { 6971541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 6981541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 6991541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 7001541Srgrimes ptcwakeup(tp, FREAD); 7011541Srgrimes } 7021541Srgrimes } else { 7031541Srgrimes if (!stop) { 7041541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 7051541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 7061541Srgrimes pti->pt_flags |= PF_NOSTOP; 7071541Srgrimes ptcwakeup(tp, FREAD); 7081541Srgrimes } 7091541Srgrimes } 7101541Srgrimes return (error); 7111541Srgrimes} 712