pty.c revision 16322
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 * 3314511Shsu * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95 3416322Sgpalmer * $Id: tty_pty.c,v 1.38 1996/04/11 18:43:37 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 5512517Sjulian#ifdef DEVFS 5612517Sjulian#include <sys/devfsext.h> 5712517Sjulian#endif /*DEVFS*/ 5812517Sjulian 5916322Sgpalmer#ifdef notyet 6012819Sphkstatic void ptyattach __P((int n)); 6116322Sgpalmer#endif 6212819Sphkstatic void ptsstart __P((struct tty *tp)); 6312819Sphkstatic void ptcwakeup __P((struct tty *tp, int flag)); 6411789Sbde 6512675Sjulianstatic d_open_t ptsopen; 6612675Sjulianstatic d_close_t ptsclose; 6712675Sjulianstatic d_read_t ptsread; 6812675Sjulianstatic d_write_t ptswrite; 6912675Sjulianstatic d_ioctl_t ptyioctl; 7012675Sjulianstatic d_stop_t ptsstop; 7112731Sbdestatic d_devtotty_t ptydevtotty; 7212675Sjulianstatic d_open_t ptcopen; 7312675Sjulianstatic d_close_t ptcclose; 7412675Sjulianstatic d_read_t ptcread; 7512675Sjulianstatic d_write_t ptcwrite; 7612675Sjulianstatic d_select_t ptcselect; 7712675Sjulian 7812675Sjulian#define CDEV_MAJOR_S 5 7912675Sjulian#define CDEV_MAJOR_C 6 8012678Sphkstatic struct cdevsw pts_cdevsw = 8112675Sjulian { ptsopen, ptsclose, ptsread, ptswrite, /*5*/ 8212675Sjulian ptyioctl, ptsstop, nullreset, ptydevtotty,/* ttyp */ 8312675Sjulian ttselect, nommap, NULL, "pts", NULL, -1 }; 8412675Sjulian 8512678Sphkstatic struct cdevsw ptc_cdevsw = 8612675Sjulian { ptcopen, ptcclose, ptcread, ptcwrite, /*6*/ 8712675Sjulian ptyioctl, nullstop, nullreset, ptydevtotty,/* ptyp */ 8812675Sjulian ptcselect, nommap, NULL, "ptc", NULL, -1 }; 8912675Sjulian 9012675Sjulian 911541Srgrimes#if NPTY == 1 921541Srgrimes#undef NPTY 931541Srgrimes#define NPTY 32 /* crude XXX */ 941541Srgrimes#endif 951541Srgrimes 961541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 971541Srgrimes 981541Srgrimes/* 9912564Sjulian * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 10012564Sjulian * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 1011541Srgrimes */ 10212819Sphkstatic struct tty pt_tty[NPTY]; /* XXX */ 10312819Sphkstatic struct pt_ioctl { 1041541Srgrimes int pt_flags; 1051541Srgrimes struct selinfo pt_selr, pt_selw; 1061541Srgrimes u_char pt_send; 1071541Srgrimes u_char pt_ucntl; 1081541Srgrimes} pt_ioctl[NPTY]; /* XXX */ 10912819Sphkstatic int npty = NPTY; /* for pstat -t */ 1101541Srgrimes 1111541Srgrimes#define PF_PKT 0x08 /* packet mode */ 1121541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 1131541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 1141541Srgrimes#define PF_NOSTOP 0x40 1151541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 1161541Srgrimes 11716322Sgpalmer#ifdef notyet 1181541Srgrimes/* 1191541Srgrimes * Establish n (or default if n is 1) ptys in the system. 1201541Srgrimes * 1211541Srgrimes * XXX cdevsw & pstat require the array `pty[]' to be an array 1221541Srgrimes */ 12312819Sphkstatic void 1241541Srgrimesptyattach(n) 1251541Srgrimes int n; 1261541Srgrimes{ 1271541Srgrimes char *mem; 1281541Srgrimes register u_long ntb; 1291541Srgrimes#define DEFAULT_NPTY 32 1301541Srgrimes 1311541Srgrimes /* maybe should allow 0 => none? */ 1321541Srgrimes if (n <= 1) 1331541Srgrimes n = DEFAULT_NPTY; 1341541Srgrimes ntb = n * sizeof(struct tty); 1351541Srgrimes mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl), 1361541Srgrimes M_DEVBUF, M_WAITOK); 1371541Srgrimes pt_tty = (struct tty *)mem; 1381541Srgrimes mem = (char *)ALIGN(mem + ntb); 1391541Srgrimes pt_ioctl = (struct pt_ioctl *)mem; 1401541Srgrimes npty = n; 14116322Sgpalmer} 1421541Srgrimes#endif 1431541Srgrimes 1441541Srgrimes/*ARGSUSED*/ 14512675Sjulianstatic int 1461541Srgrimesptsopen(dev, flag, devtype, p) 1471541Srgrimes dev_t dev; 1481541Srgrimes int flag, devtype; 1491541Srgrimes struct proc *p; 1501541Srgrimes{ 1511541Srgrimes register struct tty *tp; 1521541Srgrimes int error; 1531541Srgrimes 1541541Srgrimes if (minor(dev) >= npty) 1551541Srgrimes return (ENXIO); 1561541Srgrimes tp = &pt_tty[minor(dev)]; 1571541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 1581541Srgrimes ttychars(tp); /* Set up default chars */ 1591541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 1601541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 1611541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 1621541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 1631541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 1641541Srgrimes ttsetwater(tp); /* would be done in xxparam() */ 1651541Srgrimes } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) 1661541Srgrimes return (EBUSY); 1671541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 1689824Sbde (void)(*linesw[tp->t_line].l_modem)(tp, 1); 1691541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 1701541Srgrimes if (flag&FNONBLOCK) 1711541Srgrimes break; 1729639Sbde error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1739624Sbde "ptsopn", 0); 1743308Sphk if (error) 1751541Srgrimes return (error); 1761541Srgrimes } 1771541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 1787724Sache if (error == 0) 1797724Sache ptcwakeup(tp, FREAD|FWRITE); 1801541Srgrimes return (error); 1811541Srgrimes} 1821541Srgrimes 18312675Sjulianstatic int 1841541Srgrimesptsclose(dev, flag, mode, p) 1851541Srgrimes dev_t dev; 1861541Srgrimes int flag, mode; 1871541Srgrimes struct proc *p; 1881541Srgrimes{ 1891541Srgrimes register struct tty *tp; 1901541Srgrimes int err; 1911541Srgrimes 1921541Srgrimes tp = &pt_tty[minor(dev)]; 1931541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 1947730Sache ptsstop(tp, FREAD|FWRITE); 1957724Sache (void) ttyclose(tp); 1961541Srgrimes return (err); 1971541Srgrimes} 1981541Srgrimes 19912675Sjulianstatic int 2001541Srgrimesptsread(dev, uio, flag) 2011541Srgrimes dev_t dev; 2021541Srgrimes struct uio *uio; 2031541Srgrimes int flag; 2041541Srgrimes{ 2051541Srgrimes struct proc *p = curproc; 2061541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 2071541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 2081541Srgrimes int error = 0; 2091541Srgrimes 2101541Srgrimesagain: 2111541Srgrimes if (pti->pt_flags & PF_REMOTE) { 2121541Srgrimes while (isbackground(p, tp)) { 2131541Srgrimes if ((p->p_sigignore & sigmask(SIGTTIN)) || 2141541Srgrimes (p->p_sigmask & sigmask(SIGTTIN)) || 2151541Srgrimes p->p_pgrp->pg_jobc == 0 || 2161541Srgrimes p->p_flag & P_PPWAIT) 2171541Srgrimes return (EIO); 2181541Srgrimes pgsignal(p->p_pgrp, SIGTTIN, 1); 2199624Sbde error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 2209624Sbde 0); 2213308Sphk if (error) 2221541Srgrimes return (error); 2231541Srgrimes } 2241541Srgrimes if (tp->t_canq.c_cc == 0) { 2251541Srgrimes if (flag & IO_NDELAY) 2261541Srgrimes return (EWOULDBLOCK); 2279639Sbde error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 2289624Sbde "ptsin", 0); 2293308Sphk if (error) 2301541Srgrimes return (error); 2311541Srgrimes goto again; 2321541Srgrimes } 2331541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2341541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2351541Srgrimes error = EFAULT; 2361541Srgrimes break; 2371541Srgrimes } 2381541Srgrimes if (tp->t_canq.c_cc == 1) 2391541Srgrimes (void) getc(&tp->t_canq); 2401541Srgrimes if (tp->t_canq.c_cc) 2411541Srgrimes return (error); 2421541Srgrimes } else 2431541Srgrimes if (tp->t_oproc) 2441541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2451541Srgrimes ptcwakeup(tp, FWRITE); 2461541Srgrimes return (error); 2471541Srgrimes} 2481541Srgrimes 2491541Srgrimes/* 2501541Srgrimes * Write to pseudo-tty. 2511541Srgrimes * Wakeups of controlling tty will happen 2521541Srgrimes * indirectly, when tty driver calls ptsstart. 2531541Srgrimes */ 25412675Sjulianstatic int 2551541Srgrimesptswrite(dev, uio, flag) 2561541Srgrimes dev_t dev; 2571541Srgrimes struct uio *uio; 2581541Srgrimes int flag; 2591541Srgrimes{ 2601541Srgrimes register struct tty *tp; 2611541Srgrimes 2621541Srgrimes tp = &pt_tty[minor(dev)]; 2631541Srgrimes if (tp->t_oproc == 0) 2641541Srgrimes return (EIO); 2651541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2661541Srgrimes} 2671541Srgrimes 2681541Srgrimes/* 2691541Srgrimes * Start output on pseudo-tty. 2701541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 2711541Srgrimes */ 27212819Sphkstatic void 2731541Srgrimesptsstart(tp) 2741541Srgrimes struct tty *tp; 2751541Srgrimes{ 2761541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 2771541Srgrimes 2781541Srgrimes if (tp->t_state & TS_TTSTOP) 2791541Srgrimes return; 2801541Srgrimes if (pti->pt_flags & PF_STOPPED) { 2811541Srgrimes pti->pt_flags &= ~PF_STOPPED; 2821541Srgrimes pti->pt_send = TIOCPKT_START; 2831541Srgrimes } 2841541Srgrimes ptcwakeup(tp, FREAD); 2851541Srgrimes} 2861541Srgrimes 28712819Sphkstatic void 2881541Srgrimesptcwakeup(tp, flag) 2891541Srgrimes struct tty *tp; 2901541Srgrimes int flag; 2911541Srgrimes{ 2921541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 2931541Srgrimes 2941541Srgrimes if (flag & FREAD) { 2951541Srgrimes selwakeup(&pti->pt_selr); 2969639Sbde wakeup(TSA_PTC_READ(tp)); 2971541Srgrimes } 2981541Srgrimes if (flag & FWRITE) { 2991541Srgrimes selwakeup(&pti->pt_selw); 3009639Sbde wakeup(TSA_PTC_WRITE(tp)); 3011541Srgrimes } 3021541Srgrimes} 3031541Srgrimes 30412675Sjulianstatic int 3051541Srgrimesptcopen(dev, flag, devtype, p) 3061541Srgrimes dev_t dev; 3071541Srgrimes int flag, devtype; 3081541Srgrimes struct proc *p; 3091541Srgrimes{ 3101541Srgrimes register struct tty *tp; 3111541Srgrimes struct pt_ioctl *pti; 3121541Srgrimes 3131541Srgrimes if (minor(dev) >= npty) 3141541Srgrimes return (ENXIO); 3151541Srgrimes tp = &pt_tty[minor(dev)]; 3161541Srgrimes if (tp->t_oproc) 3171541Srgrimes return (EIO); 3181541Srgrimes tp->t_oproc = ptsstart; 3191541Srgrimes#ifdef sun4c 3201541Srgrimes tp->t_stop = ptsstop; 3211541Srgrimes#endif 3221541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 3231541Srgrimes tp->t_lflag &= ~EXTPROC; 3241541Srgrimes pti = &pt_ioctl[minor(dev)]; 3251541Srgrimes pti->pt_flags = 0; 3261541Srgrimes pti->pt_send = 0; 3271541Srgrimes pti->pt_ucntl = 0; 3281541Srgrimes return (0); 3291541Srgrimes} 3301541Srgrimes 33112675Sjulianstatic int 33210624Sbdeptcclose(dev, flags, fmt, p) 3331541Srgrimes dev_t dev; 33410624Sbde int flags; 33510624Sbde int fmt; 33610624Sbde struct proc *p; 3371541Srgrimes{ 3381541Srgrimes register struct tty *tp; 3391541Srgrimes 3401541Srgrimes tp = &pt_tty[minor(dev)]; 3411541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3429824Sbde 3439824Sbde /* 3449824Sbde * XXX MDMBUF makes no sense for ptys but would inhibit the above 3459824Sbde * l_modem(). CLOCAL makes sense but isn't supported. Special 3469824Sbde * l_modem()s that ignore carrier drop make no sense for ptys but 3479824Sbde * may be in use because other parts of the line discipline make 3489824Sbde * sense for ptys. Recover by doing everything that a normal 3499824Sbde * ttymodem() would have done except for sending a SIGHUP. 3509824Sbde */ 3519850Sbde if (tp->t_state & TS_ISOPEN) { 3529850Sbde tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 3539850Sbde tp->t_state |= TS_ZOMBIE; 3549850Sbde ttyflush(tp, FREAD | FWRITE); 3559850Sbde } 3569824Sbde 3571541Srgrimes tp->t_oproc = 0; /* mark closed */ 3581541Srgrimes return (0); 3591541Srgrimes} 3601541Srgrimes 36112675Sjulianstatic int 3621541Srgrimesptcread(dev, uio, flag) 3631541Srgrimes dev_t dev; 3641541Srgrimes struct uio *uio; 3651541Srgrimes int flag; 3661541Srgrimes{ 3671541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 3681541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 3691541Srgrimes char buf[BUFSIZ]; 3701541Srgrimes int error = 0, cc; 3711541Srgrimes 3721541Srgrimes /* 3731541Srgrimes * We want to block until the slave 3741541Srgrimes * is open, and there's something to read; 3751541Srgrimes * but if we lost the slave or we're NBIO, 3761541Srgrimes * then return the appropriate error instead. 3771541Srgrimes */ 3781541Srgrimes for (;;) { 3791541Srgrimes if (tp->t_state&TS_ISOPEN) { 3801541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 3811541Srgrimes error = ureadc((int)pti->pt_send, uio); 3821541Srgrimes if (error) 3831541Srgrimes return (error); 3841541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 3851541Srgrimes cc = min(uio->uio_resid, 3861541Srgrimes sizeof(tp->t_termios)); 3872807Sbde uiomove((caddr_t)&tp->t_termios, cc, 3882807Sbde uio); 3891541Srgrimes } 3901541Srgrimes pti->pt_send = 0; 3911541Srgrimes return (0); 3921541Srgrimes } 3931541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 3941541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 3951541Srgrimes if (error) 3961541Srgrimes return (error); 3971541Srgrimes pti->pt_ucntl = 0; 3981541Srgrimes return (0); 3991541Srgrimes } 4001541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 4011541Srgrimes break; 4021541Srgrimes } 4039824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 4041541Srgrimes return (0); /* EOF */ 4051541Srgrimes if (flag & IO_NDELAY) 4061541Srgrimes return (EWOULDBLOCK); 4079639Sbde error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 4083308Sphk if (error) 4091541Srgrimes return (error); 4101541Srgrimes } 4111541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 4121541Srgrimes error = ureadc(0, uio); 4131541Srgrimes while (uio->uio_resid > 0 && error == 0) { 4141541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 4151541Srgrimes if (cc <= 0) 4161541Srgrimes break; 4171541Srgrimes error = uiomove(buf, cc, uio); 4181541Srgrimes } 4199626Sbde ttwwakeup(tp); 4201541Srgrimes return (error); 4211541Srgrimes} 4221541Srgrimes 42312675Sjulianstatic void 4241541Srgrimesptsstop(tp, flush) 4251541Srgrimes register struct tty *tp; 4261541Srgrimes int flush; 4271541Srgrimes{ 4281541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 4291541Srgrimes int flag; 4301541Srgrimes 4311541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 4321541Srgrimes if (flush == 0) { 4331541Srgrimes flush = TIOCPKT_STOP; 4341541Srgrimes pti->pt_flags |= PF_STOPPED; 4351541Srgrimes } else 4361541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4371541Srgrimes pti->pt_send |= flush; 4381541Srgrimes /* change of perspective */ 4391541Srgrimes flag = 0; 4401541Srgrimes if (flush & FREAD) 4411541Srgrimes flag |= FWRITE; 4421541Srgrimes if (flush & FWRITE) 4431541Srgrimes flag |= FREAD; 4441541Srgrimes ptcwakeup(tp, flag); 4451541Srgrimes} 4461541Srgrimes 44712675Sjulianstatic int 4481541Srgrimesptcselect(dev, rw, p) 4491541Srgrimes dev_t dev; 4501541Srgrimes int rw; 4511541Srgrimes struct proc *p; 4521541Srgrimes{ 4531541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 4541541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 4551541Srgrimes int s; 4561541Srgrimes 4579824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 4581541Srgrimes return (1); 4591541Srgrimes switch (rw) { 4601541Srgrimes 4611541Srgrimes case FREAD: 4621541Srgrimes /* 4631541Srgrimes * Need to block timeouts (ttrstart). 4641541Srgrimes */ 4651541Srgrimes s = spltty(); 4661541Srgrimes if ((tp->t_state&TS_ISOPEN) && 4671541Srgrimes tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) { 4681541Srgrimes splx(s); 4691541Srgrimes return (1); 4701541Srgrimes } 4711541Srgrimes splx(s); 4721541Srgrimes /* FALLTHROUGH */ 4731541Srgrimes 4741541Srgrimes case 0: /* exceptional */ 4751541Srgrimes if ((tp->t_state&TS_ISOPEN) && 4763308Sphk ((pti->pt_flags&PF_PKT && pti->pt_send) || 4773308Sphk (pti->pt_flags&PF_UCNTL && pti->pt_ucntl))) 4781541Srgrimes return (1); 4791541Srgrimes selrecord(p, &pti->pt_selr); 4801541Srgrimes break; 4811541Srgrimes 4821541Srgrimes 4831541Srgrimes case FWRITE: 4841541Srgrimes if (tp->t_state&TS_ISOPEN) { 4851541Srgrimes if (pti->pt_flags & PF_REMOTE) { 4861541Srgrimes if (tp->t_canq.c_cc == 0) 4871541Srgrimes return (1); 4881541Srgrimes } else { 4891541Srgrimes if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) 4901541Srgrimes return (1); 4911541Srgrimes if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON)) 4921541Srgrimes return (1); 4931541Srgrimes } 4941541Srgrimes } 4951541Srgrimes selrecord(p, &pti->pt_selw); 4961541Srgrimes break; 4971541Srgrimes 4981541Srgrimes } 4991541Srgrimes return (0); 5001541Srgrimes} 5011541Srgrimes 50212675Sjulianstatic int 5031541Srgrimesptcwrite(dev, uio, flag) 5041541Srgrimes dev_t dev; 5051541Srgrimes register struct uio *uio; 5061541Srgrimes int flag; 5071541Srgrimes{ 5081541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 5091549Srgrimes register u_char *cp = 0; 5101541Srgrimes register int cc = 0; 5111541Srgrimes u_char locbuf[BUFSIZ]; 5121541Srgrimes int cnt = 0; 5131541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 5141541Srgrimes int error = 0; 5151541Srgrimes 5161541Srgrimesagain: 5171541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 5181541Srgrimes goto block; 5191541Srgrimes if (pti->pt_flags & PF_REMOTE) { 5201541Srgrimes if (tp->t_canq.c_cc) 5211541Srgrimes goto block; 52211789Sbde while ((uio->uio_resid > 0 || cc > 0) && 52311789Sbde tp->t_canq.c_cc < TTYHOG - 1) { 5241541Srgrimes if (cc == 0) { 5251541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5261541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 5271541Srgrimes cp = locbuf; 5281541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5291541Srgrimes if (error) 5301541Srgrimes return (error); 5311541Srgrimes /* check again for safety */ 53211789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 53311789Sbde /* adjust as usual */ 53411789Sbde uio->uio_resid += cc; 5351541Srgrimes return (EIO); 53611789Sbde } 5371541Srgrimes } 53815199Sbde if (cc > 0) { 53915199Sbde cc = b_to_q((char *)cp, cc, &tp->t_canq); 54015199Sbde /* 54115199Sbde * XXX we don't guarantee that the canq size 54215199Sbde * is >= TTYHOG, so the above b_to_q() may 54315199Sbde * leave some bytes uncopied. However, space 54415199Sbde * is guaranteed for the null terminator if 54515199Sbde * we don't fail here since (TTYHOG - 1) is 54615199Sbde * not a multiple of CBSIZE. 54715199Sbde */ 54815199Sbde if (cc > 0) 54915199Sbde break; 55015199Sbde } 5511541Srgrimes } 55211789Sbde /* adjust for data copied in but not written */ 55311789Sbde uio->uio_resid += cc; 5541541Srgrimes (void) putc(0, &tp->t_canq); 5551541Srgrimes ttwakeup(tp); 5569639Sbde wakeup(TSA_PTS_READ(tp)); 5571541Srgrimes return (0); 5581541Srgrimes } 55911789Sbde while (uio->uio_resid > 0 || cc > 0) { 5601541Srgrimes if (cc == 0) { 5611541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5621541Srgrimes cp = locbuf; 5631541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5641541Srgrimes if (error) 5651541Srgrimes return (error); 5661541Srgrimes /* check again for safety */ 56711789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 56811789Sbde /* adjust for data copied in but not written */ 56911789Sbde uio->uio_resid += cc; 5701541Srgrimes return (EIO); 57111789Sbde } 5721541Srgrimes } 5731541Srgrimes while (cc > 0) { 5741541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 5751541Srgrimes (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 5769824Sbde wakeup(TSA_HUP_OR_INPUT(tp)); 5771541Srgrimes goto block; 5781541Srgrimes } 5791541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 5801541Srgrimes cnt++; 5811541Srgrimes cc--; 5821541Srgrimes } 5831541Srgrimes cc = 0; 5841541Srgrimes } 5851541Srgrimes return (0); 5861541Srgrimesblock: 5871541Srgrimes /* 5881541Srgrimes * Come here to wait for slave to open, for space 58915199Sbde * in outq, or space in rawq, or an empty canq. 5901541Srgrimes */ 59111789Sbde if ((tp->t_state & TS_CONNECTED) == 0) { 59211789Sbde /* adjust for data copied in but not written */ 59311789Sbde uio->uio_resid += cc; 5941541Srgrimes return (EIO); 59511789Sbde } 5961541Srgrimes if (flag & IO_NDELAY) { 5971541Srgrimes /* adjust for data copied in but not written */ 5981541Srgrimes uio->uio_resid += cc; 5991541Srgrimes if (cnt == 0) 6001541Srgrimes return (EWOULDBLOCK); 6011541Srgrimes return (0); 6021541Srgrimes } 6039639Sbde error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 6043308Sphk if (error) { 6051541Srgrimes /* adjust for data copied in but not written */ 6061541Srgrimes uio->uio_resid += cc; 6071541Srgrimes return (error); 6081541Srgrimes } 6091541Srgrimes goto again; 6101541Srgrimes} 6111541Srgrimes 61212675Sjulianstatic struct tty * 6136712Spstptydevtotty(dev) 6146712Spst dev_t dev; 6156712Spst{ 6166712Spst if (minor(dev) >= npty) 6176712Spst return (NULL); 6186712Spst 6196712Spst return &pt_tty[minor(dev)]; 6206712Spst} 6216712Spst 6221541Srgrimes/*ARGSUSED*/ 62312675Sjulianstatic int 6241541Srgrimesptyioctl(dev, cmd, data, flag, p) 6251541Srgrimes dev_t dev; 6261541Srgrimes int cmd; 6271541Srgrimes caddr_t data; 6281541Srgrimes int flag; 6291541Srgrimes struct proc *p; 6301541Srgrimes{ 6311541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 6321541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 6331541Srgrimes register u_char *cc = tp->t_cc; 6341541Srgrimes int stop, error; 6351541Srgrimes 6361541Srgrimes /* 6371541Srgrimes * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 6381541Srgrimes * ttywflush(tp) will hang if there are characters in the outq. 6391541Srgrimes */ 6401541Srgrimes if (cmd == TIOCEXT) { 6411541Srgrimes /* 6421541Srgrimes * When the EXTPROC bit is being toggled, we need 6431541Srgrimes * to send an TIOCPKT_IOCTL if the packet driver 6441541Srgrimes * is turned on. 6451541Srgrimes */ 6461541Srgrimes if (*(int *)data) { 6471541Srgrimes if (pti->pt_flags & PF_PKT) { 6481541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 6491541Srgrimes ptcwakeup(tp, FREAD); 6501541Srgrimes } 6511541Srgrimes tp->t_lflag |= EXTPROC; 6521541Srgrimes } else { 65314511Shsu if ((tp->t_lflag & EXTPROC) && 6541541Srgrimes (pti->pt_flags & PF_PKT)) { 6551541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 6561541Srgrimes ptcwakeup(tp, FREAD); 6571541Srgrimes } 6581541Srgrimes tp->t_lflag &= ~EXTPROC; 6591541Srgrimes } 6601541Srgrimes return(0); 6611541Srgrimes } else 66212813Sjulian if (cdevsw[major(dev)]->d_open == ptcopen) 6631541Srgrimes switch (cmd) { 6641541Srgrimes 6651541Srgrimes case TIOCGPGRP: 6661541Srgrimes /* 6671541Srgrimes * We aviod calling ttioctl on the controller since, 6681541Srgrimes * in that case, tp must be the controlling terminal. 6691541Srgrimes */ 6701541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6711541Srgrimes return (0); 6721541Srgrimes 6731541Srgrimes case TIOCPKT: 6741541Srgrimes if (*(int *)data) { 6751541Srgrimes if (pti->pt_flags & PF_UCNTL) 6761541Srgrimes return (EINVAL); 6771541Srgrimes pti->pt_flags |= PF_PKT; 6781541Srgrimes } else 6791541Srgrimes pti->pt_flags &= ~PF_PKT; 6801541Srgrimes return (0); 6811541Srgrimes 6821541Srgrimes case TIOCUCNTL: 6831541Srgrimes if (*(int *)data) { 6841541Srgrimes if (pti->pt_flags & PF_PKT) 6851541Srgrimes return (EINVAL); 6861541Srgrimes pti->pt_flags |= PF_UCNTL; 6871541Srgrimes } else 6881541Srgrimes pti->pt_flags &= ~PF_UCNTL; 6891541Srgrimes return (0); 6901541Srgrimes 6911541Srgrimes case TIOCREMOTE: 6921541Srgrimes if (*(int *)data) 6931541Srgrimes pti->pt_flags |= PF_REMOTE; 6941541Srgrimes else 6951541Srgrimes pti->pt_flags &= ~PF_REMOTE; 6961541Srgrimes ttyflush(tp, FREAD|FWRITE); 6971541Srgrimes return (0); 6981541Srgrimes 6991541Srgrimes#ifdef COMPAT_43 7008876Srgrimes case TIOCSETP: 7011541Srgrimes case TIOCSETN: 7021541Srgrimes#endif 7031541Srgrimes case TIOCSETD: 7041541Srgrimes case TIOCSETA: 7051541Srgrimes case TIOCSETAW: 7069858Sache case TIOCSETAF: 7071541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 7081541Srgrimes break; 7091541Srgrimes 7101541Srgrimes case TIOCSIG: 7111541Srgrimes if (*(unsigned int *)data >= NSIG) 7121541Srgrimes return(EINVAL); 7131541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 7141541Srgrimes ttyflush(tp, FREAD|FWRITE); 7151541Srgrimes pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 7161541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 7171541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 7181541Srgrimes ttyinfo(tp); 7191541Srgrimes return(0); 7201541Srgrimes } 7211541Srgrimes error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 7221541Srgrimes if (error < 0) 7231541Srgrimes error = ttioctl(tp, cmd, data, flag); 7241541Srgrimes if (error < 0) { 7251541Srgrimes if (pti->pt_flags & PF_UCNTL && 7261541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 7271541Srgrimes if (cmd & 0xff) { 7281541Srgrimes pti->pt_ucntl = (u_char)cmd; 7291541Srgrimes ptcwakeup(tp, FREAD); 7301541Srgrimes } 7311541Srgrimes return (0); 7321541Srgrimes } 7331541Srgrimes error = ENOTTY; 7341541Srgrimes } 7351541Srgrimes /* 7361541Srgrimes * If external processing and packet mode send ioctl packet. 7371541Srgrimes */ 7381541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 7391541Srgrimes switch(cmd) { 7401541Srgrimes case TIOCSETA: 7411541Srgrimes case TIOCSETAW: 7421541Srgrimes case TIOCSETAF: 7431541Srgrimes#ifdef COMPAT_43 7441541Srgrimes case TIOCSETP: 7451541Srgrimes case TIOCSETN: 7461541Srgrimes#endif 7471541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 7481541Srgrimes case TIOCSETC: 7491541Srgrimes case TIOCSLTC: 7501541Srgrimes case TIOCLBIS: 7511541Srgrimes case TIOCLBIC: 7521541Srgrimes case TIOCLSET: 7531541Srgrimes#endif 7541541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 7551541Srgrimes ptcwakeup(tp, FREAD); 7561541Srgrimes default: 7571541Srgrimes break; 7581541Srgrimes } 7591541Srgrimes } 7608876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 7611541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 7621541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 7631541Srgrimes if (stop) { 7641541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 7651541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 7661541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 7671541Srgrimes ptcwakeup(tp, FREAD); 7681541Srgrimes } 7691541Srgrimes } else { 7701541Srgrimes if (!stop) { 7711541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 7721541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 7731541Srgrimes pti->pt_flags |= PF_NOSTOP; 7741541Srgrimes ptcwakeup(tp, FREAD); 7751541Srgrimes } 7761541Srgrimes } 7771541Srgrimes return (error); 7781541Srgrimes} 77912517Sjulian 78012517Sjulianstatic ptc_devsw_installed = 0; 78112675Sjulian#ifdef DEVFS 78212675Sjulian#define MAXUNITS (8 * 32) 78312675Sjulianstatic void *devfs_token_pts[MAXUNITS]; 78412675Sjulianstatic void *devfs_token_ptc[MAXUNITS]; 78512819Sphkstatic const char jnames[] = "pqrsPQRS"; 78612675Sjulian#endif 78712517Sjulian 78812675Sjulianstatic void 78912675Sjulianptc_drvinit(void *unused) 79012517Sjulian{ 79112517Sjulian#ifdef DEVFS 79212567Sjulian int i,j,k; 79312517Sjulian#endif 79412517Sjulian dev_t dev; 79512517Sjulian 79612517Sjulian if( ! ptc_devsw_installed ) { 79712675Sjulian dev = makedev(CDEV_MAJOR_S, 0); 79812675Sjulian cdevsw_add(&dev, &pts_cdevsw, NULL); 79912675Sjulian dev = makedev(CDEV_MAJOR_C, 0); 80012675Sjulian cdevsw_add(&dev, &ptc_cdevsw, NULL); 80112517Sjulian ptc_devsw_installed = 1; 80212517Sjulian#ifdef DEVFS 80312517Sjulian/*XXX*/ 80412517Sjulian#if NPTY > MAXUNITS 80512517Sjulian#undef NPTY 80612517Sjulian#define NPTY MAXUNITS 80712517Sjulian#endif 80812521Sjulian for ( i = 0 ; i<NPTY ; i++ ) { 80912564Sjulian j = i / 32; 81012564Sjulian k = i % 32; 81112675Sjulian devfs_token_pts[i] = 81213630Sphk devfs_add_devswf(&pts_cdevsw,i, 81313630Sphk DV_CHR,0,0,0666, 81413639Sphk "tty%c%n",jnames[j],k); 81512675Sjulian devfs_token_ptc[i] = 81613630Sphk devfs_add_devswf(&ptc_cdevsw,i, 81713630Sphk DV_CHR,0,0,0666, 81813639Sphk "pty%c%n",jnames[j],k); 81912521Sjulian } 82012521Sjulian#endif 82112517Sjulian } 82212517Sjulian} 82312517Sjulian 82412517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL) 825