pty.c revision 12567
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 3412567Sjulian * $Id: tty_pty.c,v 1.27 1995/12/02 07:30:19 julian 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 5512517Sjulian#ifdef DEVFS 5612517Sjulian#include <sys/devfsext.h> 5712517Sjulian#endif /*DEVFS*/ 5812517Sjulian 5911789Sbdevoid ptyattach __P((int n)); 6011789Sbdevoid ptsstart __P((struct tty *tp)); 6111789Sbdevoid ptcwakeup __P((struct tty *tp, int flag)); 6211789Sbde 631541Srgrimes#if NPTY == 1 641541Srgrimes#undef NPTY 651541Srgrimes#define NPTY 32 /* crude XXX */ 661541Srgrimes#endif 671541Srgrimes 681541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 691541Srgrimes 701541Srgrimes/* 7112564Sjulian * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 7212564Sjulian * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 731541Srgrimes */ 741541Srgrimesstruct tty pt_tty[NPTY]; /* XXX */ 751541Srgrimesstruct pt_ioctl { 761541Srgrimes int pt_flags; 771541Srgrimes struct selinfo pt_selr, pt_selw; 781541Srgrimes u_char pt_send; 791541Srgrimes u_char pt_ucntl; 801541Srgrimes} pt_ioctl[NPTY]; /* XXX */ 811541Srgrimesint npty = NPTY; /* for pstat -t */ 821541Srgrimes 831541Srgrimes#define PF_PKT 0x08 /* packet mode */ 841541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 851541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 861541Srgrimes#define PF_NOSTOP 0x40 871541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 881541Srgrimes 891541Srgrimes/* 901541Srgrimes * Establish n (or default if n is 1) ptys in the system. 911541Srgrimes * 921541Srgrimes * XXX cdevsw & pstat require the array `pty[]' to be an array 931541Srgrimes */ 941541Srgrimesvoid 951541Srgrimesptyattach(n) 961541Srgrimes int n; 971541Srgrimes{ 981541Srgrimes#ifdef notyet 991541Srgrimes char *mem; 1001541Srgrimes register u_long ntb; 1011541Srgrimes#define DEFAULT_NPTY 32 1021541Srgrimes 1031541Srgrimes /* maybe should allow 0 => none? */ 1041541Srgrimes if (n <= 1) 1051541Srgrimes n = DEFAULT_NPTY; 1061541Srgrimes ntb = n * sizeof(struct tty); 1071541Srgrimes mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl), 1081541Srgrimes M_DEVBUF, M_WAITOK); 1091541Srgrimes pt_tty = (struct tty *)mem; 1101541Srgrimes mem = (char *)ALIGN(mem + ntb); 1111541Srgrimes pt_ioctl = (struct pt_ioctl *)mem; 1121541Srgrimes npty = n; 1131541Srgrimes#endif 1141541Srgrimes} 1151541Srgrimes 1161541Srgrimes/*ARGSUSED*/ 1171549Srgrimesint 1181541Srgrimesptsopen(dev, flag, devtype, p) 1191541Srgrimes dev_t dev; 1201541Srgrimes int flag, devtype; 1211541Srgrimes struct proc *p; 1221541Srgrimes{ 1231541Srgrimes register struct tty *tp; 1241541Srgrimes int error; 1251541Srgrimes 1261541Srgrimes if (minor(dev) >= npty) 1271541Srgrimes return (ENXIO); 1281541Srgrimes tp = &pt_tty[minor(dev)]; 1291541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 1301541Srgrimes ttychars(tp); /* Set up default chars */ 1311541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 1321541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 1331541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 1341541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 1351541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 1361541Srgrimes ttsetwater(tp); /* would be done in xxparam() */ 1371541Srgrimes } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) 1381541Srgrimes return (EBUSY); 1391541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 1409824Sbde (void)(*linesw[tp->t_line].l_modem)(tp, 1); 1411541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 1421541Srgrimes if (flag&FNONBLOCK) 1431541Srgrimes break; 1449639Sbde error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1459624Sbde "ptsopn", 0); 1463308Sphk if (error) 1471541Srgrimes return (error); 1481541Srgrimes } 1491541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 1507724Sache if (error == 0) 1517724Sache ptcwakeup(tp, FREAD|FWRITE); 1521541Srgrimes return (error); 1531541Srgrimes} 1541541Srgrimes 1551549Srgrimesint 1561541Srgrimesptsclose(dev, flag, mode, p) 1571541Srgrimes dev_t dev; 1581541Srgrimes int flag, mode; 1591541Srgrimes struct proc *p; 1601541Srgrimes{ 1611541Srgrimes register struct tty *tp; 1621541Srgrimes int err; 1631541Srgrimes 1641541Srgrimes tp = &pt_tty[minor(dev)]; 1651541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 1667730Sache ptsstop(tp, FREAD|FWRITE); 1677724Sache (void) ttyclose(tp); 1681541Srgrimes return (err); 1691541Srgrimes} 1701541Srgrimes 1711549Srgrimesint 1721541Srgrimesptsread(dev, uio, flag) 1731541Srgrimes dev_t dev; 1741541Srgrimes struct uio *uio; 1751541Srgrimes int flag; 1761541Srgrimes{ 1771541Srgrimes struct proc *p = curproc; 1781541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 1791541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 1801541Srgrimes int error = 0; 1811541Srgrimes 1821541Srgrimesagain: 1831541Srgrimes if (pti->pt_flags & PF_REMOTE) { 1841541Srgrimes while (isbackground(p, tp)) { 1851541Srgrimes if ((p->p_sigignore & sigmask(SIGTTIN)) || 1861541Srgrimes (p->p_sigmask & sigmask(SIGTTIN)) || 1871541Srgrimes p->p_pgrp->pg_jobc == 0 || 1881541Srgrimes p->p_flag & P_PPWAIT) 1891541Srgrimes return (EIO); 1901541Srgrimes pgsignal(p->p_pgrp, SIGTTIN, 1); 1919624Sbde error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 1929624Sbde 0); 1933308Sphk if (error) 1941541Srgrimes return (error); 1951541Srgrimes } 1961541Srgrimes if (tp->t_canq.c_cc == 0) { 1971541Srgrimes if (flag & IO_NDELAY) 1981541Srgrimes return (EWOULDBLOCK); 1999639Sbde error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 2009624Sbde "ptsin", 0); 2013308Sphk if (error) 2021541Srgrimes return (error); 2031541Srgrimes goto again; 2041541Srgrimes } 2051541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2061541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2071541Srgrimes error = EFAULT; 2081541Srgrimes break; 2091541Srgrimes } 2101541Srgrimes if (tp->t_canq.c_cc == 1) 2111541Srgrimes (void) getc(&tp->t_canq); 2121541Srgrimes if (tp->t_canq.c_cc) 2131541Srgrimes return (error); 2141541Srgrimes } else 2151541Srgrimes if (tp->t_oproc) 2161541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2171541Srgrimes ptcwakeup(tp, FWRITE); 2181541Srgrimes return (error); 2191541Srgrimes} 2201541Srgrimes 2211541Srgrimes/* 2221541Srgrimes * Write to pseudo-tty. 2231541Srgrimes * Wakeups of controlling tty will happen 2241541Srgrimes * indirectly, when tty driver calls ptsstart. 2251541Srgrimes */ 2261549Srgrimesint 2271541Srgrimesptswrite(dev, uio, flag) 2281541Srgrimes dev_t dev; 2291541Srgrimes struct uio *uio; 2301541Srgrimes int flag; 2311541Srgrimes{ 2321541Srgrimes register struct tty *tp; 2331541Srgrimes 2341541Srgrimes tp = &pt_tty[minor(dev)]; 2351541Srgrimes if (tp->t_oproc == 0) 2361541Srgrimes return (EIO); 2371541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2381541Srgrimes} 2391541Srgrimes 2401541Srgrimes/* 2411541Srgrimes * Start output on pseudo-tty. 2421541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 2431541Srgrimes */ 2441541Srgrimesvoid 2451541Srgrimesptsstart(tp) 2461541Srgrimes struct tty *tp; 2471541Srgrimes{ 2481541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 2491541Srgrimes 2501541Srgrimes if (tp->t_state & TS_TTSTOP) 2511541Srgrimes return; 2521541Srgrimes if (pti->pt_flags & PF_STOPPED) { 2531541Srgrimes pti->pt_flags &= ~PF_STOPPED; 2541541Srgrimes pti->pt_send = TIOCPKT_START; 2551541Srgrimes } 2561541Srgrimes ptcwakeup(tp, FREAD); 2571541Srgrimes} 2581541Srgrimes 2591549Srgrimesvoid 2601541Srgrimesptcwakeup(tp, flag) 2611541Srgrimes struct tty *tp; 2621541Srgrimes int flag; 2631541Srgrimes{ 2641541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 2651541Srgrimes 2661541Srgrimes if (flag & FREAD) { 2671541Srgrimes selwakeup(&pti->pt_selr); 2689639Sbde wakeup(TSA_PTC_READ(tp)); 2691541Srgrimes } 2701541Srgrimes if (flag & FWRITE) { 2711541Srgrimes selwakeup(&pti->pt_selw); 2729639Sbde wakeup(TSA_PTC_WRITE(tp)); 2731541Srgrimes } 2741541Srgrimes} 2751541Srgrimes 2761549Srgrimesint 2771541Srgrimesptcopen(dev, flag, devtype, p) 2781541Srgrimes dev_t dev; 2791541Srgrimes int flag, devtype; 2801541Srgrimes struct proc *p; 2811541Srgrimes{ 2821541Srgrimes register struct tty *tp; 2831541Srgrimes struct pt_ioctl *pti; 2841541Srgrimes 2851541Srgrimes if (minor(dev) >= npty) 2861541Srgrimes return (ENXIO); 2871541Srgrimes tp = &pt_tty[minor(dev)]; 2881541Srgrimes if (tp->t_oproc) 2891541Srgrimes return (EIO); 2901541Srgrimes tp->t_oproc = ptsstart; 2911541Srgrimes#ifdef sun4c 2921541Srgrimes tp->t_stop = ptsstop; 2931541Srgrimes#endif 2941541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 2951541Srgrimes tp->t_lflag &= ~EXTPROC; 2961541Srgrimes pti = &pt_ioctl[minor(dev)]; 2971541Srgrimes pti->pt_flags = 0; 2981541Srgrimes pti->pt_send = 0; 2991541Srgrimes pti->pt_ucntl = 0; 3001541Srgrimes return (0); 3011541Srgrimes} 3021541Srgrimes 3031549Srgrimesint 30410624Sbdeptcclose(dev, flags, fmt, p) 3051541Srgrimes dev_t dev; 30610624Sbde int flags; 30710624Sbde int fmt; 30810624Sbde struct proc *p; 3091541Srgrimes{ 3101541Srgrimes register struct tty *tp; 3111541Srgrimes 3121541Srgrimes tp = &pt_tty[minor(dev)]; 3131541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3149824Sbde 3159824Sbde /* 3169824Sbde * XXX MDMBUF makes no sense for ptys but would inhibit the above 3179824Sbde * l_modem(). CLOCAL makes sense but isn't supported. Special 3189824Sbde * l_modem()s that ignore carrier drop make no sense for ptys but 3199824Sbde * may be in use because other parts of the line discipline make 3209824Sbde * sense for ptys. Recover by doing everything that a normal 3219824Sbde * ttymodem() would have done except for sending a SIGHUP. 3229824Sbde */ 3239850Sbde if (tp->t_state & TS_ISOPEN) { 3249850Sbde tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 3259850Sbde tp->t_state |= TS_ZOMBIE; 3269850Sbde ttyflush(tp, FREAD | FWRITE); 3279850Sbde } 3289824Sbde 3291541Srgrimes tp->t_oproc = 0; /* mark closed */ 3301541Srgrimes return (0); 3311541Srgrimes} 3321541Srgrimes 3331549Srgrimesint 3341541Srgrimesptcread(dev, uio, flag) 3351541Srgrimes dev_t dev; 3361541Srgrimes struct uio *uio; 3371541Srgrimes int flag; 3381541Srgrimes{ 3391541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 3401541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 3411541Srgrimes char buf[BUFSIZ]; 3421541Srgrimes int error = 0, cc; 3431541Srgrimes 3441541Srgrimes /* 3451541Srgrimes * We want to block until the slave 3461541Srgrimes * is open, and there's something to read; 3471541Srgrimes * but if we lost the slave or we're NBIO, 3481541Srgrimes * then return the appropriate error instead. 3491541Srgrimes */ 3501541Srgrimes for (;;) { 3511541Srgrimes if (tp->t_state&TS_ISOPEN) { 3521541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 3531541Srgrimes error = ureadc((int)pti->pt_send, uio); 3541541Srgrimes if (error) 3551541Srgrimes return (error); 3561541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 3571541Srgrimes cc = min(uio->uio_resid, 3581541Srgrimes sizeof(tp->t_termios)); 3592807Sbde uiomove((caddr_t)&tp->t_termios, cc, 3602807Sbde uio); 3611541Srgrimes } 3621541Srgrimes pti->pt_send = 0; 3631541Srgrimes return (0); 3641541Srgrimes } 3651541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 3661541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 3671541Srgrimes if (error) 3681541Srgrimes return (error); 3691541Srgrimes pti->pt_ucntl = 0; 3701541Srgrimes return (0); 3711541Srgrimes } 3721541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 3731541Srgrimes break; 3741541Srgrimes } 3759824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 3761541Srgrimes return (0); /* EOF */ 3771541Srgrimes if (flag & IO_NDELAY) 3781541Srgrimes return (EWOULDBLOCK); 3799639Sbde error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 3803308Sphk if (error) 3811541Srgrimes return (error); 3821541Srgrimes } 3831541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 3841541Srgrimes error = ureadc(0, uio); 3851541Srgrimes while (uio->uio_resid > 0 && error == 0) { 3861541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 3871541Srgrimes if (cc <= 0) 3881541Srgrimes break; 3891541Srgrimes error = uiomove(buf, cc, uio); 3901541Srgrimes } 3919626Sbde ttwwakeup(tp); 3921541Srgrimes return (error); 3931541Srgrimes} 3941541Srgrimes 3951541Srgrimesvoid 3961541Srgrimesptsstop(tp, flush) 3971541Srgrimes register struct tty *tp; 3981541Srgrimes int flush; 3991541Srgrimes{ 4001541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 4011541Srgrimes int flag; 4021541Srgrimes 4031541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 4041541Srgrimes if (flush == 0) { 4051541Srgrimes flush = TIOCPKT_STOP; 4061541Srgrimes pti->pt_flags |= PF_STOPPED; 4071541Srgrimes } else 4081541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4091541Srgrimes pti->pt_send |= flush; 4101541Srgrimes /* change of perspective */ 4111541Srgrimes flag = 0; 4121541Srgrimes if (flush & FREAD) 4131541Srgrimes flag |= FWRITE; 4141541Srgrimes if (flush & FWRITE) 4151541Srgrimes flag |= FREAD; 4161541Srgrimes ptcwakeup(tp, flag); 4171541Srgrimes} 4181541Srgrimes 4191549Srgrimesint 4201541Srgrimesptcselect(dev, rw, p) 4211541Srgrimes dev_t dev; 4221541Srgrimes int rw; 4231541Srgrimes struct proc *p; 4241541Srgrimes{ 4251541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 4261541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 4271541Srgrimes int s; 4281541Srgrimes 4299824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 4301541Srgrimes return (1); 4311541Srgrimes switch (rw) { 4321541Srgrimes 4331541Srgrimes case FREAD: 4341541Srgrimes /* 4351541Srgrimes * Need to block timeouts (ttrstart). 4361541Srgrimes */ 4371541Srgrimes s = spltty(); 4381541Srgrimes if ((tp->t_state&TS_ISOPEN) && 4391541Srgrimes tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) { 4401541Srgrimes splx(s); 4411541Srgrimes return (1); 4421541Srgrimes } 4431541Srgrimes splx(s); 4441541Srgrimes /* FALLTHROUGH */ 4451541Srgrimes 4461541Srgrimes case 0: /* exceptional */ 4471541Srgrimes if ((tp->t_state&TS_ISOPEN) && 4483308Sphk ((pti->pt_flags&PF_PKT && pti->pt_send) || 4493308Sphk (pti->pt_flags&PF_UCNTL && pti->pt_ucntl))) 4501541Srgrimes return (1); 4511541Srgrimes selrecord(p, &pti->pt_selr); 4521541Srgrimes break; 4531541Srgrimes 4541541Srgrimes 4551541Srgrimes case FWRITE: 4561541Srgrimes if (tp->t_state&TS_ISOPEN) { 4571541Srgrimes if (pti->pt_flags & PF_REMOTE) { 4581541Srgrimes if (tp->t_canq.c_cc == 0) 4591541Srgrimes return (1); 4601541Srgrimes } else { 4611541Srgrimes if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) 4621541Srgrimes return (1); 4631541Srgrimes if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON)) 4641541Srgrimes return (1); 4651541Srgrimes } 4661541Srgrimes } 4671541Srgrimes selrecord(p, &pti->pt_selw); 4681541Srgrimes break; 4691541Srgrimes 4701541Srgrimes } 4711541Srgrimes return (0); 4721541Srgrimes} 4731541Srgrimes 4741549Srgrimesint 4751541Srgrimesptcwrite(dev, uio, flag) 4761541Srgrimes dev_t dev; 4771541Srgrimes register struct uio *uio; 4781541Srgrimes int flag; 4791541Srgrimes{ 4801541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 4811549Srgrimes register u_char *cp = 0; 4821541Srgrimes register int cc = 0; 4831541Srgrimes u_char locbuf[BUFSIZ]; 4841541Srgrimes int cnt = 0; 4851541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 4861541Srgrimes int error = 0; 4871541Srgrimes 4881541Srgrimesagain: 4891541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 4901541Srgrimes goto block; 4911541Srgrimes if (pti->pt_flags & PF_REMOTE) { 4921541Srgrimes if (tp->t_canq.c_cc) 4931541Srgrimes goto block; 49411789Sbde while ((uio->uio_resid > 0 || cc > 0) && 49511789Sbde tp->t_canq.c_cc < TTYHOG - 1) { 4961541Srgrimes if (cc == 0) { 4971541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 4981541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 4991541Srgrimes cp = locbuf; 5001541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5011541Srgrimes if (error) 5021541Srgrimes return (error); 5031541Srgrimes /* check again for safety */ 50411789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 50511789Sbde /* adjust as usual */ 50611789Sbde uio->uio_resid += cc; 5071541Srgrimes return (EIO); 50811789Sbde } 5091541Srgrimes } 5101541Srgrimes if (cc) 51111789Sbde cc -= b_to_q((char *)cp, cc, &tp->t_canq); 5121541Srgrimes } 51311789Sbde /* adjust for data copied in but not written */ 51411789Sbde uio->uio_resid += cc; 5151541Srgrimes (void) putc(0, &tp->t_canq); 5161541Srgrimes ttwakeup(tp); 5179639Sbde wakeup(TSA_PTS_READ(tp)); 5181541Srgrimes return (0); 5191541Srgrimes } 52011789Sbde while (uio->uio_resid > 0 || cc > 0) { 5211541Srgrimes if (cc == 0) { 5221541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5231541Srgrimes cp = locbuf; 5241541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5251541Srgrimes if (error) 5261541Srgrimes return (error); 5271541Srgrimes /* check again for safety */ 52811789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 52911789Sbde /* adjust for data copied in but not written */ 53011789Sbde uio->uio_resid += cc; 5311541Srgrimes return (EIO); 53211789Sbde } 5331541Srgrimes } 5341541Srgrimes while (cc > 0) { 5351541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 5361541Srgrimes (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 5379824Sbde wakeup(TSA_HUP_OR_INPUT(tp)); 5381541Srgrimes goto block; 5391541Srgrimes } 5401541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 5411541Srgrimes cnt++; 5421541Srgrimes cc--; 5431541Srgrimes } 5441541Srgrimes cc = 0; 5451541Srgrimes } 5461541Srgrimes return (0); 5471541Srgrimesblock: 5481541Srgrimes /* 5491541Srgrimes * Come here to wait for slave to open, for space 5501541Srgrimes * in outq, or space in rawq. 5511541Srgrimes */ 55211789Sbde if ((tp->t_state & TS_CONNECTED) == 0) { 55311789Sbde /* adjust for data copied in but not written */ 55411789Sbde uio->uio_resid += cc; 5551541Srgrimes return (EIO); 55611789Sbde } 5571541Srgrimes if (flag & IO_NDELAY) { 5581541Srgrimes /* adjust for data copied in but not written */ 5591541Srgrimes uio->uio_resid += cc; 5601541Srgrimes if (cnt == 0) 5611541Srgrimes return (EWOULDBLOCK); 5621541Srgrimes return (0); 5631541Srgrimes } 5649639Sbde error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 5653308Sphk if (error) { 5661541Srgrimes /* adjust for data copied in but not written */ 5671541Srgrimes uio->uio_resid += cc; 5681541Srgrimes return (error); 5691541Srgrimes } 5701541Srgrimes goto again; 5711541Srgrimes} 5721541Srgrimes 5736712Spststruct tty * 5746712Spstptydevtotty(dev) 5756712Spst dev_t dev; 5766712Spst{ 5776712Spst if (minor(dev) >= npty) 5786712Spst return (NULL); 5796712Spst 5806712Spst return &pt_tty[minor(dev)]; 5816712Spst} 5826712Spst 5831541Srgrimes/*ARGSUSED*/ 5841549Srgrimesint 5851541Srgrimesptyioctl(dev, cmd, data, flag, p) 5861541Srgrimes dev_t dev; 5871541Srgrimes int cmd; 5881541Srgrimes caddr_t data; 5891541Srgrimes int flag; 5901541Srgrimes struct proc *p; 5911541Srgrimes{ 5921541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 5931541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 5941541Srgrimes register u_char *cc = tp->t_cc; 5951541Srgrimes int stop, error; 5961541Srgrimes 5971541Srgrimes /* 5981541Srgrimes * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 5991541Srgrimes * ttywflush(tp) will hang if there are characters in the outq. 6001541Srgrimes */ 6011541Srgrimes if (cmd == TIOCEXT) { 6021541Srgrimes /* 6031541Srgrimes * When the EXTPROC bit is being toggled, we need 6041541Srgrimes * to send an TIOCPKT_IOCTL if the packet driver 6051541Srgrimes * is turned on. 6061541Srgrimes */ 6071541Srgrimes if (*(int *)data) { 6081541Srgrimes if (pti->pt_flags & PF_PKT) { 6091541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 6101541Srgrimes ptcwakeup(tp, FREAD); 6111541Srgrimes } 6121541Srgrimes tp->t_lflag |= EXTPROC; 6131541Srgrimes } else { 6141541Srgrimes if ((tp->t_state & EXTPROC) && 6151541Srgrimes (pti->pt_flags & PF_PKT)) { 6161541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 6171541Srgrimes ptcwakeup(tp, FREAD); 6181541Srgrimes } 6191541Srgrimes tp->t_lflag &= ~EXTPROC; 6201541Srgrimes } 6211541Srgrimes return(0); 6221541Srgrimes } else 6231541Srgrimes if (cdevsw[major(dev)].d_open == ptcopen) 6241541Srgrimes switch (cmd) { 6251541Srgrimes 6261541Srgrimes case TIOCGPGRP: 6271541Srgrimes /* 6281541Srgrimes * We aviod calling ttioctl on the controller since, 6291541Srgrimes * in that case, tp must be the controlling terminal. 6301541Srgrimes */ 6311541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6321541Srgrimes return (0); 6331541Srgrimes 6341541Srgrimes case TIOCPKT: 6351541Srgrimes if (*(int *)data) { 6361541Srgrimes if (pti->pt_flags & PF_UCNTL) 6371541Srgrimes return (EINVAL); 6381541Srgrimes pti->pt_flags |= PF_PKT; 6391541Srgrimes } else 6401541Srgrimes pti->pt_flags &= ~PF_PKT; 6411541Srgrimes return (0); 6421541Srgrimes 6431541Srgrimes case TIOCUCNTL: 6441541Srgrimes if (*(int *)data) { 6451541Srgrimes if (pti->pt_flags & PF_PKT) 6461541Srgrimes return (EINVAL); 6471541Srgrimes pti->pt_flags |= PF_UCNTL; 6481541Srgrimes } else 6491541Srgrimes pti->pt_flags &= ~PF_UCNTL; 6501541Srgrimes return (0); 6511541Srgrimes 6521541Srgrimes case TIOCREMOTE: 6531541Srgrimes if (*(int *)data) 6541541Srgrimes pti->pt_flags |= PF_REMOTE; 6551541Srgrimes else 6561541Srgrimes pti->pt_flags &= ~PF_REMOTE; 6571541Srgrimes ttyflush(tp, FREAD|FWRITE); 6581541Srgrimes return (0); 6591541Srgrimes 6601541Srgrimes#ifdef COMPAT_43 6618876Srgrimes case TIOCSETP: 6621541Srgrimes case TIOCSETN: 6631541Srgrimes#endif 6641541Srgrimes case TIOCSETD: 6651541Srgrimes case TIOCSETA: 6661541Srgrimes case TIOCSETAW: 6679858Sache case TIOCSETAF: 6681541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 6691541Srgrimes break; 6701541Srgrimes 6711541Srgrimes case TIOCSIG: 6721541Srgrimes if (*(unsigned int *)data >= NSIG) 6731541Srgrimes return(EINVAL); 6741541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 6751541Srgrimes ttyflush(tp, FREAD|FWRITE); 6761541Srgrimes pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 6771541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 6781541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 6791541Srgrimes ttyinfo(tp); 6801541Srgrimes return(0); 6811541Srgrimes } 6821541Srgrimes error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 6831541Srgrimes if (error < 0) 6841541Srgrimes error = ttioctl(tp, cmd, data, flag); 6851541Srgrimes if (error < 0) { 6861541Srgrimes if (pti->pt_flags & PF_UCNTL && 6871541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 6881541Srgrimes if (cmd & 0xff) { 6891541Srgrimes pti->pt_ucntl = (u_char)cmd; 6901541Srgrimes ptcwakeup(tp, FREAD); 6911541Srgrimes } 6921541Srgrimes return (0); 6931541Srgrimes } 6941541Srgrimes error = ENOTTY; 6951541Srgrimes } 6961541Srgrimes /* 6971541Srgrimes * If external processing and packet mode send ioctl packet. 6981541Srgrimes */ 6991541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 7001541Srgrimes switch(cmd) { 7011541Srgrimes case TIOCSETA: 7021541Srgrimes case TIOCSETAW: 7031541Srgrimes case TIOCSETAF: 7041541Srgrimes#ifdef COMPAT_43 7051541Srgrimes case TIOCSETP: 7061541Srgrimes case TIOCSETN: 7071541Srgrimes#endif 7081541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 7091541Srgrimes case TIOCSETC: 7101541Srgrimes case TIOCSLTC: 7111541Srgrimes case TIOCLBIS: 7121541Srgrimes case TIOCLBIC: 7131541Srgrimes case TIOCLSET: 7141541Srgrimes#endif 7151541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 7161541Srgrimes ptcwakeup(tp, FREAD); 7171541Srgrimes default: 7181541Srgrimes break; 7191541Srgrimes } 7201541Srgrimes } 7218876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 7221541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 7231541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 7241541Srgrimes if (stop) { 7251541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 7261541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 7271541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 7281541Srgrimes ptcwakeup(tp, FREAD); 7291541Srgrimes } 7301541Srgrimes } else { 7311541Srgrimes if (!stop) { 7321541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 7331541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 7341541Srgrimes pti->pt_flags |= PF_NOSTOP; 7351541Srgrimes ptcwakeup(tp, FREAD); 7361541Srgrimes } 7371541Srgrimes } 7381541Srgrimes return (error); 7391541Srgrimes} 74012517Sjulian 74112517Sjulian#define CDEV_MAJOR_S 5 74212517Sjulian#define CDEV_MAJOR_C 6 74312517Sjulian#ifdef JREMOD 74412517Sjulianstruct cdevsw pts_cdevsw = 74512517Sjulian { ptsopen, ptsclose, ptsread, ptswrite, /*5*/ 74612517Sjulian ptyioctl, ptsstop, nullreset, ptydevtotty,/* ttyp */ 74712517Sjulian ttselect, nommap, NULL }; 74812517Sjulian 74912517Sjulianstruct cdevsw ptc_cdevsw = 75012517Sjulian { ptcopen, ptcclose, ptcread, ptcwrite, /*6*/ 75112517Sjulian ptyioctl, nullstop, nullreset, ptydevtotty,/* ptyp */ 75212517Sjulian ptcselect, nommap, NULL }; 75312517Sjulian 75412517Sjulianstatic ptc_devsw_installed = 0; 75512517Sjulian 75612517Sjulianstatic void ptc_drvinit(void *unused) 75712517Sjulian{ 75812517Sjulian#ifdef DEVFS 75912567Sjulian int i,j,k; 76012567Sjulian char jnames[] = "pqrsPQRS"; 76112567Sjulian char knames[] = "0123456789abcdefghijklmnopqrstuv"; 76212517Sjulian char devname[16]; 76312564Sjulian#define MAXUNITS (8 * 32) 76412517Sjulian#endif 76512517Sjulian dev_t dev; 76612517Sjulian dev_t dev_c; 76712517Sjulian 76812517Sjulian if( ! ptc_devsw_installed ) { 76912517Sjulian dev = makedev(CDEV_MAJOR_S,0); 77012517Sjulian cdevsw_add(&dev,&pts_cdevsw,NULL); 77112517Sjulian dev_c = makedev(CDEV_MAJOR_C,0); 77212517Sjulian cdevsw_add(&dev_c,&ptc_cdevsw,NULL); 77312517Sjulian ptc_devsw_installed = 1; 77412517Sjulian#ifdef DEVFS 77512517Sjulian/*XXX*/ 77612517Sjulian#if NPTY > MAXUNITS 77712517Sjulian#undef NPTY 77812517Sjulian#define NPTY MAXUNITS 77912517Sjulian#endif 78012521Sjulian for ( i = 0 ; i<NPTY ; i++ ) { 78112567Sjulian void * x; 78212517Sjulian 78312564Sjulian j = i / 32; 78412564Sjulian k = i % 32; 78512521Sjulian sprintf(devname,"pty%c%c",jnames[j],knames[k]); 78612521Sjulian x=devfs_add_devsw("/",devname,major(dev_c),0,DV_CHR,0,0,0600); 78712521Sjulian sprintf(devname,"tty%c%c",jnames[j],knames[k]); 78812521Sjulian x=devfs_add_devsw("/",devname,major(dev),0,DV_CHR,0,0,0600); 78912521Sjulian } 79012521Sjulian#endif 79112517Sjulian } 79212517Sjulian} 79312517Sjulian 79412517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL) 79512517Sjulian 79612517Sjulian#endif /* JREMOD */ 79712517Sjulian 798