pty.c revision 29354
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 3429354Speter * $Id: tty_pty.c,v 1.45 1997/09/02 20:05:55 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> 4524207Sbde#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 4624207Sbde#include <sys/ioctl_compat.h> 4724207Sbde#endif 481541Srgrimes#include <sys/proc.h> 491541Srgrimes#include <sys/tty.h> 501541Srgrimes#include <sys/conf.h> 5124131Sbde#include <sys/fcntl.h> 5229354Speter#include <sys/poll.h> 531541Srgrimes#include <sys/kernel.h> 541541Srgrimes#include <sys/vnode.h> 553308Sphk#include <sys/signalvar.h> 561541Srgrimes 5712517Sjulian#ifdef DEVFS 5812517Sjulian#include <sys/devfsext.h> 5912517Sjulian#endif /*DEVFS*/ 6012517Sjulian 6116322Sgpalmer#ifdef notyet 6212819Sphkstatic void ptyattach __P((int n)); 6316322Sgpalmer#endif 6412819Sphkstatic void ptsstart __P((struct tty *tp)); 6512819Sphkstatic void ptcwakeup __P((struct tty *tp, int flag)); 6611789Sbde 6712675Sjulianstatic d_open_t ptsopen; 6812675Sjulianstatic d_close_t ptsclose; 6912675Sjulianstatic d_read_t ptsread; 7012675Sjulianstatic d_write_t ptswrite; 7112675Sjulianstatic d_ioctl_t ptyioctl; 7212675Sjulianstatic d_stop_t ptsstop; 7312731Sbdestatic d_devtotty_t ptydevtotty; 7412675Sjulianstatic d_open_t ptcopen; 7512675Sjulianstatic d_close_t ptcclose; 7612675Sjulianstatic d_read_t ptcread; 7712675Sjulianstatic d_write_t ptcwrite; 7829354Speterstatic d_poll_t ptcpoll; 7912675Sjulian 8012675Sjulian#define CDEV_MAJOR_S 5 8112675Sjulian#define CDEV_MAJOR_C 6 8212678Sphkstatic struct cdevsw pts_cdevsw = 8312675Sjulian { ptsopen, ptsclose, ptsread, ptswrite, /*5*/ 8412675Sjulian ptyioctl, ptsstop, nullreset, ptydevtotty,/* ttyp */ 8529354Speter ttpoll, nommap, NULL, "pts", NULL, -1 }; 8612675Sjulian 8712678Sphkstatic struct cdevsw ptc_cdevsw = 8812675Sjulian { ptcopen, ptcclose, ptcread, ptcwrite, /*6*/ 8912675Sjulian ptyioctl, nullstop, nullreset, ptydevtotty,/* ptyp */ 9029354Speter ptcpoll, nommap, NULL, "ptc", NULL, -1 }; 9112675Sjulian 9212675Sjulian 931541Srgrimes#if NPTY == 1 941541Srgrimes#undef NPTY 951541Srgrimes#define NPTY 32 /* crude XXX */ 9627770Sjmg#warning You have only one pty defined, redefining to 32. 971541Srgrimes#endif 981541Srgrimes 9927770Sjmg#ifdef DEVFS 10027770Sjmg#define MAXUNITS (8 * 32) 10127770Sjmgstatic void *devfs_token_pts[MAXUNITS]; 10227770Sjmgstatic void *devfs_token_ptc[MAXUNITS]; 10327770Sjmgstatic const char jnames[] = "pqrsPQRS"; 10427770Sjmg#if NPTY > MAXUNITS 10527770Sjmg#undef NPTY 10627770Sjmg#define NPTY MAXUNITS 10727770Sjmg#warning Can't have more than 256 pty's with DEVFS defined. 10827770Sjmg#endif 10927770Sjmg#endif 11027770Sjmg 1111541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 1121541Srgrimes 1131541Srgrimes/* 11412564Sjulian * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 11512564Sjulian * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 1161541Srgrimes */ 11712819Sphkstatic struct tty pt_tty[NPTY]; /* XXX */ 11812819Sphkstatic struct pt_ioctl { 1191541Srgrimes int pt_flags; 1201541Srgrimes struct selinfo pt_selr, pt_selw; 1211541Srgrimes u_char pt_send; 1221541Srgrimes u_char pt_ucntl; 1231541Srgrimes} pt_ioctl[NPTY]; /* XXX */ 12412819Sphkstatic int npty = NPTY; /* for pstat -t */ 1251541Srgrimes 1261541Srgrimes#define PF_PKT 0x08 /* packet mode */ 1271541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 1281541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 1291541Srgrimes#define PF_NOSTOP 0x40 1301541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 1311541Srgrimes 13216322Sgpalmer#ifdef notyet 1331541Srgrimes/* 1341541Srgrimes * Establish n (or default if n is 1) ptys in the system. 1351541Srgrimes * 1361541Srgrimes * XXX cdevsw & pstat require the array `pty[]' to be an array 1371541Srgrimes */ 13812819Sphkstatic void 1391541Srgrimesptyattach(n) 1401541Srgrimes int n; 1411541Srgrimes{ 1421541Srgrimes char *mem; 1431541Srgrimes register u_long ntb; 1441541Srgrimes#define DEFAULT_NPTY 32 1451541Srgrimes 1461541Srgrimes /* maybe should allow 0 => none? */ 1471541Srgrimes if (n <= 1) 1481541Srgrimes n = DEFAULT_NPTY; 1491541Srgrimes ntb = n * sizeof(struct tty); 1501541Srgrimes mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl), 1511541Srgrimes M_DEVBUF, M_WAITOK); 1521541Srgrimes pt_tty = (struct tty *)mem; 1531541Srgrimes mem = (char *)ALIGN(mem + ntb); 1541541Srgrimes pt_ioctl = (struct pt_ioctl *)mem; 1551541Srgrimes npty = n; 15616322Sgpalmer} 1571541Srgrimes#endif 1581541Srgrimes 1591541Srgrimes/*ARGSUSED*/ 16012675Sjulianstatic int 1611541Srgrimesptsopen(dev, flag, devtype, p) 1621541Srgrimes dev_t dev; 1631541Srgrimes int flag, devtype; 1641541Srgrimes struct proc *p; 1651541Srgrimes{ 1661541Srgrimes register struct tty *tp; 1671541Srgrimes int error; 1681541Srgrimes 1691541Srgrimes if (minor(dev) >= npty) 1701541Srgrimes return (ENXIO); 1711541Srgrimes tp = &pt_tty[minor(dev)]; 1721541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 1731541Srgrimes ttychars(tp); /* Set up default chars */ 1741541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 1751541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 1761541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 1771541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 1781541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 1791541Srgrimes ttsetwater(tp); /* would be done in xxparam() */ 1801541Srgrimes } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) 1811541Srgrimes return (EBUSY); 1821541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 1839824Sbde (void)(*linesw[tp->t_line].l_modem)(tp, 1); 1841541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 1851541Srgrimes if (flag&FNONBLOCK) 1861541Srgrimes break; 1879639Sbde error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1889624Sbde "ptsopn", 0); 1893308Sphk if (error) 1901541Srgrimes return (error); 1911541Srgrimes } 1921541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 1937724Sache if (error == 0) 1947724Sache ptcwakeup(tp, FREAD|FWRITE); 1951541Srgrimes return (error); 1961541Srgrimes} 1971541Srgrimes 19812675Sjulianstatic int 1991541Srgrimesptsclose(dev, flag, mode, p) 2001541Srgrimes dev_t dev; 2011541Srgrimes int flag, mode; 2021541Srgrimes struct proc *p; 2031541Srgrimes{ 2041541Srgrimes register struct tty *tp; 2051541Srgrimes int err; 2061541Srgrimes 2071541Srgrimes tp = &pt_tty[minor(dev)]; 2081541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 2097730Sache ptsstop(tp, FREAD|FWRITE); 2107724Sache (void) ttyclose(tp); 2111541Srgrimes return (err); 2121541Srgrimes} 2131541Srgrimes 21412675Sjulianstatic int 2151541Srgrimesptsread(dev, uio, flag) 2161541Srgrimes dev_t dev; 2171541Srgrimes struct uio *uio; 2181541Srgrimes int flag; 2191541Srgrimes{ 2201541Srgrimes struct proc *p = curproc; 2211541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 2221541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 2231541Srgrimes int error = 0; 2241541Srgrimes 2251541Srgrimesagain: 2261541Srgrimes if (pti->pt_flags & PF_REMOTE) { 2271541Srgrimes while (isbackground(p, tp)) { 2281541Srgrimes if ((p->p_sigignore & sigmask(SIGTTIN)) || 2291541Srgrimes (p->p_sigmask & sigmask(SIGTTIN)) || 2301541Srgrimes p->p_pgrp->pg_jobc == 0 || 2311541Srgrimes p->p_flag & P_PPWAIT) 2321541Srgrimes return (EIO); 2331541Srgrimes pgsignal(p->p_pgrp, SIGTTIN, 1); 2349624Sbde error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 2359624Sbde 0); 2363308Sphk if (error) 2371541Srgrimes return (error); 2381541Srgrimes } 2391541Srgrimes if (tp->t_canq.c_cc == 0) { 2401541Srgrimes if (flag & IO_NDELAY) 2411541Srgrimes return (EWOULDBLOCK); 2429639Sbde error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 2439624Sbde "ptsin", 0); 2443308Sphk if (error) 2451541Srgrimes return (error); 2461541Srgrimes goto again; 2471541Srgrimes } 2481541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2491541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2501541Srgrimes error = EFAULT; 2511541Srgrimes break; 2521541Srgrimes } 2531541Srgrimes if (tp->t_canq.c_cc == 1) 2541541Srgrimes (void) getc(&tp->t_canq); 2551541Srgrimes if (tp->t_canq.c_cc) 2561541Srgrimes return (error); 2571541Srgrimes } else 2581541Srgrimes if (tp->t_oproc) 2591541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2601541Srgrimes ptcwakeup(tp, FWRITE); 2611541Srgrimes return (error); 2621541Srgrimes} 2631541Srgrimes 2641541Srgrimes/* 2651541Srgrimes * Write to pseudo-tty. 2661541Srgrimes * Wakeups of controlling tty will happen 2671541Srgrimes * indirectly, when tty driver calls ptsstart. 2681541Srgrimes */ 26912675Sjulianstatic int 2701541Srgrimesptswrite(dev, uio, flag) 2711541Srgrimes dev_t dev; 2721541Srgrimes struct uio *uio; 2731541Srgrimes int flag; 2741541Srgrimes{ 2751541Srgrimes register struct tty *tp; 2761541Srgrimes 2771541Srgrimes tp = &pt_tty[minor(dev)]; 2781541Srgrimes if (tp->t_oproc == 0) 2791541Srgrimes return (EIO); 2801541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2811541Srgrimes} 2821541Srgrimes 2831541Srgrimes/* 2841541Srgrimes * Start output on pseudo-tty. 2851541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 2861541Srgrimes */ 28712819Sphkstatic void 2881541Srgrimesptsstart(tp) 2891541Srgrimes struct tty *tp; 2901541Srgrimes{ 2911541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 2921541Srgrimes 2931541Srgrimes if (tp->t_state & TS_TTSTOP) 2941541Srgrimes return; 2951541Srgrimes if (pti->pt_flags & PF_STOPPED) { 2961541Srgrimes pti->pt_flags &= ~PF_STOPPED; 2971541Srgrimes pti->pt_send = TIOCPKT_START; 2981541Srgrimes } 2991541Srgrimes ptcwakeup(tp, FREAD); 3001541Srgrimes} 3011541Srgrimes 30212819Sphkstatic void 3031541Srgrimesptcwakeup(tp, flag) 3041541Srgrimes struct tty *tp; 3051541Srgrimes int flag; 3061541Srgrimes{ 3071541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 3081541Srgrimes 3091541Srgrimes if (flag & FREAD) { 3101541Srgrimes selwakeup(&pti->pt_selr); 3119639Sbde wakeup(TSA_PTC_READ(tp)); 3121541Srgrimes } 3131541Srgrimes if (flag & FWRITE) { 3141541Srgrimes selwakeup(&pti->pt_selw); 3159639Sbde wakeup(TSA_PTC_WRITE(tp)); 3161541Srgrimes } 3171541Srgrimes} 3181541Srgrimes 31912675Sjulianstatic int 3201541Srgrimesptcopen(dev, flag, devtype, p) 3211541Srgrimes dev_t dev; 3221541Srgrimes int flag, devtype; 3231541Srgrimes struct proc *p; 3241541Srgrimes{ 3251541Srgrimes register struct tty *tp; 3261541Srgrimes struct pt_ioctl *pti; 3271541Srgrimes 3281541Srgrimes if (minor(dev) >= npty) 3291541Srgrimes return (ENXIO); 3301541Srgrimes tp = &pt_tty[minor(dev)]; 3311541Srgrimes if (tp->t_oproc) 3321541Srgrimes return (EIO); 3331541Srgrimes tp->t_oproc = ptsstart; 3341541Srgrimes#ifdef sun4c 3351541Srgrimes tp->t_stop = ptsstop; 3361541Srgrimes#endif 3371541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 3381541Srgrimes tp->t_lflag &= ~EXTPROC; 3391541Srgrimes pti = &pt_ioctl[minor(dev)]; 3401541Srgrimes pti->pt_flags = 0; 3411541Srgrimes pti->pt_send = 0; 3421541Srgrimes pti->pt_ucntl = 0; 3431541Srgrimes return (0); 3441541Srgrimes} 3451541Srgrimes 34612675Sjulianstatic int 34710624Sbdeptcclose(dev, flags, fmt, p) 3481541Srgrimes dev_t dev; 34910624Sbde int flags; 35010624Sbde int fmt; 35110624Sbde struct proc *p; 3521541Srgrimes{ 3531541Srgrimes register struct tty *tp; 3541541Srgrimes 3551541Srgrimes tp = &pt_tty[minor(dev)]; 3561541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3579824Sbde 3589824Sbde /* 3599824Sbde * XXX MDMBUF makes no sense for ptys but would inhibit the above 3609824Sbde * l_modem(). CLOCAL makes sense but isn't supported. Special 3619824Sbde * l_modem()s that ignore carrier drop make no sense for ptys but 3629824Sbde * may be in use because other parts of the line discipline make 3639824Sbde * sense for ptys. Recover by doing everything that a normal 3649824Sbde * ttymodem() would have done except for sending a SIGHUP. 3659824Sbde */ 3669850Sbde if (tp->t_state & TS_ISOPEN) { 3679850Sbde tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 3689850Sbde tp->t_state |= TS_ZOMBIE; 3699850Sbde ttyflush(tp, FREAD | FWRITE); 3709850Sbde } 3719824Sbde 3721541Srgrimes tp->t_oproc = 0; /* mark closed */ 3731541Srgrimes return (0); 3741541Srgrimes} 3751541Srgrimes 37612675Sjulianstatic int 3771541Srgrimesptcread(dev, uio, flag) 3781541Srgrimes dev_t dev; 3791541Srgrimes struct uio *uio; 3801541Srgrimes int flag; 3811541Srgrimes{ 3821541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 3831541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 3841541Srgrimes char buf[BUFSIZ]; 3851541Srgrimes int error = 0, cc; 3861541Srgrimes 3871541Srgrimes /* 3881541Srgrimes * We want to block until the slave 3891541Srgrimes * is open, and there's something to read; 3901541Srgrimes * but if we lost the slave or we're NBIO, 3911541Srgrimes * then return the appropriate error instead. 3921541Srgrimes */ 3931541Srgrimes for (;;) { 3941541Srgrimes if (tp->t_state&TS_ISOPEN) { 3951541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 3961541Srgrimes error = ureadc((int)pti->pt_send, uio); 3971541Srgrimes if (error) 3981541Srgrimes return (error); 3991541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 4001541Srgrimes cc = min(uio->uio_resid, 4011541Srgrimes sizeof(tp->t_termios)); 4022807Sbde uiomove((caddr_t)&tp->t_termios, cc, 4032807Sbde uio); 4041541Srgrimes } 4051541Srgrimes pti->pt_send = 0; 4061541Srgrimes return (0); 4071541Srgrimes } 4081541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 4091541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 4101541Srgrimes if (error) 4111541Srgrimes return (error); 4121541Srgrimes pti->pt_ucntl = 0; 4131541Srgrimes return (0); 4141541Srgrimes } 4151541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 4161541Srgrimes break; 4171541Srgrimes } 4189824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 4191541Srgrimes return (0); /* EOF */ 4201541Srgrimes if (flag & IO_NDELAY) 4211541Srgrimes return (EWOULDBLOCK); 4229639Sbde error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 4233308Sphk if (error) 4241541Srgrimes return (error); 4251541Srgrimes } 4261541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 4271541Srgrimes error = ureadc(0, uio); 4281541Srgrimes while (uio->uio_resid > 0 && error == 0) { 4291541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 4301541Srgrimes if (cc <= 0) 4311541Srgrimes break; 4321541Srgrimes error = uiomove(buf, cc, uio); 4331541Srgrimes } 4349626Sbde ttwwakeup(tp); 4351541Srgrimes return (error); 4361541Srgrimes} 4371541Srgrimes 43812675Sjulianstatic void 4391541Srgrimesptsstop(tp, flush) 4401541Srgrimes register struct tty *tp; 4411541Srgrimes int flush; 4421541Srgrimes{ 4431541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 4441541Srgrimes int flag; 4451541Srgrimes 4461541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 4471541Srgrimes if (flush == 0) { 4481541Srgrimes flush = TIOCPKT_STOP; 4491541Srgrimes pti->pt_flags |= PF_STOPPED; 4501541Srgrimes } else 4511541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4521541Srgrimes pti->pt_send |= flush; 4531541Srgrimes /* change of perspective */ 4541541Srgrimes flag = 0; 4551541Srgrimes if (flush & FREAD) 4561541Srgrimes flag |= FWRITE; 4571541Srgrimes if (flush & FWRITE) 4581541Srgrimes flag |= FREAD; 4591541Srgrimes ptcwakeup(tp, flag); 4601541Srgrimes} 4611541Srgrimes 46212675Sjulianstatic int 46329354Speterptcpoll(dev, events, p) 4641541Srgrimes dev_t dev; 46529354Speter int events; 4661541Srgrimes struct proc *p; 4671541Srgrimes{ 4681541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 4691541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 47029354Speter int revents = 0; 4711541Srgrimes int s; 4721541Srgrimes 4739824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 47429354Speter return (seltrue(dev, events, p) | POLLHUP); 4751541Srgrimes 47629354Speter /* 47729354Speter * Need to block timeouts (ttrstart). 47829354Speter */ 47929354Speter s = spltty(); 4801541Srgrimes 48129354Speter if (events & (POLLIN | POLLRDNORM)) 48229354Speter if ((tp->t_state & TS_ISOPEN) && 48329354Speter ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || 48429354Speter ((pti->pt_flags & PF_PKT) && pti->pt_send) || 48529354Speter ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 48629354Speter revents |= events & (POLLIN | POLLRDNORM); 4871541Srgrimes 48829354Speter if (events & (POLLOUT | POLLWRNORM)) 48929354Speter if (tp->t_state & TS_ISOPEN && 49029354Speter ((pti->pt_flags & PF_REMOTE) ? 49129354Speter (tp->t_canq.c_cc == 0) : 49229354Speter ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 49329354Speter (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON))))) 49429354Speter revents |= events & (POLLOUT | POLLWRNORM); 4951541Srgrimes 49629354Speter if (events & POLLHUP) 49729354Speter if ((tp->t_state & TS_CARR_ON) == 0) 49829354Speter revents |= POLLHUP; 4991541Srgrimes 50029354Speter if (revents == 0) { 50129354Speter if (events & (POLLIN | POLLRDNORM)) 50229354Speter selrecord(p, &pti->pt_selr); 50329354Speter 50429354Speter if (events & (POLLOUT | POLLWRNORM)) 50529354Speter selrecord(p, &pti->pt_selw); 5061541Srgrimes } 50729354Speter splx(s); 50829354Speter 50929354Speter return (revents); 5101541Srgrimes} 5111541Srgrimes 51212675Sjulianstatic int 5131541Srgrimesptcwrite(dev, uio, flag) 5141541Srgrimes dev_t dev; 5151541Srgrimes register struct uio *uio; 5161541Srgrimes int flag; 5171541Srgrimes{ 5181541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 5191549Srgrimes register u_char *cp = 0; 5201541Srgrimes register int cc = 0; 5211541Srgrimes u_char locbuf[BUFSIZ]; 5221541Srgrimes int cnt = 0; 5231541Srgrimes struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 5241541Srgrimes int error = 0; 5251541Srgrimes 5261541Srgrimesagain: 5271541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 5281541Srgrimes goto block; 5291541Srgrimes if (pti->pt_flags & PF_REMOTE) { 5301541Srgrimes if (tp->t_canq.c_cc) 5311541Srgrimes goto block; 53211789Sbde while ((uio->uio_resid > 0 || cc > 0) && 53311789Sbde tp->t_canq.c_cc < TTYHOG - 1) { 5341541Srgrimes if (cc == 0) { 5351541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5361541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 5371541Srgrimes cp = locbuf; 5381541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5391541Srgrimes if (error) 5401541Srgrimes return (error); 5411541Srgrimes /* check again for safety */ 54211789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 54311789Sbde /* adjust as usual */ 54411789Sbde uio->uio_resid += cc; 5451541Srgrimes return (EIO); 54611789Sbde } 5471541Srgrimes } 54815199Sbde if (cc > 0) { 54915199Sbde cc = b_to_q((char *)cp, cc, &tp->t_canq); 55015199Sbde /* 55115199Sbde * XXX we don't guarantee that the canq size 55215199Sbde * is >= TTYHOG, so the above b_to_q() may 55315199Sbde * leave some bytes uncopied. However, space 55415199Sbde * is guaranteed for the null terminator if 55515199Sbde * we don't fail here since (TTYHOG - 1) is 55615199Sbde * not a multiple of CBSIZE. 55715199Sbde */ 55815199Sbde if (cc > 0) 55915199Sbde break; 56015199Sbde } 5611541Srgrimes } 56211789Sbde /* adjust for data copied in but not written */ 56311789Sbde uio->uio_resid += cc; 5641541Srgrimes (void) putc(0, &tp->t_canq); 5651541Srgrimes ttwakeup(tp); 5669639Sbde wakeup(TSA_PTS_READ(tp)); 5671541Srgrimes return (0); 5681541Srgrimes } 56911789Sbde while (uio->uio_resid > 0 || cc > 0) { 5701541Srgrimes if (cc == 0) { 5711541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5721541Srgrimes cp = locbuf; 5731541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5741541Srgrimes if (error) 5751541Srgrimes return (error); 5761541Srgrimes /* check again for safety */ 57711789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 57811789Sbde /* adjust for data copied in but not written */ 57911789Sbde uio->uio_resid += cc; 5801541Srgrimes return (EIO); 58111789Sbde } 5821541Srgrimes } 5831541Srgrimes while (cc > 0) { 5841541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 5851541Srgrimes (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 5869824Sbde wakeup(TSA_HUP_OR_INPUT(tp)); 5871541Srgrimes goto block; 5881541Srgrimes } 5891541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 5901541Srgrimes cnt++; 5911541Srgrimes cc--; 5921541Srgrimes } 5931541Srgrimes cc = 0; 5941541Srgrimes } 5951541Srgrimes return (0); 5961541Srgrimesblock: 5971541Srgrimes /* 5981541Srgrimes * Come here to wait for slave to open, for space 59915199Sbde * in outq, or space in rawq, or an empty canq. 6001541Srgrimes */ 60111789Sbde if ((tp->t_state & TS_CONNECTED) == 0) { 60211789Sbde /* adjust for data copied in but not written */ 60311789Sbde uio->uio_resid += cc; 6041541Srgrimes return (EIO); 60511789Sbde } 6061541Srgrimes if (flag & IO_NDELAY) { 6071541Srgrimes /* adjust for data copied in but not written */ 6081541Srgrimes uio->uio_resid += cc; 6091541Srgrimes if (cnt == 0) 6101541Srgrimes return (EWOULDBLOCK); 6111541Srgrimes return (0); 6121541Srgrimes } 6139639Sbde error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 6143308Sphk if (error) { 6151541Srgrimes /* adjust for data copied in but not written */ 6161541Srgrimes uio->uio_resid += cc; 6171541Srgrimes return (error); 6181541Srgrimes } 6191541Srgrimes goto again; 6201541Srgrimes} 6211541Srgrimes 62212675Sjulianstatic struct tty * 6236712Spstptydevtotty(dev) 6246712Spst dev_t dev; 6256712Spst{ 6266712Spst if (minor(dev) >= npty) 6276712Spst return (NULL); 6286712Spst 6296712Spst return &pt_tty[minor(dev)]; 6306712Spst} 6316712Spst 6321541Srgrimes/*ARGSUSED*/ 63312675Sjulianstatic int 6341541Srgrimesptyioctl(dev, cmd, data, flag, p) 6351541Srgrimes dev_t dev; 6361541Srgrimes int cmd; 6371541Srgrimes caddr_t data; 6381541Srgrimes int flag; 6391541Srgrimes struct proc *p; 6401541Srgrimes{ 6411541Srgrimes register struct tty *tp = &pt_tty[minor(dev)]; 6421541Srgrimes register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 6431541Srgrimes register u_char *cc = tp->t_cc; 6441541Srgrimes int stop, error; 6451541Srgrimes 6461541Srgrimes /* 6471541Srgrimes * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 6481541Srgrimes * ttywflush(tp) will hang if there are characters in the outq. 6491541Srgrimes */ 6501541Srgrimes if (cmd == TIOCEXT) { 6511541Srgrimes /* 6521541Srgrimes * When the EXTPROC bit is being toggled, we need 6531541Srgrimes * to send an TIOCPKT_IOCTL if the packet driver 6541541Srgrimes * is turned on. 6551541Srgrimes */ 6561541Srgrimes if (*(int *)data) { 6571541Srgrimes if (pti->pt_flags & PF_PKT) { 6581541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 6591541Srgrimes ptcwakeup(tp, FREAD); 6601541Srgrimes } 6611541Srgrimes tp->t_lflag |= EXTPROC; 6621541Srgrimes } else { 66314511Shsu if ((tp->t_lflag & EXTPROC) && 6641541Srgrimes (pti->pt_flags & PF_PKT)) { 6651541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 6661541Srgrimes ptcwakeup(tp, FREAD); 6671541Srgrimes } 6681541Srgrimes tp->t_lflag &= ~EXTPROC; 6691541Srgrimes } 6701541Srgrimes return(0); 6711541Srgrimes } else 67212813Sjulian if (cdevsw[major(dev)]->d_open == ptcopen) 6731541Srgrimes switch (cmd) { 6741541Srgrimes 6751541Srgrimes case TIOCGPGRP: 6761541Srgrimes /* 6771541Srgrimes * We aviod calling ttioctl on the controller since, 6781541Srgrimes * in that case, tp must be the controlling terminal. 6791541Srgrimes */ 6801541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6811541Srgrimes return (0); 6821541Srgrimes 6831541Srgrimes case TIOCPKT: 6841541Srgrimes if (*(int *)data) { 6851541Srgrimes if (pti->pt_flags & PF_UCNTL) 6861541Srgrimes return (EINVAL); 6871541Srgrimes pti->pt_flags |= PF_PKT; 6881541Srgrimes } else 6891541Srgrimes pti->pt_flags &= ~PF_PKT; 6901541Srgrimes return (0); 6911541Srgrimes 6921541Srgrimes case TIOCUCNTL: 6931541Srgrimes if (*(int *)data) { 6941541Srgrimes if (pti->pt_flags & PF_PKT) 6951541Srgrimes return (EINVAL); 6961541Srgrimes pti->pt_flags |= PF_UCNTL; 6971541Srgrimes } else 6981541Srgrimes pti->pt_flags &= ~PF_UCNTL; 6991541Srgrimes return (0); 7001541Srgrimes 7011541Srgrimes case TIOCREMOTE: 7021541Srgrimes if (*(int *)data) 7031541Srgrimes pti->pt_flags |= PF_REMOTE; 7041541Srgrimes else 7051541Srgrimes pti->pt_flags &= ~PF_REMOTE; 7061541Srgrimes ttyflush(tp, FREAD|FWRITE); 7071541Srgrimes return (0); 7081541Srgrimes 7091541Srgrimes#ifdef COMPAT_43 7108876Srgrimes case TIOCSETP: 7111541Srgrimes case TIOCSETN: 7121541Srgrimes#endif 7131541Srgrimes case TIOCSETD: 7141541Srgrimes case TIOCSETA: 7151541Srgrimes case TIOCSETAW: 7169858Sache case TIOCSETAF: 7171541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 7181541Srgrimes break; 7191541Srgrimes 7201541Srgrimes case TIOCSIG: 72127770Sjmg if (*(unsigned int *)data >= NSIG || 72227770Sjmg *(unsigned int *)data == 0) 7231541Srgrimes return(EINVAL); 7241541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 7251541Srgrimes ttyflush(tp, FREAD|FWRITE); 7261541Srgrimes pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 7271541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 7281541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 7291541Srgrimes ttyinfo(tp); 7301541Srgrimes return(0); 7311541Srgrimes } 7321541Srgrimes error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 7331541Srgrimes if (error < 0) 7341541Srgrimes error = ttioctl(tp, cmd, data, flag); 7351541Srgrimes if (error < 0) { 7361541Srgrimes if (pti->pt_flags & PF_UCNTL && 7371541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 7381541Srgrimes if (cmd & 0xff) { 7391541Srgrimes pti->pt_ucntl = (u_char)cmd; 7401541Srgrimes ptcwakeup(tp, FREAD); 7411541Srgrimes } 7421541Srgrimes return (0); 7431541Srgrimes } 7441541Srgrimes error = ENOTTY; 7451541Srgrimes } 7461541Srgrimes /* 7471541Srgrimes * If external processing and packet mode send ioctl packet. 7481541Srgrimes */ 7491541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 7501541Srgrimes switch(cmd) { 7511541Srgrimes case TIOCSETA: 7521541Srgrimes case TIOCSETAW: 7531541Srgrimes case TIOCSETAF: 7541541Srgrimes#ifdef COMPAT_43 7551541Srgrimes case TIOCSETP: 7561541Srgrimes case TIOCSETN: 7571541Srgrimes#endif 7581541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 7591541Srgrimes case TIOCSETC: 7601541Srgrimes case TIOCSLTC: 7611541Srgrimes case TIOCLBIS: 7621541Srgrimes case TIOCLBIC: 7631541Srgrimes case TIOCLSET: 7641541Srgrimes#endif 7651541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 7661541Srgrimes ptcwakeup(tp, FREAD); 7671541Srgrimes default: 7681541Srgrimes break; 7691541Srgrimes } 7701541Srgrimes } 7718876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 7721541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 7731541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 7741541Srgrimes if (stop) { 7751541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 7761541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 7771541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 7781541Srgrimes ptcwakeup(tp, FREAD); 7791541Srgrimes } 7801541Srgrimes } else { 7811541Srgrimes if (!stop) { 7821541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 7831541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 7841541Srgrimes pti->pt_flags |= PF_NOSTOP; 7851541Srgrimes ptcwakeup(tp, FREAD); 7861541Srgrimes } 7871541Srgrimes } 7881541Srgrimes return (error); 7891541Srgrimes} 79012517Sjulian 79112517Sjulianstatic ptc_devsw_installed = 0; 79212517Sjulian 79312675Sjulianstatic void 79412675Sjulianptc_drvinit(void *unused) 79512517Sjulian{ 79612517Sjulian#ifdef DEVFS 79712567Sjulian int i,j,k; 79812517Sjulian#endif 79912517Sjulian dev_t dev; 80012517Sjulian 80112517Sjulian if( ! ptc_devsw_installed ) { 80212675Sjulian dev = makedev(CDEV_MAJOR_S, 0); 80312675Sjulian cdevsw_add(&dev, &pts_cdevsw, NULL); 80412675Sjulian dev = makedev(CDEV_MAJOR_C, 0); 80512675Sjulian cdevsw_add(&dev, &ptc_cdevsw, NULL); 80612517Sjulian ptc_devsw_installed = 1; 80712517Sjulian#ifdef DEVFS 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